/
packet.go
279 lines (247 loc) · 10.7 KB
/
packet.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
package keeper
import (
"encoding/json"
"fmt"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
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"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/Stride-Labs/stride/v13/x/icacallbacks"
icacallbacktypes "github.com/Stride-Labs/stride/v13/x/icacallbacks/types"
"github.com/Stride-Labs/stride/v13/x/ratelimit/types"
)
type RateLimitedPacketInfo struct {
ChannelID string
Denom string
Amount sdkmath.Int
Sender string
Receiver string
}
// Parse the denom from the Send Packet that will be used by the rate limit module
// The denom that the rate limiter will use for a SEND packet depends on whether
// it was a NATIVE token (e.g. ustrd, stuatom, etc.) or NON-NATIVE token (e.g. ibc/...)...
//
// We can identify if the token is native or not by parsing the trace denom from the packet
// If the token is NATIVE, it will not have a prefix (e.g. ustrd),
// and if it is NON-NATIVE, it will have a prefix (e.g. transfer/channel-2/uosmo)
//
// For NATIVE denoms, return as is (e.g. ustrd)
// For NON-NATIVE denoms, take the ibc hash (e.g. hash "transfer/channel-2/usoms" into "ibc/...")
func ParseDenomFromSendPacket(packet transfertypes.FungibleTokenPacketData) (denom string) {
// Determine the denom by looking at the denom trace path
denomTrace := transfertypes.ParseDenomTrace(packet.Denom)
// Native assets will have an empty trace path and can be returned as is
if denomTrace.Path == "" {
denom = packet.Denom
} else {
// Non-native assets should be hashed
denom = denomTrace.IBCDenom()
}
return denom
}
// Parse the denom from the Recv Packet that will be used by the rate limit module
// The denom that the rate limiter will use for a RECEIVE packet depends on whether it was a source or sink
//
// Sink: The token moves forward, to a chain different than its previous hop
// The new port and channel are APPENDED to the denom trace.
// (e.g. A -> B, B is a sink) (e.g. A -> B -> C, C is a sink)
//
// Source: The token moves backwards (i.e. revisits the last chain it was sent from)
// The port and channel are REMOVED from the denom trace - undoing the last hop.
// (e.g. A -> B -> A, A is a source) (e.g. A -> B -> C -> B, B is a source)
//
// If the chain is acting as a SINK: We add on the Stride port and channel and hash it
// Ex1: uosmo sent from Osmosis to Stride
// Packet Denom: uosmo
// -> Add Prefix: transfer/channel-X/uosmo
// -> Hash: ibc/...
//
// Ex2: ujuno sent from Osmosis to Stride
// PacketDenom: transfer/channel-Y/ujuno (channel-Y is the Juno <> Osmosis channel)
// -> Add Prefix: transfer/channel-X/transfer/channel-Y/ujuno
// -> Hash: ibc/...
//
// If the chain is acting as a SOURCE: First, remove the prefix. Then if there is still a denom trace, hash it
// Ex1: ustrd sent back to Stride from Osmosis
// Packet Denom: transfer/channel-X/ustrd
// -> Remove Prefix: ustrd
// -> Leave as is: ustrd
//
// Ex2: juno was sent to Stride, then to Osmosis, then back to Stride
// Packet Denom: transfer/channel-X/transfer/channel-Z/ujuno
// -> Remove Prefix: transfer/channel-Z/ujuno
// -> Hash: ibc/...
func ParseDenomFromRecvPacket(packet channeltypes.Packet, packetData transfertypes.FungibleTokenPacketData) (denom string) {
// To determine the denom, first check whether Stride is acting as source
if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), packetData.Denom) {
// Remove the source prefix (e.g. transfer/channel-X/transfer/channel-Z/ujuno -> transfer/channel-Z/ujuno)
sourcePrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel())
unprefixedDenom := packetData.Denom[len(sourcePrefix):]
// Native assets will have an empty trace path and can be returned as is
denomTrace := transfertypes.ParseDenomTrace(unprefixedDenom)
if denomTrace.Path == "" {
denom = unprefixedDenom
} else {
// Non-native assets should be hashed
denom = denomTrace.IBCDenom()
}
} else {
// Prefix the destination channel - this will contain the trailing slash (e.g. transfer/channel-X/)
destinationPrefix := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel())
prefixedDenom := destinationPrefix + packetData.Denom
// Hash the denom trace
denomTrace := transfertypes.ParseDenomTrace(prefixedDenom)
denom = denomTrace.IBCDenom()
}
return denom
}
// Parses the sender and channelId and denom for the corresponding RateLimit object, and
// the sender/receiver/transfer amount
//
// The Stride channelID should always be used as the key for the RateLimit object (not the counterparty channelID)
// For a SEND packet, the Stride channelID is the SOURCE channel
// For a RECEIVE packet, the Stride channelID is the DESTINATION channel
//
// The Source and Desination are defined from the perspective of a packet recipient
// Meaning, when a send packet lands on a the host chain, the "Source" will be the Stride Channel,
// and the "Destination" will be the Host Channel
// And, when a receive packet lands on a Stride, the "Source" will be the host zone's channel,
// and the "Destination" will be the Stride Channel
func ParsePacketInfo(packet channeltypes.Packet, direction types.PacketDirection) (RateLimitedPacketInfo, error) {
var packetData transfertypes.FungibleTokenPacketData
if err := json.Unmarshal(packet.GetData(), &packetData); err != nil {
return RateLimitedPacketInfo{}, err
}
var channelID, denom string
if direction == types.PACKET_SEND {
channelID = packet.GetSourceChannel()
denom = ParseDenomFromSendPacket(packetData)
} else {
channelID = packet.GetDestChannel()
denom = ParseDenomFromRecvPacket(packet, packetData)
}
amount, ok := sdk.NewIntFromString(packetData.Amount)
if !ok {
return RateLimitedPacketInfo{},
errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Unable to cast packet amount '%s' to sdkmath.Int", packetData.Amount)
}
packetInfo := RateLimitedPacketInfo{
ChannelID: channelID,
Denom: denom,
Amount: amount,
Sender: packetData.Sender,
Receiver: packetData.Receiver,
}
return packetInfo, nil
}
// Middleware implementation for SendPacket with rate limiting
// Checks whether the rate limit has been exceeded - and if it hasn't, sends the packet
func (k Keeper) SendRateLimitedPacket(ctx sdk.Context, packet channeltypes.Packet) error {
packetInfo, err := ParsePacketInfo(packet, types.PACKET_SEND)
if err != nil {
return err
}
// Check if the packet would exceed the outflow rate limit
updatedFlow, err := k.CheckRateLimitAndUpdateFlow(ctx, types.PACKET_SEND, packetInfo)
if err != nil {
return err
}
// Store the sequence number of the packet so that if the transfer fails,
// we can identify if it was sent during this quota and can revert the outflow
if updatedFlow {
k.SetPendingSendPacket(ctx, packetInfo.ChannelID, packet.Sequence)
}
return nil
}
// Middleware implementation for RecvPacket with rate limiting
// Checks whether the rate limit has been exceeded - and if it hasn't, allows the packet
func (k Keeper) ReceiveRateLimitedPacket(ctx sdk.Context, packet channeltypes.Packet) error {
packetInfo, err := ParsePacketInfo(packet, types.PACKET_RECV)
if err != nil {
return err
}
_, err = k.CheckRateLimitAndUpdateFlow(ctx, types.PACKET_RECV, packetInfo)
return err
}
// Middleware implementation for OnAckPacket with rate limiting
// If the packet failed, we should decrement the Outflow
func (k Keeper) AcknowledgeRateLimitedPacket(ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte) error {
// Check whether the ack was a success or error
isICA := false
ackResponse, err := icacallbacks.UnpackAcknowledgementResponse(ctx, k.Logger(ctx), acknowledgement, isICA)
if err != nil {
return err
}
// Parse the denom, channelId, and amount from the packet
packetInfo, err := ParsePacketInfo(packet, types.PACKET_SEND)
if err != nil {
return err
}
// If the ack was successful, remove the pending packet
if ackResponse.Status == icacallbacktypes.AckResponseStatus_SUCCESS {
k.RemovePendingSendPacket(ctx, packetInfo.ChannelID, packet.Sequence)
return nil
}
// If the ack failed, undo the change to the rate limit Outflow
return k.UndoSendPacket(ctx, packetInfo.ChannelID, packet.Sequence, packetInfo.Denom, packetInfo.Amount)
}
// Middleware implementation for OnAckPacket with rate limiting
// The Outflow should be decremented from the failed packet
func (k Keeper) TimeoutRateLimitedPacket(ctx sdk.Context, packet channeltypes.Packet) error {
packetInfo, err := ParsePacketInfo(packet, types.PACKET_SEND)
if err != nil {
return err
}
return k.UndoSendPacket(ctx, packetInfo.ChannelID, packet.Sequence, packetInfo.Denom, packetInfo.Amount)
}
// SendPacket wraps IBC ChannelKeeper's SendPacket function
// If the packet does not get rate limited, it passes the packet to the IBC Channel keeper
func (k Keeper) SendPacket(
ctx sdk.Context,
channelCap *capabilitytypes.Capability,
sourcePort string,
sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (sequence uint64, err error) {
// The packet must first be sent up the stack to get the sequence number from the channel keeper
sequence, err = k.ics4Wrapper.SendPacket(
ctx,
channelCap,
sourcePort,
sourceChannel,
timeoutHeight,
timeoutTimestamp,
data,
)
if err != nil {
return sequence, err
}
err = k.SendRateLimitedPacket(ctx, channeltypes.Packet{
Sequence: sequence,
SourceChannel: sourceChannel,
SourcePort: sourcePort,
TimeoutHeight: timeoutHeight,
TimeoutTimestamp: timeoutTimestamp,
Data: data,
})
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("ICS20 packet send was denied: %s", err.Error()))
return 0, err
}
return sequence, err
}
// WriteAcknowledgement wraps IBC ChannelKeeper's WriteAcknowledgement function
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)
}
// GetAppVersion wraps IBC ChannelKeeper's GetAppVersion function
func (k Keeper) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
return k.ics4Wrapper.GetAppVersion(ctx, portID, channelID)
}