-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeeper.go
356 lines (308 loc) · 11.9 KB
/
keeper.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
package keeper
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/armon/go-metrics"
"github.com/sideprotocol/packet-forward-middleware/v7/packetforward/types"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
"github.com/cometbft/cometbft/libs/log"
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 (
// DefaultTransferPacketTimeoutHeight is the timeout height following IBC defaults
DefaultTransferPacketTimeoutHeight = clienttypes.Height{
RevisionNumber: 0,
RevisionHeight: 0,
}
// DefaultForwardTransferPacketTimeoutTimestamp is the timeout timestamp following IBC defaults
DefaultForwardTransferPacketTimeoutTimestamp = time.Duration(transfertypes.DefaultRelativePacketTimeoutTimestamp) * time.Nanosecond
// DefaultRefundTransferPacketTimeoutTimestamp is a 28-day timeout for refund packets since funds are stuck in packetforward module otherwise.
DefaultRefundTransferPacketTimeoutTimestamp = 28 * 24 * time.Hour
)
// Keeper defines the packet forward middleware keeper
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
transferKeeper types.TransferKeeper
channelKeeper types.ChannelKeeper
distrKeeper types.DistributionKeeper
bankKeeper types.BankKeeper
ics4Wrapper porttypes.ICS4Wrapper
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
}
// NewKeeper creates a new forward Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
transferKeeper types.TransferKeeper,
channelKeeper types.ChannelKeeper,
distrKeeper types.DistributionKeeper,
bankKeeper types.BankKeeper,
ics4Wrapper porttypes.ICS4Wrapper,
authority string,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: key,
transferKeeper: transferKeeper,
channelKeeper: channelKeeper,
distrKeeper: distrKeeper,
bankKeeper: bankKeeper,
ics4Wrapper: ics4Wrapper,
authority: authority,
}
}
// GetAuthority returns the module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}
// SetTransferKeeper sets the transferKeeper
func (k *Keeper) SetTransferKeeper(transferKeeper types.TransferKeeper) {
k.transferKeeper = transferKeeper
}
// Logger returns a module-specific logger.
func (k *Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+ibcexported.ModuleName+"-"+types.ModuleName)
}
func (k *Keeper) WriteAcknowledgementForForwardedPacket(
ctx sdk.Context,
packet channeltypes.Packet,
data types.WasmInterchainSwapPacketData,
multihopsPacket *types.MultiHopsPacket,
ack channeltypes.Acknowledgement,
) error {
// Lookup module by channel capability
_, chanCap, err := k.channelKeeper.LookupModuleByChannel(ctx, multihopsPacket.PacketSrcPortId, multihopsPacket.PacketSrcChannelId)
if err != nil {
return errorsmod.Wrap(err, "could not retrieve module from port-id")
}
// for forwarded packets, the funds were moved into an escrow account if the denom originated on this chain.
// On an ack error or timeout on a forwarded packet, the funds in the escrow account
// should be moved to the other escrow account on the other side or burned.
if !ack.Success() {
ackResult := fmt.Sprintf("packet forward failed after point of no return: %s", ack.GetError())
newAck := channeltypes.NewResultAcknowledgement([]byte(ackResult))
return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, channeltypes.Packet{
Data: multihopsPacket.PacketData,
Sequence: multihopsPacket.PacketSequence,
SourcePort: multihopsPacket.PacketSrcPortId,
SourceChannel: multihopsPacket.PacketSrcChannelId,
DestinationPort: multihopsPacket.PacketDestPortId,
DestinationChannel: multihopsPacket.PacketDestChannelId,
TimeoutHeight: clienttypes.MustParseHeight(multihopsPacket.PacketTimeoutHeight),
TimeoutTimestamp: multihopsPacket.PacketTimeoutTimestamp,
}, newAck)
}
return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, channeltypes.Packet{
Data: multihopsPacket.PacketData,
Sequence: multihopsPacket.PacketSequence,
SourcePort: multihopsPacket.PacketSrcPortId,
SourceChannel: multihopsPacket.PacketSrcChannelId,
DestinationPort: multihopsPacket.PacketDestPortId,
DestinationChannel: multihopsPacket.PacketDestChannelId,
TimeoutHeight: clienttypes.MustParseHeight(multihopsPacket.PacketTimeoutHeight),
TimeoutTimestamp: multihopsPacket.PacketTimeoutTimestamp,
}, ack)
}
func (k *Keeper) ForwardPacket(
ctx sdk.Context,
multihopsPacket *types.MultiHopsPacket,
srcPacket channeltypes.Packet,
data types.WasmInterchainSwapPacketData,
metadata *types.ForwardMetadata,
maxRetries uint8,
timeout time.Duration,
labels []metrics.Label,
) error {
var err error
memo := []byte("")
// set memo for next transfer with next from this transfer.
if metadata.Next != nil {
memoBz, err := json.Marshal(metadata.Next)
if err != nil {
k.Logger(ctx).Error("packetForwardMiddleware error marshaling next as JSON",
"error", err,
)
return errorsmod.Wrapf(sdkerrors.ErrJSONMarshal, err.Error())
}
memo = memoBz
}
_, chanCap, err := k.channelKeeper.LookupModuleByChannel(ctx, metadata.Port, metadata.Channel)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrJSONMarshal, err.Error())
}
// Send the packet using the channel keeper
seq, err := k.SendPacket(ctx, chanCap, metadata.Port, metadata.Channel, DefaultTransferPacketTimeoutHeight, uint64(ctx.BlockTime().UnixNano())+uint64(timeout.Nanoseconds()), srcPacket.Data)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrJSONMarshal, err.Error())
}
// Update memo
data.Memo = string(memo)
newData, err := json.Marshal(data)
srcPacket.Data = newData
k.Logger(ctx).Debug("packetForwardMiddleware ForwardTransferPacket",
"port", metadata.Port, "channel", metadata.Channel,
)
if err != nil {
k.Logger(ctx).Error("packetForwardMiddleware ForwardTransferPacket error",
"port", metadata.Port, "channel", metadata.Channel,
"error", err,
)
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
// Store the following information in keeper:
// key - information about forwarded packet: src_channel (parsedReceiver.Channel), src_port (parsedReceiver.Port), sequence
// value - information about original packet for refunding if necessary: retries, srcPacketSender, srcPacket.DestinationChannel, srcPacket.DestinationPort
if multihopsPacket == nil {
multihopsPacket = &types.MultiHopsPacket{
PacketData: srcPacket.Data,
PacketSrcPortId: srcPacket.SourcePort,
PacketSrcChannelId: srcPacket.SourceChannel,
PacketTimeoutTimestamp: srcPacket.TimeoutTimestamp,
PacketTimeoutHeight: srcPacket.TimeoutHeight.String(),
RetriesRemaining: int32(maxRetries),
Timeout: uint64(timeout.Nanoseconds()),
}
} else {
multihopsPacket.RetriesRemaining--
}
key := types.RefundPacketKey(metadata.Channel, metadata.Port, seq)
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(multihopsPacket)
store.Set(key, bz)
defer func() {
telemetry.IncrCounterWithLabels(
[]string{"ibc", types.ModuleName, "send"},
1,
labels,
)
}()
return nil
}
// TimeoutShouldRetry returns inFlightPacket and no error if retry should be attempted. Error is returned if IBC refund should occur.
func (k *Keeper) TimeoutShouldRetry(
ctx sdk.Context,
packet channeltypes.Packet,
) (*types.MultiHopsPacket, error) {
store := ctx.KVStore(k.storeKey)
key := types.RefundPacketKey(packet.SourceChannel, packet.SourcePort, packet.Sequence)
if !store.Has(key) {
// not a forwarded packet, ignore.
return nil, nil
}
bz := store.Get(key)
var multihopsPacket types.MultiHopsPacket
k.cdc.MustUnmarshal(bz, &multihopsPacket)
if multihopsPacket.RetriesRemaining <= 0 {
k.Logger(ctx).Error("packetForwardMiddleware reached max retries for packet",
"key", string(key),
"original-sender-address", multihopsPacket.OriginalSenderAddress,
"dest-channel-id", multihopsPacket.PacketDestChannelId,
"dest-port-id", multihopsPacket.PacketDestPortId,
)
return &multihopsPacket, fmt.Errorf("giving up on packet on channel (%s) port (%s) after max retries",
multihopsPacket.PacketDestChannelId, multihopsPacket.PacketDestPortId)
}
return &multihopsPacket, nil
}
func (k *Keeper) RetryTimeout(
ctx sdk.Context,
channel, port string,
data types.WasmInterchainSwapPacketData,
multihopsPacket *types.MultiHopsPacket,
) error {
// send transfer again
metadata := &types.ForwardMetadata{
Channel: channel,
Port: port,
}
if strings.TrimSpace(data.Memo) != "" {
metadata.Next = &types.JSONObject{}
if err := json.Unmarshal([]byte(data.Memo), metadata.Next); err != nil {
return fmt.Errorf("error unmarshaling memo json: %w", err)
}
}
// srcPacket and srcPacketSender are empty because inFlightPacket is non-nil.
return k.ForwardPacket(
ctx,
multihopsPacket,
channeltypes.Packet{},
data,
metadata,
uint8(multihopsPacket.RetriesRemaining),
time.Duration(multihopsPacket.Timeout)*time.Nanosecond,
[]metrics.Label{},
)
}
func (k *Keeper) RemoveMultiHopsPacket(ctx sdk.Context, packet channeltypes.Packet) {
store := ctx.KVStore(k.storeKey)
key := types.RefundPacketKey(packet.SourceChannel, packet.SourcePort, packet.Sequence)
if !store.Has(key) {
// not a forwarded packet, ignore.
return
}
// done with packet key now, delete.
store.Delete(key)
}
// GetAndClearInFlightPacket will fetch an InFlightPacket from the store, remove it if it exists, and return it.
func (k *Keeper) GetAndClearMultiHopsPacket(
ctx sdk.Context,
channel string,
port string,
sequence uint64,
) *types.MultiHopsPacket {
store := ctx.KVStore(k.storeKey)
key := types.RefundPacketKey(channel, port, sequence)
if !store.Has(key) {
// this is either not a forwarded packet, or it is the final destination for the refund.
return nil
}
bz := store.Get(key)
// done with packet key now, delete.
store.Delete(key)
var multihopsPacket types.MultiHopsPacket
k.cdc.MustUnmarshal(bz, &multihopsPacket)
return &multihopsPacket
}
// SendPacket wraps IBC ChannelKeeper's SendPacket function
func (k Keeper) SendPacket(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
sourcePort string, sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (sequence uint64, err error) {
return k.ics4Wrapper.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
// WriteAcknowledgement wraps IBC ICS4Wrapper WriteAcknowledgement function.
// ICS29 WriteAcknowledgement is used for asynchronous acknowledgements.
func (k *Keeper) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, acknowledgement ibcexported.Acknowledgement) error {
return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, packet, acknowledgement)
}
// WriteAcknowledgement wraps IBC ICS4Wrapper GetAppVersion function.
func (k *Keeper) GetAppVersion(
ctx sdk.Context,
portID,
channelID string,
) (string, bool) {
return k.ics4Wrapper.GetAppVersion(ctx, portID, channelID)
}
// LookupModuleByChannel wraps ChannelKeeper LookupModuleByChannel function.
func (k *Keeper) LookupModuleByChannel(ctx sdk.Context, portID, channelID string) (string, *capabilitytypes.Capability, error) {
return k.channelKeeper.LookupModuleByChannel(ctx, portID, channelID)
}