Skip to content
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
20 changes: 9 additions & 11 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,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]
Expand All @@ -517,10 +515,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());
Expand Down
39 changes: 37 additions & 2 deletions crates/consensus/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -122,7 +123,7 @@ pub trait HeaderValidator<H = Header>: 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})")]
Expand Down Expand Up @@ -407,6 +408,9 @@ pub enum ConsensusError {
/// Other, likely an injected L2 error.
#[error("{0}")]
Other(String),
/// Other unspecified error.
#[error(transparent)]
Custom(#[from] Arc<dyn Error + Send + Sync>),
}

impl ConsensusError {
Expand Down Expand Up @@ -444,3 +448,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<dyn Error + Send + Sync> = 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<dyn Error + Send + 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");
}
}
60 changes: 30 additions & 30 deletions crates/ethereum/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,24 @@ 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::<Header>::default()),
Ok(())
);
assert!(validate_against_parent_gas_limit(
&child,
&parent,
&ChainSpec::<Header>::default()
)
.is_ok());
}

#[test]
fn test_gas_limit_below_minimum() {
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::<Header>::default()),
Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: child.gas_limit as u64 })
);
assert!(matches!(
validate_against_parent_gas_limit(&child, &parent, &ChainSpec::<Header>::default()).unwrap_err(),
ConsensusError::GasLimitInvalidMinimum { child_gas_limit }
if child_gas_limit == child.gas_limit as u64
));
}

#[test]
Expand All @@ -250,24 +253,24 @@ mod tests {
parent.gas_limit + parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR + 1,
);

assert_eq!(
validate_against_parent_gas_limit(&child, &parent, &ChainSpec::<Header>::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::<Header>::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]
fn test_valid_gas_limit_decrease_within_limit() {
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::<Header>::default()),
Ok(())
);
assert!(validate_against_parent_gas_limit(
&child,
&parent,
&ChainSpec::<Header>::default()
)
.is_ok());
}

#[test]
Expand All @@ -277,13 +280,11 @@ mod tests {
parent.gas_limit - parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1,
);

assert_eq!(
validate_against_parent_gas_limit(&child, &parent, &ChainSpec::<Header>::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::<Header>::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]
Expand All @@ -298,9 +299,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());
}
}
23 changes: 10 additions & 13 deletions crates/ethereum/consensus/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,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]
Expand All @@ -192,16 +190,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
));
}
}
2 changes: 1 addition & 1 deletion crates/net/downloaders/src/file_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
14 changes: 7 additions & 7 deletions crates/net/downloaders/src/headers/reverse_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 3 additions & 3 deletions crates/net/downloaders/src/headers/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
2 changes: 1 addition & 1 deletion crates/net/p2p/src/headers/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_primitives_traits::SealedHeader;
pub type HeadersDownloaderResult<T, H> = Result<T, HeadersDownloaderError<H>>;

/// 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<H: Sealable> {
/// The downloaded header cannot be attached to the local head,
/// but is valid otherwise.
Expand Down
Loading
Loading