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}
11
12impl OpBuilderConfig {
13    /// Creates a new OP builder configuration with the given data availability configuration.
14    pub const fn new(da_config: OpDAConfig) -> Self {
15        Self { da_config }
16    }
17
18    /// Returns the Data Availability configuration for the OP builder, if it has configured
19    /// constraints.
20    pub fn constrained_da_config(&self) -> Option<&OpDAConfig> {
21        if self.da_config.is_empty() {
22            None
23        } else {
24            Some(&self.da_config)
25        }
26    }
27}
28
29/// Contains the Data Availability configuration for the OP builder.
30///
31/// This type is shareable and can be used to update the DA configuration for the OP payload
32/// builder.
33#[derive(Debug, Clone, Default)]
34pub struct OpDAConfig {
35    inner: Arc<OpDAConfigInner>,
36}
37
38impl OpDAConfig {
39    /// Creates a new Data Availability configuration with the given maximum sizes.
40    pub fn new(max_da_tx_size: u64, max_da_block_size: u64) -> Self {
41        let this = Self::default();
42        this.set_max_da_size(max_da_tx_size, max_da_block_size);
43        this
44    }
45
46    /// Returns whether the configuration is empty.
47    pub fn is_empty(&self) -> bool {
48        self.max_da_tx_size().is_none() && self.max_da_block_size().is_none()
49    }
50
51    /// Returns the max allowed data availability size per transactions, if any.
52    pub fn max_da_tx_size(&self) -> Option<u64> {
53        let val = self.inner.max_da_tx_size.load(std::sync::atomic::Ordering::Relaxed);
54        if val == 0 {
55            None
56        } else {
57            Some(val)
58        }
59    }
60
61    /// Returns the max allowed data availability size per block, if any.
62    pub fn max_da_block_size(&self) -> Option<u64> {
63        let val = self.inner.max_da_block_size.load(std::sync::atomic::Ordering::Relaxed);
64        if val == 0 {
65            None
66        } else {
67            Some(val)
68        }
69    }
70
71    /// Sets the maximum data availability size currently allowed for inclusion. 0 means no maximum.
72    pub fn set_max_da_size(&self, max_da_tx_size: u64, max_da_block_size: u64) {
73        self.set_max_tx_size(max_da_tx_size);
74        self.set_max_block_size(max_da_block_size);
75    }
76
77    /// Sets the maximum data availability size per transaction currently allowed for inclusion. 0
78    /// means no maximum.
79    pub fn set_max_tx_size(&self, max_da_tx_size: u64) {
80        self.inner.max_da_tx_size.store(max_da_tx_size, std::sync::atomic::Ordering::Relaxed);
81    }
82
83    /// Sets the maximum data availability size per block currently allowed for inclusion. 0 means
84    /// no maximum.
85    pub fn set_max_block_size(&self, max_da_block_size: u64) {
86        self.inner.max_da_block_size.store(max_da_block_size, std::sync::atomic::Ordering::Relaxed);
87    }
88}
89
90#[derive(Debug, Default)]
91struct OpDAConfigInner {
92    /// Don't include any transactions with data availability size larger than this in any built
93    /// block
94    ///
95    /// 0 means no limit.
96    max_da_tx_size: AtomicU64,
97    /// Maximum total data availability size for a block
98    ///
99    /// 0 means no limit.
100    max_da_block_size: AtomicU64,
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_da() {
109        let da = OpDAConfig::default();
110        assert_eq!(da.max_da_tx_size(), None);
111        assert_eq!(da.max_da_block_size(), None);
112        da.set_max_da_size(100, 200);
113        assert_eq!(da.max_da_tx_size(), Some(100));
114        assert_eq!(da.max_da_block_size(), Some(200));
115        da.set_max_da_size(0, 0);
116        assert_eq!(da.max_da_tx_size(), None);
117        assert_eq!(da.max_da_block_size(), None);
118    }
119
120    #[test]
121    fn test_da_constrained() {
122        let config = OpBuilderConfig::default();
123        assert!(config.constrained_da_config().is_none());
124    }
125}