-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathadminapi.go
241 lines (214 loc) · 7.65 KB
/
adminapi.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
package mongodb
import (
"errors"
"fmt"
"strings"
"time"
"github.com/anyswap/CrossChain-Bridge/log"
"github.com/anyswap/CrossChain-Bridge/tokens"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
const (
minTimeIntervalToReswap = int64(300) // seconds
)
// --------------- blacklist --------------------------------
func getBlacklistKey(address, pairID string) string {
return strings.ToLower(address + ":" + pairID)
}
// AddToBlacklist add to blacklist
func AddToBlacklist(address, pairID string) error {
mb := &MgoBlackAccount{
Key: getBlacklistKey(address, pairID),
Address: strings.ToLower(address),
PairID: strings.ToLower(pairID),
Timestamp: time.Now().Unix(),
}
_, err := collBlacklist.InsertOne(clientCtx, mb)
if err == nil {
log.Info("mongodb add to black list success", "address", address, "pairID", pairID)
} else {
log.Info("mongodb add to black list failed", "address", address, "pairID", pairID, "err", err)
}
return mgoError(err)
}
// RemoveFromBlacklist remove from blacklist
func RemoveFromBlacklist(address, pairID string) error {
_, err := collBlacklist.DeleteOne(clientCtx, bson.M{"_id": getBlacklistKey(address, pairID)})
if err == nil {
log.Info("mongodb remove from black list success", "address", address, "pairID", pairID)
} else {
log.Info("mongodb remove from black list failed", "address", address, "pairID", pairID, "err", err)
}
return mgoError(err)
}
// QueryBlacklist query if is blacked
func QueryBlacklist(address, pairID string) (isBlacked bool, err error) {
var result MgoBlackAccount
err = collBlacklist.FindOne(clientCtx, bson.M{"_id": getBlacklistKey(address, pairID)}).Decode(&result)
if err == nil {
return true, nil
}
if errors.Is(err, mongo.ErrNoDocuments) {
return false, nil
}
return false, err
}
// PassSwapinBigValue pass swapin big value
func PassSwapinBigValue(txid, pairID, bind string) error {
return passBigValue(txid, pairID, bind, true)
}
// PassSwapoutBigValue pass swapout big value
func PassSwapoutBigValue(txid, pairID, bind string) error {
return passBigValue(txid, pairID, bind, false)
}
func passBigValue(txid, pairID, bind string, isSwapin bool) error {
swap, err := FindSwap(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
res, err := FindSwapResult(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
if swap.Status != TxWithBigValue && res.Status != TxWithBigValue {
return fmt.Errorf("swap status is (%v, %v), not big value status %v", swap.Status.String(), res.Status.String(), TxWithBigValue.String())
}
if res.SwapTx != "" || res.SwapHeight != 0 || len(res.OldSwapTxs) > 0 {
return fmt.Errorf("already swapped with swaptx %v", res.SwapTx)
}
err = UpdateSwapResultStatus(isSwapin, txid, pairID, bind, MatchTxEmpty, time.Now().Unix(), "")
if err != nil {
return err
}
return UpdateSwapStatus(isSwapin, txid, pairID, bind, TxNotSwapped, time.Now().Unix(), "")
}
// ReverifySwapin reverify swapin
func ReverifySwapin(txid, pairID, bind string) error {
return reverifySwap(txid, pairID, bind, true)
}
// ReverifySwapout reverify swapout
func ReverifySwapout(txid, pairID, bind string) error {
return reverifySwap(txid, pairID, bind, false)
}
func reverifySwap(txid, pairID, bind string, isSwapin bool) error {
swap, err := FindSwap(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
if !swap.Status.CanReverify() {
return fmt.Errorf("swap status is %v, no need to reverify", swap.Status.String())
}
return UpdateSwapStatus(isSwapin, txid, pairID, bind, TxNotStable, time.Now().Unix(), "")
}
// Reswapin reswapin
func Reswapin(txid, pairID, bind string) error {
return reswap(txid, pairID, bind, true)
}
// Reswapout reswapout
func Reswapout(txid, pairID, bind string) error {
return reswap(txid, pairID, bind, false)
}
func reswap(txid, pairID, bind string, isSwapin bool) error {
swap, err := FindSwap(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
if !swap.Status.CanReswap() {
return fmt.Errorf("swap status is %v, can not reswap", swap.Status.String())
}
swapResult, err := FindSwapResult(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
err = checkCanReswap(swapResult, isSwapin)
if err != nil {
return err
}
log.Info("[reswap] update status to TxNotSwapped to retry", "txid", txid, "pairID", pairID, "bind", bind, "swaptx", swapResult.SwapTx)
err = UpdateSwapResultStatus(isSwapin, txid, pairID, bind, Reswapping, time.Now().Unix(), "")
if err != nil {
return err
}
return UpdateSwapStatus(isSwapin, txid, pairID, bind, TxNotSwapped, time.Now().Unix(), "")
}
func checkCanReswap(res *MgoSwapResult, isSwapin bool) error {
swapType := tokens.SwapType(res.SwapType)
if (isSwapin && swapType != tokens.SwapinType) || (!isSwapin && swapType != tokens.SwapoutType) {
return fmt.Errorf("wrong swap type %v (isSwapin=%v)", swapType.String(), isSwapin)
}
if res.Status != MatchTxFailed {
return fmt.Errorf("swap result status is %v, can not reswap", res.Status.String())
}
if res.SwapTx == "" {
return errors.New("swap without swaptx")
}
bridge := tokens.GetCrossChainBridge(!isSwapin)
txStatus, txHash := getSwapResultsTxStatus(bridge, res)
if txStatus != nil && txStatus.BlockHeight > 0 &&
!txStatus.IsSwapTxOnChainAndFailed(bridge.GetTokenConfig(res.PairID)) {
_ = UpdateSwapResultStatus(isSwapin, res.TxID, res.PairID, res.Bind, MatchTxNotStable, time.Now().Unix(), "")
return fmt.Errorf("swap succeed with swaptx %v", txHash)
}
return checkReswapNonce(bridge, res)
}
func checkReswapNonce(bridge tokens.CrossChainBridge, res *MgoSwapResult) (err error) {
nonceSetter, ok := bridge.(tokens.NonceSetter)
if !ok {
return nil
}
tokenCfg := bridge.GetTokenConfig(res.PairID)
if tokenCfg == nil {
return fmt.Errorf("no token config for pairID '%v'", res.PairID)
}
// eth enhanced, if we fail at nonce a, we should retry after nonce a
// to ensure tx with nonce a is on blockchain to prevent double swapping
var nonce uint64
retryGetNonceCount := 3
for i := 0; i < retryGetNonceCount; i++ {
nonce, err = nonceSetter.GetPoolNonce(tokenCfg.DcrmAddress, "latest")
if err == nil {
break
}
log.Warn("get account nonce failed", "address", tokenCfg.DcrmAddress)
time.Sleep(time.Second)
}
if nonce <= res.SwapNonce {
return errors.New("can not reswap with lower nonce")
}
if res.Timestamp+minTimeIntervalToReswap > time.Now().Unix() {
return errors.New("can not reswap in too short interval")
}
return nil
}
// ManualManageSwap manual manage swap
func ManualManageSwap(txid, pairID, bind, memo string, isSwapin, isPass bool) error {
swap, err := FindSwap(isSwapin, txid, pairID, bind)
if err != nil {
return err
}
if isPass {
if swap.Status == TxWithBigValue {
return passBigValue(txid, pairID, bind, isSwapin)
}
if swap.Status.CanReverify() || swap.Status == ManualMakeFail {
return UpdateSwapStatus(isSwapin, txid, pairID, bind, TxNotStable, time.Now().Unix(), memo)
}
} else if swap.Status.CanManualMakeFail() {
_ = UpdateSwapResultStatus(isSwapin, txid, pairID, bind, ManualMakeFail, time.Now().Unix(), memo)
return UpdateSwapStatus(isSwapin, txid, pairID, bind, ManualMakeFail, time.Now().Unix(), memo)
}
return fmt.Errorf("swap status is %v, can not operate. txid=%v pairID=%v bind=%v isSwapin=%v isPass=%v", swap.Status.String(), txid, pairID, bind, isSwapin, isPass)
}
func getSwapResultsTxStatus(bridge tokens.CrossChainBridge, res *MgoSwapResult) (status *tokens.TxStatus, txHash string) {
var err error
if status, err = bridge.GetTransactionStatus(res.SwapTx); err == nil {
return status, res.SwapTx
}
for _, tx := range res.OldSwapTxs {
if status, err = bridge.GetTransactionStatus(tx); err == nil {
return status, tx
}
}
return nil, ""
}