Skip to content

Commit

Permalink
test: add some keeper and type tests (#118)
Browse files Browse the repository at this point in the history
* add some initial tests

* Recover cancel unbonding logic

* Fix lint and remove unused func

* Redelegate after cancel unbonding

* Add some msg server tests

* Fix lint error

* Fix redelegate test error

* Refactor cancel unbonding msg function

* fix-lint

* Add more checks to staking module

* Fix linter error

* Add some type tests and fix bug truncate int

* Fix linter errors

* Add keeper tests

* Remove unused variables
  • Loading branch information
neitdung authored Jan 26, 2024
1 parent 0168215 commit 619dbf6
Show file tree
Hide file tree
Showing 8 changed files with 797 additions and 5 deletions.
2 changes: 1 addition & 1 deletion x/multi-staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (k Keeper) AdjustUnbondAmount(ctx sdk.Context, delAcc sdk.AccAddress, valAc
shares = delShares
}

return validator.TokensFromShares(shares).RoundInt(), nil
return validator.TokensFromShares(shares).TruncateInt(), nil
}

func (k Keeper) AdjustCancelUnbondingAmount(ctx sdk.Context, delAcc sdk.AccAddress, valAcc sdk.ValAddress, creationHeight int64, amount math.Int) (adjustedAmount math.Int, err error) {
Expand Down
223 changes: 223 additions & 0 deletions x/multi-staking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"testing"

"github.com/realio-tech/multi-staking-module/testing/simapp"
"github.com/realio-tech/multi-staking-module/testutil"
multistakingkeeper "github.com/realio-tech/multi-staking-module/x/multi-staking/keeper"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"cosmossdk.io/math"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand All @@ -34,6 +38,225 @@ func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (suite *KeeperTestSuite) TestAdjustUnbondAmount() {
delAddr := testutil.GenAddress()
valDelAddr := testutil.GenAddress()
valPubKey := testutil.GenPubKey()
valAddr := sdk.ValAddress(valPubKey.Address())

testCases := []struct {
name string
malleate func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error)
adjustAmount math.Int
expAmount math.Int
expErr bool
}{
{
name: "success and not change adjust amount",
malleate: func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error) {
multiStakingAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(1000))
delMsg := stakingtypes.NewMsgDelegate(delAddr, valAddr, multiStakingAmount)
_, err := msgServer.Delegate(ctx, delMsg)
return multiStakingAmount, err
},
adjustAmount: sdk.NewInt(800),
expAmount: sdk.NewInt(800),
expErr: false,
},
{
name: "success and reduce adjust amount",
malleate: func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error) {
multiStakingAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(1000))
delMsg := stakingtypes.NewMsgDelegate(delAddr, valAddr, multiStakingAmount)
_, err := msgServer.Delegate(ctx, delMsg)

return multiStakingAmount, err
},
adjustAmount: sdk.NewInt(2000),
expAmount: sdk.NewInt(1000),
expErr: false,
},
{
name: "not found delegation",
malleate: func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error) {
multiStakingAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(1000))
return multiStakingAmount, nil
},
expErr: true,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()
newParam := stakingtypes.DefaultParams()
newParam.MinCommissionRate = sdk.MustNewDecFromStr("0.02")
suite.app.StakingKeeper.SetParams(suite.ctx, newParam)
suite.msKeeper.SetBondWeight(suite.ctx, MultiStakingDenomA, sdk.OneDec())
bondAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(1000))
userBalance := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(10000))
err := suite.FundAccount(delAddr, sdk.NewCoins(userBalance))
suite.Require().NoError(err)
err = suite.FundAccount(valDelAddr, sdk.NewCoins(userBalance))
suite.Require().NoError(err)

createMsg := stakingtypes.MsgCreateValidator{
Description: stakingtypes.Description{
Moniker: "test",
Identity: "test",
Website: "test",
SecurityContact: "test",
Details: "test",
},
Commission: stakingtypes.CommissionRates{
Rate: sdk.MustNewDecFromStr("0.05"),
MaxRate: sdk.MustNewDecFromStr("0.1"),
MaxChangeRate: sdk.MustNewDecFromStr("0.05"),
},
MinSelfDelegation: sdk.NewInt(200),
DelegatorAddress: valDelAddr.String(),
ValidatorAddress: valAddr.String(),
Pubkey: codectypes.UnsafePackAny(valPubKey),
Value: bondAmount,
}
_, err = suite.msgServer.CreateValidator(suite.ctx, &createMsg)
suite.Require().NoError(err)
_, err = tc.malleate(suite.ctx, suite.msgServer, *suite.msKeeper)
suite.Require().NoError(err)

actualAmt, err := suite.msKeeper.AdjustUnbondAmount(suite.ctx, delAddr, valAddr, tc.adjustAmount)

if tc.expErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
suite.Require().Equal(actualAmt, tc.expAmount)
}
})
}
}

