test(x/slashing): write integration tests (#15928)
This commit is contained in:
@@ -4,65 +4,128 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cosmossdk.io/log"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gotest.tools/v3/assert"
|
||||
|
||||
"cosmossdk.io/depinject"
|
||||
"cosmossdk.io/log"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/integration"
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/testutil"
|
||||
|
||||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing"
|
||||
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/testutil"
|
||||
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
|
||||
|
||||
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
// The default power validators are initialized to have within tests
|
||||
var InitTokens = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
|
||||
|
||||
type fixture struct {
|
||||
ctx sdk.Context
|
||||
app *integration.App
|
||||
|
||||
ctx sdk.Context
|
||||
|
||||
bankKeeper bankkeeper.Keeper
|
||||
slashingKeeper slashingkeeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
addrDels []sdk.AccAddress
|
||||
|
||||
addrDels []sdk.AccAddress
|
||||
valAddrs []sdk.ValAddress
|
||||
}
|
||||
|
||||
func initFixture(t assert.TestingT) *fixture {
|
||||
f := &fixture{}
|
||||
app, err := simtestutil.Setup(
|
||||
depinject.Configs(
|
||||
testutil.AppConfig,
|
||||
depinject.Supply(log.NewNopLogger()),
|
||||
),
|
||||
&f.bankKeeper,
|
||||
&f.slashingKeeper,
|
||||
&f.stakingKeeper,
|
||||
func initFixture(t testing.TB) *fixture {
|
||||
keys := storetypes.NewKVStoreKeys(
|
||||
authtypes.StoreKey, banktypes.StoreKey, slashingtypes.StoreKey, stakingtypes.StoreKey,
|
||||
)
|
||||
assert.NilError(t, err)
|
||||
cdc := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}).Codec
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
|
||||
logger := log.NewTestLogger(t)
|
||||
cms := integration.CreateMultiStore(keys, logger)
|
||||
|
||||
newCtx := sdk.NewContext(cms, cmtproto.Header{}, true, logger)
|
||||
|
||||
authority := authtypes.NewModuleAddress("gov")
|
||||
|
||||
maccPerms := map[string][]string{
|
||||
minttypes.ModuleName: {authtypes.Minter},
|
||||
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
|
||||
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
|
||||
}
|
||||
|
||||
accountKeeper := authkeeper.NewAccountKeeper(
|
||||
cdc,
|
||||
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
|
||||
authtypes.ProtoBaseAccount,
|
||||
maccPerms,
|
||||
sdk.Bech32MainPrefix,
|
||||
authority.String(),
|
||||
)
|
||||
|
||||
blockedAddresses := map[string]bool{
|
||||
accountKeeper.GetAuthority(): false,
|
||||
}
|
||||
bankKeeper := bankkeeper.NewBaseKeeper(
|
||||
cdc,
|
||||
runtime.NewKVStoreService(keys[banktypes.StoreKey]),
|
||||
accountKeeper,
|
||||
blockedAddresses,
|
||||
authority.String(),
|
||||
log.NewNopLogger(),
|
||||
)
|
||||
|
||||
stakingKeeper := stakingkeeper.NewKeeper(cdc, keys[stakingtypes.StoreKey], accountKeeper, bankKeeper, authority.String())
|
||||
|
||||
slashingKeeper := slashingkeeper.NewKeeper(cdc, &codec.LegacyAmino{}, keys[slashingtypes.StoreKey], stakingKeeper, authority.String())
|
||||
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil)
|
||||
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil)
|
||||
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, nil, cdc.InterfaceRegistry())
|
||||
|
||||
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, bankModule, stakingModule, slashingModule)
|
||||
|
||||
sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context())
|
||||
|
||||
// Register MsgServer and QueryServer
|
||||
slashingtypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), slashingkeeper.NewMsgServerImpl(slashingKeeper))
|
||||
slashingtypes.RegisterQueryServer(integrationApp.QueryHelper(), slashingkeeper.NewQuerier(slashingKeeper))
|
||||
|
||||
// set default staking params
|
||||
stakingKeeper.SetParams(sdkCtx, stakingtypes.DefaultParams())
|
||||
|
||||
// TestParams set the SignedBlocksWindow to 1000 and MaxMissedBlocksPerWindow to 500
|
||||
f.slashingKeeper.SetParams(ctx, testutil.TestParams())
|
||||
addrDels := simtestutil.AddTestAddrsIncremental(f.bankKeeper, f.stakingKeeper, ctx, 5, f.stakingKeeper.TokensFromConsensusPower(f.ctx, 200))
|
||||
slashingKeeper.SetParams(sdkCtx, testutil.TestParams())
|
||||
addrDels := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, sdkCtx, 6, stakingKeeper.TokensFromConsensusPower(sdkCtx, 200))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)
|
||||
|
||||
info1 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3), time.Unix(2, 0), false, int64(10))
|
||||
info2 := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4), time.Unix(2, 0), false, int64(10))
|
||||
|
||||
f.slashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), info1)
|
||||
f.slashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2)
|
||||
slashingKeeper.SetValidatorSigningInfo(sdkCtx, sdk.ConsAddress(addrDels[0]), info1)
|
||||
slashingKeeper.SetValidatorSigningInfo(sdkCtx, sdk.ConsAddress(addrDels[1]), info2)
|
||||
|
||||
f.addrDels = addrDels
|
||||
f.ctx = ctx
|
||||
|
||||
return f
|
||||
return &fixture{
|
||||
app: integrationApp,
|
||||
ctx: sdkCtx,
|
||||
bankKeeper: bankKeeper,
|
||||
slashingKeeper: slashingKeeper,
|
||||
stakingKeeper: stakingKeeper,
|
||||
addrDels: addrDels,
|
||||
valAddrs: valAddrs,
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnJailNotBonded(t *testing.T) {
|
||||
@@ -73,14 +136,12 @@ func TestUnJailNotBonded(t *testing.T) {
|
||||
p.MaxValidators = 5
|
||||
f.stakingKeeper.SetParams(f.ctx, p)
|
||||
|
||||
addrDels := simtestutil.AddTestAddrsIncremental(f.bankKeeper, f.stakingKeeper, f.ctx, 6, f.stakingKeeper.TokensFromConsensusPower(f.ctx, 200))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)
|
||||
pks := simtestutil.CreateTestPubKeys(6)
|
||||
tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)
|
||||
|
||||
// create max (5) validators all with the same power
|
||||
for i := uint32(0); i < p.MaxValidators; i++ {
|
||||
addr, val := valAddrs[i], pks[i]
|
||||
addr, val := f.valAddrs[i], pks[i]
|
||||
tstaking.CreateValidatorWithValPower(addr, val, 100, true)
|
||||
}
|
||||
|
||||
@@ -88,7 +149,7 @@ func TestUnJailNotBonded(t *testing.T) {
|
||||
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)
|
||||
|
||||
// create a 6th validator with less power than the cliff validator (won't be bonded)
|
||||
addr, val := valAddrs[5], pks[5]
|
||||
addr, val := f.valAddrs[5], pks[5]
|
||||
amt := f.stakingKeeper.TokensFromConsensusPower(f.ctx, 50)
|
||||
msg := tstaking.CreateValidatorMsg(addr, val, amt)
|
||||
msg.MinSelfDelegation = amt
|
||||
@@ -113,7 +174,15 @@ func TestUnJailNotBonded(t *testing.T) {
|
||||
tstaking.CheckValidator(addr, -1, true)
|
||||
|
||||
// verify we cannot unjail (yet)
|
||||
assert.ErrorContains(t, f.slashingKeeper.Unjail(f.ctx, addr), "cannot be unjailed")
|
||||
msgUnjail := slashingtypes.MsgUnjail{
|
||||
ValidatorAddr: addr.String(),
|
||||
}
|
||||
_, err = f.app.RunMsg(
|
||||
&msgUnjail,
|
||||
integration.WithAutomaticBeginEndBlock(),
|
||||
integration.WithAutomaticCommit(),
|
||||
)
|
||||
assert.ErrorContains(t, err, "cannot be unjailed")
|
||||
|
||||
f.stakingKeeper.EndBlocker(f.ctx)
|
||||
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)
|
||||
@@ -124,7 +193,12 @@ func TestUnJailNotBonded(t *testing.T) {
|
||||
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)
|
||||
|
||||
// verify we can immediately unjail
|
||||
assert.NilError(t, f.slashingKeeper.Unjail(f.ctx, addr))
|
||||
_, err = f.app.RunMsg(
|
||||
&msgUnjail,
|
||||
integration.WithAutomaticBeginEndBlock(),
|
||||
integration.WithAutomaticCommit(),
|
||||
)
|
||||
assert.NilError(t, err)
|
||||
|
||||
tstaking.CheckValidator(addr, -1, false)
|
||||
}
|
||||
@@ -136,20 +210,23 @@ func TestHandleNewValidator(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := initFixture(t)
|
||||
|
||||
addrDels := simtestutil.AddTestAddrsIncremental(f.bankKeeper, f.stakingKeeper, f.ctx, 1, f.stakingKeeper.TokensFromConsensusPower(f.ctx, 0))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)
|
||||
pks := simtestutil.CreateTestPubKeys(1)
|
||||
addr, val := valAddrs[0], pks[0]
|
||||
addr, val := f.valAddrs[0], pks[0]
|
||||
tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)
|
||||
f.ctx = f.ctx.WithBlockHeight(f.slashingKeeper.SignedBlocksWindow(f.ctx) + 1)
|
||||
|
||||
f.slashingKeeper.AddPubkey(f.ctx, pks[0])
|
||||
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(val.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
f.slashingKeeper.SetValidatorSigningInfo(f.ctx, sdk.ConsAddress(val.Address()), info)
|
||||
|
||||
// Validator created
|
||||
amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true)
|
||||
|
||||
f.stakingKeeper.EndBlocker(f.ctx)
|
||||
assert.DeepEqual(
|
||||
t, f.bankKeeper.GetAllBalances(f.ctx, sdk.AccAddress(addr)),
|
||||
sdk.NewCoins(sdk.NewCoin(f.stakingKeeper.GetParams(f.ctx).BondDenom, InitTokens.Sub(amt))),
|
||||
sdk.NewCoins(sdk.NewCoin(f.stakingKeeper.GetParams(f.ctx).BondDenom, testutil.InitTokens.Sub(amt))),
|
||||
)
|
||||
assert.DeepEqual(t, amt, f.stakingKeeper.Validator(f.ctx, addr).GetBondedTokens())
|
||||
|
||||
@@ -170,8 +247,6 @@ func TestHandleNewValidator(t *testing.T) {
|
||||
assert.Equal(t, stakingtypes.Bonded, validator.GetStatus())
|
||||
bondPool := f.stakingKeeper.GetBondedPool(f.ctx)
|
||||
expTokens := f.stakingKeeper.TokensFromConsensusPower(f.ctx, 100)
|
||||
// adding genesis validator tokens
|
||||
expTokens = expTokens.Add(f.stakingKeeper.TokensFromConsensusPower(f.ctx, 1))
|
||||
assert.Assert(t, expTokens.Equal(f.bankKeeper.GetBalance(f.ctx, bondPool.GetAddress(), f.stakingKeeper.BondDenom(f.ctx)).Amount))
|
||||
}
|
||||
|
||||
@@ -181,13 +256,16 @@ func TestHandleAlreadyJailed(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := initFixture(t)
|
||||
|
||||
addrDels := simtestutil.AddTestAddrsIncremental(f.bankKeeper, f.stakingKeeper, f.ctx, 1, f.stakingKeeper.TokensFromConsensusPower(f.ctx, 200))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrDels)
|
||||
pks := simtestutil.CreateTestPubKeys(1)
|
||||
addr, val := valAddrs[0], pks[0]
|
||||
addr, val := f.valAddrs[0], pks[0]
|
||||
power := int64(100)
|
||||
tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)
|
||||
|
||||
f.slashingKeeper.AddPubkey(f.ctx, pks[0])
|
||||
|
||||
info := slashingtypes.NewValidatorSigningInfo(sdk.ConsAddress(val.Address()), f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
f.slashingKeeper.SetValidatorSigningInfo(f.ctx, sdk.ConsAddress(val.Address()), info)
|
||||
|
||||
amt := tstaking.CreateValidatorWithValPower(addr, val, power, true)
|
||||
|
||||
f.stakingKeeper.EndBlocker(f.ctx)
|
||||
@@ -245,10 +323,15 @@ func TestValidatorDippingInAndOut(t *testing.T) {
|
||||
tstaking := stakingtestutil.NewHelper(t, f.ctx, f.stakingKeeper)
|
||||
valAddr := sdk.ValAddress(addr)
|
||||
|
||||
f.slashingKeeper.AddPubkey(f.ctx, pks[0])
|
||||
|
||||
info := slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
f.slashingKeeper.SetValidatorSigningInfo(f.ctx, consAddr, info)
|
||||
|
||||
tstaking.CreateValidatorWithValPower(valAddr, val, power, true)
|
||||
validatorUpdates, err := f.stakingKeeper.EndBlocker(f.ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, len(validatorUpdates))
|
||||
assert.Equal(t, 1, len(validatorUpdates))
|
||||
tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false)
|
||||
|
||||
// 100 first blocks OK
|
||||
@@ -289,16 +372,18 @@ func TestValidatorDippingInAndOut(t *testing.T) {
|
||||
// validator misses an additional 500 more blocks within the SignedBlockWindow (here 1000 blocks).
|
||||
latest := f.slashingKeeper.SignedBlocksWindow(f.ctx) + height
|
||||
// misses 500 blocks + within the signing windows i.e. 700-1700
|
||||
// validators misses all 1000 block of a SignedBlockWindows
|
||||
// validators misses all 1000 blocks of a SignedBlockWindows
|
||||
for ; height < latest+1; height++ {
|
||||
f.ctx = f.ctx.WithBlockHeight(height)
|
||||
f.slashingKeeper.HandleValidatorSignature(f.ctx, val.Address(), newPower, false)
|
||||
f.slashingKeeper.HandleValidatorSignature(f.ctx.WithBlockHeight(height), val.Address(), newPower, false)
|
||||
}
|
||||
|
||||
// should now be jailed & kicked
|
||||
f.stakingKeeper.EndBlocker(f.ctx)
|
||||
tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, true)
|
||||
|
||||
info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
f.slashingKeeper.SetValidatorSigningInfo(f.ctx, consAddr, info)
|
||||
|
||||
// check all the signing information
|
||||
signInfo, found := f.slashingKeeper.GetValidatorSigningInfo(f.ctx, consAddr)
|
||||
assert.Assert(t, found)
|
||||
@@ -310,6 +395,9 @@ func TestValidatorDippingInAndOut(t *testing.T) {
|
||||
height = int64(5000)
|
||||
f.ctx = f.ctx.WithBlockHeight(height)
|
||||
|
||||
info = slashingtypes.NewValidatorSigningInfo(consAddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
|
||||
f.slashingKeeper.SetValidatorSigningInfo(f.ctx, consAddr, info)
|
||||
|
||||
// validator rejoins and starts signing again
|
||||
f.stakingKeeper.Unjail(f.ctx, consAddr)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user