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

fix: improve nonce too low error #10711

Merged
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions crates/primitives/src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ pub enum InvalidTransactionError {
/// The nonce is lower than the account's nonce, or there is a nonce gap present.
///
/// This is a consensus error.
#[display("transaction nonce is not consistent")]
NonceNotConsistent,
#[display("transaction nonce is not consistent: next nonce {state}, tx nonce {tx}")]
NonceNotConsistent {
/// The nonce of the transaction.
tx: u64,
/// The current state of the nonce in the local chain.
state: u64,
},
/// The transaction is before Spurious Dragon and has a chain ID.
#[display("transactions before Spurious Dragon should not have a chain ID")]
OldLegacyChainId,
Expand Down
24 changes: 18 additions & 6 deletions crates/rpc/rpc-eth-types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use reth_transaction_pool::error::{
Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind,
PoolTransactionError,
};
use revm::primitives::{EVMError, ExecutionResult, HaltReason, OutOfGasError};
use revm::primitives::{EVMError, ExecutionResult, HaltReason, InvalidTransaction, OutOfGasError};
use revm_inspectors::tracing::MuxError;
use tracing::error;

Expand Down Expand Up @@ -236,7 +236,12 @@ where
{
fn from(err: EVMError<T>) -> Self {
match err {
EVMError::Transaction(err) => RpcInvalidTransactionError::from(err).into(),
EVMError::Transaction(invalid_tx) => match invalid_tx {
InvalidTransaction::NonceTooLow { tx, state } => {
Self::InvalidTransaction(RpcInvalidTransactionError::NonceTooLow { tx, state })
}
_ => RpcInvalidTransactionError::from(invalid_tx).into(),
},
EVMError::Header(InvalidHeader::PrevrandaoNotSet) => Self::PrevrandaoNotSet,
EVMError::Header(InvalidHeader::ExcessBlobGasNotSet) => Self::ExcessBlobGasNotSet,
EVMError::Database(err) => err.into(),
Expand All @@ -263,8 +268,13 @@ where
#[derive(thiserror::Error, Debug)]
pub enum RpcInvalidTransactionError {
/// returned if the nonce of a transaction is lower than the one present in the local chain.
#[error("nonce too low")]
NonceTooLow,
#[error("nonce too low: next nonce {state}, tx nonce {tx}")]
NonceTooLow {
/// The nonce of the transaction.
tx: u64,
/// The current state of the nonce in the local chain.
state: u64,
},
/// returned if the nonce of a transaction is higher than the next one expected based on the
/// local chain.
#[error("nonce too high")]
Expand Down Expand Up @@ -456,7 +466,7 @@ impl From<revm::primitives::InvalidTransaction> for RpcInvalidTransactionError {
InvalidTransaction::NonceOverflowInTransaction => Self::NonceMaxValue,
InvalidTransaction::CreateInitCodeSizeLimit => Self::MaxInitCodeSizeExceeded,
InvalidTransaction::NonceTooHigh { .. } => Self::NonceTooHigh,
InvalidTransaction::NonceTooLow { .. } => Self::NonceTooLow,
InvalidTransaction::NonceTooLow { tx, state } => Self::NonceTooLow { tx, state },
InvalidTransaction::AccessListNotSupported => Self::AccessListNotSupported,
InvalidTransaction::MaxFeePerBlobGasNotSupported => Self::MaxFeePerBlobGasNotSupported,
InvalidTransaction::BlobVersionedHashesNotSupported => {
Expand Down Expand Up @@ -494,7 +504,9 @@ impl From<reth_primitives::InvalidTransactionError> for RpcInvalidTransactionErr
// txpool (e.g. `eth_sendRawTransaction`) to their corresponding RPC
match err {
InvalidTransactionError::InsufficientFunds { .. } => Self::InsufficientFunds,
InvalidTransactionError::NonceNotConsistent => Self::NonceTooLow,
InvalidTransactionError::NonceNotConsistent { tx, state } => {
Self::NonceTooLow { tx, state }
}
InvalidTransactionError::OldLegacyChainId => {
// Note: this should be unreachable since Spurious Dragon now enabled
Self::OldLegacyChainId
Expand Down
4 changes: 2 additions & 2 deletions crates/transaction-pool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl InvalidPoolTransactionError {
// intentionally caused by the sender
match err {
InvalidTransactionError::InsufficientFunds { .. } |
InvalidTransactionError::NonceNotConsistent => {
InvalidTransactionError::NonceNotConsistent { .. } => {
// transaction could just have arrived late/early
false
}
Expand Down Expand Up @@ -294,7 +294,7 @@ impl InvalidPoolTransactionError {

/// Returns `true` if an import failed due to nonce gap.
pub const fn is_nonce_gap(&self) -> bool {
matches!(self, Self::Consensus(InvalidTransactionError::NonceNotConsistent)) ||
matches!(self, Self::Consensus(InvalidTransactionError::NonceNotConsistent { .. })) ||
matches!(self, Self::Eip4844(Eip4844PoolTransactionError::Eip4844NonceGap))
}
}
7 changes: 5 additions & 2 deletions crates/transaction-pool/src/validate/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,14 @@ where
}
}

let tx_nonce = transaction.nonce();

// Checks for nonce
if transaction.nonce() < account.nonce {
if tx_nonce < account.nonce {
return TransactionValidationOutcome::Invalid(
transaction,
InvalidTransactionError::NonceNotConsistent.into(),
InvalidTransactionError::NonceNotConsistent { tx: tx_nonce, state: account.nonce }
.into(),
)
}

Expand Down
Loading