Skip to content

Commit e0d61c0

Browse files
committed
update old swaptxs by push method instead of replace
1 parent 4892dbf commit e0d61c0

File tree

5 files changed

+102
-112
lines changed

5 files changed

+102
-112
lines changed

mongodb/api.go

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ const (
2222
)
2323

2424
var (
25-
retryLock sync.Mutex
26-
updateResultLock sync.Mutex
25+
retryLock sync.Mutex
26+
updateResultLock sync.Mutex
27+
updateOldSwapinTxsLock sync.Mutex
28+
updateOldSwapoutTxsLock sync.Mutex
2729

2830
maxCountOfResults = int64(1000)
2931
)
@@ -161,7 +163,7 @@ func updateSwapStatus(collection *mongo.Collection, txid, pairID, bind string, s
161163
}
162164
printLog("mongodb update swap status", "txid", txid, "pairID", pairID, "bind", bind, "status", status, "isSwapin", isSwapin(collection))
163165
} else {
164-
log.Debug("mongodb update swap status", "txid", txid, "pairID", pairID, "bind", bind, "status", status, "isSwapin", isSwapin(collection), "err", err)
166+
log.Error("mongodb update swap status", "txid", txid, "pairID", pairID, "bind", bind, "status", status, "isSwapin", isSwapin(collection), "err", err)
165167
}
166168
return mgoError(err)
167169
}
@@ -354,12 +356,6 @@ func updateSwapResult(collection *mongo.Collection, txid, pairID, bind string, i
354356
if items.SwapTx != "" {
355357
updates["swaptx"] = items.SwapTx
356358
}
357-
if len(items.OldSwapTxs) != 0 {
358-
updates["oldswaptxs"] = items.OldSwapTxs
359-
}
360-
if len(items.OldSwapVals) != 0 {
361-
updates["oldswapvals"] = items.OldSwapVals
362-
}
363359
if items.SwapHeight != 0 {
364360
updates["swapheight"] = items.SwapHeight
365361
}
@@ -400,7 +396,7 @@ func updateSwapResult(collection *mongo.Collection, txid, pairID, bind string, i
400396
if err == nil {
401397
log.Info("mongodb update swap result", "txid", txid, "pairID", pairID, "bind", bind, "updates", updates, "isSwapin", isSwapin(collection))
402398
} else {
403-
log.Debug("mongodb update swap result", "txid", txid, "pairID", pairID, "bind", bind, "updates", updates, "isSwapin", isSwapin(collection), "err", err)
399+
log.Error("mongodb update swap result", "txid", txid, "pairID", pairID, "bind", bind, "updates", updates, "isSwapin", isSwapin(collection), "err", err)
404400
}
405401
return mgoError(err)
406402
}
@@ -425,7 +421,77 @@ func updateSwapResultStatus(collection *mongo.Collection, txid, pairID, bind str
425421
if err == nil {
426422
log.Info("mongodb update swap result status", "txid", txid, "pairID", pairID, "bind", bind, "status", status, "isSwapin", isSwapin)
427423
} else {
428-
log.Debug("mongodb update swap result status", "txid", txid, "pairID", pairID, "bind", bind, "status", status, "isSwapin", isSwapin, "err", err)
424+
log.Error("mongodb update swap result status", "txid", txid, "pairID", pairID, "bind", bind, "status", status, "isSwapin", isSwapin, "err", err)
425+
}
426+
return mgoError(err)
427+
}
428+
429+
// UpdateSwapResultOldTxs update swap result oldtxs
430+
func UpdateSwapResultOldTxs(txid, pairID, bind, swapTx, swapValue string, isSwapin bool) error {
431+
if swapTx == "" {
432+
return nil
433+
}
434+
if isSwapin {
435+
return updateSwapinResultOldTxs(txid, pairID, bind, swapTx, swapValue)
436+
}
437+
return updateSwapoutResultOldTxs(txid, pairID, bind, swapTx, swapValue)
438+
}
439+
440+
func updateSwapinResultOldTxs(txid, pairID, bind, swapTx, swapValue string) error {
441+
updateOldSwapinTxsLock.Lock()
442+
defer updateOldSwapinTxsLock.Unlock()
443+
return updateSwapResultOldTxs(collSwapinResult, txid, pairID, bind, swapTx, swapValue)
444+
}
445+
446+
func updateSwapoutResultOldTxs(txid, pairID, bind, swapTx, swapValue string) error {
447+
updateOldSwapoutTxsLock.Lock()
448+
defer updateOldSwapoutTxsLock.Unlock()
449+
return updateSwapResultOldTxs(collSwapoutResult, txid, pairID, bind, swapTx, swapValue)
450+
}
451+
452+
func updateSwapResultOldTxs(collection *mongo.Collection, txid, pairID, bind, swapTx, swapValue string) error {
453+
swapRes, err := findSwapResult(collection, txid, pairID, bind)
454+
if err != nil {
455+
return err
456+
}
457+
458+
// already exist
459+
if strings.EqualFold(swapTx, swapRes.SwapTx) {
460+
return nil
461+
}
462+
for _, oldSwapTx := range swapRes.OldSwapTxs {
463+
if strings.EqualFold(swapTx, oldSwapTx) {
464+
return nil
465+
}
466+
}
467+
468+
var updates bson.M
469+
470+
if len(swapRes.OldSwapTxs) == 0 {
471+
updates = bson.M{
472+
"swaptx": swapTx,
473+
"oldswaptxs": []string{swapRes.SwapTx, swapTx},
474+
"timestamp": time.Now().Unix(),
475+
}
476+
if swapValue != "" {
477+
updates["oldswapvals"] = []string{swapRes.SwapValue, swapValue}
478+
}
479+
} else {
480+
arrayPushes := bson.M{"oldswaptxs": swapTx}
481+
if swapValue != "" {
482+
arrayPushes["oldswapvals"] = swapValue
483+
}
484+
updates = bson.M{
485+
"$set": bson.M{"swaptx": swapTx, "timestamp": time.Now().Unix()},
486+
"$push": arrayPushes,
487+
}
488+
}
489+
490+
_, err = collection.UpdateByID(clientCtx, GetSwapKey(txid, pairID, bind), updates)
491+
if err == nil {
492+
log.Info("UpdateRouterOldSwapTxs success", "txid", txid, "pairID", pairID, "bind", bind, "swaptx", swapTx, "nonce", swapRes.SwapNonce, "swapValue", swapValue)
493+
} else {
494+
log.Error("UpdateRouterOldSwapTxs failed", "txid", txid, "pairID", pairID, "bind", bind, "swaptx", swapTx, "nonce", swapRes.SwapNonce, "swapValue", swapValue, "err", err)
429495
}
430496
return mgoError(err)
431497
}
@@ -520,8 +586,8 @@ func AddP2shAddress(ma *MgoP2shAddress) error {
520586
_, err := collP2shAddress.InsertOne(clientCtx, ma)
521587
if err == nil {
522588
log.Info("mongodb add p2sh address", "key", ma.Key, "p2shaddress", ma.P2shAddress)
523-
} else {
524-
log.Debug("mongodb add p2sh address", "key", ma.Key, "p2shaddress", ma.P2shAddress, "err", err)
589+
} else if !mongo.IsDuplicateKeyError(err) {
590+
log.Error("mongodb add p2sh address", "key", ma.Key, "p2shaddress", ma.P2shAddress, "err", err)
525591
}
526592
return mgoError(err)
527593
}
@@ -586,7 +652,7 @@ func UpdateLatestScanInfo(isSrc bool, blockHeight uint64) error {
586652
if err == nil {
587653
log.Info("mongodb update lastest scan info", "isSrc", isSrc, "updates", updates)
588654
} else {
589-
log.Debug("mongodb update latest scan info", "isSrc", isSrc, "updates", updates, "err", err)
655+
log.Error("mongodb update latest scan info", "isSrc", isSrc, "updates", updates, "err", err)
590656
}
591657
return mgoError(err)
592658
}
@@ -618,8 +684,8 @@ func AddRegisteredAddress(address string) error {
618684
_, err := collRegisteredAddress.InsertOne(clientCtx, ma)
619685
if err == nil {
620686
log.Info("mongodb add register address", "key", ma.Key)
621-
} else {
622-
log.Debug("mongodb add register address", "key", ma.Key, "err", err)
687+
} else if !mongo.IsDuplicateKeyError(err) {
688+
log.Error("mongodb add register address", "key", ma.Key, "err", err)
623689
}
624690
return mgoError(err)
625691
}
@@ -738,8 +804,8 @@ func AddSwapHistory(isSwapin bool, txid, bind, swaptx string) error {
738804
_, err := collSwapHistory.InsertOne(clientCtx, item)
739805
if err == nil {
740806
log.Info("mongodb add swap history success", "txid", txid, "bind", bind, "isSwapin", isSwapin)
741-
} else {
742-
log.Debug("mongodb add swap history failed", "txid", txid, "bind", bind, "isSwapin", isSwapin, "err", err)
807+
} else if !mongo.IsDuplicateKeyError(err) {
808+
log.Error("mongodb add swap history failed", "txid", txid, "bind", bind, "isSwapin", isSwapin, "err", err)
743809
}
744810
return mgoError(err)
745811
}

mongodb/tables.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@ type MgoSwapResult struct {
4747

4848
// SwapResultUpdateItems swap update items
4949
type SwapResultUpdateItems struct {
50-
SwapTx string
51-
OldSwapTxs []string
52-
OldSwapVals []string
53-
SwapHeight uint64
54-
SwapTime uint64
55-
SwapValue string
56-
SwapType uint32
57-
SwapNonce uint64
58-
Status SwapStatus
59-
Timestamp int64
60-
Memo string
50+
SwapTx string
51+
SwapHeight uint64
52+
SwapTime uint64
53+
SwapValue string
54+
SwapType uint32
55+
SwapNonce uint64
56+
Status SwapStatus
57+
Timestamp int64
58+
Memo string
6159
}
6260

6361
// MgoP2shAddress key is the bind address

worker/common.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,6 @@ func updateSwapResultTx(txid, pairID, bind, swapTx, swapValue string, isSwapin b
182182
return err
183183
}
184184

185-
func updateOldSwapTxs(txid, pairID, bind, swapTx string, oldSwapTxs, oldSwapVals []string, isSwapin bool) (err error) {
186-
if len(oldSwapTxs) != len(oldSwapVals) {
187-
return fmt.Errorf("update old swaptxs with different count of values")
188-
}
189-
updates := &mongodb.SwapResultUpdateItems{
190-
Status: mongodb.KeepStatus,
191-
SwapTx: swapTx,
192-
OldSwapTxs: oldSwapTxs,
193-
OldSwapVals: oldSwapVals,
194-
Timestamp: now(),
195-
}
196-
if isSwapin {
197-
err = mongodb.UpdateSwapinResult(txid, pairID, bind, updates)
198-
} else {
199-
err = mongodb.UpdateSwapoutResult(txid, pairID, bind, updates)
200-
}
201-
if err != nil {
202-
logWorkerError("update", "updateOldSwapTxs", err, "txid", txid, "pairID", pairID, "bind", bind, "swapTx", swapTx, "swaptxs", len(oldSwapTxs))
203-
} else {
204-
logWorker("update", "updateOldSwapTxs", "txid", txid, "pairID", pairID, "bind", bind, "swapTx", swapTx, "swaptxs", len(oldSwapTxs))
205-
}
206-
return err
207-
}
208-
209185
func markSwapResultUnstable(txid, pairID, bind string, isSwapin bool) (err error) {
210186
status := mongodb.MatchTxNotStable
211187
timestamp := now()
@@ -270,11 +246,12 @@ func sendSignedTransaction(bridge tokens.CrossChainBridge, signedTx interface{},
270246
retrySendTxInterval = 1 * time.Second
271247
txid, pairID, bind = args.SwapID, args.PairID, args.Bind
272248
isSwapin = args.SwapType == tokens.SwapinType
249+
swapNonce = args.GetTxNonce()
273250
)
274251
for i := 0; i < retrySendTxCount; i++ {
275252
txHash, err = bridge.SendTransaction(signedTx)
276253
if err == nil {
277-
logWorker("sendtx", "send tx success", "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "txHash", txHash)
254+
logWorker("sendtx", "send tx success", "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "txHash", txHash, "swapNonce", swapNonce)
278255
break
279256
}
280257
time.Sleep(retrySendTxInterval)
@@ -284,7 +261,7 @@ func sendSignedTransaction(bridge tokens.CrossChainBridge, signedTx interface{},
284261
_ = mongodb.AddSwapHistory(isSwapin, txid, bind, txHash)
285262
}
286263
if err != nil {
287-
logWorkerError("sendtx", "send tx failed", err, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "txHash", txHash)
264+
logWorkerError("sendtx", "send tx failed", err, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "txHash", txHash, "swapNonce", swapNonce)
288265
return txHash, err
289266
}
290267

@@ -293,7 +270,7 @@ func sendSignedTransaction(bridge tokens.CrossChainBridge, signedTx interface{},
293270
return txHash, err
294271
}
295272

296-
nonceSetter.SetNonce(pairID, args.GetTxNonce()+1) // increase for next usage
273+
nonceSetter.SetNonce(pairID, swapNonce+1) // increase for next usage
297274

298275
// update swap result tx height in goroutine
299276
go func() {

worker/replaceswap.go

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"math/big"
77
"strings"
8-
"sync"
98

109
"github.com/anyswap/CrossChain-Bridge/mongodb"
1110
"github.com/anyswap/CrossChain-Bridge/params"
@@ -24,8 +23,6 @@ var (
2423
errUpdateOldTxsFailed = errors.New("update old swaptxs failed")
2524
errNotNonceSupport = errors.New("not nonce support bridge")
2625

27-
updateOldSwapTxsLock sync.Mutex
28-
2926
maxDistanceOfSwapNonce = uint64(5)
3027
)
3128

@@ -204,63 +201,18 @@ func replaceSwap(txid, pairID, bind, gasPriceStr string, isSwapin bool) (txHash
204201
if args.SwapValue != nil {
205202
swapValue = args.SwapValue.String()
206203
}
207-
err = replaceSwapResult(txid, pairID, bind, signTxHash, swapValue, isSwapin)
204+
err = mongodb.UpdateSwapResultOldTxs(txid, pairID, bind, signTxHash, swapValue, isSwapin)
208205
if err != nil {
209206
return "", errUpdateOldTxsFailed
210207
}
211208
txHash, err = sendSignedTransaction(bridge, signedTx, args)
212209
if err == nil && txHash != signTxHash {
213210
logWorkerError("replaceSwap", "send tx success but with different hash", errSendTxWithDiffHash, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "swapNonce", nonce, "txHash", txHash, "signTxHash", signTxHash)
214-
_ = replaceSwapResult(txid, pairID, bind, txHash, swapValue, isSwapin)
211+
_ = mongodb.UpdateSwapResultOldTxs(txid, pairID, bind, txHash, swapValue, isSwapin)
215212
}
216213
return txHash, err
217214
}
218215

219-
func replaceSwapResult(txid, pairID, bind, txHash, swapValue string, isSwapin bool) (err error) {
220-
updateOldSwapTxsLock.Lock()
221-
defer updateOldSwapTxsLock.Unlock()
222-
223-
res, err := mongodb.FindSwapResult(isSwapin, txid, pairID, bind)
224-
if err != nil {
225-
return err
226-
}
227-
228-
oldSwapTxs := res.OldSwapTxs
229-
oldSwapVals := res.OldSwapVals
230-
if len(oldSwapTxs) > 0 {
231-
for _, oldSwapTx := range oldSwapTxs {
232-
if oldSwapTx == txHash {
233-
return nil
234-
}
235-
}
236-
oldSwapTxs = append(oldSwapTxs, txHash)
237-
oldSwapVals = append(oldSwapVals, swapValue)
238-
} else {
239-
if txHash == res.SwapTx {
240-
return nil
241-
}
242-
if res.SwapTx == "" {
243-
oldSwapTxs = []string{txHash}
244-
oldSwapVals = []string{swapValue}
245-
} else {
246-
oldSwapTxs = []string{res.SwapTx, txHash}
247-
oldSwapVals = []string{res.SwapValue, swapValue}
248-
}
249-
}
250-
bridge := tokens.GetCrossChainBridge(!isSwapin)
251-
if bridge.GetChainConfig().BaseGasPrice == "" {
252-
oldSwapVals = nil
253-
}
254-
swapType := tokens.SwapType(res.SwapType).String()
255-
err = updateOldSwapTxs(txid, pairID, bind, txHash, oldSwapTxs, oldSwapVals, isSwapin)
256-
if err != nil {
257-
logWorkerError("replace", "replaceSwapResult", err, "txid", txid, "pairID", pairID, "bind", bind, "swaptx", txHash, "swapType", swapType, "nonce", res.SwapNonce, "swapValue", swapValue)
258-
} else {
259-
logWorker("replace", "replaceSwapResult", "txid", txid, "pairID", pairID, "bind", bind, "swaptx", txHash, "swapType", swapType, "nonce", res.SwapNonce, "swapValue", swapValue)
260-
}
261-
return err
262-
}
263-
264216
func preventReplaceswapByHistory(res *mongodb.MgoSwapResult, isSwapin bool) error {
265217
swapHistories, _ := mongodb.GetSwapHistory(isSwapin, res.TxID, res.Bind)
266218
if len(swapHistories) == 0 {

worker/swap.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,9 @@ func doSwap(args *tokens.BuildTxArgs) (err error) {
472472
}
473473

474474
txHash, err := sendSignedTransaction(resBridge, signedTx, args)
475-
if err == nil {
476-
logWorker("doSwap", "send tx success", "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "swapNonce", swapNonce, "txHash", txHash)
477-
if txHash != signTxHash {
478-
logWorkerError("doSwap", "send tx success but with different hash", errSendTxWithDiffHash, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "swapNonce", swapNonce, "txHash", txHash, "signTxHash", signTxHash)
479-
_ = replaceSwapResult(txid, pairID, bind, txHash, matchTx.SwapValue, isSwapin)
480-
}
475+
if err == nil && txHash != signTxHash {
476+
logWorkerError("doSwap", "send tx success but with different hash", errSendTxWithDiffHash, "pairID", pairID, "txid", txid, "bind", bind, "isSwapin", isSwapin, "swapNonce", swapNonce, "txHash", txHash, "signTxHash", signTxHash)
477+
_ = mongodb.UpdateSwapResultOldTxs(txid, pairID, bind, txHash, matchTx.SwapValue, isSwapin)
481478
}
482479
return err
483480
}

0 commit comments

Comments
 (0)