alloy_eips/
eip7892.rs

1//! Contains constants and helper functions for [EIP-7892](https://github.com/ethereum/EIPs/tree/master/EIPS/eip-7892.md)
2
3use crate::eip7840::BlobParams;
4use alloc::{collections::BTreeMap, string::String, vec::Vec};
5
6/// A scheduled blob parameter update entry.
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum BlobScheduleEntry {
10    /// Blob parameters for the Cancun hardfork
11    Cancun(BlobParams),
12    /// Blob parameters for the Prague hardfork
13    Prague(BlobParams),
14    /// Blob parameters that take effect at a specific timestamp
15    TimestampUpdate(u64, BlobParams),
16}
17
18/// Blob parameters configuration for a chain, including scheduled updates.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct BlobScheduleBlobParams {
21    /// Configuration for blob-related calculations for the Cancun hardfork.
22    pub cancun: BlobParams,
23    /// Configuration for blob-related calculations for the Prague hardfork.
24    pub prague: BlobParams,
25    /// Configuration for blob-related calculations for the Osaka hardfork.
26    pub osaka: BlobParams,
27    /// Time-based scheduled updates to blob parameters.
28    ///
29    /// These are ordered by activation timestamps in natural order.
30    pub scheduled: Vec<(u64, BlobParams)>,
31}
32
33impl BlobScheduleBlobParams {
34    /// Returns the blob schedule for the ethereum mainnet.
35    pub fn mainnet() -> Self {
36        Self {
37            cancun: BlobParams::cancun(),
38            prague: BlobParams::prague(),
39            osaka: BlobParams::osaka(),
40            scheduled: Default::default(),
41        }
42    }
43
44    /// Returns the highest active blob parameters at the given timestamp.
45    ///
46    /// Note: this does only scan the entries scheduled by timestamp and not cancun or prague.
47    pub fn active_scheduled_params_at_timestamp(&self, timestamp: u64) -> Option<&BlobParams> {
48        self.scheduled.iter().rev().find(|(ts, _)| timestamp >= *ts).map(|(_, params)| params)
49    }
50
51    /// Returns the configured Cancun [`BlobParams`].
52    pub const fn cancun(&self) -> &BlobParams {
53        &self.cancun
54    }
55
56    /// Returns the configured Prague [`BlobParams`].
57    pub const fn prague(&self) -> &BlobParams {
58        &self.prague
59    }
60
61    /// Returns the configured Osaka [`BlobParams`].
62    pub const fn osaka(&self) -> &BlobParams {
63        &self.osaka
64    }
65
66    /// Finds the active scheduled blob parameters for a given timestamp.
67    pub fn from_schedule(schedule: &BTreeMap<String, BlobParams>) -> Self {
68        let mut cancun = None;
69        let mut prague = None;
70        let mut osaka = None;
71        let mut scheduled = Vec::new();
72
73        for (key, params) in schedule {
74            match key.as_str() {
75                "cancun" => cancun = Some(*params),
76                "prague" => prague = Some(*params),
77                "osaka" => osaka = Some(*params),
78                _ => {
79                    if let Ok(timestamp) = key.parse::<u64>() {
80                        scheduled.push((timestamp, *params));
81                    }
82                }
83            }
84        }
85
86        scheduled.sort_by_key(|(timestamp, _)| *timestamp);
87
88        Self {
89            cancun: cancun.unwrap_or_else(BlobParams::cancun),
90            prague: prague.unwrap_or_else(BlobParams::prague),
91            osaka: osaka.unwrap_or_else(BlobParams::osaka),
92            scheduled,
93        }
94    }
95}
96
97impl Default for BlobScheduleBlobParams {
98    fn default() -> Self {
99        Self::mainnet()
100    }
101}