test: move slash redelegation test to integration (#19581)

This commit is contained in:
Julien Robert 2024-02-28 11:09:38 +01:00 committed by GitHub
parent a248d05f70
commit e4d97090d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 44 additions and 32 deletions

View File

@ -384,7 +384,7 @@ benchmark:
### Linting ###
###############################################################################
golangci_version=v1.55.0
golangci_version=v1.56.2
#? setup-pre-commit: Set pre-commit git hook
setup-pre-commit:

View File

@ -176,7 +176,6 @@ func TestAppImportExport(t *testing.T) {
ctxA := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})
ctxB := newApp.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()})
_, err = newApp.ModuleManager.InitGenesis(ctxB, app.AppCodec(), genesisState)
if err != nil {
if strings.Contains(err.Error(), "validator set is empty after InitGenesis") {
logger.Info("Skipping simulation as all validators have been unbonded")

View File

@ -1,41 +1,52 @@
package keeper_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"cosmossdk.io/core/header"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/math"
authkeeper "cosmossdk.io/x/auth/keeper"
bankkeeper "cosmossdk.io/x/bank/keeper"
banktestutil "cosmossdk.io/x/bank/testutil"
distributionkeeper "cosmossdk.io/x/distribution/keeper"
slashingkeeper "cosmossdk.io/x/slashing/keeper"
"cosmossdk.io/x/slashing/testutil"
stakingkeeper "cosmossdk.io/x/staking/keeper"
stakingtypes "cosmossdk.io/x/staking/types"
distributionkeeper "cosmossdk.io/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/tests/integration/slashing"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestSlashRedelegation(t *testing.T) {
// setting up
var stakingKeeper *stakingkeeper.Keeper
var bankKeeper bankkeeper.Keeper
var slashKeeper slashingkeeper.Keeper
var distrKeeper distributionkeeper.Keeper
var (
authKeeper authkeeper.AccountKeeper
stakingKeeper *stakingkeeper.Keeper
bankKeeper bankkeeper.Keeper
slashKeeper slashingkeeper.Keeper
distrKeeper distributionkeeper.Keeper
)
app, err := simtestutil.Setup(depinject.Configs(
depinject.Supply(log.NewNopLogger()),
testutil.AppConfig,
), &stakingKeeper, &bankKeeper, &slashKeeper, &distrKeeper)
app, err := simtestutil.Setup(
depinject.Configs(
depinject.Supply(log.NewNopLogger()),
slashing.AppConfig,
),
&stakingKeeper,
&bankKeeper,
&slashKeeper,
&distrKeeper,
&authKeeper,
)
require.NoError(t, err)
// get sdk context, staking msg server and bond denom
@ -56,8 +67,8 @@ func TestSlashRedelegation(t *testing.T) {
// fund acc 1 and acc 2
testCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, stakingKeeper.TokensFromConsensusPower(ctx, 10)))
banktestutil.FundAccount(ctx, bankKeeper, testAcc1, testCoins)
banktestutil.FundAccount(ctx, bankKeeper, testAcc2, testCoins)
fundAccount(t, ctx, bankKeeper, authKeeper, testAcc1, testCoins)
fundAccount(t, ctx, bankKeeper, authKeeper, testAcc2, testCoins)
balance1Before := bankKeeper.GetBalance(ctx, testAcc1, bondDenom)
balance2Before := bankKeeper.GetBalance(ctx, testAcc2, bondDenom)
@ -68,7 +79,7 @@ func TestSlashRedelegation(t *testing.T) {
// creating evil val
evilValAddr := sdk.ValAddress(evilValPubKey.Address())
banktestutil.FundAccount(ctx, bankKeeper, sdk.AccAddress(evilValAddr), testCoins)
fundAccount(t, ctx, bankKeeper, authKeeper, sdk.AccAddress(evilValAddr), testCoins)
createValMsg1, _ := stakingtypes.NewMsgCreateValidator(
evilValAddr.String(), evilValPubKey, testCoins[0], stakingtypes.Description{Details: "test"}, stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)), math.OneInt())
_, err = stakingMsgServer.CreateValidator(ctx, createValMsg1)
@ -76,7 +87,7 @@ func TestSlashRedelegation(t *testing.T) {
// creating good val
goodValAddr := sdk.ValAddress(goodValPubKey.Address())
banktestutil.FundAccount(ctx, bankKeeper, sdk.AccAddress(goodValAddr), testCoins)
fundAccount(t, ctx, bankKeeper, authKeeper, sdk.AccAddress(goodValAddr), testCoins)
createValMsg2, _ := stakingtypes.NewMsgCreateValidator(
goodValAddr.String(), goodValPubKey, testCoins[0], stakingtypes.Description{Details: "test"}, stakingtypes.NewCommissionRates(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)), math.OneInt())
_, err = stakingMsgServer.CreateValidator(ctx, createValMsg2)
@ -99,7 +110,7 @@ func TestSlashRedelegation(t *testing.T) {
require.NoError(t, err)
// next block, commit height 2, move to height 3
// with the new delegations, evil val increases in voting power and commit byzantine behaviour at height 3 consensus
// with the new delegations, evil val increases in voting power and commit byzantine behavior at height 3 consensus
// at the same time, acc 1 and acc 2 withdraw delegation from evil val
ctx, err = simtestutil.NextBlock(app, ctx, time.Duration(1))
require.NoError(t, err)
@ -126,9 +137,9 @@ func TestSlashRedelegation(t *testing.T) {
require.NoError(t, err)
// next block, commit height 3, move to height 4
// Slash evil val for byzantine behaviour at height 3 consensus,
// Slash evil val for byzantine behavior at height 3 consensus,
// at which acc 1 and acc 2 still contributed to evil val voting power
// even tho they undelegate at block 3, the valset update is applied after commited block 3 when height 3 consensus already passes
// even tho they undelegate at block 3, the valset update is applied after committed block 3 when height 3 consensus already passes
ctx, err = simtestutil.NextBlock(app, ctx, time.Duration(1))
require.NoError(t, err)
@ -163,3 +174,14 @@ func TestSlashRedelegation(t *testing.T) {
require.Equal(t, balance1AfterSlashing.Amount.Mul(math.NewIntFromUint64(10)).String(), balance1Before.Amount.String())
require.Equal(t, balance2AfterSlashing.Amount.Mul(math.NewIntFromUint64(10)).String(), balance2Before.Amount.String())
}
func fundAccount(t *testing.T, ctx context.Context, bankKeeper bankkeeper.Keeper, authKeeper authkeeper.AccountKeeper, addr sdk.AccAddress, amount sdk.Coins) {
t.Helper()
if authKeeper.GetAccount(ctx, addr) == nil {
addrAcc := authKeeper.NewAccountWithAddress(ctx, addr)
authKeeper.SetAccount(ctx, addrAcc)
}
require.NoError(t, banktestutil.FundAccount(ctx, bankKeeper, addr, amount))
}

View File

@ -121,7 +121,6 @@ func (k Keeper) InitGenesis(ctx context.Context, data types.GenesisState) {
),
evt.ValidatorSlashEvent,
)
if err != nil {
panic(err)
}
@ -256,7 +255,6 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState {
return false, nil
},
)
if err != nil {
panic(err)
}

View File

@ -330,7 +330,6 @@ func (k Querier) DelegatorValidators(ctx context.Context, req *types.QueryDelega
return false
},
)
if err != nil {
return nil, err
}

