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
@@ -9,6 +9,7 @@ import (
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
coreheader "cosmossdk.io/core/header"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/auth"
@@ -92,7 +93,7 @@ func initFixture(tb testing.TB) *fixture {
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))
slashingKeeper := slashingkeeper.NewKeeper(cdc, &codec.LegacyAmino{}, runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), stakingKeeper, authority.String())
@@ -308,7 +309,7 @@ func TestHandleAlreadyJailed(t *testing.T) {
consaddr, err := f.stakingKeeper.ConsensusAddressCodec().BytesToString(val.Address())
assert.NilError(t, err)
info := slashingtypes.NewValidatorSigningInfo(consaddr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
info := slashingtypes.NewValidatorSigningInfo(consaddr, f.ctx.HeaderInfo().Height, int64(0), time.Unix(0, 0), false, int64(0))
assert.NilError(t, f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, sdk.ConsAddress(val.Address()), info))
acc := f.accountKeeper.NewAccountWithAddress(f.ctx, sdk.AccAddress(addr))
@@ -325,7 +326,7 @@ func TestHandleAlreadyJailed(t *testing.T) {
// 1000 first blocks OK
height := int64(0)
for ; height < signedBlocksWindow; height++ {
f.ctx = f.ctx.WithBlockHeight(height)
f.ctx = f.ctx.WithHeaderInfo(coreheader.Info{Height: height}).WithBlockHeight(height)
err = f.slashingKeeper.HandleValidatorSignature(f.ctx, val.Address(), power, comet.BlockIDFlagCommit)
assert.NilError(t, err)
}
@@ -335,7 +336,7 @@ func TestHandleAlreadyJailed(t *testing.T) {
// 501 blocks missed
for ; height < signedBlocksWindow+(signedBlocksWindow-minSignedPerWindow)+1; height++ {
f.ctx = f.ctx.WithBlockHeight(height)
f.ctx = f.ctx.WithHeaderInfo(coreheader.Info{Height: height}).WithBlockHeight(height)
err = f.slashingKeeper.HandleValidatorSignature(f.ctx, val.Address(), power, comet.BlockIDFlagAbsent)
assert.NilError(t, err)
}
@@ -353,7 +354,7 @@ func TestHandleAlreadyJailed(t *testing.T) {
assert.DeepEqual(t, resultingTokens, validator.GetTokens())
// another block missed
f.ctx = f.ctx.WithBlockHeight(height)
f.ctx = f.ctx.WithHeaderInfo(coreheader.Info{Height: height}).WithBlockHeight(height)
assert.NilError(t, f.slashingKeeper.HandleValidatorSignature(f.ctx, val.Address(), power, comet.BlockIDFlagAbsent))
// validator should not have been slashed twice
@@ -404,7 +405,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
// 100 first blocks OK
height := int64(0)
for ; height < int64(100); height++ {
f.ctx = f.ctx.WithBlockHeight(height)
f.ctx = f.ctx.WithBlockHeight(height).WithHeaderInfo(coreheader.Info{Height: height})
assert.NilError(t, f.slashingKeeper.HandleValidatorSignature(f.ctx, val.Address(), power, comet.BlockIDFlagCommit))
}
@@ -418,7 +419,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
// 600 more blocks happened
height += 600
f.ctx = f.ctx.WithBlockHeight(height)
f.ctx = f.ctx.WithBlockHeight(height).WithHeaderInfo(coreheader.Info{Height: height})
// validator added back in
tstaking.DelegateWithPower(sdk.AccAddress(pks[2].Address()), valAddr, 50)
@@ -444,7 +445,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
// misses 500 blocks + within the signing windows i.e. 700-1700
// validators misses all 1000 blocks of a SignedBlockWindows
for ; height < latest+1; height++ {
err = f.slashingKeeper.HandleValidatorSignature(f.ctx.WithBlockHeight(height), val.Address(), newPower, comet.BlockIDFlagAbsent)
err = f.slashingKeeper.HandleValidatorSignature(f.ctx.WithBlockHeight(height).WithHeaderInfo(coreheader.Info{Height: height}), val.Address(), newPower, comet.BlockIDFlagAbsent)
assert.NilError(t, err)
}
@@ -466,7 +467,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
// some blocks pass
height = int64(5000)
f.ctx = f.ctx.WithBlockHeight(height)
f.ctx = f.ctx.WithBlockHeight(height).WithHeaderInfo(coreheader.Info{Height: height})
info = slashingtypes.NewValidatorSigningInfo(consaddrStr, f.ctx.BlockHeight(), int64(0), time.Unix(0, 0), false, int64(0))
err = f.slashingKeeper.ValidatorSigningInfo.Set(f.ctx, consAddr, info)
@@ -494,7 +495,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
// validator misses 501 blocks after SignedBlockWindow period (1000 blocks)
latest = signedBlocksWindow + height
for ; height < latest+minSignedPerWindow; height++ {
f.ctx = f.ctx.WithBlockHeight(height)
f.ctx = f.ctx.WithBlockHeight(height).WithHeaderInfo(coreheader.Info{Height: height})
err = f.slashingKeeper.HandleValidatorSignature(f.ctx, val.Address(), newPower, comet.BlockIDFlagAbsent)
assert.NilError(t, err)
}