@@ -22,8 +22,10 @@ const (
2222)
2323
2424var (
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}
0 commit comments