Skip to content

Commit de2fe4e

Browse files
authored
Merge pull request #41 from LATOKEN/feature-fix-lost-tx
Added nonce check
2 parents 08af407 + 5e27090 commit de2fe4e

6 files changed

Lines changed: 28 additions & 30 deletions

File tree

src/service/claim.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func (r *RelayerSRV) emitChainSendClaim() {
1515
for {
1616
swaps := r.storage.GetSwapsByTypeAndStatuses(
17-
[]storage.SwapStatus{storage.SwapStatusDepositConfirmed, storage.SwapStatusClaimConfirmed, storage.SwapStatusDepositInit, storage.SwapStatusClaimSentFailed})
17+
[]storage.SwapStatus{storage.SwapStatusDepositConfirmed, storage.SwapStatusClaimConfirmed, storage.SwapStatusDepositInit, storage.SwapStatusClaimSentFailed, storage.SwapStatusClaimSent})
1818
for _, swap := range swaps {
1919
if swap.Status == storage.SwapStatusDepositConfirmed {
2020
r.logger.Info("attempting to send claim for swap")
@@ -98,7 +98,7 @@ func (r *RelayerSRV) sendClaim(worker workers.IWorker, swap *storage.Swap) (stri
9898
r.logger.Infof("claim parameters: depositNonce(%d) | sender(%s) | outAmount(%d) | resourceID(%s)\n",
9999
swap.DepositNonce, swap.SenderAddr, amount, swap.ResourceID)
100100

101-
txHash, err := worker.Vote(swap.DepositNonce, utils.StringToBytes8(swap.OriginChainID), utils.StringToBytes8(swap.DestinationChainID),
101+
txHash, nonce, err := worker.Vote(swap.DepositNonce, utils.StringToBytes8(swap.OriginChainID), utils.StringToBytes8(swap.DestinationChainID),
102102
utils.StringToBytes32(swap.ResourceID), swap.ReceiverAddr, amount)
103103
if err != nil {
104104
txSent.ErrMsg = err.Error()
@@ -107,6 +107,7 @@ func (r *RelayerSRV) sendClaim(worker workers.IWorker, swap *storage.Swap) (stri
107107
return "", fmt.Errorf("could not send claim tx: %w", err)
108108
}
109109
txSent.TxHash = txHash
110+
txSent.Nonce = nonce
110111
r.storage.UpdateSwapStatus(swap, storage.SwapStatusClaimSent, "")
111112

112113
r.logger.Infof("send claim tx success | chain=%s, swap_ID=%s, tx_hash=%s", worker.GetChainName(),

src/service/relayer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (r *RelayerSRV) CheckTxSent(worker workers.IWorker) {
195195

196196
for _, txSent := range txsSent {
197197
// Get status of tx from chain
198-
status := worker.GetSentTxStatus(txSent.TxHash)
198+
status := worker.GetSentTxStatus(txSent.TxHash, txSent.Nonce)
199199
if err := r.storage.UpdateTxSentStatus(txSent, status); err != nil {
200200
r.logger.WithFields(logrus.Fields{"function": "CheckTxSent() | UpdateTxSentStatus()"}).Errorln(err)
201201
return
@@ -240,5 +240,5 @@ func (r *RelayerSRV) getAutoRetryConfig(chain string) (int64, int) {
240240
// autoRetryNum = r.Config.ChainConfig.WorkerChainAutoRetryNum
241241
// }
242242

243-
return 10, 10
243+
return 120, 2
244244
}

src/service/storage/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type TxSent struct {
7878
TxHash string `json:"tx_hash" gorm:"type:TEXT"`
7979
ErrMsg string `json:"err_msg" gorm:"type:TEXT"`
8080
Status TxStatus `json:"status" gorm:"type:tx_statuses"`
81+
Nonce uint64 `json:"nonce" gorm:"type:UINT"`
8182
CreateTime int64 `json:"create_time" gorm:"type:BIGINT"`
8283
UpdateTime int64 `json:"update_time" gorm:"type:BIGINT"`
8384
}

src/service/storage/swap.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,6 @@ func (d *DataBase) GetSwapByTxHash(txHash string) (*Swap, error) {
5555
return swap, nil
5656
}
5757

58-
// GetSwapByTxHash ...
59-
func (d *DataBase) GetSwapByTxHash(txHash string) (*Swap, error) {
60-
swap := &Swap{}
61-
if err := d.db.Where("tx_hash = ? and status in (?)", txHash,
62-
[]SwapStatus{SwapStatusDepositConfirmed, SwapStatusClaimSent, SwapStatusClaimConfirmed, SwapStatusClaimSentFailed, SwapStatusDepositFailed, SwapStatusPassedConfirmed, SwapStatusPassedSent, SwapStatusSpendSent, SwapStatusSpendConfirmed, SwapStatusRejected}).
63-
Find(&swap).Error; err != nil {
64-
return nil, err
65-
}
66-
67-
return swap, nil
68-
}
69-
7058
// UpdateSwapStatus ...
7159
func (d *DataBase) UpdateSwapStatus(swap *Swap, status SwapStatus, rOutAmount string) {
7260
swap.Status = status

src/service/workers/eth-compatible/erc20-worker.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,31 +222,31 @@ func (w *Erc20Worker) GetHeight() (int64, error) {
222222
}
223223

224224
// Vote ...
225-
func (w *Erc20Worker) Vote(depositNonce uint64, originchainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, error) {
225+
func (w *Erc20Worker) Vote(depositNonce uint64, originchainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, uint64, error) {
226226
auth, err := w.getTransactor()
227227
if err != nil {
228-
return "", err
228+
return "", 0, err
229229
}
230230

231231
instance, err := labr.NewLabr(w.swapContractAddr, w.client)
232232
if err != nil {
233-
return "", err
233+
return "", 0, err
234234
}
235235

236236
value, _ := new(big.Int).SetString(amount, 10)
237237
tx, err := instance.VoteProposal(auth, originchainID, destinationChainID, depositNonce, resourceID, common.HexToAddress(receiptAddr), value)
238238
if err != nil {
239-
return "", err
239+
return "", 0, err
240240
}
241241

242-
return tx.Hash().String(), nil
242+
return tx.Hash().String(), auth.Nonce.Uint64(), nil
243243
}
244244

245245
func (w *Erc20Worker) GetTxCountLatest() (uint64, error) {
246246
var result hexutil.Uint64
247247
rpcClient := jsonrpc.NewClient(w.provider)
248248

249-
resp, err := rpcClient.Call("eth_getTransactionCount", w.config.WorkerAddr.Hex(), "pending")
249+
resp, err := rpcClient.Call("eth_getTransactionCount", w.config.WorkerAddr.Hex(), "latest")
250250
if err != nil {
251251
return 0, err
252252
}
@@ -301,17 +301,25 @@ func (w *Erc20Worker) GetWorkerAddress() string {
301301
}
302302

303303
// GetSentTxStatus ...
304-
func (w *Erc20Worker) GetSentTxStatus(hash string) storage.TxStatus {
304+
func (w *Erc20Worker) GetSentTxStatus(hash string, nonce uint64) storage.TxStatus {
305305
txReceipt, err := w.client.TransactionReceipt(context.Background(), common.HexToHash(hash))
306306
if err != nil {
307-
_, isPending, err := w.client.TransactionByHash(context.Background(), common.HexToHash(hash))
308-
if err != nil {
309-
if err == ethereum.NotFound {
310-
return storage.TxSentStatusLost
307+
if nonce == 0 {
308+
_, isPending, err := w.client.TransactionByHash(context.Background(), common.HexToHash(hash))
309+
if err != nil {
310+
if err == ethereum.NotFound {
311+
return storage.TxSentStatusLost
312+
}
313+
return storage.TxSentStatusNotFound
314+
}
315+
if isPending {
316+
return storage.TxSentStatusPending
311317
}
312318
return storage.TxSentStatusNotFound
313319
}
314-
if isPending {
320+
321+
txCount, _ := w.GetTxCountLatest()
322+
if nonce >= txCount {
315323
return storage.TxSentStatusPending
316324
}
317325
return storage.TxSentStatusNotFound

src/service/workers/worker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type IWorker interface {
3131
// // GetColdWalletAddress returns the address of the relayer's cold wallet
3232
// GetColdWalletAddress() string
3333
// GetSentTxStatus returns status of tx sent
34-
GetSentTxStatus(hash string) storage.TxStatus
34+
GetSentTxStatus(hash string, nonce uint64) storage.TxStatus
3535
// // GetBalance returns balance of swap token for any address
3636
// GetBalance(address, tokenSymbol string) (*big.Int, error)
3737
// // GetStatus returns status of relayer account(balance eg)
@@ -54,5 +54,5 @@ type IWorker interface {
5454
// CreateRequest sends wrapped tokens tx
5555
// CreateRequest(swapID common.Hash) (string, error)
5656
// Vote
57-
Vote(depositNonce uint64, originChainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, error)
57+
Vote(depositNonce uint64, originChainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, uint64, error)
5858
}

0 commit comments

Comments
 (0)