Skip to content

Commit

Permalink
experimental altruism mode
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnguyen22 committed Oct 7, 2020
1 parent ec834b2 commit f3b7edf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
4 changes: 3 additions & 1 deletion app/cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
keybase bool
mainnet bool
testnet bool
altruism bool
)

var CLIVersion = app.AppVersion
Expand Down Expand Up @@ -56,6 +57,7 @@ func init() {
startCmd.Flags().BoolVar(&keybase, "keybase", true, "run with keybase, if disabled allows you to stake for the current validator only. providing a keybase is still neccesary for staking for apps & sending transactions")
startCmd.Flags().BoolVar(&mainnet, "mainnet", false, "run with mainnet genesis")
startCmd.Flags().BoolVar(&testnet, "testnet", false, "run with testnet genesis")
startCmd.Flags().BoolVar(&altruism, "altruism", false, "run with no relay validation")
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(resetCmd)
rootCmd.AddCommand(version)
Expand All @@ -78,7 +80,7 @@ var startCmd = &cobra.Command{
if testnet {
genesisType = app.TestnetGenesisType
}
tmNode := app.InitApp(datadir, tmNode, persistentPeers, seeds, remoteCLIURL, keybase, genesisType)
tmNode := app.InitApp(datadir, tmNode, persistentPeers, seeds, remoteCLIURL, keybase, genesisType, altruism)
go rpc.StartRPC(app.GlobalConfig.PocketConfig.RPCPort, simulateRelay)
// trap kill signals (2,3,15,9)
signalChannel := make(chan os.Signal, 1)
Expand Down
8 changes: 4 additions & 4 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ func DefaultConfig(dataDir string) Config {
return c
}

func InitApp(datadir, tmNode, persistentPeers, seeds, remoteCLIURL string, keybase bool, genesisType GenesisType) *node.Node {
func InitApp(datadir, tmNode, persistentPeers, seeds, remoteCLIURL string, keybase bool, genesisType GenesisType, altruism bool) *node.Node {
// init config
InitConfig(datadir, tmNode, persistentPeers, seeds, remoteCLIURL)
// init the keyfiles
InitKeyfiles()
// init cache
InitPocketCoreConfig()
InitPocketCoreConfig(altruism)
// init genesis
InitGenesis(genesisType)
// init the tendermint node
Expand Down Expand Up @@ -370,8 +370,8 @@ func InitKeyfiles() {
}
}

func InitPocketCoreConfig() {
types.InitConfig(GlobalConfig.PocketConfig.UserAgent, GlobalConfig.PocketConfig.DataDir, GlobalConfig.PocketConfig.DataDir, GlobalConfig.PocketConfig.SessionDBType, GlobalConfig.PocketConfig.EvidenceDBType, GlobalConfig.PocketConfig.MaxEvidenceCacheEntires, GlobalConfig.PocketConfig.MaxSessionCacheEntries, GlobalConfig.PocketConfig.EvidenceDBName, GlobalConfig.PocketConfig.SessionDBName)
func InitPocketCoreConfig(altruism bool) {
types.InitConfig(GlobalConfig.PocketConfig.UserAgent, GlobalConfig.PocketConfig.DataDir, GlobalConfig.PocketConfig.DataDir, GlobalConfig.PocketConfig.SessionDBType, GlobalConfig.PocketConfig.EvidenceDBType, GlobalConfig.PocketConfig.MaxEvidenceCacheEntires, GlobalConfig.PocketConfig.MaxSessionCacheEntries, GlobalConfig.PocketConfig.EvidenceDBName, GlobalConfig.PocketConfig.SessionDBName, altruism)
types.InitClientBlockAllowance(GlobalConfig.PocketConfig.ClientBlockSyncAllowance)
types.InitJSONSorting(GlobalConfig.PocketConfig.JSONSortRelayResponses)
nodesTypes.InitConfig(GlobalConfig.PocketConfig.ValidatorCacheSize)
Expand Down
56 changes: 29 additions & 27 deletions x/pocketcore/keeper/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,36 @@ import (

// "HandleRelay" - Handles an api (read/write) request to a non-native (external) blockchain
func (k Keeper) HandleRelay(ctx sdk.Ctx, relay pc.Relay) (*pc.RelayResponse, sdk.Error) {
// get the latest session block height because this relay will correspond with the latest session
sessionBlockHeight := k.GetLatestSessionBlockHeight(ctx)
// get self node (your validator) from the current state
selfNode, err := k.GetSelfNode(ctx)
if err != nil {
return nil, err
}
// retrieve the nonNative blockchains your node is hosting
hostedBlockchains := k.GetHostedBlockchains()
// get the application that staked on behalf of the client
app, found := k.GetAppFromPublicKey(ctx, relay.Proof.Token.ApplicationPublicKey)
if !found {
return nil, pc.NewAppNotFoundError(pc.ModuleName)
}
// get the session context
sessionCtx, er := ctx.PrevCtx(sessionBlockHeight)
if er != nil {
return nil, sdk.ErrInternal(er.Error())
}
sessionNodeCount := k.SessionNodeCount(sessionCtx)
// ensure the validity of the relay
maxPossibleRelays, err := relay.Validate(ctx, k.posKeeper, selfNode, hostedBlockchains, sessionBlockHeight, int(sessionNodeCount), app)
if err != nil {
ctx.Logger().Error(fmt.Errorf("could not validate relay for %v, %v, %v %v, %v", selfNode, hostedBlockchains, sessionBlockHeight, int(k.SessionNodeCount(sessionCtx)), app).Error())
return nil, err
if pc.GlobalAltruism == false {
// get the latest session block height because this relay will correspond with the latest session
sessionBlockHeight := k.GetLatestSessionBlockHeight(ctx)
// get self node (your validator) from the current state
selfNode, err := k.GetSelfNode(ctx)
if err != nil {
return nil, err
}
// get the application that staked on behalf of the client
app, found := k.GetAppFromPublicKey(ctx, relay.Proof.Token.ApplicationPublicKey)
if !found {
return nil, pc.NewAppNotFoundError(pc.ModuleName)
}
// get the session context
sessionCtx, er := ctx.PrevCtx(sessionBlockHeight)
if er != nil {
return nil, sdk.ErrInternal(er.Error())
}
sessionNodeCount := k.SessionNodeCount(sessionCtx)
// ensure the validity of the relay
maxPossibleRelays, err := relay.Validate(ctx, k.posKeeper, selfNode, hostedBlockchains, sessionBlockHeight, int(sessionNodeCount), app)
if err != nil {
ctx.Logger().Error(fmt.Errorf("could not validate relay for %v, %v, %v %v, %v", selfNode, hostedBlockchains, sessionBlockHeight, int(k.SessionNodeCount(sessionCtx)), app).Error())
return nil, err
}
// store the proof before execution, because the proof corresponds to the previous relay
relay.Proof.Store(maxPossibleRelays)
}
// store the proof before execution, because the proof corresponds to the previous relay
relay.Proof.Store(maxPossibleRelays)
// attempt to execute
respPayload, err := relay.Execute(hostedBlockchains)
if err != nil {
Expand All @@ -51,13 +53,13 @@ func (k Keeper) HandleRelay(ctx sdk.Ctx, relay pc.Relay) (*pc.RelayResponse, sdk
// get the private key from the private validator file
pk, er := k.GetPKFromFile(ctx)
if er != nil {
ctx.Logger().Error(fmt.Errorf("could not get PK to Sign response for address: %v with hash: %v", selfNode.GetAddress().String(), resp.Hash()).Error())
ctx.Logger().Error(fmt.Errorf("could not get PK to Sign response for address: %v with hash: %v", "XXX", resp.Hash()).Error())
return nil, pc.NewKeybaseError(pc.ModuleName, er)
}
// sign the response
sig, er := pk.Sign(resp.Hash())
if er != nil {
ctx.Logger().Error(fmt.Errorf("could not sign response for address: %v with hash: %v", selfNode.GetAddress().String(), resp.Hash()).Error())
ctx.Logger().Error(fmt.Errorf("could not sign response for address: XXX with hash: %v", resp.Hash()).Error())
return nil, pc.NewKeybaseError(pc.ModuleName, er)
}
// attach the signature in hex to the response
Expand Down
4 changes: 3 additions & 1 deletion x/pocketcore/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (

var (
globalUserAgent string
GlobalAltruism bool
)

// "InitConfig" - Initializes the cache for sessions and evidence
func InitConfig(userAgent, evidenceDir, sessionDir string, sessionDBType, evidenceDBType db.DBBackendType, maxEvidenceEntries, maxSessionEntries int, evidenceDBName, sessionDBName string) {
func InitConfig(userAgent, evidenceDir, sessionDir string, sessionDBType, evidenceDBType db.DBBackendType, maxEvidenceEntries, maxSessionEntries int, evidenceDBName, sessionDBName string, altruism bool) {
cacheOnce.Do(func() {
globalEvidenceCache = new(CacheStorage)
globalSessionCache = new(CacheStorage)
globalEvidenceCache.Init(evidenceDir, evidenceDBName, evidenceDBType, maxEvidenceEntries)
globalSessionCache.Init(sessionDir, sessionDBName, sessionDBType, maxSessionEntries)
})
globalUserAgent = userAgent
GlobalAltruism = altruism
}

func FlushCache() {
Expand Down

0 comments on commit f3b7edf

Please sign in to comment.