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    /// Time-based scheduled updates to blob parameters.
26    ///
27    /// These are ordered by activation timestamps in natural order.
28    pub scheduled: Vec<(u64, BlobParams)>,
29}
30
31impl BlobScheduleBlobParams {
32    /// Returns the blob schedule for the ethereum mainnet.
33    pub fn mainnet() -> Self {
34        Self {
35            cancun: BlobParams::cancun(),
36            prague: BlobParams::prague(),
37            scheduled: Default::default(),
38        }
39    }
40
41    /// Returns the highest active blob parameters at the given timestamp.
42    ///
43    /// Note: this does only scan the entries scheduled by timestamp and not cancun or prague.
44    pub fn active_scheduled_params_at_timestamp(&self, timestamp: u64) -> Option<&BlobParams> {
45        self.scheduled.iter().rev().find(|(ts, _)| timestamp >= *ts).map(|(_, params)| params)
46    }
47
48    /// Returns the configured cancun [`BlobParams`].
49    pub const fn cancun(&self) -> &BlobParams {
50        &self.cancun
51    }
52
53    /// Returns the configured prague [`BlobParams`].
54    pub const fn prague(&self) -> &BlobParams {
55        &self.prague
56    }
57
58    /// Finds the active scheduled blob parameters for a given timestamp.
59    pub fn from_schedule(schedule: &BTreeMap<String, BlobParams>) -> Self {
60        let mut cancun = None;
61        let mut prague = None;
62        let mut scheduled = Vec::new();
63
64        for (key, params) in schedule {
65            match key.as_str() {
66                "cancun" => cancun = Some(*params),
67                "prague" => prague = Some(*params),
68                _ => {
69                    if let Ok(timestamp) = key.parse::<u64>() {
70                        scheduled.push((timestamp, *params));
71                    }
72                }
73            }
74        }
75
76        scheduled.sort_by_key(|(timestamp, _)| *timestamp);
77
78        Self {
79            cancun: cancun.unwrap_or_else(BlobParams::cancun),
80            prague: prague.unwrap_or_else(BlobParams::prague),
81            scheduled,
82        }
83    }
84}
85
86impl Default for BlobScheduleBlobParams {
87    fn default() -> Self {
88        Self::mainnet()
89    }
90}