Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: A few tests for module #92

Closed
wants to merge 22 commits into from
Closed
6 changes: 3 additions & 3 deletions testing/simapp/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
panic(err)
}
// compute not bonded balance
notBondedTokens := sdk.ZeroInt()
notBondedAmount := sdk.ZeroInt()
for _, val := range stakingState.Validators {
if val.Status != stakingtypes.Unbonded {
continue
}
notBondedTokens = notBondedTokens.Add(val.GetTokens())
notBondedAmount = notBondedAmount.Add(val.GetTokens())
}
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedAmount)
// edit bank state to make it have the not bonded pool tokens
bankStateBz, ok := rawState[banktypes.ModuleName]
// TODO(fdymylja/jonathan): should we panic in this case
Expand Down
2 changes: 1 addition & 1 deletion testing/simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Setup(isCheckTx bool) *SimApp {

// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit in the default token of the simapp from first genesis
// of one consensus engine unit in the default coin of the simapp from first genesis
// account. A Nop logger is set in SimApp.
func SetupWithGenesisValSet(valSet *tmtypes.ValidatorSet) *SimApp {
app, genesisState := setup(true, 5)
Expand Down
5 changes: 4 additions & 1 deletion x/multi-staking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func (k Keeper) BurnUnbondedCoinAndUnlockedMultiStakingCoin(
}
// burn remaining coin in unlock
remaningCoin := unlockEntry.UnlockingCoin.ToCoin().Sub(unlockedCoin)
k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(remaningCoin))
err = k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(remaningCoin))
if err != nil {
return sdk.Coin{}, err
}

err = k.UnescrowCoinTo(ctx, multiStakerAddr, unlockedCoin)
if err != nil {
Expand Down
63 changes: 63 additions & 0 deletions x/multi-staking/keeper/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package keeper_test

import (
"github.com/realio-tech/multi-staking-module/testutil"
"github.com/realio-tech/multi-staking-module/x/multi-staking/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"
)

func (suite *KeeperTestSuite) TestEndBlocker() {
suite.SetupTest()

mulStaker := testutil.GenAddress()
valAddr := testutil.GenValAddress()
const multiStakingDenom = "ario"

suite.msKeeper.SetValidatorMultiStakingCoin(suite.ctx, valAddr, multiStakingDenom)

mulBalance := sdk.NewCoins(sdk.NewCoin(stakingtypes.DefaultParams().BondDenom, sdk.NewInt(10000)), sdk.NewCoin(multiStakingDenom, sdk.NewInt(10000)))
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, mulBalance)
suite.NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, mulStaker, mulBalance)
suite.NoError(err)

unlockEntry := types.UnlockEntry{
CreationHeight: suite.ctx.BlockHeight(),
UnlockingCoin: types.MultiStakingCoin{
Denom: multiStakingDenom,
Amount: sdk.NewInt(10000),
BondWeight: sdk.NewDec(1),
},
}
newUbd := types.MultiStakingUnlock{
UnlockID: types.UnlockID{
MultiStakerAddr: mulStaker.String(),
ValAddr: valAddr.String(),
},
Entries: []types.UnlockEntry{unlockEntry},
}

suite.msKeeper.SetMultiStakingUnlock(suite.ctx, newUbd)
matureUnbondingDelegations := suite.msKeeper.GetMatureUnbondingDelegations(suite.ctx)
completionTime := suite.ctx.BlockHeader().Time

unbondingDelegationEntry := stakingtypes.UnbondingDelegationEntry{
CreationHeight: suite.ctx.BlockHeight(),
CompletionTime: completionTime,
InitialBalance: sdk.NewInt(1000),
Balance: sdk.NewInt(1000),
}

unbondingDelegation := stakingtypes.UnbondingDelegation{
DelegatorAddress: mulStaker.String(),
ValidatorAddress: valAddr.String(),
Entries: []stakingtypes.UnbondingDelegationEntry{unbondingDelegationEntry},
}

matureUnbondingDelegations = append(matureUnbondingDelegations, unbondingDelegation)

suite.msKeeper.EndBlocker(suite.ctx, matureUnbondingDelegations)
}
64 changes: 64 additions & 0 deletions x/multi-staking/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package types_test

import (
"testing"

"github.com/realio-tech/multi-staking-module/testutil"
"github.com/realio-tech/multi-staking-module/x/multi-staking/types"
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestGenesisState_Validate(t *testing.T) {
multiStakerAddress := testutil.GenAddress()
DongLieu marked this conversation as resolved.
Show resolved Hide resolved
valAddr := testutil.GenValAddress()
const multiStakingDenom = "ario"

for _, tc := range []struct {
desc string
genState *types.GenesisState
valid bool
}{
{
desc: "default is valid",
genState: types.DefaultGenesis(),
valid: true,
},
{
desc: "valid genesis state",
genState: &types.GenesisState{
MultiStakingLocks: []types.MultiStakingLock{
{
LockID: types.LockID{
MultiStakerAddr: multiStakerAddress.String(),
ValAddr: valAddr.String(),
},
LockedCoin: types.MultiStakingCoin{
Denom: multiStakingDenom,
Amount: sdk.NewInt(1000),
BondWeight: sdk.NewDec(1),
},
},
},
ValidatorMultiStakingCoins: []types.ValidatorMultiStakingCoin{
{
ValAddr: valAddr.String(),
CoinDenom: multiStakingDenom,
},
},
StakingGenesisState: types.DefaultGenesis().StakingGenesisState,
},
valid: true,
},
} {
t.Run(tc.desc, func(t *testing.T) {
err := tc.genState.Validate()
if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
19 changes: 19 additions & 0 deletions x/multi-staking/types/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types_test

import (
"testing"

"github.com/realio-tech/multi-staking-module/testutil"
"github.com/realio-tech/multi-staking-module/x/multi-staking/types"
"github.com/stretchr/testify/require"
)

func TestAccAddrAndValAddrFromStrings(t *testing.T) {
accountAddress := testutil.GenAddress()
valAddress := testutil.GenValAddress()

actualAccAddr, actualValAddr, err := types.AccAddrAndValAddrFromStrings(accountAddress.String(), valAddress.String())
require.NoError(t, err)
require.Equal(t, accountAddress, actualAccAddr)
require.Equal(t, valAddress, actualValAddr)
}
Loading