-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibc_callbacks.go
270 lines (232 loc) · 9 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
package keeper
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
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/v3/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
"github.com/enronchain/enron/v3/ibc"
enron "github.com/enronchain/enron/v3/types"
"github.com/enronchain/enron/v3/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 Enron 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 {
return ack
}
// Get addresses in `enron1` and the original bech32 format
sender, recipient, senderBech32, recipientBech32, err := ibc.GetTransferSenderRecipient(packet)
if err != nil {
return channeltypes.NewErrorAcknowledgement(err.Error())
}
// return error ACK if the address is on the deny list
if k.bankKeeper.BlockedAddr(sender) || k.bankKeeper.BlockedAddr(recipient) {
return channeltypes.NewErrorAcknowledgement(
sdkerrors.Wrapf(
types.ErrBlockedAddress,
"sender (%s) or recipient (%s) address are in the deny list for sending and receiving transfers",
senderBech32, recipientBech32,
).Error(),
)
}
// Check if sender != recipient, as recovery is only possible for transfers to
// a sender's own account on Enron (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 && enron.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
err = k.transferKeeper.SendTransfer(
ctx,
packet.DestinationPort, // packet destination port is now the source
packet.DestinationChannel, // packet destination channel is now the source
coin, // balance of the coin
recipient, // recipient is the address in the Enron chain
senderBech32, // transfer to your own account address on the source chain
clienttypes.ZeroHeight(), // timeout height disabled
timeout, // timeout timestamp is 4 hours from now
)
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(
sdkerrors.Wrapf(
err,
"failed to recover IBC vouchers back to sender '%s' in the corresponding IBC chain", senderBech32,
).Error(),
)
}
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,
)
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 Enron 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 "", "", sdkerrors.Wrap(transfertypes.ErrInvalidDenomForTransfer, denom)
}
hash, err := transfertypes.ParseHexHash(ibcDenom[1])
if err != nil {
return "", "", sdkerrors.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 "", "", sdkerrors.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 "", "", sdkerrors.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 "", "", sdkerrors.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 "", "", sdkerrors.Wrapf(
host.ErrInvalidID,
"invalid port ID '%s': %s", destinationPort, err.Error(),
)
}
if err := host.ChannelIdentifierValidator(destinationChannel); err != nil {
// shouldn't occur
return "", "", sdkerrors.Wrapf(
channeltypes.ErrInvalidChannelIdentifier,
"channel ID '%s': %s", destinationChannel, err.Error(),
)
}
return destinationPort, destinationChannel, nil
}