diff --git a/beacon_node/beacon_chain/src/builder.rs b/beacon_node/beacon_chain/src/builder.rs index c390f881092..56381a7806f 100644 --- a/beacon_node/beacon_chain/src/builder.rs +++ b/beacon_node/beacon_chain/src/builder.rs @@ -25,7 +25,7 @@ use eth1::Config as Eth1Config; use execution_layer::ExecutionLayer; use fork_choice::{ForkChoice, ResetPayloadStatuses}; use futures::channel::mpsc::Sender; -use kzg::{Kzg, TrustedSetup}; +use kzg::Kzg; use operation_pool::{OperationPool, PersistedOperationPool}; use parking_lot::{Mutex, RwLock}; use proto_array::{DisallowedReOrgOffsets, ReOrgThreshold}; @@ -101,7 +101,7 @@ pub struct BeaconChainBuilder { // Pending I/O batch that is constructed during building and should be executed atomically // alongside `PersistedBeaconChain` storage when `BeaconChainBuilder::build` is called. pending_io_batch: Vec, - trusted_setup: Option, + kzg: Option>, task_executor: Option, validator_monitor_config: Option, } @@ -142,7 +142,7 @@ where graffiti: Graffiti::default(), slasher: None, pending_io_batch: vec![], - trusted_setup: None, + kzg: None, task_executor: None, validator_monitor_config: None, } @@ -669,8 +669,8 @@ where self } - pub fn trusted_setup(mut self, trusted_setup: TrustedSetup) -> Self { - self.trusted_setup = Some(trusted_setup); + pub fn kzg(mut self, kzg: Option>) -> Self { + self.kzg = kzg; self } @@ -718,15 +718,6 @@ where slot_clock.now().ok_or("Unable to read slot")? }; - let kzg = if let Some(trusted_setup) = self.trusted_setup { - let kzg = Kzg::new_from_trusted_setup(trusted_setup) - .map_err(|e| format!("Failed to load trusted setup: {:?}", e))?; - let kzg_arc = Arc::new(kzg); - Some(kzg_arc) - } else { - None - }; - let initial_head_block_root = fork_choice .get_head(current_slot, &self.spec) .map_err(|e| format!("Unable to get fork choice head: {:?}", e))?; @@ -967,10 +958,10 @@ where validator_monitor: RwLock::new(validator_monitor), genesis_backfill_slot, data_availability_checker: Arc::new( - DataAvailabilityChecker::new(slot_clock, kzg.clone(), store, &log, self.spec) + DataAvailabilityChecker::new(slot_clock, self.kzg.clone(), store, &log, self.spec) .map_err(|e| format!("Error initializing DataAvailabiltyChecker: {:?}", e))?, ), - kzg, + kzg: self.kzg.clone(), block_production_state: Arc::new(Mutex::new(None)), }; diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 7721c9b00ff..71c96d0fd55 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -86,7 +86,7 @@ pub use events::ServerSentEventHandler; pub use execution_layer::EngineState; pub use execution_payload::NotifyExecutionLayer; pub use fork_choice::{ExecutionStatus, ForkchoiceUpdateParameters}; -pub use kzg::TrustedSetup; +pub use kzg::{Kzg, TrustedSetup}; pub use metrics::scrape_for_metrics; pub use migrate::MigratorConfig; pub use parking_lot; diff --git a/beacon_node/beacon_chain/src/test_utils.rs b/beacon_node/beacon_chain/src/test_utils.rs index a73ba87f67d..542262487ae 100644 --- a/beacon_node/beacon_chain/src/test_utils.rs +++ b/beacon_node/beacon_chain/src/test_utils.rs @@ -31,6 +31,7 @@ use futures::channel::mpsc::Receiver; pub use genesis::{interop_genesis_state_with_eth1, DEFAULT_ETH1_BLOCK_HASH}; use int_to_bytes::int_to_bytes32; use kzg::{Kzg, TrustedSetup}; +use lazy_static::lazy_static; use merkle_proof::MerkleTree; use operation_pool::ReceivedPreCapella; use parking_lot::Mutex; @@ -76,6 +77,16 @@ pub const FORK_NAME_ENV_VAR: &str = "FORK_NAME"; // a different value. pub const DEFAULT_TARGET_AGGREGATORS: u64 = u64::MAX; +lazy_static! { + pub static ref KZG: Arc = { + let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP_BYTES) + .map_err(|e| format!("Unable to read trusted setup file: {}", e)) + .expect("should have trusted setup"); + let kzg = Kzg::new_from_trusted_setup(trusted_setup).expect("should create kzg"); + Arc::new(kzg) + }; +} + pub type BaseHarnessType = Witness, E, THotStore, TColdStore>; @@ -505,16 +516,14 @@ where let validator_keypairs = self .validator_keypairs .expect("cannot build without validator keypairs"); - let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP_BYTES) - .map_err(|e| format!("Unable to read trusted setup file: {}", e)) - .unwrap(); + let kzg = spec.deneb_fork_epoch.map(|_| KZG.clone()); let validator_monitor_config = self.validator_monitor_config.unwrap_or_default(); let chain_config = self.chain_config.unwrap_or_default(); let mut builder = BeaconChainBuilder::new(self.eth_spec_instance) .logger(log.clone()) - .custom_spec(spec) + .custom_spec(spec.clone()) .store(self.store.expect("cannot build without store")) .store_migrator_config( MigratorConfig::default() @@ -532,7 +541,7 @@ where 5, ))) .validator_monitor_config(validator_monitor_config) - .trusted_setup(trusted_setup); + .kzg(kzg); builder = if let Some(mutator) = self.initial_mutator { mutator(builder) @@ -587,10 +596,7 @@ pub fn mock_execution_layer_from_parts( HARNESS_GENESIS_TIME + spec.seconds_per_slot * E::slots_per_epoch() * epoch.as_u64() }); - let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP_BYTES) - .map_err(|e| format!("Unable to read trusted setup file: {}", e)) - .expect("should have trusted setup"); - let kzg = Kzg::new_from_trusted_setup(trusted_setup).expect("should create kzg"); + let kzg_opt = spec.deneb_fork_epoch.map(|_| KZG.clone()); MockExecutionLayer::new( task_executor, @@ -600,7 +606,7 @@ pub fn mock_execution_layer_from_parts( prague_time, Some(JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap()), spec.clone(), - Some(kzg), + kzg_opt, ) } diff --git a/beacon_node/beacon_chain/tests/store_tests.rs b/beacon_node/beacon_chain/tests/store_tests.rs index 66f4138afb4..51d62cf8a19 100644 --- a/beacon_node/beacon_chain/tests/store_tests.rs +++ b/beacon_node/beacon_chain/tests/store_tests.rs @@ -7,15 +7,13 @@ use beacon_chain::data_availability_checker::AvailableBlock; use beacon_chain::schema_change::migrate_schema; use beacon_chain::test_utils::{ mock_execution_layer_from_parts, test_spec, AttestationStrategy, BeaconChainHarness, - BlockStrategy, DiskHarnessType, + BlockStrategy, DiskHarnessType, KZG, }; use beacon_chain::{ data_availability_checker::MaybeAvailableBlock, historical_blocks::HistoricalBlockError, migrate::MigratorConfig, BeaconChain, BeaconChainError, BeaconChainTypes, BeaconSnapshot, BlockError, ChainConfig, NotifyExecutionLayer, ServerSentEventHandler, WhenSlotSkipped, }; -use eth2_network_config::TRUSTED_SETUP_BYTES; -use kzg::TrustedSetup; use lazy_static::lazy_static; use logging::test_logger; use maplit::hashset; @@ -2418,9 +2416,7 @@ async fn weak_subjectivity_sync_test(slots: Vec, checkpoint_slot: Slot) { let store = get_store(&temp2); let spec = test_spec::(); let seconds_per_slot = spec.seconds_per_slot; - let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP_BYTES) - .map_err(|e| println!("Unable to read trusted setup file: {}", e)) - .unwrap(); + let kzg = spec.deneb_fork_epoch.map(|_| KZG.clone()); let mock = mock_execution_layer_from_parts(&harness.spec, harness.runtime.task_executor.clone()); @@ -2457,7 +2453,7 @@ async fn weak_subjectivity_sync_test(slots: Vec, checkpoint_slot: Slot) { 1, ))) .execution_layer(Some(mock.el)) - .trusted_setup(trusted_setup) + .kzg(kzg) .build() .expect("should build"); diff --git a/beacon_node/client/src/builder.rs b/beacon_node/client/src/builder.rs index 8ae4b9e2500..6c505751542 100644 --- a/beacon_node/client/src/builder.rs +++ b/beacon_node/client/src/builder.rs @@ -606,7 +606,12 @@ where }; let beacon_chain_builder = if let Some(trusted_setup) = config.trusted_setup { - beacon_chain_builder.trusted_setup(trusted_setup) + let kzg = trusted_setup + .try_into() + .map(Arc::new) + .map(Some) + .map_err(|e| format!("Failed to load trusted setup: {:?}", e))?; + beacon_chain_builder.kzg(kzg) } else { beacon_chain_builder }; diff --git a/beacon_node/execution_layer/src/test_utils/execution_block_generator.rs b/beacon_node/execution_layer/src/test_utils/execution_block_generator.rs index 87484ced67c..08f8ca68435 100644 --- a/beacon_node/execution_layer/src/test_utils/execution_block_generator.rs +++ b/beacon_node/execution_layer/src/test_utils/execution_block_generator.rs @@ -155,7 +155,7 @@ impl ExecutionBlockGenerator { shanghai_time: Option, cancun_time: Option, prague_time: Option, - kzg: Option, + kzg: Option>, ) -> Self { let mut gen = Self { head_block: <_>::default(), @@ -172,7 +172,7 @@ impl ExecutionBlockGenerator { cancun_time, prague_time, blobs_bundles: <_>::default(), - kzg: kzg.map(Arc::new), + kzg, rng: make_rng(), }; diff --git a/beacon_node/execution_layer/src/test_utils/mock_execution_layer.rs b/beacon_node/execution_layer/src/test_utils/mock_execution_layer.rs index fbd6744ea60..f76edfa90b7 100644 --- a/beacon_node/execution_layer/src/test_utils/mock_execution_layer.rs +++ b/beacon_node/execution_layer/src/test_utils/mock_execution_layer.rs @@ -43,7 +43,7 @@ impl MockExecutionLayer { prague_time: Option, jwt_key: Option, spec: ChainSpec, - kzg: Option, + kzg: Option>, ) -> Self { let handle = executor.handle().unwrap(); diff --git a/beacon_node/execution_layer/src/test_utils/mod.rs b/beacon_node/execution_layer/src/test_utils/mod.rs index 29ef1bb08df..eeb449b9920 100644 --- a/beacon_node/execution_layer/src/test_utils/mod.rs +++ b/beacon_node/execution_layer/src/test_utils/mod.rs @@ -109,7 +109,7 @@ impl MockServer { pub fn new_with_config( handle: &runtime::Handle, config: MockExecutionConfig, - kzg: Option, + kzg: Option>, ) -> Self { let MockExecutionConfig { jwt_key, @@ -193,7 +193,7 @@ impl MockServer { shanghai_time: Option, cancun_time: Option, prague_time: Option, - kzg: Option, + kzg: Option>, ) -> Self { Self::new_with_config( handle, diff --git a/crypto/kzg/src/lib.rs b/crypto/kzg/src/lib.rs index 0e096ba55c2..658dc9fe06e 100644 --- a/crypto/kzg/src/lib.rs +++ b/crypto/kzg/src/lib.rs @@ -142,3 +142,11 @@ impl Kzg { .map_err(Into::into) } } + +impl TryFrom for Kzg { + type Error = Error; + + fn try_from(trusted_setup: TrustedSetup) -> Result { + Kzg::new_from_trusted_setup(trusted_setup) + } +}