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

feat: use ibcmodule idiom #2692

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ignite/templates/ibc/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func moduleOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genn
}

// Recv packet dispatch
templateRecv := `oracleAck, err := am.handleOraclePacket(ctx, modulePacket)
templateRecv := `oracleAck, err := im.handleOraclePacket(ctx, modulePacket)
if err != nil {
return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: "+err.Error()))
} else if ack != oracleAck {
Expand All @@ -97,7 +97,7 @@ func moduleOracleModify(replacer placeholder.Replacer, opts *OracleOptions) genn
content := replacer.ReplaceOnce(f.String(), PlaceholderOraclePacketModuleRecv, replacementRecv)

// Ack packet dispatch
templateAck := `sdkResult, err := am.handleOracleAcknowledgment(ctx, ack, modulePacket)
templateAck := `sdkResult, err := im.handleOracleAcknowledgment(ctx, ack, modulePacket)
if err != nil {
return err
}
Expand Down Expand Up @@ -334,7 +334,7 @@ func packetHandlerOracleModify(replacer placeholder.Replacer, opts *OracleOption
return ack, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest,
"cannot decode the %[2]v received packet")
}
am.keeper.Set%[3]vResult(ctx, types.OracleRequestID(modulePacketData.RequestID), %[2]vResult)
im.keeper.Set%[3]vResult(ctx, types.OracleRequestID(modulePacketData.RequestID), %[2]vResult)

