reth_optimism_consensus/
lib.rs
1#![doc(
4 html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
5 html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
6 issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
7)]
8#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
9#![cfg_attr(not(feature = "std"), no_std)]
10#![cfg_attr(not(test), warn(unused_crate_dependencies))]
11
12extern crate alloc;
13
14use alloc::{format, sync::Arc};
15use alloy_consensus::{BlockHeader as _, EMPTY_OMMER_ROOT_HASH};
16use alloy_primitives::{B64, U256};
17use core::fmt::Debug;
18use reth_chainspec::{EthChainSpec, EthereumHardforks};
19use reth_consensus::{Consensus, ConsensusError, FullConsensus, HeaderValidator};
20use reth_consensus_common::validation::{
21 validate_against_parent_4844, validate_against_parent_eip1559_base_fee,
22 validate_against_parent_hash_number, validate_against_parent_timestamp, validate_cancun_gas,
23 validate_header_base_fee, validate_header_extra_data, validate_header_gas,
24};
25use reth_execution_types::BlockExecutionResult;
26use reth_optimism_forks::OpHardforks;
27use reth_optimism_primitives::DepositReceipt;
28use reth_primitives_traits::{
29 Block, BlockBody, BlockHeader, GotExpected, NodePrimitives, RecoveredBlock, SealedBlock,
30 SealedHeader,
31};
32
33mod proof;
34pub use proof::calculate_receipt_root_no_memo_optimism;
35
36pub mod validation;
37pub use validation::{
38 canyon, decode_holocene_base_fee, isthmus, next_block_base_fee, validate_block_post_execution,
39};
40
41pub mod error;
42pub use error::OpConsensusError;
43
44#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct OpBeaconConsensus<ChainSpec> {
49 chain_spec: Arc<ChainSpec>,
51}
52
53impl<ChainSpec> OpBeaconConsensus<ChainSpec> {
54 pub const fn new(chain_spec: Arc<ChainSpec>) -> Self {
56 Self { chain_spec }
57 }
58}
59
60impl<ChainSpec: EthChainSpec + OpHardforks, N: NodePrimitives<Receipt: DepositReceipt>>
61 FullConsensus<N> for OpBeaconConsensus<ChainSpec>
62{
63 fn validate_block_post_execution(
64 &self,
65 block: &RecoveredBlock<N::Block>,
66 result: &BlockExecutionResult<N::Receipt>,
67 ) -> Result<(), ConsensusError> {
68 validate_block_post_execution(block.header(), &self.chain_spec, &result.receipts)
69 }
70}
71
72impl<ChainSpec: EthChainSpec + OpHardforks, B: Block> Consensus<B>
73 for OpBeaconConsensus<ChainSpec>
74{
75 type Error = ConsensusError;
76
77 fn validate_body_against_header(
78 &self,
79 body: &B::Body,
80 header: &SealedHeader<B::Header>,
81 ) -> Result<(), ConsensusError> {
82 validation::validate_body_against_header_op(&self.chain_spec, body, header.header())
83 }
84
85 fn validate_block_pre_execution(&self, block: &SealedBlock<B>) -> Result<(), ConsensusError> {
86 let ommers_hash = block.body().calculate_ommers_root();
88 if Some(block.ommers_hash()) != ommers_hash {
89 return Err(ConsensusError::BodyOmmersHashDiff(
90 GotExpected {
91 got: ommers_hash.unwrap_or(EMPTY_OMMER_ROOT_HASH),
92 expected: block.ommers_hash(),
93 }
94 .into(),
95 ))
96 }
97
98 if let Err(error) = block.ensure_transaction_root_valid() {
100 return Err(ConsensusError::BodyTransactionRootDiff(error.into()))
101 }
102
103 if self.chain_spec.is_shanghai_active_at_timestamp(block.timestamp()) {
105 canyon::ensure_empty_shanghai_withdrawals(block.body()).map_err(|err| {
106 ConsensusError::Other(format!("failed to verify block {}: {err}", block.number()))
107 })?
108 } else {
109 return Ok(())
110 }
111
112 if self.chain_spec.is_cancun_active_at_timestamp(block.timestamp()) {
113 validate_cancun_gas(block)?;
114 } else {
115 return Ok(())
116 }
117
118 if self.chain_spec.is_isthmus_active_at_timestamp(block.timestamp()) {
120 isthmus::ensure_withdrawals_storage_root_is_some(block.header()).map_err(|err| {
122 ConsensusError::Other(format!("failed to verify block {}: {err}", block.number()))
123 })?
124 } else {
125 canyon::ensure_empty_withdrawals_root(block.header())?
127 }
128
129 Ok(())
130 }
131}
132
133impl<ChainSpec: EthChainSpec + OpHardforks, H: BlockHeader> HeaderValidator<H>
134 for OpBeaconConsensus<ChainSpec>
135{
136 fn validate_header(&self, header: &SealedHeader<H>) -> Result<(), ConsensusError> {
137 validate_header_gas(header.header())?;
138 validate_header_base_fee(header.header(), &self.chain_spec)
139 }
140
141 fn validate_header_against_parent(
142 &self,
143 header: &SealedHeader<H>,
144 parent: &SealedHeader<H>,
145 ) -> Result<(), ConsensusError> {
146 validate_against_parent_hash_number(header.header(), parent)?;
147
148 if self.chain_spec.is_bedrock_active_at_block(header.number()) {
149 validate_against_parent_timestamp(header.header(), parent.header())?;
150 }
151
152 if self.chain_spec.is_holocene_active_at_timestamp(parent.timestamp()) {
157 let header_base_fee =
158 header.base_fee_per_gas().ok_or(ConsensusError::BaseFeeMissing)?;
159 let expected_base_fee =
160 decode_holocene_base_fee(&self.chain_spec, parent.header(), header.timestamp())
161 .map_err(|_| ConsensusError::BaseFeeMissing)?;
162 if expected_base_fee != header_base_fee {
163 return Err(ConsensusError::BaseFeeDiff(GotExpected {
164 expected: expected_base_fee,
165 got: header_base_fee,
166 }))
167 }
168 } else {
169 validate_against_parent_eip1559_base_fee(
170 header.header(),
171 parent.header(),
172 &self.chain_spec,
173 )?;
174 }
175
176 if let Some(blob_params) = self.chain_spec.blob_params_at_timestamp(header.timestamp()) {
178 validate_against_parent_4844(header.header(), parent.header(), blob_params)?;
179 }
180
181 Ok(())
182 }
183
184 fn validate_header_with_total_difficulty(
185 &self,
186 header: &H,
187 _total_difficulty: U256,
188 ) -> Result<(), ConsensusError> {
189 debug_assert!(
191 self.chain_spec.is_bedrock_active_at_block(header.number()),
192 "manually import OVM blocks"
193 );
194
195 if header.nonce() != Some(B64::ZERO) {
196 return Err(ConsensusError::TheMergeNonceIsNotZero)
197 }
198
199 if header.ommers_hash() != EMPTY_OMMER_ROOT_HASH {
200 return Err(ConsensusError::TheMergeOmmerRootIsNotEmpty)
201 }
202
203 validate_header_extra_data(header)?;
213
214 Ok(())
218 }
219}