Skip to content

Commit

Permalink
test: Add prooposal test
Browse files Browse the repository at this point in the history
  • Loading branch information
dudong2 committed May 9, 2024
1 parent 5afc5c9 commit 354fb4c
Show file tree
Hide file tree
Showing 15 changed files with 340 additions and 259 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ func NewCanto(
app.AccountKeeper,
app.Erc20Keeper,
govKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.CSRKeeper = csrkeeper.NewKeeper(
Expand Down
5 changes: 1 addition & 4 deletions x/csr/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ func (suite *KeeperTestSuite) SetupApp() {
suite.app.StakingKeeper.Hooks().AfterValidatorCreated(suite.ctx, valbz)
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
require.NoError(t, err)

validators, err := s.app.StakingKeeper.GetValidators(s.ctx, 1)
require.NoError(t, err)
suite.validator = validators[0]
suite.validator = validator

suite.ethSigner = ethtypes.LatestSignerForChainID(s.app.EvmKeeper.ChainID())
}
Expand Down
68 changes: 45 additions & 23 deletions x/csr/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
csrtypes "github.com/Canto-Network/Canto/v7/x/csr/types"
)

func (suite *KeeperTestSuite) TestMsgUpdateParamsByProposal() {
func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
suite.SetupTest()

// change mindeposit for denom
Expand All @@ -33,30 +33,52 @@ func (suite *KeeperTestSuite) TestMsgUpdateParamsByProposal() {
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,
testCases := []struct {
name string
msg sdk.Msg
checkFunc func(uint64)
}{
{
"ok - proposal MsgUpdateParams",
&csrtypes.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: csrtypes.Params{
EnableCsr: false,
CsrShares: sdkmath.LegacyNewDecWithPrec(20, 2),
},
},
func(proposalId uint64) {
changeParams := csrtypes.Params{
EnableCsr: false,
CsrShares: sdkmath.LegacyNewDecWithPrec(20, 2),
}

proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposalId)
suite.Require().NoError(err)
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)
suite.Require().Equal(suite.app.CSRKeeper.GetParams(suite.ctx), 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()
for _, tc := range testCases {
suite.Run(tc.name, func() {
// submit proposal
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{tc.msg}, "", "test", "description", proposer, false)
suite.Require().NoError(err)
suite.Commit()

err = suite.app.GovKeeper.AddVote(suite.ctx, proposal.Id, proposer, govtypesv1.NewNonSplitVoteOption(govtypesv1.OptionYes), "")
suite.Require().NoError(err)
suite.CommitAfter(*govParams.VotingPeriod)
ok, err := suite.app.GovKeeper.AddDeposit(suite.ctx, proposal.Id, proposer, govParams.MinDeposit)
suite.Require().NoError(err)
suite.Require().True(ok)
suite.Commit()

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)
err = suite.app.GovKeeper.AddVote(suite.ctx, proposal.Id, proposer, govtypesv1.NewNonSplitVoteOption(govtypesv1.OptionYes), "")
suite.Require().NoError(err)
suite.CommitAfter(*govParams.VotingPeriod)

// check proposal result
tc.checkFunc(proposal.Id)
})
}
}
13 changes: 0 additions & 13 deletions x/erc20/client/proposal_handler.go

This file was deleted.

20 changes: 17 additions & 3 deletions x/erc20/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type KeeperTestSuite struct {
ethSigner ethtypes.Signer
signer keyring.Signer
mintFeeCollector bool
validator stakingtypes.Validator
}

var s *KeeperTestSuite
Expand All @@ -90,7 +91,6 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {

// consensus key
pubKey := ed25519.GenPrivKey().PubKey()
require.NoError(t, err)
suite.consAddress = sdk.ConsAddress(pubKey.Address())

// setup feemarketGenesis params
Expand Down Expand Up @@ -173,12 +173,18 @@ func (suite *KeeperTestSuite) DoSetupTest(t require.TestingT) {

suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

// Set Validator
valAddr := sdk.ValAddress(suite.address.Bytes())
validator, err := stakingtypes.NewValidator(valAddr.String(), pubKey, stakingtypes.Description{})
require.NoError(t, err)

valbz, err := s.app.StakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
s.NoError(err)
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
suite.app.StakingKeeper.Hooks().AfterValidatorCreated(suite.ctx, valbz)
err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator)
require.NoError(t, err)
suite.app.StakingKeeper.SetValidator(suite.ctx, validator)
suite.validator = validator

encodingConfig := encoding.MakeTestEncodingConfig()
suite.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
Expand Down Expand Up @@ -342,7 +348,12 @@ func (suite *KeeperTestSuite) DeployContractDirectBalanceManipulation(name strin
return crypto.CreateAddress(suite.address, nonce)
}

// Commit commits and starts a new block with an updated context.
func (suite *KeeperTestSuite) Commit() {
suite.CommitAfter(time.Second * 0)
}

func (suite *KeeperTestSuite) CommitAfter(t time.Duration) {
header := suite.ctx.BlockHeader()
suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: header.Height,
Expand All @@ -351,8 +362,11 @@ func (suite *KeeperTestSuite) Commit() {
suite.app.Commit()

header.Height += 1
header.Time = header.Time.Add(t)
suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: header.Height,
Height: header.Height,
Time: header.Time,
ProposerAddress: suite.consAddress,
})

// update ctx
Expand Down
170 changes: 167 additions & 3 deletions x/erc20/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ package keeper_test

import (
"fmt"
"math"
"math/big"

sdkmath "cosmossdk.io/math"
"github.com/Canto-Network/Canto/v7/x/erc20/keeper"
"github.com/Canto-Network/Canto/v7/x/erc20/types"
inflationtypes "github.com/Canto-Network/Canto/v7/x/inflation/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/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/ethereum/go-ethereum/common"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/evmos/ethermint/tests"
"github.com/evmos/ethermint/x/evm/statedb"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/mock"

"github.com/Canto-Network/Canto/v7/testutil"
"github.com/Canto-Network/Canto/v7/x/erc20/keeper"
"github.com/Canto-Network/Canto/v7/x/erc20/types"
inflationtypes "github.com/Canto-Network/Canto/v7/x/inflation/types"
)

const (
Expand Down Expand Up @@ -1500,3 +1506,161 @@ func (suite *KeeperTestSuite) TestConvertERC20NativeIBCVoucher() {
}
suite.mintFeeCollector = false
}

func (suite *KeeperTestSuite) TestMsgExecutionByProposal() {
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)))

