reth_optimism_chainspec/
basefee.rs

1//! Base fee related utilities for Optimism chains.
2
3use alloy_consensus::BlockHeader;
4use op_alloy_consensus::{decode_holocene_extra_data, decode_jovian_extra_data, EIP1559ParamError};
5use reth_chainspec::{BaseFeeParams, EthChainSpec};
6use reth_optimism_forks::OpHardforks;
7
8fn next_base_fee_params<H: BlockHeader>(
9    chain_spec: impl EthChainSpec + OpHardforks,
10    parent: &H,
11    timestamp: u64,
12    denominator: u32,
13    elasticity: u32,
14) -> u64 {
15    let base_fee_params = if elasticity == 0 && denominator == 0 {
16        chain_spec.base_fee_params_at_timestamp(timestamp)
17    } else {
18        BaseFeeParams::new(denominator as u128, elasticity as u128)
19    };
20
21    parent.next_block_base_fee(base_fee_params).unwrap_or_default()
22}
23
24/// Extracts the Holocene 1599 parameters from the encoded extra data from the parent header.
25///
26/// Caution: Caller must ensure that holocene is active in the parent header.
27///
28/// See also [Base fee computation](https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#base-fee-computation)
29pub fn decode_holocene_base_fee<H>(
30    chain_spec: impl EthChainSpec + OpHardforks,
31    parent: &H,
32    timestamp: u64,
33) -> Result<u64, EIP1559ParamError>
34where
35    H: BlockHeader,
36{
37    let (elasticity, denominator) = decode_holocene_extra_data(parent.extra_data())?;
38
39    Ok(next_base_fee_params(chain_spec, parent, timestamp, denominator, elasticity))
40}
41
42/// Extracts the Jovian 1599 parameters from the encoded extra data from the parent header.
43/// Additionally to [`decode_holocene_base_fee`], checks if the next block base fee is less than the
44/// minimum base fee, then the minimum base fee is returned.
45///
46/// Caution: Caller must ensure that jovian is active in the parent header.
47///
48/// See also [Base fee computation](https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/jovian/exec-engine.md#base-fee-computation)
49/// and [Minimum base fee in block header](https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/jovian/exec-engine.md#minimum-base-fee-in-block-header)
50pub fn compute_jovian_base_fee<H>(
51    chain_spec: impl EthChainSpec + OpHardforks,
52    parent: &H,
53    timestamp: u64,
54) -> Result<u64, EIP1559ParamError>
55where
56    H: BlockHeader,
57{
58    let (elasticity, denominator, min_base_fee) = decode_jovian_extra_data(parent.extra_data())?;
59
60    let next_base_fee =
61        next_base_fee_params(chain_spec, parent, timestamp, denominator, elasticity);
62
63    if next_base_fee < min_base_fee {
64        return Ok(min_base_fee);
65    }
66
67    Ok(next_base_fee)
68}