From e61d7016b41ffc89d1a78e7e86d1b0c538e2fddd Mon Sep 17 00:00:00 2001 From: poktblade Date: Wed, 15 Mar 2023 23:17:45 -0500 Subject: [PATCH 1/6] initial session rollover implemenation [squashed] --- types/config.go | 121 +++++++++++++++++---------------- types/utils.go | 4 ++ x/pocketcore/keeper/service.go | 10 ++- x/pocketcore/keeper/session.go | 12 ++++ 4 files changed, 86 insertions(+), 61 deletions(-) diff --git a/types/config.go b/types/config.go index 83dde1dc9..ab34943d5 100644 --- a/types/config.go +++ b/types/config.go @@ -18,36 +18,37 @@ type SDKConfig struct { } type PocketConfig struct { - DataDir string `json:"data_dir"` - GenesisName string `json:"genesis_file"` - ChainsName string `json:"chains_name"` - EvidenceDBName string `json:"evidence_db_name"` - TendermintURI string `json:"tendermint_uri"` - KeybaseName string `json:"keybase_name"` - RPCPort string `json:"rpc_port"` - ClientBlockSyncAllowance int `json:"client_block_sync_allowance"` - MaxEvidenceCacheEntires int `json:"max_evidence_cache_entries"` - MaxSessionCacheEntries int `json:"max_session_cache_entries"` - JSONSortRelayResponses bool `json:"json_sort_relay_responses"` - RemoteCLIURL string `json:"remote_cli_url"` - UserAgent string `json:"user_agent"` - ValidatorCacheSize int64 `json:"validator_cache_size"` - ApplicationCacheSize int64 `json:"application_cache_size"` - RPCTimeout int64 `json:"rpc_timeout"` - PrometheusAddr string `json:"pocket_prometheus_port"` - PrometheusMaxOpenfiles int `json:"prometheus_max_open_files"` - MaxClaimAgeForProofRetry int `json:"max_claim_age_for_proof_retry"` - ProofPrevalidation bool `json:"proof_prevalidation"` - CtxCacheSize int `json:"ctx_cache_size"` - ABCILogging bool `json:"abci_logging"` - RelayErrors bool `json:"show_relay_errors"` - DisableTxEvents bool `json:"disable_tx_events"` - Cache bool `json:"-"` - IavlCacheSize int64 `json:"iavl_cache_size"` - ChainsHotReload bool `json:"chains_hot_reload"` - GenerateTokenOnStart bool `json:"generate_token_on_start"` - LeanPocket bool `json:"lean_pocket"` - LeanPocketUserKeyFileName string `json:"lean_pocket_user_key_file"` + DataDir string `json:"data_dir"` + GenesisName string `json:"genesis_file"` + ChainsName string `json:"chains_name"` + EvidenceDBName string `json:"evidence_db_name"` + TendermintURI string `json:"tendermint_uri"` + KeybaseName string `json:"keybase_name"` + RPCPort string `json:"rpc_port"` + ClientBlockSyncAllowance int `json:"client_block_sync_allowance"` + ClientSessionBlockSyncAllowance int64 `json:"client_session_block_sync_allowance"` + MaxEvidenceCacheEntires int `json:"max_evidence_cache_entries"` + MaxSessionCacheEntries int `json:"max_session_cache_entries"` + JSONSortRelayResponses bool `json:"json_sort_relay_responses"` + RemoteCLIURL string `json:"remote_cli_url"` + UserAgent string `json:"user_agent"` + ValidatorCacheSize int64 `json:"validator_cache_size"` + ApplicationCacheSize int64 `json:"application_cache_size"` + RPCTimeout int64 `json:"rpc_timeout"` + PrometheusAddr string `json:"pocket_prometheus_port"` + PrometheusMaxOpenfiles int `json:"prometheus_max_open_files"` + MaxClaimAgeForProofRetry int `json:"max_claim_age_for_proof_retry"` + ProofPrevalidation bool `json:"proof_prevalidation"` + CtxCacheSize int `json:"ctx_cache_size"` + ABCILogging bool `json:"abci_logging"` + RelayErrors bool `json:"show_relay_errors"` + DisableTxEvents bool `json:"disable_tx_events"` + Cache bool `json:"-"` + IavlCacheSize int64 `json:"iavl_cache_size"` + ChainsHotReload bool `json:"chains_hot_reload"` + GenerateTokenOnStart bool `json:"generate_token_on_start"` + LeanPocket bool `json:"lean_pocket"` + LeanPocketUserKeyFileName string `json:"lean_pocket_user_key_file"` } func (c PocketConfig) GetLeanPocketUserKeyFilePath() string { @@ -82,6 +83,7 @@ const ( DefaultMaxEvidenceCacheEntries = 500 DefaultListenAddr = "tcp://0.0.0.0:" DefaultClientBlockSyncAllowance = 10 + DefaultSessionBlockSyncAllowance = 1 // 1 session (irrespective of num blocks per session) DefaultJSONSortRelayResponses = true DefaultTxIndexer = "kv" DefaultRPCDisableTransactionEvents = true @@ -117,35 +119,36 @@ func DefaultConfig(dataDir string) Config { c := Config{ TendermintConfig: *config.DefaultConfig(), PocketConfig: PocketConfig{ - DataDir: dataDir, - GenesisName: DefaultGenesisName, - ChainsName: DefaultChainsName, - EvidenceDBName: DefaultEvidenceDBName, - TendermintURI: DefaultTMURI, - KeybaseName: DefaultKeybaseName, - RPCPort: DefaultRPCPort, - ClientBlockSyncAllowance: DefaultClientBlockSyncAllowance, - MaxEvidenceCacheEntires: DefaultMaxEvidenceCacheEntries, - MaxSessionCacheEntries: DefaultMaxSessionCacheEntries, - JSONSortRelayResponses: DefaultJSONSortRelayResponses, - RemoteCLIURL: DefaultRemoteCLIURL, - UserAgent: DefaultUserAgent, - ValidatorCacheSize: DefaultValidatorCacheSize, - ApplicationCacheSize: DefaultApplicationCacheSize, - RPCTimeout: DefaultRPCTimeout, - PrometheusAddr: DefaultPocketPrometheusListenAddr, - PrometheusMaxOpenfiles: DefaultPrometheusMaxOpenFile, - MaxClaimAgeForProofRetry: DefaultMaxClaimProofRetryAge, - ProofPrevalidation: DefaultProofPrevalidation, - CtxCacheSize: DefaultCtxCacheSize, - ABCILogging: DefaultABCILogging, - RelayErrors: DefaultRelayErrors, - DisableTxEvents: DefaultRPCDisableTransactionEvents, - IavlCacheSize: DefaultIavlCacheSize, - ChainsHotReload: DefaultChainHotReload, - GenerateTokenOnStart: DefaultGenerateTokenOnStart, - LeanPocket: DefaultLeanPocket, - LeanPocketUserKeyFileName: DefaultLeanPocketUserKeyFileName, + DataDir: dataDir, + GenesisName: DefaultGenesisName, + ChainsName: DefaultChainsName, + EvidenceDBName: DefaultEvidenceDBName, + TendermintURI: DefaultTMURI, + KeybaseName: DefaultKeybaseName, + RPCPort: DefaultRPCPort, + ClientBlockSyncAllowance: DefaultClientBlockSyncAllowance, + ClientSessionBlockSyncAllowance: DefaultSessionBlockSyncAllowance, + MaxEvidenceCacheEntires: DefaultMaxEvidenceCacheEntries, + MaxSessionCacheEntries: DefaultMaxSessionCacheEntries, + JSONSortRelayResponses: DefaultJSONSortRelayResponses, + RemoteCLIURL: DefaultRemoteCLIURL, + UserAgent: DefaultUserAgent, + ValidatorCacheSize: DefaultValidatorCacheSize, + ApplicationCacheSize: DefaultApplicationCacheSize, + RPCTimeout: DefaultRPCTimeout, + PrometheusAddr: DefaultPocketPrometheusListenAddr, + PrometheusMaxOpenfiles: DefaultPrometheusMaxOpenFile, + MaxClaimAgeForProofRetry: DefaultMaxClaimProofRetryAge, + ProofPrevalidation: DefaultProofPrevalidation, + CtxCacheSize: DefaultCtxCacheSize, + ABCILogging: DefaultABCILogging, + RelayErrors: DefaultRelayErrors, + DisableTxEvents: DefaultRPCDisableTransactionEvents, + IavlCacheSize: DefaultIavlCacheSize, + ChainsHotReload: DefaultChainHotReload, + GenerateTokenOnStart: DefaultGenerateTokenOnStart, + LeanPocket: DefaultLeanPocket, + LeanPocketUserKeyFileName: DefaultLeanPocketUserKeyFileName, }, } c.TendermintConfig.LevelDBOptions = config.DefaultLevelDBOpts() diff --git a/types/utils.go b/types/utils.go index 21c3d4cdb..971e6d306 100644 --- a/types/utils.go +++ b/types/utils.go @@ -34,6 +34,10 @@ func GetCacheKey(height int, value string) (key string) { return key } +func IsBetween(target, minInclusive, maxInclusive int64) bool { + return minInclusive <= target && target <= maxInclusive +} + // SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces // are removed. // This method can be used to canonicalize JSON to be returned by GetSignBytes, diff --git a/x/pocketcore/keeper/service.go b/x/pocketcore/keeper/service.go index f2d437df8..6b0481edc 100644 --- a/x/pocketcore/keeper/service.go +++ b/x/pocketcore/keeper/service.go @@ -12,8 +12,14 @@ 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) { relayTimeStart := time.Now() - // get the latest session block height because this relay will correspond with the latest session - sessionBlockHeight := k.GetLatestSessionBlockHeight(ctx) + + sessionBlockHeight := relay.Proof.SessionBlockHeight + + if !k.IsProofSessionHeightWithinTolerance(ctx, sessionBlockHeight) { + // For legacy support, we are intentionally returning the invalid block height error. + return nil, pc.NewInvalidBlockHeightError(pc.ModuleName) + } + var node *pc.PocketNode // There is reference to node address so that way we don't have to recreate address twice for pre-leanpokt var nodeAddress sdk.Address diff --git a/x/pocketcore/keeper/session.go b/x/pocketcore/keeper/session.go index 6466bbc39..4a1e78556 100644 --- a/x/pocketcore/keeper/session.go +++ b/x/pocketcore/keeper/session.go @@ -55,6 +55,18 @@ func (k Keeper) IsSessionBlock(ctx sdk.Ctx) bool { return ctx.BlockHeight()%k.posKeeper.BlocksPerSession(ctx) == 1 } +// IsProofSessionHeightWithinTolerance checks if the relaySessionBlockHeight is bounded by (latestSessionBlockHeight - tolerance ) <= x <= latestSessionHeight +func (k Keeper) IsProofSessionHeightWithinTolerance(ctx sdk.Ctx, relaySessionBlockHeight int64) bool { + // Session block height can never be zero. + if relaySessionBlockHeight <= 0 { + return false + } + latestSessionHeight := k.GetLatestSessionBlockHeight(ctx) + tolerance := types.GlobalPocketConfig.ClientSessionBlockSyncAllowance * k.posKeeper.BlocksPerSession(ctx) + minHeight := latestSessionHeight - tolerance + return sdk.IsBetween(relaySessionBlockHeight, minHeight, latestSessionHeight) +} + // "GetLatestSessionBlockHeight" - Returns the latest session block height (first block of the session, (see blocksPerSession)) func (k Keeper) GetLatestSessionBlockHeight(ctx sdk.Ctx) (sessionBlockHeight int64) { // get the latest block height From 2918b9abcf17b7b3eadd652f2128614d0678bdd2 Mon Sep 17 00:00:00 2001 From: poktblade Date: Fri, 7 Apr 2023 01:46:47 -0500 Subject: [PATCH 2/6] add tests off based off new handle relay tests --- x/pocketcore/keeper/service_test.go | 93 +++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 5 deletions(-) diff --git a/x/pocketcore/keeper/service_test.go b/x/pocketcore/keeper/service_test.go index 5bfd61d9a..b48d91257 100644 --- a/x/pocketcore/keeper/service_test.go +++ b/x/pocketcore/keeper/service_test.go @@ -119,7 +119,6 @@ func TestKeeper_HandleRelay(t *testing.T) { ctx, keeper, kvkeys, clientPrivateKey, appPrivateKey, nodePubKey, chain := setupHandleRelayTest(t) - SessionRangesToTest := 2 // Eliminate the impact of ClientBlockSyncAllowance types.GlobalPocketConfig.ClientBlockSyncAllowance = 10000 @@ -137,8 +136,11 @@ func TestKeeper_HandleRelay(t *testing.T) { mockCtx.On("BlockHeight").Return(ctx.BlockHeight()) mockCtx.On("Logger").Return(ctx.Logger()) mockCtx.On("PrevCtx", nodeBlockHeight).Return(ctx, nil) + + AllSessionRangesTests := 4 // The range of block heights we will mock + // Set up mocks for heights we'll query later. - for i := int64(1); i <= blocksPerSesssion*int64(SessionRangesToTest); i++ { + for i := int64(1); i <= blocksPerSesssion*int64(AllSessionRangesTests); i++ { mockCtx.On("PrevCtx", nodeBlockHeight-i).Return(ctx, nil) mockCtx.On("PrevCtx", nodeBlockHeight+i).Return(ctx, nil) } @@ -159,9 +161,11 @@ func TestKeeper_HandleRelay(t *testing.T) { assert.NotEmpty(t, resp) assert.Equal(t, resp.Response, "bar") - // Client is behind or advanced beyond Node's height - // --> CodeInvalidBlockHeightError - for i := 1; i <= SessionRangesToTest; i++ { + // TC 1: + // Client is behind or advanced beyond Node's height with ClientSessionSyncAllowance 0 + // --> CodeInvalidBlockHeightError + types.GlobalPocketConfig.ClientSessionSyncAllowance = 0 + for i := 1; i <= AllSessionRangesTests; i++ { resp, err = testRelayAt( t, mockCtx, @@ -191,4 +195,83 @@ func TestKeeper_HandleRelay(t *testing.T) { assert.Equal(t, err.Codespace(), sdk.CodespaceType(types.ModuleName)) assert.Equal(t, err.Code(), sdk.CodeType(types.CodeInvalidBlockHeightError)) } + + // TC2: + // Test a relay while one session behind and forward, + // while ClientSessionSyncAllowance = 1 + // --> Success on behind session block height + // --> InvalidBlockHeightError on forward session block height + SessionRangeTc := 1 + types.GlobalPocketConfig.ClientSessionSyncAllowance = int64(SessionRangeTc) + + // First test the minimum boundary + resp, err = testRelayAt( + t, + mockCtx, + keeper, + latestSessionHeight-blocksPerSesssion*int64(SessionRangeTc), + clientPrivateKey, + appPrivateKey, + nodePubKey, + chain, + ) + assert.Nil(t, err, err) + assert.NotNil(t, resp) + assert.NotEmpty(t, resp) + assert.Equal(t, resp.Response, "bar") + + // Second test the maximum boundary + resp, err = testRelayAt( + t, + mockCtx, + keeper, + latestSessionHeight+blocksPerSesssion*int64(SessionRangeTc), + clientPrivateKey, + appPrivateKey, + nodePubKey, + chain, + ) + assert.Nil(t, resp) + assert.NotNil(t, err) + assert.Equal(t, err.Codespace(), sdk.CodespaceType(types.ModuleName)) + assert.Equal(t, err.Code(), sdk.CodeType(types.CodeInvalidBlockHeightError)) + + // TC2: + // Test a relay while two sessions behind and forward, + // while ClientSessionSyncAllowance = 1 + // --> InvalidBlockHeightError on forward session block height + // --> InvalidBlockHeightError on forward session block height + SessionRangeTc = 2 + types.GlobalPocketConfig.ClientSessionBlockSyncAllowance = 1 + + resp, err = testRelayAt( + t, + mockCtx, + keeper, + latestSessionHeight-blocksPerSesssion*int64(SessionRangeTc), + clientPrivateKey, + appPrivateKey, + nodePubKey, + chain, + ) + assert.Nil(t, resp) + assert.NotNil(t, err) + assert.Equal(t, err.Codespace(), sdk.CodespaceType(types.ModuleName)) + assert.Equal(t, err.Code(), sdk.CodeType(types.CodeInvalidBlockHeightError)) + + // Second test the maximum boundary + resp, err = testRelayAt( + t, + mockCtx, + keeper, + latestSessionHeight+blocksPerSesssion*int64(SessionRangeTc), + clientPrivateKey, + appPrivateKey, + nodePubKey, + chain, + ) + assert.Nil(t, resp) + assert.NotNil(t, err) + assert.Equal(t, err.Codespace(), sdk.CodespaceType(types.ModuleName)) + assert.Equal(t, err.Code(), sdk.CodeType(types.CodeInvalidBlockHeightError)) } From 727cf3a78dad1ecd7bc0f89fc6820c939bafe336 Mon Sep 17 00:00:00 2001 From: poktblade Date: Fri, 7 Apr 2023 01:49:33 -0500 Subject: [PATCH 3/6] reanme SessionSyncBlockAllowance > DefaultSessionSyncAllowance & update config cmd --- app/config.go | 1 + types/config.go | 124 ++++++++++++++-------------- x/pocketcore/keeper/service_test.go | 2 +- x/pocketcore/keeper/session.go | 2 +- 4 files changed, 65 insertions(+), 64 deletions(-) diff --git a/app/config.go b/app/config.go index 1a6a707b0..6b20615e6 100644 --- a/app/config.go +++ b/app/config.go @@ -240,6 +240,7 @@ func UpdateConfig(datadir string) { GlobalConfig.PocketConfig.RPCTimeout = sdk.DefaultRPCTimeout GlobalConfig.PocketConfig.IavlCacheSize = sdk.DefaultIavlCacheSize GlobalConfig.PocketConfig.LeanPocket = sdk.DefaultLeanPocket + GlobalConfig.PocketConfig.ClientSessionSyncAllowance = sdk.DefaultSessionSyncAllowance // Backup and Save the File var jsonFile *os.File diff --git a/types/config.go b/types/config.go index ab34943d5..2a66e1865 100644 --- a/types/config.go +++ b/types/config.go @@ -18,37 +18,37 @@ type SDKConfig struct { } type PocketConfig struct { - DataDir string `json:"data_dir"` - GenesisName string `json:"genesis_file"` - ChainsName string `json:"chains_name"` - EvidenceDBName string `json:"evidence_db_name"` - TendermintURI string `json:"tendermint_uri"` - KeybaseName string `json:"keybase_name"` - RPCPort string `json:"rpc_port"` - ClientBlockSyncAllowance int `json:"client_block_sync_allowance"` - ClientSessionBlockSyncAllowance int64 `json:"client_session_block_sync_allowance"` - MaxEvidenceCacheEntires int `json:"max_evidence_cache_entries"` - MaxSessionCacheEntries int `json:"max_session_cache_entries"` - JSONSortRelayResponses bool `json:"json_sort_relay_responses"` - RemoteCLIURL string `json:"remote_cli_url"` - UserAgent string `json:"user_agent"` - ValidatorCacheSize int64 `json:"validator_cache_size"` - ApplicationCacheSize int64 `json:"application_cache_size"` - RPCTimeout int64 `json:"rpc_timeout"` - PrometheusAddr string `json:"pocket_prometheus_port"` - PrometheusMaxOpenfiles int `json:"prometheus_max_open_files"` - MaxClaimAgeForProofRetry int `json:"max_claim_age_for_proof_retry"` - ProofPrevalidation bool `json:"proof_prevalidation"` - CtxCacheSize int `json:"ctx_cache_size"` - ABCILogging bool `json:"abci_logging"` - RelayErrors bool `json:"show_relay_errors"` - DisableTxEvents bool `json:"disable_tx_events"` - Cache bool `json:"-"` - IavlCacheSize int64 `json:"iavl_cache_size"` - ChainsHotReload bool `json:"chains_hot_reload"` - GenerateTokenOnStart bool `json:"generate_token_on_start"` - LeanPocket bool `json:"lean_pocket"` - LeanPocketUserKeyFileName string `json:"lean_pocket_user_key_file"` + DataDir string `json:"data_dir"` + GenesisName string `json:"genesis_file"` + ChainsName string `json:"chains_name"` + EvidenceDBName string `json:"evidence_db_name"` + TendermintURI string `json:"tendermint_uri"` + KeybaseName string `json:"keybase_name"` + RPCPort string `json:"rpc_port"` + ClientBlockSyncAllowance int `json:"client_block_sync_allowance"` + ClientSessionSyncAllowance int64 `json:"client_session_sync_allowance"` + MaxEvidenceCacheEntires int `json:"max_evidence_cache_entries"` + MaxSessionCacheEntries int `json:"max_session_cache_entries"` + JSONSortRelayResponses bool `json:"json_sort_relay_responses"` + RemoteCLIURL string `json:"remote_cli_url"` + UserAgent string `json:"user_agent"` + ValidatorCacheSize int64 `json:"validator_cache_size"` + ApplicationCacheSize int64 `json:"application_cache_size"` + RPCTimeout int64 `json:"rpc_timeout"` + PrometheusAddr string `json:"pocket_prometheus_port"` + PrometheusMaxOpenfiles int `json:"prometheus_max_open_files"` + MaxClaimAgeForProofRetry int `json:"max_claim_age_for_proof_retry"` + ProofPrevalidation bool `json:"proof_prevalidation"` + CtxCacheSize int `json:"ctx_cache_size"` + ABCILogging bool `json:"abci_logging"` + RelayErrors bool `json:"show_relay_errors"` + DisableTxEvents bool `json:"disable_tx_events"` + Cache bool `json:"-"` + IavlCacheSize int64 `json:"iavl_cache_size"` + ChainsHotReload bool `json:"chains_hot_reload"` + GenerateTokenOnStart bool `json:"generate_token_on_start"` + LeanPocket bool `json:"lean_pocket"` + LeanPocketUserKeyFileName string `json:"lean_pocket_user_key_file"` } func (c PocketConfig) GetLeanPocketUserKeyFilePath() string { @@ -83,7 +83,7 @@ const ( DefaultMaxEvidenceCacheEntries = 500 DefaultListenAddr = "tcp://0.0.0.0:" DefaultClientBlockSyncAllowance = 10 - DefaultSessionBlockSyncAllowance = 1 // 1 session (irrespective of num blocks per session) + DefaultSessionSyncAllowance = 1 // 1 session (irrespective of num blocks per session) DefaultJSONSortRelayResponses = true DefaultTxIndexer = "kv" DefaultRPCDisableTransactionEvents = true @@ -119,36 +119,36 @@ func DefaultConfig(dataDir string) Config { c := Config{ TendermintConfig: *config.DefaultConfig(), PocketConfig: PocketConfig{ - DataDir: dataDir, - GenesisName: DefaultGenesisName, - ChainsName: DefaultChainsName, - EvidenceDBName: DefaultEvidenceDBName, - TendermintURI: DefaultTMURI, - KeybaseName: DefaultKeybaseName, - RPCPort: DefaultRPCPort, - ClientBlockSyncAllowance: DefaultClientBlockSyncAllowance, - ClientSessionBlockSyncAllowance: DefaultSessionBlockSyncAllowance, - MaxEvidenceCacheEntires: DefaultMaxEvidenceCacheEntries, - MaxSessionCacheEntries: DefaultMaxSessionCacheEntries, - JSONSortRelayResponses: DefaultJSONSortRelayResponses, - RemoteCLIURL: DefaultRemoteCLIURL, - UserAgent: DefaultUserAgent, - ValidatorCacheSize: DefaultValidatorCacheSize, - ApplicationCacheSize: DefaultApplicationCacheSize, - RPCTimeout: DefaultRPCTimeout, - PrometheusAddr: DefaultPocketPrometheusListenAddr, - PrometheusMaxOpenfiles: DefaultPrometheusMaxOpenFile, - MaxClaimAgeForProofRetry: DefaultMaxClaimProofRetryAge, - ProofPrevalidation: DefaultProofPrevalidation, - CtxCacheSize: DefaultCtxCacheSize, - ABCILogging: DefaultABCILogging, - RelayErrors: DefaultRelayErrors, - DisableTxEvents: DefaultRPCDisableTransactionEvents, - IavlCacheSize: DefaultIavlCacheSize, - ChainsHotReload: DefaultChainHotReload, - GenerateTokenOnStart: DefaultGenerateTokenOnStart, - LeanPocket: DefaultLeanPocket, - LeanPocketUserKeyFileName: DefaultLeanPocketUserKeyFileName, + DataDir: dataDir, + GenesisName: DefaultGenesisName, + ChainsName: DefaultChainsName, + EvidenceDBName: DefaultEvidenceDBName, + TendermintURI: DefaultTMURI, + KeybaseName: DefaultKeybaseName, + RPCPort: DefaultRPCPort, + ClientBlockSyncAllowance: DefaultClientBlockSyncAllowance, + ClientSessionSyncAllowance: DefaultSessionSyncAllowance, + MaxEvidenceCacheEntires: DefaultMaxEvidenceCacheEntries, + MaxSessionCacheEntries: DefaultMaxSessionCacheEntries, + JSONSortRelayResponses: DefaultJSONSortRelayResponses, + RemoteCLIURL: DefaultRemoteCLIURL, + UserAgent: DefaultUserAgent, + ValidatorCacheSize: DefaultValidatorCacheSize, + ApplicationCacheSize: DefaultApplicationCacheSize, + RPCTimeout: DefaultRPCTimeout, + PrometheusAddr: DefaultPocketPrometheusListenAddr, + PrometheusMaxOpenfiles: DefaultPrometheusMaxOpenFile, + MaxClaimAgeForProofRetry: DefaultMaxClaimProofRetryAge, + ProofPrevalidation: DefaultProofPrevalidation, + CtxCacheSize: DefaultCtxCacheSize, + ABCILogging: DefaultABCILogging, + RelayErrors: DefaultRelayErrors, + DisableTxEvents: DefaultRPCDisableTransactionEvents, + IavlCacheSize: DefaultIavlCacheSize, + ChainsHotReload: DefaultChainHotReload, + GenerateTokenOnStart: DefaultGenerateTokenOnStart, + LeanPocket: DefaultLeanPocket, + LeanPocketUserKeyFileName: DefaultLeanPocketUserKeyFileName, }, } c.TendermintConfig.LevelDBOptions = config.DefaultLevelDBOpts() diff --git a/x/pocketcore/keeper/service_test.go b/x/pocketcore/keeper/service_test.go index b48d91257..2c307ffc4 100644 --- a/x/pocketcore/keeper/service_test.go +++ b/x/pocketcore/keeper/service_test.go @@ -242,7 +242,7 @@ func TestKeeper_HandleRelay(t *testing.T) { // --> InvalidBlockHeightError on forward session block height // --> InvalidBlockHeightError on forward session block height SessionRangeTc = 2 - types.GlobalPocketConfig.ClientSessionBlockSyncAllowance = 1 + types.GlobalPocketConfig.ClientSessionSyncAllowance = 1 resp, err = testRelayAt( t, diff --git a/x/pocketcore/keeper/session.go b/x/pocketcore/keeper/session.go index 4a1e78556..9c546902b 100644 --- a/x/pocketcore/keeper/session.go +++ b/x/pocketcore/keeper/session.go @@ -62,7 +62,7 @@ func (k Keeper) IsProofSessionHeightWithinTolerance(ctx sdk.Ctx, relaySessionBlo return false } latestSessionHeight := k.GetLatestSessionBlockHeight(ctx) - tolerance := types.GlobalPocketConfig.ClientSessionBlockSyncAllowance * k.posKeeper.BlocksPerSession(ctx) + tolerance := types.GlobalPocketConfig.ClientSessionSyncAllowance * k.posKeeper.BlocksPerSession(ctx) minHeight := latestSessionHeight - tolerance return sdk.IsBetween(relaySessionBlockHeight, minHeight, latestSessionHeight) } From eef50cfe90c10ccf8608f9dab7857c95395160df Mon Sep 17 00:00:00 2001 From: poktblade Date: Fri, 7 Apr 2023 01:53:00 -0500 Subject: [PATCH 4/6] add cleanup on adjusting the allowance parameters --- x/pocketcore/keeper/service_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/x/pocketcore/keeper/service_test.go b/x/pocketcore/keeper/service_test.go index 2c307ffc4..4a9dd55dc 100644 --- a/x/pocketcore/keeper/service_test.go +++ b/x/pocketcore/keeper/service_test.go @@ -119,7 +119,11 @@ func TestKeeper_HandleRelay(t *testing.T) { ctx, keeper, kvkeys, clientPrivateKey, appPrivateKey, nodePubKey, chain := setupHandleRelayTest(t) - // Eliminate the impact of ClientBlockSyncAllowance + // Store the original allowances to clean up at the end of this test + originalClientBlockSyncAllowance := types.GlobalPocketConfig.ClientBlockSyncAllowance + originalClientSessionSyncAllowance := types.GlobalPocketConfig.ClientSessionSyncAllowance + + // Eliminate the impact of ClientBlockSyncAllowance to avoid any relay meta validation errors (OutOfSyncError) types.GlobalPocketConfig.ClientBlockSyncAllowance = 10000 nodeBlockHeight := ctx.BlockHeight() @@ -127,6 +131,8 @@ func TestKeeper_HandleRelay(t *testing.T) { latestSessionHeight := keeper.GetLatestSessionBlockHeight(ctx) t.Cleanup(func() { + types.GlobalPocketConfig.ClientBlockSyncAllowance = originalClientBlockSyncAllowance + types.GlobalPocketConfig.ClientSessionSyncAllowance = originalClientSessionSyncAllowance gock.Off() // Flush pending mocks after test execution }) From 70908d8bf70cf06a4a27684aeac74cb87d485775 Mon Sep 17 00:00:00 2001 From: poktblade Date: Fri, 7 Apr 2023 01:55:22 -0500 Subject: [PATCH 5/6] add documentation to quick start --- doc/guides/quickstart.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/guides/quickstart.md b/doc/guides/quickstart.md index d064dcb7c..8d829760b 100644 --- a/doc/guides/quickstart.md +++ b/doc/guides/quickstart.md @@ -250,6 +250,7 @@ Pocket Core provides a configuration file found in `/config/config.json - **"rpc_port"**: The port of Pocket Core's RPC - **"client_block_sync_allowance"**: The +/- allowance in blocks for a relay request \(security mechanism that can help filter misconfigured clients\) +- **"client_session_sync_allowance"**: The +/- allowance in amount of sessions for a relay request \(mechanism to allow for servicing stale sessions for whenever session height changes.) - **"max_evidence_cache_entries"**: Maximum number of relay evidence stored in cache memory - **"max_session_cache_entries"**: Maximum number of sessions stored in cache memory - **"json_sort_relay_responses"**: Detect and sort if relay response is in json \(can help response comparisons if From 0d0ec1ae5ec30d1f24d3ca49928a1bde5f4b9328 Mon Sep 17 00:00:00 2001 From: poktblade Date: Sat, 8 Apr 2023 04:25:56 -0500 Subject: [PATCH 6/6] resolve nits --- x/pocketcore/keeper/service_test.go | 36 ++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/x/pocketcore/keeper/service_test.go b/x/pocketcore/keeper/service_test.go index 4a9dd55dc..cd0766e92 100644 --- a/x/pocketcore/keeper/service_test.go +++ b/x/pocketcore/keeper/service_test.go @@ -143,10 +143,10 @@ func TestKeeper_HandleRelay(t *testing.T) { mockCtx.On("Logger").Return(ctx.Logger()) mockCtx.On("PrevCtx", nodeBlockHeight).Return(ctx, nil) - AllSessionRangesTests := 4 // The range of block heights we will mock + allSessionRangesTests := 4 // The range of block heights we will mock // Set up mocks for heights we'll query later. - for i := int64(1); i <= blocksPerSesssion*int64(AllSessionRangesTests); i++ { + for i := int64(1); i <= blocksPerSesssion*int64(allSessionRangesTests); i++ { mockCtx.On("PrevCtx", nodeBlockHeight-i).Return(ctx, nil) mockCtx.On("PrevCtx", nodeBlockHeight+i).Return(ctx, nil) } @@ -162,7 +162,7 @@ func TestKeeper_HandleRelay(t *testing.T) { nodePubKey, chain, ) - assert.Nil(t, err, err) + assert.Nil(t, err) assert.NotNil(t, resp) assert.NotEmpty(t, resp) assert.Equal(t, resp.Response, "bar") @@ -171,7 +171,7 @@ func TestKeeper_HandleRelay(t *testing.T) { // Client is behind or advanced beyond Node's height with ClientSessionSyncAllowance 0 // --> CodeInvalidBlockHeightError types.GlobalPocketConfig.ClientSessionSyncAllowance = 0 - for i := 1; i <= AllSessionRangesTests; i++ { + for i := 1; i <= allSessionRangesTests; i++ { resp, err = testRelayAt( t, mockCtx, @@ -205,33 +205,33 @@ func TestKeeper_HandleRelay(t *testing.T) { // TC2: // Test a relay while one session behind and forward, // while ClientSessionSyncAllowance = 1 - // --> Success on behind session block height - // --> InvalidBlockHeightError on forward session block height - SessionRangeTc := 1 - types.GlobalPocketConfig.ClientSessionSyncAllowance = int64(SessionRangeTc) + // --> Success on one session behind + // --> InvalidBlockHeightError on one session forward + sessionRangeTc := 1 + types.GlobalPocketConfig.ClientSessionSyncAllowance = int64(sessionRangeTc) // First test the minimum boundary resp, err = testRelayAt( t, mockCtx, keeper, - latestSessionHeight-blocksPerSesssion*int64(SessionRangeTc), + latestSessionHeight-blocksPerSesssion*int64(sessionRangeTc), clientPrivateKey, appPrivateKey, nodePubKey, chain, ) - assert.Nil(t, err, err) + assert.Nil(t, err) assert.NotNil(t, resp) assert.NotEmpty(t, resp) assert.Equal(t, resp.Response, "bar") - // Second test the maximum boundary + // Second test the maximum boundary - Error resp, err = testRelayAt( t, mockCtx, keeper, - latestSessionHeight+blocksPerSesssion*int64(SessionRangeTc), + latestSessionHeight+blocksPerSesssion*int64(sessionRangeTc), clientPrivateKey, appPrivateKey, nodePubKey, @@ -245,16 +245,16 @@ func TestKeeper_HandleRelay(t *testing.T) { // TC2: // Test a relay while two sessions behind and forward, // while ClientSessionSyncAllowance = 1 - // --> InvalidBlockHeightError on forward session block height - // --> InvalidBlockHeightError on forward session block height - SessionRangeTc = 2 + // --> InvalidBlockHeightError on two sessions behind and forwards + sessionRangeTc = 2 types.GlobalPocketConfig.ClientSessionSyncAllowance = 1 + // First test two sessions back - Error resp, err = testRelayAt( t, mockCtx, keeper, - latestSessionHeight-blocksPerSesssion*int64(SessionRangeTc), + latestSessionHeight-blocksPerSesssion*int64(sessionRangeTc), clientPrivateKey, appPrivateKey, nodePubKey, @@ -265,12 +265,12 @@ func TestKeeper_HandleRelay(t *testing.T) { assert.Equal(t, err.Codespace(), sdk.CodespaceType(types.ModuleName)) assert.Equal(t, err.Code(), sdk.CodeType(types.CodeInvalidBlockHeightError)) - // Second test the maximum boundary + // Second test two sessions forward - Error resp, err = testRelayAt( t, mockCtx, keeper, - latestSessionHeight+blocksPerSesssion*int64(SessionRangeTc), + latestSessionHeight+blocksPerSesssion*int64(sessionRangeTc), clientPrivateKey, appPrivateKey, nodePubKey,