arbos/l2_pricing/
gas_constraint.rs1use alloy_primitives::B256;
2use arb_storage::{StorageBackedUint64, StorageBackend, SystemStateBackend};
3
4use super::L2PricingError;
5
6const TARGET_OFFSET: u64 = 0;
7const ADJUSTMENT_WINDOW_OFFSET: u64 = 1;
8const BACKLOG_OFFSET: u64 = 2;
9
10#[derive(Clone, Copy, Debug)]
12pub struct GasConstraint {
13 target: StorageBackedUint64,
14 adjustment_window: StorageBackedUint64,
15 backlog: StorageBackedUint64,
16}
17
18pub fn open_gas_constraint(base_key: B256) -> GasConstraint {
19 GasConstraint {
20 target: StorageBackedUint64::new(base_key, TARGET_OFFSET),
21 adjustment_window: StorageBackedUint64::new(base_key, ADJUSTMENT_WINDOW_OFFSET),
22 backlog: StorageBackedUint64::new(base_key, BACKLOG_OFFSET),
23 }
24}
25
26impl GasConstraint {
27 pub fn target<B: SystemStateBackend>(&self, backend: &mut B) -> Result<u64, L2PricingError> {
28 Ok(self.target.get(backend)?)
29 }
30
31 pub fn set_target<B: StorageBackend>(
32 &self,
33 backend: &mut B,
34 val: u64,
35 ) -> Result<(), L2PricingError> {
36 Ok(self.target.set(backend, val)?)
37 }
38
39 pub fn adjustment_window<B: SystemStateBackend>(
40 &self,
41 backend: &mut B,
42 ) -> Result<u64, L2PricingError> {
43 Ok(self.adjustment_window.get(backend)?)
44 }
45
46 pub fn set_adjustment_window<B: StorageBackend>(
47 &self,
48 backend: &mut B,
49 val: u64,
50 ) -> Result<(), L2PricingError> {
51 Ok(self.adjustment_window.set(backend, val)?)
52 }
53
54 pub fn backlog<B: SystemStateBackend>(&self, backend: &mut B) -> Result<u64, L2PricingError> {
55 Ok(self.backlog.get(backend)?)
56 }
57
58 pub fn set_backlog<B: StorageBackend>(
59 &self,
60 backend: &mut B,
61 val: u64,
62 ) -> Result<(), L2PricingError> {
63 Ok(self.backlog.set(backend, val)?)
64 }
65
66 pub fn clear<B: StorageBackend>(&self, backend: &mut B) -> Result<(), L2PricingError> {
67 self.target.set(backend, 0)?;
68 self.adjustment_window.set(backend, 0)?;
69 Ok(self.backlog.set(backend, 0)?)
70 }
71}