alloy_eips/
eip7840.rs

1//! Contains constants and utility functions for [EIP-7840](https://github.com/ethereum/EIPs/tree/master/EIPS/eip-7840.md)
2
3use crate::{eip4844, eip4844::DATA_GAS_PER_BLOB, eip7691};
4
5// helpers for serde
6#[cfg(feature = "serde")]
7const DEFAULT_BLOB_FEE_GETTER: fn() -> u128 = || eip4844::BLOB_TX_MIN_BLOB_GASPRICE;
8#[cfg(feature = "serde")]
9const IS_DEFAULT_BLOB_FEE: fn(&u128) -> bool = |&x| x == eip4844::BLOB_TX_MIN_BLOB_GASPRICE;
10
11/// Configuration for the blob-related calculations.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct BlobParams {
15    /// Target blob count for the block.
16    #[cfg_attr(feature = "serde", serde(rename = "target"))]
17    pub target_blob_count: u64,
18    /// Max blob count for the block.
19    #[cfg_attr(feature = "serde", serde(rename = "max"))]
20    pub max_blob_count: u64,
21    /// Update fraction for excess blob gas calculation.
22    #[cfg_attr(feature = "serde", serde(rename = "baseFeeUpdateFraction"))]
23    pub update_fraction: u128,
24    /// Minimum gas price for a data blob.
25    ///
26    /// Not required per EIP-7840 and assumed to be the default
27    /// [`eip4844::BLOB_TX_MIN_BLOB_GASPRICE`] if not set.
28    #[cfg_attr(
29        feature = "serde",
30        serde(default = "DEFAULT_BLOB_FEE_GETTER", skip_serializing_if = "IS_DEFAULT_BLOB_FEE")
31    )]
32    pub min_blob_fee: u128,
33}
34
35impl BlobParams {
36    /// Returns [`BlobParams`] configuration activated with Cancun hardfork.
37    pub const fn cancun() -> Self {
38        Self {
39            target_blob_count: eip4844::TARGET_BLOBS_PER_BLOCK,
40            max_blob_count: eip4844::MAX_BLOBS_PER_BLOCK as u64,
41            update_fraction: eip4844::BLOB_GASPRICE_UPDATE_FRACTION,
42            min_blob_fee: eip4844::BLOB_TX_MIN_BLOB_GASPRICE,
43        }
44    }
45
46    /// Returns [`BlobParams`] configuration activated with Prague hardfork.
47    pub const fn prague() -> Self {
48        Self {
49            target_blob_count: eip7691::TARGET_BLOBS_PER_BLOCK_ELECTRA,
50            max_blob_count: eip7691::MAX_BLOBS_PER_BLOCK_ELECTRA,
51            update_fraction: eip7691::BLOB_GASPRICE_UPDATE_FRACTION_PECTRA,
52            min_blob_fee: eip4844::BLOB_TX_MIN_BLOB_GASPRICE,
53        }
54    }
55
56    /// Returns the maximum available blob gas in a block.
57    ///
58    /// This is `max blob count * DATA_GAS_PER_BLOB`
59    pub const fn max_blob_gas_per_block(&self) -> u64 {
60        self.max_blob_count * DATA_GAS_PER_BLOB
61    }
62
63    /// Returns the blob gas target per block.
64    ///
65    /// This is `target blob count * DATA_GAS_PER_BLOB`
66    pub const fn target_blob_gas_per_block(&self) -> u64 {
67        self.target_blob_count * DATA_GAS_PER_BLOB
68    }
69
70    /// Calculates the `excess_blob_gas` value for the next block based on the current block
71    /// `excess_blob_gas` and `blob_gas_used`.
72    #[inline]
73    pub const fn next_block_excess_blob_gas(
74        &self,
75        excess_blob_gas: u64,
76        blob_gas_used: u64,
77    ) -> u64 {
78        (excess_blob_gas + blob_gas_used)
79            .saturating_sub(eip4844::DATA_GAS_PER_BLOB * self.target_blob_count)
80    }
81
82    /// Calculates the blob fee for block based on its `excess_blob_gas`.
83    #[inline]
84    pub const fn calc_blob_fee(&self, excess_blob_gas: u64) -> u128 {
85        eip4844::fake_exponential(self.min_blob_fee, excess_blob_gas as u128, self.update_fraction)
86    }
87}