refactor(x/staking): introduce and use TryUnwrapSDKContext (#20699)
This commit is contained in:
parent
e26cb4813e
commit
0b42bf97f4
@ -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))
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user