From 274a33dc0cc8012ffaf98457b571333f93af4019 Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 6 Jul 2021 15:12:56 +0200 Subject: [PATCH 1/2] x/capability: Cap Initialization Fix (#9392) (cherry picked from commit 3776793ce6492f1df0d7134655e82dc12cdf2052) # Conflicts: # x/ibc/applications/transfer/simulation/params.go --- x/bank/simulation/params.go | 2 +- x/capability/keeper/keeper.go | 25 +++++++++++++++ .../transfer/simulation/params.go | 31 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 x/ibc/applications/transfer/simulation/params.go diff --git a/x/bank/simulation/params.go b/x/bank/simulation/params.go index c3da182205b7..222f88acaba2 100644 --- a/x/bank/simulation/params.go +++ b/x/bank/simulation/params.go @@ -23,7 +23,7 @@ func ParamChanges(r *rand.Rand) []simtypes.ParamChange { if err != nil { panic(err) } - return fmt.Sprintf("%s", paramsBytes) + return string(paramsBytes) }, ), simulation.NewSimParamChange(types.ModuleName, string(types.KeyDefaultSendEnabled), diff --git a/x/capability/keeper/keeper.go b/x/capability/keeper/keeper.go index 2398ec2aa215..ead46dd1f4f9 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 @@ -342,6 +350,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 } @@ -358,6 +382,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 } diff --git a/x/ibc/applications/transfer/simulation/params.go b/x/ibc/applications/transfer/simulation/params.go new file mode 100644 index 000000000000..0f4fefb93b43 --- /dev/null +++ b/x/ibc/applications/transfer/simulation/params.go @@ -0,0 +1,31 @@ +package simulation + +import ( + "math/rand" + + gogotypes "github.com/gogo/protobuf/types" + + "github.com/cosmos/cosmos-sdk/x/simulation" + + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types" +) + +// ParamChanges defines the parameters that can be modified by param change proposals +// on the simulation +func ParamChanges(r *rand.Rand) []simtypes.ParamChange { + return []simtypes.ParamChange{ + simulation.NewSimParamChange(types.ModuleName, string(types.KeySendEnabled), + func(r *rand.Rand) string { + sendEnabled := RadomEnabled(r) + return string(types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: sendEnabled})) + }, + ), + simulation.NewSimParamChange(types.ModuleName, string(types.KeyReceiveEnabled), + func(r *rand.Rand) string { + receiveEnabled := RadomEnabled(r) + return string(types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: receiveEnabled})) + }, + ), + } +} From b6a10f67f62e355954cd1437ee756d31b6eeccec Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Wed, 28 Jul 2021 13:00:19 +0200 Subject: [PATCH 2/2] remove ibc file --- .../transfer/simulation/params.go | 31 ------------------- 1 file changed, 31 deletions(-) delete mode 100644 x/ibc/applications/transfer/simulation/params.go diff --git a/x/ibc/applications/transfer/simulation/params.go b/x/ibc/applications/transfer/simulation/params.go deleted file mode 100644 index 0f4fefb93b43..000000000000 --- a/x/ibc/applications/transfer/simulation/params.go +++ /dev/null @@ -1,31 +0,0 @@ -package simulation - -import ( - "math/rand" - - gogotypes "github.com/gogo/protobuf/types" - - "github.com/cosmos/cosmos-sdk/x/simulation" - - simtypes "github.com/cosmos/cosmos-sdk/types/simulation" - "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types" -) - -// ParamChanges defines the parameters that can be modified by param change proposals -// on the simulation -func ParamChanges(r *rand.Rand) []simtypes.ParamChange { - return []simtypes.ParamChange{ - simulation.NewSimParamChange(types.ModuleName, string(types.KeySendEnabled), - func(r *rand.Rand) string { - sendEnabled := RadomEnabled(r) - return string(types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: sendEnabled})) - }, - ), - simulation.NewSimParamChange(types.ModuleName, string(types.KeyReceiveEnabled), - func(r *rand.Rand) string { - receiveEnabled := RadomEnabled(r) - return string(types.ModuleCdc.MustMarshalJSON(&gogotypes.BoolValue{Value: receiveEnabled})) - }, - ), - } -}