Skip to content

Commit

Permalink
feat: feature gate c-kzg in reth-primitives (#5240)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored Nov 2, 2023
1 parent c0e70ba commit 24fade3
Show file tree
Hide file tree
Showing 8 changed files with 550 additions and 506 deletions.
5 changes: 3 additions & 2 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ethers-core = { workspace = true, default-features = false, optional = true }
secp256k1 = { workspace = true, features = ["global-context", "recovery"] }

# for eip-4844
c-kzg = { workspace = true, features = ["serde"] }
c-kzg = { workspace = true, features = ["serde"], optional = true }

# used for forkid
crc = "3"
Expand Down Expand Up @@ -88,8 +88,9 @@ criterion = "0.5"
pprof = { version = "0.12", features = ["flamegraph", "frame-pointer", "criterion"] }

[features]
default = []
default = ["c-kzg"]
arbitrary = ["revm-primitives/arbitrary", "reth-rpc-types/arbitrary", "dep:arbitrary", "dep:proptest", "dep:proptest-derive"]
c-kzg = ["revm-primitives/c-kzg", "dep:c-kzg"]
test-utils = ["dep:plain_hasher", "dep:hash-db", "dep:ethers-core"]
# value-256 controls whether transaction Value fields are DB-encoded as 256 bits instead of the
# default of 128 bits.
Expand Down
88 changes: 48 additions & 40 deletions crates/primitives/src/constants/eip4844.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#parameters) protocol constants and utils for shard Blob Transactions.

use crate::kzg::KzgSettings;
use once_cell::sync::Lazy;
use std::{io::Write, sync::Arc};
#[cfg(feature = "c-kzg")]
pub use trusted_setup::*;

/// Size a single field element in bytes.
pub const FIELD_ELEMENT_BYTES: u64 = 32;
Expand Down Expand Up @@ -34,45 +32,55 @@ pub const BLOB_TX_MIN_BLOB_GASPRICE: u128 = 1u128;
/// Commitment version of a KZG commitment
pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;

/// KZG trusted setup
pub static MAINNET_KZG_TRUSTED_SETUP: Lazy<Arc<KzgSettings>> = Lazy::new(|| {
Arc::new(
c_kzg::KzgSettings::load_trusted_setup(
&revm_primitives::kzg::G1_POINTS.0,
&revm_primitives::kzg::G2_POINTS.0,
#[cfg(feature = "c-kzg")]
mod trusted_setup {
use crate::kzg::KzgSettings;
use once_cell::sync::Lazy;
use std::{io::Write, sync::Arc};

/// KZG trusted setup
pub static MAINNET_KZG_TRUSTED_SETUP: Lazy<Arc<KzgSettings>> = Lazy::new(|| {
Arc::new(
c_kzg::KzgSettings::load_trusted_setup(
&revm_primitives::kzg::G1_POINTS.0,
&revm_primitives::kzg::G2_POINTS.0,
)
.expect("failed to load trusted setup"),
)
.expect("failed to load trusted setup"),
)
});

/// Loads the trusted setup parameters from the given bytes and returns the [KzgSettings].
///
/// This creates a temp file to store the bytes and then loads the [KzgSettings] from the file via
/// [KzgSettings::load_trusted_setup_file].
pub fn load_trusted_setup_from_bytes(bytes: &[u8]) -> Result<KzgSettings, LoadKzgSettingsError> {
let mut file = tempfile::NamedTempFile::new().map_err(LoadKzgSettingsError::TempFileErr)?;
file.write_all(bytes).map_err(LoadKzgSettingsError::TempFileErr)?;
KzgSettings::load_trusted_setup_file(file.path()).map_err(LoadKzgSettingsError::KzgError)
}
});

/// Loads the trusted setup parameters from the given bytes and returns the [KzgSettings].
///
/// This creates a temp file to store the bytes and then loads the [KzgSettings] from the file
/// via [KzgSettings::load_trusted_setup_file].
pub fn load_trusted_setup_from_bytes(
bytes: &[u8],
) -> Result<KzgSettings, LoadKzgSettingsError> {
let mut file = tempfile::NamedTempFile::new().map_err(LoadKzgSettingsError::TempFileErr)?;
file.write_all(bytes).map_err(LoadKzgSettingsError::TempFileErr)?;
KzgSettings::load_trusted_setup_file(file.path()).map_err(LoadKzgSettingsError::KzgError)
}

/// Error type for loading the trusted setup.
#[derive(Debug, thiserror::Error)]
pub enum LoadKzgSettingsError {
/// Failed to create temp file to store bytes for loading [KzgSettings] via
/// [KzgSettings::load_trusted_setup_file].
#[error("failed to setup temp file: {0}")]
TempFileErr(#[from] std::io::Error),
/// Kzg error
#[error("KZG error: {0:?}")]
KzgError(#[from] c_kzg::Error),
}
/// Error type for loading the trusted setup.
#[derive(Debug, thiserror::Error)]
pub enum LoadKzgSettingsError {
/// Failed to create temp file to store bytes for loading [KzgSettings] via
/// [KzgSettings::load_trusted_setup_file].
#[error("failed to setup temp file: {0}")]
TempFileErr(#[from] std::io::Error),
/// Kzg error
#[error("KZG error: {0:?}")]
KzgError(#[from] c_kzg::Error),
}

#[cfg(test)]
mod tests {
use super::*;
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;

#[test]
fn ensure_load_kzg_settings() {
let _settings = Arc::clone(&MAINNET_KZG_TRUSTED_SETUP);
#[test]
fn ensure_load_kzg_settings() {
let _settings = Arc::clone(&MAINNET_KZG_TRUSTED_SETUP);
}
}
}
10 changes: 5 additions & 5 deletions crates/primitives/src/eip4844.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Helpers for working with EIP-4844 blob fee
use crate::{
constants::eip4844::{TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG},
kzg::KzgCommitment,
B256,
};
use crate::constants::eip4844::TARGET_DATA_GAS_PER_BLOCK;
#[cfg(feature = "c-kzg")]
use crate::{constants::eip4844::VERSIONED_HASH_VERSION_KZG, kzg::KzgCommitment, B256};
#[cfg(feature = "c-kzg")]
use sha2::{Digest, Sha256};

// re-exports from revm for calculating blob fee
Expand All @@ -12,6 +11,7 @@ pub use revm_primitives::calc_blob_gasprice;
/// Calculates the versioned hash for a KzgCommitment
///
/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension)
#[cfg(feature = "c-kzg")]
pub fn kzg_to_versioned_hash(commitment: KzgCommitment) -> B256 {
let mut res = Sha256::digest(commitment.as_slice());
res[0] = VERSIONED_HASH_VERSION_KZG;
Expand Down
16 changes: 12 additions & 4 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub use constants::{
DEV_GENESIS_HASH, EMPTY_OMMER_ROOT_HASH, GOERLI_GENESIS_HASH, HOLESKY_GENESIS_HASH,
KECCAK_EMPTY, MAINNET_GENESIS_HASH, SEPOLIA_GENESIS_HASH,
};
#[cfg(feature = "c-kzg")]
pub use eip4844::{calculate_excess_blob_gas, kzg_to_versioned_hash};
pub use forkid::{ForkFilter, ForkHash, ForkId, ForkTransition, ValidationError};
pub use genesis::{Genesis, GenesisAccount};
Expand All @@ -84,12 +85,18 @@ pub use receipt::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef, Receipts};
pub use serde_helper::JsonU256;
pub use snapshot::SnapshotSegment;
pub use storage::StorageEntry;

#[cfg(feature = "c-kzg")]
pub use transaction::{
BlobTransaction, BlobTransactionSidecar, BlobTransactionValidationError,
FromRecoveredPooledTransaction, PooledTransactionsElement,
PooledTransactionsElementEcRecovered,
};

pub use transaction::{
util::secp256k1::{public_key_to_address, recover_signer, sign_message},
AccessList, AccessListItem, BlobTransaction, BlobTransactionSidecar,
BlobTransactionValidationError, FromRecoveredPooledTransaction, FromRecoveredTransaction,
IntoRecoveredTransaction, InvalidTransactionError, PooledTransactionsElement,
PooledTransactionsElementEcRecovered, Signature, Transaction, TransactionKind, TransactionMeta,
AccessList, AccessListItem, FromRecoveredTransaction, IntoRecoveredTransaction,
InvalidTransactionError, Signature, Transaction, TransactionKind, TransactionMeta,
TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, TxEip1559, TxEip2930,
TxEip4844, TxHashOrNumber, TxLegacy, TxType, TxValue, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID,
EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID,
Expand Down Expand Up @@ -125,6 +132,7 @@ pub type H64 = B64;
pub use arbitrary;

/// EIP-4844 + KZG helpers
#[cfg(feature = "c-kzg")]
pub mod kzg {
pub use c_kzg::*;
}
Loading

0 comments on commit 24fade3

Please sign in to comment.