1//! Test utilities for the block header.
23use crate::BlockHeader;
4use alloy_consensus::Header;
5use alloy_primitives::{BlockHash, BlockNumber, B256, U256};
6use proptest::{arbitrary::any, prop_compose};
7use proptest_arbitrary_interop::arb;
89/// 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.
14fn set_parent_hash(&mut self, hash: BlockHash);
1516/// Updates the block number.
17fn set_block_number(&mut self, number: BlockNumber);
1819/// Updates the block state root.
20fn set_state_root(&mut self, state_root: B256);
2122/// Updates the block difficulty.
23fn set_difficulty(&mut self, difficulty: U256);
24}
2526impl TestHeaderfor Header {
27fn set_parent_hash(&mut self, hash: BlockHash) {
28self.parent_hash = hash29 }
3031fn set_block_number(&mut self, number: BlockNumber) {
32self.number = number;
33 }
3435fn set_state_root(&mut self, state_root: B256) {
36self.state_root = state_root;
37 }
3839fn set_difficulty(&mut self, difficulty: U256) {
40self.difficulty = difficulty;
41 }
42}
4344/// 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(
53mut 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
60if header.base_fee_per_gas.is_none() {
61header.withdrawals_root = None;
62 }
6364// Set fields based on EIP-4844 being active
65if eip_4844_active {
66header.blob_gas_used = Some(blob_gas_used);
67header.excess_blob_gas = Some(excess_blob_gas);
68header.parent_beacon_block_root = Some(parent_beacon_block_root);
69 } else {
70header.blob_gas_used = None;
71header.excess_blob_gas = None;
72header.parent_beacon_block_root = None;
73 }
7475// Placeholder for future EIP adjustments
76header.requests_hash = None;
7778header79}
8081prop_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.
86pub 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}