From a6bc921c9a7f60c3f55fa3e4763e3a7a02595b60 Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Thu, 2 May 2024 21:14:22 -0400 Subject: [PATCH] refactor: fetch CometInfo from service (#20238) Co-authored-by: Marko --- runtime/comet.go | 23 ++++++++++++++++ runtime/environment.go | 5 +++- runtime/module.go | 18 +++++++++++-- simapp/app.go | 18 ++++++++++--- .../distribution/keeper/msg_server_test.go | 3 ++- .../evidence/keeper/infraction_test.go | 26 +++++++++++-------- tests/integration/gov/keeper/keeper_test.go | 2 +- tests/integration/slashing/abci_test.go | 8 +++--- .../slashing/keeper/keeper_test.go | 6 +++-- .../integration/staking/keeper/common_test.go | 8 ++++-- .../staking/keeper/deterministic_test.go | 25 +++++++++++++++--- x/evidence/CHANGELOG.md | 1 + x/evidence/depinject.go | 4 ++- x/evidence/keeper/abci.go | 5 ++-- x/evidence/module.go | 7 +++-- x/slashing/CHANGELOG.md | 1 + x/slashing/abci.go | 8 +++--- x/slashing/depinject.go | 14 +++++----- x/slashing/module.go | 10 ++++--- x/staking/CHANGELOG.md | 1 + x/staking/depinject.go | 3 +++ x/staking/keeper/historical_info.go | 5 ++-- x/staking/keeper/keeper.go | 4 +++ x/staking/keeper/keeper_test.go | 1 + 24 files changed, 154 insertions(+), 52 deletions(-) create mode 100644 runtime/comet.go diff --git a/runtime/comet.go b/runtime/comet.go new file mode 100644 index 0000000000..e0143d44df --- /dev/null +++ b/runtime/comet.go @@ -0,0 +1,23 @@ +package runtime + +import ( + "context" + + corecomet "cosmossdk.io/core/comet" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var _ corecomet.Service = &ContextAwareCometInfoService{} + +// ContextAwareCometInfoService provides CometInfo which is embedded as a value in a Context. +// This the legacy (server v1, baseapp) way of accessing CometInfo at the module level. +type ContextAwareCometInfoService struct{} + +func (c ContextAwareCometInfoService) CometInfo(ctx context.Context) corecomet.Info { + return sdk.UnwrapSDKContext(ctx).CometInfo() +} + +func NewContextAwareCometInfoService() *ContextAwareCometInfoService { + return &ContextAwareCometInfoService{} +} diff --git a/runtime/environment.go b/runtime/environment.go index 6fce64b35d..b8814df0b7 100644 --- a/runtime/environment.go +++ b/runtime/environment.go @@ -35,7 +35,10 @@ func NewEnvironment( type EnvOption func(*appmodule.Environment) -func EnvWithRouterService(queryServiceRouter *baseapp.GRPCQueryRouter, msgServiceRouter *baseapp.MsgServiceRouter) EnvOption { +func EnvWithRouterService( + queryServiceRouter *baseapp.GRPCQueryRouter, + msgServiceRouter *baseapp.MsgServiceRouter, +) EnvOption { return func(env *appmodule.Environment) { env.RouterService = NewRouterService(env.KVStoreService, queryServiceRouter, msgServiceRouter) } diff --git a/runtime/module.go b/runtime/module.go index aff84e7308..435515e4cb 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -16,6 +16,7 @@ import ( stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/core/genesis" "cosmossdk.io/core/store" "cosmossdk.io/depinject" @@ -101,6 +102,7 @@ func init() { ProvideModuleManager, ProvideAppVersionModifier, ProvideAddressCodec, + ProvideCometService, ), appconfig.Invoke(SetupAppBuilder), ) @@ -172,7 +174,11 @@ func SetupAppBuilder(inputs AppInputs) { app.ModuleManager.RegisterLegacyAminoCodec(inputs.LegacyAmino) } -func ProvideInterfaceRegistry(addressCodec address.Codec, validatorAddressCodec address.ValidatorAddressCodec, customGetSigners []signing.CustomGetSigner) (codectypes.InterfaceRegistry, error) { +func ProvideInterfaceRegistry( + addressCodec address.Codec, + validatorAddressCodec address.ValidatorAddressCodec, + customGetSigners []signing.CustomGetSigner, +) (codectypes.InterfaceRegistry, error) { signingOptions := signing.Options{ AddressCodec: addressCodec, ValidatorAddressCodec: validatorAddressCodec, @@ -209,7 +215,11 @@ func storeKeyOverride(config *runtimev1alpha1.Module, moduleName string) *runtim return nil } -func ProvideKVStoreKey(config *runtimev1alpha1.Module, key depinject.ModuleKey, app *AppBuilder) *storetypes.KVStoreKey { +func ProvideKVStoreKey( + config *runtimev1alpha1.Module, + key depinject.ModuleKey, + app *AppBuilder, +) *storetypes.KVStoreKey { override := storeKeyOverride(config, key.Name()) var storeKeyName string @@ -275,6 +285,10 @@ func ProvideAppVersionModifier(app *AppBuilder) baseapp.AppVersionModifier { return app.app } +func ProvideCometService() comet.Service { + return NewContextAwareCometInfoService() +} + type AddressCodecInputs struct { depinject.In diff --git a/simapp/app.go b/simapp/app.go index fcb756cb7b..b51977a488 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -281,6 +281,7 @@ func NewSimApp( interfaceRegistry: interfaceRegistry, keys: keys, } + cometService := runtime.NewContextAwareCometInfoService() // set the BaseApp's parameter store app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[consensusparamtypes.StoreKey]), logger.With(log.ModuleKey, "x/consensus")), authtypes.NewModuleAddress(govtypes.ModuleName).String()) @@ -338,8 +339,19 @@ func NewSimApp( app.txConfig = txConfig app.StakingKeeper = stakingkeeper.NewKeeper( - appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), logger.With(log.ModuleKey, "x/staking"), runtime.EnvWithRouterService(app.GRPCQueryRouter(), app.MsgServiceRouter())), app.AuthKeeper, app.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), signingCtx.ValidatorAddressCodec(), authcodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), + appCodec, + runtime.NewEnvironment( + runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), + logger.With(log.ModuleKey, "x/staking"), + runtime.EnvWithRouterService(app.GRPCQueryRouter(), app.MsgServiceRouter())), + app.AuthKeeper, + app.BankKeeper, + authtypes.NewModuleAddress(govtypes.ModuleName).String(), + signingCtx.ValidatorAddressCodec(), + authcodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), + cometService, ) + app.MintKeeper = mintkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[minttypes.StoreKey]), logger.With(log.ModuleKey, "x/mint")), app.StakingKeeper, app.AuthKeeper, app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String()) app.PoolKeeper = poolkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), logger.With(log.ModuleKey, "x/protocolpool")), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String()) @@ -436,11 +448,11 @@ func NewSimApp( feegrantmodule.NewAppModule(appCodec, app.AuthKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry), gov.NewAppModule(appCodec, &app.GovKeeper, app.AuthKeeper, app.BankKeeper, app.PoolKeeper), mint.NewAppModule(appCodec, app.MintKeeper, app.AuthKeeper, nil), - slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.interfaceRegistry), + slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.interfaceRegistry, cometService), distr.NewAppModule(appCodec, app.DistrKeeper, app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.PoolKeeper), staking.NewAppModule(appCodec, app.StakingKeeper, app.AuthKeeper, app.BankKeeper), upgrade.NewAppModule(app.UpgradeKeeper), - evidence.NewAppModule(appCodec, app.EvidenceKeeper), + evidence.NewAppModule(appCodec, app.EvidenceKeeper, cometService), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AuthKeeper, app.BankKeeper, app.interfaceRegistry), groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AuthKeeper, app.BankKeeper, app.interfaceRegistry), nftmodule.NewAppModule(appCodec, app.NFTKeeper, app.AuthKeeper, app.BankKeeper, app.interfaceRegistry), diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 281ade5b9c..19dd57a6c7 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -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()) diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index 4e40b006fa..20d3d2188b 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -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 })) diff --git a/tests/integration/gov/keeper/keeper_test.go b/tests/integration/gov/keeper/keeper_test.go index c29953cf13..911bcc7632 100644 --- a/tests/integration/gov/keeper/keeper_test.go +++ b/tests/integration/gov/keeper/keeper_test.go @@ -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()) diff --git a/tests/integration/slashing/abci_test.go b/tests/integration/slashing/abci_test.go index 51facaf45c..2d45d09b18 100644 --- a/tests/integration/slashing/abci_test.go +++ b/tests/integration/slashing/abci_test.go @@ -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) } diff --git a/tests/integration/slashing/keeper/keeper_test.go b/tests/integration/slashing/keeper/keeper_test.go index 294dbbccb8..8f95d921cf 100644 --- a/tests/integration/slashing/keeper/keeper_test.go +++ b/tests/integration/slashing/keeper/keeper_test.go @@ -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(), diff --git a/tests/integration/staking/keeper/common_test.go b/tests/integration/staking/keeper/common_test.go index 1ff2b47a13..f85f66a8d2 100644 --- a/tests/integration/staking/keeper/common_test.go +++ b/tests/integration/staking/keeper/common_test.go @@ -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) diff --git a/tests/integration/staking/keeper/deterministic_test.go b/tests/integration/staking/keeper/deterministic_test.go index b8fe56f48e..6cb8bd374b 100644 --- a/tests/integration/staking/keeper/deterministic_test.go +++ b/tests/integration/staking/keeper/deterministic_test.go @@ -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)) diff --git a/x/evidence/CHANGELOG.md b/x/evidence/CHANGELOG.md index ae72299b60..cd91ad4345 100644 --- a/x/evidence/CHANGELOG.md +++ b/x/evidence/CHANGELOG.md @@ -27,6 +27,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Api Breaking Changes +* [#20238](https://github.com/cosmos/cosmos-sdk/pull/20238) `NewAppModule` now takes in a `core/comet.Service` an argument. `BeginBlocker` now takes in a `core/comet.Service`. * [#20016](https://github.com/cosmos/cosmos-sdk/pull/20016) `NewMsgSubmitEvidence` now takes a string as argument instead of an `AccAddress`. * [#19482](https://github.com/cosmos/cosmos-sdk/pull/19482) `appmodule.Environment` is passed to `NewKeeper` instead of individual services * [#19627](https://github.com/cosmos/cosmos-sdk/pull/19627) `NewAppModule` now takes in a `codec.Codec` as its first argument diff --git a/x/evidence/depinject.go b/x/evidence/depinject.go index 335cef402e..9f72496a21 100644 --- a/x/evidence/depinject.go +++ b/x/evidence/depinject.go @@ -4,6 +4,7 @@ import ( modulev1 "cosmossdk.io/api/cosmos/evidence/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" eviclient "cosmossdk.io/x/evidence/client" @@ -30,6 +31,7 @@ type ModuleInputs struct { Environment appmodule.Environment Cdc codec.Codec EvidenceHandlers []eviclient.EvidenceHandler `optional:"true"` + CometService comet.Service StakingKeeper types.StakingKeeper SlashingKeeper types.SlashingKeeper @@ -45,7 +47,7 @@ type ModuleOutputs struct { func ProvideModule(in ModuleInputs) ModuleOutputs { k := keeper.NewKeeper(in.Cdc, in.Environment, in.StakingKeeper, in.SlashingKeeper, in.AddressCodec) - m := NewAppModule(in.Cdc, *k, in.EvidenceHandlers...) + m := NewAppModule(in.Cdc, *k, in.CometService, in.EvidenceHandlers...) return ModuleOutputs{EvidenceKeeper: *k, Module: m} } diff --git a/x/evidence/keeper/abci.go b/x/evidence/keeper/abci.go index a759e13ace..86dbd78adf 100644 --- a/x/evidence/keeper/abci.go +++ b/x/evidence/keeper/abci.go @@ -8,15 +8,14 @@ import ( "cosmossdk.io/x/evidence/types" "github.com/cosmos/cosmos-sdk/telemetry" - sdk "github.com/cosmos/cosmos-sdk/types" ) // BeginBlocker iterates through and handles any newly discovered evidence of // misbehavior submitted by CometBFT. Currently, only equivocation is handled. -func (k Keeper) BeginBlocker(ctx context.Context) error { +func (k Keeper) BeginBlocker(ctx context.Context, cometService comet.Service) error { defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) - bi := sdk.UnwrapSDKContext(ctx).CometInfo() + bi := cometService.CometInfo(ctx) evidences := bi.Evidence for _, evidence := range evidences { diff --git a/x/evidence/module.go b/x/evidence/module.go index b78e0060a0..4482fbf36f 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -10,6 +10,7 @@ import ( "google.golang.org/grpc" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/core/registry" eviclient "cosmossdk.io/x/evidence/client" "cosmossdk.io/x/evidence/client/cli" @@ -42,14 +43,16 @@ type AppModule struct { cdc codec.Codec evidenceHandlers []eviclient.EvidenceHandler keeper keeper.Keeper + cometService comet.Service } // NewAppModule creates a new AppModule object. -func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, evidenceHandlers ...eviclient.EvidenceHandler) AppModule { +func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, cometService comet.Service, evidenceHandlers ...eviclient.EvidenceHandler) AppModule { return AppModule{ keeper: keeper, evidenceHandlers: evidenceHandlers, cdc: cdc, + cometService: cometService, } } @@ -135,7 +138,7 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } // BeginBlock executes all ABCI BeginBlock logic respective to the evidence module. func (am AppModule) BeginBlock(ctx context.Context) error { - return am.keeper.BeginBlocker(ctx) + return am.keeper.BeginBlocker(ctx, am.cometService) } // AppModuleSimulation functions diff --git a/x/slashing/CHANGELOG.md b/x/slashing/CHANGELOG.md index 0ba949ddaa..787d759973 100644 --- a/x/slashing/CHANGELOG.md +++ b/x/slashing/CHANGELOG.md @@ -35,6 +35,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* [#20238](https://github.com/cosmos/cosmos-sdk/pull/20238) `NewAppModule` now takes in a `core/comet.Service` an argument. `BeginBlocker` now takes in a `core/comet.Service`. * [#20026](https://github.com/cosmos/cosmos-sdk/pull/20026) Removal of the Address.String() method and related changes: * `Migrate` now takes a `ValidatorAddressCodec` as argument. * `Migrator` has a new field of `ValidatorAddressCodec` type. diff --git a/x/slashing/abci.go b/x/slashing/abci.go index 72645213d8..66cf3feff6 100644 --- a/x/slashing/abci.go +++ b/x/slashing/abci.go @@ -3,16 +3,16 @@ package slashing import ( "context" + "cosmossdk.io/core/comet" "cosmossdk.io/x/slashing/keeper" "cosmossdk.io/x/slashing/types" "github.com/cosmos/cosmos-sdk/telemetry" - sdk "github.com/cosmos/cosmos-sdk/types" ) // BeginBlocker check for infraction evidence or downtime of validators // on every begin block -func BeginBlocker(ctx context.Context, k keeper.Keeper) error { +func BeginBlocker(ctx context.Context, k keeper.Keeper, cometService comet.Service) error { defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) // Iterate over all the validators which *should* have signed this block @@ -22,8 +22,8 @@ func BeginBlocker(ctx context.Context, k keeper.Keeper) error { if err != nil { return err } - sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO remove by passing the comet service - for _, vote := range sdkCtx.CometInfo().LastCommit.Votes { + ci := cometService.CometInfo(ctx) + for _, vote := range ci.LastCommit.Votes { err := k.HandleValidatorSignatureWithParams(ctx, params, vote.Validator.Address, vote.Validator.Power, vote.BlockIDFlag) if err != nil { return err diff --git a/x/slashing/depinject.go b/x/slashing/depinject.go index 60f865aff1..d1af9fa19b 100644 --- a/x/slashing/depinject.go +++ b/x/slashing/depinject.go @@ -5,6 +5,7 @@ import ( modulev1 "cosmossdk.io/api/cosmos/slashing/module/v1" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" authtypes "cosmossdk.io/x/auth/types" @@ -31,11 +32,12 @@ func init() { type ModuleInputs struct { depinject.In - Config *modulev1.Module - Environment appmodule.Environment - Cdc codec.Codec - LegacyAmino *codec.LegacyAmino - Registry cdctypes.InterfaceRegistry + Config *modulev1.Module + Environment appmodule.Environment + Cdc codec.Codec + LegacyAmino *codec.LegacyAmino + Registry cdctypes.InterfaceRegistry + CometService comet.Service AccountKeeper types.AccountKeeper BankKeeper types.BankKeeper @@ -63,7 +65,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { } k := keeper.NewKeeper(in.Environment, in.Cdc, in.LegacyAmino, in.StakingKeeper, authStr) - m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.Registry) + m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.Registry, in.CometService) return ModuleOutputs{ Keeper: k, Module: m, diff --git a/x/slashing/module.go b/x/slashing/module.go index c5ae4fef19..c38157b2d8 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -9,6 +9,7 @@ import ( "google.golang.org/grpc" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/core/registry" "cosmossdk.io/x/slashing/keeper" "cosmossdk.io/x/slashing/simulation" @@ -40,8 +41,9 @@ var ( // AppModule implements an application module for the slashing module. type AppModule struct { - cdc codec.Codec - registry cdctypes.InterfaceRegistry + cdc codec.Codec + registry cdctypes.InterfaceRegistry + cometService comet.Service keeper keeper.Keeper accountKeeper types.AccountKeeper @@ -57,6 +59,7 @@ func NewAppModule( bk types.BankKeeper, sk types.StakingKeeper, registry cdctypes.InterfaceRegistry, + cs comet.Service, ) AppModule { return AppModule{ cdc: cdc, @@ -65,6 +68,7 @@ func NewAppModule( accountKeeper: ak, bankKeeper: bk, stakingKeeper: sk, + cometService: cs, } } @@ -158,7 +162,7 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion } // BeginBlock returns the begin blocker for the slashing module. func (am AppModule) BeginBlock(ctx context.Context) error { - return BeginBlocker(ctx, am.keeper) + return BeginBlocker(ctx, am.keeper, am.cometService) } // AppModuleSimulation functions diff --git a/x/staking/CHANGELOG.md b/x/staking/CHANGELOG.md index aa45a5df80..dda05c42d9 100644 --- a/x/staking/CHANGELOG.md +++ b/x/staking/CHANGELOG.md @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* [#20238](https://github.com/cosmos/cosmos-sdk/pull/20238) `NewKeeper` now accepts a `core/comet.Service` as its last argument. * [#19788](https://github.com/cosmos/cosmos-sdk/pull/19788) Remove `ABCIValidatorUpdate` and `ABCIValidatorUpdateZero`, use `ModuleValidatorUpdate` and `ModuleValidatorUpdateIsZero` instead. * [#19754](https://github.com/cosmos/cosmos-sdk/pull/19754) Update to use `[]appmodule.ValidatorUpdate` as return for `ApplyAndReturnValidatorSetUpdates`. * [#19414](https://github.com/cosmos/cosmos-sdk/pull/19414) `NewStakingKeeper` takes an environment variable instead of individual services. diff --git a/x/staking/depinject.go b/x/staking/depinject.go index 1eaf44baa8..a4dfc2630d 100644 --- a/x/staking/depinject.go +++ b/x/staking/depinject.go @@ -9,6 +9,7 @@ import ( modulev1 "cosmossdk.io/api/cosmos/staking/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" authtypes "cosmossdk.io/x/auth/types" @@ -44,6 +45,7 @@ type ModuleInputs struct { BankKeeper types.BankKeeper Cdc codec.Codec Environment appmodule.Environment + CometInfoService comet.Service } // Dependency Injection Outputs @@ -74,6 +76,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { as, in.ValidatorAddressCodec, in.ConsensusAddressCodec, + in.CometInfoService, ) m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper) return ModuleOutputs{StakingKeeper: k, Module: m} diff --git a/x/staking/keeper/historical_info.go b/x/staking/keeper/historical_info.go index 9e556b8f88..d0d500ebad 100644 --- a/x/staking/keeper/historical_info.go +++ b/x/staking/keeper/historical_info.go @@ -4,8 +4,6 @@ import ( "context" "cosmossdk.io/x/staking/types" - - sdk "github.com/cosmos/cosmos-sdk/types" ) // TrackHistoricalInfo saves the latest historical-info and deletes the oldest @@ -43,9 +41,10 @@ func (k Keeper) TrackHistoricalInfo(ctx context.Context) error { return nil } + ci := k.cometInfoService.CometInfo(ctx) historicalEntry := types.HistoricalRecord{ Time: &headerInfo.Time, - ValidatorsHash: sdk.UnwrapSDKContext(ctx).CometInfo().ValidatorsHash, + ValidatorsHash: ci.ValidatorsHash, Apphash: headerInfo.AppHash, } diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 8db9e7dbaa..0ac1aae23b 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -11,6 +11,7 @@ import ( "cosmossdk.io/collections/indexes" addresscodec "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/comet" "cosmossdk.io/math" "cosmossdk.io/x/staking/types" @@ -76,6 +77,7 @@ type Keeper struct { authority string validatorAddressCodec addresscodec.Codec consensusAddressCodec addresscodec.Codec + cometInfoService comet.Service Schema collections.Schema @@ -138,6 +140,7 @@ func NewKeeper( authority string, validatorAddressCodec addresscodec.Codec, consensusAddressCodec addresscodec.Codec, + cometInfoService comet.Service, ) *Keeper { sb := collections.NewSchemaBuilder(env.KVStoreService) // ensure bonded and not bonded module accounts are set @@ -167,6 +170,7 @@ func NewKeeper( authority: authority, validatorAddressCodec: validatorAddressCodec, consensusAddressCodec: consensusAddressCodec, + cometInfoService: cometInfoService, LastTotalPower: collections.NewItem(sb, types.LastTotalPowerKey, "last_total_power", sdk.IntValue), HistoricalInfo: collections.NewMap(sb, types.HistoricalInfoKey, "historical_info", collections.Uint64Key, HistoricalInfoCodec(cdc)), Delegations: collections.NewMap( diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index 0c75f5c28b..4bb2c4bf67 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -98,6 +98,7 @@ func (s *KeeperTestSuite) SetupTest() { authority, address.NewBech32Codec("cosmosvaloper"), address.NewBech32Codec("cosmosvalcons"), + runtime.NewContextAwareCometInfoService(), ) require.NoError(keeper.Params.Set(ctx, stakingtypes.DefaultParams()))