From 57a6f173c460ac3da20ed3e204bc58025e7532e7 Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Tue, 20 Feb 2024 15:25:41 +0800 Subject: [PATCH 1/4] Change nonce calculation flow --- claimtxman/claimtxman.go | 84 +++++++++++++++------------------------- 1 file changed, 32 insertions(+), 52 deletions(-) diff --git a/claimtxman/claimtxman.go b/claimtxman/claimtxman.go index 3edf52e7..45caae65 100644 --- a/claimtxman/claimtxman.go +++ b/claimtxman/claimtxman.go @@ -2,7 +2,6 @@ package claimtxman import ( "context" - "errors" "fmt" "math/big" "sync" @@ -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 ( @@ -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, } @@ -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) @@ -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 @@ -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 @@ -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() @@ -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) @@ -683,7 +676,6 @@ 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 { @@ -691,20 +683,10 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error { 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) } @@ -741,14 +723,12 @@ 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 } @@ -756,7 +736,7 @@ func (tm *ClaimTxManager) ResetL2NodeNonce(mTx *ctmtypes.MonitoredTx) error { // 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 @@ -784,20 +764,20 @@ 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 - } - } + //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 } From aee0ffc9d74cc9cfb956d8bf061c30b43e380d5d Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Tue, 20 Feb 2024 15:54:39 +0800 Subject: [PATCH 2/4] Remove comments --- claimtxman/claimtxman.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/claimtxman/claimtxman.go b/claimtxman/claimtxman.go index 45caae65..a285d5a4 100644 --- a/claimtxman/claimtxman.go +++ b/claimtxman/claimtxman.go @@ -764,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 } From 03220621803932a13d8df621926b5d12fcc120cd Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Tue, 20 Feb 2024 16:13:16 +0800 Subject: [PATCH 3/4] Code review update --- claimtxman/claimtxman.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/claimtxman/claimtxman.go b/claimtxman/claimtxman.go index a285d5a4..16428e4a 100644 --- a/claimtxman/claimtxman.go +++ b/claimtxman/claimtxman.go @@ -556,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 @@ -603,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)) @@ -642,10 +642,12 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error { 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 + if mTx.Nonce <= 0 { + err = tm.setTxNonce(&mTx) + if err != nil { + mTxLog.Errorf("failed to set tx nonce: %v", err) + continue + } } // rebuild transaction From d07de47cbef264793c2a977e60772e855696d3a0 Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Tue, 20 Feb 2024 16:42:24 +0800 Subject: [PATCH 4/4] Remove DB nonce check --- claimtxman/claimtxman.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/claimtxman/claimtxman.go b/claimtxman/claimtxman.go index 16428e4a..0da2a82b 100644 --- a/claimtxman/claimtxman.go +++ b/claimtxman/claimtxman.go @@ -642,12 +642,10 @@ func (tm *ClaimTxManager) monitorTxs(ctx context.Context) error { mTxLog.Infof("Using gasPrice: %s. The gasPrice suggested by the network is %s", mTx.GasPrice.String(), gasPrice.String()) // Calculate nonce before signing - if mTx.Nonce <= 0 { - err = tm.setTxNonce(&mTx) - if err != nil { - mTxLog.Errorf("failed to set tx nonce: %v", err) - continue - } + err = tm.setTxNonce(&mTx) + if err != nil { + mTxLog.Errorf("failed to set tx nonce: %v", err) + continue } // rebuild transaction