reth_optimism_payload_builder/
config.rs

1//! Additional configuration for the OP builder
2
3use std::sync::{atomic::AtomicU64, Arc};
4
5/// Settings for the OP builder.
6#[derive(Debug, Clone, Default)]
7pub struct OpBuilderConfig {
8    /// Data availability configuration for the OP builder.
9    pub da_config: OpDAConfig,
10    /// Gas limit configuration for the OP builder.
11    pub gas_limit_config: OpGasLimitConfig,
12}
13
14impl OpBuilderConfig {
15    /// Creates a new OP builder configuration with the given data availability configuration.
16    pub const fn new(da_config: OpDAConfig, gas_limit_config: OpGasLimitConfig) -> Self {
17        Self { da_config, gas_limit_config }
18    }
19
20    /// Returns the Data Availability configuration for the OP builder, if it has configured
21    /// constraints.
22    pub fn constrained_da_config(&self) -> Option<&OpDAConfig> {
23        if self.da_config.is_empty() {
24            None
25        } else {
26            Some(&self.da_config)
27        }
28    }
29}
30
31/// Contains the Data Availability configuration for the OP builder.
32///
33/// This type is shareable and can be used to update the DA configuration for the OP payload
34/// builder.
35#[derive(Debug, Clone, Default)]
36pub struct OpDAConfig {
37    inner: Arc<OpDAConfigInner>,
38}
39
40impl OpDAConfig {
41    /// Creates a new Data Availability configuration with the given maximum sizes.
42    pub fn new(max_da_tx_size: u64, max_da_block_size: u64) -> Self {
43        let this = Self::default();
44        this.set_max_da_size(max_da_tx_size, max_da_block_size);
45        this
46    }
47
48    /// Returns whether the configuration is empty.
49    pub fn is_empty(&self) -> bool {
50        self.max_da_tx_size().is_none() && self.max_da_block_size().is_none()
51    }
52
53    /// Returns the max allowed data availability size per transactions, if any.
54    pub fn max_da_tx_size(&self) -> Option<u64> {
55        let val = self.inner.max_da_tx_size.load(std::sync::atomic::Ordering::Relaxed);
56        if val == 0 {
57            None
58        } else {
59            Some(val)
60        }
61    }
62
63    /// Returns the max allowed data availability size per block, if any.
64    pub fn max_da_block_size(&self) -> Option<u64> {
65        let val = self.inner.max_da_block_size.load(std::sync::atomic::Ordering::Relaxed);
66        if val == 0 {
67            None
68        } else {
69            Some(val)
70        }
71    }
72
73    /// Sets the maximum data availability size currently allowed for inclusion. 0 means no maximum.
74    pub fn set_max_da_size(&self, max_da_tx_size: u64, max_da_block_size: u64) {
75        self.set_max_tx_size(max_da_tx_size);
76        self.set_max_block_size(max_da_block_size);
77    }
78
79    /// Sets the maximum data availability size per transaction currently allowed for inclusion. 0
80    /// means no maximum.
81    pub fn set_max_tx_size(&self, max_da_tx_size: u64) {
82        self.inner.max_da_tx_size.store(max_da_tx_size, std::sync::atomic::Ordering::Relaxed);
83    }
84
85    /// Sets the maximum data availability size per block currently allowed for inclusion. 0 means
86    /// no maximum.
87    pub fn set_max_block_size(&self, max_da_block_size: u64) {
88        self.inner.max_da_block_size.store(max_da_block_size, std::sync::atomic::Ordering::Relaxed);
89    }
90}
91
92#[derive(Debug, Default)]
93struct OpDAConfigInner {
94    /// Don't include any transactions with data availability size larger than this in any built
95    /// block
96    ///
97    /// 0 means no limit.
98    max_da_tx_size: AtomicU64,
99    /// Maximum total data availability size for a block
100    ///
101    /// 0 means no limit.
102    max_da_block_size: AtomicU64,
103}
104
105/// Contains the Gas Limit configuration for the OP builder.
106///
107/// This type is shareable and can be used to update the Gas Limit configuration for the OP payload
108/// builder.
109#[derive(Debug, Clone, Default)]
110pub struct OpGasLimitConfig {
111    /// Gas limit for a transaction
112    ///
113    /// 0 means use the default gas limit.
114    gas_limit: Arc<AtomicU64>,
115}
116
117impl OpGasLimitConfig {
118    /// Creates a new Gas Limit configuration with the given maximum gas limit.
119    pub fn new(max_gas_limit: u64) -> Self {
120        let this = Self::default();
121        this.set_gas_limit(max_gas_limit);
122        this
123    }
124    /// Returns the gas limit for a transaction, if any.
125    pub fn gas_limit(&self) -> Option<u64> {
126        let val = self.gas_limit.load(std::sync::atomic::Ordering::Relaxed);
127        if val == 0 {
128            None
129        } else {
130            Some(val)
131        }
132    }
133    /// Sets the gas limit for a transaction. 0 means use the default gas limit.
134    pub fn set_gas_limit(&self, gas_limit: u64) {
135        self.gas_limit.store(gas_limit, std::sync::atomic::Ordering::Relaxed);
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142
143    #[test]
144    fn test_da() {
145        let da = OpDAConfig::default();
146        assert_eq!(da.max_da_tx_size(), None);
147        assert_eq!(da.max_da_block_size(), None);
148        da.set_max_da_size(100, 200);
149        assert_eq!(da.max_da_tx_size(), Some(100));
150        assert_eq!(da.max_da_block_size(), Some(200));
151        da.set_max_da_size(0, 0);
152        assert_eq!(da.max_da_tx_size(), None);
153        assert_eq!(da.max_da_block_size(), None);
154    }
155
156    #[test]
157    fn test_da_constrained() {
158        let config = OpBuilderConfig::default();
159        assert!(config.constrained_da_config().is_none());
160    }
161
162    #[test]
163    fn test_gas_limit() {
164        let gas_limit = OpGasLimitConfig::default();
165        assert_eq!(gas_limit.gas_limit(), None);
166        gas_limit.set_gas_limit(50000);
167        assert_eq!(gas_limit.gas_limit(), Some(50000));
168        gas_limit.set_gas_limit(0);
169        assert_eq!(gas_limit.gas_limit(), None);
170    }
171}