refactor(core,x/**): simplify core service api and embed environment in keepers (#20071)

This commit is contained in:
Julien Robert
2024-04-17 18:18:16 +00:00
committed by GitHub
parent a4ff821981
commit 5e7aae0db1
115 changed files with 546 additions and 651 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ func (k Keeper) SigningInfos(ctx context.Context, req *types.QuerySigningInfosRe
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
store := k.environment.KVStoreService.OpenKVStore(ctx)
store := k.KVStoreService.OpenKVStore(ctx)
var signInfos []types.ValidatorSigningInfo
sigInfoStore := prefix.NewStore(runtime.KVStoreAdapter(store), types.ValidatorSigningInfoKeyPrefix)
+1 -1
View File
@@ -28,7 +28,7 @@ func (k Keeper) Hooks() Hooks {
// AfterValidatorBonded updates the signing info start height or create a new signing info
func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
signingInfo, err := h.k.ValidatorSigningInfo.Get(ctx, consAddr)
blockHeight := h.k.environment.HeaderService.GetHeaderInfo(ctx).Height
blockHeight := h.k.HeaderService.HeaderInfo(ctx).Height
if err == nil {
signingInfo.StartHeight = blockHeight
} else {
+7 -8
View File
@@ -25,8 +25,7 @@ func (k Keeper) HandleValidatorSignature(ctx context.Context, addr cryptotypes.A
}
func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params types.Params, addr cryptotypes.Address, power int64, signed comet.BlockIDFlag) error {
logger := k.Logger(ctx)
height := k.environment.HeaderService.GetHeaderInfo(ctx).Height
height := k.HeaderService.HeaderInfo(ctx).Height
// fetch the validator public key
consAddr := sdk.ConsAddress(addr)
@@ -115,7 +114,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
}
if missed {
if err := k.environment.EventService.EventManager(ctx).EmitKV(
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeLiveness,
event.NewAttribute(types.AttributeKeyAddress, consStr),
event.NewAttribute(types.AttributeKeyMissedBlocks, fmt.Sprintf("%d", signInfo.MissedBlocksCounter)),
@@ -124,7 +123,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
return err
}
logger.Debug(
k.Logger.Debug(
"absent validator",
"height", height,
"validator", consStr,
@@ -162,7 +161,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
return err
}
if err := k.environment.EventService.EventManager(ctx).EmitKV(
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeSlash,
event.NewAttribute(types.AttributeKeyAddress, consStr),
event.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
@@ -181,7 +180,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
if err != nil {
return err
}
signInfo.JailedUntil = k.environment.HeaderService.GetHeaderInfo(ctx).Time.Add(downtimeJailDur)
signInfo.JailedUntil = k.HeaderService.HeaderInfo(ctx).Time.Add(downtimeJailDur)
// We need to reset the counter & bitmap so that the validator won't be
// immediately slashed for downtime upon re-bonding.
@@ -193,7 +192,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
return err
}
logger.Info(
k.Logger.Info(
"slashing and jailing validator due to liveness fault",
"height", height,
"validator", consStr,
@@ -204,7 +203,7 @@ func (k Keeper) HandleValidatorSignatureWithParams(ctx context.Context, params t
)
} else {
// validator was (a) not found or (b) already jailed so we do not slash
logger.Info(
k.Logger.Info(
"validator would have been slashed for downtime, but was either not found in store or already jailed",
"validator", consStr,
)
+5 -10
View File
@@ -8,7 +8,6 @@ import (
"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/event"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/slashing/types"
@@ -19,7 +18,8 @@ import (
// Keeper of the slashing store
type Keeper struct {
environment appmodule.Environment
appmodule.Environment
cdc codec.BinaryCodec
legacyAmino *codec.LegacyAmino
sk types.StakingKeeper
@@ -41,7 +41,7 @@ type Keeper struct {
func NewKeeper(environment appmodule.Environment, cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, sk types.StakingKeeper, authority string) Keeper {
sb := collections.NewSchemaBuilder(environment.KVStoreService)
k := Keeper{
environment: environment,
Environment: environment,
cdc: cdc,
legacyAmino: legacyAmino,
sk: sk,
@@ -83,11 +83,6 @@ func (k Keeper) GetAuthority() string {
return k.authority
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx context.Context) log.Logger {
return k.environment.Logger.With("module", "x/"+types.ModuleName)
}
// GetPubkey returns the pubkey from the adddress-pubkey relation
func (k Keeper) GetPubkey(ctx context.Context, a cryptotypes.Address) (cryptotypes.PubKey, error) {
return k.AddrPubkeyRelation.Get(ctx, a)
@@ -120,7 +115,7 @@ func (k Keeper) SlashWithInfractionReason(ctx context.Context, consAddr sdk.Cons
return err
}
return k.environment.EventService.EventManager(ctx).EmitKV(
return k.EventService.EventManager(ctx).EmitKV(
types.EventTypeSlash,
event.NewAttribute(types.AttributeKeyAddress, consStr),
event.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", power)),
@@ -141,7 +136,7 @@ func (k Keeper) Jail(ctx context.Context, consAddr sdk.ConsAddress) error {
return err
}
if err := k.environment.EventService.EventManager(ctx).EmitKV(
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeSlash,
event.NewAttribute(types.AttributeKeyJailed, consStr),
); err != nil {
+1 -1
View File
@@ -40,7 +40,7 @@ func (m Migrator) Migrate2to3(ctx context.Context) error {
// version 3 to version 4. Specifically, it migrates the validator missed block
// bitmap.
func (m Migrator) Migrate3to4(ctx context.Context) error {
store := runtime.KVStoreAdapter(m.keeper.environment.KVStoreService.OpenKVStore(ctx))
store := runtime.KVStoreAdapter(m.keeper.KVStoreService.OpenKVStore(ctx))
params, err := m.keeper.Params.Get(ctx)
if err != nil {
return err
+1 -1
View File
@@ -62,7 +62,7 @@ func (k Keeper) Unjail(ctx context.Context, validatorAddr sdk.ValAddress) error
return types.ErrValidatorJailed
}
if k.environment.HeaderService.GetHeaderInfo(ctx).Time.Before(info.JailedUntil) {
if k.HeaderService.HeaderInfo(ctx).Time.Before(info.JailedUntil) {
return types.ErrValidatorJailed
}
}