Skip to content

Commit

Permalink
adams feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stackman27 committed Dec 1, 2022
1 parent b7c31d0 commit 050a9ee
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions x/valset-pref/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand Down Expand Up @@ -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)

Expand All @@ -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])
}
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 050a9ee

Please sign in to comment.