reth_primitives_traits/header/
test_utils.rs

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