-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibc_callbacks.go
308 lines (267 loc) · 10.5 KB
/
ibc_callbacks.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
// Copyright 2022 Serv Foundation
// This file is part of the Serv Network packages.
//
// Serv is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Serv packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Serv packages. If not, see https://github.com/twobitedd/serv/blob/main/LICENSE
package keeper
import (
"strings"
errorsmod "cosmossdk.io/errors"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v6/modules/core/24-host"
"github.com/cosmos/ibc-go/v6/modules/core/exported"
"github.com/twobitedd/serv/v12/ibc"
"github.com/twobitedd/serv/v12/utils"
"github.com/twobitedd/serv/v12/x/recovery/types"
)
// OnRecvPacket performs an IBC receive callback. It returns the tokens that
// users transferred to their Cosmos secp256k1 address instead of the Ethereum
// ethsecp256k1 address. The expected behavior is as follows:
//
// First transfer from authorized source chain:
// - sends back IBC tokens which originated from the source chain
// - sends over all Serv native tokens
//
// Second transfer from a different authorized source chain:
// - only sends back IBC tokens which originated from the source chain
func (k Keeper) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
ack exported.Acknowledgement,
) exported.Acknowledgement {
logger := k.Logger(ctx)
params := k.GetParams(ctx)
claimsParams := k.claimsKeeper.GetParams(ctx)
// Check and return original ACK if:
// - recovery is disabled globally
// - channel is not authorized
// - channel is an EVM channel
if !params.EnableRecovery ||
!claimsParams.IsAuthorizedChannel(packet.DestinationChannel) ||
claimsParams.IsEVMChannel(packet.DestinationChannel) {
return ack
}
// Get addresses in `sx1` and the original bech32 format
sender, recipient, senderBech32, recipientBech32, err := ibc.GetTransferSenderRecipient(packet)
if err != nil {
return channeltypes.NewErrorAcknowledgement(err)
}
// return error ACK if the address is on the deny list
if k.bankKeeper.BlockedAddr(sender) || k.bankKeeper.BlockedAddr(recipient) {
return channeltypes.NewErrorAcknowledgement(
errorsmod.Wrapf(
types.ErrBlockedAddress,
"sender (%s) or recipient (%s) address are in the deny list for sending and receiving transfers",
senderBech32, recipientBech32,
),
)
}
// Check if sender != recipient, as recovery is only possible for transfers to
// a sender's own account on Serv (sender == recipient)
if !sender.Equals(recipient) {
// Continue to the next IBC middleware by returning the original ACK.
return ack
}
// get the recipient/sender account
account := k.accountKeeper.GetAccount(ctx, recipient)
// recovery is not supported for vesting or module accounts
if _, isVestingAcc := account.(vestexported.VestingAccount); isVestingAcc {
return ack
}
if _, isModuleAccount := account.(authtypes.ModuleAccountI); isModuleAccount {
return ack
}
// Check if recipient pubkey is a supported key (eth_secp256k1, amino multisig,
// ed25519). Continue and return success ACK as the funds are not stuck on
// chain for supported keys
if account != nil && utils.IsSupportedKey(account.GetPubKey()) {
return ack
}
// Perform recovery to transfer the balance back to the sender bech32 address.
// NOTE: Since destination channel is authorized and not from an EVM chain, we
// know that only secp256k1 keys are supported in the source chain.
var destPort, destChannel string
balances := sdk.Coins{}
// iterate over all tokens owned by the address (i.e recipient balance) and
// transfer them to the original sender address in the source chain (if
// applicable, see cases for IBC vouchers below).
k.bankKeeper.IterateAccountBalances(ctx, recipient, func(coin sdk.Coin) (stop bool) {
if coin.IsZero() {
// safety check: continue
return false
}
if strings.HasPrefix(coin.Denom, "ibc/") {
// IBC vouchers, obtain the destination port and channel from the denom path
destPort, destChannel, err = k.GetIBCDenomDestinationIdentifiers(ctx, coin.Denom, senderBech32)
if err != nil {
logger.Error(
"failed to get the IBC full denom path of source chain",
"error", err.Error(),
)
return true // stop iteration
}
// NOTE: only recover the IBC tokens from the source chain connected
// through our authorized destination channel
if packet.DestinationPort != destPort || packet.DestinationChannel != destChannel {
// continue
return false
}
}
// NOTE: Don't use the consensus state because it may become unreliable if updates slow down
timeout := uint64(ctx.BlockTime().Add(params.PacketTimeoutDuration).UnixNano())
// Recover the tokens to the bech32 prefixed address of the source chain
packetTransfer := &transfertypes.MsgTransfer{
SourcePort: packet.DestinationPort, // packet destination port is now the source
SourceChannel: packet.DestinationChannel, // packet destination channel is now the source
Token: coin, // balance of the coin
Sender: recipient.String(), // recipient is the address in the Serv chain
Receiver: senderBech32, // transfer to your own account address on the source chain
TimeoutHeight: clienttypes.ZeroHeight(), // timeout height disabled
TimeoutTimestamp: timeout, // timeout timestamp is 4 hours from now
Memo: "",
}
_, err = k.transferKeeper.Transfer(sdk.WrapSDKContext(ctx), packetTransfer)
if err != nil {
return true // stop iteration
}
balances = balances.Add(coin)
return false
})
// check error from the iteration above
if err != nil {
logger.Error(
"failed to recover IBC vouchers",
"sender", senderBech32,
"receiver", recipientBech32,
"source-port", packet.SourcePort,
"source-channel", packet.SourceChannel,
"error", err.Error(),
)
return channeltypes.NewErrorAcknowledgement(
errorsmod.Wrapf(
err,
"failed to recover IBC vouchers back to sender '%s' in the corresponding IBC chain", senderBech32,
),
)
}
if balances.IsZero() {
// short circuit in case the user doesn't have any balance
return ack
}
amtStr := balances.String()
logger.Info(
"balances recovered to sender address",
"sender", senderBech32,
"receiver", recipientBech32,
"amount", amtStr,
"source-port", packet.SourcePort,
"source-channel", packet.SourceChannel,
"dest-port", packet.DestinationPort,
"dest-channel", packet.DestinationChannel,
)
defer func() {
telemetry.IncrCounter(1, types.ModuleName, "ibc", "on_recv", "total")
for _, b := range balances {
if b.Amount.IsInt64() {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, "ibc", "on_recv", "token", "total"},
float32(b.Amount.Int64()),
[]metrics.Label{
telemetry.NewLabel("denom", b.Denom),
},
)
}
}
}()
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRecovery,
sdk.NewAttribute(sdk.AttributeKeySender, senderBech32),
sdk.NewAttribute(transfertypes.AttributeKeyReceiver, recipientBech32),
sdk.NewAttribute(sdk.AttributeKeyAmount, amtStr),
sdk.NewAttribute(channeltypes.AttributeKeySrcChannel, packet.SourceChannel),
sdk.NewAttribute(channeltypes.AttributeKeySrcPort, packet.SourcePort),
sdk.NewAttribute(channeltypes.AttributeKeyDstPort, packet.DestinationPort),
sdk.NewAttribute(channeltypes.AttributeKeyDstChannel, packet.DestinationChannel),
),
)
// return original acknowledgement
return ack
}
// GetIBCDenomDestinationIdentifiers returns the destination port and channel of
// the IBC denomination, i.e port and channel on Serv for the voucher. It
// returns an error if:
// - the denomination is invalid
// - the denom trace is not found on the store
// - destination port or channel ID are invalid
func (k Keeper) GetIBCDenomDestinationIdentifiers(ctx sdk.Context, denom, sender string) (destinationPort, destinationChannel string, err error) {
ibcDenom := strings.SplitN(denom, "/", 2)
if len(ibcDenom) < 2 {
return "", "", errorsmod.Wrap(transfertypes.ErrInvalidDenomForTransfer, denom)
}
hash, err := transfertypes.ParseHexHash(ibcDenom[1])
if err != nil {
return "", "", errorsmod.Wrapf(
err,
"failed to recover IBC vouchers back to sender '%s' in the corresponding IBC chain", sender,
)
}
denomTrace, found := k.transferKeeper.GetDenomTrace(ctx, hash)
if !found {
return "", "", errorsmod.Wrapf(
transfertypes.ErrTraceNotFound,
"failed to recover IBC vouchers back to sender '%s' in the corresponding IBC chain", sender,
)
}
path := strings.Split(denomTrace.Path, "/")
if len(path)%2 != 0 {
// safety check: shouldn't occur
return "", "", errorsmod.Wrapf(
transfertypes.ErrInvalidDenomForTransfer,
"invalid denom (%s) trace path %s", denomTrace.BaseDenom, denomTrace.Path,
)
}
destinationPort = path[0]
destinationChannel = path[1]
_, found = k.channelKeeper.GetChannel(ctx, destinationPort, destinationChannel)
if !found {
return "", "", errorsmod.Wrapf(
channeltypes.ErrChannelNotFound,
"port ID %s, channel ID %s", destinationPort, destinationChannel,
)
}
// NOTE: optimistic handshakes could cause unforeseen issues.
// Safety check: verify that the destination port and channel are valid
if err := host.PortIdentifierValidator(destinationPort); err != nil {
// shouldn't occur
return "", "", errorsmod.Wrapf(
host.ErrInvalidID,
"invalid port ID '%s': %s", destinationPort, err.Error(),
)
}
if err := host.ChannelIdentifierValidator(destinationChannel); err != nil {
// shouldn't occur
return "", "", errorsmod.Wrapf(
channeltypes.ErrInvalidChannelIdentifier,
"channel ID '%s': %s", destinationChannel, err.Error(),
)
}
return destinationPort, destinationChannel, nil
}