Skip to content

Commit

Permalink
Load DASContext conditionally.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed Aug 2, 2024
1 parent 0807808 commit 89fd62a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 41 deletions.
10 changes: 5 additions & 5 deletions beacon_node/beacon_chain/src/kzg_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub fn reconstruct_data_columns<E: EthSpec>(
#[cfg(test)]
mod test {
use crate::kzg_utils::{build_data_column_sidecars, reconstruct_data_columns};
use crate::test_utils::KZG;
use crate::test_utils::KZG_DAS;
use bls::Signature;
use kzg::KzgCommitment;
use types::{
Expand All @@ -343,7 +343,7 @@ mod test {
let (signed_block, blob_sidecars) = create_test_block_and_blobs::<E>(num_of_blobs, &spec);

let column_sidecars =
build_data_column_sidecars(&blob_sidecars, &signed_block, &KZG, &spec).unwrap();
build_data_column_sidecars(&blob_sidecars, &signed_block, &KZG_DAS, &spec).unwrap();

assert!(column_sidecars.is_empty());
}
Expand All @@ -356,7 +356,7 @@ mod test {
let (signed_block, blob_sidecars) = create_test_block_and_blobs::<E>(num_of_blobs, &spec);

let column_sidecars =
build_data_column_sidecars(&blob_sidecars, &signed_block, &KZG, &spec).unwrap();
build_data_column_sidecars(&blob_sidecars, &signed_block, &KZG_DAS, &spec).unwrap();

let block_kzg_commitments = signed_block
.message()
Expand Down Expand Up @@ -395,11 +395,11 @@ mod test {
let (signed_block, blob_sidecars) = create_test_block_and_blobs::<E>(num_of_blobs, &spec);

let column_sidecars =
build_data_column_sidecars(&blob_sidecars, &signed_block, &KZG, &spec).unwrap();
build_data_column_sidecars(&blob_sidecars, &signed_block, &KZG_DAS, &spec).unwrap();

// Now reconstruct
let reconstructed_columns = reconstruct_data_columns(
&KZG,
&KZG_DAS,
&column_sidecars.iter().as_slice()[0..column_sidecars.len() / 2],
&spec,
)
Expand Down
16 changes: 12 additions & 4 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ pub const FORK_NAME_ENV_VAR: &str = "FORK_NAME";
// a different value.
pub const DEFAULT_TARGET_AGGREGATORS: u64 = u64::MAX;

pub static KZG: LazyLock<Arc<Kzg>> = LazyLock::new(|| {
pub static KZG_4844_ONLY: LazyLock<Arc<Kzg>> = LazyLock::new(|| {
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 = Kzg::new_from_trusted_setup(trusted_setup, false).expect("should create kzg");
Arc::new(kzg)
});

pub static KZG_DAS: LazyLock<Arc<Kzg>> = LazyLock::new(|| {
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, true).expect("should create kzg");
Arc::new(kzg)
});

Expand Down Expand Up @@ -507,7 +515,7 @@ where
let validator_keypairs = self
.validator_keypairs
.expect("cannot build without validator keypairs");
let kzg = spec.deneb_fork_epoch.map(|_| KZG.clone());
let kzg = spec.deneb_fork_epoch.map(|_| KZG_4844_ONLY.clone());

let validator_monitor_config = self.validator_monitor_config.unwrap_or_default();

Expand Down Expand Up @@ -587,7 +595,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 kzg_opt = spec.deneb_fork_epoch.map(|_| KZG.clone());
let kzg_opt = spec.deneb_fork_epoch.map(|_| KZG_4844_ONLY.clone());

MockExecutionLayer::new(
task_executor,
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/tests/store_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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, KZG,
BlockStrategy, DiskHarnessType, KZG_4844_ONLY,
};
use beacon_chain::{
data_availability_checker::MaybeAvailableBlock, historical_blocks::HistoricalBlockError,
Expand Down Expand Up @@ -2392,7 +2392,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 kzg = spec.deneb_fork_epoch.map(|_| KZG.clone());
let kzg = spec.deneb_fork_epoch.map(|_| KZG_4844_ONLY.clone());

let mock =
mock_execution_layer_from_parts(&harness.spec, harness.runtime.task_executor.clone());
Expand Down
8 changes: 4 additions & 4 deletions beacon_node/client/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use beacon_chain::graffiti_calculator::start_engine_version_cache_refresh_servic
use beacon_chain::otb_verification_service::start_otb_verification_service;
use beacon_chain::proposer_prep_service::start_proposer_prep_service;
use beacon_chain::schema_change::migrate_schema;
use beacon_chain::LightClientProducerEvent;
use beacon_chain::{
builder::{BeaconChainBuilder, Witness},
eth1_chain::{CachingEth1Backend, Eth1Chain},
Expand All @@ -19,6 +18,7 @@ use beacon_chain::{
store::{HotColdDB, ItemStore, LevelDB, StoreConfig},
BeaconChain, BeaconChainTypes, Eth1ChainBackend, MigratorConfig, ServerSentEventHandler,
};
use beacon_chain::{Kzg, LightClientProducerEvent};
use beacon_processor::{BeaconProcessor, BeaconProcessorChannels};
use beacon_processor::{BeaconProcessorConfig, BeaconProcessorQueueLengths};
use environment::RuntimeContext;
Expand Down Expand Up @@ -504,7 +504,7 @@ where
deposit_snapshot.and_then(|snapshot| match Eth1Service::from_deposit_snapshot(
config.eth1,
context.log().clone(),
spec,
spec.clone(),
&snapshot,
) {
Ok(service) => {
Expand Down Expand Up @@ -623,8 +623,8 @@ where
};

let beacon_chain_builder = if let Some(trusted_setup) = config.trusted_setup {
let kzg = trusted_setup
.try_into()
let peer_das_scheduled = spec.eip7594_fork_epoch == Some(spec.far_future_epoch);
let kzg = Kzg::new_from_trusted_setup(trusted_setup, peer_das_scheduled)
.map(Arc::new)
.map(Some)
.map_err(|e| format!("Failed to load trusted setup: {:?}", e))?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ mod test {
fn load_kzg() -> Result<Kzg, String> {
let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP_BYTES)
.map_err(|e| format!("Unable to read trusted setup file: {e:?}"))?;
Kzg::new_from_trusted_setup(trusted_setup)
Kzg::new_from_trusted_setup(trusted_setup, false)
.map_err(|e| format!("Failed to load trusted setup: {e:?}"))
}
}
51 changes: 27 additions & 24 deletions crypto/kzg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub enum Error {
KzgVerificationFailed,
/// Misc indexing error
InconsistentArrayLength(String),
/// Unexpected PeerDAS KZG usage, PeerDAS not scheduled.
DASContextUninitialized,
}

impl From<c_kzg::Error> for Error {
Expand All @@ -47,23 +49,28 @@ impl From<c_kzg::Error> for Error {
#[derive(Debug)]
pub struct Kzg {
trusted_setup: KzgSettings,
context: DASContext,
context: Option<DASContext>,
}

impl Kzg {
/// Load the kzg trusted setup parameters from a vec of G1 and G2 points.
pub fn new_from_trusted_setup(trusted_setup: TrustedSetup) -> Result<Self, Error> {
// Initialize the trusted setup using default parameters
//
// Note: One can also use `from_json` to initialize it from the consensus-specs
// json string.
let peerdas_trusted_setup = PeerDASTrustedSetup::from(&trusted_setup);
// Set the number of threads to be used
//
// we set it to 1 to match the c-kzg performance
const NUM_THREADS: usize = 1;

let context = DASContext::with_threads(&peerdas_trusted_setup, NUM_THREADS);
pub fn new_from_trusted_setup(
trusted_setup: TrustedSetup,
das_enabled: bool,
) -> Result<Self, Error> {
let context = das_enabled.then(|| {
// Initialize the trusted setup using default parameters
//
// Note: One can also use `from_json` to initialize it from the consensus-specs
// json string.
let peerdas_trusted_setup = PeerDASTrustedSetup::from(&trusted_setup);
// Set the number of threads to be used
//
// we set it to 1 to match the c-kzg performance
const NUM_THREADS: usize = 1;

DASContext::with_threads(&peerdas_trusted_setup, NUM_THREADS)
});

Ok(Self {
trusted_setup: KzgSettings::load_trusted_setup(
Expand All @@ -74,6 +81,10 @@ impl Kzg {
})
}

fn context(&self) -> Result<&DASContext, Error> {
self.context.as_ref().ok_or(Error::DASContextUninitialized)
}

/// Compute the kzg proof given a blob and its kzg commitment.
pub fn compute_blob_kzg_proof(
&self,
Expand Down Expand Up @@ -179,7 +190,7 @@ impl Kzg {
blob: KzgBlobRef<'a>,
) -> Result<CellsAndKzgProofs, Error> {
let (cells, proofs) = self
.context
.context()?
.compute_cells_and_kzg_proofs(blob)
.map_err(Error::PeerDASKZG)?;

Expand All @@ -206,7 +217,7 @@ impl Kzg {
.iter()
.map(|commitment| commitment.as_ref())
.collect();
let verification_result = self.context.verify_cell_kzg_proof_batch(
let verification_result = self.context()?.verify_cell_kzg_proof_batch(
commitments.to_vec(),
columns,
cells.to_vec(),
Expand All @@ -228,7 +239,7 @@ impl Kzg {
cells: &[CellRef<'a>],
) -> Result<CellsAndKzgProofs, Error> {
let (cells, proofs) = self
.context
.context()?
.recover_cells_and_proofs(cell_ids.to_vec(), cells.to_vec())
.map_err(Error::PeerDASKZG)?;

Expand All @@ -237,11 +248,3 @@ impl Kzg {
Ok((cells, c_kzg_proof))
}
}

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)
}
}
2 changes: 1 addition & 1 deletion testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use types::Blob;
pub fn get_kzg() -> Result<Kzg, Error> {
let trusted_setup: TrustedSetup = serde_json::from_reader(TRUSTED_SETUP_BYTES)
.map_err(|e| Error::InternalError(format!("Failed to initialize kzg: {:?}", e)))?;
Kzg::new_from_trusted_setup(trusted_setup)
Kzg::new_from_trusted_setup(trusted_setup, false)
.map_err(|e| Error::InternalError(format!("Failed to initialize kzg: {:?}", e)))
}

Expand Down

0 comments on commit 89fd62a

Please sign in to comment.