Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change nonce calculation flow #80

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 20 additions & 55 deletions claimtxman/claimtxman.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package claimtxman

import (
"context"
"errors"
"fmt"
"math/big"
"sync"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/jackc/pgx/v4"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -455,18 +455,11 @@ func (tm *ClaimTxManager) addClaimTx(depositCount uint, from common.Address, to
log.Errorf("failed to estimate gas. Ignoring tx... Error: %v, data: %s", err, common.Bytes2Hex(data))
return nil
}
// get next nonce
nonce, err := tm.getNextNonce(from)
if err != nil {
err := fmt.Errorf("failed to get current nonce: %v", err)
log.Errorf("error getting next nonce. Error: %s", err.Error())
return err
}

// create monitored tx
mTx := ctmtypes.MonitoredTx{
DepositID: depositCount, From: from, To: to,
Nonce: nonce, Value: value, Data: data,
Value: value, Data: data,
Gas: gas, Status: ctmtypes.MonitoredTxStatusCreated,
}

Expand Down Expand Up @@ -507,9 +500,6 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {

isResetNonce := false // it will reset the nonce in one cycle
for _, mTx := range mTxs {
if isResetNonce {
break
}
mTx := mTx // force variable shadowing to avoid pointer conflicts
mTxLog := mLog.WithFields("monitoredTx", mTx.DepositID)
mTxLog.Infof("processing tx with nonce %d", mTx.Nonce)
Expand Down Expand Up @@ -558,10 +548,6 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {
_, _, err = tm.l2Node.TransactionByHash(ctx, txHash)
}
if errors.Is(err, ethereum.NotFound) {
_ = tm.ResetL2NodeNonce(&mTx)
if signedTx, err := tm.auth.Signer(mTx.From, mTx.Tx()); err == nil {
_ = tm.l2Node.SendTransaction(ctx, signedTx)
}
mTxLog.Error("maximum retries and the tx is still missing in the pool. TxHash: ", txHash.String())
hasFailedReceipts = true
continue
Expand All @@ -570,7 +556,7 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {
continue
}
}
log.Infof("tx: %s not mined yet", txHash.String())
mTxLog.Infof("tx: %s not mined yet", txHash.String())

allHistoryTxMined = false
continue
Expand Down Expand Up @@ -617,7 +603,7 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {
// Retrieve L1 transaction info
deposit, err := tm.storage.GetDeposit(ctx, mTx.DepositID, 0, nil)
if err != nil {
log.Errorf("push message: GetDeposit error: %v", err)
mTxLog.Errorf("push message: GetDeposit error: %v", err)
return
}
tm.pushTransactionUpdate(deposit, uint32(pb.TransactionStatus_TX_PENDING_USER_CLAIM))
Expand All @@ -634,7 +620,7 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {
// review the tx information
if hasFailedReceipts {
mTxLog.Infof("monitored tx needs to be updated")
err := tm.ReviewMonitoredTx(ctx, &mTx, true)
err := tm.ReviewMonitoredTx(ctx, &mTx)
if err != nil {
mTxLog.Errorf("failed to review monitored tx: %v", err)
continue
Expand All @@ -653,7 +639,14 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {

//Multiply gasPrice by 10 to increase the efficiency of the tx in the sequence
mTx.GasPrice = big.NewInt(0).Mul(gasPrice, big.NewInt(10)) //nolint:gomnd
log.Infof("Using gasPrice: %s. The gasPrice suggested by the network is %s", mTx.GasPrice.String(), gasPrice.String())
mTxLog.Infof("Using gasPrice: %s. The gasPrice suggested by the network is %s", mTx.GasPrice.String(), gasPrice.String())

// Calculate nonce before signing
err = tm.setTxNonce(&mTx)
if err != nil {
mTxLog.Errorf("failed to set tx nonce: %v", err)
continue
}

// rebuild transaction
tx := mTx.Tx()
Expand All @@ -666,7 +659,7 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {
mTxLog.Errorf("failed to sign tx %v created from monitored tx: %v", tx.Hash().String(), err)
continue
}
mTxLog.Debugf("signed tx %v created using gasPrice: %s", signedTx.Hash().String(), signedTx.GasPrice().String())
mTxLog.Debugf("signed tx %v created using gasPrice: %s, nonce: %v", signedTx.Hash().String(), signedTx.GasPrice().String(), signedTx.Nonce())

// add tx to monitored tx history
err = mTx.AddHistory(signedTx)
Expand All @@ -683,28 +676,17 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {
err := tm.l2Node.SendTransaction(ctx, signedTx)
if err != nil {
mTxLog.Errorf("failed to send tx %s to network: %v", signedTx.Hash().String(), err)
var reviewNonce bool
if err.Error() == pool.ErrNonceTooLow.Error() {
mTxLog.Infof("nonce error detected, Nonce used: %d", signedTx.Nonce())
if !isResetNonce {
isResetNonce = true
tm.nonceCache.Remove(mTx.From.Hex())
mTxLog.Infof("nonce cache cleared for address %v", mTx.From.Hex())
}
reviewNonce = true
} else if err.Error() == pool.ErrNonceTooHigh.Error() {
if !isResetNonce {
isResetNonce = true
_ = tm.ResetL2NodeNonce(&mTx)
mTxLog.Infof("nonce ResetL2NodeNonce %v", mTx.From.Hex())
if signedTx, err := tm.auth.Signer(mTx.From, mTx.Tx()); err == nil {
_ = tm.l2Node.SendTransaction(ctx, signedTx)
}
}
}
mTx.RemoveHistory(signedTx)
// we should rebuild the monitored tx to fix the nonce
err := tm.ReviewMonitoredTx(ctx, &mTx, reviewNonce)
err := tm.ReviewMonitoredTx(ctx, &mTx)
if err != nil {
mTxLog.Errorf("failed to review monitored tx: %v", err)
}
Expand Down Expand Up @@ -741,22 +723,20 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error {
return nil
}

func (tm *ClaimTxManager) ResetL2NodeNonce(mTx *ctmtypes.MonitoredTx) error {
mTxLog := log.WithFields("monitoredTx", mTx.DepositID)
mTxLog.Debug("ResetL2NodeNonce")
nonce, err := tm.l2Node.NonceAt(tm.ctx, mTx.From, nil)
// setTxNonce get the next nonce from the nonce cache and set it to the tx
func (tm *ClaimTxManager) setTxNonce(mTx *ctmtypes.MonitoredTx) error {
nonce, err := tm.getNextNonce(mTx.From)
if err != nil {
return err
return errors.Wrap(err, "getNextNonce err")
}
mTxLog.Debugf("ResetL2NodeNonce mtxNonce:%d, new nonce:%d", mTx.Nonce, nonce)
mTx.Nonce = nonce
return nil
}

// ReviewMonitoredTx checks if tx needs to be updated
// accordingly to the current information stored and the current
// state of the blockchain
func (tm *ClaimTxManager) ReviewMonitoredTx(ctx context.Context, mTx *ctmtypes.MonitoredTx, reviewNonce bool) error {
func (tm *ClaimTxManager) ReviewMonitoredTx(ctx context.Context, mTx *ctmtypes.MonitoredTx) error {
mTxLog := log.WithFields("monitoredTx", mTx.DepositID)
mTxLog.Debug("reviewing")
// get gas
Expand Down Expand Up @@ -784,21 +764,6 @@ func (tm *ClaimTxManager) ReviewMonitoredTx(ctx context.Context, mTx *ctmtypes.M
mTx.Gas = gas
}

if reviewNonce {
// check nonce
nonce, err := tm.getNextNonce(mTx.From)
if err != nil {
err := fmt.Errorf("failed to get nonce: %w", err)
mTxLog.Errorf(err.Error())
return err
}
mTxLog.Infof("monitored tx nonce from %v to %v", mTx.Nonce, nonce)
if nonce != mTx.Nonce {
mTxLog.Infof("monitored tx nonce updated from %v to %v", mTx.Nonce, nonce)
mTx.Nonce = nonce
}
}

return nil
}

Expand Down
Loading