Skip to content

Commit 92996ba

Browse files
seantkingmergify-bot
authored and
mergify-bot
committed
chore: fix mispelled words (#991)
(cherry picked from commit 556cc01) # Conflicts: # modules/apps/transfer/ibc_module.go # modules/core/04-channel/keeper/keeper.go
1 parent 66c9b4c commit 92996ba

File tree

2 files changed

+310
-0
lines changed

2 files changed

+310
-0
lines changed

modules/apps/transfer/ibc_module.go

+280
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
package transfer
2+
3+
import (
4+
"fmt"
5+
"math"
6+
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
9+
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
10+
11+
"github.com/cosmos/ibc-go/v3/modules/apps/transfer/keeper"
12+
"github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
13+
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
14+
porttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types"
15+
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
16+
ibcexported "github.com/cosmos/ibc-go/v3/modules/core/exported"
17+
)
18+
19+
// IBCModule implements the ICS26 interface for transfer given the transfer keeper.
20+
type IBCModule struct {
21+
keeper keeper.Keeper
22+
}
23+
24+
// NewIBCModule creates a new IBCModule given the keeper
25+
func NewIBCModule(k keeper.Keeper) IBCModule {
26+
return IBCModule{
27+
keeper: k,
28+
}
29+
}
30+
31+
// ValidateTransferChannelParams does validation of a newly created transfer channel. A transfer
32+
// channel must be UNORDERED, use the correct port (by default 'transfer'), and use the current
33+
// supported version. Only 2^32 channels are allowed to be created.
34+
func ValidateTransferChannelParams(
35+
ctx sdk.Context,
36+
keeper keeper.Keeper,
37+
order channeltypes.Order,
38+
portID string,
39+
channelID string,
40+
) error {
41+
// NOTE: for escrow address security only 2^32 channels are allowed to be created
42+
// Issue: https://github.com/cosmos/cosmos-sdk/issues/7737
43+
channelSequence, err := channeltypes.ParseChannelSequence(channelID)
44+
if err != nil {
45+
return err
46+
}
47+
if channelSequence > uint64(math.MaxUint32) {
48+
return sdkerrors.Wrapf(types.ErrMaxTransferChannels, "channel sequence %d is greater than max allowed transfer channels %d", channelSequence, uint64(math.MaxUint32))
49+
}
50+
if order != channeltypes.UNORDERED {
51+
return sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s ", channeltypes.UNORDERED, order)
52+
}
53+
54+
// Require portID is the portID transfer module is bound to
55+
boundPort := keeper.GetPort(ctx)
56+
if boundPort != portID {
57+
return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "invalid port: %s, expected %s", portID, boundPort)
58+
}
59+
60+
return nil
61+
}
62+
63+
// OnChanOpenInit implements the IBCModule interface
64+
func (im IBCModule) OnChanOpenInit(
65+
ctx sdk.Context,
66+
order channeltypes.Order,
67+
connectionHops []string,
68+
portID string,
69+
channelID string,
70+
chanCap *capabilitytypes.Capability,
71+
counterparty channeltypes.Counterparty,
72+
version string,
73+
) error {
74+
if err := ValidateTransferChannelParams(ctx, im.keeper, order, portID, channelID); err != nil {
75+
return err
76+
}
77+
78+
if version != types.Version {
79+
return sdkerrors.Wrapf(types.ErrInvalidVersion, "got %s, expected %s", version, types.Version)
80+
}
81+
82+
// Claim channel capability passed back by IBC module
83+
if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
84+
return err
85+
}
86+
87+
return nil
88+
}
89+
90+
// OnChanOpenTry implements the IBCModule interface.
91+
func (im IBCModule) OnChanOpenTry(
92+
ctx sdk.Context,
93+
order channeltypes.Order,
94+
connectionHops []string,
95+
portID,
96+
channelID string,
97+
chanCap *capabilitytypes.Capability,
98+
counterparty channeltypes.Counterparty,
99+
counterpartyVersion string,
100+
) (string, error) {
101+
if err := ValidateTransferChannelParams(ctx, im.keeper, order, portID, channelID); err != nil {
102+
return "", err
103+
}
104+
105+
if counterpartyVersion != types.Version {
106+
return "", sdkerrors.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: got: %s, expected %s", counterpartyVersion, types.Version)
107+
}
108+
109+
// Module may have already claimed capability in OnChanOpenInit in the case of crossing hellos
110+
// (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry)
111+
// If module can already authenticate the capability then module already owns it so we don't need to claim
112+
// Otherwise, module does not have channel capability and we must claim it from IBC
113+
if !im.keeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) {
114+
// Only claim channel capability passed back by IBC module if we do not already own it
115+
if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
116+
return "", err
117+
}
118+
}
119+
120+
return types.Version, nil
121+
}
122+
123+
// OnChanOpenAck implements the IBCModule interface
124+
func (im IBCModule) OnChanOpenAck(
125+
ctx sdk.Context,
126+
portID,
127+
channelID string,
128+
counterpartyVersion string,
129+
) error {
130+
if counterpartyVersion != types.Version {
131+
return sdkerrors.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: %s, expected %s", counterpartyVersion, types.Version)
132+
}
133+
return nil
134+
}
135+
136+
// OnChanOpenConfirm implements the IBCModule interface
137+
func (im IBCModule) OnChanOpenConfirm(
138+
ctx sdk.Context,
139+
portID,
140+
channelID string,
141+
) error {
142+
return nil
143+
}
144+
145+
// OnChanCloseInit implements the IBCModule interface
146+
func (im IBCModule) OnChanCloseInit(
147+
ctx sdk.Context,
148+
portID,
149+
channelID string,
150+
) error {
151+
// Disallow user-initiated channel closing for transfer channels
152+
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
153+
}
154+
155+
// OnChanCloseConfirm implements the IBCModule interface
156+
func (im IBCModule) OnChanCloseConfirm(
157+
ctx sdk.Context,
158+
portID,
159+
channelID string,
160+
) error {
161+
return nil
162+
}
163+
164+
// OnRecvPacket implements the IBCModule interface. A successful acknowledgement
165+
// is returned if the packet data is successfully decoded and the receive application
166+
// logic returns without error.
167+
func (im IBCModule) OnRecvPacket(
168+
ctx sdk.Context,
169+
packet channeltypes.Packet,
170+
relayer sdk.AccAddress,
171+
) ibcexported.Acknowledgement {
172+
ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)})
173+
174+
var data types.FungibleTokenPacketData
175+
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
176+
ack = channeltypes.NewErrorAcknowledgement("cannot unmarshal ICS-20 transfer packet data")
177+
}
178+
179+
// only attempt the application logic if the packet data
180+
// was successfully decoded
181+
if ack.Success() {
182+
err := im.keeper.OnRecvPacket(ctx, packet, data)
183+
if err != nil {
184+
ack = types.NewErrorAcknowledgement(err)
185+
}
186+
}
187+
188+
ctx.EventManager().EmitEvent(
189+
sdk.NewEvent(
190+
types.EventTypePacket,
191+
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
192+
sdk.NewAttribute(types.AttributeKeyReceiver, data.Receiver),
193+
sdk.NewAttribute(types.AttributeKeyDenom, data.Denom),
194+
sdk.NewAttribute(types.AttributeKeyAmount, data.Amount),
195+
sdk.NewAttribute(types.AttributeKeyAckSuccess, fmt.Sprintf("%t", ack.Success())),
196+
),
197+
)
198+
199+
// NOTE: acknowledgement will be written synchronously during IBC handler execution.
200+
return ack
201+
}
202+
203+
// OnAcknowledgementPacket implements the IBCModule interface
204+
func (im IBCModule) OnAcknowledgementPacket(
205+
ctx sdk.Context,
206+
packet channeltypes.Packet,
207+
acknowledgement []byte,
208+
relayer sdk.AccAddress,
209+
) error {
210+
var ack channeltypes.Acknowledgement
211+
if err := types.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil {
212+
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet acknowledgement: %v", err)
213+
}
214+
var data types.FungibleTokenPacketData
215+
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
216+
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet data: %s", err.Error())
217+
}
218+
219+
if err := im.keeper.OnAcknowledgementPacket(ctx, packet, data, ack); err != nil {
220+
return err
221+
}
222+
223+
ctx.EventManager().EmitEvent(
224+
sdk.NewEvent(
225+
types.EventTypePacket,
226+
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
227+
sdk.NewAttribute(types.AttributeKeyReceiver, data.Receiver),
228+
sdk.NewAttribute(types.AttributeKeyDenom, data.Denom),
229+
sdk.NewAttribute(types.AttributeKeyAmount, data.Amount),
230+
sdk.NewAttribute(types.AttributeKeyAck, ack.String()),
231+
),
232+
)
233+
234+
switch resp := ack.Response.(type) {
235+
case *channeltypes.Acknowledgement_Result:
236+
ctx.EventManager().EmitEvent(
237+
sdk.NewEvent(
238+
types.EventTypePacket,
239+
sdk.NewAttribute(types.AttributeKeyAckSuccess, string(resp.Result)),
240+
),
241+
)
242+
case *channeltypes.Acknowledgement_Error:
243+
ctx.EventManager().EmitEvent(
244+
sdk.NewEvent(
245+
types.EventTypePacket,
246+
sdk.NewAttribute(types.AttributeKeyAckError, resp.Error),
247+
),
248+
)
249+
}
250+
251+
return nil
252+
}
253+
254+
// OnTimeoutPacket implements the IBCModule interface
255+
func (im IBCModule) OnTimeoutPacket(
256+
ctx sdk.Context,
257+
packet channeltypes.Packet,
258+
relayer sdk.AccAddress,
259+
) error {
260+
var data types.FungibleTokenPacketData
261+
if err := types.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
262+
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet data: %s", err.Error())
263+
}
264+
// refund tokens
265+
if err := im.keeper.OnTimeoutPacket(ctx, packet, data); err != nil {
266+
return err
267+
}
268+
269+
ctx.EventManager().EmitEvent(
270+
sdk.NewEvent(
271+
types.EventTypeTimeout,
272+
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
273+
sdk.NewAttribute(types.AttributeKeyRefundReceiver, data.Sender),
274+
sdk.NewAttribute(types.AttributeKeyRefundDenom, data.Denom),
275+
sdk.NewAttribute(types.AttributeKeyRefundAmount, data.Amount),
276+
),
277+
)
278+
279+
return nil
280+
}

