/
anycallswap.go
303 lines (259 loc) · 9.3 KB
/
anycallswap.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
package tron
import (
"bytes"
"errors"
"math/big"
"strings"
"github.com/anyswap/CrossChain-Router/v3/common"
"github.com/anyswap/CrossChain-Router/v3/common/hexutil"
"github.com/anyswap/CrossChain-Router/v3/log"
"github.com/anyswap/CrossChain-Router/v3/params"
"github.com/anyswap/CrossChain-Router/v3/router"
"github.com/anyswap/CrossChain-Router/v3/tokens"
"github.com/anyswap/CrossChain-Router/v3/tokens/eth/abicoder"
"github.com/anyswap/CrossChain-Router/v3/types"
)
// anycall lot topics and func hashes
var (
// v5 LogAnyCall(address,address,bytes,address,uint256)
LogAnyCallV5Topic = common.FromHex("0x9ca1de98ebed0a9c38ace93d3ca529edacbbe199cf1b6f0f416ae9b724d4a81c")
AnyExecV5FuncHash = common.FromHex("0xb4c5dbd0")
// v6 LogAnyCall(address,address,bytes,address,uint256,uint256,string,uint256)
LogAnyCallV6Topic = common.FromHex("0xa17aef042e1a5dd2b8e68f0d0d92f9a6a0b35dc25be1d12c0cb3135bfd8951c9")
AnyExecV6FuncHash = common.FromHex("0x4a578150")
defMinReserveBudget = big.NewInt(1e16)
)
func getAnyCallLogTopic() ([]byte, error) {
switch params.GetSwapSubType() {
case tokens.AnycallSubTypeV5, tokens.CurveAnycallSubType:
return LogAnyCallV5Topic, nil
case tokens.AnycallSubTypeV6:
return LogAnyCallV6Topic, nil
default:
return nil, tokens.ErrUnknownSwapSubType
}
}
func getAnyExecFuncHash() ([]byte, error) {
switch params.GetSwapSubType() {
case tokens.AnycallSubTypeV5, tokens.CurveAnycallSubType:
return AnyExecV5FuncHash, nil
case tokens.AnycallSubTypeV6:
return AnyExecV6FuncHash, nil
default:
return nil, tokens.ErrUnknownSwapSubType
}
}
func (b *Bridge) registerAnyCallSwapTx(txHash string, logIndex int) ([]*tokens.SwapTxInfo, []error) {
commonInfo := &tokens.SwapTxInfo{}
commonInfo.SwapType = tokens.AnyCallSwapType // SwapType
commonInfo.Hash = strings.ToLower(txHash) // Hash
commonInfo.LogIndex = logIndex // LogIndex
commonInfo.FromChainID = b.ChainConfig.GetChainID() // FromChainID
var logs []*types.RPCLog
var err error
logs, err = b.GetTransactionLog(commonInfo.Hash)
if err != nil {
return []*tokens.SwapTxInfo{commonInfo}, []error{err}
}
swapInfos := make([]*tokens.SwapTxInfo, 0)
errs := make([]error, 0)
startIndex, endIndex := 0, len(logs)
if logIndex != 0 {
if logIndex >= endIndex || logIndex < 0 {
return []*tokens.SwapTxInfo{commonInfo}, []error{tokens.ErrLogIndexOutOfRange}
}
startIndex = logIndex
endIndex = logIndex + 1
}
for i := startIndex; i < endIndex; i++ {
swapInfo := &tokens.SwapTxInfo{}
*swapInfo = *commonInfo
swapInfo.LogIndex = i // LogIndex
err := b.verifyAnyCallSwapTxLog(swapInfo, logs[i])
switch {
case errors.Is(err, tokens.ErrSwapoutLogNotFound),
errors.Is(err, tokens.ErrTxWithWrongTopics),
errors.Is(err, tokens.ErrTxWithWrongContract):
continue
case err == nil:
err = b.checkAnyCallSwapInfo(swapInfo)
default:
log.Debug(b.ChainConfig.BlockChain+" register anycall swap error", "txHash", txHash, "logIndex", swapInfo.LogIndex, "err", err)
}
swapInfos = append(swapInfos, swapInfo)
errs = append(errs, err)
}
if len(swapInfos) == 0 {
return []*tokens.SwapTxInfo{commonInfo}, []error{tokens.ErrSwapoutLogNotFound}
}
return swapInfos, errs
}
func (b *Bridge) verifyAnyCallSwapTx(txHash string, logIndex int, allowUnstable bool) (*tokens.SwapTxInfo, error) {
swapInfo := &tokens.SwapTxInfo{}
swapInfo.SwapType = tokens.AnyCallSwapType // SwapType
swapInfo.Hash = strings.ToLower(txHash) // Hash
swapInfo.LogIndex = logIndex // LogIndex
swapInfo.FromChainID = b.ChainConfig.GetChainID() // FromChainID
err := b.checkTxSuccess(swapInfo, allowUnstable)
if err != nil {
return swapInfo, err
}
var logs []*types.RPCLog
logs, err = b.GetTransactionLog(swapInfo.Hash)
if err != nil {
return swapInfo, err
}
if logIndex >= len(logs) {
return swapInfo, tokens.ErrLogIndexOutOfRange
}
err = b.verifyAnyCallSwapTxLog(swapInfo, logs[logIndex])
if err != nil {
return swapInfo, err
}
err = b.checkAnyCallSwapInfo(swapInfo)
if err != nil {
return swapInfo, err
}
if !allowUnstable {
log.Info("verify anycall swap tx stable pass", "identifier", params.GetIdentifier(),
"from", swapInfo.From, "to", swapInfo.To, "txid", txHash, "logIndex", logIndex,
"height", swapInfo.Height, "timestamp", swapInfo.Timestamp,
"fromChainID", swapInfo.FromChainID, "toChainID", swapInfo.ToChainID,
"callFrom", getCallFrom(swapInfo))
}
return swapInfo, nil
}
func getCallFrom(swapInfo *tokens.SwapTxInfo) string {
return swapInfo.AnyCallSwapInfo.CallFrom
}
func (b *Bridge) verifyAnyCallSwapTxLog(swapInfo *tokens.SwapTxInfo, rlog *types.RPCLog) (err error) {
swapInfo.To = rlog.Address.LowerHex() // To
err = b.parseAnyCallSwapTxLog(swapInfo, rlog)
if err != nil {
log.Info(b.ChainConfig.BlockChain+" b.verifyAnyCallSwapTxLog fail", "tx", swapInfo.Hash, "logIndex", swapInfo.LogIndex, "err", err)
return err
}
if rlog.Removed != nil && *rlog.Removed {
return tokens.ErrTxWithRemovedLog
}
routerContract := b.GetRouterContract("")
if !common.IsEqualIgnoreCase(rlog.Address.LowerHex(), routerContract) {
log.Warn("tx to address mismatch", "have", rlog.Address.LowerHex(), "want", routerContract, "chainID", b.ChainConfig.ChainID, "txid", swapInfo.Hash, "logIndex", swapInfo.LogIndex, "err", tokens.ErrTxWithWrongContract)
return tokens.ErrTxWithWrongContract
}
return nil
}
func (b *Bridge) parseAnyCallSwapTxLog(swapInfo *tokens.SwapTxInfo, rlog *types.RPCLog) (err error) {
logTopics := rlog.Topics
if len(logTopics) != 4 {
return tokens.ErrTxWithWrongTopics
}
anycallLogTopic, err := getAnyCallLogTopic()
if err != nil {
return err
}
logTopic := rlog.Topics[0].Bytes()
if !bytes.Equal(logTopic, anycallLogTopic) {
return tokens.ErrSwapoutLogNotFound
}
logData := *rlog.Data
if len(logData) < 96 {
return abicoder.ErrParseDataError
}
swapInfo.SwapInfo = tokens.SwapInfo{AnyCallSwapInfo: &tokens.AnyCallSwapInfo{}}
anycallSwapInfo := swapInfo.AnyCallSwapInfo
anycallSwapInfo.CallFrom = common.BytesToAddress(logTopics[1].Bytes()).LowerHex()
anycallSwapInfo.CallTo = common.BytesToAddress(logTopics[2].Bytes()).LowerHex()
swapInfo.ToChainID = new(big.Int).SetBytes(logTopics[3].Bytes())
anycallSwapInfo.CallData, err = abicoder.ParseBytesInData(logData, 0)
if err != nil {
return err
}
anycallSwapInfo.Fallback = common.BytesToAddress(common.GetData(logData, 32, 32)).LowerHex()
if params.GetSwapSubType() == tokens.AnycallSubTypeV5 || params.GetSwapSubType() == tokens.CurveAnycallSubType {
return nil
}
if len(logData) < 224 {
return abicoder.ErrParseDataError
}
anycallSwapInfo.Flags = common.GetBigInt(logData, 64, 32).String()
anycallSwapInfo.AppID, err = abicoder.ParseStringInData(logData, 96)
if err != nil {
return err
}
anycallSwapInfo.Nonce = common.GetBigInt(logData, 128, 32).String()
return nil
}
func (b *Bridge) checkAnyCallSwapInfo(swapInfo *tokens.SwapTxInfo) error {
if swapInfo.FromChainID.String() != b.ChainConfig.ChainID {
log.Error("anycall swap tx with mismatched fromChainID in receipt", "txid", swapInfo.Hash, "logIndex", swapInfo.LogIndex, "fromChainID", swapInfo.FromChainID, "toChainID", swapInfo.ToChainID, "chainID", b.ChainConfig.ChainID)
return tokens.ErrFromChainIDMismatch
}
if swapInfo.FromChainID.Cmp(swapInfo.ToChainID) == 0 {
return tokens.ErrSameFromAndToChainID
}
dstBridge := router.GetBridgeByChainID(swapInfo.ToChainID.String())
if dstBridge == nil {
return tokens.ErrNoBridgeForChainID
}
// check budget on dest chain to prvent DOS attack
if params.HasMinReserveBudgetConfig() {
minReserveBudget := params.GetMinReserveBudget(dstBridge.GetChainConfig().ChainID)
if minReserveBudget == nil {
minReserveBudget = defMinReserveBudget
}
callFrom := getCallFrom(swapInfo)
if err := tokens.CheckNativeBalance(dstBridge, callFrom, minReserveBudget); err != nil {
return tokens.ErrNoEnoughReserveBudget
}
}
return nil
}
func (b *Bridge) buildAnyCallSwapTxInput(args *tokens.BuildTxArgs) (err error) {
if b.ChainConfig.ChainID != args.ToChainID.String() {
return errors.New("anycall to chainId mismatch")
}
anycallSwapInfo := args.AnyCallSwapInfo
if anycallSwapInfo == nil {
return errors.New("build anycall swaptx without swapinfo")
}
funcHash, err := getAnyExecFuncHash()
if err != nil {
return err
}
var input []byte
switch params.GetSwapSubType() {
case tokens.AnycallSubTypeV5, tokens.CurveAnycallSubType:
input = abicoder.PackDataWithFuncHash(funcHash,
common.HexToAddress(anycallSwapInfo.CallFrom),
common.HexToAddress(anycallSwapInfo.CallTo),
anycallSwapInfo.CallData,
common.HexToAddress(anycallSwapInfo.Fallback),
args.FromChainID,
)
case tokens.AnycallSubTypeV6:
nonce, err := common.GetBigIntFromStr(anycallSwapInfo.Nonce)
if err != nil {
return err
}
flags, err := common.GetBigIntFromStr(anycallSwapInfo.Flags)
if err != nil {
return err
}
input = abicoder.PackDataWithFuncHash(funcHash,
common.HexToAddress(anycallSwapInfo.CallTo),
anycallSwapInfo.CallData,
common.HexToAddress(anycallSwapInfo.Fallback),
anycallSwapInfo.AppID,
common.HexToHash(args.SwapID),
common.HexToAddress(anycallSwapInfo.CallFrom),
args.FromChainID,
nonce,
flags,
)
}
args.Input = (*hexutil.Bytes)(&input) // input
routerContract := b.GetRouterContract("")
args.To = routerContract // to
return nil
}