From 15637dad5dc73697b7c2bc61d1ad616d21175daf Mon Sep 17 00:00:00 2001 From: sampocs Date: Wed, 2 Aug 2023 18:03:03 -0500 Subject: [PATCH 1/3] copied over test --- x/staking/keeper/msg_server_test.go | 133 ++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index 545365ee5cff..ed867d35d9e2 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -7,6 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" simapp "github.com/cosmos/cosmos-sdk/simapp" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -1221,3 +1222,135 @@ func TestICADelegateUndelegate(t *testing.T) { require.True(t, found, "validator should have been found") require.Equal(t, sdk.ZeroDec(), validator.LiquidShares, "validator liquid shares after undelegation") } + +func TestCancelUnbondingDelegation(t *testing.T) { + // setup the app + app := simapp.Setup(t, false) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + msgServer := keeper.NewMsgServerImpl(app.StakingKeeper) + bondDenom := app.StakingKeeper.BondDenom(ctx) + + // set the not bonded pool module account + notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx) + startTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 5) + + require.NoError(t, simapp.FundModuleAccount(app.BankKeeper, ctx, notBondedPool.GetName(), sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), startTokens)))) + app.AccountKeeper.SetModuleAccount(ctx, notBondedPool) + + moduleBalance := app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), app.StakingKeeper.BondDenom(ctx)) + require.Equal(t, sdk.NewInt64Coin(bondDenom, startTokens.Int64()), moduleBalance) + + // accounts + delAddrs := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(10000)) + validators := app.StakingKeeper.GetValidators(ctx, 10) + require.Equal(t, len(validators), 1) + + validatorAddr, err := sdk.ValAddressFromBech32(validators[0].OperatorAddress) + require.NoError(t, err) + delegatorAddr := delAddrs[0] + + // setting the ubd entry + unbondingAmount := sdk.NewInt64Coin(app.StakingKeeper.BondDenom(ctx), 5) + ubd := types.NewUnbondingDelegation( + delegatorAddr, validatorAddr, 10, + ctx.BlockTime().Add(time.Minute*10), + unbondingAmount.Amount, + ) + + // set and retrieve a record + app.StakingKeeper.SetUnbondingDelegation(ctx, ubd) + resUnbond, found := app.StakingKeeper.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr) + require.True(t, found) + require.Equal(t, ubd, resUnbond) + + testCases := []struct { + Name string + ExceptErr bool + req types.MsgCancelUnbondingDelegation + }{ + { + Name: "invalid height", + ExceptErr: true, + req: types.MsgCancelUnbondingDelegation{ + DelegatorAddress: resUnbond.DelegatorAddress, + ValidatorAddress: resUnbond.ValidatorAddress, + Amount: sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), sdk.NewInt(4)), + CreationHeight: 0, + }, + }, + { + Name: "invalid coin", + ExceptErr: true, + req: types.MsgCancelUnbondingDelegation{ + DelegatorAddress: resUnbond.DelegatorAddress, + ValidatorAddress: resUnbond.ValidatorAddress, + Amount: sdk.NewCoin("dump_coin", sdk.NewInt(4)), + CreationHeight: 0, + }, + }, + { + Name: "validator not exists", + ExceptErr: true, + req: types.MsgCancelUnbondingDelegation{ + DelegatorAddress: resUnbond.DelegatorAddress, + ValidatorAddress: sdk.ValAddress(sdk.AccAddress("asdsad")).String(), + Amount: unbondingAmount, + CreationHeight: 0, + }, + }, + { + Name: "invalid delegator address", + ExceptErr: true, + req: types.MsgCancelUnbondingDelegation{ + DelegatorAddress: "invalid_delegator_addrtess", + ValidatorAddress: resUnbond.ValidatorAddress, + Amount: unbondingAmount, + CreationHeight: 0, + }, + }, + { + Name: "invalid amount", + ExceptErr: true, + req: types.MsgCancelUnbondingDelegation{ + DelegatorAddress: resUnbond.DelegatorAddress, + ValidatorAddress: resUnbond.ValidatorAddress, + Amount: unbondingAmount.Add(sdk.NewInt64Coin(bondDenom, 10)), + CreationHeight: 10, + }, + }, + { + Name: "success", + ExceptErr: false, + req: types.MsgCancelUnbondingDelegation{ + DelegatorAddress: resUnbond.DelegatorAddress, + ValidatorAddress: resUnbond.ValidatorAddress, + Amount: unbondingAmount.Sub(sdk.NewInt64Coin(bondDenom, 1)), + CreationHeight: 10, + }, + }, + { + Name: "success", + ExceptErr: false, + req: types.MsgCancelUnbondingDelegation{ + DelegatorAddress: resUnbond.DelegatorAddress, + ValidatorAddress: resUnbond.ValidatorAddress, + Amount: unbondingAmount.Sub(unbondingAmount.Sub(sdk.NewInt64Coin(bondDenom, 1))), + CreationHeight: 10, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.Name, func(t *testing.T) { + _, err := msgServer.CancelUnbondingDelegation(ctx, &testCase.req) + if testCase.ExceptErr { + require.Error(t, err) + } else { + require.NoError(t, err) + balanceForNotBondedPool := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(notBondedPool.GetAddress()), bondDenom) + require.Equal(t, balanceForNotBondedPool, moduleBalance.Sub(testCase.req.Amount)) + moduleBalance = moduleBalance.Sub(testCase.req.Amount) + } + }) + } +} From 47f9991183475fb1e35a80e753e726117925a4b8 Mon Sep 17 00:00:00 2001 From: sampocs Date: Wed, 2 Aug 2023 18:03:30 -0500 Subject: [PATCH 2/3] WIP: migrate to old testing framework --- x/staking/keeper/msg_server_test.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index ed867d35d9e2..4c137432e364 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" simapp "github.com/cosmos/cosmos-sdk/simapp" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -1225,28 +1224,32 @@ func TestICADelegateUndelegate(t *testing.T) { func TestCancelUnbondingDelegation(t *testing.T) { // setup the app - app := simapp.Setup(t, false) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + _, app, ctx := createTestInput() msgServer := keeper.NewMsgServerImpl(app.StakingKeeper) bondDenom := app.StakingKeeper.BondDenom(ctx) // set the not bonded pool module account notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx) startTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 5) + startCoin := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), startTokens)) - require.NoError(t, simapp.FundModuleAccount(app.BankKeeper, ctx, notBondedPool.GetName(), sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), startTokens)))) + require.NoError(t, simapp.FundModuleAccount(app.BankKeeper, ctx, notBondedPool.GetName(), startCoin)) app.AccountKeeper.SetModuleAccount(ctx, notBondedPool) moduleBalance := app.BankKeeper.GetBalance(ctx, notBondedPool.GetAddress(), app.StakingKeeper.BondDenom(ctx)) require.Equal(t, sdk.NewInt64Coin(bondDenom, startTokens.Int64()), moduleBalance) - // accounts - delAddrs := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(10000)) - validators := app.StakingKeeper.GetValidators(ctx, 10) - require.Equal(t, len(validators), 1) + // create a validator + validatorPubKey := simapp.CreateTestPubKeys(1)[0] + validatorAddr := sdk.ValAddress(validatorPubKey.Address()) - validatorAddr, err := sdk.ValAddressFromBech32(validators[0].OperatorAddress) - require.NoError(t, err) + validator := teststaking.NewValidator(t, validatorAddr, validatorPubKey) + validator.Tokens = startTokens + validator.DelegatorShares = sdk.NewDecFromInt(startTokens) + app.StakingKeeper.SetValidator(ctx, validator) + + // create a delegator + delAddrs := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(10000)) delegatorAddr := delAddrs[0] // setting the ubd entry @@ -1255,6 +1258,7 @@ func TestCancelUnbondingDelegation(t *testing.T) { delegatorAddr, validatorAddr, 10, ctx.BlockTime().Add(time.Minute*10), unbondingAmount.Amount, + 1, ) // set and retrieve a record @@ -1342,7 +1346,7 @@ func TestCancelUnbondingDelegation(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.Name, func(t *testing.T) { - _, err := msgServer.CancelUnbondingDelegation(ctx, &testCase.req) + _, err := msgServer.CancelUnbondingDelegation(sdk.WrapSDKContext(ctx), &testCase.req) if testCase.ExceptErr { require.Error(t, err) } else { From 8f4c9dc622dcda82c1a065dc5a602a9dcb9598e9 Mon Sep 17 00:00:00 2001 From: sampocs Date: Thu, 3 Aug 2023 14:27:16 -0500 Subject: [PATCH 3/3] fixed unit test --- x/staking/keeper/msg_server_test.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/x/staking/keeper/msg_server_test.go b/x/staking/keeper/msg_server_test.go index 4c137432e364..501ecfb8264f 100644 --- a/x/staking/keeper/msg_server_test.go +++ b/x/staking/keeper/msg_server_test.go @@ -1246,6 +1246,7 @@ func TestCancelUnbondingDelegation(t *testing.T) { validator := teststaking.NewValidator(t, validatorAddr, validatorPubKey) validator.Tokens = startTokens validator.DelegatorShares = sdk.NewDecFromInt(startTokens) + validator.Status = types.Bonded app.StakingKeeper.SetValidator(ctx, validator) // create a delegator @@ -1282,16 +1283,6 @@ func TestCancelUnbondingDelegation(t *testing.T) { CreationHeight: 0, }, }, - { - Name: "invalid coin", - ExceptErr: true, - req: types.MsgCancelUnbondingDelegation{ - DelegatorAddress: resUnbond.DelegatorAddress, - ValidatorAddress: resUnbond.ValidatorAddress, - Amount: sdk.NewCoin("dump_coin", sdk.NewInt(4)), - CreationHeight: 0, - }, - }, { Name: "validator not exists", ExceptErr: true,