reth_evm_ethereum/
build.rs1use alloc::{sync::Arc, vec::Vec};
2use alloy_consensus::{
3 proofs::{self, calculate_receipt_root},
4 Block, BlockBody, BlockHeader, Header, Transaction, TxReceipt, EMPTY_OMMER_ROOT_HASH,
5};
6use alloy_eips::merge::BEACON_NONCE;
7use alloy_evm::{block::BlockExecutorFactory, eth::EthBlockExecutionCtx};
8use alloy_primitives::Bytes;
9use reth_chainspec::{EthChainSpec, EthereumHardforks};
10use reth_evm::execute::{BlockAssembler, BlockAssemblerInput, BlockExecutionError};
11use reth_execution_types::BlockExecutionResult;
12use reth_primitives_traits::{logs_bloom, Receipt, SignedTransaction};
13
14#[derive(Debug, Clone)]
16pub struct EthBlockAssembler<ChainSpec = reth_chainspec::ChainSpec> {
17 pub chain_spec: Arc<ChainSpec>,
19 pub extra_data: Bytes,
21}
22
23impl<ChainSpec> EthBlockAssembler<ChainSpec> {
24 pub fn new(chain_spec: Arc<ChainSpec>) -> Self {
26 Self { chain_spec, extra_data: Default::default() }
27 }
28}
29
30impl<F, ChainSpec> BlockAssembler<F> for EthBlockAssembler<ChainSpec>
31where
32 F: for<'a> BlockExecutorFactory<
33 ExecutionCtx<'a> = EthBlockExecutionCtx<'a>,
34 Transaction: SignedTransaction,
35 Receipt: Receipt,
36 >,
37 ChainSpec: EthChainSpec + EthereumHardforks,
38{
39 type Block = Block<F::Transaction>;
40
41 fn assemble_block(
42 &self,
43 input: BlockAssemblerInput<'_, '_, F>,
44 ) -> Result<Self::Block, BlockExecutionError> {
45 let BlockAssemblerInput {
46 evm_env,
47 execution_ctx: ctx,
48 parent,
49 transactions,
50 output: BlockExecutionResult { receipts, requests, gas_used },
51 state_root,
52 ..
53 } = input;
54
55 let timestamp = evm_env.block_env.timestamp.saturating_to();
56
57 let transactions_root = proofs::calculate_transaction_root(&transactions);
58 let receipts_root = calculate_receipt_root(
59 &receipts.iter().map(|r| r.with_bloom_ref()).collect::<Vec<_>>(),
60 );
61 let logs_bloom = logs_bloom(receipts.iter().flat_map(|r| r.logs()));
62
63 let withdrawals = self
64 .chain_spec
65 .is_shanghai_active_at_timestamp(timestamp)
66 .then(|| ctx.withdrawals.map(|w| w.into_owned()).unwrap_or_default());
67
68 let withdrawals_root =
69 withdrawals.as_deref().map(|w| proofs::calculate_withdrawals_root(w));
70 let requests_hash = self
71 .chain_spec
72 .is_prague_active_at_timestamp(timestamp)
73 .then(|| requests.requests_hash());
74
75 let mut excess_blob_gas = None;
76 let mut blob_gas_used = None;
77
78 if self.chain_spec.is_cancun_active_at_timestamp(timestamp) {
80 blob_gas_used =
81 Some(transactions.iter().map(|tx| tx.blob_gas_used().unwrap_or_default()).sum());
82 excess_blob_gas = if self.chain_spec.is_cancun_active_at_timestamp(parent.timestamp) {
83 parent.maybe_next_block_excess_blob_gas(
84 self.chain_spec.blob_params_at_timestamp(timestamp),
85 )
86 } else {
87 Some(
90 alloy_eips::eip7840::BlobParams::cancun()
91 .next_block_excess_blob_gas_osaka(0, 0, 0),
92 )
93 };
94 }
95
96 let header = Header {
97 parent_hash: ctx.parent_hash,
98 ommers_hash: EMPTY_OMMER_ROOT_HASH,
99 beneficiary: evm_env.block_env.beneficiary,
100 state_root,
101 transactions_root,
102 receipts_root,
103 withdrawals_root,
104 logs_bloom,
105 timestamp,
106 mix_hash: evm_env.block_env.prevrandao.unwrap_or_default(),
107 nonce: BEACON_NONCE.into(),
108 base_fee_per_gas: Some(evm_env.block_env.basefee),
109 number: evm_env.block_env.number.saturating_to(),
110 gas_limit: evm_env.block_env.gas_limit,
111 difficulty: evm_env.block_env.difficulty,
112 gas_used: *gas_used,
113 extra_data: self.extra_data.clone(),
114 parent_beacon_block_root: ctx.parent_beacon_block_root,
115 blob_gas_used,
116 excess_blob_gas,
117 requests_hash,
118 };
119
120 Ok(Block {
121 header,
122 body: BlockBody { transactions, ommers: Default::default(), withdrawals },
123 })
124 }
125}