From 9cafddf503f54a159abcf057a7aa71434a7f5dca Mon Sep 17 00:00:00 2001 From: dudong2 Date: Thu, 9 May 2024 15:43:48 +0900 Subject: [PATCH] test: Add MsgUpdateParams test --- x/csr/keeper/keeper_test.go | 7 ++- x/csr/keeper/msg_server.go | 3 - x/csr/keeper/msg_server_test.go | 62 +++++++++++++++++++ x/csr/module.go | 1 + x/csr/types/codec.go | 11 +++- x/epochs/keeper/keeper_test.go | 7 ++- x/erc20/keeper/keeper_test.go | 7 ++- x/inflation/keeper/keeper_test.go | 9 ++- x/inflation/keeper/msg_server.go | 3 - x/inflation/keeper/msg_server_test.go | 84 ++++++++++++++++++++++++++ x/inflation/module.go | 5 +- x/inflation/types/codec.go | 28 ++++++++- x/onboarding/keeper/keeper_test.go | 42 ++++++++++--- x/onboarding/keeper/msg_server.go | 3 - x/onboarding/keeper/msg_server_test.go | 70 +++++++++++++++++++++ x/onboarding/module.go | 6 +- x/onboarding/types/codec.go | 35 +++++++++++ 17 files changed, 348 insertions(+), 35 deletions(-) create mode 100644 x/csr/keeper/msg_server_test.go create mode 100644 x/inflation/keeper/msg_server_test.go create mode 100644 x/onboarding/keeper/msg_server_test.go create mode 100644 x/onboarding/types/codec.go diff --git a/x/csr/keeper/keeper_test.go b/x/csr/keeper/keeper_test.go index fe4535aa7..40ec23dd2 100644 --- a/x/csr/keeper/keeper_test.go +++ b/x/csr/keeper/keeper_test.go @@ -15,6 +15,7 @@ import ( "github.com/Canto-Network/Canto/v7/x/csr/types" "github.com/cosmos/cosmos-sdk/baseapp" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + cosmosed25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" @@ -84,9 +85,9 @@ func (suite *KeeperTestSuite) SetupApp() { suite.denom = "acanto" // consensus key - privCons, err := ethsecp256k1.GenerateKey() + pubKey := cosmosed25519.GenPrivKey().PubKey() require.NoError(t, err) - suite.consAddress = sdk.ConsAddress(privCons.PubKey().Address()) + suite.consAddress = sdk.ConsAddress(pubKey.Address()) suite.ctx = suite.app.BaseApp.NewContextLegacy(false, tmproto.Header{ Height: 1, ChainID: "canto_9001-1", @@ -139,7 +140,7 @@ func (suite *KeeperTestSuite) SetupApp() { // Set Validator valAddr := sdk.ValAddress(suite.address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr.String(), privCons.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr.String(), pubKey, stakingtypes.Description{}) require.NoError(t, err) validator = stakingkeeper.TestingUpdateValidator(suite.app.StakingKeeper, suite.ctx, validator, true) diff --git a/x/csr/keeper/msg_server.go b/x/csr/keeper/msg_server.go index 6ed6eac74..bdf6217e7 100644 --- a/x/csr/keeper/msg_server.go +++ b/x/csr/keeper/msg_server.go @@ -33,9 +33,6 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam ctx := sdk.UnwrapSDKContext(goCtx) k.SetParams(ctx, req.Params) - // if err := k.SetParams(ctx, req.Params); err != nil { - // return nil, err - // } return &types.MsgUpdateParamsResponse{}, nil } diff --git a/x/csr/keeper/msg_server_test.go b/x/csr/keeper/msg_server_test.go new file mode 100644 index 000000000..14c855145 --- /dev/null +++ b/x/csr/keeper/msg_server_test.go @@ -0,0 +1,62 @@ +package keeper_test + +import ( + "math" + + sdkmath "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + "github.com/Canto-Network/Canto/v7/testutil" + csrtypes "github.com/Canto-Network/Canto/v7/x/csr/types" +) + +func (suite *KeeperTestSuite) TestMsgUpdateParamsByProposal() { + suite.SetupTest() + + // change mindeposit for denom + govParams, err := suite.app.GovKeeper.Params.Get(suite.ctx) + suite.Require().NoError(err) + govParams.MinDeposit = []sdk.Coin{sdk.NewCoin(suite.denom, sdkmath.NewInt(1))} + err = suite.app.GovKeeper.Params.Set(suite.ctx, govParams) + suite.Require().NoError(err) + + // create account and deligate to validator + _, proposer := GenerateKey() + initAmount := sdkmath.NewInt(int64(math.Pow10(18)) * 2) + initBalance := sdk.NewCoins(sdk.NewCoin(suite.denom, initAmount)) + testutil.FundAccount(suite.app.BankKeeper, suite.ctx, proposer, initBalance) + shares, err := suite.app.StakingKeeper.Delegate(suite.ctx, proposer, sdk.DefaultPowerReduction, stakingtypes.Unbonded, suite.validator, true) + suite.Require().NoError(err) + suite.Require().True(shares.GT(sdkmath.LegacyNewDec(0))) + + // submit proposal for change params + changeParams := csrtypes.Params{ + EnableCsr: false, + CsrShares: sdkmath.LegacyNewDecWithPrec(20, 2), + } + msg := &csrtypes.MsgUpdateParams{ + Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), + Params: changeParams, + } + proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{msg}, "", "test", "description", proposer, false) + suite.Require().NoError(err) + suite.Commit() + + ok, err := suite.app.GovKeeper.AddDeposit(suite.ctx, proposal.Id, proposer, govParams.MinDeposit) + suite.Require().NoError(err) + suite.Require().True(ok) + suite.Commit() + + err = suite.app.GovKeeper.AddVote(suite.ctx, proposal.Id, proposer, govtypesv1.NewNonSplitVoteOption(govtypesv1.OptionYes), "") + suite.Require().NoError(err) + suite.CommitAfter(*govParams.VotingPeriod) + + proposal, err = suite.app.GovKeeper.Proposals.Get(suite.ctx, proposal.Id) + suite.Require().NoError(err) + suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status) + suite.Require().Equal(suite.app.CSRKeeper.GetParams(suite.ctx), changeParams) +} diff --git a/x/csr/module.go b/x/csr/module.go index 4a308aaf8..97cbc7f28 100644 --- a/x/csr/module.go +++ b/x/csr/module.go @@ -127,6 +127,7 @@ func (am AppModule) Name() string { // RegisterServices registers a GRPC query service to respond to the // module-specific GRPC queries. func (am AppModule) RegisterServices(cfg module.Configurator) { + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) } diff --git a/x/csr/types/codec.go b/x/csr/types/codec.go index 5356c4e70..69de2e136 100644 --- a/x/csr/types/codec.go +++ b/x/csr/types/codec.go @@ -3,12 +3,13 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/msgservice" ) var ( amino = codec.NewLegacyAmino() ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) - AminoCdc = codec.NewAminoCodec(amino) ) // method required for x/csr msg GetSignBytes methods @@ -19,8 +20,16 @@ func init() { // register interfaces for the AminoCodec func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + registry.RegisterImplementations( + (*sdk.Msg)(nil), + &MsgUpdateParams{}, + ) + + msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } // register csr msg types for Amino Codec in adherence to EIP-712 signing conventions func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterConcrete(&MsgUpdateParams{}, "canto/x/csr/MsgUpdateParams", nil) + cdc.RegisterConcrete(&Params{}, "canto/x/csr/Params", nil) } diff --git a/x/epochs/keeper/keeper_test.go b/x/epochs/keeper/keeper_test.go index 64686d83b..812091805 100644 --- a/x/epochs/keeper/keeper_test.go +++ b/x/epochs/keeper/keeper_test.go @@ -14,6 +14,7 @@ import ( tmversion "github.com/cometbft/cometbft/proto/tendermint/version" "github.com/cometbft/cometbft/version" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -77,9 +78,9 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) { suite.denom = "acanto" // consensus key - privCons, err := ethsecp256k1.GenerateKey() + pubKey := ed25519.GenPrivKey().PubKey() require.NoError(t, err) - suite.consAddress = sdk.ConsAddress(privCons.PubKey().Address()) + suite.consAddress = sdk.ConsAddress(pubKey.Address()) // setup context suite.ctx = suite.app.BaseApp.NewContextLegacy(checkTx, tmproto.Header{ @@ -127,7 +128,7 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) { // Set Validator valAddr := sdk.ValAddress(suite.address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr.String(), privCons.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr.String(), pubKey, stakingtypes.Description{}) require.NoError(t, err) validator = stakingkeeper.TestingUpdateValidator(suite.app.StakingKeeper, suite.ctx, validator, true) diff --git a/x/erc20/keeper/keeper_test.go b/x/erc20/keeper/keeper_test.go index 1f5c8ea56..2e4cbf93e 100644 --- a/x/erc20/keeper/keeper_test.go +++ b/x/erc20/keeper/keeper_test.go @@ -20,6 +20,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/keyring" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -88,9 +89,9 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) { suite.signer = tests.NewSigner(priv) // consensus key - priv, err = ethsecp256k1.GenerateKey() + pubKey := ed25519.GenPrivKey().PubKey() require.NoError(t, err) - suite.consAddress = sdk.ConsAddress(priv.PubKey().Address()) + suite.consAddress = sdk.ConsAddress(pubKey.Address()) // setup feemarketGenesis params feemarketGenesis := feemarkettypes.DefaultGenesisState() @@ -173,7 +174,7 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) { suite.app.AccountKeeper.SetAccount(suite.ctx, acc) valAddr := sdk.ValAddress(suite.address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr.String(), priv.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr.String(), pubKey, stakingtypes.Description{}) require.NoError(t, err) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) require.NoError(t, err) diff --git a/x/inflation/keeper/keeper_test.go b/x/inflation/keeper/keeper_test.go index 04a31c599..3e26dcbdf 100644 --- a/x/inflation/keeper/keeper_test.go +++ b/x/inflation/keeper/keeper_test.go @@ -14,12 +14,12 @@ import ( tmversion "github.com/cometbft/cometbft/proto/tendermint/version" "github.com/cometbft/cometbft/version" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/ethereum/go-ethereum/common" - "github.com/evmos/ethermint/crypto/ethsecp256k1" evm "github.com/evmos/ethermint/x/evm/types" "github.com/Canto-Network/Canto/v7/app" @@ -68,9 +68,8 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) { // init app suite.app = app.Setup(checkTx, nil) - privCons, err := ethsecp256k1.GenerateKey() - require.NoError(t, err) - suite.consAddress = sdk.ConsAddress(privCons.PubKey().Address()) + pubKey := ed25519.GenPrivKey().PubKey() + suite.consAddress = sdk.ConsAddress(pubKey.Address()) // setup context suite.ctx = suite.app.BaseApp.NewContextLegacy(checkTx, tmproto.Header{ Height: 1, @@ -117,7 +116,7 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) { // Set Validator valAddr := sdk.ValAddress(suite.address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr.String(), privCons.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr.String(), pubKey, stakingtypes.Description{}) require.NoError(t, err) validator = stakingkeeper.TestingUpdateValidator(suite.app.StakingKeeper, suite.ctx, validator, true) diff --git a/x/inflation/keeper/msg_server.go b/x/inflation/keeper/msg_server.go index b35dea17a..c6fd5bf84 100644 --- a/x/inflation/keeper/msg_server.go +++ b/x/inflation/keeper/msg_server.go @@ -33,9 +33,6 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam ctx := sdk.UnwrapSDKContext(goCtx) k.SetParams(ctx, req.Params) - // if err := k.SetParams(ctx, req.Params); err != nil { - // return nil, err - // } return &types.MsgUpdateParamsResponse{}, nil } diff --git a/x/inflation/keeper/msg_server_test.go b/x/inflation/keeper/msg_server_test.go new file mode 100644 index 000000000..cdb126c0e --- /dev/null +++ b/x/inflation/keeper/msg_server_test.go @@ -0,0 +1,84 @@ +package keeper_test + +import ( + "math" + + sdkmath "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + "github.com/evmos/ethermint/crypto/ethsecp256k1" + + "github.com/Canto-Network/Canto/v7/testutil" + inflationtypes "github.com/Canto-Network/Canto/v7/x/inflation/types" +) + +func (suite *KeeperTestSuite) TestMsgUpdateParamsByProposal() { + suite.SetupTest() + + // set denom + stakingParams, err := suite.app.StakingKeeper.GetParams(suite.ctx) + suite.Require().NoError(err) + denom := stakingParams.BondDenom + + // change mindeposit for denom + govParams, err := suite.app.GovKeeper.Params.Get(suite.ctx) + suite.Require().NoError(err) + govParams.MinDeposit = []sdk.Coin{sdk.NewCoin(denom, sdkmath.NewInt(1))} + err = suite.app.GovKeeper.Params.Set(suite.ctx, govParams) + suite.Require().NoError(err) + + // create account + privKey, err := ethsecp256k1.GenerateKey() + suite.Require().NoError(err) + proposer := sdk.AccAddress(privKey.PubKey().Address().Bytes()) + + // deligate to validator + initAmount := sdkmath.NewInt(int64(math.Pow10(18)) * 2) + initBalance := sdk.NewCoins(sdk.NewCoin(denom, initAmount)) + testutil.FundAccount(suite.app.BankKeeper, suite.ctx, proposer, initBalance) + shares, err := suite.app.StakingKeeper.Delegate(suite.ctx, proposer, sdk.DefaultPowerReduction, stakingtypes.Unbonded, suite.validator, true) + suite.Require().NoError(err) + suite.Require().True(shares.GT(sdkmath.LegacyNewDec(0))) + + // submit proposal for change params + changeParams := inflationtypes.Params{ + MintDenom: "btc", + ExponentialCalculation: inflationtypes.ExponentialCalculation{ + A: sdkmath.LegacyNewDec(int64(16_304_348)), + R: sdkmath.LegacyNewDecWithPrec(35, 2), + C: sdkmath.LegacyZeroDec(), + BondingTarget: sdkmath.LegacyNewDecWithPrec(66, 2), + MaxVariance: sdkmath.LegacyZeroDec(), + }, + InflationDistribution: inflationtypes.InflationDistribution{ + StakingRewards: sdkmath.LegacyNewDecWithPrec(1000000, 6), + CommunityPool: sdkmath.LegacyZeroDec(), + }, + EnableInflation: false, + } + msg := &inflationtypes.MsgUpdateParams{ + Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), + Params: changeParams, + } + proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{msg}, "", "test", "description", proposer, false) + suite.Require().NoError(err) + suite.Commit() + + ok, err := suite.app.GovKeeper.AddDeposit(suite.ctx, proposal.Id, proposer, govParams.MinDeposit) + suite.Require().NoError(err) + suite.Require().True(ok) + suite.Commit() + + err = suite.app.GovKeeper.AddVote(suite.ctx, proposal.Id, proposer, govtypesv1.NewNonSplitVoteOption(govtypesv1.OptionYes), "") + suite.Require().NoError(err) + suite.CommitAfter(*govParams.VotingPeriod) + + proposal, err = suite.app.GovKeeper.Proposals.Get(suite.ctx, proposal.Id) + suite.Require().NoError(err) + suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status) + suite.Require().Equal(suite.app.InflationKeeper.GetParams(suite.ctx), changeParams) +} diff --git a/x/inflation/module.go b/x/inflation/module.go index a1c718c49..8a1865c8f 100644 --- a/x/inflation/module.go +++ b/x/inflation/module.go @@ -41,7 +41,9 @@ func (AppModuleBasic) Name() string { } // RegisterLegacyAminoCodec registers the inflation module's types on the given LegacyAmino codec. -func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + types.RegisterLegacyAminoCodec(cdc) +} // ConsensusVersion returns the consensus state-breaking version for the module. func (AppModuleBasic) ConsensusVersion() uint64 { @@ -135,6 +137,7 @@ func (am AppModule) NewHandler() baseapp.MsgServiceHandler { // RegisterServices registers a gRPC query service to respond to the // module-specific gRPC queries. func (am AppModule) RegisterServices(cfg module.Configurator) { + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) migrator := keeper.NewMigrator(am.keeper) diff --git a/x/inflation/types/codec.go b/x/inflation/types/codec.go index 091e3000e..134b15412 100644 --- a/x/inflation/types/codec.go +++ b/x/inflation/types/codec.go @@ -3,6 +3,8 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/msgservice" ) // ModuleCdc references the global incentives module codec. Note, the codec @@ -10,7 +12,29 @@ import ( // // The actual codec used for serialization should be provided to // modules/incentives and defined at the application level. -var ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) +var ( + amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) +) + +// method required for x/inflation msg GetSignBytes methods +func init() { + RegisterLegacyAminoCodec(amino) + amino.Seal() +} // RegisterInterfaces register implementations -func RegisterInterfaces(registry codectypes.InterfaceRegistry) {} +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + registry.RegisterImplementations( + (*sdk.Msg)(nil), + &MsgUpdateParams{}, + ) + + msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) +} + +// register inflation msg types for Amino Codec in adherence to EIP-712 signing conventions +func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterConcrete(&MsgUpdateParams{}, "canto/x/inflation/MsgUpdateParams", nil) + cdc.RegisterConcrete(&Params{}, "canto/x/inflation/Params", nil) +} diff --git a/x/onboarding/keeper/keeper_test.go b/x/onboarding/keeper/keeper_test.go index 342a29eeb..eed487238 100644 --- a/x/onboarding/keeper/keeper_test.go +++ b/x/onboarding/keeper/keeper_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/suite" + abci "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/crypto/tmhash" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" tmversion "github.com/cometbft/cometbft/proto/tendermint/version" @@ -15,10 +16,10 @@ import ( feemarkettypes "github.com/evmos/ethermint/x/feemarket/types" "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/evmos/ethermint/crypto/ethsecp256k1" "github.com/Canto-Network/Canto/v7/app" "github.com/Canto-Network/Canto/v7/x/onboarding/types" @@ -32,13 +33,13 @@ type KeeperTestSuite struct { app *app.Canto queryClient types.QueryClient queryClientEvm evm.QueryClient + validator stakingtypes.Validator } func (suite *KeeperTestSuite) SetupTest() { // consensus key - privCons, err := ethsecp256k1.GenerateKey() - suite.NoError(err) - consAddress := sdk.ConsAddress(privCons.PubKey().Address()) + pubKey := ed25519.GenPrivKey().PubKey() + consAddress := sdk.ConsAddress(pubKey.Address()) suite.app = app.Setup(false, feemarkettypes.DefaultGenesisState()) suite.ctx = suite.app.BaseApp.NewContextLegacy(false, tmproto.Header{ @@ -71,16 +72,21 @@ func (suite *KeeperTestSuite) SetupTest() { suite.queryClient = types.NewQueryClient(queryHelper) // Set Validator - valAddr := sdk.ValAddress(privCons.PubKey().Address().Bytes()) - validator, err := stakingtypes.NewValidator(valAddr.String(), privCons.PubKey(), stakingtypes.Description{}) + valAddr := sdk.ValAddress(pubKey.Address().Bytes()) + validator, err := stakingtypes.NewValidator(valAddr.String(), pubKey, stakingtypes.Description{}) suite.NoError(err) + validator = stakingkeeper.TestingUpdateValidator(suite.app.StakingKeeper, suite.ctx, validator, true) valbz, err := suite.app.StakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator()) - s.NoError(err) + suite.NoError(err) suite.app.StakingKeeper.Hooks().AfterValidatorCreated(suite.ctx, valbz) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) suite.NoError(err) + validators, err := suite.app.StakingKeeper.GetValidators(suite.ctx, 1) + suite.NoError(err) + suite.validator = validators[0] + stakingParams, err := suite.app.StakingKeeper.GetParams(suite.ctx) suite.NoError(err) stakingParams.BondDenom = "acanto" @@ -90,3 +96,25 @@ func (suite *KeeperTestSuite) SetupTest() { func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } + +func (suite *KeeperTestSuite) Commit() { + suite.CommitAfter(time.Nanosecond) +} + +func (suite *KeeperTestSuite) CommitAfter(t time.Duration) { + header := suite.ctx.BlockHeader() + header.Time = header.Time.Add(t) + suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{ + Height: header.Height, + Time: header.Time, + }) + suite.app.Commit() + + // update ctx + header.Height += 1 + suite.ctx = suite.app.BaseApp.NewUncachedContext(false, header) + + queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry()) + evm.RegisterQueryServer(queryHelper, suite.app.EvmKeeper) + suite.queryClientEvm = evm.NewQueryClient(queryHelper) +} diff --git a/x/onboarding/keeper/msg_server.go b/x/onboarding/keeper/msg_server.go index d5eec660e..cf4f3960d 100644 --- a/x/onboarding/keeper/msg_server.go +++ b/x/onboarding/keeper/msg_server.go @@ -33,9 +33,6 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam ctx := sdk.UnwrapSDKContext(goCtx) k.SetParams(ctx, req.Params) - // if err := k.SetParams(ctx, req.Params); err != nil { - // return nil, err - // } return &types.MsgUpdateParamsResponse{}, nil } diff --git a/x/onboarding/keeper/msg_server_test.go b/x/onboarding/keeper/msg_server_test.go new file mode 100644 index 000000000..f6abffc3a --- /dev/null +++ b/x/onboarding/keeper/msg_server_test.go @@ -0,0 +1,70 @@ +package keeper_test + +import ( + "math" + + sdkmath "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + "github.com/evmos/ethermint/crypto/ethsecp256k1" + + "github.com/Canto-Network/Canto/v7/testutil" + onboardingtypes "github.com/Canto-Network/Canto/v7/x/onboarding/types" +) + +func (suite *KeeperTestSuite) TestMsgUpdateParamsByProposal() { + suite.SetupTest() + + // set denom + stakingParams, err := suite.app.StakingKeeper.GetParams(suite.ctx) + suite.Require().NoError(err) + denom := stakingParams.BondDenom + + // change mindeposit for denom + govParams, err := suite.app.GovKeeper.Params.Get(suite.ctx) + suite.Require().NoError(err) + govParams.MinDeposit = []sdk.Coin{sdk.NewCoin(denom, sdkmath.NewInt(1))} + err = suite.app.GovKeeper.Params.Set(suite.ctx, govParams) + suite.Require().NoError(err) + + // create account + privKey, err := ethsecp256k1.GenerateKey() + suite.Require().NoError(err) + proposer := sdk.AccAddress(privKey.PubKey().Address().Bytes()) + + // deligate to validator + initAmount := sdkmath.NewInt(int64(math.Pow10(18)) * 2) + initBalance := sdk.NewCoins(sdk.NewCoin(denom, initAmount)) + testutil.FundAccount(suite.app.BankKeeper, suite.ctx, proposer, initBalance) + shares, err := suite.app.StakingKeeper.Delegate(suite.ctx, proposer, sdk.DefaultPowerReduction, stakingtypes.Unbonded, suite.validator, true) + suite.Require().NoError(err) + suite.Require().True(shares.GT(sdkmath.LegacyNewDec(0))) + + // submit proposal for change params + changeParams := onboardingtypes.NewParams(true, sdkmath.NewInt(10000), []string{"channel-0"}) + msg := &onboardingtypes.MsgUpdateParams{ + Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), + Params: changeParams, + } + proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{msg}, "", "test", "description", proposer, false) + suite.Require().NoError(err) + suite.Commit() + + ok, err := suite.app.GovKeeper.AddDeposit(suite.ctx, proposal.Id, proposer, govParams.MinDeposit) + suite.Require().NoError(err) + suite.Require().True(ok) + suite.Commit() + + err = suite.app.GovKeeper.AddVote(suite.ctx, proposal.Id, proposer, govtypesv1.NewNonSplitVoteOption(govtypesv1.OptionYes), "") + suite.Require().NoError(err) + suite.CommitAfter(*govParams.VotingPeriod) + + proposal, err = suite.app.GovKeeper.Proposals.Get(suite.ctx, proposal.Id) + suite.Require().NoError(err) + suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status) + suite.Require().Equal(suite.app.OnboardingKeeper.GetParams(suite.ctx), changeParams) +} diff --git a/x/onboarding/module.go b/x/onboarding/module.go index 85deea094..0c7a29110 100644 --- a/x/onboarding/module.go +++ b/x/onboarding/module.go @@ -40,7 +40,9 @@ func (AppModuleBasic) Name() string { } // RegisterLegacyAminoCodec performs a no-op as the onboarding doesn't support Amino encoding -func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + types.RegisterLegacyAminoCodec(cdc) +} // ConsensusVersion returns the consensus state-breaking version for the module. func (AppModuleBasic) ConsensusVersion() uint64 { @@ -50,6 +52,7 @@ func (AppModuleBasic) ConsensusVersion() uint64 { // RegisterInterfaces registers interfaces and implementations of the onboarding // module. func (AppModuleBasic) RegisterInterfaces(interfaceRegistry codectypes.InterfaceRegistry) { + types.RegisterInterfaces(interfaceRegistry) } // DefaultGenesis returns default genesis state as raw bytes for the onboarding @@ -118,6 +121,7 @@ func (AppModule) NewHandler() baseapp.MsgServiceHandler { } func (am AppModule) RegisterServices(cfg module.Configurator) { + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) } diff --git a/x/onboarding/types/codec.go b/x/onboarding/types/codec.go new file mode 100644 index 000000000..9f156bd84 --- /dev/null +++ b/x/onboarding/types/codec.go @@ -0,0 +1,35 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/msgservice" +) + +var ( + amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) +) + +// method required for x/csr msg GetSignBytes methods +func init() { + RegisterLegacyAminoCodec(amino) + amino.Seal() +} + +// register interfaces for the AminoCodec +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + registry.RegisterImplementations( + (*sdk.Msg)(nil), + &MsgUpdateParams{}, + ) + + msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) +} + +// register csr msg types for Amino Codec in adherence to EIP-712 signing conventions +func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterConcrete(&MsgUpdateParams{}, "canto/x/onboarding/MsgUpdateParams", nil) + cdc.RegisterConcrete(&Params{}, "canto/x/onboarding/Params", nil) +}