/
swap.go
503 lines (453 loc) · 15.7 KB
/
swap.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package worker
import (
"errors"
"fmt"
"strings"
"github.com/anyswap/CrossChain-Bridge/cmd/utils"
"github.com/anyswap/CrossChain-Bridge/mongodb"
"github.com/anyswap/CrossChain-Bridge/params"
"github.com/anyswap/CrossChain-Bridge/tokens"
"github.com/anyswap/CrossChain-Bridge/types"
mapset "github.com/deckarep/golang-set"
)
var (
cachedSwapHistoty = mapset.NewSet()
maxCachedSwapHistorySize = 1000
cachedSwapTasks = mapset.NewSet()
maxCachedSwapTasks = 1000
swapChanSize = 10
swapinTaskChanMap = make(map[string]chan *tokens.BuildTxArgs)
swapoutTaskChanMap = make(map[string]chan *tokens.BuildTxArgs)
errAlreadySwapped = errors.New("already swapped")
errDBError = errors.New("database error")
errSendTxWithDiffHash = errors.New("send tx with different hash")
)
// StartSwapJob swap job
func StartSwapJob() {
swapinNonces, swapoutNonces := mongodb.LoadAllSwapNonces()
if tokens.DstNonceSetter != nil {
tokens.DstNonceSetter.InitNonces(swapinNonces)
}
if tokens.SrcNonceSetter != nil {
tokens.SrcNonceSetter.InitNonces(swapoutNonces)
}
for _, pairCfg := range tokens.GetTokenPairsConfig() {
AddSwapJob(pairCfg)
}
mongodb.MgoWaitGroup.Add(2)
go startSwapinSwapJob()
go startSwapoutSwapJob()
}
// AddSwapJob add swap job
func AddSwapJob(pairCfg *tokens.TokenPairConfig) {
swapinDcrmAddr := strings.ToLower(pairCfg.DestToken.DcrmAddress)
if _, exist := swapinTaskChanMap[swapinDcrmAddr]; !exist {
swapinTaskChanMap[swapinDcrmAddr] = make(chan *tokens.BuildTxArgs, swapChanSize)
utils.TopWaitGroup.Add(1)
go processSwapTask(swapinTaskChanMap[swapinDcrmAddr], swapinDcrmAddr, true)
}
swapoutDcrmAddr := strings.ToLower(pairCfg.SrcToken.DcrmAddress)
if _, exist := swapoutTaskChanMap[swapoutDcrmAddr]; !exist {
swapoutTaskChanMap[swapoutDcrmAddr] = make(chan *tokens.BuildTxArgs, swapChanSize)
utils.TopWaitGroup.Add(1)
go processSwapTask(swapoutTaskChanMap[swapoutDcrmAddr], swapoutDcrmAddr, false)
}
}
func startSwapinSwapJob() {
logWorker("swap", "start swapin swap job")
defer mongodb.MgoWaitGroup.Done()
for {
if utils.IsCleanuping() {
logWorker("swap", "stop swapin swap job")
return
}
processSwapins(mongodb.TxNotSwapped)
restInJob(restIntervalInDoSwapJob)
}
}
func startSwapoutSwapJob() {
logWorker("swap", "start swapout swap job")
defer mongodb.MgoWaitGroup.Done()
for {
if utils.IsCleanuping() {
logWorker("swap", "stop swapout swap job")
return
}
processSwapouts(mongodb.TxNotSwapped)
restInJob(restIntervalInDoSwapJob)
}
}
func processSwapins(status mongodb.SwapStatus) {
swapins, err := findSwapinsToSwap(status)
if err != nil {
logWorkerError("swapin", "find swapins error", err, "status", status)
return
}
if len(swapins) == 0 {
return
}
logWorker("swapin", "find swapins to swap", "status", status, "count", len(swapins))
for _, swap := range swapins {
if utils.IsCleanuping() {
return
}
err := processSwapinSwap(swap)
switch {
case err == nil,
errors.Is(err, errAlreadySwapped),
errors.Is(err, errDBError),
errors.Is(err, tokens.ErrUnknownPairID),
errors.Is(err, tokens.ErrAddressIsInBlacklist),
errors.Is(err, tokens.ErrSwapIsClosed):
default:
logWorkerError("swapin", "process swapin swap error", err, "pairID", swap.PairID, "txid", swap.TxID, "bind", swap.Bind)
}
}
}
func processSwapouts(status mongodb.SwapStatus) {
swapouts, err := findSwapoutsToSwap(status)
if err != nil {
logWorkerError("swapout", "find swapouts error", err, "status", status)
}
if len(swapouts) == 0 {
return
}
logWorker("swapout", "find swapouts to swap", "status", status, "count", len(swapouts))
for _, swap := range swapouts {
if utils.IsCleanuping() {
return
}
err := processSwapoutSwap(swap)
switch {
case err == nil,
errors.Is(err, errAlreadySwapped),
errors.Is(err, errDBError),
errors.Is(err, tokens.ErrUnknownPairID),
errors.Is(err, tokens.ErrAddressIsInBlacklist),
errors.Is(err, tokens.ErrSwapIsClosed):
default:
logWorkerError("swapout", "process swapout swap error", err, "pairID", swap.PairID, "txid", swap.TxID, "bind", swap.Bind)
}
}
}
func findSwapinsToSwap(status mongodb.SwapStatus) ([]*mongodb.MgoSwap, error) {
septime := getSepTimeInFind(maxDoSwapLifetime)
return mongodb.FindSwapinsWithStatus(status, septime)
}
func findSwapoutsToSwap(status mongodb.SwapStatus) ([]*mongodb.MgoSwap, error) {
septime := getSepTimeInFind(maxDoSwapLifetime)
return mongodb.FindSwapoutsWithStatus(status, septime)
}
func isSwapInBlacklist(swap *mongodb.MgoSwapResult) (isBlacked bool, err error) {
isBlacked, err = mongodb.QueryBlacklist(swap.From, swap.PairID)
if err != nil {
logWorkerTrace("swap", "query blacklist failed", "err", err)
return isBlacked, err
}
if !isBlacked && swap.Bind != swap.From {
isBlacked, err = mongodb.QueryBlacklist(swap.Bind, swap.PairID)
if err != nil {
logWorkerTrace("swap", "query blacklist failed", "err", err)
return isBlacked, err
}
}
return isBlacked, nil
}
func processSwapinSwap(swap *mongodb.MgoSwap) (err error) {
return processSwap(swap, true)
}
func processSwapoutSwap(swap *mongodb.MgoSwap) (err error) {
return processSwap(swap, false)
}
func processSwap(swap *mongodb.MgoSwap, isSwapin bool) (err error) {
pairID := swap.PairID
txid := swap.TxID
bind := swap.Bind
cacheKey := getSwapCacheKey(isSwapin, txid, bind)
if cachedSwapTasks.Contains(cacheKey) {
return errAlreadySwapped
}
res, err := mongodb.FindSwapResult(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
err = preventReswap(res, isSwapin)
if err != nil {
return err
}
dcrmAddress, err := checkSwapResult(res, isSwapin)
if err != nil {
return err
}
logWorker("swap", "start process swap", "pairID", pairID, "txid", txid, "bind", bind, "status", swap.Status, "isSwapin", isSwapin, "value", res.Value)
srcBridge := tokens.GetCrossChainBridge(isSwapin)
swapInfo, err := verifySwapTransaction(srcBridge, pairID, txid, bind, tokens.SwapTxType(swap.TxType))
if err != nil {
return fmt.Errorf("[doSwap] reverify swap failed, %w", err)
}
if swapInfo.Value.String() != res.Value {
return fmt.Errorf("[doSwap] reverify swap value mismatch, in db %v != %v", res.Value, swapInfo.Value)
}
if !strings.EqualFold(swapInfo.Bind, bind) {
return fmt.Errorf("[doSwap] reverify swap bind address mismatch, in db %v != %v", bind, swapInfo.Bind)
}
swapType := getSwapType(isSwapin)
args := &tokens.BuildTxArgs{
SwapInfo: tokens.SwapInfo{
Identifier: params.GetIdentifier(),
PairID: pairID,
SwapID: txid,
SwapType: swapType,
TxType: tokens.SwapTxType(swap.TxType),
Bind: bind,
Reswapping: res.Status == mongodb.Reswapping,
},
From: dcrmAddress,
OriginValue: swapInfo.Value,
}
return dispatchSwapTask(args)
}
func checkSwapResult(res *mongodb.MgoSwapResult, isSwapin bool) (dcrmAddress string, err error) {
pairID := res.PairID
txid := res.TxID
bind := res.Bind
fromTokenCfg, toTokenCfg := tokens.GetTokenConfigsByDirection(pairID, isSwapin)
if fromTokenCfg == nil || toTokenCfg == nil {
logWorkerTrace("swap", "swap is not configed", "pairID", pairID, "isSwapin", isSwapin)
return "", tokens.ErrUnknownPairID
}
if fromTokenCfg.DisableSwap {
logWorkerTrace("swap", "swap is disabled", "pairID", pairID, "isSwapin", isSwapin)
return "", tokens.ErrSwapIsClosed
}
isBlacked, err := isSwapInBlacklist(res)
if err != nil {
return "", errDBError
}
if isBlacked {
logWorkerTrace("swap", "address is in blacklist", "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin)
err = tokens.ErrAddressIsInBlacklist
_ = mongodb.UpdateSwapStatus(isSwapin, txid, pairID, bind, mongodb.SwapInBlacklist, now(), err.Error())
return "", err
}
return toTokenCfg.DcrmAddress, nil
}
func preventReswap(res *mongodb.MgoSwapResult, isSwapin bool) error {
if res.SwapNonce > 0 || res.SwapTx != "" || res.SwapHeight != 0 || len(res.OldSwapTxs) > 0 {
_ = mongodb.UpdateSwapStatus(isSwapin, res.TxID, res.PairID, res.Bind, mongodb.TxProcessed, now(), "")
return errAlreadySwapped
}
switch res.Status {
case mongodb.TxWithBigValue,
mongodb.TxWithWrongMemo,
mongodb.BindAddrIsContract,
mongodb.TxWithWrongValue:
_ = mongodb.UpdateSwapStatus(isSwapin, res.TxID, res.PairID, res.Bind, res.Status, now(), "")
return fmt.Errorf("forbid doswap for swap with status %v", res.Status.String())
default:
}
if res.Status != mongodb.Reswapping {
if isSwapHistoryExist(isSwapin, res.TxID, res.Bind) {
logWorkerError("[doSwap]", "forbid reswap by cache", errAlreadySwapped, "isSwapin", isSwapin, "txid", res.TxID, "bind", res.Bind)
_ = mongodb.UpdateSwapStatus(isSwapin, res.TxID, res.PairID, res.Bind, mongodb.TxProcessed, now(), "")
return errAlreadySwapped
}
}
return preventReswapByHistory(res, isSwapin)
}
func preventReswapByHistory(res *mongodb.MgoSwapResult, isSwapin bool) error {
swapHistories, _ := mongodb.GetSwapHistory(isSwapin, res.TxID, res.Bind)
if len(swapHistories) == 0 {
return nil
}
var alreadySwapped bool
if res.Status != mongodb.Reswapping {
alreadySwapped = true
} else {
resBridge := tokens.GetCrossChainBridge(!isSwapin)
for _, swaphist := range swapHistories {
txStatus, err := resBridge.GetTransactionStatus(swaphist.SwapTx)
if err != nil {
continue
}
if txStatus.Receipt != nil {
receipt, ok := txStatus.Receipt.(*types.RPCTxReceipt)
if ok && *receipt.Status == 1 {
alreadySwapped = true
break
}
} else if txStatus != nil && txStatus.BlockHeight > 0 {
alreadySwapped = true
break
}
}
}
if alreadySwapped {
logWorkerError("[doSwap]", "forbid reswap by history", errAlreadySwapped,
"isSwapin", isSwapin, "txid", res.TxID, "bind", res.Bind, "history", swapHistories)
_ = mongodb.UpdateSwapStatus(isSwapin, res.TxID, res.PairID, res.Bind, mongodb.TxProcessed, now(), "")
return errAlreadySwapped
}
return nil
}
func dispatchSwapTask(args *tokens.BuildTxArgs) error {
from := strings.ToLower(args.From)
switch args.SwapType {
case tokens.SwapinType:
swapChan, exist := swapinTaskChanMap[from]
if !exist {
return fmt.Errorf("no swapin task channel for dcrm address '%v'", args.From)
}
swapChan <- args
case tokens.SwapoutType:
swapChan, exist := swapoutTaskChanMap[from]
if !exist {
return fmt.Errorf("no swapout task channel for dcrm address '%v'", args.From)
}
swapChan <- args
default:
return fmt.Errorf("wrong swap type '%v'", args.SwapType.String())
}
logWorker("doSwap", "dispatch swap task", "pairID", args.PairID, "txid", args.SwapID, "bind", args.Bind, "swapType", args.SwapType.String(), "value", args.OriginValue)
return nil
}
func processSwapTask(swapChan <-chan *tokens.BuildTxArgs, dcrmAddress string, isSwapin bool) {
defer utils.TopWaitGroup.Done()
for {
select {
case <-utils.CleanupChan:
logWorker("doSwap", "stop process swap task", "isSwapin", isSwapin, "dcrmAddress", dcrmAddress)
return
case args := <-swapChan:
if !strings.EqualFold(args.From, dcrmAddress) || args.SwapType != getSwapType(isSwapin) {
logWorkerWarn("doSwap", "ignore swap task as mismatch reason", "isSwapin", isSwapin, "dcrmAddress", dcrmAddress, "args", args)
continue
}
err := doSwap(args)
switch {
case err == nil,
errors.Is(err, errAlreadySwapped):
default:
logWorkerError("doSwap", "process failed", err, "pairID", args.PairID, "txid", args.SwapID, "swapType", args.SwapType.String(), "value", args.OriginValue)
}
}
}
}
func getSwapCacheKey(isSwapin bool, txid, bind string) string {
return strings.ToLower(fmt.Sprintf("%s:%s:%t", txid, bind, isSwapin))
}
func checkAndUpdateProcessSwapTaskCache(key string) error {
if cachedSwapTasks.Contains(key) {
return errAlreadySwapped
}
if cachedSwapTasks.Cardinality() >= maxCachedSwapTasks {
cachedSwapTasks.Pop()
}
cachedSwapTasks.Add(key)
return nil
}
func doSwap(args *tokens.BuildTxArgs) (err error) {
pairID := args.PairID
txid := args.SwapID
bind := args.Bind
swapType := args.SwapType
isSwapin := swapType == tokens.SwapinType
resBridge := tokens.GetCrossChainBridge(!isSwapin)
cacheKey := getSwapCacheKey(isSwapin, txid, bind)
err = checkAndUpdateProcessSwapTaskCache(cacheKey)
if err != nil {
return err
}
logWorker("doSwap", "add swap cache", "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "value", args.OriginValue)
isCachedSwapProcessed := false
defer func() {
if !isCachedSwapProcessed {
logWorkerError("doSwap", "delete swap cache", err, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "value", args.OriginValue)
cachedSwapTasks.Remove(cacheKey)
}
}()
logWorker("doSwap", "start to process", "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "value", args.OriginValue)
rawTx, err := resBridge.BuildRawTransaction(args)
if err != nil {
logWorkerError("doSwap", "build tx failed", err, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin)
return err
}
swapNonce := args.GetTxNonce()
var signedTx interface{}
var signTxHash string
tokenCfg := resBridge.GetTokenConfig(pairID)
for i := 1; i <= 3; i++ { // with retry
if tokenCfg.GetDcrmAddressPrivateKey() != nil {
signedTx, signTxHash, err = resBridge.SignTransaction(rawTx, pairID)
} else {
signedTx, signTxHash, err = resBridge.DcrmSignTransaction(rawTx, args.GetExtraArgs())
}
if err == nil {
break
}
logWorkerError("doSwap", "sign tx failed", err, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "signCount", i)
restInJob(retrySignInterval)
}
if err != nil {
return err
}
// recheck reswap before update db
res, err := mongodb.FindSwapResult(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
err = preventReswap(res, isSwapin)
if err != nil {
return err
}
// update database before sending transaction
matchTx := &MatchTx{
SwapTx: signTxHash,
SwapType: swapType,
SwapNonce: swapNonce,
}
if args.SwapValue != nil {
matchTx.SwapValue = args.SwapValue.String()
} else {
matchTx.SwapValue = tokens.CalcSwappedValue(pairID, args.OriginValue, isSwapin).String()
}
err = updateSwapResult(txid, pairID, bind, matchTx)
if err != nil {
logWorkerError("doSwap", "update swap result failed", err, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin)
return err
}
isCachedSwapProcessed = true
err = mongodb.UpdateSwapStatus(isSwapin, txid, pairID, bind, mongodb.TxProcessed, now(), "")
if err != nil {
logWorkerError("doSwap", "update swap status failed", err, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin)
return err
}
txHash, err := sendSignedTransaction(resBridge, signedTx, txid, pairID, bind, isSwapin)
if err == nil {
logWorker("doSwap", "send tx success", "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "swapNonce", swapNonce, "txHash", txHash)
if txHash != signTxHash {
logWorkerError("doSwap", "send tx success but with different hash", errSendTxWithDiffHash, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "swapNonce", swapNonce, "txHash", txHash, "signTxHash", signTxHash)
_ = replaceSwapResult(txid, pairID, bind, txHash, matchTx.SwapValue, isSwapin)
}
if nonceSetter, ok := resBridge.(tokens.NonceSetter); ok {
nonceSetter.SetNonce(pairID, swapNonce+1) // increase for next usage
}
}
return err
}
// DeleteCachedSwap delete cached swap
func DeleteCachedSwap(isSwapin bool, txid, bind string) {
cacheKey := getSwapCacheKey(isSwapin, txid, bind)
cachedSwapTasks.Remove(cacheKey)
}
func addSwapHistory(isSwapin bool, txid, bind string) {
if cachedSwapHistoty.Cardinality() >= maxCachedSwapHistorySize {
cachedSwapHistoty.Pop()
}
key := getSwapCacheKey(isSwapin, txid, bind)
cachedSwapHistoty.Add(key)
}
func isSwapHistoryExist(isSwapin bool, txid, bind string) bool {
key := getSwapCacheKey(isSwapin, txid, bind)
return cachedSwapHistoty.Contains(key)
}