Skip to content

Commit

Permalink
Trying something
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Aug 17, 2023
1 parent 5ff5406 commit 8b0eb15
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 71 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -81,7 +81,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::{TrustedSetup, get_trusted_setup_from_id, KzgPresetId};
pub use metrics::scrape_for_metrics;
pub use migrate::MigratorConfig;
pub use parking_lot;
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ where
.expect("cannot build without validator keypairs");
let chain_config = self.chain_config.unwrap_or_default();
let trusted_setup: TrustedSetup =
serde_json::from_reader(eth2_network_config::get_trusted_setup::<E::Kzg>())
serde_json::from_reader(kzg::get_trusted_setup::<E::Kzg>())
.map_err(|e| format!("Unable to read trusted setup file: {}", e))
.unwrap();

Expand Down Expand Up @@ -602,7 +602,7 @@ pub fn mock_execution_layer_from_parts<T: EthSpec>(
});

let trusted_setup: TrustedSetup =
serde_json::from_reader(eth2_network_config::get_trusted_setup::<T::Kzg>())
serde_json::from_reader(kzg::get_trusted_setup::<T::Kzg>())
.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");
Expand Down
8 changes: 4 additions & 4 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ pub fn get_config<E: EthSpec>(
}

// 4844 params
client_config.trusted_setup = context
.eth2_network_config
.as_ref()
.and_then(|config| config.kzg_trusted_setup.clone());
let trusted_setup_bytes = beacon_chain::get_trusted_setup_from_id(
beacon_chain::KzgPresetId::from_str(&E::spec_name().to_string())?,
);
client_config.trusted_setup = serde_json::from_reader(trusted_setup_bytes).unwrap();

// Override default trusted setup file if required
// TODO: consider removing this when we get closer to launch
Expand Down
1 change: 0 additions & 1 deletion common/eth2_network_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ tempfile = "3.1.0"
serde_yaml = "0.8.13"
serde_json = "1.0.58"
types = { path = "../../consensus/types"}
kzg = { path = "../../crypto/kzg" }
ethereum_ssz = "0.5.0"
eth2_config = { path = "../eth2_config"}
discv5 = "0.3.1"
91 changes: 45 additions & 46 deletions common/eth2_network_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use discv5::enr::{CombinedKey, Enr};
use eth2_config::{instantiate_hardcoded_nets, HardcodedNet};
use kzg::{KzgPreset, KzgPresetId, TrustedSetup};
// use kzg::{KzgPreset, KzgPresetId, TrustedSetup};
use std::fs::{create_dir_all, File};
use std::io::{Read, Write};
use std::path::PathBuf;
Expand All @@ -39,25 +39,25 @@ pub const DEFAULT_HARDCODED_NETWORK: &str = "mainnet";
///
/// This is done to ensure that testnets also inherit the high security and
/// randomness of the mainnet kzg trusted setup ceremony.
const TRUSTED_SETUP: &[u8] =
pub const TRUSTED_SETUP: &[u8] =
include_bytes!("../built_in_network_configs/testing_trusted_setups.json");

const TRUSTED_SETUP_MINIMAL: &[u8] =
pub const TRUSTED_SETUP_MINIMAL: &[u8] =
include_bytes!("../built_in_network_configs/minimal_testing_trusted_setups.json");

pub fn get_trusted_setup<P: KzgPreset>() -> &'static [u8] {
match P::spec_name() {
KzgPresetId::Mainnet => TRUSTED_SETUP,
KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
}
}
// pub fn get_trusted_setup<P: KzgPreset>() -> &'static [u8] {
// match P::spec_name() {
// KzgPresetId::Mainnet => TRUSTED_SETUP,
// KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
// }
// }

pub fn get_trusted_setup_from_id(id: KzgPresetId) -> &'static [u8] {
match id {
KzgPresetId::Mainnet => TRUSTED_SETUP,
KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
}
}
// pub fn get_trusted_setup_from_id(id: KzgPresetId) -> &'static [u8] {
// match id {
// KzgPresetId::Mainnet => TRUSTED_SETUP,
// KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
// }
// }

/// Specifies an Eth2 network.
///
Expand All @@ -70,7 +70,7 @@ pub struct Eth2NetworkConfig {
pub boot_enr: Option<Vec<Enr<CombinedKey>>>,
pub genesis_state_bytes: Option<Vec<u8>>,
pub config: Config,
pub kzg_trusted_setup: Option<TrustedSetup>,
// pub kzg_trusted_setup: Option<TrustedSetup>,
}

