reth_ethereum_consensus/
lib.rs

1//! Beacon consensus implementation.
2
3#![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(not(test), warn(unused_crate_dependencies))]
9#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
10#![cfg_attr(not(feature = "std"), no_std)]
11
12extern crate alloc;
13
14use alloc::{fmt::Debug, sync::Arc};
15use alloy_consensus::EMPTY_OMMER_ROOT_HASH;
16use alloy_eips::eip7840::BlobParams;
17use alloy_primitives::U256;
18use reth_chainspec::{EthChainSpec, EthereumHardforks};
19use reth_consensus::{Consensus, ConsensusError, FullConsensus, HeaderValidator};
20use reth_consensus_common::validation::{
21    validate_4844_header_standalone, validate_against_parent_4844,
22    validate_against_parent_eip1559_base_fee, validate_against_parent_hash_number,
23    validate_against_parent_timestamp, validate_block_pre_execution, validate_body_against_header,
24    validate_header_base_fee, validate_header_extra_data, validate_header_gas,
25};
26use reth_execution_types::BlockExecutionResult;
27use reth_primitives_traits::{
28    constants::{GAS_LIMIT_BOUND_DIVISOR, MINIMUM_GAS_LIMIT},
29    Block, BlockHeader, NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader,
30};
31
32mod validation;
33pub use validation::validate_block_post_execution;
34
35/// Ethereum beacon consensus
36///
37/// This consensus engine does basic checks as outlined in the execution specs.
38#[derive(Debug, Clone)]
39pub struct EthBeaconConsensus<ChainSpec> {
40    /// Configuration
41    chain_spec: Arc<ChainSpec>,
42}
43
44impl<ChainSpec: EthChainSpec + EthereumHardforks> EthBeaconConsensus<ChainSpec> {
45    /// Create a new instance of [`EthBeaconConsensus`]
46    pub const fn new(chain_spec: Arc<ChainSpec>) -> Self {
47        Self { chain_spec }
48    }
49
50    /// Checks the gas limit for consistency between parent and self headers.
51    ///
52    /// The maximum allowable difference between self and parent gas limits is determined by the
53    /// parent's gas limit divided by the [`GAS_LIMIT_BOUND_DIVISOR`].
54    fn validate_against_parent_gas_limit<H: BlockHeader>(
55        &self,
56        header: &SealedHeader<H>,
57        parent: &SealedHeader<H>,
58    ) -> Result<(), ConsensusError> {
59        // Determine the parent gas limit, considering elasticity multiplier on the London fork.
60        let parent_gas_limit = if !self.chain_spec.is_london_active_at_block(parent.number()) &&
61            self.chain_spec.is_london_active_at_block(header.number())
62        {
63            parent.gas_limit() *
64                self.chain_spec
65                    .base_fee_params_at_timestamp(header.timestamp())
66                    .elasticity_multiplier as u64
67        } else {
68            parent.gas_limit()
69        };
70
71        // Check for an increase in gas limit beyond the allowed threshold.
72        if header.gas_limit() > parent_gas_limit {
73            if header.gas_limit() - parent_gas_limit >= parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR {
74                return Err(ConsensusError::GasLimitInvalidIncrease {
75                    parent_gas_limit,
76                    child_gas_limit: header.gas_limit(),
77                })
78            }
79        }
80        // Check for a decrease in gas limit beyond the allowed threshold.
81        else if parent_gas_limit - header.gas_limit() >=
82            parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR
83        {
84            return Err(ConsensusError::GasLimitInvalidDecrease {
85                parent_gas_limit,
86                child_gas_limit: header.gas_limit(),
87            })
88        }
89        // Check if the self gas limit is below the minimum required limit.
90        else if header.gas_limit() < MINIMUM_GAS_LIMIT {
91            return Err(ConsensusError::GasLimitInvalidMinimum {
92                child_gas_limit: header.gas_limit(),
93            })
94        }
95
96        Ok(())
97    }
98}
99
100impl<ChainSpec, N> FullConsensus<N> for EthBeaconConsensus<ChainSpec>
101where
102    ChainSpec: Send + Sync + EthChainSpec + EthereumHardforks + Debug,
103    N: NodePrimitives,
104{
105    fn validate_block_post_execution(
106        &self,
107        block: &RecoveredBlock<N::Block>,
108        result: &BlockExecutionResult<N::Receipt>,
109    ) -> Result<(), ConsensusError> {
110        validate_block_post_execution(block, &self.chain_spec, &result.receipts, &result.requests)
111    }
112}
113
114impl<B, ChainSpec: Send + Sync + EthChainSpec + EthereumHardforks + Debug> Consensus<B>
115    for EthBeaconConsensus<ChainSpec>
116where
117    B: Block,
118{
119    type Error = ConsensusError;
120
121    fn validate_body_against_header(
122        &self,
123        body: &B::Body,
124        header: &SealedHeader<B::Header>,
125    ) -> Result<(), Self::Error> {
126        validate_body_against_header(body, header.header())
127    }
128
129    fn validate_block_pre_execution(&self, block: &SealedBlock<B>) -> Result<(), Self::Error> {
130        validate_block_pre_execution(block, &self.chain_spec)
131    }
132}
133
134impl<H, ChainSpec: Send + Sync + EthChainSpec + EthereumHardforks + Debug> HeaderValidator<H>
135    for EthBeaconConsensus<ChainSpec>
136where
137    H: BlockHeader,
138{
139    fn validate_header(&self, header: &SealedHeader<H>) -> Result<(), ConsensusError> {
140        validate_header_gas(header.header())?;
141        validate_header_base_fee(header.header(), &self.chain_spec)?;
142
143        // EIP-4895: Beacon chain push withdrawals as operations
144        if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp()) &&
145            header.withdrawals_root().is_none()
146        {
147            return Err(ConsensusError::WithdrawalsRootMissing)
148        } else if !self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp()) &&
149            header.withdrawals_root().is_some()
150        {
151            return Err(ConsensusError::WithdrawalsRootUnexpected)
152        }
153
154        // Ensures that EIP-4844 fields are valid once cancun is active.
155        if self.chain_spec.is_cancun_active_at_timestamp(header.timestamp()) {
156            validate_4844_header_standalone(
157                header.header(),
158                self.chain_spec
159                    .blob_params_at_timestamp(header.timestamp())
160                    .unwrap_or_else(BlobParams::cancun),
161            )?;
162        } else if header.blob_gas_used().is_some() {
163            return Err(ConsensusError::BlobGasUsedUnexpected)
164        } else if header.excess_blob_gas().is_some() {
165            return Err(ConsensusError::ExcessBlobGasUnexpected)
166        } else if header.parent_beacon_block_root().is_some() {
167            return Err(ConsensusError::ParentBeaconBlockRootUnexpected)
168        }
169
170        if self.chain_spec.is_prague_active_at_timestamp(header.timestamp()) {
171            if header.requests_hash().is_none() {
172                return Err(ConsensusError::RequestsHashMissing)
173            }
174        } else if header.requests_hash().is_some() {
175            return Err(ConsensusError::RequestsHashUnexpected)
176        }
177
178        Ok(())
179    }
180
181    fn validate_header_against_parent(
182        &self,
183        header: &SealedHeader<H>,
184        parent: &SealedHeader<H>,
185    ) -> Result<(), ConsensusError> {
186        validate_against_parent_hash_number(header.header(), parent)?;
187
188        validate_against_parent_timestamp(header.header(), parent.header())?;
189
190        // TODO Check difficulty increment between parent and self
191        // Ace age did increment it by some formula that we need to follow.
192        self.validate_against_parent_gas_limit(header, parent)?;
193
194        validate_against_parent_eip1559_base_fee(
195            header.header(),
196            parent.header(),
197            &self.chain_spec,
198        )?;
199
200        // ensure that the blob gas fields for this block
201        if let Some(blob_params) = self.chain_spec.blob_params_at_timestamp(header.timestamp()) {
202            validate_against_parent_4844(header.header(), parent.header(), blob_params)?;
203        }
204
205        Ok(())
206    }
207
208    fn validate_header_with_total_difficulty(
209        &self,
210        header: &H,
211        _total_difficulty: U256,
212    ) -> Result<(), ConsensusError> {
213        let is_post_merge = self.chain_spec.is_paris_active_at_block(header.number());
214
215        if is_post_merge {
216            if !header.difficulty().is_zero() {
217                return Err(ConsensusError::TheMergeDifficultyIsNotZero)
218            }
219
220            if !header.nonce().is_some_and(|nonce| nonce.is_zero()) {
221                return Err(ConsensusError::TheMergeNonceIsNotZero)
222            }
223
224            if header.ommers_hash() != EMPTY_OMMER_ROOT_HASH {
225                return Err(ConsensusError::TheMergeOmmerRootIsNotEmpty)
226            }
227
228            // Post-merge, the consensus layer is expected to perform checks such that the block
229            // timestamp is a function of the slot. This is different from pre-merge, where blocks
230            // are only allowed to be in the future (compared to the system's clock) by a certain
231            // threshold.
232            //
233            // Block validation with respect to the parent should ensure that the block timestamp
234            // is greater than its parent timestamp.
235
236            // validate header extra data for all networks post merge
237            validate_header_extra_data(header)?;
238
239            // mixHash is used instead of difficulty inside EVM
240            // https://eips.ethereum.org/EIPS/eip-4399#using-mixhash-field-instead-of-difficulty
241        } else {
242            // Note: This does not perform any pre merge checks for difficulty, mix_hash & nonce
243            // because those are deprecated and the expectation is that a reth node syncs in reverse
244            // order, making those checks obsolete.
245
246            // Check if timestamp is in the future. Clock can drift but this can be consensus issue.
247            #[cfg(feature = "std")]
248            {
249                let present_timestamp = std::time::SystemTime::now()
250                    .duration_since(std::time::SystemTime::UNIX_EPOCH)
251                    .unwrap()
252                    .as_secs();
253                if header.timestamp() >
254                    present_timestamp + alloy_eips::merge::ALLOWED_FUTURE_BLOCK_TIME_SECONDS
255                {
256                    return Err(ConsensusError::TimestampIsInFuture {
257                        timestamp: header.timestamp(),
258                        present_timestamp,
259                    })
260                }
261            }
262
263            validate_header_extra_data(header)?;
264        }
265
266        Ok(())
267    }
268}
269
270#[cfg(test)]
271mod tests {
272    use super::*;
273    use alloy_primitives::B256;
274    use reth_chainspec::{ChainSpec, ChainSpecBuilder};
275    use reth_primitives_traits::proofs;
276
277    fn header_with_gas_limit(gas_limit: u64) -> SealedHeader {
278        let header = reth_primitives_traits::Header { gas_limit, ..Default::default() };
279        SealedHeader::new(header, B256::ZERO)
280    }
281
282    #[test]
283    fn test_valid_gas_limit_increase() {
284        let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
285        let child = header_with_gas_limit((parent.gas_limit + 5) as u64);
286
287        assert_eq!(
288            EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
289                .validate_against_parent_gas_limit(&child, &parent),
290            Ok(())
291        );
292    }
293
294    #[test]
295    fn test_gas_limit_below_minimum() {
296        let parent = header_with_gas_limit(MINIMUM_GAS_LIMIT);
297        let child = header_with_gas_limit(MINIMUM_GAS_LIMIT - 1);
298
299        assert_eq!(
300            EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
301                .validate_against_parent_gas_limit(&child, &parent),
302            Err(ConsensusError::GasLimitInvalidMinimum { child_gas_limit: child.gas_limit as u64 })
303        );
304    }
305
306    #[test]
307    fn test_invalid_gas_limit_increase_exceeding_limit() {
308        let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
309        let child = header_with_gas_limit(
310            parent.gas_limit + parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR + 1,
311        );
312
313        assert_eq!(
314            EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
315                .validate_against_parent_gas_limit(&child, &parent),
316            Err(ConsensusError::GasLimitInvalidIncrease {
317                parent_gas_limit: parent.gas_limit,
318                child_gas_limit: child.gas_limit,
319            })
320        );
321    }
322
323    #[test]
324    fn test_valid_gas_limit_decrease_within_limit() {
325        let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
326        let child = header_with_gas_limit(parent.gas_limit - 5);
327
328        assert_eq!(
329            EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
330                .validate_against_parent_gas_limit(&child, &parent),
331            Ok(())
332        );
333    }
334
335    #[test]
336    fn test_invalid_gas_limit_decrease_exceeding_limit() {
337        let parent = header_with_gas_limit(GAS_LIMIT_BOUND_DIVISOR * 10);
338        let child = header_with_gas_limit(
339            parent.gas_limit - parent.gas_limit / GAS_LIMIT_BOUND_DIVISOR - 1,
340        );
341
342        assert_eq!(
343            EthBeaconConsensus::new(Arc::new(ChainSpec::default()))
344                .validate_against_parent_gas_limit(&child, &parent),
345            Err(ConsensusError::GasLimitInvalidDecrease {
346                parent_gas_limit: parent.gas_limit,
347                child_gas_limit: child.gas_limit,
348            })
349        );
350    }
351
352    #[test]
353    fn shanghai_block_zero_withdrawals() {
354        // ensures that if shanghai is activated, and we include a block with a withdrawals root,
355        // that the header is valid
356        let chain_spec = Arc::new(ChainSpecBuilder::mainnet().shanghai_activated().build());
357
358        let header = reth_primitives_traits::Header {
359            base_fee_per_gas: Some(1337),
360            withdrawals_root: Some(proofs::calculate_withdrawals_root(&[])),
361            ..Default::default()
362        };
363
364        assert_eq!(
365            EthBeaconConsensus::new(chain_spec).validate_header(&SealedHeader::seal_slow(header,)),
366            Ok(())
367        );
368    }
369}