From 86f3900787970147697c8ba2f5bc8349242f0cd7 Mon Sep 17 00:00:00 2001 From: Trinity Date: Wed, 24 Apr 2024 13:20:34 +0700 Subject: [PATCH] add tests for decorator and keeper --- testutil/keepers/keepers.go | 7 + x/vesting/keeper/keeper_test.go | 34 ++++ .../keeper/permissioned_decorator_test.go | 152 ++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 x/vesting/keeper/keeper_test.go create mode 100644 x/vesting/keeper/permissioned_decorator_test.go diff --git a/testutil/keepers/keepers.go b/testutil/keepers/keepers.go index df346579..227f65b6 100644 --- a/testutil/keepers/keepers.go +++ b/testutil/keepers/keepers.go @@ -10,6 +10,7 @@ import ( hubgenkeeper "github.com/dymensionxyz/dymension-rdk/x/hub-genesis/keeper" mintkeeper "github.com/dymensionxyz/dymension-rdk/x/mint/keeper" seqkeeper "github.com/dymensionxyz/dymension-rdk/x/sequencers/keeper" + vestingkeeper "github.com/dymensionxyz/dymension-rdk/x/vesting/keeper" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" app "github.com/dymensionxyz/dymension-rdk/testutil/app" @@ -44,3 +45,9 @@ func NewTestHubGenesisKeeperFromApp(app *app.App) (*hubgenkeeper.Keeper, sdk.Con ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "rollapp-1", Time: time.Now().UTC()}) return k, ctx } + +func NewTestVestingKeeperFromApp(app *app.App) (*vestingkeeper.Keeper, sdk.Context) { + k := &app.VestingKeeper + ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "rollapp-1", Time: time.Now().UTC()}) + return k, ctx +} diff --git a/x/vesting/keeper/keeper_test.go b/x/vesting/keeper/keeper_test.go new file mode 100644 index 00000000..1c53781a --- /dev/null +++ b/x/vesting/keeper/keeper_test.go @@ -0,0 +1,34 @@ +package keeper_test + +import ( + "testing" + + testkeepers "github.com/dymensionxyz/dymension-rdk/testutil/keepers" + "github.com/dymensionxyz/dymension-rdk/testutil/utils" + "github.com/dymensionxyz/dymension-rdk/x/vesting/types" + "github.com/stretchr/testify/require" +) + +func TestParams(t *testing.T) { + // Setup the test environment + app := utils.Setup(t, false) + k, ctx := testkeepers.NewTestVestingKeeperFromApp(app) + + // Set some initial parameters + initialParams := types.DefaultParams() + initialParams.AllowedAddresses = []string{"cosmos19crd4fwzm9qtf5ln5l3e2vmquhevjwprk8tgxp", "cosmos1gusne8eh37myphx09hgdsy85zpl2t0kzdvu3en"} // Example addresses + k.SetParams(ctx, initialParams) + + // Retrieve the parameters + retrievedParams := k.GetParams(ctx) + + // Assert that the retrieved parameters match the initial ones + require.Equal(t, initialParams, retrievedParams, "retrieved parameters should match the initial ones") + + // Test setting and getting a different set of parameters + updatedParams := initialParams + updatedParams.AllowedAddresses = append(updatedParams.AllowedAddresses, "cosmos1s77x8wr2gzdhq8gt8c085vate0s23xu9u80wtx") + k.SetParams(ctx, updatedParams) + retrievedParams = k.GetParams(ctx) + require.Equal(t, updatedParams, retrievedParams, "retrieved parameters should match the updated ones") +} diff --git a/x/vesting/keeper/permissioned_decorator_test.go b/x/vesting/keeper/permissioned_decorator_test.go new file mode 100644 index 00000000..4435a6a5 --- /dev/null +++ b/x/vesting/keeper/permissioned_decorator_test.go @@ -0,0 +1,152 @@ +package keeper_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/client" + clienttx "github.com/cosmos/cosmos-sdk/client/tx" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/testutil/testdata" + sdk "github.com/cosmos/cosmos-sdk/types" + authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + sdkvestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" + "github.com/cosmos/cosmos-sdk/x/bank/testutil" + "github.com/stretchr/testify/suite" + + "github.com/dymensionxyz/dymension-rdk/testutil/app" + "github.com/dymensionxyz/dymension-rdk/testutil/ibctest" + testkeepers "github.com/dymensionxyz/dymension-rdk/testutil/keepers" + + "github.com/dymensionxyz/dymension-rdk/x/vesting/keeper" + "github.com/dymensionxyz/dymension-rdk/x/vesting/types" +) + +const ( + rollappDenom = "arax" +) + +type VestingKeeperTestSuite struct { + ibctest.IBCTestUtilSuite + + app *app.App + k *keeper.Keeper + ctx sdk.Context + clientCtx client.Context + txBuilder client.TxBuilder + fees sdk.Coins +} + +func TestVestingKeeperTestSuite(t *testing.T) { + suite.Run(t, new(VestingKeeperTestSuite)) +} + +func (suite *VestingKeeperTestSuite) setupTest() { + coinAmount := sdk.NewCoin(rollappDenom, sdk.NewInt(20)) + suite.fees = sdk.NewCoins(coinAmount) + + suite.IBCTestUtilSuite.SetupTest(rollappDenom) + suite.app = suite.RollAppChain.App.(*app.App) + suite.k, suite.ctx = testkeepers.NewTestVestingKeeperFromApp(suite.app) + suite.clientCtx = client.Context{}. + WithTxConfig(suite.app.GetTxConfig()). + WithCodec(suite.app.AppCodec()) + + suite.txBuilder = suite.app.GetTxConfig().NewTxBuilder() + suite.txBuilder.SetFeeAmount(suite.fees) + suite.txBuilder.SetGasLimit(200000) +} + +func (suite *VestingKeeperTestSuite) TestPermissionedVestingDecorator() { + suite.setupTest() + + // Generate a permission account + _, _, addr0 := testdata.KeyTestPubAddr() + acc0, err := sdk.Bech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), addr0) + suite.Require().NoError(err) + + suite.k.SetParams(suite.ctx, types.Params{ + AllowedAddresses: []string{acc0}, + }) + + _, _, addr1 := testdata.KeyTestPubAddr() + _, _, addr2 := testdata.KeyTestPubAddr() + + type testcase struct { + name string + msgs []sdk.Msg + msgTypeURLs []string + isSimulate bool // if blank, is false + expectPass bool + } + + tests := []testcase{ + { + name: "permission account should success", + msgs: []sdk.Msg{ + sdkvestingtypes.NewMsgCreateVestingAccount(addr0, addr1, sdk.NewCoins(), 10000000, false), + }, + msgTypeURLs: []string{ + sdk.MsgTypeURL(&sdkvestingtypes.MsgCreateVestingAccount{}), + }, + expectPass: true, + }, + { + name: "non permission account should return error", + msgs: []sdk.Msg{ + sdkvestingtypes.NewMsgCreateVestingAccount(addr1, addr2, sdk.NewCoins(), 10000000, false), + }, + msgTypeURLs: []string{ + sdk.MsgTypeURL(&sdkvestingtypes.MsgCreateVestingAccount{}), + }, + expectPass: false, + }, + } + + for _, tc := range tests { + tx := suite.CreateVestingMsgTxBuilder(tc.msgs) + + pvd := keeper.NewPermissionedVestingDecorator(suite.app.VestingKeeper, tc.msgTypeURLs) + antehandlerPVD := sdk.ChainAnteDecorators(pvd) + _, err := antehandlerPVD(suite.ctx, tx, tc.isSimulate) + if tc.expectPass { + + suite.Require().NoError(err, "test: %s", tc.name) + } else { + suite.Require().Error(err, "test: %s", tc.name) + } + } + +} + +func (suite *VestingKeeperTestSuite) CreateVestingMsgTxBuilder(msgs []sdk.Msg) authsigning.Tx { + + // TxBuilder components reset for every test case + priv0, _, addr0 := testdata.KeyTestPubAddr() + acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0) + suite.app.AccountKeeper.SetAccount(suite.ctx, acc1) + privs, accNums, accSeqs := []cryptotypes.PrivKey{priv0}, []uint64{0}, []uint64{0} + signerData := authsigning.SignerData{ + ChainID: suite.ctx.ChainID(), + AccountNumber: accNums[0], + Sequence: accSeqs[0], + } + + suite.txBuilder.SetMsgs(msgs...) + + sigV2, _ := clienttx.SignWithPrivKey( + 1, + signerData, + suite.txBuilder, + privs[0], + suite.clientCtx.TxConfig, + accSeqs[0], + ) + suite.txBuilder.SetSignatures(sigV2) + suite.txBuilder.SetMemo("") + + err := testutil.FundAccount(suite.app.BankKeeper, suite.ctx, addr0, suite.fees) + suite.Require().NoError(err) + + tx := suite.txBuilder.GetTx() + return tx +}