// TODO: %[3]v oracle data reception logic
%[1]v`
Expand All @@ -350,7 +350,7 @@ func packetHandlerOracleModify(replacer placeholder.Replacer, opts *OracleOption
return nil, sdkerrors.Wrap(err,
"cannot decode the %[2]v oracle acknowledgment packet")
}
am.keeper.SetLast%[3]vID(ctx, requestID)
im.keeper.SetLast%[3]vID(ctx, requestID)
return &sdk.Result{}, nil
%[1]v`
replacementInterface := fmt.Sprintf(templateAck, PlaceholderOracleModuleAck,
Expand Down
4 changes: 2 additions & 2 deletions ignite/templates/ibc/oracle/x/{{moduleName}}/oracle.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// handleOraclePacket handles the result of the received BandChain oracles
// packet and saves the data into the KV database
func (am AppModule) handleOraclePacket(
func (im IBCModule) handleOraclePacket(
ctx sdk.Context,
modulePacket channeltypes.Packet,
) (channeltypes.Acknowledgement, error) {
Expand Down Expand Up @@ -41,7 +41,7 @@ func (am AppModule) handleOraclePacket(

// handleOracleAcknowledgment handles the acknowledgment result from the BandChain
// request and saves the request-id into the KV database
func (am AppModule) handleOracleAcknowledgment(
func (im IBCModule) handleOracleAcknowledgment(
ctx sdk.Context,
ack channeltypes.Acknowledgement,
modulePacket channeltypes.Packet,
Expand Down
6 changes: 3 additions & 3 deletions ignite/templates/ibc/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func moduleModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunF

// Recv packet dispatch
templateRecv := `case *types.%[2]vPacketData_%[3]vPacket:
packetAck, err := am.keeper.OnRecv%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket)
packetAck, err := im.keeper.OnRecv%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket)
if err != nil {
ack = channeltypes.NewErrorAcknowledgement(err)
} else {
Expand Down Expand Up @@ -138,7 +138,7 @@ func moduleModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunF

// Ack packet dispatch
templateAck := `case *types.%[2]vPacketData_%[3]vPacket:
err := am.keeper.OnAcknowledgement%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket, ack)
err := im.keeper.OnAcknowledgement%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket, ack)
if err != nil {
return err
}
Expand All @@ -154,7 +154,7 @@ func moduleModify(replacer placeholder.Replacer, opts *PacketOptions) genny.RunF

// Timeout packet dispatch
templateTimeout := `case *types.%[2]vPacketData_%[3]vPacket:
err := am.keeper.OnTimeout%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket)
err := im.keeper.OnTimeout%[3]vPacket(ctx, modulePacket, *packet.%[3]vPacket)
if err != nil {
return err
}
Expand Down
15 changes: 13 additions & 2 deletions ignite/templates/module/create/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,26 @@ func appIBCModify(replacer placeholder.Replacer, opts *CreateOptions) genny.RunF
return err
}

// create IBC module
templateIBCModule := `%[2]vIBCModule := %[2]vmodule.NewIBCModule(app.%[3]vKeeper)
%[1]v`
replacementIBCModule := fmt.Sprintf(
templateIBCModule,
module.PlaceholderSgAppKeeperDefinition,
opts.ModuleName,
xstrings.Title(opts.ModuleName),
)
content := replacer.Replace(f.String(), module.PlaceholderSgAppKeeperDefinition, replacementIBCModule)

// Add route to IBC router
templateRouter := `ibcRouter.AddRoute(%[2]vmoduletypes.ModuleName, %[2]vModule)
templateRouter := `ibcRouter.AddRoute(%[2]vmoduletypes.ModuleName, %[2]vIBCModule)
%[1]v`
replacementRouter := fmt.Sprintf(
templateRouter,
module.PlaceholderIBCAppRouter,
opts.ModuleName,
)
content := replacer.Replace(f.String(), module.PlaceholderIBCAppRouter, replacementRouter)
content = replacer.Replace(content, module.PlaceholderIBCAppRouter, replacementRouter)

// Scoped keeper declaration for the module
templateScopedKeeperDeclaration := `Scoped%[1]vKeeper capabilitykeeper.ScopedKeeper`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ import (
porttypes "github.com/cosmos/ibc-go/v5/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v5/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v5/modules/core/exported"
"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
)

type IBCModule struct {
keeper keeper.Keeper
}

func NewIBCModule(k keeper.Keeper) IBCModule {
return IBCModule{
keeper: k,
}
}

// OnChanOpenInit implements the IBCModule interface
func (am AppModule) OnChanOpenInit(
func (im IBCModule) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
Expand All @@ -29,7 +40,7 @@ func (am AppModule) OnChanOpenInit(
}<% } %>

// Require portID is the portID module is bound to
boundPort := am.keeper.GetPort(ctx)
boundPort := im.keeper.GetPort(ctx)
if boundPort != portID {
return "", sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
}
Expand All @@ -39,15 +50,15 @@ func (am AppModule) OnChanOpenInit(
}

// Claim channel capability passed back by IBC module
if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return "", err
}

return "", nil
}

// OnChanOpenTry implements the IBCModule interface
func (am AppModule) OnChanOpenTry(
func (im IBCModule) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
Expand All @@ -62,7 +73,7 @@ func (am AppModule) OnChanOpenTry(
}<% } %>

// Require portID is the portID module is bound to
boundPort := am.keeper.GetPort(ctx)
boundPort := im.keeper.GetPort(ctx)
if boundPort != portID {
return "", sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
}
Expand All @@ -75,9 +86,9 @@ func (am AppModule) OnChanOpenTry(
// (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry)
// If module can already authenticate the capability then module already owns it so we don't need to claim
// Otherwise, module does not have channel capability and we must claim it from IBC
if !am.keeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) {
if !im.keeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) {
// Only claim channel capability passed back by IBC module if we do not already own it
if err := am.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return "", err
}
}
Expand All @@ -86,7 +97,7 @@ func (am AppModule) OnChanOpenTry(
}

// OnChanOpenAck implements the IBCModule interface
func (am AppModule) OnChanOpenAck(
func (im IBCModule) OnChanOpenAck(
ctx sdk.Context,
portID,
channelID string,
Expand All @@ -100,7 +111,7 @@ func (am AppModule) OnChanOpenAck(
}

// OnChanOpenConfirm implements the IBCModule interface
func (am AppModule) OnChanOpenConfirm(
func (im IBCModule) OnChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
Expand All @@ -109,7 +120,7 @@ func (am AppModule) OnChanOpenConfirm(
}

// OnChanCloseInit implements the IBCModule interface
func (am AppModule) OnChanCloseInit(
func (im IBCModule) OnChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
Expand All @@ -119,7 +130,7 @@ func (am AppModule) OnChanCloseInit(
}

// OnChanCloseConfirm implements the IBCModule interface
func (am AppModule) OnChanCloseConfirm(
func (im IBCModule) OnChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
Expand All @@ -128,7 +139,7 @@ func (am AppModule) OnChanCloseConfirm(
}

// OnRecvPacket implements the IBCModule interface
func (am AppModule) OnRecvPacket(
func (im IBCModule) OnRecvPacket(
ctx sdk.Context,
modulePacket channeltypes.Packet,
relayer sdk.AccAddress,
Expand All @@ -155,7 +166,7 @@ func (am AppModule) OnRecvPacket(
}

// OnAcknowledgementPacket implements the IBCModule interface
func (am AppModule) OnAcknowledgementPacket(
func (im IBCModule) OnAcknowledgementPacket(
ctx sdk.Context,
modulePacket channeltypes.Packet,
acknowledgement []byte,
Expand Down Expand Up @@ -212,7 +223,7 @@ func (am AppModule) OnAcknowledgementPacket(
}

// OnTimeoutPacket implements the IBCModule interface
func (am AppModule) OnTimeoutPacket(
func (im IBCModule) OnTimeoutPacket(
ctx sdk.Context,
modulePacket channeltypes.Packet,
relayer sdk.AccAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
<%= if (isIBC) { %>_ porttypes.IBCModule = AppModule{}<% } %>
<%= if (isIBC) { %>_ porttypes.IBCModule = IBCModule{}<% } %>
)

// ----------------------------------------------------------------------------
Expand Down