Skip to content

migrate EIP 4844 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ redundant-clone = "warn"
[workspace.dependencies]
alloy-primitives = { version = "1.0", default-features = false }
alloy-rlp = { version = "0.3", default-features = false }
alloy-serde = { version = "0.8", default-features = false }

# serde
serde = { version = "1.0", default-features = false, features = [
Expand All @@ -39,6 +40,10 @@ serde = { version = "1.0", default-features = false, features = [
] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

# crypto
c-kzg = { version = "1.0", default-features = false }
sha2 = { version = "0.10", default-features = false }

# arbitrary
arbitrary = "1.3"

Expand All @@ -48,3 +53,12 @@ rand = "0.8"

# misc
thiserror = { version = "2.0.0", default-features = false }
derive_more = { version = "1.0.0", default-features = false }
once_cell = { version = "1.19", default-features = false }

# internal
alloy-eip4844-core = { path = "crates/alloy_eip4844_core" }
alloy-eip7691 = { path = "crates/eip7691" }
alloy-eip2930 = { path = "crates/eip2930" }
alloy-eip7702 = { path = "crates/eip7702" }
alloy-eip7840 = { path = "crates/eip7840" }
18 changes: 18 additions & 0 deletions crates/alloy_eip4844_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "alloy-eip4844-core"
version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lints]
workspace = true

[dependencies]
51 changes: 51 additions & 0 deletions crates/alloy_eip4844_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! [EIP-4844] types, constants and helper functions.
//!
//! [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844

/// Determines the maximum rate of change for blob fee
pub const BLOB_GASPRICE_UPDATE_FRACTION: u128 = 3_338_477u128; // 3338477

/// Minimum gas price for a data blob
pub const BLOB_TX_MIN_BLOB_GASPRICE: u128 = 1u128;

/// Gas consumption of a single data blob.
pub const DATA_GAS_PER_BLOB: u64 = 131_072u64; // 32*4096 = 131072 == 2^17 == 0x20000

/// Maximum data gas for data blobs in a single block.
pub const MAX_DATA_GAS_PER_BLOCK: u64 = 786_432u64; // 0xC0000 = 6 * 0x20000

/// Target data gas for data blobs in a single block.
pub const TARGET_DATA_GAS_PER_BLOCK: u64 = 393_216u64; // 0x60000 = 3 * 0x20000

/// Maximum number of data blobs in a single block.
pub const MAX_BLOBS_PER_BLOCK: usize = (MAX_DATA_GAS_PER_BLOCK / DATA_GAS_PER_BLOB) as usize; // 786432 / 131072 = 6

/// Target number of data blobs in a single block.
pub const TARGET_BLOBS_PER_BLOCK: u64 = TARGET_DATA_GAS_PER_BLOCK / DATA_GAS_PER_BLOB; // 393216 / 131072 = 3

/// Approximates `factor * e ** (numerator / denominator)` using Taylor expansion.
///
/// This is used to calculate the blob price.
///
/// See also [the EIP-4844 helpers](https://eips.ethereum.org/EIPS/eip-4844#helpers)
/// (`fake_exponential`).
///
/// # Panics
///
/// This function panics if `denominator` is zero.
#[inline]
pub const fn fake_exponential(factor: u128, numerator: u128, denominator: u128) -> u128 {
assert!(denominator != 0, "attempt to divide by zero");

let mut i = 1;
let mut output = 0;
let mut numerator_accum = factor * denominator;
while numerator_accum > 0 {
output += numerator_accum;

// Denominator is asserted as not zero at the start of the function.
numerator_accum = (numerator_accum * numerator) / (denominator * i);
i += 1;
}
output / denominator
}
68 changes: 68 additions & 0 deletions crates/eip4844/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[package]
name = "alloy-eip4844"
description = "Implementation of EIP-4844 type definitions"

version = "0.1.0"
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lints]
workspace = true

[features]
default = ["std", "kzg-sidecar"]
std = ["alloy-primitives/std", "alloy-rlp/std", "serde?/std", "c-kzg?/std", "once_cell?/std"]
sha2 = ["dep:sha2"]
kzg = ["kzg-sidecar", "sha2", "dep:c-kzg", "dep:once_cell"]
kzg-sidecar = ["sha2"]
serde = ["dep:alloy-serde", "dep:serde", "alloy-primitives/serde", "c-kzg?/serde"]
arbitrary = [
"std",
"kzg-sidecar",
"dep:arbitrary",
"alloy-primitives/arbitrary",
"alloy-serde?/arbitrary",
]

[dependencies]
alloy-primitives = { workspace = true, features = ["rlp"] }
alloy-rlp = { workspace = true, features = ["derive"] }

# crypto
sha2 = { workspace = true, optional = true }
c-kzg = { workspace = true, optional = true }

# misc
derive_more = { workspace = true, features = [
"as_ref",
"deref",
"deref_mut",
"from",
"into_iterator",
], default-features = false }
once_cell = { workspace = true, features = ["race", "alloc"], optional = true }

# serde
alloy-serde = { workspace = true, optional = true }
serde = { workspace = true, optional = true }

# eips
alloy-eip7840.workspace = true

arbitrary = { workspace = true, features = ["derive"], optional = true }

[dev-dependencies]
arbitrary = { workspace = true, features = ["derive"] }
alloy-primitives = { workspace = true, features = [
"rand",
"serde",
"arbitrary",
] }
1 change: 1 addition & 0 deletions crates/eip4844/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# EIP-4844
Loading