-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: fetch CometInfo from service #20238
Changes from 13 commits
bd3bee3
19927df
02fd92d
ec59398
13fd302
fb6f2a7
75cfec2
50427ef
0541b0b
de0a145
99db87f
de6cba4
f896734
bb5368f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper error handling for - 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())
+ stakingKeeper, err := 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())
+ require.NoError(t, err) This change ensures that any errors during the initialization of Committable suggestion
Suggested change
|
||||||||
|
||||||||
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 | ||||||||
})) | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper error handling for - 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())
+ stakingKeeper, err := 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())
+ require.NoError(t, err) This change ensures that any errors during the initialization of Committable suggestion
Suggested change
|
||||||||
|
||||||||
poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, stakingKeeper, authority.String()) | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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) | ||||||||||||||||||
Comment on lines
+87
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Introduce error handling for - cometInfoService := runtime.NewContextAwareCometInfoService()
+ cometInfoService, err := runtime.NewContextAwareCometInfoService()
+ require.NoError(t, err) This change ensures that any errors during the initialization of Committable suggestion
Suggested change
|
||||||||||||||||||
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) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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()) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper error handling for - 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())
+ stakingKeeper, err := 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())
+ require.NoError(t, err) This change ensures that any errors during the initialization of Committable suggestion
Suggested change
|
||||||||
|
||||||||
authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts) | ||||||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper) | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
CometInfo
directly unwraps the SDK context to fetchCometInfo
. Consider error handling if the context cannot be unwrapped or ifCometInfo
is not available.Committable suggestion