From b8dcf78301f92a2fcdacf539397e8f3bd476d9c0 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 28 Jul 2021 15:08:20 +0200 Subject: [PATCH] fix: Cap Initialization Fix (forward-port #9392) (#9805) * x/capability: Cap Initialization Fix (#9392) (cherry picked from commit 3776793ce6492f1df0d7134655e82dc12cdf2052) # Conflicts: # x/ibc/applications/transfer/simulation/params.go * remove ibc file Co-authored-by: Aditya Co-authored-by: Amaury M <1293565+amaurym@users.noreply.github.com> --- x/capability/keeper/keeper.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/x/capability/keeper/keeper.go b/x/capability/keeper/keeper.go index 35b8addf4273..f0b85ca1b6e2 100644 --- a/x/capability/keeper/keeper.go +++ b/x/capability/keeper/keeper.go @@ -13,6 +13,14 @@ import ( "github.com/cosmos/cosmos-sdk/x/capability/types" ) +// initialized is a global variable used by GetCapability to ensure that the memory store +// and capability map are correctly populated. A state-synced node may copy over all the persistent +// state and start running the application without having the in-memory state required for x/capability. +// Thus, we must initialized the memory stores on-the-fly during tx execution once the first GetCapability +// is called. +// This is a temporary fix and should be replaced by a more robust solution in the next breaking release. +var initialized = false + type ( // Keeper defines the capability module's keeper. It is responsible for provisioning, // tracking, and authenticating capabilities at runtime. During application @@ -362,6 +370,22 @@ func (sk ScopedKeeper) ReleaseCapability(ctx sdk.Context, cap *types.Capability) // by name. The module is not allowed to retrieve capabilities which it does not // own. func (sk ScopedKeeper) GetCapability(ctx sdk.Context, name string) (*types.Capability, bool) { + // Create a keeper that will set all in-memory mappings correctly into memstore and capmap if scoped keeper is not initialized yet. + // This ensures that the in-memory mappings are correctly filled in, in case this is a state-synced node. + // This is a temporary non-breaking fix, a future PR should store the reverse mapping in the persistent store and reconstruct forward mapping and capmap on the fly. + if !initialized { + // create context with infinite gas meter to avoid app state mismatch. + initCtx := ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) + k := Keeper{ + cdc: sk.cdc, + storeKey: sk.storeKey, + memKey: sk.memKey, + capMap: sk.capMap, + } + k.InitializeAndSeal(initCtx) + initialized = true + } + if strings.TrimSpace(name) == "" { return nil, false }