refactor(x/staking): migrate to use env (#19414)

Co-authored-by: Facundo <facundomedica@gmail.com>
This commit is contained in:
Marko
2024-02-14 23:11:06 +00:00
committed by GitHub
co-authored by Facundo
parent f6d970d52f
commit 2dafb8780c
30 changed files with 207 additions and 202 deletions
@@ -142,7 +142,7 @@ func initFixture(tb testing.TB) *fixture {
log.NewNopLogger(),
)
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[types.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[types.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
@@ -66,7 +66,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
totalUnbonded := math.NewInt(0)
for i := int64(0); i < int64(maxEntries); i++ {
var err error
ctx = ctx.WithBlockHeight(i)
ctx = ctx.WithHeaderInfo(header.Info{Height: i})
var amount math.Int
completionTime, amount, err = f.stakingKeeper.Undelegate(ctx, addrDel, addrVal, math.LegacyNewDec(1))
assert.NilError(t, err)
@@ -106,7 +106,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
log.NewNopLogger(),
)
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
@@ -327,8 +327,7 @@ func TestRotateConsPubKey(t *testing.T) {
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
newCtx := ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.Add(params.UnbondingTime)})
newCtx = newCtx.WithBlockHeight(newCtx.BlockHeight() + 1)
newCtx := ctx.WithHeaderInfo(header.Info{Height: ctx.BlockHeight() + 1, Time: ctx.HeaderInfo().Time.Add(params.UnbondingTime)}).WithBlockHeight(ctx.BlockHeight() + 1)
// this should remove keys from waiting queue since unbonding time is reached
_, err = stakingKeeper.EndBlocker(newCtx)
assert.NilError(t, err)
@@ -254,7 +254,10 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) {
assert.DeepEqual(t, f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 5).String(), diffTokens.String())
}
// tests Slash at a previous height with an unbonding delegation
// TestSlashWithUnbondingDelegation tests the slashing of a validator with an unbonding delegation.
// It sets up an environment with a validator and an unbonding delegation, and then performs slashing
// operations on the validator. The test verifies that the slashing correctly affects the unbonding
// delegation and the validator's power.
func TestSlashWithUnbondingDelegation(t *testing.T) {
f, addrDels, addrVals := bootstrapSlashTest(t, 10)
@@ -271,7 +274,7 @@ func TestSlashWithUnbondingDelegation(t *testing.T) {
assert.NilError(t, f.stakingKeeper.SetUnbondingDelegation(f.sdkCtx, ubd))
// slash validator for the first time
f.sdkCtx = f.sdkCtx.WithBlockHeight(12)
f.sdkCtx = f.sdkCtx.WithHeaderInfo(header.Info{Height: 12})
bondedPool := f.stakingKeeper.GetBondedPool(f.sdkCtx)
oldBondedPoolBalances := f.bankKeeper.GetAllBalances(f.sdkCtx, bondedPool.GetAddress())
@@ -417,7 +420,7 @@ func TestSlashWithRedelegation(t *testing.T) {
oldNotBonded := f.bankKeeper.GetBalance(f.sdkCtx, notBondedPool.GetAddress(), bondDenom).Amount
// slash validator
f.sdkCtx = f.sdkCtx.WithBlockHeight(12)
f.sdkCtx = f.sdkCtx.WithBlockHeight(12).WithHeaderInfo(header.Info{Height: 12})
_, found := f.stakingKeeper.GetValidatorByConsAddr(f.sdkCtx, consAddr)
assert.Assert(t, found)
@@ -485,7 +488,7 @@ func TestSlashWithRedelegation(t *testing.T) {
assert.Equal(t, int64(4), validator.GetConsensusPower(f.stakingKeeper.PowerReduction(f.sdkCtx)))
// slash the validator again, by 100%
f.sdkCtx = f.sdkCtx.WithBlockHeight(12)
f.sdkCtx = f.sdkCtx.WithBlockHeight(12).WithHeaderInfo(header.Info{Height: 12})
_, found = f.stakingKeeper.GetValidatorByConsAddr(f.sdkCtx, consAddr)
assert.Assert(t, found)
@@ -518,7 +521,7 @@ func TestSlashWithRedelegation(t *testing.T) {
// slash the validator again, by 100%
// no stake remains to be slashed
f.sdkCtx = f.sdkCtx.WithBlockHeight(12)
f.sdkCtx = f.sdkCtx.WithBlockHeight(12).WithHeaderInfo(header.Info{Height: 12})
// validator still in unbonding period
validator, _ = f.stakingKeeper.GetValidatorByConsAddr(f.sdkCtx, consAddr)
assert.Equal(t, validator.GetStatus(), sdk.Unbonding)
@@ -585,7 +588,7 @@ func TestSlashBoth(t *testing.T) {
oldBonded := f.bankKeeper.GetBalance(f.sdkCtx, bondedPool.GetAddress(), bondDenom).Amount
oldNotBonded := f.bankKeeper.GetBalance(f.sdkCtx, notBondedPool.GetAddress(), bondDenom).Amount
// slash validator
f.sdkCtx = f.sdkCtx.WithBlockHeight(12)
f.sdkCtx = f.sdkCtx.WithBlockHeight(12).WithHeaderInfo(header.Info{Height: 12})
_, found := f.stakingKeeper.GetValidatorByConsAddr(f.sdkCtx, sdk.GetConsAddress(PKs[0]))
assert.Assert(t, found)
consAddr0 := sdk.ConsAddress(PKs[0].Address())