From 050a9ee123038a30e94bb02f056bb637b4331b0e Mon Sep 17 00:00:00 2001 From: stackman27 Date: Sat, 26 Nov 2022 00:09:46 -0800 Subject: [PATCH] adams feedback --- x/valset-pref/msg_server_test.go | 63 +++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/x/valset-pref/msg_server_test.go b/x/valset-pref/msg_server_test.go index 32592e3ff80..942d35c43d7 100644 --- a/x/valset-pref/msg_server_test.go +++ b/x/valset-pref/msg_server_test.go @@ -128,16 +128,16 @@ func (suite *KeeperTestSuite) TestSetValidatorSetPreference() { func (suite *KeeperTestSuite) TestDelegateToValidatorSet() { suite.SetupTest() - amountToFund := sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 100_000_000)} // 100 osmo - // prepare validators to delegate to preferences := suite.PrepareDelegateToValidatorSet() + amountToFund := sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 100_000_000)} // 100 osmo + tests := []struct { name string delegator sdk.AccAddress coin sdk.Coin // amount to delegate - expectedShares []sdk.Dec //expected shares after delegation + expectedShares []sdk.Dec // expected shares after delegation expectPass bool valSetExists bool }{ @@ -170,6 +170,7 @@ func (suite *KeeperTestSuite) TestDelegateToValidatorSet() { msgServer := valPref.NewMsgServerImpl(suite.App.ValidatorSetPreferenceKeeper) c := sdk.WrapSDKContext(suite.Ctx) + // if validatorSetExist no need to refund and setValSet again if !test.valSetExists { suite.FundAcc(test.delegator, amountToFund) @@ -184,14 +185,19 @@ func (suite *KeeperTestSuite) TestDelegateToValidatorSet() { // check if the user balance decreased balance := suite.App.BankKeeper.GetBalance(suite.Ctx, test.delegator, sdk.DefaultBondDenom) - suite.Require().True(balance.IsLT(amountToFund[0])) + expectedBalance := amountToFund[0].Amount.Sub(test.coin.Amount) + if test.valSetExists { + expectedBalance = balance.Amount + } + + suite.Require().Equal(expectedBalance, balance.Amount) // check if the expectedShares matches after delegation for i, val := range preferences { valAddr, err := sdk.ValAddressFromBech32(val.ValOperAddress) suite.Require().NoError(err) - //guarantees that the delegator exists because we check it in DelegateToValidatorSet + // guarantees that the delegator exists because we check it in DelegateToValidatorSet del, _ := suite.App.StakingKeeper.GetDelegation(suite.Ctx, test.delegator, valAddr) suite.Require().Equal(del.Shares, test.expectedShares[i]) } @@ -205,29 +211,39 @@ func (suite *KeeperTestSuite) TestDelegateToValidatorSet() { func (suite *KeeperTestSuite) TestUnDelegateFromValidatorSet() { tests := []struct { - name string - delegator sdk.AccAddress - coinToStake sdk.Coin - coinToUnStake sdk.Coin - expectPass bool + name string + delegator sdk.AccAddress + coinToStake sdk.Coin + coinToUnStake sdk.Coin + expectedShares []sdk.Dec // expected shares after undelegation + expectPass bool }{ { - name: "Unstake x amount from a validator Set", - delegator: sdk.AccAddress([]byte("addr1---------------")), - coinToStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20_000_000)), - coinToUnStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10_000_000)), - expectPass: true, + name: "Unstake half from the ValSet", + delegator: sdk.AccAddress([]byte("addr1---------------")), + coinToStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20_000_000)), // delegate 20osmo + coinToUnStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10_000_000)), // undelegate 10osmo + expectedShares: []sdk.Dec{sdk.NewDec(2_000_000), sdk.NewDec(3_320_000), sdk.NewDec(1_200_000), sdk.NewDec(3_480_000)}, + expectPass: true, + }, + { + name: "Unstake x amount from ValSet", + delegator: sdk.AccAddress([]byte("addr2---------------")), + coinToStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20_000_000)), // delegate 20osmo + coinToUnStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(15_000_000)), // undelegate 15osmo + expectedShares: []sdk.Dec{sdk.NewDec(1_000_000), sdk.NewDec(1_660_000), sdk.NewDec(600_000), sdk.NewDec(1_740_000)}, // validatorDelegatedShares - (weight * coinToUnstake) + expectPass: true, }, { name: "Unstake everything", - delegator: sdk.AccAddress([]byte("addr2---------------")), + delegator: sdk.AccAddress([]byte("addr3---------------")), coinToStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20_000_000)), coinToUnStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20_000_000)), expectPass: true, }, { name: "Unstake more amount than the staked amount", - delegator: sdk.AccAddress([]byte("addr3---------------")), + delegator: sdk.AccAddress([]byte("addr4---------------")), coinToStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(20_000_000)), coinToUnStake: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(40_000_000)), expectPass: false, @@ -257,6 +273,19 @@ func (suite *KeeperTestSuite) TestUnDelegateFromValidatorSet() { _, err = msgServer.UndelegateFromValidatorSet(c, types.NewMsgUndelegateFromValidatorSet(test.delegator, test.coinToUnStake)) if test.expectPass { suite.Require().NoError(err) + + // check if the expectedShares matches after undelegation + for i, val := range preferences { + valAddr, err := sdk.ValAddressFromBech32(val.ValOperAddress) + suite.Require().NoError(err) + + // guarantees that the delegator exists because we check it in UnDelegateToValidatorSet + del, found := suite.App.StakingKeeper.GetDelegation(suite.Ctx, test.delegator, valAddr) + if found { + suite.Require().Equal(del.GetShares(), test.expectedShares[i]) + } + } + } else { suite.Require().Error(err) }