-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibc_middleware.go
382 lines (336 loc) · 13.6 KB
/
ibc_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package packetforward
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/armon/go-metrics"
"github.com/sideprotocol/packet-forward-middleware/v7/packetforward/keeper"
"github.com/sideprotocol/packet-forward-middleware/v7/packetforward/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)
var _ porttypes.Middleware = &IBCMiddleware{}
// IBCMiddleware implements the ICS26 callbacks for the forward middleware given the
// forward keeper and the underlying application.
type IBCMiddleware struct {
app porttypes.IBCModule
keeper *keeper.Keeper
retriesOnTimeout uint8
forwardTimeout time.Duration
refundTimeout time.Duration
}
// NewIBCMiddleware creates a new IBCMiddleware given the keeper and underlying application.
func NewIBCMiddleware(
app porttypes.IBCModule,
k *keeper.Keeper,
retriesOnTimeout uint8,
forwardTimeout time.Duration,
refundTimeout time.Duration,
) IBCMiddleware {
return IBCMiddleware{
app: app,
keeper: k,
retriesOnTimeout: retriesOnTimeout,
forwardTimeout: forwardTimeout,
refundTimeout: refundTimeout,
}
}
// OnChanOpenInit implements the IBCModule interface.
func (im IBCMiddleware) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) (string, error) {
return im.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version)
}
// OnChanOpenTry implements the IBCModule interface.
func (im IBCMiddleware) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID, channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
counterpartyVersion string,
) (version string, err error) {
return im.app.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, counterpartyVersion)
}
// OnChanOpenAck implements the IBCModule interface.
func (im IBCMiddleware) OnChanOpenAck(
ctx sdk.Context,
portID, channelID string,
counterpartyChannelID string,
counterpartyVersion string,
) error {
return im.app.OnChanOpenAck(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion)
}
// OnChanOpenConfirm implements the IBCModule interface.
func (im IBCMiddleware) OnChanOpenConfirm(ctx sdk.Context, portID, channelID string) error {
return im.app.OnChanOpenConfirm(ctx, portID, channelID)
}
// OnChanCloseInit implements the IBCModule interface.
func (im IBCMiddleware) OnChanCloseInit(ctx sdk.Context, portID, channelID string) error {
return im.app.OnChanCloseInit(ctx, portID, channelID)
}
// OnChanCloseConfirm implements the IBCModule interface.
func (im IBCMiddleware) OnChanCloseConfirm(ctx sdk.Context, portID, channelID string) error {
return im.app.OnChanCloseConfirm(ctx, portID, channelID)
}
func getDenomForThisChain(port, channel, counterpartyPort, counterpartyChannel, denom string) string {
counterpartyPrefix := transfertypes.GetDenomPrefix(counterpartyPort, counterpartyChannel)
if strings.HasPrefix(denom, counterpartyPrefix) {
// unwind denom
unwoundDenom := denom[len(counterpartyPrefix):]
denomTrace := transfertypes.ParseDenomTrace(unwoundDenom)
if denomTrace.Path == "" {
// denom is now unwound back to native denom
return unwoundDenom
}
// denom is still IBC denom
return denomTrace.IBCDenom()
}
// append port and channel from this chain to denom
prefixedDenom := transfertypes.GetDenomPrefix(port, channel) + denom
return transfertypes.ParseDenomTrace(prefixedDenom).IBCDenom()
}
// getBoolFromAny returns the bool value is any is a valid bool, otherwise false.
func getBoolFromAny(value any) bool {
if value == nil {
return false
}
boolVal, ok := value.(bool)
if !ok {
return false
}
return boolVal
}
// GetReceiver returns the receiver address for a given channel and original sender.
// it overrides the receiver address to be a hash of the channel/origSender so that
// the receiver address is deterministic and can be used to identify the sender on the
// initial chain.
func GetReceiver(channel string, originalSender string) (string, error) {
senderStr := fmt.Sprintf("%s/%s", channel, originalSender)
senderHash32 := address.Hash(types.ModuleName, []byte(senderStr))
sender := sdk.AccAddress(senderHash32[:20])
bech32Prefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
return sdk.Bech32ifyAddressBytes(bech32Prefix, sender)
}
// newErrorAcknowledgement returns an error that identifies PFM and provides the error.
// It's okay if these errors are non-deterministic, because they will not be committed to state, only emitted as events.
func newErrorAcknowledgement(err error) channeltypes.Acknowledgement {
return channeltypes.Acknowledgement{
Response: &channeltypes.Acknowledgement_Error{
Error: fmt.Sprintf("packet-forward-middleware error: %s", err.Error()),
},
}
}
// OnRecvPacket checks the memo field on this packet and if the metadata inside's root key indicates this packet
// should be handled by the swap middleware it attempts to perform a swap. If the swap is successful
// the underlying application's OnRecvPacket callback is invoked, an ack error is returned otherwise.
func (im IBCMiddleware) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
//return channeltypes.NewResultAcknowledgement([]byte("{}"))
logger := im.keeper.Logger(ctx)
logger.Info("PacketForward:OnReceive: Received data", "error", packet.GetData())
var data types.WasmInterchainSwapPacketData
if err := json.Unmarshal(packet.GetData(), &data); err != nil {
logger.Error("PacketForward:OnReceive: Error forwarding packet", "error", err)
return im.app.OnRecvPacket(ctx, packet, relayer)
}
logger.Debug("PFM:OnReceive:",
"sequence", packet.Sequence,
"src-channel", packet.SourceChannel, "src-port", packet.SourcePort,
"dst-channel", packet.DestinationChannel, "dst-port", packet.DestinationPort,
"memo", data.Memo,
)
if strings.TrimSpace(data.Memo) == "" {
logger.Error("PacketForward:OnReceive:", "non existing memo")
return im.app.OnRecvPacket(ctx, packet, relayer)
}
memoBytes, err := base64.StdEncoding.DecodeString(string(data.Memo))
if err != nil {
// handle error: invalid base64 string
logger.Error("PacketForward:OnReceive:", "error", err)
return newErrorAcknowledgement(fmt.Errorf("error decoding memo from base64: %w", err))
}
if memoBytes == nil {
logger.Error("PacketForward:OnReceive:", "error", data)
return im.app.OnRecvPacket(ctx, packet, relayer)
}
m := &types.PacketMetadata{}
err = json.Unmarshal(memoBytes, m)
if err != nil {
logger.Error("PacketForward:OnReceive:", "error", err)
return newErrorAcknowledgement(fmt.Errorf("error parsing forward metadata: %w", err))
}
metadata := m.Forward
if err := metadata.Validate(); err != nil {
logger.Error("PacketForward:OnReceive:", "error", err)
return newErrorAcknowledgement(err)
}
timeout := time.Duration(metadata.Timeout)
if timeout.Nanoseconds() <= 0 {
timeout = im.forwardTimeout
}
var retries uint8
if metadata.Retries != nil {
retries = *metadata.Retries
} else {
retries = im.retriesOnTimeout
}
err = im.keeper.ForwardPacket(ctx, nil, packet, data, metadata, retries, timeout, []metrics.Label{})
if err != nil {
logger.Error("PacketForward:OnReceive: Error forwarding packet", "error", err)
return newErrorAcknowledgement(err)
}
// returning nil ack will prevent WriteAcknowledgement from occurring for forwarded packet.
// This is intentional so that the acknowledgement will be written later based on the ack/timeout of the forwarded packet.
return nil
}
// receiveFunds receives funds from the packet into the override receiver
// address and returns an error if the funds cannot be received.
func (im IBCMiddleware) receiveFunds(
ctx sdk.Context,
packet channeltypes.Packet,
data transfertypes.FungibleTokenPacketData,
overrideReceiver string,
relayer sdk.AccAddress,
) error {
overrideData := transfertypes.FungibleTokenPacketData{
Denom: data.Denom,
Amount: data.Amount,
Sender: data.Sender,
Receiver: overrideReceiver, // override receiver
// Memo explicitly zeroed
}
overrideDataBz := transfertypes.ModuleCdc.MustMarshalJSON(&overrideData)
overridePacket := channeltypes.Packet{
Sequence: packet.Sequence,
SourcePort: packet.SourcePort,
SourceChannel: packet.SourceChannel,
DestinationPort: packet.DestinationPort,
DestinationChannel: packet.DestinationChannel,
Data: overrideDataBz, // override data
TimeoutHeight: packet.TimeoutHeight,
TimeoutTimestamp: packet.TimeoutTimestamp,
}
ack := im.app.OnRecvPacket(ctx, overridePacket, relayer)
if ack == nil {
return fmt.Errorf("ack is nil")
}
if !ack.Success() {
return fmt.Errorf("ack error: %s", string(ack.Acknowledgement()))
}
return nil
}
// OnAcknowledgementPacket implements the IBCModule interface.
func (im IBCMiddleware) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
var data types.WasmInterchainSwapPacketData
if err := json.Unmarshal(packet.GetData(), &data); err != nil {
im.keeper.Logger(ctx).Error("packetForwardMiddleware error parsing packet data from ack packet",
"sequence", packet.Sequence,
"src-channel", packet.SourceChannel, "src-port", packet.SourcePort,
"dst-channel", packet.DestinationChannel, "dst-port", packet.DestinationPort,
"error", err,
)
return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
}
im.keeper.Logger(ctx).Debug("packetForwardMiddleware OnAcknowledgementPacket",
"sequence", packet.Sequence,
"src-channel", packet.SourceChannel, "src-port", packet.SourcePort,
"dst-channel", packet.DestinationChannel, "dst-port", packet.DestinationPort,
)
var ack channeltypes.Acknowledgement
if err := channeltypes.SubModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet acknowledgement: %v", err)
}
multihopsPacket := im.keeper.GetAndClearMultiHopsPacket(ctx, packet.SourceChannel, packet.SourcePort, packet.Sequence)
if multihopsPacket != nil {
// this is a forwarded packet, so override handling to avoid refund from being processed.
return im.keeper.WriteAcknowledgementForForwardedPacket(ctx, packet, data, multihopsPacket, ack)
}
return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
}
// OnTimeoutPacket implements the IBCModule interface.
func (im IBCMiddleware) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error {
var data types.WasmInterchainSwapPacketData
if err := json.Unmarshal(packet.GetData(), &data); err != nil {
im.keeper.Logger(ctx).Error("packetForwardMiddleware error parsing packet data from timeout packet",
"sequence", packet.Sequence,
"src-channel", packet.SourceChannel, "src-port", packet.SourcePort,
"dst-channel", packet.DestinationChannel, "dst-port", packet.DestinationPort,
"error", err,
)
return im.app.OnTimeoutPacket(ctx, packet, relayer)
}
im.keeper.Logger(ctx).Debug("packetForwardMiddleware OnAcknowledgementPacket",
"sequence", packet.Sequence,
"src-channel", packet.SourceChannel, "src-port", packet.SourcePort,
"dst-channel", packet.DestinationChannel, "dst-port", packet.DestinationPort,
)
multihopsPacket, err := im.keeper.TimeoutShouldRetry(ctx, packet)
if multihopsPacket != nil {
if err != nil {
im.keeper.RemoveMultiHopsPacket(ctx, packet)
// this is a forwarded packet, so override handling to avoid refund from being processed on this chain.
// WriteAcknowledgement with proxied ack to return success/fail to previous chain.
return im.keeper.WriteAcknowledgementForForwardedPacket(ctx, packet, data, multihopsPacket, newErrorAcknowledgement(err))
}
// timeout should be retried. In order to do that, we need to handle this timeout to refund on this chain first.
if err := im.app.OnTimeoutPacket(ctx, packet, relayer); err != nil {
return err
}
return im.keeper.RetryTimeout(ctx, packet.SourceChannel, packet.SourcePort, data, multihopsPacket)
}
return im.app.OnTimeoutPacket(ctx, packet, relayer)
}
// SendPacket implements the ICS4 Wrapper interface.
func (im IBCMiddleware) SendPacket(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
sourcePort string, sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (sequence uint64, err error) {
return im.keeper.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
// WriteAcknowledgement implements the ICS4 Wrapper interface.
func (im IBCMiddleware) WriteAcknowledgement(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
packet ibcexported.PacketI,
ack ibcexported.Acknowledgement,
) error {
return im.keeper.WriteAcknowledgement(ctx, chanCap, packet, ack)
}
func (im IBCMiddleware) GetAppVersion(
ctx sdk.Context,
portID,
channelID string,
) (string, bool) {
return im.keeper.GetAppVersion(ctx, portID, channelID)
}