diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 8cb44661b83..812fee1a0d6 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -492,13 +492,11 @@ mod tests { let expected_blob_gas_used = 10 * DATA_GAS_PER_BLOB; // validate blob, it should fail blob gas used validation - assert_eq!( - validate_block_pre_execution(&block, &chain_spec), - Err(ConsensusError::BlobGasUsedDiff(GotExpected { - got: 1, - expected: expected_blob_gas_used - })) - ); + assert!(matches!( + validate_block_pre_execution(&block, &chain_spec).unwrap_err(), + ConsensusError::BlobGasUsedDiff(diff) + if diff.got == 1 && diff.expected == expected_blob_gas_used + )); } #[test] @@ -509,10 +507,10 @@ mod tests { // Test exceeding default - should fail let header_33 = Header { extra_data: Bytes::from(vec![0; 33]), ..Default::default() }; - assert_eq!( - validate_header_extra_data(&header_33, 32), - Err(ConsensusError::ExtraDataExceedsMax { len: 33 }) - ); + assert!(matches!( + validate_header_extra_data(&header_33, 32).unwrap_err(), + ConsensusError::ExtraDataExceedsMax { len } if len == 33 + )); // Test with custom larger limit - should pass assert!(validate_header_extra_data(&header_33, 64).is_ok()); diff --git a/crates/consensus/consensus/src/lib.rs b/crates/consensus/consensus/src/lib.rs index e714b614090..5a937134880 100644 --- a/crates/consensus/consensus/src/lib.rs +++ b/crates/consensus/consensus/src/lib.rs @@ -11,9 +11,10 @@ extern crate alloc; -use alloc::{boxed::Box, fmt::Debug, string::String, vec::Vec}; +use alloc::{boxed::Box, fmt::Debug, string::String, sync::Arc, vec::Vec}; use alloy_consensus::Header; use alloy_primitives::{BlockHash, BlockNumber, Bloom, B256}; +use core::error::Error; use reth_execution_types::BlockExecutionResult; use reth_primitives_traits::{ constants::{GAS_LIMIT_BOUND_DIVISOR, MAXIMUM_GAS_LIMIT_BLOCK, MINIMUM_GAS_LIMIT}, @@ -125,7 +126,7 @@ pub trait HeaderValidator: Debug + Send + Sync { } /// Consensus Errors -#[derive(Debug, PartialEq, Eq, Clone, thiserror::Error)] +#[derive(Debug, Clone, thiserror::Error)] pub enum ConsensusError { /// Error when the gas used in the header exceeds the gas limit. #[error("block used gas ({gas_used}) is greater than gas limit ({gas_limit})")] @@ -410,6 +411,9 @@ pub enum ConsensusError { /// Other, likely an injected L2 error. #[error("{0}")] Other(String), + /// Other unspecified error. + #[error(transparent)] + Custom(#[from] Arc), } impl ConsensusError { @@ -447,3 +451,34 @@ pub struct TxGasLimitTooHighErr { /// The maximum allowed gas limit pub max_allowed: u64, } + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(thiserror::Error, Debug)] + #[error("Custom L2 consensus error")] + struct CustomL2Error; + + #[test] + fn test_custom_error_conversion() { + // Test conversion from custom error to ConsensusError + let custom_err = CustomL2Error; + let arc_err: Arc = Arc::new(custom_err); + let consensus_err: ConsensusError = arc_err.into(); + + // Verify it's the Custom variant + assert!(matches!(consensus_err, ConsensusError::Custom(_))); + } + + #[test] + fn test_custom_error_display() { + let custom_err = CustomL2Error; + let arc_err: Arc = Arc::new(custom_err); + let consensus_err: ConsensusError = arc_err.into(); + + // Verify the error message is preserved through transparent attribute + let error_message = format!("{}", consensus_err); + assert_eq!(error_message, "Custom L2 consensus error"); + } +} diff --git a/crates/ethereum/consensus/src/lib.rs b/crates/ethereum/consensus/src/lib.rs index 9d89b4a73da..3654704cc26 100644 --- a/crates/ethereum/consensus/src/lib.rs +++ b/crates/ethereum/consensus/src/lib.rs @@ -228,10 +228,12 @@ mod tests { let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10); let child = header_with_gas_limit((parent.gas_limit + 5) as u64); - assert_eq!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Ok(()) - ); + assert!(validate_against_parent_gas_limit( + &child, + &parent, + &ChainSpec::
::default() + ) + .is_ok()); } #[test] @@ -239,10 +241,11 @@ mod tests { let parent = header_with_gas_limit(MINIMUM_GAS_LIMIT); let child = header_with_gas_limit(MINIMUM_GAS_LIMIT - 1); - assert_eq!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: child.gas_limit as u64 }) - ); + assert!(matches!( + validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()).unwrap_err(), + ConsensusError::GasLimitInvalidMinimum { child_gas_limit } + if child_gas_limit == child.gas_limit as u64 + )); } #[test] @@ -252,13 +255,11 @@ mod tests { parent.gas_limit + parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR + 1, ); - assert_eq!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidIncrease { - parent_gas_limit: parent.gas_limit, - child_gas_limit: child.gas_limit, - }) - ); + assert!(matches!( + validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()).unwrap_err(), + ConsensusError::GasLimitInvalidIncrease { parent_gas_limit, child_gas_limit } + if parent_gas_limit == parent.gas_limit && child_gas_limit == child.gas_limit + )); } #[test] @@ -266,10 +267,12 @@ mod tests { let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10); let child = header_with_gas_limit(parent.gas_limit - 5); - assert_eq!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Ok(()) - ); + assert!(validate_against_parent_gas_limit( + &child, + &parent, + &ChainSpec::
::default() + ) + .is_ok()); } #[test] @@ -279,13 +282,11 @@ mod tests { parent.gas_limit - parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1, ); - assert_eq!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidDecrease { - parent_gas_limit: parent.gas_limit, - child_gas_limit: child.gas_limit, - }) - ); + assert!(matches!( + validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()).unwrap_err(), + ConsensusError::GasLimitInvalidDecrease { parent_gas_limit, child_gas_limit } + if parent_gas_limit == parent.gas_limit && child_gas_limit == child.gas_limit + )); } #[test] @@ -300,9 +301,8 @@ mod tests { ..Default::default() }; - assert_eq!( - EthBeaconConsensus::new(chain_spec).validate_header(&SealedHeader::seal_slow(header,)), - Ok(()) - ); + assert!(EthBeaconConsensus::new(chain_spec) + .validate_header(&SealedHeader::seal_slow(header,)) + .is_ok()); } } diff --git a/crates/ethereum/consensus/src/validation.rs b/crates/ethereum/consensus/src/validation.rs index 71affffeb0c..31039d1ee6b 100644 --- a/crates/ethereum/consensus/src/validation.rs +++ b/crates/ethereum/consensus/src/validation.rs @@ -172,18 +172,16 @@ mod tests { let expected_receipts_root = B256::random(); let expected_logs_bloom = calculated_logs_bloom; - assert_eq!( + assert!(matches!( compare_receipts_root_and_logs_bloom( calculated_receipts_root, calculated_logs_bloom, expected_receipts_root, expected_logs_bloom - ), - Err(ConsensusError::BodyReceiptRootDiff( - GotExpected { got: calculated_receipts_root, expected: expected_receipts_root } - .into() - )) - ); + ).unwrap_err(), + ConsensusError::BodyReceiptRootDiff(diff) + if diff.got == calculated_receipts_root && diff.expected == expected_receipts_root + )); } #[test] @@ -194,16 +192,15 @@ mod tests { let expected_receipts_root = calculated_receipts_root; let expected_logs_bloom = Bloom::random(); - assert_eq!( + assert!(matches!( compare_receipts_root_and_logs_bloom( calculated_receipts_root, calculated_logs_bloom, expected_receipts_root, expected_logs_bloom - ), - Err(ConsensusError::BodyBloomLogDiff( - GotExpected { got: calculated_logs_bloom, expected: expected_logs_bloom }.into() - )) - ); + ).unwrap_err(), + ConsensusError::BodyBloomLogDiff(diff) + if diff.got == calculated_logs_bloom && diff.expected == expected_logs_bloom + )); } } diff --git a/crates/net/downloaders/src/file_client.rs b/crates/net/downloaders/src/file_client.rs index 4d545aec178..5227611d996 100644 --- a/crates/net/downloaders/src/file_client.rs +++ b/crates/net/downloaders/src/file_client.rs @@ -726,7 +726,7 @@ mod tests { downloader.update_sync_target(SyncTarget::Tip(p0.hash())); let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p0, p1, p2])); + assert_eq!(headers.unwrap(), vec![p0, p1, p2]); assert!(downloader.next().await.is_none()); assert!(downloader.next().await.is_none()); } diff --git a/crates/net/downloaders/src/headers/reverse_headers.rs b/crates/net/downloaders/src/headers/reverse_headers.rs index cb6b36c9ff9..059b76834ca 100644 --- a/crates/net/downloaders/src/headers/reverse_headers.rs +++ b/crates/net/downloaders/src/headers/reverse_headers.rs @@ -1464,7 +1464,7 @@ mod tests { .await; let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p0, p1, p2,])); + assert_eq!(headers.unwrap(), vec![p0, p1, p2,]); assert!(downloader.buffered_responses.is_empty()); assert!(downloader.next().await.is_none()); assert!(downloader.next().await.is_none()); @@ -1496,18 +1496,18 @@ mod tests { .await; let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p0])); let headers = headers.unwrap(); + assert_eq!(headers, vec![p0]); assert_eq!(headers.capacity(), headers.len()); let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p1])); let headers = headers.unwrap(); + assert_eq!(headers, vec![p1]); assert_eq!(headers.capacity(), headers.len()); let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p2])); let headers = headers.unwrap(); + assert_eq!(headers, vec![p2]); assert_eq!(headers.capacity(), headers.len()); assert!(downloader.next().await.is_none()); @@ -1539,18 +1539,18 @@ mod tests { .await; let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p0])); let headers = headers.unwrap(); + assert_eq!(headers, vec![p0]); assert_eq!(headers.capacity(), headers.len()); let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p1])); let headers = headers.unwrap(); + assert_eq!(headers, vec![p1]); assert_eq!(headers.capacity(), headers.len()); let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p2])); let headers = headers.unwrap(); + assert_eq!(headers, vec![p2]); assert_eq!(headers.capacity(), headers.len()); assert!(downloader.next().await.is_none()); diff --git a/crates/net/downloaders/src/headers/task.rs b/crates/net/downloaders/src/headers/task.rs index 38eb1429a8f..779ad7ab106 100644 --- a/crates/net/downloaders/src/headers/task.rs +++ b/crates/net/downloaders/src/headers/task.rs @@ -223,11 +223,11 @@ mod tests { .await; let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p0])); + assert_eq!(headers.unwrap(), vec![p0]); let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p1])); + assert_eq!(headers.unwrap(), vec![p1]); let headers = downloader.next().await.unwrap(); - assert_eq!(headers, Ok(vec![p2])); + assert_eq!(headers.unwrap(), vec![p2]); } } diff --git a/crates/net/p2p/src/headers/error.rs b/crates/net/p2p/src/headers/error.rs index bc9b09194c5..5e66a1b45f5 100644 --- a/crates/net/p2p/src/headers/error.rs +++ b/crates/net/p2p/src/headers/error.rs @@ -7,7 +7,7 @@ use reth_primitives_traits::SealedHeader; pub type HeadersDownloaderResult = Result>; /// Error variants that can happen when sending requests to a session. -#[derive(Debug, Clone, Eq, PartialEq, Display, Error)] +#[derive(Debug, Clone, Display, Error)] pub enum HeadersDownloaderError { /// The downloaded header cannot be attached to the local head, /// but is valid otherwise. diff --git a/crates/optimism/consensus/src/lib.rs b/crates/optimism/consensus/src/lib.rs index a68b356f0db..8db40c3dbce 100644 --- a/crates/optimism/consensus/src/lib.rs +++ b/crates/optimism/consensus/src/lib.rs @@ -249,7 +249,7 @@ mod tests { use reth_consensus::{Consensus, ConsensusError, FullConsensus, HeaderValidator}; use reth_optimism_chainspec::{OpChainSpec, OpChainSpecBuilder, OP_MAINNET}; use reth_optimism_primitives::{OpPrimitives, OpReceipt, OpTransactionSigned}; - use reth_primitives_traits::{proofs, GotExpected, RecoveredBlock, SealedBlock, SealedHeader}; + use reth_primitives_traits::{proofs, RecoveredBlock, SealedBlock, SealedHeader}; use reth_provider::BlockExecutionResult; use crate::OpBeaconConsensus; @@ -344,11 +344,10 @@ mod tests { // validate blob, it should fail blob gas used validation let pre_execution = beacon_consensus.validate_block_pre_execution(&block); - assert!(pre_execution.is_err()); - assert_eq!( + assert!(matches!( pre_execution.unwrap_err(), - ConsensusError::BlobGasUsedDiff(GotExpected { got: 10, expected: 0 }) - ); + ConsensusError::BlobGasUsedDiff(diff) if diff.got == 10 && diff.expected == 0 + )); } #[test] @@ -484,14 +483,11 @@ mod tests { ); // validate blob, it should fail blob gas used validation post execution. - assert!(post_execution.is_err()); - assert_eq!( + assert!(matches!( post_execution.unwrap_err(), - ConsensusError::BlobGasUsedDiff(GotExpected { - got: BLOB_GAS_USED + 1, - expected: BLOB_GAS_USED, - }) - ); + ConsensusError::BlobGasUsedDiff(diff) + if diff.got == BLOB_GAS_USED + 1 && diff.expected == BLOB_GAS_USED + )); } #[test] @@ -625,14 +621,11 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); - assert!(result.is_err()); - assert_eq!( + assert!(matches!( result.unwrap_err(), - ConsensusError::BaseFeeDiff(GotExpected { - got: MIN_BASE_FEE - 1, - expected: MIN_BASE_FEE, - }) - ); + ConsensusError::BaseFeeDiff(diff) + if diff.got == MIN_BASE_FEE - 1 && diff.expected == MIN_BASE_FEE + )); } #[test] @@ -768,10 +761,10 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); - assert!(result.is_err()); - assert_eq!( + assert!(matches!( result.unwrap_err(), - ConsensusError::BlobGasUsedDiff(GotExpected { got: DA_FOOTPRINT, expected: 0 }) - ); + ConsensusError::BlobGasUsedDiff(diff) + if diff.got == DA_FOOTPRINT && diff.expected == 0 + )); } } diff --git a/crates/optimism/consensus/src/validation/mod.rs b/crates/optimism/consensus/src/validation/mod.rs index c17e8429c81..50c45f7172c 100644 --- a/crates/optimism/consensus/src/validation/mod.rs +++ b/crates/optimism/consensus/src/validation/mod.rs @@ -564,12 +564,10 @@ mod tests { requests: Requests::default(), gas_used: GAS_USED, }; - assert_eq!( - validate_block_post_execution(&header, &chainspec, &result), - Err(ConsensusError::BlobGasUsedDiff(GotExpected { - got: BLOB_GAS_USED, - expected: BLOB_GAS_USED + 1, - })) - ); + assert!(matches!( + validate_block_post_execution(&header, &chainspec, &result).unwrap_err(), + ConsensusError::BlobGasUsedDiff(diff) + if diff.got == BLOB_GAS_USED && diff.expected == BLOB_GAS_USED + 1 + )); } }