View File

@ -153,7 +153,6 @@ func ReferenceCountInvariant(k Keeper) sdk.Invariant {
return false, nil
},
)
if err != nil {
panic(err)
}
@ -168,7 +167,6 @@ func ReferenceCountInvariant(k Keeper) sdk.Invariant {
return false, nil
},
)
if err != nil {
panic(err)
}

View File

@ -959,7 +959,6 @@ func SimulateMsgWithdrawProposal(
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
if strings.Contains(err.Error(), "group was modified") || strings.Contains(err.Error(), "group policy was modified") {
return simtypes.NoOpMsg(group.ModuleName, sdk.MsgTypeURL(msg), "no-op:group/group-policy was modified"), nil, nil
@ -1067,7 +1066,6 @@ func SimulateMsgVote(
}
_, _, err = app.SimDeliver(txGen.TxEncoder(), tx)
if err != nil {
if strings.Contains(err.Error(), "group was modified") || strings.Contains(err.Error(), "group policy was modified") {
return simtypes.NoOpMsg(group.ModuleName, sdk.MsgTypeURL(msg), "no-op:group/group-policy was modified"), nil, nil

View File

@ -431,7 +431,6 @@ func (k Keeper) GetRedelegations(ctx context.Context, delegator sdk.AccAddress,
i++
return false, nil
})
if err != nil {
return nil, err
}

View File

@ -118,7 +118,6 @@ func (k Querier) ValidatorDelegations(ctx context.Context, req *types.QueryValid
return delegation, nil
}, query.WithCollectionPaginationPairPrefix[sdk.ValAddress, sdk.AccAddress](valAddr),
)
if err != nil {
delegations, pageResponse, err := k.getValidatorDelegationsLegacy(ctx, req)
if err != nil {