reth_primitives_traits/header/
test_utils.rs

1//! Test utilities for the block header.
2
3use crate::BlockHeader;
4use alloy_consensus::Header;
5use alloy_primitives::{BlockHash, BlockNumber, B256, U256};
6use proptest::{arbitrary::any, prop_compose};
7use proptest_arbitrary_interop::arb;
8
9/// A helper trait for [`Header`]s that allows for mutable access to the headers values.
10///
11/// This allows for modifying the header for testing purposes.
12pub trait TestHeader: BlockHeader {
13    /// Updates the parent block hash.
14    fn set_parent_hash(&mut self, hash: BlockHash);
15
16    /// Updates the block number.
17    fn set_block_number(&mut self, number: BlockNumber);
18
19    /// Updates the block state root.
20    fn set_state_root(&mut self, state_root: B256);
21
22    /// Updates the block difficulty.
23    fn set_difficulty(&mut self, difficulty: U256);
24}
25
26impl TestHeader for Header {
27    fn set_parent_hash(&mut self, hash: BlockHash) {
28        self.parent_hash = hash
29    }
30
31    fn set_block_number(&mut self, number: BlockNumber) {
32        self.number = number;
33    }
34
35    fn set_state_root(&mut self, state_root: B256) {
36        self.state_root = state_root;
37    }
38
39    fn set_difficulty(&mut self, difficulty: U256) {
40        self.difficulty = difficulty;
41    }
42}
43
44/// Generates a header which is valid __with respect to past and future forks__. This means, for
45/// example, that if the withdrawals root is present, the base fee per gas is also present.
46///
47/// If blob gas used were present, then the excess blob gas and parent beacon block root are also
48/// present. In this example, the withdrawals root would also be present.
49///
50/// This __does not, and should not guarantee__ that the header is valid with respect to __anything
51/// else__.
52pub const fn generate_valid_header(
53    mut header: Header,
54    eip_4844_active: bool,
55    blob_gas_used: u64,
56    excess_blob_gas: u64,
57    parent_beacon_block_root: B256,
58) -> Header {
59    // Clear all related fields if EIP-1559 is inactive
60    if header.base_fee_per_gas.is_none() {
61        header.withdrawals_root = None;
62    }
63
64    // Set fields based on EIP-4844 being active
65    if eip_4844_active {
66        header.blob_gas_used = Some(blob_gas_used);
67        header.excess_blob_gas = Some(excess_blob_gas);
68        header.parent_beacon_block_root = Some(parent_beacon_block_root);
69    } else {
70        header.blob_gas_used = None;
71        header.excess_blob_gas = None;
72        header.parent_beacon_block_root = None;
73    }
74
75    // Placeholder for future EIP adjustments
76    header.requests_hash = None;
77
78    header
79}
80
81prop_compose! {
82    /// Generates a proptest strategy for constructing an instance of a header which is valid __with
83    /// respect to past and future forks__.
84    ///
85    /// See docs for [generate_valid_header] for more information.
86    pub fn valid_header_strategy()(
87        header in arb::<Header>(),
88        eip_4844_active in any::<bool>(),
89        blob_gas_used in any::<u64>(),
90        excess_blob_gas in any::<u64>(),
91        parent_beacon_block_root in arb::<B256>()
92    ) -> Header {
93        generate_valid_header(header, eip_4844_active, blob_gas_used, excess_blob_gas, parent_beacon_block_root)
94    }
95}