reth_primitives_traits/header/
test_utils.rs

1//! Test utilities for the block header.
2
3use alloy_consensus::Header;
4use alloy_primitives::B256;
5use proptest::{arbitrary::any, prop_compose};
6use proptest_arbitrary_interop::arb;
7
8/// Re-export `HeaderMut` for backward compatibility in tests.
9pub use super::HeaderMut as TestHeader;
10
11/// Generates a header which is valid __with respect to past and future forks__. This means, for
12/// example, that if the withdrawals root is present, the base fee per gas is also present.
13///
14/// If blob gas used were present, then the excess blob gas and parent beacon block root are also
15/// present. In this example, the withdrawals root would also be present.
16///
17/// This __does not, and should not guarantee__ that the header is valid with respect to __anything
18/// else__.
19pub const fn generate_valid_header(
20    mut header: Header,
21    eip_4844_active: bool,
22    blob_gas_used: u64,
23    excess_blob_gas: u64,
24    parent_beacon_block_root: B256,
25) -> Header {
26    // Clear all related fields if EIP-1559 is inactive
27    if header.base_fee_per_gas.is_none() {
28        header.withdrawals_root = None;
29    }
30
31    // Set fields based on EIP-4844 being active
32    if eip_4844_active {
33        header.blob_gas_used = Some(blob_gas_used);
34        header.excess_blob_gas = Some(excess_blob_gas);
35        header.parent_beacon_block_root = Some(parent_beacon_block_root);
36    } else {
37        header.blob_gas_used = None;
38        header.excess_blob_gas = None;
39        header.parent_beacon_block_root = None;
40    }
41
42    // Placeholder for future EIP adjustments
43    header.requests_hash = None;
44
45    header
46}
47
48prop_compose! {
49    /// Generates a proptest strategy for constructing an instance of a header which is valid __with
50    /// respect to past and future forks__.
51    ///
52    /// See docs for [generate_valid_header] for more information.
53    pub fn valid_header_strategy()(
54        header in arb::<Header>(),
55        eip_4844_active in any::<bool>(),
56        blob_gas_used in any::<u64>(),
57        excess_blob_gas in any::<u64>(),
58        parent_beacon_block_root in arb::<B256>()
59    ) -> Header {
60        generate_valid_header(header, eip_4844_active, blob_gas_used, excess_blob_gas, parent_beacon_block_root)
61    }
62}