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