From 45db5a6368018bb377832401d941383a09eed025 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 16 Aug 2023 21:02:51 +0200 Subject: [PATCH] feat: support custom PoolTransaction errors (#4237) --- crates/rpc/rpc/src/eth/error.rs | 6 +++++- crates/transaction-pool/src/error.rs | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/rpc/rpc/src/eth/error.rs b/crates/rpc/rpc/src/eth/error.rs index 3b3d179b48ae..d4c8813f5dd3 100644 --- a/crates/rpc/rpc/src/eth/error.rs +++ b/crates/rpc/rpc/src/eth/error.rs @@ -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; @@ -468,6 +468,9 @@ pub enum RpcPoolError { ExceedsMaxInitCodeSize, #[error(transparent)] Invalid(#[from] RpcInvalidTransactionError), + /// Custom pool error + #[error("{0:?}")] + PoolTransactionError(Box), #[error(transparent)] Other(Box), } @@ -505,6 +508,7 @@ impl From for RpcPoolError { } InvalidPoolTransactionError::OversizedData(_, _) => RpcPoolError::OversizedData, InvalidPoolTransactionError::Underpriced => RpcPoolError::Underpriced, + InvalidPoolTransactionError::Other(err) => RpcPoolError::PoolTransactionError(err), } } } diff --git a/crates/transaction-pool/src/error.rs b/crates/transaction-pool/src/error.rs index 12e0fec66240..75b2f66316f2 100644 --- a/crates/transaction-pool/src/error.rs +++ b/crates/transaction-pool/src/error.rs @@ -5,6 +5,18 @@ use reth_primitives::{Address, InvalidTransactionError, TxHash}; /// Transaction pool result type. pub type PoolResult = Result; +/// 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 { @@ -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)] @@ -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), } // === impl InvalidPoolTransactionError === @@ -178,6 +193,7 @@ impl InvalidPoolTransactionError { // local setting false } + InvalidPoolTransactionError::Other(err) => err.is_bad_transaction(), } } }