modules/core/04-channel/keeper/keeper.go

+30
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,36 @@ func (k Keeper) GetChannelClientState(ctx sdk.Context, portID, channelID string)
401401
return connection.ClientId, clientState, nil
402402
}
403403

404+
<<<<<<< HEAD
405+
=======
406+
// GetConnection wraps the connection keeper's GetConnection function.
407+
func (k Keeper) GetConnection(ctx sdk.Context, connectionID string) (exported.ConnectionI, error) {
408+
connection, found := k.connectionKeeper.GetConnection(ctx, connectionID)
409+
if !found {
410+
return nil, sdkerrors.Wrapf(connectiontypes.ErrConnectionNotFound, "connection-id: %s", connectionID)
411+
}
412+
413+
return connection, nil
414+
}
415+
416+
// GetChannelConnection returns the connection ID and state associated with the given port and channel identifier.
417+
func (k Keeper) GetChannelConnection(ctx sdk.Context, portID, channelID string) (string, exported.ConnectionI, error) {
418+
channel, found := k.GetChannel(ctx, portID, channelID)
419+
if !found {
420+
return "", nil, sdkerrors.Wrapf(types.ErrChannelNotFound, "port-id: %s, channel-id: %s", portID, channelID)
421+
}
422+
423+
connectionID := channel.ConnectionHops[0]
424+
425+
connection, found := k.connectionKeeper.GetConnection(ctx, connectionID)
426+
if !found {
427+
return "", nil, sdkerrors.Wrapf(connectiontypes.ErrConnectionNotFound, "connection-id: %s", connectionID)
428+
}
429+
430+
return connectionID, connection, nil
431+
}
432+
433+
>>>>>>> 556cc01 (chore: fix mispelled words (#991))
404434
// LookupModuleByChannel will return the IBCModule along with the capability associated with a given channel defined by its portID and channelID
405435
func (k Keeper) LookupModuleByChannel(ctx sdk.Context, portID, channelID string) (string, *capabilitytypes.Capability, error) {
406436
modules, cap, err := k.scopedKeeper.LookupModules(ctx, host.ChannelCapabilityPath(portID, channelID))

0 commit comments

Comments
 (0)