From 980e1241b6cae9cc1cbabdc14e6b7aa786cdb49f Mon Sep 17 00:00:00 2001 From: Lee Date: Sat, 13 Dec 2025 12:10:33 +0800 Subject: [PATCH 1/7] chore(consensus): Add trait object error variant to ConsensusError --- crates/consensus/consensus/src/lib.rs | 43 ++++++++++++++++++- crates/net/downloaders/src/file_client.rs | 2 +- .../src/headers/reverse_headers.rs | 14 +++--- crates/net/downloaders/src/headers/task.rs | 6 +-- crates/net/p2p/src/headers/error.rs | 2 +- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/crates/consensus/consensus/src/lib.rs b/crates/consensus/consensus/src/lib.rs index e714b614090..1538ef6c242 100644 --- a/crates/consensus/consensus/src/lib.rs +++ b/crates/consensus/consensus/src/lib.rs @@ -125,7 +125,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 +410,9 @@ pub enum ConsensusError { /// Other, likely an injected L2 error. #[error("{0}")] Other(String), + /// Other unspecified error. + #[error(transparent)] + Custom(#[from] alloc::sync::Arc), } impl ConsensusError { @@ -447,3 +450,41 @@ 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: alloc::sync::Arc = + alloc::sync::Arc::new(custom_err); + let consensus_err: ConsensusError = arc_err.into(); + + // Verify it's the Custom variant + match consensus_err { + ConsensusError::Custom(_) => { + // Success - the error was properly converted to Custom variant + } + _ => panic!("Expected Custom variant"), + } + } + + #[test] + fn test_custom_error_display() { + let custom_err = CustomL2Error; + let arc_err: alloc::sync::Arc = + alloc::sync::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/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. From 923a776c736581da9fe634347257d1ff11e6e503 Mon Sep 17 00:00:00 2001 From: Lee Date: Mon, 15 Dec 2025 23:51:18 +0800 Subject: [PATCH 2/7] fix from review and make compile success --- crates/consensus/common/src/validation.rs | 14 +++---- crates/consensus/consensus/src/lib.rs | 18 +++----- crates/ethereum/consensus/src/lib.rs | 39 ++++++----------- crates/ethereum/consensus/src/validation.rs | 17 +++----- crates/optimism/consensus/src/lib.rs | 42 +++++++------------ .../optimism/consensus/src/validation/mod.rs | 9 ++-- 6 files changed, 48 insertions(+), 91 deletions(-) diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 8cb44661b83..2398be90990 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -492,13 +492,9 @@ 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 - })) - ); + let result = validate_block_pre_execution(&block, &chain_spec); + assert!(result.is_err()); + assert!(matches!(result.unwrap_err(), ConsensusError::BlobGasUsedDiff(diff) if diff.got == 1 && diff.expected == expected_blob_gas_used)); } #[test] @@ -509,10 +505,10 @@ mod tests { // Test exceeding default - should fail let header_33 = Header { extra_data: Bytes::from(vec![0; 33]), ..Default::default() }; - assert_eq!( + assert!(matches!( validate_header_extra_data(&header_33, 32), Err(ConsensusError::ExtraDataExceedsMax { 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 1538ef6c242..4dff3fad067 100644 --- a/crates/consensus/consensus/src/lib.rs +++ b/crates/consensus/consensus/src/lib.rs @@ -11,7 +11,8 @@ 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 core::error::Error; use alloy_consensus::Header; use alloy_primitives::{BlockHash, BlockNumber, Bloom, B256}; use reth_execution_types::BlockExecutionResult; @@ -412,7 +413,7 @@ pub enum ConsensusError { Other(String), /// Other unspecified error. #[error(transparent)] - Custom(#[from] alloc::sync::Arc), + Custom(#[from] Arc), } impl ConsensusError { @@ -463,24 +464,17 @@ mod tests { fn test_custom_error_conversion() { // Test conversion from custom error to ConsensusError let custom_err = CustomL2Error; - let arc_err: alloc::sync::Arc = - alloc::sync::Arc::new(custom_err); + let arc_err: Arc = Arc::new(custom_err); let consensus_err: ConsensusError = arc_err.into(); // Verify it's the Custom variant - match consensus_err { - ConsensusError::Custom(_) => { - // Success - the error was properly converted to Custom variant - } - _ => panic!("Expected Custom variant"), - } + assert!(matches!(consensus_err, ConsensusError::Custom(_))); } #[test] fn test_custom_error_display() { let custom_err = CustomL2Error; - let arc_err: alloc::sync::Arc = - alloc::sync::Arc::new(custom_err); + let arc_err: Arc = Arc::new(custom_err); let consensus_err: ConsensusError = arc_err.into(); // Verify the error message is preserved through transparent attribute diff --git a/crates/ethereum/consensus/src/lib.rs b/crates/ethereum/consensus/src/lib.rs index 9d89b4a73da..68f90dae624 100644 --- a/crates/ethereum/consensus/src/lib.rs +++ b/crates/ethereum/consensus/src/lib.rs @@ -228,10 +228,7 @@ 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 +236,10 @@ mod tests { let parent = header_with_gas_limit(MINIMUM_GAS_LIMIT); let child = header_with_gas_limit(MINIMUM_GAS_LIMIT - 1); - assert_eq!( + assert!(matches!( validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: child.gas_limit as u64 }) - ); + Err(ConsensusError::GasLimitInvalidMinimum { .. }) + )); } #[test] @@ -252,13 +249,10 @@ mod tests { parent.gas_limit + parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR + 1, ); - assert_eq!( + assert!(matches!( validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidIncrease { - parent_gas_limit: parent.gas_limit, - child_gas_limit: child.gas_limit, - }) - ); + Err(ConsensusError::GasLimitInvalidIncrease { .. }) + )); } #[test] @@ -266,10 +260,7 @@ 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 +270,10 @@ mod tests { parent.gas_limit - parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1, ); - assert_eq!( + assert!(matches!( validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidDecrease { - parent_gas_limit: parent.gas_limit, - child_gas_limit: child.gas_limit, - }) - ); + Err(ConsensusError::GasLimitInvalidDecrease { .. }) + )); } #[test] @@ -300,9 +288,6 @@ 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..c112d71fdbd 100644 --- a/crates/ethereum/consensus/src/validation.rs +++ b/crates/ethereum/consensus/src/validation.rs @@ -172,18 +172,15 @@ 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() - )) - ); + Err(ConsensusError::BodyReceiptRootDiff(_)) + )); } #[test] @@ -194,16 +191,14 @@ 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() - )) - ); + Err(ConsensusError::BodyBloomLogDiff(_)) + )); } } diff --git a/crates/optimism/consensus/src/lib.rs b/crates/optimism/consensus/src/lib.rs index a68b356f0db..85a3628d3e8 100644 --- a/crates/optimism/consensus/src/lib.rs +++ b/crates/optimism/consensus/src/lib.rs @@ -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!( - pre_execution.unwrap_err(), - ConsensusError::BlobGasUsedDiff(GotExpected { got: 10, expected: 0 }) - ); + assert!(matches!( + pre_execution, + Err(ConsensusError::BlobGasUsedDiff(_)) + )); } #[test] @@ -484,14 +483,10 @@ mod tests { ); // validate blob, it should fail blob gas used validation post execution. - assert!(post_execution.is_err()); - assert_eq!( - post_execution.unwrap_err(), - ConsensusError::BlobGasUsedDiff(GotExpected { - got: BLOB_GAS_USED + 1, - expected: BLOB_GAS_USED, - }) - ); + assert!(matches!( + post_execution, + Err(ConsensusError::BlobGasUsedDiff(_)) + )); } #[test] @@ -625,14 +620,10 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); - assert!(result.is_err()); - assert_eq!( - result.unwrap_err(), - ConsensusError::BaseFeeDiff(GotExpected { - got: MIN_BASE_FEE - 1, - expected: MIN_BASE_FEE, - }) - ); + assert!(matches!( + result, + Err(ConsensusError::BaseFeeDiff(_)) + )); } #[test] @@ -768,10 +759,9 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); - assert!(result.is_err()); - assert_eq!( - result.unwrap_err(), - ConsensusError::BlobGasUsedDiff(GotExpected { got: DA_FOOTPRINT, expected: 0 }) - ); + assert!(matches!( + result, + Err(ConsensusError::BlobGasUsedDiff(_)) + )); } } diff --git a/crates/optimism/consensus/src/validation/mod.rs b/crates/optimism/consensus/src/validation/mod.rs index c17e8429c81..ac3f46441ae 100644 --- a/crates/optimism/consensus/src/validation/mod.rs +++ b/crates/optimism/consensus/src/validation/mod.rs @@ -564,12 +564,9 @@ mod tests { requests: Requests::default(), gas_used: GAS_USED, }; - assert_eq!( + assert!(matches!( validate_block_post_execution(&header, &chainspec, &result), - Err(ConsensusError::BlobGasUsedDiff(GotExpected { - got: BLOB_GAS_USED, - expected: BLOB_GAS_USED + 1, - })) - ); + Err(ConsensusError::BlobGasUsedDiff(_)) + )); } } From 246a9c5735059dc09bb966642f5e3c2d06ebc569 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Tue, 16 Dec 2025 13:28:39 +0100 Subject: [PATCH 3/7] Fix use matches to verify result in test_compare_log_bloom_failure --- crates/ethereum/consensus/src/validation.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/ethereum/consensus/src/validation.rs b/crates/ethereum/consensus/src/validation.rs index c112d71fdbd..4bd0a9b848a 100644 --- a/crates/ethereum/consensus/src/validation.rs +++ b/crates/ethereum/consensus/src/validation.rs @@ -197,8 +197,11 @@ mod tests { calculated_logs_bloom, expected_receipts_root, expected_logs_bloom - ), - Err(ConsensusError::BodyBloomLogDiff(_)) + ).unwrap_err(), + ConsensusError::BodyBloomLogDiff(err) + if matches!(*err, GotExpected { got, expected } + if got == calculated_logs_bloom && expected == expected_logs_bloom + ) )); } } From be41bf21f884c93cc511f6e7a61b98f632b9aa0b Mon Sep 17 00:00:00 2001 From: Lee Date: Tue, 16 Dec 2025 22:46:08 +0800 Subject: [PATCH 4/7] update based on 246a9c5 --- crates/consensus/common/src/validation.rs | 12 +++++++----- crates/ethereum/consensus/src/lib.rs | 15 +++++++++------ crates/ethereum/consensus/src/validation.rs | 7 +++++-- crates/optimism/consensus/src/lib.rs | 19 +++++++++++-------- .../optimism/consensus/src/validation/mod.rs | 5 +++-- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 2398be90990..923d94dc406 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -492,9 +492,11 @@ mod tests { let expected_blob_gas_used = 10 * DATA_GAS_PER_BLOB; // validate blob, it should fail blob gas used validation - let result = validate_block_pre_execution(&block, &chain_spec); - assert!(result.is_err()); - assert!(matches!(result.unwrap_err(), ConsensusError::BlobGasUsedDiff(diff) if diff.got == 1 && diff.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] @@ -506,8 +508,8 @@ mod tests { // Test exceeding default - should fail let header_33 = Header { extra_data: Bytes::from(vec![0; 33]), ..Default::default() }; assert!(matches!( - validate_header_extra_data(&header_33, 32), - Err(ConsensusError::ExtraDataExceedsMax { len: 33 }) + validate_header_extra_data(&header_33, 32).unwrap_err(), + ConsensusError::ExtraDataExceedsMax { len: 33 } )); // Test with custom larger limit - should pass diff --git a/crates/ethereum/consensus/src/lib.rs b/crates/ethereum/consensus/src/lib.rs index 68f90dae624..bd6042fbc4f 100644 --- a/crates/ethereum/consensus/src/lib.rs +++ b/crates/ethereum/consensus/src/lib.rs @@ -237,8 +237,9 @@ mod tests { let child = header_with_gas_limit(MINIMUM_GAS_LIMIT - 1); assert!(matches!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidMinimum { .. }) + 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 )); } @@ -250,8 +251,9 @@ mod tests { ); assert!(matches!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidIncrease { .. }) + 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 )); } @@ -271,8 +273,9 @@ mod tests { ); assert!(matches!( - validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()), - Err(ConsensusError::GasLimitInvalidDecrease { .. }) + 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 )); } diff --git a/crates/ethereum/consensus/src/validation.rs b/crates/ethereum/consensus/src/validation.rs index 4bd0a9b848a..06f49755d7c 100644 --- a/crates/ethereum/consensus/src/validation.rs +++ b/crates/ethereum/consensus/src/validation.rs @@ -178,8 +178,11 @@ mod tests { calculated_logs_bloom, expected_receipts_root, expected_logs_bloom - ), - Err(ConsensusError::BodyReceiptRootDiff(_)) + ).unwrap_err(), + ConsensusError::BodyReceiptRootDiff(err) + if matches!(*err, GotExpected { got, expected } + if got == calculated_receipts_root && expected == expected_receipts_root + ) )); } diff --git a/crates/optimism/consensus/src/lib.rs b/crates/optimism/consensus/src/lib.rs index 85a3628d3e8..c0beee13b7d 100644 --- a/crates/optimism/consensus/src/lib.rs +++ b/crates/optimism/consensus/src/lib.rs @@ -345,8 +345,8 @@ mod tests { let pre_execution = beacon_consensus.validate_block_pre_execution(&block); assert!(matches!( - pre_execution, - Err(ConsensusError::BlobGasUsedDiff(_)) + pre_execution.unwrap_err(), + ConsensusError::BlobGasUsedDiff(diff) if diff.got == 10 && diff.expected == 0 )); } @@ -484,8 +484,9 @@ mod tests { // validate blob, it should fail blob gas used validation post execution. assert!(matches!( - post_execution, - Err(ConsensusError::BlobGasUsedDiff(_)) + post_execution.unwrap_err(), + ConsensusError::BlobGasUsedDiff(diff) + if diff.got == BLOB_GAS_USED + 1 && diff.expected == BLOB_GAS_USED )); } @@ -621,8 +622,9 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); assert!(matches!( - result, - Err(ConsensusError::BaseFeeDiff(_)) + result.unwrap_err(), + ConsensusError::BaseFeeDiff(diff) + if diff.got == MIN_BASE_FEE - 1 && diff.expected == MIN_BASE_FEE )); } @@ -760,8 +762,9 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); assert!(matches!( - result, - Err(ConsensusError::BlobGasUsedDiff(_)) + result.unwrap_err(), + 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 ac3f46441ae..50c45f7172c 100644 --- a/crates/optimism/consensus/src/validation/mod.rs +++ b/crates/optimism/consensus/src/validation/mod.rs @@ -565,8 +565,9 @@ mod tests { gas_used: GAS_USED, }; assert!(matches!( - validate_block_post_execution(&header, &chainspec, &result), - Err(ConsensusError::BlobGasUsedDiff(_)) + validate_block_post_execution(&header, &chainspec, &result).unwrap_err(), + ConsensusError::BlobGasUsedDiff(diff) + if diff.got == BLOB_GAS_USED && diff.expected == BLOB_GAS_USED + 1 )); } } From ed88b427c27cc94eaedfb840cd2832e0831c0b02 Mon Sep 17 00:00:00 2001 From: Lee Date: Wed, 17 Dec 2025 03:49:01 +0800 Subject: [PATCH 5/7] fix logical and simplify the code --- crates/consensus/common/src/validation.rs | 2 +- crates/ethereum/consensus/src/validation.rs | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 923d94dc406..3eb5974530c 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -509,7 +509,7 @@ mod tests { let header_33 = Header { extra_data: Bytes::from(vec![0; 33]), ..Default::default() }; assert!(matches!( validate_header_extra_data(&header_33, 32).unwrap_err(), - ConsensusError::ExtraDataExceedsMax { len: 33 } + ConsensusError::ExtraDataExceedsMax { len } if len == 33 )); // Test with custom larger limit - should pass diff --git a/crates/ethereum/consensus/src/validation.rs b/crates/ethereum/consensus/src/validation.rs index 06f49755d7c..f042507c075 100644 --- a/crates/ethereum/consensus/src/validation.rs +++ b/crates/ethereum/consensus/src/validation.rs @@ -179,10 +179,8 @@ mod tests { expected_receipts_root, expected_logs_bloom ).unwrap_err(), - ConsensusError::BodyReceiptRootDiff(err) - if matches!(*err, GotExpected { got, expected } - if got == calculated_receipts_root && expected == expected_receipts_root - ) + ConsensusError::BodyReceiptRootDiff(diff) + if diff.got == calculated_receipts_root && diff.expected == expected_receipts_root )); } @@ -201,10 +199,8 @@ mod tests { expected_receipts_root, expected_logs_bloom ).unwrap_err(), - ConsensusError::BodyBloomLogDiff(err) - if matches!(*err, GotExpected { got, expected } - if got == calculated_logs_bloom && expected == expected_logs_bloom - ) + ConsensusError::BodyBloomLogDiff(diff) + if diff.got == calculated_logs_bloom && diff.expected == expected_logs_bloom )); } } From 44258c8b8797aa892fcff52faccdb0443b87d2ef Mon Sep 17 00:00:00 2001 From: Lee Date: Wed, 17 Dec 2025 03:52:52 +0800 Subject: [PATCH 6/7] fmt --- crates/consensus/common/src/validation.rs | 2 +- crates/consensus/consensus/src/lib.rs | 6 +++--- crates/ethereum/consensus/src/lib.rs | 18 +++++++++++++++--- crates/ethereum/consensus/src/validation.rs | 4 ++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 3eb5974530c..812fee1a0d6 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -494,7 +494,7 @@ mod tests { // validate blob, it should fail blob gas used validation assert!(matches!( validate_block_pre_execution(&block, &chain_spec).unwrap_err(), - ConsensusError::BlobGasUsedDiff(diff) + ConsensusError::BlobGasUsedDiff(diff) if diff.got == 1 && diff.expected == expected_blob_gas_used )); } diff --git a/crates/consensus/consensus/src/lib.rs b/crates/consensus/consensus/src/lib.rs index 4dff3fad067..5a937134880 100644 --- a/crates/consensus/consensus/src/lib.rs +++ b/crates/consensus/consensus/src/lib.rs @@ -12,9 +12,9 @@ extern crate alloc; use alloc::{boxed::Box, fmt::Debug, string::String, sync::Arc, vec::Vec}; -use core::error::Error; 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}, @@ -466,7 +466,7 @@ mod tests { 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(_))); } @@ -476,7 +476,7 @@ mod tests { 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 bd6042fbc4f..3654704cc26 100644 --- a/crates/ethereum/consensus/src/lib.rs +++ b/crates/ethereum/consensus/src/lib.rs @@ -228,7 +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!(validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()).is_ok()); + assert!(validate_against_parent_gas_limit( + &child, + &parent, + &ChainSpec::
::default() + ) + .is_ok()); } #[test] @@ -262,7 +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!(validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()).is_ok()); + assert!(validate_against_parent_gas_limit( + &child, + &parent, + &ChainSpec::
::default() + ) + .is_ok()); } #[test] @@ -291,6 +301,8 @@ mod tests { ..Default::default() }; - assert!(EthBeaconConsensus::new(chain_spec).validate_header(&SealedHeader::seal_slow(header,)).is_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 f042507c075..31039d1ee6b 100644 --- a/crates/ethereum/consensus/src/validation.rs +++ b/crates/ethereum/consensus/src/validation.rs @@ -179,7 +179,7 @@ mod tests { expected_receipts_root, expected_logs_bloom ).unwrap_err(), - ConsensusError::BodyReceiptRootDiff(diff) + ConsensusError::BodyReceiptRootDiff(diff) if diff.got == calculated_receipts_root && diff.expected == expected_receipts_root )); } @@ -199,7 +199,7 @@ mod tests { expected_receipts_root, expected_logs_bloom ).unwrap_err(), - ConsensusError::BodyBloomLogDiff(diff) + ConsensusError::BodyBloomLogDiff(diff) if diff.got == calculated_logs_bloom && diff.expected == expected_logs_bloom )); } From 5c937b6dc0d7defcb7dbb5fa01e6fa1f190151e8 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Wed, 17 Dec 2025 13:32:53 +0100 Subject: [PATCH 7/7] Fix lint --- crates/consensus/common/src/validation.rs | 4 +++- crates/consensus/consensus/src/lib.rs | 6 +++--- crates/ethereum/consensus/src/lib.rs | 18 +++++++++++++++--- crates/optimism/consensus/src/lib.rs | 22 +++++----------------- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/crates/consensus/common/src/validation.rs b/crates/consensus/common/src/validation.rs index 2398be90990..15c78e127dd 100644 --- a/crates/consensus/common/src/validation.rs +++ b/crates/consensus/common/src/validation.rs @@ -494,7 +494,9 @@ mod tests { // validate blob, it should fail blob gas used validation let result = validate_block_pre_execution(&block, &chain_spec); assert!(result.is_err()); - assert!(matches!(result.unwrap_err(), ConsensusError::BlobGasUsedDiff(diff) if diff.got == 1 && diff.expected == expected_blob_gas_used)); + assert!( + matches!(result.unwrap_err(), ConsensusError::BlobGasUsedDiff(diff) if diff.got == 1 && diff.expected == expected_blob_gas_used) + ); } #[test] diff --git a/crates/consensus/consensus/src/lib.rs b/crates/consensus/consensus/src/lib.rs index 4dff3fad067..5a937134880 100644 --- a/crates/consensus/consensus/src/lib.rs +++ b/crates/consensus/consensus/src/lib.rs @@ -12,9 +12,9 @@ extern crate alloc; use alloc::{boxed::Box, fmt::Debug, string::String, sync::Arc, vec::Vec}; -use core::error::Error; 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}, @@ -466,7 +466,7 @@ mod tests { 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(_))); } @@ -476,7 +476,7 @@ mod tests { 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 68f90dae624..ea847a4e81a 100644 --- a/crates/ethereum/consensus/src/lib.rs +++ b/crates/ethereum/consensus/src/lib.rs @@ -228,7 +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!(validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()).is_ok()); + assert!(validate_against_parent_gas_limit( + &child, + &parent, + &ChainSpec::
::default() + ) + .is_ok()); } #[test] @@ -260,7 +265,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!(validate_against_parent_gas_limit(&child, &parent, &ChainSpec::
::default()).is_ok()); + assert!(validate_against_parent_gas_limit( + &child, + &parent, + &ChainSpec::
::default() + ) + .is_ok()); } #[test] @@ -288,6 +298,8 @@ mod tests { ..Default::default() }; - assert!(EthBeaconConsensus::new(chain_spec).validate_header(&SealedHeader::seal_slow(header,)).is_ok()); + assert!(EthBeaconConsensus::new(chain_spec) + .validate_header(&SealedHeader::seal_slow(header,)) + .is_ok()); } } diff --git a/crates/optimism/consensus/src/lib.rs b/crates/optimism/consensus/src/lib.rs index 85a3628d3e8..e06068b3f30 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,10 +344,7 @@ mod tests { // validate blob, it should fail blob gas used validation let pre_execution = beacon_consensus.validate_block_pre_execution(&block); - assert!(matches!( - pre_execution, - Err(ConsensusError::BlobGasUsedDiff(_)) - )); + assert!(matches!(pre_execution, Err(ConsensusError::BlobGasUsedDiff(_)))); } #[test] @@ -483,10 +480,7 @@ mod tests { ); // validate blob, it should fail blob gas used validation post execution. - assert!(matches!( - post_execution, - Err(ConsensusError::BlobGasUsedDiff(_)) - )); + assert!(matches!(post_execution, Err(ConsensusError::BlobGasUsedDiff(_)))); } #[test] @@ -620,10 +614,7 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); - assert!(matches!( - result, - Err(ConsensusError::BaseFeeDiff(_)) - )); + assert!(matches!(result, Err(ConsensusError::BaseFeeDiff(_)))); } #[test] @@ -759,9 +750,6 @@ mod tests { let result = beacon_consensus.validate_header_against_parent(&header, &parent); - assert!(matches!( - result, - Err(ConsensusError::BlobGasUsedDiff(_)) - )); + assert!(matches!(result, Err(ConsensusError::BlobGasUsedDiff(_)))); } }