reth_ethereum_payload_builder/
config.rs

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