Skip to content

Commit

Permalink
feat: support custom PoolTransaction errors (paradigmxyz#4237)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored and alessandromazza98 committed Aug 19, 2023
1 parent 2d43651 commit 31130c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion crates/rpc/rpc/src/eth/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jsonrpsee::{
use reth_primitives::{abi::decode_revert_reason, Address, Bytes, U256};
use reth_revm::tracing::js::JsInspectorError;
use reth_rpc_types::{error::EthRpcErrorCode, BlockError, CallInputError};
use reth_transaction_pool::error::{InvalidPoolTransactionError, PoolError};
use reth_transaction_pool::error::{InvalidPoolTransactionError, PoolError, PoolTransactionError};
use revm::primitives::{EVMError, ExecutionResult, Halt, OutOfGasError};
use std::time::Duration;

Expand Down Expand Up @@ -468,6 +468,9 @@ pub enum RpcPoolError {
ExceedsMaxInitCodeSize,
#[error(transparent)]
Invalid(#[from] RpcInvalidTransactionError),
/// Custom pool error
#[error("{0:?}")]
PoolTransactionError(Box<dyn PoolTransactionError>),
#[error(transparent)]
Other(Box<dyn std::error::Error + Send + Sync>),
}
Expand Down Expand Up @@ -505,6 +508,7 @@ impl From<InvalidPoolTransactionError> for RpcPoolError {
}
InvalidPoolTransactionError::OversizedData(_, _) => RpcPoolError::OversizedData,
InvalidPoolTransactionError::Underpriced => RpcPoolError::Underpriced,
InvalidPoolTransactionError::Other(err) => RpcPoolError::PoolTransactionError(err),
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion crates/transaction-pool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ use reth_primitives::{Address, InvalidTransactionError, TxHash};
/// Transaction pool result type.
pub type PoolResult<T> = Result<T, PoolError>;

/// A trait for additional errors that can be thrown by the transaction pool.
///
/// For example during validation
/// [TransactionValidator::validate_transaction](crate::validate::TransactionValidator::validate_transaction)
pub trait PoolTransactionError: std::error::Error + Send + Sync {
/// Returns `true` if the error was caused by a transaction that is considered bad in the
/// context of the transaction pool and warrants peer penalization.
///
/// See [PoolError::is_bad_transaction].
fn is_bad_transaction(&self) -> bool;
}

/// All errors the Transaction pool can throw.
#[derive(Debug, thiserror::Error)]
pub enum PoolError {
Expand Down Expand Up @@ -105,7 +117,7 @@ impl PoolError {
/// Represents errors that can happen when validating transactions for the pool
///
/// See [TransactionValidator](crate::TransactionValidator).
#[derive(Debug, Clone, thiserror::Error)]
#[derive(Debug, thiserror::Error)]
pub enum InvalidPoolTransactionError {
/// Hard consensus errors
#[error(transparent)]
Expand All @@ -126,6 +138,9 @@ pub enum InvalidPoolTransactionError {
/// Thrown if the transaction's fee is below the minimum fee
#[error("transaction underpriced")]
Underpriced,
/// Any other error that occurred while inserting/validating that is transaction specific
#[error("{0:?}")]
Other(Box<dyn PoolTransactionError>),
}

// === impl InvalidPoolTransactionError ===
Expand Down Expand Up @@ -178,6 +193,7 @@ impl InvalidPoolTransactionError {
// local setting
false
}
InvalidPoolTransactionError::Other(err) => err.is_bad_transaction(),
}
}
}

0 comments on commit 31130c0

Please sign in to comment.