11use super :: transaction:: { TransactionInfo , TypedReceipt } ;
2- use alloy_consensus:: { EMPTY_OMMER_ROOT_HASH , Header , proofs :: calculate_transaction_root } ;
3- use alloy_primitives :: { Address , B64 , B256 , Bloom , Bytes , U256 } ;
4- use alloy_rlp :: { RlpDecodable , RlpEncodable } ;
2+ use alloy_consensus:: {
3+ BlockBody , EMPTY_OMMER_ROOT_HASH , Header , proofs :: calculate_transaction_root ,
4+ } ;
55
66// Type alias to optionally support impersonated transactions
77type Transaction = crate :: eth:: transaction:: MaybeImpersonatedTransaction ;
88
9+ /// Type alias for Ethereum Block with Anvil's transaction type
10+ pub type Block = alloy_consensus:: Block < Transaction > ;
11+
912/// Container type that gathers all block data
1013#[ derive( Clone , Debug ) ]
1114pub struct BlockInfo {
@@ -14,110 +17,28 @@ pub struct BlockInfo {
1417 pub receipts : Vec < TypedReceipt > ,
1518}
1619
17- /// An Ethereum Block
18- #[ derive( Clone , Debug , PartialEq , Eq , RlpEncodable , RlpDecodable ) ]
19- pub struct Block {
20- pub header : Header ,
21- pub transactions : Vec < Transaction > ,
22- pub ommers : Vec < Header > ,
23- }
24-
25- impl Block {
26- /// Creates a new block.
27- ///
28- /// Note: if the `impersonate-tx` feature is enabled this will also accept
29- /// `MaybeImpersonatedTransaction`.
30- pub fn new < T > ( partial_header : PartialHeader , transactions : impl IntoIterator < Item = T > ) -> Self
31- where
32- T : Into < Transaction > ,
33- {
34- let transactions: Vec < _ > = transactions. into_iter ( ) . map ( Into :: into) . collect ( ) ;
35- let transactions_root = calculate_transaction_root ( & transactions) ;
36-
37- Self {
38- header : Header {
39- parent_hash : partial_header. parent_hash ,
40- beneficiary : partial_header. beneficiary ,
41- ommers_hash : EMPTY_OMMER_ROOT_HASH ,
42- state_root : partial_header. state_root ,
43- transactions_root,
44- receipts_root : partial_header. receipts_root ,
45- logs_bloom : partial_header. logs_bloom ,
46- difficulty : partial_header. difficulty ,
47- number : partial_header. number ,
48- gas_limit : partial_header. gas_limit ,
49- gas_used : partial_header. gas_used ,
50- timestamp : partial_header. timestamp ,
51- extra_data : partial_header. extra_data ,
52- mix_hash : partial_header. mix_hash ,
53- withdrawals_root : partial_header. withdrawals_root ,
54- blob_gas_used : partial_header. blob_gas_used ,
55- excess_blob_gas : partial_header. excess_blob_gas ,
56- parent_beacon_block_root : partial_header. parent_beacon_block_root ,
57- nonce : partial_header. nonce ,
58- base_fee_per_gas : partial_header. base_fee ,
59- requests_hash : partial_header. requests_hash ,
60- } ,
61- transactions,
62- ommers : vec ! [ ] ,
63- }
64- }
65- }
66-
67- /// Partial header definition without ommers hash and transactions root
68- #[ derive( Clone , Debug , Default , PartialEq , Eq ) ]
69- pub struct PartialHeader {
70- pub parent_hash : B256 ,
71- pub beneficiary : Address ,
72- pub state_root : B256 ,
73- pub receipts_root : B256 ,
74- pub logs_bloom : Bloom ,
75- pub difficulty : U256 ,
76- pub number : u64 ,
77- pub gas_limit : u64 ,
78- pub gas_used : u64 ,
79- pub timestamp : u64 ,
80- pub extra_data : Bytes ,
81- pub mix_hash : B256 ,
82- pub blob_gas_used : Option < u64 > ,
83- pub excess_blob_gas : Option < u64 > ,
84- pub parent_beacon_block_root : Option < B256 > ,
85- pub nonce : B64 ,
86- pub base_fee : Option < u64 > ,
87- pub withdrawals_root : Option < B256 > ,
88- pub requests_hash : Option < B256 > ,
89- }
90-
91- impl From < Header > for PartialHeader {
92- fn from ( value : Header ) -> Self {
93- Self {
94- parent_hash : value. parent_hash ,
95- beneficiary : value. beneficiary ,
96- state_root : value. state_root ,
97- receipts_root : value. receipts_root ,
98- logs_bloom : value. logs_bloom ,
99- difficulty : value. difficulty ,
100- number : value. number ,
101- gas_limit : value. gas_limit ,
102- gas_used : value. gas_used ,
103- timestamp : value. timestamp ,
104- extra_data : value. extra_data ,
105- mix_hash : value. mix_hash ,
106- nonce : value. nonce ,
107- base_fee : value. base_fee_per_gas ,
108- blob_gas_used : value. blob_gas_used ,
109- excess_blob_gas : value. excess_blob_gas ,
110- parent_beacon_block_root : value. parent_beacon_block_root ,
111- requests_hash : value. requests_hash ,
112- withdrawals_root : value. withdrawals_root ,
113- }
114- }
20+ /// Helper function to create a new block with Header and Anvil transactions
21+ ///
22+ /// Note: if the `impersonate-tx` feature is enabled this will also accept
23+ /// `MaybeImpersonatedTransaction`.
24+ pub fn create_block < T > ( mut header : Header , transactions : impl IntoIterator < Item = T > ) -> Block
25+ where
26+ T : Into < Transaction > ,
27+ {
28+ let transactions: Vec < _ > = transactions. into_iter ( ) . map ( Into :: into) . collect ( ) ;
29+ let transactions_root = calculate_transaction_root ( & transactions) ;
30+
31+ header. transactions_root = transactions_root;
32+ header. ommers_hash = EMPTY_OMMER_ROOT_HASH ;
33+
34+ let body = BlockBody { transactions, ommers : Vec :: new ( ) , withdrawals : None } ;
35+ Block :: new ( header, body)
11536}
11637
11738#[ cfg( test) ]
11839mod tests {
11940 use alloy_primitives:: {
120- b256,
41+ Address , B64 , B256 , Bloom , U256 , b256,
12142 hex:: { self , FromHex } ,
12243 } ;
12344 use alloy_rlp:: Decodable ;
0 commit comments