Skip to content
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

Cap Initialization Fix #9392

Merged
merged 17 commits into from
Jul 6, 2021
30 changes: 25 additions & 5 deletions x/capability/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/capability/types"
)

var initialized = false
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
type (
// Keeper defines the capability module's keeper. It is responsible for provisioning,
// tracking, and authenticating capabilities at runtime. During application
Expand Down Expand Up @@ -42,11 +44,12 @@ type (
// by name, in addition to creating new capabilities & authenticating capabilities
// passed by other modules.
ScopedKeeper struct {
cdc codec.BinaryMarshaler
storeKey sdk.StoreKey
memKey sdk.StoreKey
capMap map[uint64]*types.Capability
module string
cdc codec.BinaryMarshaler
storeKey sdk.StoreKey
memKey sdk.StoreKey
capMap map[uint64]*types.Capability
module string
initialized bool
}
)

Expand Down Expand Up @@ -342,6 +345,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
}
Expand All @@ -358,6 +377,7 @@ func (sk ScopedKeeper) GetCapability(ctx sdk.Context, name string) (*types.Capab
// so we delete here to remove unnecessary values in map
// TODO: Delete index correctly from capMap by storing some reverse lookup
// in-memory map. Issue: https://github.com/cosmos/cosmos-sdk/issues/7805

return nil, false
}

Expand Down