Skip to content

Commit

Permalink
Merge of #5555
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Apr 11, 2024
2 parents 34dbb32 + 72f692c commit e6f571d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 40 deletions.
23 changes: 7 additions & 16 deletions beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -101,7 +101,7 @@ pub struct BeaconChainBuilder<T: BeaconChainTypes> {
// 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<KeyValueStoreOp>,
trusted_setup: Option<TrustedSetup>,
kzg: Option<Arc<Kzg>>,
task_executor: Option<TaskExecutor>,
validator_monitor_config: Option<ValidatorMonitorConfig>,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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<Arc<Kzg>>) -> Self {
self.kzg = kzg;
self
}

Expand Down Expand Up @@ -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))?;
Expand Down Expand Up @@ -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)),
};

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 16 additions & 10 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Kzg> = {
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<E, THotStore, TColdStore> =
Witness<TestingSlotClock, CachingEth1Backend<E>, E, THotStore, TColdStore>;

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -587,10 +596,7 @@ pub fn mock_execution_layer_from_parts<E: EthSpec>(
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,
Expand All @@ -600,7 +606,7 @@ pub fn mock_execution_layer_from_parts<E: EthSpec>(
prague_time,
Some(JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap()),
spec.clone(),
Some(kzg),
kzg_opt,
)
}

Expand Down
10 changes: 3 additions & 7 deletions beacon_node/beacon_chain/tests/store_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2418,9 +2416,7 @@ async fn weak_subjectivity_sync_test(slots: Vec<Slot>, checkpoint_slot: Slot) {
let store = get_store(&temp2);
let spec = test_spec::<E>();
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());
Expand Down Expand Up @@ -2457,7 +2453,7 @@ async fn weak_subjectivity_sync_test(slots: Vec<Slot>, checkpoint_slot: Slot) {
1,
)))
.execution_layer(Some(mock.el))
.trusted_setup(trusted_setup)
.kzg(kzg)
.build()
.expect("should build");

Expand Down
7 changes: 6 additions & 1 deletion beacon_node/client/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
shanghai_time: Option<u64>,
cancun_time: Option<u64>,
prague_time: Option<u64>,
kzg: Option<Kzg>,
kzg: Option<Arc<Kzg>>,
) -> Self {
let mut gen = Self {
head_block: <_>::default(),
Expand All @@ -172,7 +172,7 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
cancun_time,
prague_time,
blobs_bundles: <_>::default(),
kzg: kzg.map(Arc::new),
kzg,
rng: make_rng(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<E: EthSpec> MockExecutionLayer<E> {
prague_time: Option<u64>,
jwt_key: Option<JwtKey>,
spec: ChainSpec,
kzg: Option<Kzg>,
kzg: Option<Arc<Kzg>>,
) -> Self {
let handle = executor.handle().unwrap();

Expand Down
4 changes: 2 additions & 2 deletions beacon_node/execution_layer/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<E: EthSpec> MockServer<E> {
pub fn new_with_config(
handle: &runtime::Handle,
config: MockExecutionConfig,
kzg: Option<Kzg>,
kzg: Option<Arc<Kzg>>,
) -> Self {
let MockExecutionConfig {
jwt_key,
Expand Down Expand Up @@ -193,7 +193,7 @@ impl<E: EthSpec> MockServer<E> {
shanghai_time: Option<u64>,
cancun_time: Option<u64>,
prague_time: Option<u64>,
kzg: Option<Kzg>,
kzg: Option<Arc<Kzg>>,
) -> Self {
Self::new_with_config(
handle,
Expand Down
8 changes: 8 additions & 0 deletions crypto/kzg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ impl Kzg {
.map_err(Into::into)
}
}

impl TryFrom<TrustedSetup> for Kzg {
type Error = Error;

fn try_from(trusted_setup: TrustedSetup) -> Result<Self, Self::Error> {
Kzg::new_from_trusted_setup(trusted_setup)
}
}

0 comments on commit e6f571d

Please sign in to comment.