From cee783ce4e545a620824821cb289e21242001fc2 Mon Sep 17 00:00:00 2001 From: Omid Chenane Date: Mon, 24 Jun 2024 17:18:30 +0330 Subject: [PATCH 1/2] fix: Change Arc to EnvKzgSettings --- .../src/commands/debug_cmd/build_block.rs | 17 +++++++------ crates/e2e-test-utils/src/transaction.rs | 9 +++---- crates/node-core/src/node_config.rs | 9 ++++--- crates/node/builder/src/builder/mod.rs | 10 ++++---- crates/primitives/benches/validate_blob_tx.rs | 14 ++++------- crates/primitives/src/constants/eip4844.rs | 24 +------------------ crates/primitives/src/transaction/sidecar.rs | 10 ++++---- crates/rpc/rpc/src/eth/bundle.rs | 5 ++-- crates/transaction-pool/src/validate/eth.rs | 21 ++++++++-------- 9 files changed, 46 insertions(+), 73 deletions(-) diff --git a/bin/reth/src/commands/debug_cmd/build_block.rs b/bin/reth/src/commands/debug_cmd/build_block.rs index 40b9722ae4b5..6c1125677815 100644 --- a/bin/reth/src/commands/debug_cmd/build_block.rs +++ b/bin/reth/src/commands/debug_cmd/build_block.rs @@ -24,16 +24,15 @@ use reth_fs_util as fs; use reth_node_api::PayloadBuilderAttributes; use reth_payload_builder::database::CachedReads; use reth_primitives::{ - constants::eip4844::{LoadKzgSettingsError, MAINNET_KZG_TRUSTED_SETUP}, - revm_primitives::KzgSettings, - Address, BlobTransaction, BlobTransactionSidecar, Bytes, PooledTransactionsElement, - SealedBlock, SealedBlockWithSenders, Transaction, TransactionSigned, TxEip4844, B256, U256, + constants::eip4844::LoadKzgSettingsError, revm_primitives::KzgSettings, Address, + BlobTransaction, BlobTransactionSidecar, Bytes, PooledTransactionsElement, SealedBlock, + SealedBlockWithSenders, Transaction, TransactionSigned, TxEip4844, B256, U256, }; use reth_provider::{ providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter, ChainSpecProvider, ProviderFactory, StageCheckpointReader, StateProviderFactory, }; -use reth_revm::database::StateProviderDatabase; +use reth_revm::{database::StateProviderDatabase, primitives::EnvKzgSettings}; use reth_rpc_types::engine::{BlobsBundleV1, PayloadAttributes}; use reth_stages::StageId; use reth_transaction_pool::{ @@ -103,14 +102,14 @@ impl Command { } /// Loads the trusted setup params from a given file path or falls back to - /// `MAINNET_KZG_TRUSTED_SETUP`. - fn kzg_settings(&self) -> eyre::Result> { + /// `EnvKzgSettings::Default`. + fn kzg_settings(&self) -> eyre::Result { if let Some(ref trusted_setup_file) = self.trusted_setup_file { let trusted_setup = KzgSettings::load_trusted_setup_file(trusted_setup_file) .map_err(LoadKzgSettingsError::KzgError)?; - Ok(Arc::new(trusted_setup)) + Ok(EnvKzgSettings::Custom(Arc::new(trusted_setup))) } else { - Ok(Arc::clone(&MAINNET_KZG_TRUSTED_SETUP)) + Ok(EnvKzgSettings::Default) } } diff --git a/crates/e2e-test-utils/src/transaction.rs b/crates/e2e-test-utils/src/transaction.rs index b278c96365a5..0719c7733e6a 100644 --- a/crates/e2e-test-utils/src/transaction.rs +++ b/crates/e2e-test-utils/src/transaction.rs @@ -1,5 +1,6 @@ use alloy_consensus::{ - BlobTransactionSidecar, SidecarBuilder, SimpleCoder, TxEip4844Variant, TxEnvelope, + BlobTransactionSidecar, EnvKzgSettings, SidecarBuilder, SimpleCoder, TxEip4844Variant, + TxEnvelope, }; use alloy_network::{eip2718::Encodable2718, EthereumWallet, TransactionBuilder}; use alloy_rpc_types::{TransactionInput, TransactionRequest}; @@ -7,7 +8,7 @@ use alloy_signer_local::PrivateKeySigner; use eyre::Ok; use reth_primitives::{hex, Address, Bytes, U256}; -use reth_primitives::{constants::eip4844::MAINNET_KZG_TRUSTED_SETUP, B256}; +use reth_primitives::B256; pub struct TransactionTestContext; @@ -71,12 +72,12 @@ impl TransactionTestContext { /// Validates the sidecar of a given tx envelope and returns the versioned hashes pub fn validate_sidecar(tx: TxEnvelope) -> Vec { - let proof_setting = MAINNET_KZG_TRUSTED_SETUP.clone(); + let proof_setting = EnvKzgSettings::Default; match tx { TxEnvelope::Eip4844(signed) => match signed.tx() { TxEip4844Variant::TxEip4844WithSidecar(tx) => { - tx.validate_blob(&proof_setting).unwrap(); + tx.validate_blob(proof_setting.get()).unwrap(); tx.sidecar.versioned_hashes().collect() } _ => panic!("Expected Eip4844 transaction with sidecar"), diff --git a/crates/node-core/src/node_config.rs b/crates/node-core/src/node_config.rs index 3a3b742cef02..b7c2ce3f81ab 100644 --- a/crates/node-core/src/node_config.rs +++ b/crates/node-core/src/node_config.rs @@ -16,8 +16,7 @@ use reth_config::config::PruneConfig; use reth_db_api::{database::Database, database_metrics::DatabaseMetrics}; use reth_network_p2p::headers::client::HeadersClient; use reth_primitives::{ - constants::eip4844::MAINNET_KZG_TRUSTED_SETUP, kzg::KzgSettings, BlockHashOrNumber, - BlockNumber, Head, SealedHeader, B256, + revm_primitives::EnvKzgSettings, BlockHashOrNumber, BlockNumber, Head, SealedHeader, B256, }; use reth_provider::{ providers::StaticFileProvider, BlockHashReader, HeaderProvider, ProviderFactory, @@ -267,9 +266,9 @@ impl NodeConfig { Ok(max_block) } - /// Loads '`MAINNET_KZG_TRUSTED_SETUP`' - pub fn kzg_settings(&self) -> eyre::Result> { - Ok(Arc::clone(&MAINNET_KZG_TRUSTED_SETUP)) + /// Loads '`EnvKzgSettings::Default`' + pub fn kzg_settings(&self) -> eyre::Result { + Ok(EnvKzgSettings::Default) } /// Installs the prometheus recorder. diff --git a/crates/node/builder/src/builder/mod.rs b/crates/node/builder/src/builder/mod.rs index c3255ceed212..4b727cb61145 100644 --- a/crates/node/builder/src/builder/mod.rs +++ b/crates/node/builder/src/builder/mod.rs @@ -30,10 +30,10 @@ use reth_node_core::{ cli::config::{PayloadBuilderConfig, RethTransactionPoolConfig}, dirs::{ChainPath, DataDirPath, MaybePlatformPath}, node_config::NodeConfig, - primitives::{kzg::KzgSettings, Head}, + primitives::Head, utils::write_peers_to_file, }; -use reth_primitives::constants::eip4844::MAINNET_KZG_TRUSTED_SETUP; +use reth_primitives::revm_primitives::EnvKzgSettings; use reth_provider::{providers::BlockchainProvider, ChainSpecProvider}; use reth_tasks::TaskExecutor; use reth_transaction_pool::{PoolConfig, TransactionPool}; @@ -469,9 +469,9 @@ impl BuilderContext { self.config().txpool.pool_config() } - /// Loads `MAINNET_KZG_TRUSTED_SETUP`. - pub fn kzg_settings(&self) -> eyre::Result> { - Ok(Arc::clone(&MAINNET_KZG_TRUSTED_SETUP)) + /// Loads `EnvKzgSettings::Default`. + pub fn kzg_settings(&self) -> eyre::Result { + Ok(EnvKzgSettings::Default) } /// Returns the config for payload building. diff --git a/crates/primitives/benches/validate_blob_tx.rs b/crates/primitives/benches/validate_blob_tx.rs index 40f8b90927cb..bc3dee337d71 100644 --- a/crates/primitives/benches/validate_blob_tx.rs +++ b/crates/primitives/benches/validate_blob_tx.rs @@ -1,7 +1,7 @@ #![allow(missing_docs)] +use alloy_eips::eip4844::env_settings::EnvKzgSettings; use alloy_primitives::hex; -use c_kzg::KzgSettings; use criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, }; @@ -11,11 +11,8 @@ use proptest::{ test_runner::{RngAlgorithm, TestRng, TestRunner}, }; use proptest_arbitrary_interop::arb; -use reth_primitives::{ - constants::eip4844::MAINNET_KZG_TRUSTED_SETUP, BlobTransactionSidecar, TxEip4844, -}; +use reth_primitives::{BlobTransactionSidecar, TxEip4844}; use revm_primitives::MAX_BLOB_NUMBER_PER_BLOCK; -use std::sync::Arc; // constant seed to use for the rng const SEED: [u8; 32] = hex!("1337133713371337133713371337133713371337133713371337133713371337"); @@ -23,11 +20,10 @@ const SEED: [u8; 32] = hex!("133713371337133713371337133713371337133713371337133 /// Benchmarks EIP-48444 blob validation. fn blob_validation(c: &mut Criterion) { let mut group = c.benchmark_group("Blob Transaction KZG validation"); - let kzg_settings = MAINNET_KZG_TRUSTED_SETUP.clone(); for num_blobs in 1..=MAX_BLOB_NUMBER_PER_BLOCK { println!("Benchmarking validation for tx with {num_blobs} blobs"); - validate_blob_tx(&mut group, "ValidateBlob", num_blobs, kzg_settings.clone()); + validate_blob_tx(&mut group, "ValidateBlob", num_blobs, EnvKzgSettings::Default); } } @@ -35,7 +31,7 @@ fn validate_blob_tx( group: &mut BenchmarkGroup<'_, WallTime>, description: &str, num_blobs: u64, - kzg_settings: Arc, + kzg_settings: EnvKzgSettings, ) { let setup = || { let config = ProptestConfig::default(); @@ -73,7 +69,7 @@ fn validate_blob_tx( // for now we just use the default SubPoolLimit group.bench_function(group_id, |b| { b.iter_with_setup(setup, |(tx, blob_sidecar)| { - if let Err(err) = std::hint::black_box(tx.validate_blob(&blob_sidecar, &kzg_settings)) { + if let Err(err) = std::hint::black_box(tx.validate_blob(&blob_sidecar, kzg_settings.get())) { println!("Validation failed: {err:?}"); } }); diff --git a/crates/primitives/src/constants/eip4844.rs b/crates/primitives/src/constants/eip4844.rs index c1748edf74b6..48a3aebb3c32 100644 --- a/crates/primitives/src/constants/eip4844.rs +++ b/crates/primitives/src/constants/eip4844.rs @@ -11,19 +11,7 @@ pub use alloy_eips::eip4844::{ #[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> = 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"), - ) - }); + use std::io::Write; /// Loads the trusted setup parameters from the given bytes and returns the [`KzgSettings`]. /// @@ -48,14 +36,4 @@ mod trusted_setup { #[error("KZG error: {0:?}")] KzgError(#[from] c_kzg::Error), } - - #[cfg(test)] - mod tests { - use super::*; - - #[test] - fn ensure_load_kzg_settings() { - let _settings = Arc::clone(&MAINNET_KZG_TRUSTED_SETUP); - } - } } diff --git a/crates/primitives/src/transaction/sidecar.rs b/crates/primitives/src/transaction/sidecar.rs index c45683ce7980..2c6f4598a513 100644 --- a/crates/primitives/src/transaction/sidecar.rs +++ b/crates/primitives/src/transaction/sidecar.rs @@ -281,14 +281,16 @@ impl BlobTransaction { /// Generates a [`BlobTransactionSidecar`] structure containing blobs, commitments, and proofs. #[cfg(all(feature = "c-kzg", any(test, feature = "arbitrary")))] pub fn generate_blob_sidecar(blobs: Vec) -> BlobTransactionSidecar { - use crate::constants::eip4844::MAINNET_KZG_TRUSTED_SETUP; + use alloy_eips::eip4844::env_settings::EnvKzgSettings; use c_kzg::{KzgCommitment, KzgProof}; - let kzg_settings = MAINNET_KZG_TRUSTED_SETUP.clone(); + let kzg_settings = EnvKzgSettings::Default; let commitments: Vec = blobs .iter() - .map(|blob| KzgCommitment::blob_to_kzg_commitment(&blob.clone(), &kzg_settings).unwrap()) + .map(|blob| { + KzgCommitment::blob_to_kzg_commitment(&blob.clone(), kzg_settings.get()).unwrap() + }) .map(|commitment| commitment.to_bytes()) .collect(); @@ -296,7 +298,7 @@ pub fn generate_blob_sidecar(blobs: Vec) -> BlobTransactionSidecar .iter() .zip(commitments.iter()) .map(|(blob, commitment)| { - KzgProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings).unwrap() + KzgProof::compute_blob_kzg_proof(blob, commitment, kzg_settings.get()).unwrap() }) .map(|proof| proof.to_bytes()) .collect(); diff --git a/crates/rpc/rpc/src/eth/bundle.rs b/crates/rpc/rpc/src/eth/bundle.rs index a8e088278dcb..97ea4bb4c0fe 100644 --- a/crates/rpc/rpc/src/eth/bundle.rs +++ b/crates/rpc/rpc/src/eth/bundle.rs @@ -8,7 +8,6 @@ use crate::eth::{ }; use jsonrpsee::core::RpcResult; use reth_primitives::{ - constants::eip4844::MAINNET_KZG_TRUSTED_SETUP, keccak256, revm_primitives::db::{DatabaseCommit, DatabaseRef}, PooledTransactionsElement, U256, @@ -21,7 +20,7 @@ use revm::{ db::CacheDB, primitives::{ResultAndState, TxEnv}, }; -use revm_primitives::{EnvWithHandlerCfg, MAX_BLOB_GAS_PER_BLOCK}; +use revm_primitives::{EnvKzgSettings, EnvWithHandlerCfg, MAX_BLOB_GAS_PER_BLOCK}; use std::sync::Arc; /// `Eth` bundle implementation. @@ -126,7 +125,7 @@ where // Verify that the given blob data, commitments, and proofs are all valid for // this transaction. if let PooledTransactionsElement::BlobTransaction(ref tx) = tx { - tx.validate(MAINNET_KZG_TRUSTED_SETUP.as_ref()) + tx.validate(EnvKzgSettings::Default.get()) .map_err(|e| EthApiError::InvalidParams(e.to_string()))?; } diff --git a/crates/transaction-pool/src/validate/eth.rs b/crates/transaction-pool/src/validate/eth.rs index 8bdd68d62154..8680a11e121f 100644 --- a/crates/transaction-pool/src/validate/eth.rs +++ b/crates/transaction-pool/src/validate/eth.rs @@ -11,17 +11,16 @@ use crate::{ }; use reth_chainspec::ChainSpec; use reth_primitives::{ - constants::{ - eip4844::{MAINNET_KZG_TRUSTED_SETUP, MAX_BLOBS_PER_BLOCK}, - ETHEREUM_BLOCK_GAS_LIMIT, - }, - kzg::KzgSettings, + constants::{eip4844::MAX_BLOBS_PER_BLOCK, ETHEREUM_BLOCK_GAS_LIMIT}, Address, GotExpected, InvalidTransactionError, SealedBlock, TxKind, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID, U256, }; use reth_provider::{AccountReader, BlockReaderIdExt, StateProviderFactory}; use reth_tasks::TaskSpawner; -use revm::{interpreter::gas::validate_initial_tx_gas, primitives::SpecId}; +use revm::{ + interpreter::gas::validate_initial_tx_gas, + primitives::{EnvKzgSettings, SpecId}, +}; use std::{ marker::PhantomData, sync::{atomic::AtomicBool, Arc}, @@ -125,7 +124,7 @@ pub(crate) struct EthTransactionValidatorInner { /// Minimum priority fee to enforce for acceptance into the pool. minimum_priority_fee: Option, /// Stores the setup and parameters needed for validating KZG proofs. - kzg_settings: Arc, + kzg_settings: EnvKzgSettings, /// How to handle [`TransactionOrigin::Local`](TransactionOrigin) transactions. local_transactions_config: LocalTransactionConfig, /// Maximum size in bytes a single transaction can have in order to be accepted into the pool. @@ -369,7 +368,7 @@ where } EthBlobTransactionSidecar::Present(blob) => { // validate the blob - if let Err(err) = transaction.validate_blob(&blob, &self.kzg_settings) { + if let Err(err) = transaction.validate_blob(&blob, &self.kzg_settings.get()) { return TransactionValidationOutcome::Invalid( transaction, InvalidPoolTransactionError::Eip4844( @@ -435,7 +434,7 @@ pub struct EthTransactionValidatorBuilder { additional_tasks: usize, /// Stores the setup and parameters needed for validating KZG proofs. - kzg_settings: Arc, + kzg_settings: EnvKzgSettings, /// How to handle [`TransactionOrigin::Local`](TransactionOrigin) transactions. local_transactions_config: LocalTransactionConfig, /// Max size in bytes of a single transaction allowed @@ -457,7 +456,7 @@ impl EthTransactionValidatorBuilder { block_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT, minimum_priority_fee: None, additional_tasks: 1, - kzg_settings: Arc::clone(&MAINNET_KZG_TRUSTED_SETUP), + kzg_settings: EnvKzgSettings::Default, local_transactions_config: Default::default(), max_tx_input_bytes: DEFAULT_MAX_TX_INPUT_BYTES, @@ -539,7 +538,7 @@ impl EthTransactionValidatorBuilder { } /// Sets the [`KzgSettings`] to use for validating KZG proofs. - pub fn kzg_settings(mut self, kzg_settings: Arc) -> Self { + pub fn kzg_settings(mut self, kzg_settings: EnvKzgSettings) -> Self { self.kzg_settings = kzg_settings; self } From f3857a89bd1d4d46b03262fbbcdce6feab19958f Mon Sep 17 00:00:00 2001 From: Omid Chenane Date: Mon, 24 Jun 2024 17:21:47 +0330 Subject: [PATCH 2/2] Fix style --- crates/node-core/src/node_config.rs | 2 +- crates/node/builder/src/builder/mod.rs | 2 +- crates/primitives/benches/validate_blob_tx.rs | 4 +++- crates/transaction-pool/src/validate/eth.rs | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/node-core/src/node_config.rs b/crates/node-core/src/node_config.rs index b7c2ce3f81ab..932c12e70436 100644 --- a/crates/node-core/src/node_config.rs +++ b/crates/node-core/src/node_config.rs @@ -267,7 +267,7 @@ impl NodeConfig { } /// Loads '`EnvKzgSettings::Default`' - pub fn kzg_settings(&self) -> eyre::Result { + pub const fn kzg_settings(&self) -> eyre::Result { Ok(EnvKzgSettings::Default) } diff --git a/crates/node/builder/src/builder/mod.rs b/crates/node/builder/src/builder/mod.rs index 4b727cb61145..f23fc873e885 100644 --- a/crates/node/builder/src/builder/mod.rs +++ b/crates/node/builder/src/builder/mod.rs @@ -470,7 +470,7 @@ impl BuilderContext { } /// Loads `EnvKzgSettings::Default`. - pub fn kzg_settings(&self) -> eyre::Result { + pub const fn kzg_settings(&self) -> eyre::Result { Ok(EnvKzgSettings::Default) } diff --git a/crates/primitives/benches/validate_blob_tx.rs b/crates/primitives/benches/validate_blob_tx.rs index bc3dee337d71..622168bb35f8 100644 --- a/crates/primitives/benches/validate_blob_tx.rs +++ b/crates/primitives/benches/validate_blob_tx.rs @@ -69,7 +69,9 @@ fn validate_blob_tx( // for now we just use the default SubPoolLimit group.bench_function(group_id, |b| { b.iter_with_setup(setup, |(tx, blob_sidecar)| { - if let Err(err) = std::hint::black_box(tx.validate_blob(&blob_sidecar, kzg_settings.get())) { + if let Err(err) = + std::hint::black_box(tx.validate_blob(&blob_sidecar, kzg_settings.get())) + { println!("Validation failed: {err:?}"); } }); diff --git a/crates/transaction-pool/src/validate/eth.rs b/crates/transaction-pool/src/validate/eth.rs index 8680a11e121f..79a93cf2e2a7 100644 --- a/crates/transaction-pool/src/validate/eth.rs +++ b/crates/transaction-pool/src/validate/eth.rs @@ -368,7 +368,7 @@ where } EthBlobTransactionSidecar::Present(blob) => { // validate the blob - if let Err(err) = transaction.validate_blob(&blob, &self.kzg_settings.get()) { + if let Err(err) = transaction.validate_blob(&blob, self.kzg_settings.get()) { return TransactionValidationOutcome::Invalid( transaction, InvalidPoolTransactionError::Eip4844( @@ -537,7 +537,7 @@ impl EthTransactionValidatorBuilder { self } - /// Sets the [`KzgSettings`] to use for validating KZG proofs. + /// Sets the [`EnvKzgSettings`] to use for validating KZG proofs. pub fn kzg_settings(mut self, kzg_settings: EnvKzgSettings) -> Self { self.kzg_settings = kzg_settings; self