Skip to content

Commit

Permalink
core/node/crypto: refetch tx nonce in case chain rpc node reports non…
Browse files Browse the repository at this point in the history
…ce issues
  • Loading branch information
bas-vk committed Sep 10, 2024
1 parent 8bbec46 commit f07e992
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions core/node/crypto/chain_txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"math/big"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -507,17 +508,24 @@ func (r *transactionPool) Submit(
name string,
createTx CreateTransaction,
) (TransactionPoolPendingTransaction, error) {
var span trace.Span
// lock to prevent tx.Nonce collisions
r.mu.Lock()
defer r.mu.Unlock()

return r.submitNoLock(ctx, name, createTx, true)
}
func (r *transactionPool) submitNoLock(
ctx context.Context,
name string,
createTx CreateTransaction,
canRetry bool,
) (TransactionPoolPendingTransaction, error) {
var span trace.Span
if r.tracer != nil {
ctx, span = r.tracer.Start(ctx, "txpool_submit")
defer span.End()
}

// lock to prevent tx.Nonce collisions
r.mu.Lock()
defer r.mu.Unlock()

nonce, err := r.nextNonce(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -548,6 +556,12 @@ func (r *transactionPool) Submit(
}

if err := r.client.SendTransaction(ctx, tx); err != nil {
// in case a tx could not be submitted due to a nonce issue recover from it by dropping the internal cached
// last tx that is used to determine the next tx nonce.
if canRetry && strings.Contains(strings.ToLower(err.Error()), "nonce") {
r.lastPendingTx = nil
return r.submitNoLock(ctx, name, createTx, false)
}
return nil, err
}

Expand Down

0 comments on commit f07e992

Please sign in to comment.