refactor: fetch CometInfo from service (#20238)

Co-authored-by: Marko <marko@baricevic.me>
This commit is contained in:
Matt Kocubinski
2024-05-03 01:14:22 +00:00
committed by GitHub
co-authored by Marko
parent 599ae55c41
commit a6bc921c9a
24 changed files with 154 additions and 52 deletions
@@ -118,8 +118,9 @@ func initFixture(t *testing.T) *fixture {
msgRouter := baseapp.NewMsgServiceRouter()
grpcRouter := baseapp.NewGRPCQueryRouter()
cometService := runtime.NewContextAwareCometInfoService()
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(grpcRouter, msgRouter)), 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(), runtime.EnvWithRouterService(grpcRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), cometService)
require.NoError(t, stakingKeeper.Params.Set(newCtx, stakingtypes.DefaultParams()))
poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, stakingKeeper, authority.String())
@@ -69,8 +69,9 @@ var (
}
// The default power validators are initialized to have within tests
initAmt = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
initAmt = sdk.TokensFromConsensusPower(200, sdk.DefaultPowerReduction)
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
cometInfoService = runtime.NewContextAwareCometInfoService()
)
type fixture struct {
@@ -136,7 +137,7 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(grpcQueryRouter, msgRouter)), 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(), runtime.EnvWithRouterService(grpcQueryRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), runtime.NewContextAwareCometInfoService())
slashingKeeper := slashingkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), log.NewNopLogger()), cdc, codec.NewLegacyAmino(), stakingKeeper, authority.String())
@@ -150,8 +151,8 @@ func initFixture(tb testing.TB) *fixture {
authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper)
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, cdc.InterfaceRegistry())
evidenceModule := evidence.NewAppModule(cdc, *evidenceKeeper)
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, cdc.InterfaceRegistry(), cometInfoService)
evidenceModule := evidence.NewAppModule(cdc, *evidenceKeeper, cometInfoService)
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc,
encodingCfg.InterfaceRegistry.SigningContext().AddressCodec(),
@@ -241,7 +242,7 @@ func TestHandleDoubleSign(t *testing.T) {
}
ctx = ctx.WithCometInfo(nci)
assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci)))
assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci), cometInfoService))
// should be jailed and tombstoned
val, err = f.stakingKeeper.Validator(ctx, operatorAddr)
@@ -254,7 +255,7 @@ func TestHandleDoubleSign(t *testing.T) {
assert.Assert(t, newTokens.LT(oldTokens))
// submit duplicate evidence
assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx))
assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx, cometInfoService))
// tokens should be the same (capped slash)
val, err = f.stakingKeeper.Validator(ctx, operatorAddr)
@@ -329,7 +330,7 @@ func TestHandleDoubleSign_TooOld(t *testing.T) {
ctx = ctx.WithConsensusParams(cp)
ctx = ctx.WithHeaderInfo(header.Info{Height: ctx.BlockHeight() + cp.Evidence.MaxAgeNumBlocks + 1, Time: ctx.HeaderInfo().Time.Add(cp.Evidence.MaxAgeDuration + 1)})
assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx))
assert.NilError(t, f.evidenceKeeper.BeginBlocker(ctx, cometInfoService))
val, err = f.stakingKeeper.Validator(ctx, operatorAddr)
assert.NilError(t, err)
@@ -404,7 +405,7 @@ func TestHandleDoubleSignAfterRotation(t *testing.T) {
}},
}
err = f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci))
err = f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci), cometInfoService)
assert.NilError(t, err)
// should be jailed and tombstoned
@@ -420,7 +421,7 @@ func TestHandleDoubleSignAfterRotation(t *testing.T) {
assert.Assert(t, newTokens.LT(oldTokens))
// submit duplicate evidence
err = f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci))
err = f.evidenceKeeper.BeginBlocker(ctx.WithCometInfo(nci), cometInfoService)
assert.NilError(t, err)
// tokens should be the same (capped slash)
@@ -449,7 +450,10 @@ func TestHandleDoubleSignAfterRotation(t *testing.T) {
// query evidence from store
var evidences []exported.Evidence
assert.NilError(t, f.evidenceKeeper.Evidences.Walk(ctx, nil, func(key []byte, value exported.Evidence) (stop bool, err error) {
assert.NilError(t, f.evidenceKeeper.Evidences.Walk(ctx, nil, func(
key []byte,
value exported.Evidence,
) (stop bool, err error) {
evidences = append(evidences, value)
return false, nil
}))
+1 -1
View File
@@ -100,7 +100,7 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)
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))
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), runtime.NewContextAwareCometInfoService())
poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, stakingKeeper, authority.String())
+5 -3
View File
@@ -19,6 +19,7 @@ import (
stakingtestutil "cosmossdk.io/x/staking/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -83,8 +84,9 @@ func TestBeginBlocker(t *testing.T) {
BlockIDFlag: comet.BlockIDFlagCommit,
}}},
})
cometInfoService := runtime.NewContextAwareCometInfoService()
err = slashing.BeginBlocker(ctx, slashingKeeper)
err = slashing.BeginBlocker(ctx, slashingKeeper, cometInfoService)
require.NoError(t, err)
info, err := slashingKeeper.ValidatorSigningInfo.Get(ctx, sdk.ConsAddress(pk.Address()))
@@ -102,7 +104,7 @@ func TestBeginBlocker(t *testing.T) {
for ; height < signedBlocksWindow; height++ {
ctx = ctx.WithHeaderInfo(coreheader.Info{Height: height})
err = slashing.BeginBlocker(ctx, slashingKeeper)
err = slashing.BeginBlocker(ctx, slashingKeeper, cometInfoService)
require.NoError(t, err)
}
@@ -117,7 +119,7 @@ func TestBeginBlocker(t *testing.T) {
}}},
})
err = slashing.BeginBlocker(ctx, slashingKeeper)
err = slashing.BeginBlocker(ctx, slashingKeeper, cometInfoService)
require.NoError(t, err)
}
@@ -106,13 +106,15 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(queryRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
cometInfoService := runtime.NewContextAwareCometInfoService()
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(queryRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), cometInfoService)
slashingKeeper := slashingkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(queryRouter, msgRouter)), cdc, &codec.LegacyAmino{}, stakingKeeper, authority.String())
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper)
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, cdc.InterfaceRegistry())
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, cdc.InterfaceRegistry(), cometInfoService)
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc,
encodingCfg.InterfaceRegistry.SigningContext().AddressCodec(),
@@ -70,7 +70,11 @@ func generateAddresses(f *fixture, numAddrs int) ([]sdk.AccAddress, []sdk.ValAdd
return addrDels, addrVals
}
func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddress, []sdk.ValAddress, []types.Validator) {
func createValidators(
t *testing.T,
f *fixture,
powers []int64,
) ([]sdk.AccAddress, []sdk.ValAddress, []types.Validator) {
t.Helper()
addrs := simtestutil.AddTestAddrsIncremental(f.bankKeeper, f.stakingKeeper, f.sdkCtx, 5, f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, 300))
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs)
@@ -155,7 +159,7 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[types.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(queryRouter, msgRouter)), 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(), runtime.EnvWithRouterService(queryRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), runtime.NewContextAwareCometInfoService())
authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
@@ -115,7 +115,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
authority.String(),
)
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))
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), runtime.NewContextAwareCometInfoService())
authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
@@ -230,7 +230,12 @@ func createValidator(t *testing.T, rt *rapid.T, f *deterministicFixture) staking
}
// createAndSetValidatorWithStatus creates a validator with random values but with given status and sets to the state
func createAndSetValidatorWithStatus(t *testing.T, rt *rapid.T, f *deterministicFixture, status stakingtypes.BondStatus) stakingtypes.Validator {
func createAndSetValidatorWithStatus(
t *testing.T,
rt *rapid.T,
f *deterministicFixture,
status stakingtypes.BondStatus,
) stakingtypes.Validator {
t.Helper()
val := createValidator(t, rt, f)
val.Status = status
@@ -338,14 +343,26 @@ func getStaticValidator2(t *testing.T, f *deterministicFixture) stakingtypes.Val
}
// createDelegationAndDelegate funds the delegator account with a random delegation in range 100-1000 and delegates.
func createDelegationAndDelegate(t *testing.T, rt *rapid.T, f *deterministicFixture, delegator sdk.AccAddress, validator stakingtypes.Validator) (newShares math.LegacyDec, err error) {
func createDelegationAndDelegate(
t *testing.T,
rt *rapid.T,
f *deterministicFixture,
delegator sdk.AccAddress,
validator stakingtypes.Validator,
) (newShares math.LegacyDec, err error) {
t.Helper()
amt := f.stakingKeeper.TokensFromConsensusPower(f.ctx, rapid.Int64Range(100, 1000).Draw(rt, "amount"))
return fundAccountAndDelegate(t, f, delegator, validator, amt)
}
// fundAccountAndDelegate funds the delegator account with the specified delegation and delegates.
func fundAccountAndDelegate(t *testing.T, f *deterministicFixture, delegator sdk.AccAddress, validator stakingtypes.Validator, amt math.Int) (newShares math.LegacyDec, err error) {
func fundAccountAndDelegate(
t *testing.T,
f *deterministicFixture,
delegator sdk.AccAddress,
validator stakingtypes.Validator,
amt math.Int,
) (newShares math.LegacyDec, err error) {
t.Helper()
coins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amt))