refactor(x/staking): introduce and use TryUnwrapSDKContext (#20699)

This commit is contained in:
Matt Kocubinski 2024-06-18 07:57:08 -05:00 committed by GitHub
parent e26cb4813e
commit 0b42bf97f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 3 deletions

View File

@ -393,6 +393,19 @@ func UnwrapSDKContext(ctx context.Context) Context {
return ctx.Value(SdkContextKey).(Context)
}
// TryUnwrapSDKContext attempts to retrieve a Context from a context.Context
func TryUnwrapSDKContext(ctx context.Context) (Context, bool) {
if sdkCtx, ok := ctx.(Context); ok {
return sdkCtx, true
}
v := ctx.Value(SdkContextKey)
if v == nil {
return Context{}, false
}
c, ok := v.(Context)
return c, ok
}
// ToSDKEvidence takes comet evidence and returns sdk evidence
func ToSDKEvidence(ev []abci.Misbehavior) []comet.Evidence {
evidence := make([]comet.Evidence, len(ev))

View File

@ -26,9 +26,11 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) ([]ap
// initialized for the validator set e.g. with a one-block offset - the
// first TM block is at height 1, so state updates applied from
// genesis.json are in block 0.
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx = sdkCtx.WithBlockHeight(1 - sdk.ValidatorUpdateDelay) // TODO: remove this need for WithBlockHeight
ctx = sdkCtx
if sdkCtx, ok := sdk.TryUnwrapSDKContext(ctx); ok {
// this munging of the context is not necessary for server/v2 code paths, `ok` will be false
sdkCtx = sdkCtx.WithBlockHeight(1 - sdk.ValidatorUpdateDelay) // TODO: remove this need for WithBlockHeight
ctx = sdkCtx
}
if err := k.Params.Set(ctx, data.Params); err != nil {
return nil, err