refactor abci test to use simapp

This commit is contained in:
Jonathan Gimeno 2020-03-03 11:25:32 +01:00
parent 2b95d5317e
commit 24d25fa207
1 changed files with 23 additions and 18 deletions

View File

@ -4,32 +4,37 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
)
func TestBeginBlocker(t *testing.T) {
ctx, bk, sk, _, keeper := slashingkeeper.CreateTestInput(t, slashing.DefaultParams())
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
pks := simapp.CreateTestPubKeys(1)
simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(200))
power := int64(100)
amt := sdk.TokensFromConsensusPower(power)
addr, pk := slashingkeeper.Addrs[2], slashingkeeper.Pks[2]
addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
// bond the validator
res, err := staking.NewHandler(sk)(ctx, slashingkeeper.NewTestMsgCreateValidator(addr, pk, amt))
res, err := staking.NewHandler(app.StakingKeeper)(ctx, slashingkeeper.NewTestMsgCreateValidator(addr, pk, amt))
require.NoError(t, err)
require.NotNil(t, res)
staking.EndBlocker(ctx, sk)
staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(
t, bk.GetAllBalances(ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))),
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))),
)
require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens())
require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens())
val := abci.Validator{
Address: pk.Address(),
@ -46,9 +51,9 @@ func TestBeginBlocker(t *testing.T) {
},
}
slashing.BeginBlocker(ctx, req, keeper)
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
info, found := keeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
info, found := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
require.True(t, found)
require.Equal(t, ctx.BlockHeight(), info.StartHeight)
require.Equal(t, int64(1), info.IndexOffset)
@ -58,7 +63,7 @@ func TestBeginBlocker(t *testing.T) {
height := int64(0)
// for 1000 blocks, mark the validator as having signed
for ; height < keeper.SignedBlocksWindow(ctx); height++ {
for ; height < app.SlashingKeeper.SignedBlocksWindow(ctx); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
@ -69,11 +74,11 @@ func TestBeginBlocker(t *testing.T) {
},
}
slashing.BeginBlocker(ctx, req, keeper)
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
}
// for 500 blocks, mark the validator as having not signed
for ; height < ((keeper.SignedBlocksWindow(ctx) * 2) - keeper.MinSignedPerWindow(ctx) + 1); height++ {
for ; height < ((app.SlashingKeeper.SignedBlocksWindow(ctx) * 2) - app.SlashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
@ -84,14 +89,14 @@ func TestBeginBlocker(t *testing.T) {
},
}
slashing.BeginBlocker(ctx, req, keeper)
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
}
// end block
staking.EndBlocker(ctx, sk)
staking.EndBlocker(ctx, app.StakingKeeper)
// validator should be jailed
validator, found := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
validator, found := app.StakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
require.True(t, found)
require.Equal(t, sdk.Unbonding, validator.GetStatus())
}