reth_ethereum_payload_builder/
config.rs

1use alloy_eips::eip1559::ETHEREUM_BLOCK_GAS_LIMIT_30M;
2use reth_primitives_traits::constants::GAS_LIMIT_BOUND_DIVISOR;
3
4/// Settings for the Ethereum builder.
5#[derive(PartialEq, Eq, Clone, Debug)]
6pub struct EthereumBuilderConfig {
7    /// Desired gas limit.
8    pub desired_gas_limit: u64,
9    /// Waits for the first payload to be built if there is no payload built when the payload is
10    /// being resolved.
11    pub await_payload_on_missing: bool,
12    /// Maximum number of blobs to include per block (EIP-7872).
13    ///
14    /// If `None`, defaults to the protocol maximum.
15    pub max_blobs_per_block: Option<u64>,
16}
17
18impl Default for EthereumBuilderConfig {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl EthereumBuilderConfig {
25    /// Create new payload builder config.
26    pub const fn new() -> Self {
27        Self {
28            desired_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT_30M,
29            await_payload_on_missing: true,
30            max_blobs_per_block: None,
31        }
32    }
33
34    /// Set desired gas limit.
35    pub const fn with_gas_limit(mut self, desired_gas_limit: u64) -> Self {
36        self.desired_gas_limit = desired_gas_limit;
37        self
38    }
39
40    /// Configures whether the initial payload should be awaited when the payload job is being
41    /// resolved and no payload has been built yet.
42    pub const fn with_await_payload_on_missing(mut self, await_payload_on_missing: bool) -> Self {
43        self.await_payload_on_missing = await_payload_on_missing;
44        self
45    }
46
47    /// Set the maximum number of blobs per block (EIP-7872).
48    pub const fn with_max_blobs_per_block(mut self, max_blobs_per_block: Option<u64>) -> Self {
49        self.max_blobs_per_block = max_blobs_per_block;
50        self
51    }
52}
53
54impl EthereumBuilderConfig {
55    /// Returns the gas limit for the next block based
56    /// on parent and desired gas limits.
57    pub fn gas_limit(&self, parent_gas_limit: u64) -> u64 {
58        calculate_block_gas_limit(parent_gas_limit, self.desired_gas_limit)
59    }
60}
61
62/// Calculate the gas limit for the next block based on parent and desired gas limits.
63/// Ref: <https://github.com/ethereum/go-ethereum/blob/88cbfab332c96edfbe99d161d9df6a40721bd786/core/block_validator.go#L166>
64pub fn calculate_block_gas_limit(parent_gas_limit: u64, desired_gas_limit: u64) -> u64 {
65    let delta = (parent_gas_limit / GAS_LIMIT_BOUND_DIVISOR).saturating_sub(1);
66    let min_gas_limit = parent_gas_limit - delta;
67    let max_gas_limit = parent_gas_limit + delta;
68    desired_gas_limit.clamp(min_gas_limit, max_gas_limit)
69}