var (
erc20Address = suite.DeployContractDirectBalanceManipulation(erc20Name, erc20Symbol).String()
)

testCases := []struct {
name string
msg sdk.Msg
malleate func()
checkFunc func(uint64)
}{
{
"ok - proposal MsgRegisterCoin",
&types.MsgRegisterCoin{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Title: "MsgRegisterCoin",
Description: "MsgRegisterCoin test",
Metadata: banktypes.Metadata{
Description: "ATOM IBC voucher (channel 14)",
Base: ibcBase,
// NOTE: Denom units MUST be increasing
DenomUnits: []*banktypes.DenomUnit{
{
Denom: ibcBase,
Exponent: 0,
},
},
Name: "ATOM channel-14",
Symbol: "ibcATOM-14",
Display: ibcBase,
},
},
func() {
err := suite.app.BankKeeper.MintCoins(suite.ctx, inflationtypes.ModuleName, sdk.Coins{sdk.NewInt64Coin(ibcBase, 1)})
suite.Require().NoError(err)
},
func(proposalId uint64) {
proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposalId)
suite.Require().NoError(err)
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)

id := suite.app.Erc20Keeper.GetTokenPairID(suite.ctx, ibcBase)
pair, ok := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, id)
suite.Require().True(ok)
suite.Require().Equal(suite.app.Erc20Keeper.GetDenomMap(suite.ctx, pair.Denom), id)
suite.Require().Equal(suite.app.Erc20Keeper.GetERC20Map(suite.ctx, common.HexToAddress(pair.Erc20Address)), id)
},
},
{
"ok - proposal MsgRegisterERC20",
&types.MsgRegisterERC20{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Title: "MsgRegisterERC20",
Description: "MsgRegisterERC20 test",
Erc20Address: erc20Address,
},
func() {},
func(proposalId uint64) {
proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposalId)
suite.Require().NoError(err)
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)

id := suite.app.Erc20Keeper.GetTokenPairID(suite.ctx, erc20Address)
pair, ok := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, id)
suite.Require().True(ok)
suite.Require().Equal(suite.app.Erc20Keeper.GetDenomMap(suite.ctx, pair.Denom), id)
suite.Require().Equal(suite.app.Erc20Keeper.GetERC20Map(suite.ctx, common.HexToAddress(pair.Erc20Address)), id)
},
},
{
"ok - proposal MsgToggleTokenConversion",
&types.MsgToggleTokenConversion{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Title: "MsgToggleTokenConversion",
Description: "MsgToggleTokenConversion test",
Token: erc20Address,
},
func() {},
func(proposalId uint64) {
proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposalId)
suite.Require().NoError(err)
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)

id := suite.app.Erc20Keeper.GetTokenPairID(suite.ctx, erc20Address)
pair, ok := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, id)
suite.Require().True(ok)
suite.Require().Equal(pair.Enabled, false)
},
},
{
"ok - proposal MsgUpdateParams",
&types.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: types.NewParams(false, false),
},
func() {},
func(proposalId uint64) {
changeParams := types.NewParams(false, false)

proposal, err := suite.app.GovKeeper.Proposals.Get(suite.ctx, proposalId)
suite.Require().NoError(err)
suite.Require().Equal(govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED, proposal.Status)
suite.Require().Equal(suite.app.Erc20Keeper.GetParams(suite.ctx), changeParams)
},
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.malleate()

// submit proposal
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{tc.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)

// check proposal result
tc.checkFunc(proposal.Id)
})
}
}
Loading

0 comments on commit 354fb4c

Please sign in to comment.