-
Notifications
You must be signed in to change notification settings - Fork 162
/
stable.go
183 lines (166 loc) · 5.53 KB
/
stable.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
package worker
import (
"sync"
"time"
"github.com/anyswap/CrossChain-Bridge/cmd/utils"
"github.com/anyswap/CrossChain-Bridge/mongodb"
"github.com/anyswap/CrossChain-Bridge/tokens"
)
var (
swapinStableStarter sync.Once
swapoutStableStarter sync.Once
treatAsNoncePassedInterval = int64(600) // seconds
)
// StartStableJob stable job
func StartStableJob() {
mongodb.MgoWaitGroup.Add(2)
go startSwapinStableJob()
go startSwapoutStableJob()
}
func startSwapinStableJob() {
swapinStableStarter.Do(func() {
logWorker("stable", "start update swapin stable job")
defer mongodb.MgoWaitGroup.Done()
for {
res, err := findSwapinResultsToStable()
if err != nil {
logWorkerError("stable", "find swapin results error", err)
}
if len(res) > 0 {
logWorker("stable", "find swapin results to stable", "count", len(res))
}
for _, swap := range res {
if utils.IsCleanuping() {
logWorker("stable", "stop update swapin stable job")
return
}
err = processSwapinStable(swap)
if err != nil {
logWorkerError("stable", "process swapin stable error", err)
}
time.Sleep(3 * time.Second) // in case of too frequently rpc calling
}
if utils.IsCleanuping() {
logWorker("stable", "stop update swapin stable job")
return
}
restInJob(restIntervalInStableJob)
}
})
}
func startSwapoutStableJob() {
swapoutStableStarter.Do(func() {
logWorker("stable", "start update swapout stable job")
defer mongodb.MgoWaitGroup.Done()
for {
res, err := findSwapoutResultsToStable()
if err != nil {
logWorkerError("stable", "find swapout results error", err)
}
if len(res) > 0 {
logWorker("stable", "find swapout results to stable", "count", len(res))
}
for _, swap := range res {
if utils.IsCleanuping() {
logWorker("stable", "stop update swapout stable job")
return
}
err = processSwapoutStable(swap)
if err != nil {
logWorkerError("stable", "process swapout stable error", err)
}
}
if utils.IsCleanuping() {
logWorker("stable", "stop update swapout stable job")
return
}
restInJob(restIntervalInStableJob)
}
})
}
func findSwapinResultsToStable() ([]*mongodb.MgoSwapResult, error) {
status := mongodb.MatchTxNotStable
septime := getSepTimeInFind(maxStableLifetime)
return mongodb.FindSwapinResultsWithStatus(status, septime)
}
func findSwapoutResultsToStable() ([]*mongodb.MgoSwapResult, error) {
status := mongodb.MatchTxNotStable
septime := getSepTimeInFind(maxStableLifetime)
return mongodb.FindSwapoutResultsWithStatus(status, septime)
}
func processSwapinStable(swap *mongodb.MgoSwapResult) error {
logWorker("stable", "start processSwapinStable", "swaptxid", swap.SwapTx, "txid", swap.TxID, "bind", swap.Bind, "status", swap.Status)
return processSwapStable(swap, true)
}
func processSwapoutStable(swap *mongodb.MgoSwapResult) (err error) {
logWorker("stable", "start processSwapoutStable", "swaptxid", swap.SwapTx, "txid", swap.TxID, "bind", swap.Bind, "status", swap.Status)
return processSwapStable(swap, false)
}
func getSwapTxStatus(resBridge tokens.CrossChainBridge, swap *mongodb.MgoSwapResult) *tokens.TxStatus {
txStatus, err := resBridge.GetTransactionStatus(swap.SwapTx)
if err == nil && txStatus != nil && txStatus.BlockHeight > 0 {
return txStatus
}
for i, oldSwapTx := range swap.OldSwapTxs {
if swap.SwapTx == oldSwapTx {
continue
}
txStatus, err = resBridge.GetTransactionStatus(oldSwapTx)
if err == nil && txStatus != nil && txStatus.BlockHeight > 0 {
swap.SwapTx = oldSwapTx
swap.SwapValue = swap.OldSwapVals[i]
return txStatus
}
}
return nil
}
func processSwapStable(swap *mongodb.MgoSwapResult, isSwapin bool) (err error) {
oldSwapTx := swap.SwapTx
resBridge := tokens.GetCrossChainBridge(!isSwapin)
txStatus := getSwapTxStatus(resBridge, swap)
if txStatus == nil || txStatus.BlockHeight == 0 {
if swap.SwapHeight == 0 {
return processUpdateSwapHeight(resBridge, swap)
}
return nil
}
if swap.SwapHeight != 0 {
if txStatus.Confirmations < *resBridge.GetChainConfig().Confirmations {
return nil
}
if swap.SwapTx != oldSwapTx {
_ = updateSwapResultTx(swap.TxID, swap.PairID, swap.Bind, swap.SwapTx, swap.SwapValue, isSwapin, mongodb.KeepStatus)
}
if txStatus.IsSwapTxOnChainAndFailed(resBridge.GetTokenConfig(swap.PairID)) {
logWorkerWarn("[stable]", "mark swap result failed with wrong status", "pairID", swap.PairID, "txid", swap.TxID, "bind", swap.Bind, "isSwapin", isSwapin, "swaptime", swap.Timestamp, "nowtime", now(), "confirmations", txStatus.Confirmations)
return markSwapResultFailed(swap.TxID, swap.PairID, swap.Bind, isSwapin)
}
return markSwapResultStable(swap.TxID, swap.PairID, swap.Bind, isSwapin)
}
return updateSwapResultHeight(swap, txStatus.BlockHeight, txStatus.BlockTime, swap.SwapTx != oldSwapTx)
}
func processUpdateSwapHeight(resBridge tokens.CrossChainBridge, swap *mongodb.MgoSwapResult) (err error) {
nonceSetter, ok := resBridge.(tokens.NonceSetter)
if !ok {
return nil
}
oldSwapTx := swap.SwapTx
blockHeight, blockTime := nonceSetter.GetTxBlockInfo(swap.SwapTx)
if blockHeight == 0 {
for i, oldSwapTx := range swap.OldSwapTxs {
if swap.SwapTx == oldSwapTx {
continue
}
blockHeight, blockTime = nonceSetter.GetTxBlockInfo(oldSwapTx)
if blockHeight > 0 {
swap.SwapTx = oldSwapTx
swap.SwapValue = swap.OldSwapVals[i]
break
}
}
}
if blockHeight == 0 && swap.SwapNonce > 0 {
return checkIfSwapNonceHasPassed(resBridge, swap, false)
}
return updateSwapResultHeight(swap, blockHeight, blockTime, swap.SwapTx != oldSwapTx)
}