From 592eb8704f39efa8a8c011ba807278ad14f56cb1 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Thu, 22 Jun 2023 20:21:24 +0200 Subject: [PATCH] fix ibckeeper injection --- ignite/templates/app/files/app/app.go.plush | 9 +++++- ignite/templates/app/files/app/ibc.go.plush | 4 +-- .../keeper/msg_{{queryName}}.go.plush | 6 ++-- .../keeper/{{packetName}}.go.plush | 2 +- .../x/{{moduleName}}/keeper/keeper.go.plush | 30 ++++++------------- .../base/x/{{moduleName}}/module.go.plush | 9 +++--- .../testutil/keeper/{{moduleName}}.go.plush | 7 ++--- ignite/templates/module/create/ibc.go | 1 - 8 files changed, 30 insertions(+), 38 deletions(-) diff --git a/ignite/templates/app/files/app/app.go.plush b/ignite/templates/app/files/app/app.go.plush index bd22e60234..7b8eb4e661 100644 --- a/ignite/templates/app/files/app/app.go.plush +++ b/ignite/templates/app/files/app/app.go.plush @@ -220,6 +220,8 @@ func New( depinject.Supply( // supply the application options appOpts, + // supply ibc keeper getter for the IBC modules + app.GetIBCeKeeper, // ADVANCED CONFIGURATION // @@ -302,7 +304,7 @@ func New( app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...) // Register legacy modules - app.RegisterIBCModules() + app.registerIBCModules() // load state streaming if enabled if _, _, err := streaming.LoadStreamingServices(app.App.BaseApp, appOpts, app.appCodec, logger, app.kvStoreKeys()); err != nil { @@ -429,6 +431,11 @@ func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig docs.RegisterOpenAPIService(Name, apiSvr.Router) } +// GetIBCeKeeper returns the IBC keeper +func (app *App) GetIBCeKeeper() *ibckeeper.Keeper { + return app.IBCKeeper +} + // GetMaccPerms returns a copy of the module account permissions // // NOTE: This is solely to be used for testing purposes. diff --git a/ignite/templates/app/files/app/ibc.go.plush b/ignite/templates/app/files/app/ibc.go.plush index ea2e671478..485b160784 100644 --- a/ignite/templates/app/files/app/ibc.go.plush +++ b/ignite/templates/app/files/app/ibc.go.plush @@ -32,7 +32,7 @@ import ( // this line is used by starport scaffolding # ibc/app/import ) -func (app *App) RegisterIBCModules() { +func (app *App) registerIBCModules() { // set up non depinject support modules store keys app.keys = sdk.NewKVStoreKeys( ibcexported.StoreKey, @@ -161,6 +161,4 @@ func (app *App) RegisterIBCModules() { app.ScopedIBCTransferKeeper = scopedIBCTransferKeeper app.ScopedICAHostKeeper = scopedICAHostKeeper app.ScopedICAControllerKeeper = scopedICAControllerKeeper - - app.ModuleManager.RegisterInvariants(app.CrisisKeeper) } diff --git a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/msg_{{queryName}}.go.plush b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/msg_{{queryName}}.go.plush index 34e3454fd2..4c17a3d3d8 100644 --- a/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/msg_{{queryName}}.go.plush +++ b/ignite/templates/ibc/files/oracle/x/{{moduleName}}/keeper/msg_{{queryName}}.go.plush @@ -20,7 +20,7 @@ func (k msgServer) <%= queryName.UpperCamel %>Data(goCtx context.Context, msg *t ctx := sdk.UnwrapSDKContext(goCtx) sourcePort := types.PortID - sourceChannelEnd, found := k.channelKeeper.GetChannel(ctx, sourcePort, msg.SourceChannel) + sourceChannelEnd, found := k.ibcKeeperFn().ChannelKeeper.GetChannel(ctx, sourcePort, msg.SourceChannel) if !found { return nil, sdkerrors.Wrapf( sdkerrors.ErrUnknownRequest, @@ -33,7 +33,7 @@ func (k msgServer) <%= queryName.UpperCamel %>Data(goCtx context.Context, msg *t destinationChannel := sourceChannelEnd.GetCounterparty().GetChannelID() // get the next sequence - sequence, found := k.channelKeeper.GetNextSequenceSend(ctx, sourcePort, msg.SourceChannel) + sequence, found := k.ibcKeeperFn().ChannelKeeper.GetNextSequenceSend(ctx, sourcePort, msg.SourceChannel) if !found { return nil, sdkerrors.Wrapf( channeltypes.ErrSequenceSendNotFound, @@ -64,5 +64,5 @@ func (k msgServer) <%= queryName.UpperCamel %>Data(goCtx context.Context, msg *t return sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, "cannot marshal the packet: " + err.Error()) } - return k.channelKeeper.SendPacket(ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, packetBytes) + return k.ibcKeeperFn().ChannelKeeper.SendPacket(ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, packetBytes) } diff --git a/ignite/templates/ibc/files/packet/component/x/{{moduleName}}/keeper/{{packetName}}.go.plush b/ignite/templates/ibc/files/packet/component/x/{{moduleName}}/keeper/{{packetName}}.go.plush index 36a53b2e7e..7c36266d1d 100644 --- a/ignite/templates/ibc/files/packet/component/x/{{moduleName}}/keeper/{{packetName}}.go.plush +++ b/ignite/templates/ibc/files/packet/component/x/{{moduleName}}/keeper/{{packetName}}.go.plush @@ -30,7 +30,7 @@ func (k Keeper) Transmit<%= packetName.UpperCamel %>Packet( return 0, sdkerrors.Wrapf(sdkerrors.ErrJSONMarshal, "cannot marshal the packet: %w", err) } - return k.channelKeeper.SendPacket(ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, packetBytes) + return k.ibcKeeperFn().ChannelKeeper.SendPacket(ctx, channelCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, packetBytes) } // OnRecv<%= packetName.UpperCamel %>Packet processes packet reception diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush index bf19dc0eab..472305d6b0 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/keeper/keeper.go.plush @@ -11,7 +11,8 @@ import ( channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" host "github.com/cosmos/ibc-go/v7/modules/core/24-host" - "github.com/cosmos/ibc-go/v7/modules/core/exported"<% } %> + "github.com/cosmos/ibc-go/v7/modules/core/exported" + ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"<% } %> "<%= modulePath %>/x/<%= moduleName %>/types" ) @@ -26,8 +27,7 @@ type ( // should be the x/gov module account. authority string <%= if (isIBC) { %> - channelKeeper types.ChannelKeeper - portKeeper types.PortKeeper + ibcKeeperFn func() *ibckeeper.Keeper scopedKeeper exported.ScopedKeeper<% } %> <%= for (dependency) in dependencies { %> <%= toVariableName(dependency.KeeperName()) %> types.<%= dependency.KeeperName() %><% } %> @@ -38,9 +38,8 @@ func NewKeeper( cdc codec.BinaryCodec, storeKey, memKey storetypes.StoreKey, - authority string, - <%= if (isIBC) { %>channelKeeper types.ChannelKeeper, - portKeeper types.PortKeeper, + authority string,<%= if (isIBC) { %> + ibcKeeperFn func() *ibckeeper.Keeper, scopedKeeper exported.ScopedKeeper,<% } %> <%= for (dependency) in dependencies { %> <%= toVariableName(dependency.KeeperName()) %> types.<%= dependency.KeeperName() %>,<% } %> @@ -53,10 +52,8 @@ func NewKeeper( cdc: cdc, storeKey: storeKey, memKey: memKey, - authority: authority, - <%= if (isIBC) { %> - channelKeeper: channelKeeper, - portKeeper: portKeeper, + authority: authority,<%= if (isIBC) { %> + ibcKeeperFn: ibcKeeperFn, scopedKeeper: scopedKeeper,<% } %> <%= for (dependency) in dependencies { %> <%= toVariableName(dependency.KeeperName()) %>: <%= toVariableName(dependency.KeeperName()) %>,<% } %> @@ -84,7 +81,7 @@ func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error { if !ok { return sdkerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName) } - return k.channelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap) + return k.ibcKeeperFn().ChannelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap) } // IsBound checks if the IBC app module is already bound to the desired port @@ -96,7 +93,7 @@ func (k Keeper) IsBound(ctx sdk.Context, portID string) bool { // BindPort defines a wrapper function for the port Keeper's function in // order to expose it to module's InitGenesis function func (k Keeper) BindPort(ctx sdk.Context, portID string) error { - cap := k.portKeeper.BindPort(ctx, portID) + cap := k.ibcKeeperFn().PortKeeper.BindPort(ctx, portID) return k.ClaimCapability(ctx, cap, host.PortPath(portID)) } @@ -126,13 +123,4 @@ func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability // ScopedKeeper returns the ScopedKeeper func (k Keeper) ScopedKeeper() exported.ScopedKeeper { return k.scopedKeeper -} - -// SetIBCKeepers set IBCKeepers for Keeper -func (k Keeper) SetIBCKeepers( - channelKeeper types.ChannelKeeper, - portKeeper types.PortKeeper, -) { - k.channelKeeper = channelKeeper - k.portKeeper = portKeeper }<% } %> diff --git a/ignite/templates/module/create/files/base/x/{{moduleName}}/module.go.plush b/ignite/templates/module/create/files/base/x/{{moduleName}}/module.go.plush index 7361cbce31..0d7066038b 100644 --- a/ignite/templates/module/create/files/base/x/{{moduleName}}/module.go.plush +++ b/ignite/templates/module/create/files/base/x/{{moduleName}}/module.go.plush @@ -19,7 +19,8 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" <%= if (isIBC) { %>porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" - capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"<% } %> + capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" + ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"<% } %> // this line is used by starport scaffolding # 1 @@ -193,7 +194,8 @@ type <%= title(moduleName) %>Inputs struct { BankKeeper types.BankKeeper<%= for (dependency) in dependencies { %><%= if (dependency.Name != "Bank" && dependency.Name != "Account") { %> <%= dependency.KeeperName() %> types.<%= dependency.KeeperName() %><% } %><% } %> - <%= if (isIBC) { %> CapabilityKeeper *capabilitykeeper.Keeper<% } %> + <%= if (isIBC) { %> CapabilityKeeper *capabilitykeeper.Keeper + IBCKeeperFn func() *ibckeeper.Keeper<% } %> } type <%= title(moduleName) %>Outputs struct { @@ -215,8 +217,7 @@ func ProvideModule(in <%= title(moduleName) %>Inputs) <%= title(moduleName) %>Ou in.KvStoreKey, in.MemStoreKey, authority.String(), <%= if (isIBC) { %> - nil, - nil, + in.IBCKeeperFn, scopedVoteKeeper,<% } %><%= for (dependency) in dependencies { %> in.<%= dependency.KeeperName() %>,<% } %> ) diff --git a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush b/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush index 8cc484a806..cc6ba6d957 100644 --- a/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush +++ b/ignite/templates/module/create/files/ibc/testutil/keeper/{{moduleName}}.go.plush @@ -57,8 +57,6 @@ func (<%= moduleName %>PortKeeper) BindPort(ctx sdk.Context, portID string) *cap return &capabilitytypes.Capability{} } - - func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context) { logger := log.NewNopLogger() @@ -81,8 +79,9 @@ func <%= title(moduleName) %>Keeper(t testing.TB) (keeper.Keeper, sdk.Context) { storeKey, memStoreKey, authority.String(), - <%= moduleName %>ChannelKeeper{}, - <%= moduleName %>PortKeeper{}, + func() *ibckeeper.Keeper { + return &ibckeeper.Keeper{} + }, capabilityKeeper.ScopeToModule("<%= title(moduleName) %>ScopedKeeper"),<%= for (dependency) in dependencies { %> nil,<% } %> ) diff --git a/ignite/templates/module/create/ibc.go b/ignite/templates/module/create/ibc.go index a8863cee37..2a5d969cc2 100644 --- a/ignite/templates/module/create/ibc.go +++ b/ignite/templates/module/create/ibc.go @@ -197,7 +197,6 @@ func appIBCModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunF // create IBC module templateIBCModule := `%[2]vIBCModule := ibcfee.NewIBCMiddleware(%[2]vmodule.NewIBCModule(app.%[3]vKeeper), app.IBCFeeKeeper) ibcRouter.AddRoute(%[2]vmoduletypes.ModuleName, %[2]vIBCModule) -app.%[3]vKeeper.SetIBCKeepers(app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper) %[1]v` replacementIBCModule := fmt.Sprintf( templateIBCModule,