impl Eth2NetworkConfig {
Expand All @@ -88,20 +88,20 @@ impl Eth2NetworkConfig {
fn from_hardcoded_net(net: &HardcodedNet) -> Result<Self, String> {
let config: Config = serde_yaml::from_reader(net.config)
.map_err(|e| format!("Unable to parse yaml config: {:?}", e))?;
let kzg_trusted_setup = if let Some(epoch) = config.deneb_fork_epoch {
// Only load the trusted setup if the deneb fork epoch is set
if epoch.value != Epoch::max_value() {
let trusted_setup_bytes =
get_trusted_setup_from_id(KzgPresetId::from_str(&config.preset_base)?);
let trusted_setup: TrustedSetup = serde_json::from_reader(trusted_setup_bytes)
.map_err(|e| format!("Unable to read trusted setup file: {}", e))?;
Some(trusted_setup)
} else {
None
}
} else {
None
};
// let kzg_trusted_setup = if let Some(epoch) = config.deneb_fork_epoch {
// // Only load the trusted setup if the deneb fork epoch is set
// if epoch.value != Epoch::max_value() {
// let trusted_setup_bytes =
// get_trusted_setup_from_id(KzgPresetId::from_str(&config.preset_base)?);
// let trusted_setup: TrustedSetup = serde_json::from_reader(trusted_setup_bytes)
// .map_err(|e| format!("Unable to read trusted setup file: {}", e))?;
// Some(trusted_setup)
// } else {
// None
// }
// } else {
// None
// };
Ok(Self {
deposit_contract_deploy_block: serde_yaml::from_reader(net.deploy_block)
.map_err(|e| format!("Unable to parse deploy block: {:?}", e))?,
Expand All @@ -112,7 +112,6 @@ impl Eth2NetworkConfig {
genesis_state_bytes: Some(net.genesis_state_bytes.to_vec())
.filter(|bytes| !bytes.is_empty()),
config,
kzg_trusted_setup,
})
}

Expand Down Expand Up @@ -256,27 +255,27 @@ impl Eth2NetworkConfig {
None
};

let kzg_trusted_setup = if let Some(epoch) = config.deneb_fork_epoch {
// Only load the trusted setup if the deneb fork epoch is set
if epoch.value != Epoch::max_value() {
let trusted_setup: TrustedSetup = serde_json::from_reader(
get_trusted_setup_from_id(KzgPresetId::from_str(&config.preset_base)?),
)
.map_err(|e| format!("Unable to read trusted setup file: {}", e))?;
Some(trusted_setup)
} else {
None
}
} else {
None
};
// let kzg_trusted_setup = if let Some(epoch) = config.deneb_fork_epoch {
// // Only load the trusted setup if the deneb fork epoch is set
// if epoch.value != Epoch::max_value() {
// let trusted_setup: TrustedSetup = serde_json::from_reader(
// get_trusted_setup_from_id(KzgPresetId::from_str(&config.preset_base)?),
// )
// .map_err(|e| format!("Unable to read trusted setup file: {}", e))?;
// Some(trusted_setup)
// } else {
// None
// }
// } else {
// None
// };

Ok(Self {
deposit_contract_deploy_block,
boot_enr,
genesis_state_bytes,
config,
kzg_trusted_setup,
// kzg_trusted_setup,
})
}
}
Expand Down
26 changes: 26 additions & 0 deletions crypto/kzg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,29 @@ impl<P: KzgPreset> Kzg<P> {
.map_err(Error::InvalidKzgProof)
}
}


/// Contains the bytes from the trusted setup json.
/// The mainnet trusted setup is also reused in testnets.
///
/// This is done to ensure that testnets also inherit the high security and
/// randomness of the mainnet kzg trusted setup ceremony.
const TRUSTED_SETUP: &[u8] =
include_bytes!("../../../common/eth2_network_config/built_in_network_configs/testing_trusted_setups.json");

const TRUSTED_SETUP_MINIMAL: &[u8] =
include_bytes!("../../../common/eth2_network_config/built_in_network_configs/minimal_testing_trusted_setups.json");

pub fn get_trusted_setup<P: KzgPreset>() -> &'static [u8] {
match P::spec_name() {
KzgPresetId::Mainnet => TRUSTED_SETUP,
KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
}
}

pub fn get_trusted_setup_from_id(id: KzgPresetId) -> &'static [u8] {
match id {
KzgPresetId::Mainnet => TRUSTED_SETUP,
KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
}
}
17 changes: 2 additions & 15 deletions lcli/src/new_testnet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use account_utils::eth2_keystore::keypair_from_secret;
use clap::ArgMatches;
use clap_utils::{parse_optional, parse_required, parse_ssz_optional};
use eth2_network_config::{get_trusted_setup, Eth2NetworkConfig};
use eth2_network_config::{Eth2NetworkConfig};
use eth2_wallet::bip39::Seed;
use eth2_wallet::bip39::{Language, Mnemonic};
use eth2_wallet::{recover_validator_secret_from_mnemonic, KeyType};
Expand Down Expand Up @@ -196,25 +196,12 @@ pub fn run<T: EthSpec>(testnet_dir_path: PathBuf, matches: &ArgMatches) -> Resul
None
};

let kzg_trusted_setup = if let Some(epoch) = spec.deneb_fork_epoch {
// Only load the trusted setup if the deneb fork epoch is set
if epoch != Epoch::max_value() {
let trusted_setup: TrustedSetup =
serde_json::from_reader(get_trusted_setup::<T::Kzg>())
.map_err(|e| format!("Unable to read trusted setup file: {}", e))?;
Some(trusted_setup)
} else {
None
}
} else {
None
};

let testnet = Eth2NetworkConfig {
deposit_contract_deploy_block,
boot_enr: Some(vec![]),
genesis_state_bytes,
config: Config::from_chain_spec::<T>(&spec),
kzg_trusted_setup,
};

testnet.write_to_file(testnet_dir_path, overwrite_files)
Expand Down
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
@@ -1,7 +1,7 @@
use super::*;
use crate::case_result::compare_result;
use beacon_chain::kzg_utils::validate_blob;
use eth2_network_config::get_trusted_setup;
use kzg::get_trusted_setup;
use kzg::{Kzg, KzgCommitment, KzgPreset, KzgProof, TrustedSetup};
use serde_derive::Deserialize;
use std::convert::TryInto;
Expand Down

0 comments on commit 8b0eb15

Please sign in to comment.