1//! Additional configuration for the OP builder
23use std::sync::{atomic::AtomicU64, Arc};
45/// Settings for the OP builder.
6#[derive(Debug, Clone, Default)]
7pub struct OpBuilderConfig {
8/// Data availability configuration for the OP builder.
9pub da_config: OpDAConfig,
10}
1112impl OpBuilderConfig {
13/// Creates a new OP builder configuration with the given data availability configuration.
14pub const fn new(da_config: OpDAConfig) -> Self {
15Self { da_config }
16 }
1718/// Returns the Data Availability configuration for the OP builder, if it has configured
19 /// constraints.
20pub fn constrained_da_config(&self) -> Option<&OpDAConfig> {
21if self.da_config.is_empty() {
22None23 } else {
24Some(&self.da_config)
25 }
26 }
27}
2829/// 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}
3738impl OpDAConfig {
39/// Creates a new Data Availability configuration with the given maximum sizes.
40pub fn new(max_da_tx_size: u64, max_da_block_size: u64) -> Self {
41let this = Self::default();
42this.set_max_da_size(max_da_tx_size, max_da_block_size);
43this44 }
4546/// Returns whether the configuration is empty.
47pub fn is_empty(&self) -> bool {
48self.max_da_tx_size().is_none() && self.max_da_block_size().is_none()
49 }
5051/// Returns the max allowed data availability size per transactions, if any.
52pub fn max_da_tx_size(&self) -> Option<u64> {
53let val = self.inner.max_da_tx_size.load(std::sync::atomic::Ordering::Relaxed);
54if val == 0 {
55None56 } else {
57Some(val)
58 }
59 }
6061/// Returns the max allowed data availability size per block, if any.
62pub fn max_da_block_size(&self) -> Option<u64> {
63let val = self.inner.max_da_block_size.load(std::sync::atomic::Ordering::Relaxed);
64if val == 0 {
65None66 } else {
67Some(val)
68 }
69 }
7071/// Sets the maximum data availability size currently allowed for inclusion. 0 means no maximum.
72pub fn set_max_da_size(&self, max_da_tx_size: u64, max_da_block_size: u64) {
73self.set_max_tx_size(max_da_tx_size);
74self.set_max_block_size(max_da_block_size);
75 }
7677/// Sets the maximum data availability size per transaction currently allowed for inclusion. 0
78 /// means no maximum.
79pub fn set_max_tx_size(&self, max_da_tx_size: u64) {
80self.inner.max_da_tx_size.store(max_da_tx_size, std::sync::atomic::Ordering::Relaxed);
81 }
8283/// Sets the maximum data availability size per block currently allowed for inclusion. 0 means
84 /// no maximum.
85pub fn set_max_block_size(&self, max_da_block_size: u64) {
86self.inner.max_da_block_size.store(max_da_block_size, std::sync::atomic::Ordering::Relaxed);
87 }
88}
8990#[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.
96max_da_tx_size: AtomicU64,
97/// Maximum total data availability size for a block
98 ///
99 /// 0 means no limit.
100max_da_block_size: AtomicU64,
101}
102103#[cfg(test)]
104mod tests {
105use super::*;
106107#[test]
108fn test_da() {
109let da = OpDAConfig::default();
110assert_eq!(da.max_da_tx_size(), None);
111assert_eq!(da.max_da_block_size(), None);
112 da.set_max_da_size(100, 200);
113assert_eq!(da.max_da_tx_size(), Some(100));
114assert_eq!(da.max_da_block_size(), Some(200));
115 da.set_max_da_size(0, 0);
116assert_eq!(da.max_da_tx_size(), None);
117assert_eq!(da.max_da_block_size(), None);
118 }
119120#[test]
121fn test_da_constrained() {
122let config = OpBuilderConfig::default();
123assert!(config.constrained_da_config().is_none());
124 }
125}