From eb3f719fba9fbcb0ad6605ec6bfb6be7362e3b87 Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Mon, 12 Aug 2024 09:21:14 +0000 Subject: [PATCH 1/6] Fix convert failure from eth pubkey to tendermint pubkey --- x/compliance/keeper/keeper_test.go | 8 ++-- x/evm/handler_test.go | 42 +++++++++------------ x/evm/keeper/keeper_sgxvm_test.go | 36 ++++++++---------- x/evm/keeper/keeper_test.go | 59 +++++++++++++----------------- x/feemarket/keeper/keeper_test.go | 36 ++++++++---------- 5 files changed, 79 insertions(+), 102 deletions(-) diff --git a/x/compliance/keeper/keeper_test.go b/x/compliance/keeper/keeper_test.go index bad8563d..72d74c15 100644 --- a/x/compliance/keeper/keeper_test.go +++ b/x/compliance/keeper/keeper_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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" @@ -62,9 +63,8 @@ func (suite *KeeperTestSuite) Setup(t *testing.T) { address := common.BytesToAddress(priv.PubKey().Address().Bytes()) // consensus key - privCons, err := ethsecp256k1.GenerateKey() - require.NoError(t, err) - consAddress := sdk.ConsAddress(privCons.PubKey().Address()) + pks := simtestutil.CreateTestPubKeys(1) + consAddress := sdk.ConsAddress(pks[0].Address()) header := testutil.NewHeader( 1, time.Now().UTC(), chainID, consAddress, nil, nil, @@ -86,7 +86,7 @@ func (suite *KeeperTestSuite) Setup(t *testing.T) { // Set Validator valAddr := sdk.ValAddress(address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr, privCons.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr, pks[0], stakingtypes.Description{}) require.NoError(t, err) validator = stakingkeeper.TestingUpdateValidator(&suite.app.StakingKeeper, suite.ctx, validator, true) err = suite.app.StakingKeeper.Hooks().AfterValidatorCreated(suite.ctx, validator.GetOperator()) diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index 68432bca..bbb6a314 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -1,37 +1,36 @@ package evm_test import ( + "errors" "math/big" "testing" "time" sdkmath "cosmossdk.io/math" - "github.com/gogo/protobuf/proto" - + "cosmossdk.io/simapp" + "github.com/SigmaGmbH/librustgo" abci "github.com/cometbft/cometbft/abci/types" + "github.com/cometbft/cometbft/crypto/tmhash" tmjson "github.com/cometbft/cometbft/libs/json" - - "cosmossdk.io/simapp" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmversion "github.com/cometbft/cometbft/proto/tendermint/version" + "github.com/cometbft/cometbft/version" + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + 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" - - feemarkettypes "swisstronik/x/feemarket/types" - - "errors" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" - "swisstronik/app" "swisstronik/crypto/ethsecp256k1" "swisstronik/tests" @@ -40,13 +39,7 @@ import ( "swisstronik/x/evm" "swisstronik/x/evm/keeper" "swisstronik/x/evm/types" - - "github.com/cometbft/cometbft/crypto/tmhash" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - tmversion "github.com/cometbft/cometbft/proto/tendermint/version" - - "github.com/SigmaGmbH/librustgo" - "github.com/cometbft/cometbft/version" + feemarkettypes "swisstronik/x/feemarket/types" ) type EvmTestSuite struct { @@ -79,9 +72,8 @@ func (suite *EvmTestSuite) DoSetupTest(t require.TestingT) { suite.signer = tests.NewTestSigner(priv) suite.from = address // consensus key - priv, err = ethsecp256k1.GenerateKey() - require.NoError(t, err) - consAddress := sdk.ConsAddress(priv.PubKey().Address()) + pks := simtestutil.CreateTestPubKeys(1) + consAddress := sdk.ConsAddress(pks[0].Address()) suite.app = app.Setup(func(app *app.App, genesis simapp.GenesisState) simapp.GenesisState { if suite.dynamicTxFee { @@ -161,7 +153,7 @@ func (suite *EvmTestSuite) DoSetupTest(t require.TestingT) { suite.app.AccountKeeper.SetAccount(suite.ctx, acc) valAddr := sdk.ValAddress(address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr, pks[0], stakingtypes.Description{}) require.NoError(t, err) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) diff --git a/x/evm/keeper/keeper_sgxvm_test.go b/x/evm/keeper/keeper_sgxvm_test.go index 2b21b8a9..2b7ab786 100644 --- a/x/evm/keeper/keeper_sgxvm_test.go +++ b/x/evm/keeper/keeper_sgxvm_test.go @@ -7,19 +7,27 @@ import ( "math/big" "time" - "github.com/SigmaGmbH/librustgo" - - feemarkettypes "swisstronik/x/feemarket/types" - sdkmath "cosmossdk.io/math" "cosmossdk.io/simapp" + "github.com/SigmaGmbH/librustgo" + abci "github.com/cometbft/cometbft/abci/types" + "github.com/cometbft/cometbft/crypto/tmhash" tmjson "github.com/cometbft/cometbft/libs/json" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmversion "github.com/cometbft/cometbft/proto/tendermint/version" + "github.com/cometbft/cometbft/version" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/params" "github.com/stretchr/testify/require" "swisstronik/app" @@ -30,18 +38,7 @@ import ( evmcommontypes "swisstronik/types" "swisstronik/x/evm/types" evmtypes "swisstronik/x/evm/types" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" - ethtypes "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/params" - - 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" - "github.com/cometbft/cometbft/version" + feemarkettypes "swisstronik/x/feemarket/types" ) func (suite *KeeperTestSuite) SetupSGXVMTest() { @@ -76,9 +73,8 @@ func (suite *KeeperTestSuite) SetupSGXVMAppWithT(t require.TestingT) { suite.signer = tests.NewTestSigner(priv) // consensus key - priv, err = ethsecp256k1.GenerateKey() - require.NoError(t, err) - suite.consAddress = sdk.ConsAddress(priv.PubKey().Address()) + pks := simtestutil.CreateTestPubKeys(1) + suite.consAddress = sdk.ConsAddress(pks[0].Address()) suite.app = app.Setup(func(app *app.App, genesis simapp.GenesisState) simapp.GenesisState { feemarketGenesis := feemarkettypes.DefaultGenesisState() @@ -171,7 +167,7 @@ func (suite *KeeperTestSuite) SetupSGXVMAppWithT(t require.TestingT) { suite.app.AccountKeeper.SetAccount(suite.ctx, acc) valAddr := sdk.ValAddress(suite.address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr, pks[0], stakingtypes.Description{}) require.NoError(t, err) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) require.NoError(t, err) diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index eb5cb14f..6028c6ba 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -10,31 +10,38 @@ import ( "testing" "time" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/store/prefix" - authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" - authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - sdkmath "cosmossdk.io/math" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" - - feemarkettypes "swisstronik/x/feemarket/types" - "cosmossdk.io/simapp" + "github.com/SigmaGmbH/librustgo" + abci "github.com/cometbft/cometbft/abci/types" + "github.com/cometbft/cometbft/crypto/tmhash" tmjson "github.com/cometbft/cometbft/libs/json" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmversion "github.com/cometbft/cometbft/proto/tendermint/version" + "github.com/cometbft/cometbft/version" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keyring" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/store/prefix" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" + authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/params" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" "swisstronik/app" "swisstronik/crypto/ethsecp256k1" @@ -42,22 +49,9 @@ import ( "swisstronik/server/config" "swisstronik/tests" evmcommontypes "swisstronik/types" - evmtypes "swisstronik/x/evm/types" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" - ethtypes "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/params" - "swisstronik/utils" - - "github.com/SigmaGmbH/librustgo" - 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" - "github.com/cometbft/cometbft/version" + evmtypes "swisstronik/x/evm/types" + feemarkettypes "swisstronik/x/feemarket/types" ) type KeeperTestSuite struct { @@ -141,9 +135,8 @@ func (suite *KeeperTestSuite) SetupAppWithT(t require.TestingT, chainID string) suite.signer = tests.NewTestSigner(priv) // consensus key - priv, err = ethsecp256k1.GenerateKey() - require.NoError(t, err) - suite.consAddress = sdk.ConsAddress(priv.PubKey().Address()) + pks := simtestutil.CreateTestPubKeys(1) + suite.consAddress = sdk.ConsAddress(pks[0].Address()) suite.app = app.Setup(func(app *app.App, genesis simapp.GenesisState) simapp.GenesisState { feemarketGenesis := feemarkettypes.DefaultGenesisState() @@ -239,7 +232,7 @@ func (suite *KeeperTestSuite) SetupAppWithT(t require.TestingT, chainID string) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) valAddr := sdk.ValAddress(suite.address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr, pks[0], stakingtypes.Description{}) require.NoError(t, err) err = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, validator) require.NoError(t, err) diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go index e6874cb4..6d224b6e 100644 --- a/x/feemarket/keeper/keeper_test.go +++ b/x/feemarket/keeper/keeper_test.go @@ -6,20 +6,27 @@ import ( "testing" "time" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/stretchr/testify/require" - "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" + "github.com/cometbft/cometbft/version" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keyring" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/ethereum/go-ethereum/common" + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" "swisstronik/app" "swisstronik/crypto/ethsecp256k1" @@ -29,16 +36,6 @@ import ( "swisstronik/utils" evmtypes "swisstronik/x/evm/types" "swisstronik/x/feemarket/types" - - "github.com/ethereum/go-ethereum/common" - ethtypes "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - - 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" - "github.com/cometbft/cometbft/version" ) type KeeperTestSuite struct { @@ -86,9 +83,8 @@ func (suite *KeeperTestSuite) SetupApp() { suite.signer = tests.NewTestSigner(priv) // consensus key - priv, err = ethsecp256k1.GenerateKey() - require.NoError(t, err) - suite.consAddress = sdk.ConsAddress(priv.PubKey().Address()) + pks := simtestutil.CreateTestPubKeys(1) + suite.consAddress = sdk.ConsAddress(pks[0].Address()) suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{ Height: 1, @@ -126,7 +122,7 @@ func (suite *KeeperTestSuite) SetupApp() { suite.app.AccountKeeper.SetAccount(suite.ctx, acc) valAddr := sdk.ValAddress(suite.address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr, priv.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr, pks[0], stakingtypes.Description{}) require.NoError(t, err) validator = stakingkeeper.TestingUpdateValidator(&suite.app.StakingKeeper, suite.ctx, validator, true) err = suite.app.StakingKeeper.Hooks().AfterValidatorCreated(suite.ctx, validator.GetOperator()) From 010fb3416976e501e65ee750cef0b1fa5ed88a3c Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Mon, 12 Aug 2024 09:21:45 +0000 Subject: [PATCH 2/6] Optimize msg delivery functions --- x/vesting/keeper/integration_test.go | 1252 +++++++++++--------------- 1 file changed, 543 insertions(+), 709 deletions(-) diff --git a/x/vesting/keeper/integration_test.go b/x/vesting/keeper/integration_test.go index 069b1db7..59b398dc 100644 --- a/x/vesting/keeper/integration_test.go +++ b/x/vesting/keeper/integration_test.go @@ -9,6 +9,7 @@ import ( "cosmossdk.io/math" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" 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" @@ -19,7 +20,6 @@ import ( "swisstronik/app" "swisstronik/app/ante" - "swisstronik/crypto/ethsecp256k1" "swisstronik/tests" "swisstronik/testutil" utiltx "swisstronik/testutil/tx" @@ -39,12 +39,22 @@ type VestingTestSuite struct { querier keeper.Querier msgServer types.MsgServer stkQuerier stakingkeeper.Querier + + initialVesting sdk.Coins // initial vesting coins + gasCoins sdk.Coins // additional funds to pay gas fees for tx + + vaPrivKey cryptotypes.PrivKey // private key of vesting account + va sdk.AccAddress // vesting account + funder sdk.AccAddress // funder account who initiates monthly vesting } func TestVestingTestSuite(t *testing.T) { // Run Ginkgo integration tests RegisterFailHandler(Fail) RunSpecs(t, "Vesting Keeper Suite") + + s := new(VestingTestSuite) + suite.Run(t, s) } // Commit commits and starts a new block with an updated context. @@ -60,6 +70,47 @@ func (suite *VestingTestSuite) CommitAfter(t time.Duration) error { return err } +func (suite *VestingTestSuite) ExpectFundCoins(address sdk.AccAddress, coins sdk.Coins) { + err := testutil.FundAccount(suite.ctx, suite.app.BankKeeper, address, coins) + Expect(err).To(BeNil()) + err = suite.Commit() + Expect(err).To(BeNil()) +} + +func (suite *VestingTestSuite) ExpectDelegateSuccess(privKey cryptotypes.PrivKey, delegating sdk.Coin, validator stakingtypes.Validator) { + res, err := testutil.Delegate( + suite.ctx, + suite.app, + privKey, + delegating, + validator, + ) + Expect(err).To(BeNil()) + Expect(res.IsOK()).To(BeTrue()) + err = suite.Commit() + Expect(err).To(BeNil()) +} + +func (suite *VestingTestSuite) ExpectUndelegateSuccess(privKey cryptotypes.PrivKey, undelegating sdk.Coin, validator stakingtypes.Validator) { + // Undelegate delegated amount and consume gas + res, err := testutil.Undelegate( + suite.ctx, + suite.app, + privKey, + undelegating, + validator, + ) + Expect(err).To(BeNil()) + Expect(res.IsOK()).To(BeTrue()) + + // Wait unbonding time to finish undelegation + ut := suite.app.StakingKeeper.UnbondingTime(suite.ctx) + err = suite.CommitAfter(ut) + Expect(err).To(BeNil()) + err = suite.Commit() + Expect(err).To(BeNil()) +} + func (suite *VestingTestSuite) validateEthVestingTransactionDecorator(msgs ...sdk.Msg) error { dec := ante.NewEthVestingTransactionDecorator(suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.EvmKeeper) return testutil.ValidateAnteForMsgs(suite.ctx, dec, msgs...) @@ -113,11 +164,8 @@ func (suite *VestingTestSuite) SetupTest() error { address := tests.RandomAccAddress() - privCons, err := ethsecp256k1.GenerateKey() - if err != nil { - return err - } - consAddress := sdk.ConsAddress(privCons.PubKey().Address()) + pks := simtestutil.CreateTestPubKeys(1) + consAddress := sdk.ConsAddress(pks[0].Address()) header := testutil.NewHeader( 1, time.Now().UTC(), chainID, consAddress, nil, nil, @@ -127,7 +175,7 @@ func (suite *VestingTestSuite) SetupTest() error { stakingParams := appS.StakingKeeper.GetParams(suite.ctx) stakingParams.BondDenom = utils.BaseDenom - err = appS.StakingKeeper.SetParams(suite.ctx, stakingParams) + err := appS.StakingKeeper.SetParams(suite.ctx, stakingParams) if err != nil { return err } @@ -142,7 +190,7 @@ func (suite *VestingTestSuite) SetupTest() error { // Set Validator valAddr := sdk.ValAddress(address.Bytes()) - validator, err := stakingtypes.NewValidator(valAddr, privCons.PubKey(), stakingtypes.Description{}) + validator, err := stakingtypes.NewValidator(valAddr, pks[0], stakingtypes.Description{}) if err != nil { return err } @@ -199,7 +247,8 @@ func validateVestingAccountBalances( app *app.App, address sdk.AccAddress, // address to monthly vesting account prevDelegatedFree, prevDelegatedVesting sdk.Coins, - delegating, undelegating, expVested, expUnvested, initialBalance, sentAmount, receivedAmount, consumedFee sdk.Coins, + delegating, undelegating sdk.Coins, + expVested, expUnvested, initialBalance, sentAmount, receivedAmount, consumedFee sdk.Coins, ) { mva, err := app.VestingKeeper.GetMonthlyVestingAccount(ctx, address) Expect(err).To(BeNil()) @@ -332,14 +381,9 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { var ( s *VestingTestSuite - vaPrivKey cryptotypes.PrivKey // private key of vesting account - va sdk.AccAddress // vesting account - funder sdk.AccAddress // funder account who initiates monthly vesting - user sdk.AccAddress + user sdk.AccAddress - initialVesting sdk.Coins // initial vesting coins - extraCoins sdk.Coins // additional funds to pay gas fees for tx - mva *types.MonthlyVestingAccount + mva *types.MonthlyVestingAccount unvested sdk.Coins vested sdk.Coins @@ -357,47 +401,42 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { now := s.ctx.BlockTime() var from common.Address - from, vaPrivKey = tests.RandomEthAddressWithPrivateKey() - va = sdk.AccAddress(from.Bytes()) + from, s.vaPrivKey = tests.RandomEthAddressWithPrivateKey() + s.va = sdk.AccAddress(from.Bytes()) user = tests.RandomAccAddress() - funder = tests.RandomAccAddress() + s.funder = tests.RandomAccAddress() amount := math.NewInt(1e17).Mul(math.NewInt(months)) - initialVesting = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, amount)) + s.initialVesting = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, amount)) // Fund coins to funder to create monthly vesting - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, funder, initialVesting) - Expect(err).To(BeNil()) - err = s.Commit() + s.ExpectFundCoins(s.funder, s.initialVesting) // Create monthly vesting resp, err := s.msgServer.HandleCreateMonthlyVestingAccount(s.goCtx, &types.MsgCreateMonthlyVestingAccount{ - FromAddress: funder.String(), - ToAddress: va.String(), + FromAddress: s.funder.String(), + ToAddress: s.va.String(), CliffDays: cliffDays, // 30 days Months: months, // 3 months - Amount: initialVesting, + Amount: s.initialVesting, }) Expect(resp).To(Equal(&types.MsgCreateMonthlyVestingAccountResponse{})) err = s.Commit() Expect(err).To(BeNil()) // Query monthly vesting account - mva, err = s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, err = s.querier.GetMonthlyVestingAccount(s.ctx, s.va) Expect(err).To(BeNil()) Expect(mva).ToNot(BeNil()) // Fund again as spendable coins for gas consuming - extraCoins = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e6))) - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.gasCoins = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e6))) + s.ExpectFundCoins(mva.GetAddress(), s.gasCoins) // Check if all the coins are unvested at beginning unvested := mva.GetVestingCoins(now) vested := mva.GetVestedCoins(now) - Expect(unvested).To(Equal(initialVesting)) + Expect(unvested).To(Equal(s.initialVesting)) Expect(vested.IsZero()).To(BeTrue()) }) @@ -411,68 +450,60 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { now := s.ctx.BlockTime() unvested = mva.GetVestingCoins(now) vested = mva.GetVestedCoins(now) - Expect(unvested).To(Equal(initialVesting)) + Expect(unvested).To(Equal(s.initialVesting)) Expect(vested.IsZero()).To(BeTrue()) - expUnvested = initialVesting + expUnvested = s.initialVesting expVested = sdk.NewCoins() // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // vesting - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - sdk.NewCoins(), // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // vesting + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + sdk.NewCoins(), // consumed fee ) }) It("can transfer spendable coins", func() { - err := testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), unvested) - Expect(err).To(BeNil()) - err = s.app.BankKeeper.SendCoins(s.ctx, mva.GetAddress(), user, unvested) + s.ExpectFundCoins(s.va, unvested) + + err := s.app.BankKeeper.SendCoins(s.ctx, s.va, user, unvested) Expect(err).To(BeNil()) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, + s.va, sdk.NewCoins(), // prev delegated free sdk.NewCoins(), // prev delegated vesting nil, // delegating nil, // undelegating expVested, // vested expUnvested, // unvested - initialVesting.Add(extraCoins...).Add(unvested...), // initial balance + s.initialVesting.Add(s.gasCoins...).Add(unvested...), // initial balance unvested, // sent amount sdk.NewCoins(), // received amount sdk.NewCoins(), // consumed fee ) }) It("cannot transfer unvested coins", func() { - err := s.app.BankKeeper.SendCoins(s.ctx, va, user, unvested) + err := s.app.BankKeeper.SendCoins(s.ctx, s.va, user, unvested) Expect(err).ToNot(BeNil()) }) It("can delegate unvested coins", func() { // Delegate unvested coins delegating := unvested - res, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) // Check delegation was created successfully delegations, err := s.stkQuerier.DelegatorDelegations( @@ -489,17 +520,17 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) }) @@ -515,71 +546,61 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { now := s.ctx.BlockTime() unvested = mva.GetVestingCoins(now) vested = mva.GetVestedCoins(now) - Expect(unvested).To(Equal(initialVesting)) + Expect(unvested).To(Equal(s.initialVesting)) Expect(vested.IsZero()).To(BeTrue()) - expUnvested = initialVesting + expUnvested = s.initialVesting expVested = sdk.NewCoins() // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - sdk.NewCoins(), // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + sdk.NewCoins(), // consumed fee ) }) It("can transfer spendable coins", func() { coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e18))) - err := testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), coins) - Expect(err).To(BeNil()) - err = s.app.BankKeeper.SendCoins(s.ctx, mva.GetAddress(), user, coins) + s.ExpectFundCoins(s.va, coins) + + err := s.app.BankKeeper.SendCoins(s.ctx, s.va, user, coins) Expect(err).To(BeNil()) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, + s.va, sdk.NewCoins(), // prev delegated free sdk.NewCoins(), // prev delegated vesting nil, // delegating nil, // undelegating expVested, // vested expUnvested, // unvested - initialVesting.Add(extraCoins...).Add(coins...), // initial balance + s.initialVesting.Add(s.gasCoins...).Add(coins...), // initial balance coins, // sent amount sdk.NewCoins(), // received amount sdk.NewCoins(), // consumed fee ) }) It("cannot transfer unvested coins", func() { - err := s.app.BankKeeper.SendCoins(s.ctx, va, user, unvested) + err := s.app.BankKeeper.SendCoins(s.ctx, s.va, user, unvested) Expect(err).ToNot(BeNil()) }) It("can delegate portion of unvested coins", func() { // Delegate portion of unvested and consume gas - delegating := unvested.Sub(extraCoins...) - res, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) - err = s.Commit() - Expect(err).To(BeNil()) + delegating := unvested.Sub(s.gasCoins...) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) // Check delegation was created successfully delegations, err := s.stkQuerier.DelegatorDelegations( @@ -596,55 +617,40 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) // Fund gas coins for next transaction - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) // Undelegate portion of delegated amount and consume gas undelegating := delegating.QuoInt(sdk.NewInt(2)) - res, err = testutil.Undelegate( - s.ctx, - s.app, - vaPrivKey, - undelegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) - - ut := s.app.StakingKeeper.UnbondingTime(s.ctx) - err = s.CommitAfter(ut) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], s.validator) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - undelegating, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + undelegating, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) It("can delegate all unvested coins", func() { @@ -652,8 +658,8 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { res, err := testutil.Delegate( s.ctx, s.app, - vaPrivKey, - unvested[0].Add(extraCoins[0]), + s.vaPrivKey, + unvested[0].Add(s.gasCoins[0]), s.validator, ) Expect(err).ToNot(BeNil()) @@ -661,15 +667,14 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { Expect(res.IsOK()).To(BeTrue()) // Fund gas coins for next transaction - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) // Delegate unvested coins, consume gas delegating := unvested res, err = testutil.Delegate( s.ctx, s.app, - vaPrivKey, + s.vaPrivKey, delegating[0], s.validator, ) @@ -691,72 +696,56 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) // Fund gas coins for next transaction - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) // Undelegate all unvested amount and consume gas undelegating := delegating - res, err = testutil.Undelegate( - s.ctx, - s.app, - vaPrivKey, - undelegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) - - ut := s.app.StakingKeeper.UnbondingTime(s.ctx) - err = s.CommitAfter(ut) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], s.validator) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - undelegating, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + undelegating, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) It("can perform ethereum tx with spendable coins", func() { coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e18))) - err := testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), coins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, coins) amount := coins.AmountOf(utils.BaseDenom) - msg, err := utiltx.CreateEthTx(s.ctx, s.app, vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) + msg, err := utiltx.CreateEthTx(s.ctx, s.app, s.vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) Expect(err).To(BeNil()) - s.assertSuccessEthNative(mva.GetAddress(), user, amount, utils.BaseDenom, vaPrivKey, msg) + s.assertSuccessEthNative(mva.GetAddress(), user, amount, utils.BaseDenom, s.vaPrivKey, msg) }) It("cannot perform ethereum tx with unvested coins", func() { amount := unvested.AmountOf(utils.BaseDenom) - msg, err := utiltx.CreateEthTx(s.ctx, s.app, vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) + msg, err := utiltx.CreateEthTx(s.ctx, s.app, s.vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) Expect(err).To(BeNil()) - s.assertFailEthNative(vaPrivKey, msg) + s.assertFailEthNative(s.vaPrivKey, msg) }) }) @@ -772,8 +761,8 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { now := s.ctx.BlockTime() vested = mva.GetVestedCoins(now) unvested = mva.GetVestingCoins(now) - expVested = initialVesting.QuoInt(sdk.NewInt(months)) - expUnvested = initialVesting.Sub(initialVesting.QuoInt(sdk.NewInt(months))...) + expVested = s.initialVesting.QuoInt(sdk.NewInt(months)) + expUnvested = s.initialVesting.Sub(s.initialVesting.QuoInt(sdk.NewInt(months))...) Expect(vested).To(Equal(expVested)) Expect(unvested).To(Equal(expUnvested)) @@ -783,45 +772,38 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { Expect(balances.Vested).To(Equal(expVested)) Expect(balances.Unvested).To(Equal(expUnvested)) // All coins from vesting schedule should be locked - Expect(balances.Locked).To(Equal(initialVesting.Sub(expVested...))) + Expect(balances.Locked).To(Equal(s.initialVesting.Sub(expVested...))) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - sdk.NewCoins(), // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + sdk.NewCoins(), // consumed fee ) }) It("can delegate portion of vested coins", func() { // Verify that the total spendable coins should include vested amount. spendablePre := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePre).To(Equal(vested.Add(extraCoins...))) + Expect(spendablePre).To(Equal(vested.Add(s.gasCoins...))) // Delegate the vested coins, consume gas delegating := vested.QuoInt(sdk.NewInt(2)) - _, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) // Check spendable coins have not been reduced except gas fee. // Delegate unvested coins first and then vested coins. spendablePost := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePost).To(Equal(spendablePre.Sub(extraCoins...))) + Expect(spendablePost).To(Equal(spendablePre.Sub(s.gasCoins...))) // Check delegation was created successfully delegations, err := s.stkQuerier.DelegatorDelegations( @@ -838,28 +820,27 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) // Fund gas coins for next transaction - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) // Undelegate more than delegated amount and consume gas undelegating := vested res, err := testutil.Undelegate( s.ctx, s.app, - vaPrivKey, + s.vaPrivKey, undelegating[0], s.validator, ) @@ -868,61 +849,41 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { Expect(res.IsOK()).To(BeTrue()) // Fund gas coins for next transaction - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) // Undelegate more than delegated amount and consume gas undelegating = delegating - res, err = testutil.Undelegate( - s.ctx, - s.app, - vaPrivKey, - undelegating[0], - s.validator, - ) - - ut := s.app.StakingKeeper.UnbondingTime(s.ctx) - err = s.CommitAfter(ut) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], s.validator) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - undelegating, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + undelegating, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) It("can delegate unvested + vested coins", func() { // Verify that the total spendable coins should include vested amount. spendablePre := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePre).To(Equal(vested.Add(extraCoins...))) + Expect(spendablePre).To(Equal(vested.Add(s.gasCoins...))) // Delegate the vested coins, consume gas - delegating := initialVesting - _, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) + delegating := s.initialVesting + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) // Delegate unvested coins first and then vested coins. // Check spendable coins have been reduced vested coins as well. spendablePost := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePost).To(Equal(spendablePre.Sub(extraCoins...).Sub(vested...))) + Expect(spendablePost).To(Equal(spendablePre.Sub(s.gasCoins...).Sub(vested...))) // Check delegation was created successfully delegations, err := s.stkQuerier.DelegatorDelegations( @@ -939,78 +900,54 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) // Fund gas coins for next transaction - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) // Undelegate portion of delegated amount and consume gas undelegating := delegating - res, err := testutil.Undelegate( - s.ctx, - s.app, - vaPrivKey, - undelegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) - - ut := s.app.StakingKeeper.UnbondingTime(s.ctx) - err = s.CommitAfter(ut) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], s.validator) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - undelegating, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + undelegating, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) It("can delegate coins from account balance and initial vesting", func() { // Funds some coins to delegate coinsToDelegate := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e18))) - err := testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), coinsToDelegate) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, coinsToDelegate) // Verify that the total spendable coins should include vested coins and newly funded coins. spendablePre := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePre).To(Equal(vested.Add(extraCoins...).Add(coinsToDelegate...))) + Expect(spendablePre).To(Equal(vested.Add(s.gasCoins...).Add(coinsToDelegate...))) // Delegate funds not in vesting schedule - delegating := sdk.NewCoins(coinsToDelegate.Add(initialVesting...)...) - res, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) + delegating := sdk.NewCoins(coinsToDelegate.Add(s.initialVesting...)...) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) // Check spendable balance is updated properly spendablePost := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) @@ -1031,17 +968,17 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) It("can transfer vested coins", func() { @@ -1052,17 +989,17 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - vested, // sent amount - sdk.NewCoins(), // received amount - sdk.NewCoins(), // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + vested, // sent amount + sdk.NewCoins(), // received amount + sdk.NewCoins(), // consumed fee ) }) It("cannot transfer unvested coins", func() { @@ -1071,28 +1008,28 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { }) It("can perform ethereum tx with vested coins", func() { amount := vested.AmountOf(utils.BaseDenom) - msg, err := utiltx.CreateEthTx(s.ctx, s.app, vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) + msg, err := utiltx.CreateEthTx(s.ctx, s.app, s.vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) Expect(err).To(BeNil()) - s.assertSuccessEthNative(mva.GetAddress(), user, amount, utils.BaseDenom, vaPrivKey, msg) + s.assertSuccessEthNative(mva.GetAddress(), user, amount, utils.BaseDenom, s.vaPrivKey, msg) }) It("cannot perform ethereum tx with unvested coins", func() { amount := unvested.AmountOf(utils.BaseDenom) - msg, err := utiltx.CreateEthTx(s.ctx, s.app, vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) + msg, err := utiltx.CreateEthTx(s.ctx, s.app, s.vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) Expect(err).To(BeNil()) - s.assertFailEthNative(vaPrivKey, msg) + s.assertFailEthNative(s.vaPrivKey, msg) }) It("cannot perform ethereum tx with vested coins if not sufficient coins for gas consuming", func() { // Send extra coins to another user to empty extra balance - err := s.app.BankKeeper.SendCoins(s.ctx, mva.GetAddress(), user, extraCoins) + err := s.app.BankKeeper.SendCoins(s.ctx, mva.GetAddress(), user, s.gasCoins) Expect(err).To(BeNil()) // Try to send vested amount, but should be failed because of gas fee amount := vested.AmountOf(utils.BaseDenom) - msg, err := utiltx.CreateEthTx(s.ctx, s.app, vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) + msg, err := utiltx.CreateEthTx(s.ctx, s.app, s.vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) Expect(err).To(BeNil()) err = s.validateEthVestingTransactionDecorator(msg) Expect(err).To(BeNil()) - _, err = testutil.DeliverEthTx(s.app, vaPrivKey, msg) + _, err = testutil.DeliverEthTx(s.app, s.vaPrivKey, msg) Expect(err).ToNot(BeNil()) Expect(err).Should(MatchError(ContainSubstring("Insert account failed. Empty response"))) }) @@ -1110,7 +1047,7 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { now := s.ctx.BlockTime() vested = mva.GetVestedCoins(now) unvested = mva.GetVestingCoins(now) - expVested = initialVesting + expVested = s.initialVesting expUnvested = sdk.NewCoins() Expect(vested).To(Equal(expVested)) Expect(unvested).To(Equal(expUnvested)) @@ -1126,64 +1063,57 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - sdk.NewCoins(), // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + sdk.NewCoins(), // consumed fee ) }) It("can send entire initial vesting coins", func() { spendablePre := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePre).To(Equal(initialVesting.Add(extraCoins...))) + Expect(spendablePre).To(Equal(s.initialVesting.Add(s.gasCoins...))) - err := s.app.BankKeeper.SendCoins(s.ctx, mva.GetAddress(), user, initialVesting) + err := s.app.BankKeeper.SendCoins(s.ctx, mva.GetAddress(), user, s.initialVesting) Expect(err).To(BeNil()) spendablePost := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePost).To(Equal(extraCoins)) + Expect(spendablePost).To(Equal(s.gasCoins)) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - initialVesting, // sent amount - sdk.NewCoins(), // received amount - sdk.NewCoins(), // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + s.initialVesting, // sent amount + sdk.NewCoins(), // received amount + sdk.NewCoins(), // consumed fee ) }) It("can delegate portion of initial vesting coins", func() { // Verify that the total spendable coins should include initial vesting coins. spendablePre := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePre).To(Equal(initialVesting.Add(extraCoins...))) + Expect(spendablePre).To(Equal(s.initialVesting.Add(s.gasCoins...))) // Delegate the initial vesting coins - delegating := initialVesting.QuoInt(sdk.NewInt(3)) - _, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) + delegating := s.initialVesting.QuoInt(sdk.NewInt(3)) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) spendablePost := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePost).To(Equal(spendablePre.Sub(extraCoins...).Sub(delegating...))) + Expect(spendablePost).To(Equal(spendablePre.Sub(s.gasCoins...).Sub(delegating...))) // Check delegation was created successfully delegations, err := s.stkQuerier.DelegatorDelegations( @@ -1201,37 +1131,30 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) It("can delegate initial vesting coins", func() { // Verify that the total spendable coins should include initial vesting coins. spendablePre := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePre).To(Equal(initialVesting.Add(extraCoins...))) + Expect(spendablePre).To(Equal(s.initialVesting.Add(s.gasCoins...))) // Delegate the initial vesting coins - delegating := initialVesting - _, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) + delegating := s.initialVesting + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) spendablePost := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) - Expect(spendablePost).To(Equal(spendablePre.Sub(extraCoins...).Sub(delegating...))) + Expect(spendablePost).To(Equal(spendablePre.Sub(s.gasCoins...).Sub(delegating...))) // Check delegation was created successfully delegations, err := s.stkQuerier.DelegatorDelegations( @@ -1249,62 +1172,47 @@ var _ = Describe("Monthly Vesting Account", Ordered, func() { validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) // Fund gas coins for next transaction - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), extraCoins) - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) // Undelegate portion of delegated amount and consume gas undelegating := delegating - res, err := testutil.Undelegate( - s.ctx, - s.app, - vaPrivKey, - undelegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) - - ut := s.app.StakingKeeper.UnbondingTime(s.ctx) - err = s.CommitAfter(ut) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], s.validator) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - undelegating, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(extraCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - extraCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + undelegating, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) }) It("can perform ethereum tx with initial vesting coins", func() { - amount := initialVesting.AmountOf(utils.BaseDenom) - msg, err := utiltx.CreateEthTx(s.ctx, s.app, vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) + amount := s.initialVesting.AmountOf(utils.BaseDenom) + msg, err := utiltx.CreateEthTx(s.ctx, s.app, s.vaPrivKey, mva.GetAddress(), user, amount.BigInt(), 0) Expect(err).To(BeNil()) - s.assertSuccessEthNative(mva.GetAddress(), user, amount, utils.BaseDenom, vaPrivKey, msg) + s.assertSuccessEthNative(mva.GetAddress(), user, amount, utils.BaseDenom, s.vaPrivKey, msg) }) }) }) @@ -1313,13 +1221,7 @@ var _ = Describe("Additional tests for Monthly Vesting Account", Ordered, func() var ( s *VestingTestSuite - vaPrivKey cryptotypes.PrivKey // private key of vesting account - va sdk.AccAddress - funder sdk.AccAddress - user sdk.AccAddress - - initialVesting sdk.Coins - gasCoins sdk.Coins + user sdk.AccAddress err error @@ -1333,18 +1235,16 @@ var _ = Describe("Additional tests for Monthly Vesting Account", Ordered, func() Expect(err).To(BeNil()) var from common.Address - from, vaPrivKey = tests.RandomEthAddressWithPrivateKey() - va = sdk.AccAddress(from.Bytes()) + from, s.vaPrivKey = tests.RandomEthAddressWithPrivateKey() + s.va = sdk.AccAddress(from.Bytes()) user = tests.RandomAccAddress() - funder = tests.RandomAccAddress() + s.funder = tests.RandomAccAddress() - initialVesting = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1000, 18))) - gasCoins = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e6))) + s.initialVesting = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1000, 18))) + s.gasCoins = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e6))) // Fund coins to funder to create monthly vesting account - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, funder, initialVesting) - Expect(err).To(BeNil()) - err = s.Commit() + s.ExpectFundCoins(s.funder, s.initialVesting) }) Context("Create vesting account cliff days 10, month 12", func() { @@ -1353,75 +1253,64 @@ var _ = Describe("Additional tests for Monthly Vesting Account", Ordered, func() BeforeEach(func() { // Create monthly vesting account resp, err := s.msgServer.HandleCreateMonthlyVestingAccount(s.goCtx, &types.MsgCreateMonthlyVestingAccount{ - FromAddress: funder.String(), - ToAddress: va.String(), + FromAddress: s.funder.String(), + ToAddress: s.va.String(), CliffDays: 10, Months: 12, - Amount: initialVesting, + Amount: s.initialVesting, }) Expect(resp).To(Equal(&types.MsgCreateMonthlyVestingAccountResponse{})) err = s.Commit() Expect(err).To(BeNil()) // Query monthly vesting account - mva, err := s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, err := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) Expect(err).To(BeNil()) Expect(mva).ToNot(BeNil()) // Fund coins to mva for gas consuming - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), gasCoins) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) - expUnvested = initialVesting + expUnvested = s.initialVesting expVested = sdk.NewCoins() // stake 500 swtr delegating = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) - res, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) }) It("validate", func() { // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( s.ctx, s.app, - va, - delegating, // delegated vesting = 500 swtr - sdk.NewCoins(), // delegated free = 0 swtr - delegating, // locked = 500 swtr - sdk.NewCoins(), // vested = 0 swtr - initialVesting, // unvested = 1000 swtr - subCoins(initialVesting, delegating), // balances = 500 swtr - sdk.NewCoins(), // spendable balances = 0 swtr + s.va, + delegating, // delegated vesting = 500 swtr + sdk.NewCoins(), // delegated free = 0 swtr + delegating, // locked = 500 swtr + sdk.NewCoins(), // vested = 0 swtr + s.initialVesting, // unvested = 1000 swtr + subCoins(s.initialVesting, delegating), // balances = 500 swtr + sdk.NewCoins(), // spendable balances = 0 swtr ) }) It("undelegate after cliff", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) // Pass after cliff duration := time.Duration(mva.CliffTime-s.ctx.BlockTime().Unix()) * time.Second @@ -1429,141 +1318,120 @@ var _ = Describe("Additional tests for Monthly Vesting Account", Ordered, func() Expect(err).To(BeNil()) // Fund coins to mva for gas consuming - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), gasCoins) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) undelegating := delegating - res, err := testutil.Undelegate( - s.ctx, - s.app, - vaPrivKey, - undelegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], s.validator) - ut := s.app.StakingKeeper.UnbondingTime(s.ctx) - err = s.CommitAfter(ut) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) - - expUnvested = initialVesting + expUnvested = s.initialVesting expVested = sdk.NewCoins() // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - undelegating, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + undelegating, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( // todo s.ctx, s.app, - va, - sdk.NewCoins(), // delegated vesting = 0 swtr - sdk.NewCoins(), // delegated free = 0 swtr - initialVesting, // locked = 1000 swtr - sdk.NewCoins(), // vested = 0 swtr - initialVesting, // unvested = 1000 swtr - initialVesting, // balances = 1000 swtr - sdk.NewCoins(), // spendable balances = 0 swtr + s.va, + sdk.NewCoins(), // delegated vesting = 0 swtr + sdk.NewCoins(), // delegated free = 0 swtr + s.initialVesting, // locked = 1000 swtr + sdk.NewCoins(), // vested = 0 swtr + s.initialVesting, // unvested = 1000 swtr + s.initialVesting, // balances = 1000 swtr + sdk.NewCoins(), // spendable balances = 0 swtr ) }) It("wait until the end of vesting period", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) duration := time.Duration(mva.EndTime-s.ctx.BlockTime().Unix()) * time.Second err = s.CommitAfter(duration) Expect(err).To(BeNil()) - expVested = initialVesting + expVested = s.initialVesting expUnvested = sdk.NewCoins() // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( s.ctx, s.app, - va, - delegating, // delegated vesting = 500 swtr - sdk.NewCoins(), // delegated free = 0 swtr - sdk.NewCoins(), // locked = 0 swtr - initialVesting, // vested = 1000 swtr - sdk.NewCoins(), // unvested = 0 swtr - subCoins(initialVesting, delegating), // balances = 500 swtr - subCoins(initialVesting, delegating), // spendable balances = 500 swtr + s.va, + delegating, // delegated vesting = 500 swtr + sdk.NewCoins(), // delegated free = 0 swtr + sdk.NewCoins(), // locked = 0 swtr + s.initialVesting, // vested = 1000 swtr + sdk.NewCoins(), // unvested = 0 swtr + subCoins(s.initialVesting, delegating), // balances = 500 swtr + subCoins(s.initialVesting, delegating), // spendable balances = 500 swtr ) }) It("receive coins", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) - coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(300, 18))) - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), coins) - Expect(err).To(BeNil()) - err = s.Commit() + s.ExpectFundCoins(s.va, coins) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - coins, // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + coins, // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( s.ctx, s.app, - va, - delegating, // delegated vesting = 500 swtr - sdk.NewCoins(), // delegated free = 0 swtr - delegating, // locked = 500 swtr - sdk.NewCoins(), // vested = 0 swtr - initialVesting, // unvested = 1000 swtr - addCoins(subCoins(initialVesting, delegating), coins), // balances = 500 swtr + 300 swtr + s.va, + delegating, // delegated vesting = 500 swtr + sdk.NewCoins(), // delegated free = 0 swtr + delegating, // locked = 500 swtr + sdk.NewCoins(), // vested = 0 swtr + s.initialVesting, // unvested = 1000 swtr + addCoins(subCoins(s.initialVesting, delegating), coins), // balances = 500 swtr + 300 swtr coins, // spendable balances = 300 swtr ) }) It("failed in sending coins", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) // Try to send 300 coins from monthly vesting account coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(300, 18))) @@ -1578,215 +1446,181 @@ var _ = Describe("Additional tests for Monthly Vesting Account", Ordered, func() BeforeEach(func() { // Create monthly vesting account resp, err := s.msgServer.HandleCreateMonthlyVestingAccount(s.goCtx, &types.MsgCreateMonthlyVestingAccount{ - FromAddress: funder.String(), - ToAddress: va.String(), + FromAddress: s.funder.String(), + ToAddress: s.va.String(), CliffDays: 0, Months: 12, - Amount: initialVesting, + Amount: s.initialVesting, }) Expect(resp).To(Equal(&types.MsgCreateMonthlyVestingAccountResponse{})) err = s.Commit() Expect(err).To(BeNil()) // Query monthly vesting account - mva, err := s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, err := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) Expect(err).To(BeNil()) Expect(mva).ToNot(BeNil()) // Fund coins to mva for gas consuming - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), gasCoins) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) - expUnvested = initialVesting + expUnvested = s.initialVesting expVested = sdk.NewCoins() // stake 500 swtr delegating = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) - res, err := testutil.Delegate( - s.ctx, - s.app, - vaPrivKey, - delegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], s.validator) }) It("validate", func() { // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( s.ctx, s.app, - va, - delegating, // delegated vesting = 500 swtr - sdk.NewCoins(), // delegated free = 0 swtr - delegating, // locked = 500 swtr - sdk.NewCoins(), // vested = 0 swtr - initialVesting, // unvested = 1000 swtr - subCoins(initialVesting, delegating), // balances = 500 swtr - sdk.NewCoins(), // spendable balances = 0 swtr + s.va, + delegating, // delegated vesting = 500 swtr + sdk.NewCoins(), // delegated free = 0 swtr + delegating, // locked = 500 swtr + sdk.NewCoins(), // vested = 0 swtr + s.initialVesting, // unvested = 1000 swtr + subCoins(s.initialVesting, delegating), // balances = 500 swtr + sdk.NewCoins(), // spendable balances = 0 swtr ) }) It("undelegate after cliff", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) - // No need to pass, cliff 0 // Fund coins to mva for gas consuming - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), gasCoins) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) + s.ExpectFundCoins(s.va, s.gasCoins) undelegating := delegating - res, err := testutil.Undelegate( - s.ctx, - s.app, - vaPrivKey, - undelegating[0], - s.validator, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], s.validator) - ut := s.app.StakingKeeper.UnbondingTime(s.ctx) - err = s.CommitAfter(ut) - Expect(err).To(BeNil()) - err = s.Commit() - Expect(err).To(BeNil()) - - expUnvested = initialVesting + expUnvested = s.initialVesting expVested = sdk.NewCoins() // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - undelegating, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + undelegating, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( s.ctx, s.app, - va, - sdk.NewCoins(), // delegated vesting = 0 swtr - sdk.NewCoins(), // delegated free = 0 swtr - initialVesting, // locked = 1000 swtr - sdk.NewCoins(), // vested = 0 swtr - initialVesting, // unvested = 1000 swtr - initialVesting, // balances = 1000 swtr - sdk.NewCoins(), // spendable balances = 0 swtr + s.va, + sdk.NewCoins(), // delegated vesting = 0 swtr + sdk.NewCoins(), // delegated free = 0 swtr + s.initialVesting, // locked = 1000 swtr + sdk.NewCoins(), // vested = 0 swtr + s.initialVesting, // unvested = 1000 swtr + s.initialVesting, // balances = 1000 swtr + sdk.NewCoins(), // spendable balances = 0 swtr ) }) It("wait until the end of vesting period", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) duration := time.Duration(mva.EndTime-s.ctx.BlockTime().Unix()) * time.Second //duration := time.Duration(mva.EndTime-mva.StartTime) * time.Second err = s.CommitAfter(duration) Expect(err).To(BeNil()) - expVested = initialVesting + expVested = s.initialVesting expUnvested = sdk.NewCoins() // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - delegating, // prev delegated vesting - nil, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - sdk.NewCoins(), // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + delegating, // prev delegated vesting + nil, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + sdk.NewCoins(), // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( s.ctx, s.app, - va, - delegating, // delegated vesting = 500 swtr - sdk.NewCoins(), // delegated free = 0 swtr - sdk.NewCoins(), // locked = 0 swtr - initialVesting, // vested = 1000 swtr - sdk.NewCoins(), // unvested = 0 swtr - subCoins(initialVesting, delegating), // balances = 500 swtr - subCoins(initialVesting, delegating), // spendable balances = 500 swtr + s.va, + delegating, // delegated vesting = 500 swtr + sdk.NewCoins(), // delegated free = 0 swtr + sdk.NewCoins(), // locked = 0 swtr + s.initialVesting, // vested = 1000 swtr + sdk.NewCoins(), // unvested = 0 swtr + subCoins(s.initialVesting, delegating), // balances = 500 swtr + subCoins(s.initialVesting, delegating), // spendable balances = 500 swtr ) }) It("receive coins", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) - coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(300, 18))) - err = testutil.FundAccount(s.ctx, s.app.BankKeeper, mva.GetAddress(), coins) - Expect(err).To(BeNil()) - err = s.Commit() + s.ExpectFundCoins(s.va, coins) // Check all the balances of vesting account validateVestingAccountBalances( s.ctx, s.app, - va, - sdk.NewCoins(), // prev delegated free - sdk.NewCoins(), // prev delegated vesting - delegating, // delegating - nil, // undelegating - expVested, // vested - expUnvested, // unvested - initialVesting.Add(gasCoins...), // initial balance - sdk.NewCoins(), // sent amount - coins, // received amount - gasCoins, // consumed fee + s.va, + sdk.NewCoins(), // prev delegated free + sdk.NewCoins(), // prev delegated vesting + delegating, // delegating + nil, // undelegating + expVested, // vested + expUnvested, // unvested + s.initialVesting.Add(s.gasCoins...), // initial balance + sdk.NewCoins(), // sent amount + coins, // received amount + s.gasCoins, // consumed fee ) validateVestingAccountBalancesWithValues( s.ctx, s.app, - va, - delegating, // delegated vesting = 500 swtr - sdk.NewCoins(), // delegated free = 0 swtr - delegating, // locked = 500 swtr - sdk.NewCoins(), // vested = 0 swtr - initialVesting, // unvested = 1000 swtr - addCoins(subCoins(initialVesting, delegating), coins), // balances = 500 swtr + 300 swtr + s.va, + delegating, // delegated vesting = 500 swtr + sdk.NewCoins(), // delegated free = 0 swtr + delegating, // locked = 500 swtr + sdk.NewCoins(), // vested = 0 swtr + s.initialVesting, // unvested = 1000 swtr + addCoins(subCoins(s.initialVesting, delegating), coins), // balances = 500 swtr + 300 swtr coins, // spendable balances = 300 swtr ) }) It("failed in sending coins", func() { - mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, va) + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) // Try to send 300 coins from monthly vesting account coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(300, 18))) From fcb3547dff8f88c055ef29e14c8948d255b34dd3 Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Wed, 14 Aug 2024 15:01:48 +0000 Subject: [PATCH 3/6] Add integration test for different types of validators --- .../keeper/integration_validator_test.go | 928 ++++++++++++++++++ 1 file changed, 928 insertions(+) create mode 100644 x/vesting/keeper/integration_validator_test.go diff --git a/x/vesting/keeper/integration_validator_test.go b/x/vesting/keeper/integration_validator_test.go new file mode 100644 index 00000000..1c3f6033 --- /dev/null +++ b/x/vesting/keeper/integration_validator_test.go @@ -0,0 +1,928 @@ +package keeper_test + +import ( + "cosmossdk.io/math" + "fmt" + abci "github.com/cometbft/cometbft/abci/types" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + sdk "github.com/cosmos/cosmos-sdk/types" + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/ethereum/go-ethereum/common" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "swisstronik/tests" + "swisstronik/testutil" + "swisstronik/utils" + "swisstronik/x/vesting/types" + "time" +) + +func (suite *VestingTestSuite) bootstrapValidators(numVals int) ([]sdk.AccAddress, []sdk.ValAddress, []cryptotypes.PubKey, []stakingtypes.Validator) { + var ( + addrDels = make([]sdk.AccAddress, numVals) + addrVals = make([]sdk.ValAddress, numVals) + pks = simtestutil.CreateTestPubKeys(numVals) + vs = make([]stakingtypes.Validator, numVals) + ) + for i := 0; i < numVals; i++ { + addrDels[i] = tests.RandomAccAddress() + addrVals[i] = addrDels[i].Bytes() + + vs[i], _ = stakingtypes.NewValidator(addrVals[i], pks[i], stakingtypes.Description{}) + vs[i] = stakingkeeper.TestingUpdateValidator(&suite.app.StakingKeeper, suite.ctx, vs[i], true) + _ = suite.app.StakingKeeper.Hooks().AfterValidatorCreated(suite.ctx, vs[i].GetOperator()) + _ = suite.app.StakingKeeper.SetValidatorByConsAddr(suite.ctx, vs[i]) + } + return addrDels, addrVals, pks, vs +} + +func (suite *VestingTestSuite) initializeValidators(powers []int64) ([]sdk.AccAddress, []sdk.ValAddress, []cryptotypes.PubKey, []stakingtypes.Validator) { + numVals := len(powers) + var totalPower int64 + for _, power := range powers { + totalPower += power + } + + addrDels, addrVals, pks, vs := suite.bootstrapValidators(numVals) + + amount := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, totalPower) + totalSupply := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, amount)) + notBondedPool := suite.app.StakingKeeper.GetNotBondedPool(suite.ctx) + suite.app.AccountKeeper.SetModuleAccount(suite.ctx, notBondedPool) + _ = testutil.FundModuleAccount(suite.ctx, suite.app.BankKeeper, notBondedPool.GetName(), totalSupply) + + var issuedShares = make([]sdk.Dec, numVals) + for i, power := range powers { + vs[i], _ = stakingtypes.NewValidator(addrVals[i], pks[i], stakingtypes.Description{}) + tokens := suite.app.StakingKeeper.TokensFromConsensusPower(suite.ctx, power) + vs[i], issuedShares[i] = vs[i].AddTokensFromDel(tokens) + vs[i] = stakingkeeper.TestingUpdateValidator(&suite.app.StakingKeeper, suite.ctx, vs[i], true) + _ = suite.app.StakingKeeper.Hooks().AfterValidatorCreated(suite.ctx, vs[i].GetOperator()) + + if !tokens.Equal(math.ZeroInt()) { + // setup delegation if non-zero shares + bond := stakingtypes.NewDelegation(addrDels[i], addrVals[i], issuedShares[i]) + suite.app.StakingKeeper.SetDelegation(suite.ctx, bond) + suite.app.DistrKeeper.SetDelegatorStartingInfo(suite.ctx, vs[i].GetOperator(), addrDels[i], distrtypes.NewDelegatorStartingInfo(0, math.LegacyOneDec(), uint64(suite.ctx.BlockHeight()))) + // set historical reward record for new validator + decCoins := sdk.NewDecCoins() + historicalRewards := distrtypes.NewValidatorHistoricalRewards(decCoins, 2) + suite.app.DistrKeeper.SetValidatorHistoricalRewards(suite.ctx, addrVals[i], 0, historicalRewards) + } + } + + _ = suite.Commit() + + return addrDels, addrVals, pks, vs +} + +func (suite *VestingTestSuite) TestSetupTestWithBondedValidators() error { + err := suite.SetupTest() + if err != nil { + return err + } + + powers := []int64{0, 100, 400, 400, 200} + _, addrVals, _, _ := suite.initializeValidators(powers) + + for i, address := range addrVals { + val, ok := suite.app.StakingKeeper.GetValidator(suite.ctx, address) + Expect(ok).To(BeTrue()) + if i == 0 { + Expect(val.Status).To(Equal(stakingtypes.Unbonded)) + } else { + Expect(val.Status).To(Equal(stakingtypes.Bonded)) + } + fmt.Printf("%d: status: %s, tokens: %s\n", i, val.Status.String(), val.Tokens.String()) + } + + return nil +} + +func (suite *VestingTestSuite) TestSetupWithUnbondedValidators() error { + err := suite.SetupTest() + if err != nil { + return err + } + + _, addrVals, _, _ := suite.bootstrapValidators(5) + _ = suite.Commit() + + for i, address := range addrVals { + val, ok := suite.app.StakingKeeper.GetValidator(suite.ctx, address) + Expect(ok).To(BeTrue()) + Expect(val.Status).To(Equal(stakingtypes.Unbonded)) + fmt.Printf("%d: status: %s, tokens: %s\n", i, val.Status.String(), val.Tokens.String()) + } + + bootValidators := suite.app.StakingKeeper.GetValidators(suite.ctx, 20) + for i, val := range bootValidators { + fmt.Printf("%d: status: %s, tokens: %s\n", i, val.Status.String(), val.Tokens.String()) + } + return nil +} + +func (suite *VestingTestSuite) TestSetupWithJailedValidatorsBySelfUndelegation() error { + err := suite.SetupTest() + if err != nil { + return err + } + + powers := []int64{0, 100, 400, 400, 200} + addrDels, addrVals, _, vs := suite.initializeValidators(powers) + + for i, val := range vs { + fmt.Printf("%d: status: %s, tokens: %s\n", i, val.Status.String(), val.Tokens.String()) + + if val.Status == stakingtypes.Bonded { + // Make active validator jailed + val.MinSelfDelegation = val.Tokens + + coin := sdk.NewCoin(utils.BaseDenom, val.Tokens) + undelMsg := stakingtypes.NewMsgUndelegate(addrDels[i], addrVals[i], coin) + msgServer := stakingkeeper.NewMsgServerImpl(&suite.app.StakingKeeper) + _, err = msgServer.Undelegate(suite.ctx, undelMsg) + Expect(err).To(BeNil()) + + _ = suite.Commit() + + val2, ok := suite.app.StakingKeeper.GetValidator(suite.ctx, addrVals[i]) + Expect(ok).To(BeTrue()) + Expect(val2.Jailed).To(BeTrue()) + + fmt.Printf("> %d: status: %s, tokens: %s\n", i, val2.Status.String(), val2.Tokens.String()) + } + } + + return nil +} + +func (suite *VestingTestSuite) TestSetupWithJailedValidators() error { + err := suite.SetupTest() + if err != nil { + return err + } + + powers := []int64{0, 100, 400, 400, 200} + _, _, pks, _ := suite.initializeValidators(powers) + + for i, pk := range pks { + consAddr := sdk.ConsAddress(pk.Address()) + val := suite.app.StakingKeeper.ValidatorByConsAddr(suite.ctx, consAddr) + fmt.Printf("%d: status: %s, tokens: %s\n", i, val.GetStatus().String(), val.GetTokens().String()) + + if val.GetStatus() == stakingtypes.Bonded { + params := suite.app.SlashingKeeper.GetParams(suite.ctx) + if i%2 == 0 { + // set slash fraction double sign 5% for odd + params.SlashFractionDoubleSign = sdk.MustNewDecFromStr("0.05") // 5% + _ = suite.app.SlashingKeeper.SetParams(suite.ctx, params) + } else { + // set slash fraction double sign 10% for even + params.SlashFractionDoubleSign = sdk.MustNewDecFromStr("0.1") // 1% + _ = suite.app.SlashingKeeper.SetParams(suite.ctx, params) + } + + // Pass blocks of signed blocks window + signedBlockWindow := suite.app.SlashingKeeper.SignedBlocksWindow(suite.ctx) + for j := int64(0); j <= signedBlockWindow; j++ { + _ = suite.Commit() + } + + signingInfo := slashingtypes.NewValidatorSigningInfo( + consAddr, + 0, + int64(0), + time.Unix(0, 0), + false, + int64(signedBlockWindow), // should be over signedBlockWindow - minSignedBlockWindow + ) + suite.app.SlashingKeeper.SetValidatorSigningInfo(suite.ctx, consAddr, signingInfo) + suite.app.SlashingKeeper.HandleValidatorSignature(suite.ctx, consAddr.Bytes(), powers[i], false) + + val = suite.app.StakingKeeper.ValidatorByConsAddr(suite.ctx, consAddr) + Expect(val).To(Not(BeNil())) + Expect(val.IsJailed()).To(BeTrue()) + + _ = suite.Commit() + + fmt.Printf("> %d: slash:%s status: %s, tokens: %s\n", i, params.SlashFractionDoubleSign.String(), val.GetStatus().String(), val.GetTokens().String()) + } + } + + return nil +} + +func (suite *VestingTestSuite) TestSetupWithTombstonedValidators() error { + err := suite.SetupTest() + if err != nil { + return err + } + + powers := []int64{0, 100, 400, 400, 200} + _, _, pks, _ := suite.initializeValidators(powers) + + for i, pk := range pks { + consAddr := sdk.ConsAddress(pk.Address()) + val := suite.app.StakingKeeper.ValidatorByConsAddr(suite.ctx, consAddr) + fmt.Printf("%d: status: %s, tokens: %s\n", i, val.GetStatus().String(), val.GetTokens().String()) + + if val.GetStatus() == stakingtypes.Bonded { + params := suite.app.SlashingKeeper.GetParams(suite.ctx) + if i%2 == 0 { + // set slash fraction double sign 5% for odd + params.SlashFractionDoubleSign = sdk.MustNewDecFromStr("0.05") // 5% + _ = suite.app.SlashingKeeper.SetParams(suite.ctx, params) + } else { + // set slash fraction double sign 10% for even + params.SlashFractionDoubleSign = sdk.MustNewDecFromStr("0.1") // 1% + _ = suite.app.SlashingKeeper.SetParams(suite.ctx, params) + } + + signingInfo := slashingtypes.NewValidatorSigningInfo( + consAddr, + suite.ctx.BlockHeight(), + int64(0), + time.Unix(0, 0), + false, + int64(0), + ) + suite.app.SlashingKeeper.SetValidatorSigningInfo(suite.ctx, consAddr, signingInfo) + + // BeginBlocker in evidence keeper with misbehavior of duplicated vote + tmEvidence := abci.Misbehavior{ + Type: abci.MisbehaviorType_DUPLICATE_VOTE, + Validator: abci.Validator{ + Address: consAddr.Bytes(), + Power: powers[i], + }, + Height: suite.ctx.BlockHeight(), + Time: time.Now(), + TotalVotingPower: powers[i], + } + evidence := evidencetypes.FromABCIEvidence(tmEvidence) + suite.app.EvidenceKeeper.HandleEquivocationEvidence(suite.ctx, evidence.(*evidencetypes.Equivocation)) + + _ = suite.Commit() + + isTombstoned := suite.app.SlashingKeeper.IsTombstoned(suite.ctx, consAddr) + Expect(isTombstoned).To(BeTrue()) + + fmt.Printf("> tombstoned: %t\n", isTombstoned) + + val = suite.app.StakingKeeper.ValidatorByConsAddr(suite.ctx, consAddr) + Expect(val).To(Not(BeNil())) + Expect(val.IsJailed()).To(BeTrue()) + + fmt.Printf("> %d: slash:%s status: %s, tokens: %s\n", i, params.SlashFractionDoubleSign.String(), val.GetStatus().String(), val.GetTokens().String()) + } + } + + return nil +} + +func (suite *VestingTestSuite) ExpectJailValidatorSuccess(addrVal sdk.ValAddress, slashFraction sdk.Dec) stakingtypes.ValidatorI { + params := suite.app.SlashingKeeper.GetParams(suite.ctx) + params.SlashFractionDowntime = slashFraction + _ = suite.app.SlashingKeeper.SetParams(suite.ctx, params) + + validator, ok := suite.app.StakingKeeper.GetValidator(suite.ctx, addrVal) + Expect(ok).To(BeTrue()) + Expect(validator.Status).To(Equal(stakingtypes.Bonded)) + + signedBlockWindow := suite.app.SlashingKeeper.SignedBlocksWindow(suite.ctx) + for j := int64(0); j <= signedBlockWindow; j++ { + _ = suite.Commit() + } + + pk, _ := validator.ConsPubKey() + consAddr := sdk.ConsAddress(pk.Address()) + + signingInfo := slashingtypes.NewValidatorSigningInfo( + consAddr, + 0, + int64(0), + time.Unix(0, 0), + false, + int64(signedBlockWindow), // should be over signedBlockWindow - minSignedBlockWindow + ) + suite.app.SlashingKeeper.SetValidatorSigningInfo(suite.ctx, consAddr, signingInfo) + power := validator.GetConsensusPower(suite.app.StakingKeeper.PowerReduction(suite.ctx)) + suite.app.SlashingKeeper.HandleValidatorSignature(suite.ctx, consAddr.Bytes(), power, false) + + _ = suite.Commit() // not necessary + + val := suite.app.StakingKeeper.ValidatorByConsAddr(suite.ctx, consAddr) + Expect(val).To(Not(BeNil())) + Expect(val.IsJailed()).To(BeTrue()) + + return val +} + +func (suite *VestingTestSuite) ExpectTombstoneValidtorSuccess(addrVal sdk.ValAddress, slashFraction sdk.Dec) stakingtypes.ValidatorI { + params := suite.app.SlashingKeeper.GetParams(suite.ctx) + params.SlashFractionDoubleSign = slashFraction + _ = suite.app.SlashingKeeper.SetParams(suite.ctx, params) + + validator, ok := suite.app.StakingKeeper.GetValidator(suite.ctx, addrVal) + Expect(ok).To(BeTrue()) + Expect(validator.Status).To(Equal(stakingtypes.Bonded)) + + pk, _ := validator.ConsPubKey() + consAddr := sdk.ConsAddress(pk.Address()) + + signingInfo := slashingtypes.NewValidatorSigningInfo( + consAddr, + suite.ctx.BlockHeight(), + int64(0), + time.Unix(0, 0), + false, + int64(0), + ) + suite.app.SlashingKeeper.SetValidatorSigningInfo(suite.ctx, consAddr, signingInfo) + + power := validator.GetConsensusPower(suite.app.StakingKeeper.PowerReduction(suite.ctx)) + // BeginBlocker in evidence keeper with misbehavior of duplicated vote + tmEvidence := abci.Misbehavior{ + Type: abci.MisbehaviorType_DUPLICATE_VOTE, + Validator: abci.Validator{ + Address: consAddr.Bytes(), + Power: power, + }, + Height: suite.ctx.BlockHeight(), + Time: time.Now(), + TotalVotingPower: power, + } + evidence := evidencetypes.FromABCIEvidence(tmEvidence) + suite.app.EvidenceKeeper.HandleEquivocationEvidence(suite.ctx, evidence.(*evidencetypes.Equivocation)) + + _ = suite.Commit() // not necessary + + isTombstoned := suite.app.SlashingKeeper.IsTombstoned(suite.ctx, consAddr) + Expect(isTombstoned).To(BeTrue()) + + val := suite.app.StakingKeeper.ValidatorByConsAddr(suite.ctx, consAddr) + Expect(val).To(Not(BeNil())) + Expect(val.IsJailed()).To(BeTrue()) + + return val +} + +var _ = Describe("Additional tests with multiple validators for Monthly Vesting Account", func() { + var ( + s *VestingTestSuite + + err error + + // Different types of validators + powers []int64 + //addrDels []sdk.AccAddress + addrVals []sdk.ValAddress + //pks []cryptotypes.PubKey + ) + + BeforeEach(func() { + s = new(VestingTestSuite) + err = s.SetupTest() + Expect(err).To(BeNil()) + + var from common.Address + from, s.vaPrivKey = tests.RandomEthAddressWithPrivateKey() + s.va = sdk.AccAddress(from.Bytes()) + s.funder = tests.RandomAccAddress() + + s.initialVesting = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1000, 18))) + s.gasCoins = sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewInt(1e6))) + + // Fund coins to funder to create monthly vesting account + s.ExpectFundCoins(s.funder, s.initialVesting) + + // Setup multiple validators + powers = []int64{0, 50, 100, 0, 200} + //addrDels, addrVals, pks, _ = s.initializeValidators(powers) + _, addrVals, _, _ = s.initializeValidators(powers) + }) + + Context("Create vesting account with cliff 0 day, 2 month, amount 1000", func() { + BeforeEach(func() { + // Create monthly vesting account + resp, err := s.msgServer.HandleCreateMonthlyVestingAccount(s.goCtx, &types.MsgCreateMonthlyVestingAccount{ + FromAddress: s.funder.String(), + ToAddress: s.va.String(), + CliffDays: 0, + Months: 2, + Amount: s.initialVesting, + }) + Expect(resp).To(Equal(&types.MsgCreateMonthlyVestingAccountResponse{})) + err = s.Commit() + Expect(err).To(BeNil()) + + // Query monthly vesting account + mva, err := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + Expect(err).To(BeNil()) + Expect(mva).ToNot(BeNil()) + }) + It("slashing-tombstoned validator", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 1000 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1000, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 1500 SWTR during vesting time + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1500, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unlock 50 % of vesting + duration := time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // “Slashing” during vesting period for tombstoned validator - 10% + valI := s.ExpectTombstoneValidtorSuccess(address, sdk.MustNewDecFromStr("0.1")) + Expect(valI).To(Not(BeNil())) + + // Expected result: staked amount = (delegated vesting + free vesting) - 10% + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expAmount := mva.DelegatedVesting.Add(mva.DelegatedFree...).AmountOf(utils.BaseDenom) + expAmount = expAmount.Sub(expAmount.MulRaw(10).QuoRaw(100)) + Expect(valI.GetTokens()).To(Equal(expAmount)) + + break + } + } + + }) + It("slashing-jailed validator", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Stake 1000 SWTR during vesting time + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := s.initialVesting + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unlock 50 % of vesting + duration := time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // Unlock 100% of vesting + duration = time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // “Slashing” for jailed validator - 5% + valI := s.ExpectJailValidatorSuccess(address, sdk.MustNewDecFromStr("0.05")) + Expect(valI).To(Not(BeNil())) + + // Expected result: delegation vesting 950 + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expAmount := mva.DelegatedVesting.Add(mva.DelegatedFree...).AmountOf(utils.BaseDenom) + expAmount = expAmount.Sub(expAmount.MulRaw(5).QuoRaw(100)) + Expect(valI.GetTokens()).To(Equal(expAmount)) + expAmount = math.NewIntWithDecimal(950, 18) + Expect(valI.GetTokens()).To(Equal(expAmount)) + + break + } + } + }) + It("slashing-tombstoned validator-2", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Stake 1000 SWTR during vesting time + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := s.initialVesting + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unlock 50 % of vesting + duration := time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // Unlock 100% of vesting + duration = time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // “Slashing” after vesting time for tombtoned validator - 10% + valI := s.ExpectTombstoneValidtorSuccess(address, sdk.MustNewDecFromStr("0.1")) + Expect(valI).To(Not(BeNil())) + + // Expected result: delegation vesting 900 + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expAmount := mva.DelegatedVesting.Add(mva.DelegatedFree...).AmountOf(utils.BaseDenom) + expAmount = expAmount.Sub(expAmount.MulRaw(10).QuoRaw(100)) + Expect(valI.GetTokens()).To(Equal(expAmount)) + + break + } + } + }) + It("slashing-jailed validator-2", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 1000 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1000, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 1500 SWTR during vesting time + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1500, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unlock 50 % of vesting + duration := time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // “Slashing” during vesting period for jailed validator - 5% + valI := s.ExpectJailValidatorSuccess(address, sdk.MustNewDecFromStr("0.05")) + Expect(valI).To(Not(BeNil())) + + // Unlock 100% of vesting + duration = time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // Expected result:staked amount = (delegated vesting + free vesting) - 5% + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expAmount := mva.DelegatedVesting.Add(mva.DelegatedFree...).AmountOf(utils.BaseDenom) + expAmount = expAmount.Sub(expAmount.MulRaw(5).QuoRaw(100)) + Expect(valI.GetTokens()).To(Equal(expAmount)) + expAmount = math.NewIntWithDecimal(1425, 18) + Expect(valI.GetTokens()).To(Equal(expAmount)) + + break + } + } + + }) + It("slashing-tombstoned validator-3", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 1000 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1000, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 1500 SWTR during vesting time + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1500, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unlock 50 % of vesting + duration := time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // Unlock 100% of vesting + duration = time.Duration(types.SecondsOfMonth) * time.Second + err = s.CommitAfter(duration) + Expect(err).To(BeNil()) + + // “Slashing” during vesting period for tombstoned validator - 10% + valI := s.ExpectTombstoneValidtorSuccess(address, sdk.MustNewDecFromStr("0.1")) + Expect(valI).To(Not(BeNil())) + + // Expected result: staked amount = (delegated vesting + free vesting) - 10% + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expAmount := mva.DelegatedVesting.Add(mva.DelegatedFree...).AmountOf(utils.BaseDenom) + expAmount = expAmount.Sub(expAmount.MulRaw(10).QuoRaw(100)) + + Expect(valI.GetTokens()).To(Equal(expAmount)) + break + } + } + }) + }) + Context("Create vesting account with cliff 50 days, 12 months, amount 1000 swtr", func() { + BeforeEach(func() { + // Create monthly vesting account + resp, err := s.msgServer.HandleCreateMonthlyVestingAccount(s.goCtx, &types.MsgCreateMonthlyVestingAccount{ + FromAddress: s.funder.String(), + ToAddress: s.va.String(), + CliffDays: 50, + Months: 12, + Amount: s.initialVesting, + }) + Expect(resp).To(Equal(&types.MsgCreateMonthlyVestingAccountResponse{})) + err = s.Commit() + Expect(err).To(BeNil()) + + // Query monthly vesting account + mva, err := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + Expect(err).To(BeNil()) + Expect(mva).ToNot(BeNil()) + }) + It("active validator", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Bonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 1200 SWTR + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1200, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unstake during cliff 1200 SWTR (Active validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 0, all balance 1500 + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(BeNil()) + expBalance := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1500, 18))) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance).To(Equal(expBalance)) + + break + } + } + }) + It("jailed validator", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 1200 SWTR + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1200, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Make validator jailed + valI := s.ExpectJailValidatorSuccess(address, sdk.MustNewDecFromStr("0.1")) + Expect(valI).To(Not(BeNil())) + + // Unstake during cliff 1200 SWTR (Jailed validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating.MulInt(sdk.NewInt(90)).QuoInt(sdk.NewInt(100)) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 120(=10%), all balance (1500-1200) + 1200 * 90% + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(Equal(delegating.QuoInt(sdk.NewInt(10)))) + expBalance := math.NewIntWithDecimal(1380, 18) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance.AmountOf(utils.BaseDenom)).To(Equal(expBalance)) + + break + } + } + }) + It("unbonded validator", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 1200 SWTR + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1200, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unstake during cliff time 1200 SWTR (Unbonded validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 0, all balance 1500 + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(BeNil()) + expBalance := math.NewIntWithDecimal(1500, 18) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance.AmountOf(utils.BaseDenom)).To(Equal(expBalance)) + + break + } + } + + }) + It("tombstoned validator", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 1200 SWTR + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1200, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Make validator tombstoned + valI := s.ExpectTombstoneValidtorSuccess(address, sdk.MustNewDecFromStr("0.1")) + Expect(valI).To(Not(BeNil())) + + // Unstake during cliff time 1200*90% SWTR (Tombstoned validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating.MulInt(sdk.NewInt(90)).QuoInt(sdk.NewInt(100)) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 120(=10%), all balance (1500-1200) + 1200 * 90% + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(Equal(delegating.QuoInt(sdk.NewInt(10)))) + expBalance := math.NewIntWithDecimal(1380, 18) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance.AmountOf(utils.BaseDenom)).To(Equal(expBalance)) + + break + } + } + }) + It("active validator-2", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Bonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 500 SWTR (vesting amount) + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unstake during cliff 500 SWTR (Active validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 0 , all balance 1500 + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(BeNil()) + expBalance := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(1500, 18))) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance).To(Equal(expBalance)) + + break + } + } + }) + It("jailed validator-2", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 500 SWTR (vesting amount) + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Make validator jailed + valI := s.ExpectJailValidatorSuccess(address, sdk.MustNewDecFromStr("0.1")) + Expect(valI).To(Not(BeNil())) + + // Unstake during cliff 500 SWTR (Jailed validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating.MulInt(sdk.NewInt(90)).QuoInt(sdk.NewInt(100)) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 50(=10%), all balance 1000+500*90% + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(Equal(delegating.QuoInt(sdk.NewInt(10)))) + expBalance := math.NewIntWithDecimal(1450, 18) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance.AmountOf(utils.BaseDenom)).To(Equal(expBalance)) + + break + } + } + }) + It("unbonded validator-2", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 500 SWTR + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Unstake during cliff time 500 SWTR (Unbonded validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 0 , all balance 1500 + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(BeNil()) + expBalance := math.NewIntWithDecimal(1500, 18) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance.AmountOf(utils.BaseDenom)).To(Equal(expBalance)) + + break + } + } + }) + It("tombstoned validator-2", func() { + for _, address := range addrVals { + val, ok := s.app.StakingKeeper.GetValidator(s.ctx, address) + Expect(ok).To(BeTrue()) + + if val.GetStatus() == stakingtypes.Unbonded { + // Receive 500 SWTR + coins := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectFundCoins(s.va, coins) + + // Stake 500 SWTR + s.ExpectFundCoins(s.va, s.gasCoins) + delegating := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + s.ExpectDelegateSuccess(s.vaPrivKey, delegating[0], val) + + // Make validator tombstoned + valI := s.ExpectTombstoneValidtorSuccess(address, sdk.MustNewDecFromStr("0.1")) + Expect(valI).To(Not(BeNil())) + + // Unstake during cliff time 500 SWTR (Tombstoned validator) + s.ExpectFundCoins(s.va, s.gasCoins) + undelegating := delegating.MulInt(sdk.NewInt(90)).QuoInt(sdk.NewInt(100)) + s.ExpectUndelegateSuccess(s.vaPrivKey, undelegating[0], val) + + // Expected result: spendable balance 500, delegated vesting 50(=10%), all balance 1000+500*90% + mva, _ := s.querier.GetMonthlyVestingAccount(s.ctx, s.va) + expSpendable := sdk.NewCoins(sdk.NewCoin(utils.BaseDenom, math.NewIntWithDecimal(500, 18))) + spendable := s.app.BankKeeper.SpendableCoins(s.ctx, mva.GetAddress()) + Expect(spendable).To(Equal(expSpendable)) + Expect(mva.DelegatedVesting).To(Equal(delegating.QuoInt(sdk.NewInt(10)))) + expBalance := math.NewIntWithDecimal(1450, 18) + balance := s.app.BankKeeper.GetAllBalances(s.ctx, mva.GetAddress()) + Expect(balance.AmountOf(utils.BaseDenom)).To(Equal(expBalance)) + + break + } + } + }) + }) +}) From 4f07e4020ef55efe68f62f4f5f6ddb8d93d7ce7b Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Wed, 14 Aug 2024 15:09:36 +0000 Subject: [PATCH 4/6] Import order --- x/vesting/keeper/integration_validator_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x/vesting/keeper/integration_validator_test.go b/x/vesting/keeper/integration_validator_test.go index 1c3f6033..e40c72e0 100644 --- a/x/vesting/keeper/integration_validator_test.go +++ b/x/vesting/keeper/integration_validator_test.go @@ -1,8 +1,10 @@ package keeper_test import ( - "cosmossdk.io/math" "fmt" + "time" + + "cosmossdk.io/math" abci "github.com/cometbft/cometbft/abci/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" @@ -15,11 +17,11 @@ import ( "github.com/ethereum/go-ethereum/common" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "swisstronik/tests" "swisstronik/testutil" "swisstronik/utils" "swisstronik/x/vesting/types" - "time" ) func (suite *VestingTestSuite) bootstrapValidators(numVals int) ([]sdk.AccAddress, []sdk.ValAddress, []cryptotypes.PubKey, []stakingtypes.Validator) { From d7dab5ac8580db9d91c233ed19c4f979b1697353 Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Wed, 14 Aug 2024 15:10:54 +0000 Subject: [PATCH 5/6] Fix typo --- x/vesting/keeper/integration_validator_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/vesting/keeper/integration_validator_test.go b/x/vesting/keeper/integration_validator_test.go index e40c72e0..e6837a6c 100644 --- a/x/vesting/keeper/integration_validator_test.go +++ b/x/vesting/keeper/integration_validator_test.go @@ -164,7 +164,7 @@ func (suite *VestingTestSuite) TestSetupWithJailedValidatorsBySelfUndelegation() return nil } -func (suite *VestingTestSuite) TestSetupWithJailedValidators() error { +func (suite *VestingTestSuite) TestSetupWithJailedValidatorsByDowntime() error { err := suite.SetupTest() if err != nil { return err @@ -213,7 +213,7 @@ func (suite *VestingTestSuite) TestSetupWithJailedValidators() error { _ = suite.Commit() - fmt.Printf("> %d: slash:%s status: %s, tokens: %s\n", i, params.SlashFractionDoubleSign.String(), val.GetStatus().String(), val.GetTokens().String()) + fmt.Printf("> %d: slash:%s status: %s, tokens: %s\n", i, params.SlashFractionDowntime.String(), val.GetStatus().String(), val.GetTokens().String()) } } From 2934dd85b258439da66ac9a17efacdcb3a087785 Mon Sep 17 00:00:00 2001 From: deep-quality-dev <84747155+deep-quality-dev@users.noreply.github.com> Date: Tue, 20 Aug 2024 08:09:21 -0400 Subject: [PATCH 6/6] Update integration_validator_test.go --- x/vesting/keeper/integration_validator_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/x/vesting/keeper/integration_validator_test.go b/x/vesting/keeper/integration_validator_test.go index e6837a6c..52f936c1 100644 --- a/x/vesting/keeper/integration_validator_test.go +++ b/x/vesting/keeper/integration_validator_test.go @@ -383,9 +383,7 @@ var _ = Describe("Additional tests with multiple validators for Monthly Vesting // Different types of validators powers []int64 - //addrDels []sdk.AccAddress addrVals []sdk.ValAddress - //pks []cryptotypes.PubKey ) BeforeEach(func() { @@ -406,7 +404,6 @@ var _ = Describe("Additional tests with multiple validators for Monthly Vesting // Setup multiple validators powers = []int64{0, 50, 100, 0, 200} - //addrDels, addrVals, pks, _ = s.initializeValidators(powers) _, addrVals, _, _ = s.initializeValidators(powers) })