Commit 18d87e2a authored by Michael de Hoog's avatar Michael de Hoog

Avoid incrementing the txmgr nonce if tx signing fails

parent bc8a1da1
...@@ -238,23 +238,15 @@ func (m *SimpleTxManager) craftTx(ctx context.Context, candidate TxCandidate) (* ...@@ -238,23 +238,15 @@ func (m *SimpleTxManager) craftTx(ctx context.Context, candidate TxCandidate) (*
rawTx.Gas = gas rawTx.Gas = gas
} }
// Avoid bumping the nonce if the gas estimation fails. return m.signWithNextNonce(ctx, rawTx)
nonce, err := m.nextNonce(ctx)
if err != nil {
return nil, err
}
rawTx.Nonce = nonce
ctx, cancel := context.WithTimeout(ctx, m.cfg.NetworkTimeout)
defer cancel()
return m.cfg.Signer(ctx, m.cfg.From, types.NewTx(rawTx))
} }
// nextNonce returns a nonce to use for the next transaction. It uses // signWithNextNonce returns a signed transaction with the next available nonce.
// eth_getTransactionCount with "latest" once, and then subsequent calls simply // The nonce is fetched once using eth_getTransactionCount with "latest", and
// increment this number. If the transaction manager is reset, it will query the // then subsequent calls simply increment this number. If the transaction manager
// eth_getTransactionCount nonce again. // is reset, it will query the eth_getTransactionCount nonce again. If signing
func (m *SimpleTxManager) nextNonce(ctx context.Context) (uint64, error) { // fails, the nonce is not incremented.
func (m *SimpleTxManager) signWithNextNonce(ctx context.Context, rawTx *types.DynamicFeeTx) (*types.Transaction, error) {
m.nonceLock.Lock() m.nonceLock.Lock()
defer m.nonceLock.Unlock() defer m.nonceLock.Unlock()
...@@ -265,15 +257,23 @@ func (m *SimpleTxManager) nextNonce(ctx context.Context) (uint64, error) { ...@@ -265,15 +257,23 @@ func (m *SimpleTxManager) nextNonce(ctx context.Context) (uint64, error) {
nonce, err := m.backend.NonceAt(childCtx, m.cfg.From, nil) nonce, err := m.backend.NonceAt(childCtx, m.cfg.From, nil)
if err != nil { if err != nil {
m.metr.RPCError() m.metr.RPCError()
return 0, fmt.Errorf("failed to get nonce: %w", err) return nil, fmt.Errorf("failed to get nonce: %w", err)
} }
m.nonce = &nonce m.nonce = &nonce
} else { } else {
*m.nonce++ *m.nonce++
} }
m.metr.RecordNonce(*m.nonce) rawTx.Nonce = *m.nonce
return *m.nonce, nil tx, err := m.cfg.Signer(ctx, m.cfg.From, types.NewTx(rawTx))
if err != nil {
// decrement the nonce, so we can retry signing with the same nonce next time
// signWithNextNonce is called
*m.nonce--
} else {
m.metr.RecordNonce(*m.nonce)
}
return tx, err
} }
// resetNonce resets the internal nonce tracking. This is called if any pending send // resetNonce resets the internal nonce tracking. This is called if any pending send
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment