Skip to content

Commit

Permalink
Merge branch 'feature-fix-lost-tx' of https://github.com/LATOKEN/rela…
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel.soloviev committed Oct 10, 2022
2 parents 08af407 + ae029c4 commit 0f78fef
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/service/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func (r *RelayerSRV) emitChainSendClaim() {
for {
swaps := r.storage.GetSwapsByTypeAndStatuses(
[]storage.SwapStatus{storage.SwapStatusDepositConfirmed, storage.SwapStatusClaimConfirmed, storage.SwapStatusDepositInit, storage.SwapStatusClaimSentFailed})
[]storage.SwapStatus{storage.SwapStatusDepositConfirmed, storage.SwapStatusClaimConfirmed, storage.SwapStatusDepositInit, storage.SwapStatusClaimSentFailed, storage.SwapStatusClaimSent})
for _, swap := range swaps {
if swap.Status == storage.SwapStatusDepositConfirmed {
r.logger.Info("attempting to send claim for swap")
Expand Down Expand Up @@ -98,7 +98,7 @@ func (r *RelayerSRV) sendClaim(worker workers.IWorker, swap *storage.Swap) (stri
r.logger.Infof("claim parameters: depositNonce(%d) | sender(%s) | outAmount(%d) | resourceID(%s)\n",
swap.DepositNonce, swap.SenderAddr, amount, swap.ResourceID)

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

r.logger.Infof("send claim tx success | chain=%s, swap_ID=%s, tx_hash=%s", worker.GetChainName(),
Expand Down
2 changes: 1 addition & 1 deletion src/service/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (r *RelayerSRV) CheckTxSent(worker workers.IWorker) {

for _, txSent := range txsSent {
// Get status of tx from chain
status := worker.GetSentTxStatus(txSent.TxHash)
status := worker.GetSentTxStatus(txSent.TxHash, txSent.Nonce)
if err := r.storage.UpdateTxSentStatus(txSent, status); err != nil {
r.logger.WithFields(logrus.Fields{"function": "CheckTxSent() | UpdateTxSentStatus()"}).Errorln(err)
return
Expand Down
1 change: 1 addition & 0 deletions src/service/storage/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type TxSent struct {
TxHash string `json:"tx_hash" gorm:"type:TEXT"`
ErrMsg string `json:"err_msg" gorm:"type:TEXT"`
Status TxStatus `json:"status" gorm:"type:tx_statuses"`
Nonce string `json:"nonce" gorm:"type:TEXT"`
CreateTime int64 `json:"create_time" gorm:"type:BIGINT"`
UpdateTime int64 `json:"update_time" gorm:"type:BIGINT"`
}
Expand Down
32 changes: 21 additions & 11 deletions src/service/workers/eth-compatible/erc20-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -222,31 +223,31 @@ func (w *Erc20Worker) GetHeight() (int64, error) {
}

// Vote ...
func (w *Erc20Worker) Vote(depositNonce uint64, originchainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, error) {
func (w *Erc20Worker) Vote(depositNonce uint64, originchainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, string, error) {
auth, err := w.getTransactor()
if err != nil {
return "", err
return "", "", err
}

instance, err := labr.NewLabr(w.swapContractAddr, w.client)
if err != nil {
return "", err
return "", "", err
}

value, _ := new(big.Int).SetString(amount, 10)
tx, err := instance.VoteProposal(auth, originchainID, destinationChainID, depositNonce, resourceID, common.HexToAddress(receiptAddr), value)
if err != nil {
return "", err
return "", "", err
}

return tx.Hash().String(), nil
return tx.Hash().String(), auth.Nonce.String(), nil
}

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

resp, err := rpcClient.Call("eth_getTransactionCount", w.config.WorkerAddr.Hex(), "pending")
resp, err := rpcClient.Call("eth_getTransactionCount", w.config.WorkerAddr.Hex(), "latest")
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -301,17 +302,26 @@ func (w *Erc20Worker) GetWorkerAddress() string {
}

// GetSentTxStatus ...
func (w *Erc20Worker) GetSentTxStatus(hash string) storage.TxStatus {
func (w *Erc20Worker) GetSentTxStatus(hash string, nonce string) storage.TxStatus {
txReceipt, err := w.client.TransactionReceipt(context.Background(), common.HexToHash(hash))
if err != nil {
_, isPending, err := w.client.TransactionByHash(context.Background(), common.HexToHash(hash))
txNonce, err := strconv.ParseUint(nonce, 10, 64)
if err != nil {
if err == ethereum.NotFound {
return storage.TxSentStatusLost
_, isPending, err := w.client.TransactionByHash(context.Background(), common.HexToHash(hash))
if err != nil {
if err == ethereum.NotFound {
return storage.TxSentStatusLost
}
return storage.TxSentStatusNotFound
}
if isPending {
return storage.TxSentStatusPending
}
return storage.TxSentStatusNotFound
}
if isPending {

txCount, _ := w.GetTxCountLatest()
if txNonce >= txCount {
return storage.TxSentStatusPending
}
return storage.TxSentStatusNotFound
Expand Down
4 changes: 2 additions & 2 deletions src/service/workers/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type IWorker interface {
// // GetColdWalletAddress returns the address of the relayer's cold wallet
// GetColdWalletAddress() string
// GetSentTxStatus returns status of tx sent
GetSentTxStatus(hash string) storage.TxStatus
GetSentTxStatus(hash string, nonce string) storage.TxStatus
// // GetBalance returns balance of swap token for any address
// GetBalance(address, tokenSymbol string) (*big.Int, error)
// // GetStatus returns status of relayer account(balance eg)
Expand All @@ -54,5 +54,5 @@ type IWorker interface {
// CreateRequest sends wrapped tokens tx
// CreateRequest(swapID common.Hash) (string, error)
// Vote
Vote(depositNonce uint64, originChainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, error)
Vote(depositNonce uint64, originChainID [8]byte, destinationChainID [8]byte, resourceID [32]byte, receiptAddr string, amount string) (string, string, error)
}

0 comments on commit 0f78fef

Please sign in to comment.