Skip to content

Commit

Permalink
test: Add MsgUpdateParams test
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed May 9, 2024
1 parent 722fb25 commit 9cafddf
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 35 deletions.
7 changes: 4 additions & 3 deletions x/csr/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions x/csr/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
62 changes: 62 additions & 0 deletions x/csr/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions x/csr/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
11 changes: 10 additions & 1 deletion x/csr/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
7 changes: 4 additions & 3 deletions x/epochs/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions x/erc20/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions x/inflation/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions x/inflation/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
84 changes: 84 additions & 0 deletions x/inflation/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 4 additions & 1 deletion x/inflation/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 26 additions & 2 deletions x/inflation/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@ 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
// should ONLY be used in certain instances of tests and for JSON encoding.
//
// 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)
}
Loading

0 comments on commit 9cafddf

Please sign in to comment.