func (suite *KeeperTestSuite) TestAdjustCancelUnbondAmount() {
delAddr := testutil.GenAddress()
valPubKey := testutil.GenPubKey()
valAddr := sdk.ValAddress(valPubKey.Address())

testCases := []struct {
name string
malleate func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error)
adjustAmount math.Int
expAmount math.Int
expErr bool
}{
{
name: "success and not change adjust amount",
malleate: func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error) {
multiStakingAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(1000))
undelMsg := stakingtypes.NewMsgUndelegate(delAddr, valAddr, multiStakingAmount)
_, err := msgServer.Undelegate(ctx, undelMsg)
return multiStakingAmount, err
},
adjustAmount: sdk.NewInt(800),
expAmount: sdk.NewInt(800),
expErr: false,
},
{
name: "success with many unbonding delegations",
malleate: func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error) {
multiStakingAmount1 := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(400))
undelMsg1 := stakingtypes.NewMsgUndelegate(delAddr, valAddr, multiStakingAmount1)
_, err := msgServer.Undelegate(ctx, undelMsg1)
suite.Require().NoError(err)

multiStakingAmount2 := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(500))
undelMsg2 := stakingtypes.NewMsgUndelegate(delAddr, valAddr, multiStakingAmount2)
_, err = msgServer.Undelegate(ctx, undelMsg2)
suite.Require().NoError(err)

multiStakingAmount3 := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(600))
undelMsg3 := stakingtypes.NewMsgUndelegate(delAddr, valAddr, multiStakingAmount3)
_, err = msgServer.Undelegate(ctx, undelMsg3)
suite.Require().NoError(err)

return multiStakingAmount1, nil
},
adjustAmount: sdk.NewInt(1500),
expAmount: sdk.NewInt(1500),
expErr: false,
},
{
name: "success and reduce adjust amount",
malleate: func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error) {
multiStakingAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(1000))
undelMsg := stakingtypes.NewMsgUndelegate(delAddr, valAddr, multiStakingAmount)
_, err := msgServer.Undelegate(ctx, undelMsg)

return multiStakingAmount, err
},
adjustAmount: sdk.NewInt(2000),
expAmount: sdk.NewInt(1000),
expErr: false,
},
{
name: "not found delegation",
malleate: func(ctx sdk.Context, msgServer stakingtypes.MsgServer, msKeeper multistakingkeeper.Keeper) (sdk.Coin, error) {
multiStakingAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(1000))
return multiStakingAmount, nil
},
expErr: true,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()
newParam := stakingtypes.DefaultParams()
newParam.MinCommissionRate = sdk.MustNewDecFromStr("0.02")
suite.app.StakingKeeper.SetParams(suite.ctx, newParam)
suite.msKeeper.SetBondWeight(suite.ctx, MultiStakingDenomA, sdk.OneDec())
bondAmount := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(5000))
userBalance := sdk.NewCoin(MultiStakingDenomA, sdk.NewInt(10000))
err := suite.FundAccount(delAddr, sdk.NewCoins(userBalance))
suite.Require().NoError(err)

createMsg := stakingtypes.MsgCreateValidator{
Description: stakingtypes.Description{
Moniker: "test",
Identity: "test",
Website: "test",
SecurityContact: "test",
Details: "test",
},
Commission: stakingtypes.CommissionRates{
Rate: sdk.MustNewDecFromStr("0.05"),
MaxRate: sdk.MustNewDecFromStr("0.1"),
MaxChangeRate: sdk.MustNewDecFromStr("0.05"),
},
MinSelfDelegation: sdk.NewInt(200),
DelegatorAddress: delAddr.String(),
ValidatorAddress: valAddr.String(),
Pubkey: codectypes.UnsafePackAny(valPubKey),
Value: bondAmount,
}
_, err = suite.msgServer.CreateValidator(suite.ctx, &createMsg)
suite.Require().NoError(err)
_, err = tc.malleate(suite.ctx, suite.msgServer, *suite.msKeeper)
suite.Require().NoError(err)

actualAmt, err := suite.msKeeper.AdjustCancelUnbondingAmount(suite.ctx, delAddr, valAddr, suite.ctx.BlockHeight(), tc.adjustAmount)

if tc.expErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
suite.Require().Equal(actualAmt, tc.expAmount)
}
})
}
}

// Todo: add CheckBalance; AddAccountWithCoin; FundAccount
func (suite *KeeperTestSuite) FundAccount(addr sdk.AccAddress, amounts sdk.Coins) error {
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, amounts)
Expand Down
2 changes: 1 addition & 1 deletion x/multi-staking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (suite *KeeperTestSuite) TestCreateValidator() {
actualBond, found := suite.app.StakingKeeper.GetDelegation(suite.ctx, delAddr, valAddr)
suite.Require().True(found)
suite.Require().Equal(bondAmount.Amount, lockRecord.LockedCoin.Amount)
suite.Require().Equal(tc.expOut.Amount, actualBond.Shares.RoundInt())
suite.Require().Equal(tc.expOut.Amount, actualBond.Shares.TruncateInt())
}
})
}
Expand Down
Loading

0 comments on commit 619dbf6

Please sign in to comment.