From 4a60defe2bacc392e31ba9e4a211dfee813acf65 Mon Sep 17 00:00:00 2001 From: adu-crypto Date: Mon, 9 Jan 2023 11:29:49 +0800 Subject: [PATCH 1/3] make GetParams compatible with old context where params stored in x/params only --- app/app.go | 1 + x/cronos/keeper/evm_hooks_test.go | 1 + x/cronos/keeper/evmhandlers_test.go | 3 +++ x/cronos/keeper/ibc_test.go | 1 + x/cronos/keeper/keeper.go | 10 +++++++--- x/cronos/keeper/keeper_test.go | 2 ++ 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/app.go b/app/app.go index 589f0a900d..3c769362fd 100644 --- a/app/app.go +++ b/app/app.go @@ -498,6 +498,7 @@ func New( appCodec, keys[cronostypes.StoreKey], keys[cronostypes.MemStoreKey], + app.GetSubspace(cronostypes.ModuleName), app.BankKeeper, app.TransferKeeper, gravityKeeper, diff --git a/x/cronos/keeper/evm_hooks_test.go b/x/cronos/keeper/evm_hooks_test.go index 41ca05b588..eb4192b877 100644 --- a/x/cronos/keeper/evm_hooks_test.go +++ b/x/cronos/keeper/evm_hooks_test.go @@ -201,6 +201,7 @@ func (suite *KeeperTestSuite) TestEvmHooks() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, diff --git a/x/cronos/keeper/evmhandlers_test.go b/x/cronos/keeper/evmhandlers_test.go index cfd77f2d8a..8f41f3d4de 100644 --- a/x/cronos/keeper/evmhandlers_test.go +++ b/x/cronos/keeper/evmhandlers_test.go @@ -358,6 +358,7 @@ func (suite *KeeperTestSuite) TestSendToIbcHandler() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, @@ -480,6 +481,7 @@ func (suite *KeeperTestSuite) TestSendToIbcV2Handler() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, @@ -577,6 +579,7 @@ func (suite *KeeperTestSuite) TestSendCroToIbcHandler() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, diff --git a/x/cronos/keeper/ibc_test.go b/x/cronos/keeper/ibc_test.go index ec7ab125bc..47f0b80a93 100644 --- a/x/cronos/keeper/ibc_test.go +++ b/x/cronos/keeper/ibc_test.go @@ -267,6 +267,7 @@ func (suite *KeeperTestSuite) TestIbcTransferCoins() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, diff --git a/x/cronos/keeper/keeper.go b/x/cronos/keeper/keeper.go index 167fd4530a..d74fbb34b3 100644 --- a/x/cronos/keeper/keeper.go +++ b/x/cronos/keeper/keeper.go @@ -6,6 +6,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/crypto-org-chain/cronos/x/cronos/exported" "github.com/tendermint/tendermint/libs/log" @@ -21,9 +22,10 @@ import ( type ( Keeper struct { - cdc codec.Codec - storeKey storetypes.StoreKey - memKey storetypes.StoreKey + cdc codec.Codec + storeKey storetypes.StoreKey + memKey storetypes.StoreKey + paramSpace exported.Subspace // update balance and accounting operations with coins bankKeeper types.BankKeeper @@ -48,6 +50,7 @@ func NewKeeper( cdc codec.Codec, storeKey, memKey storetypes.StoreKey, + paramSpace exported.Subspace, bankKeeper types.BankKeeper, transferKeeper types.TransferKeeper, gravityKeeper types.GravityKeeper, @@ -64,6 +67,7 @@ func NewKeeper( cdc: cdc, storeKey: storeKey, memKey: memKey, + paramSpace: paramSpace, bankKeeper: bankKeeper, transferKeeper: transferKeeper, gravityKeeper: gravityKeeper, diff --git a/x/cronos/keeper/keeper_test.go b/x/cronos/keeper/keeper_test.go index b3adfd2a78..9842ca7e76 100644 --- a/x/cronos/keeper/keeper_test.go +++ b/x/cronos/keeper/keeper_test.go @@ -265,6 +265,7 @@ func (suite *KeeperTestSuite) TestOnRecvVouchers() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, @@ -409,6 +410,7 @@ func (suite *KeeperTestSuite) TestRegisterOrUpdateTokenMapping() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, From a1114c643db204cb79df750ae23b9aa5bd6ac793 Mon Sep 17 00:00:00 2001 From: adu-crypto Date: Mon, 9 Jan 2023 16:00:13 +0800 Subject: [PATCH 2/3] change Subspace interface --- app/app.go | 2 +- x/cronos/exported/exported.go | 2 ++ x/cronos/keeper/keeper.go | 14 +++++++++++--- x/cronos/keeper/params.go | 1 + x/cronos/keeper/params_test.go | 1 + x/cronos/migrations/v2/migrate_test.go | 9 +++++++++ 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/app.go b/app/app.go index 3c769362fd..5be90ab911 100644 --- a/app/app.go +++ b/app/app.go @@ -969,7 +969,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(gravitytypes.ModuleName) } // this line is used by starport scaffolding # stargate/app/paramSubspace - paramsKeeper.Subspace(cronostypes.ModuleName).WithKeyTable(cronostypes.ParamKeyTable()) + paramsKeeper.Subspace(cronostypes.ModuleName) return paramsKeeper } diff --git a/x/cronos/exported/exported.go b/x/cronos/exported/exported.go index 000114e619..2d8532c46e 100644 --- a/x/cronos/exported/exported.go +++ b/x/cronos/exported/exported.go @@ -14,5 +14,7 @@ type ( // NOTE: This is used solely for migration of x/params managed parameters. Subspace interface { GetParamSet(ctx sdk.Context, ps ParamSet) + HasKeyTable() bool + WithKeyTable(paramtypes.KeyTable) paramtypes.Subspace } ) diff --git a/x/cronos/keeper/keeper.go b/x/cronos/keeper/keeper.go index d74fbb34b3..c65905c9e7 100644 --- a/x/cronos/keeper/keeper.go +++ b/x/cronos/keeper/keeper.go @@ -22,9 +22,11 @@ import ( type ( Keeper struct { - cdc codec.Codec - storeKey storetypes.StoreKey - memKey storetypes.StoreKey + cdc codec.Codec + storeKey storetypes.StoreKey + memKey storetypes.StoreKey + + // paramsSpace is needed incase we need to get params in old blocks context paramSpace exported.Subspace // update balance and accounting operations with coins @@ -63,6 +65,12 @@ func NewKeeper( panic(err) } + // set KeyTable if it has not already been set. + // we need paramsSpace incase we need to get params in old blocks context. + if !paramSpace.HasKeyTable() { + paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable()) + } + return &Keeper{ cdc: cdc, storeKey: storeKey, diff --git a/x/cronos/keeper/params.go b/x/cronos/keeper/params.go index 4c54c934b1..606c17c954 100644 --- a/x/cronos/keeper/params.go +++ b/x/cronos/keeper/params.go @@ -15,6 +15,7 @@ func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { store := ctx.KVStore(k.storeKey) bz := store.Get(types.ParamsKey) if bz == nil { + k.paramSpace.GetParamSet(ctx, ¶ms) return params } k.cdc.MustUnmarshal(bz, ¶ms) diff --git a/x/cronos/keeper/params_test.go b/x/cronos/keeper/params_test.go index fcb315fa32..49bd5ce36a 100644 --- a/x/cronos/keeper/params_test.go +++ b/x/cronos/keeper/params_test.go @@ -42,6 +42,7 @@ func (suite *KeeperTestSuite) TestGetSourceChannelID() { app.MakeEncodingConfig().Codec, suite.app.GetKey(types.StoreKey), suite.app.GetKey(types.MemStoreKey), + suite.app.GetSubspace(types.ModuleName), suite.app.BankKeeper, keepertest.IbcKeeperMock{}, suite.app.GravityKeeper, diff --git a/x/cronos/migrations/v2/migrate_test.go b/x/cronos/migrations/v2/migrate_test.go index d20ae38557..b2e479c39d 100644 --- a/x/cronos/migrations/v2/migrate_test.go +++ b/x/cronos/migrations/v2/migrate_test.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/crypto-org-chain/cronos/x/cronos/exported" v2 "github.com/crypto-org-chain/cronos/x/cronos/migrations/v2" "github.com/crypto-org-chain/cronos/x/cronos/types" @@ -24,6 +25,14 @@ func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) { *ps.(*types.Params) = ms.ps } +func (ms mockSubspace) HasKeyTable() bool { + return false +} + +func (ms mockSubspace) WithKeyTable(paramtypes.KeyTable) paramtypes.Subspace { + return paramtypes.Subspace{} +} + func TestMigrate(t *testing.T) { storeKey := sdk.NewKVStoreKey(types.ModuleName) ctx := testutil.DefaultContext(storeKey, sdk.NewTransientStoreKey("test")) From befb56e07b2bd81af6df50fe07953c7563fb88f4 Mon Sep 17 00:00:00 2001 From: adu-crypto Date: Thu, 12 Jan 2023 17:39:41 +0800 Subject: [PATCH 3/3] fix query-params cmd to support query by blockheight --- integration_tests/cosmoscli.py | 3 ++- integration_tests/test_upgrade.py | 11 ++++++++++- x/cronos/client/cli/query.go | 21 +++++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/integration_tests/cosmoscli.py b/integration_tests/cosmoscli.py index b9246b2645..f364d4e83a 100644 --- a/integration_tests/cosmoscli.py +++ b/integration_tests/cosmoscli.py @@ -1431,13 +1431,14 @@ def turn_bridge(self, enable, **kwargs): ) ) - def query_params(self): + def query_params(self, height: int): "query cronos params" return json.loads( self.raw( "query", "cronos", "params", + height, home=self.data_dir, ) ) diff --git a/integration_tests/test_upgrade.py b/integration_tests/test_upgrade.py index 2c3c862649..e6dfcb5f41 100644 --- a/integration_tests/test_upgrade.py +++ b/integration_tests/test_upgrade.py @@ -166,10 +166,19 @@ def test_cosmovisor_upgrade(custom_cronos: Cronos): contract.caller(block_identifier=target_height - 2).balanceOf(ADDRS["validator"]) # check we could fetch the right params after cronos Migrate1To2 - assert cli.query_params() == { + assert cli.query_params(0) == { "cronos_admin": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", "enable_auto_deployment": True, "ibc_cro_denom": "ibc/6411AE2ADA1E73DB59DB151" "A8988F9B7D5E7E233D8414DB6817F8F1A01611F86", "ibc_timeout": "86400000000000", } + + assert cli.query_params(target_height-5) == { + "cronos_admin": "crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp", + "enable_auto_deployment": True, + "ibc_cro_denom": "ibc/6411AE2ADA1E73DB59DB151" + "A8988F9B7D5E7E233D8414DB6817F8F1A01611F86", + "ibc_timeout": "86400000000000", + } + diff --git a/x/cronos/client/cli/query.go b/x/cronos/client/cli/query.go index 8efec106a1..28facfc8e9 100644 --- a/x/cronos/client/cli/query.go +++ b/x/cronos/client/cli/query.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "strconv" "strings" rpctypes "github.com/evmos/ethermint/rpc/types" @@ -38,12 +39,13 @@ func GetQueryCmd(queryRoute string) *cobra.Command { // QueryParamsCmd returns the command handler for evidence parameter querying. func QueryParamsCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "params", - Short: "Query the current cronos parameters", - Args: cobra.NoArgs, - Long: strings.TrimSpace(`Query the current cronos parameters: + Use: "params [height]", + Short: "Query the cronos parameters associated with a specific blockheight", + Args: cobra.ExactArgs(1), + Long: strings.TrimSpace(`Query the cronos parameters associated with a specific blockheight, 0 or negative + means the latest blockheight: -$ query cronos params +$ query cronos params [height] `), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) @@ -51,8 +53,15 @@ $ query cronos params return err } + height, err := strconv.ParseInt(args[0], 10, 64) + if err != nil { + return err + } + if height <= 0 { + height = clientCtx.Height + } queryClient := types.NewQueryClient(clientCtx) - res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{}) + res, err := queryClient.Params(rpctypes.ContextWithHeight(height), &types.QueryParamsRequest{}) if err != nil { return err }