* some blob reprocessing work * remove ForceBlockLookup * reorder enum match arms in sync manager * a lot more reprocessing work * impl logic for triggerng blob lookups along with block lookups * deal with rpc blobs in groups per block in the da checker. don't cache missing blob ids in the da checker. * make single block lookup generic * more work * add delayed processing logic and combine some requests * start fixing some compile errors * fix compilation in main block lookup mod * much work * get things compiling * parent blob lookups * fix compile * revert red/stevie changes * fix up sync manager delay message logic * add peer usefulness enum * should remove lookup refactor * consolidate retry error handling * improve peer scoring during certain failures in parent lookups * improve retry code * drop parent lookup if either req has a peer disconnect during download * refactor single block processed method * processing peer refactor * smol bugfix * fix some todos * fix lints * fix lints * fix compile in lookup tests * fix lints * fix lints * fix existing block lookup tests * renamings * fix after merge * cargo fmt * compilation fix in beacon chain tests * fix * refactor lookup tests to work with multiple forks and response types * make tests into macros * wrap availability check error * fix compile after merge * add random blobs * start fixing up lookup verify error handling * some bug fixes and the start of deneb only tests * make tests work for all forks * track information about peer source * error refactoring * improve peer scoring * fix test compilation * make sure blobs are sent for processing after stream termination, delete copied tests * add some tests and fix a bug * smol bugfixes and moar tests * add tests and fix some things * compile after merge * lots of refactoring * retry on invalid block/blob * merge unknown parent messages before current slot lookup * get tests compiling * penalize blob peer on invalid blobs * Check disk on in-memory cache miss * Update beacon_node/beacon_chain/src/data_availability_checker/overflow_lru_cache.rs * Update beacon_node/network/src/sync/network_context.rs Co-authored-by: Divma <26765164+divagant-martian@users.noreply.github.com> * fix bug in matching blocks and blobs in range sync * pr feedback * fix conflicts * upgrade logs from warn to crit when we receive incorrect response in range * synced_and_connected_within_tolerance -> should_search_for_block * remove todo * add data gas used and update excess data gas to u64 * Fix Broken Overflow Tests * payload verification with commitments * fix merge conflicts * restore payload file * Restore payload file * remove todo * add max blob commitments per block * c-kzg lib update * Fix ef tests * Abstract over minimal/mainnet spec in kzg crate * Start integrating new KZG * checkpoint sync without alignment * checkpoint sync without alignment * add import * add import * query for checkpoint state by slot rather than state root (teku doesn't serve by state root) * query for checkpoint state by slot rather than state root (teku doesn't serve by state root) * loosen check * get state first and query by most recent block root * Revert "loosen check" This reverts commit 069d13dd63aa794a3505db9f17bd1a6b73f0be81. * get state first and query by most recent block root * merge max blobs change * simplify delay logic * rename unknown parent sync message variants * rename parameter, block_slot -> slot * add some docs to the lookup module * use interval instead of sleep * drop request if blocks and blobs requests both return `None` for `Id` * clean up `find_single_lookup` logic * add lookup source enum * clean up `find_single_lookup` logic * add docs to find_single_lookup_request * move LookupSource our of param where unnecessary * remove unnecessary todo * query for block by `state.latest_block_header.slot` * fix lint * fix merge transition ef tests * fix test * fix test * fix observed blob sidecars test * Add some metrics (#33) * fix protocol limits for blobs by root * Update Engine API for 1:1 Structure Method * make beacon chain tests to fix devnet 6 changes * get ckzg working and fix some tests * fix remaining tests * fix lints * Fix KZG linking issues * remove unused dep * lockfile * test fixes * remove dbgs * remove unwrap * cleanup tx generator * small fixes * fixing fixes * more self reivew * more self review * refactor genesis header initialization * refactor mock el instantiations * fix compile * fix network test, make sure they run for each fork * pr feedback * fix last test (hopefully) --------- Co-authored-by: Pawan Dhananjay <pawandhananjay@gmail.com> Co-authored-by: Mark Mackey <mark@sigmaprime.io> Co-authored-by: Divma <26765164+divagant-martian@users.noreply.github.com> Co-authored-by: Michael Sproul <michael@sigmaprime.io>
404 lines
15 KiB
Rust
404 lines
15 KiB
Rust
//! Provides the `Eth2NetworkConfig` struct which defines the configuration of an eth2 network or
|
|
//! test-network (aka "testnet").
|
|
//!
|
|
//! Whilst the `Eth2NetworkConfig` struct can be used to read a specification from a directory at
|
|
//! runtime, this crate also includes some pre-defined network configurations "built-in" to the
|
|
//! binary itself (the most notable of these being the "mainnet" configuration). When a network is
|
|
//! "built-in", the genesis state and configuration files is included in the final binary via the
|
|
//! `std::include_bytes` macro. This provides convenience to the user, the binary is self-sufficient
|
|
//! and does not require the configuration to be read from the filesystem at runtime.
|
|
//!
|
|
//! To add a new built-in testnet, add it to the `define_hardcoded_nets` invocation in the `eth2_config`
|
|
//! crate.
|
|
|
|
use discv5::enr::{CombinedKey, Enr};
|
|
use eth2_config::{instantiate_hardcoded_nets, HardcodedNet};
|
|
use kzg::{KzgPreset, KzgPresetId, TrustedSetup};
|
|
use std::fs::{create_dir_all, File};
|
|
use std::io::{Read, Write};
|
|
use std::path::PathBuf;
|
|
use std::str::FromStr;
|
|
use types::{BeaconState, ChainSpec, Config, Epoch, EthSpec, EthSpecId};
|
|
|
|
pub const DEPLOY_BLOCK_FILE: &str = "deploy_block.txt";
|
|
pub const BOOT_ENR_FILE: &str = "boot_enr.yaml";
|
|
pub const GENESIS_STATE_FILE: &str = "genesis.ssz";
|
|
pub const BASE_CONFIG_FILE: &str = "config.yaml";
|
|
|
|
// Creates definitions for:
|
|
//
|
|
// - Each of the `HardcodedNet` values (e.g., `MAINNET`, `PRATER`, etc).
|
|
// - `HARDCODED_NETS: &[HardcodedNet]`
|
|
// - `HARDCODED_NET_NAMES: &[&'static str]`
|
|
instantiate_hardcoded_nets!(eth2_config);
|
|
|
|
pub const DEFAULT_HARDCODED_NETWORK: &str = "mainnet";
|
|
|
|
/// 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!("../built_in_network_configs/testing_trusted_setups.json");
|
|
|
|
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_from_id(id: KzgPresetId) -> &'static [u8] {
|
|
match id {
|
|
KzgPresetId::Mainnet => TRUSTED_SETUP,
|
|
KzgPresetId::Minimal => TRUSTED_SETUP_MINIMAL,
|
|
}
|
|
}
|
|
|
|
/// Specifies an Eth2 network.
|
|
///
|
|
/// See the crate-level documentation for more details.
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
pub struct Eth2NetworkConfig {
|
|
/// Note: instead of the block where the contract is deployed, it is acceptable to set this
|
|
/// value to be the block number where the first deposit occurs.
|
|
pub deposit_contract_deploy_block: u64,
|
|
pub boot_enr: Option<Vec<Enr<CombinedKey>>>,
|
|
pub genesis_state_bytes: Option<Vec<u8>>,
|
|
pub config: Config,
|
|
pub kzg_trusted_setup: Option<TrustedSetup>,
|
|
}
|
|
|
|
impl Eth2NetworkConfig {
|
|
/// When Lighthouse is built it includes zero or more "hardcoded" network specifications. This
|
|
/// function allows for instantiating one of these nets by name.
|
|
pub fn constant(name: &str) -> Result<Option<Self>, String> {
|
|
HARDCODED_NETS
|
|
.iter()
|
|
.find(|net| net.name == name)
|
|
.map(Self::from_hardcoded_net)
|
|
.transpose()
|
|
}
|
|
|
|
/// Instantiates `Self` from a `HardcodedNet`.
|
|
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
|
|
};
|
|
Ok(Self {
|
|
deposit_contract_deploy_block: serde_yaml::from_reader(net.deploy_block)
|
|
.map_err(|e| format!("Unable to parse deploy block: {:?}", e))?,
|
|
boot_enr: Some(
|
|
serde_yaml::from_reader(net.boot_enr)
|
|
.map_err(|e| format!("Unable to parse boot enr: {:?}", e))?,
|
|
),
|
|
genesis_state_bytes: Some(net.genesis_state_bytes.to_vec())
|
|
.filter(|bytes| !bytes.is_empty()),
|
|
config,
|
|
kzg_trusted_setup,
|
|
})
|
|
}
|
|
|
|
/// Returns an identifier that should be used for selecting an `EthSpec` instance for this
|
|
/// network configuration.
|
|
pub fn eth_spec_id(&self) -> Result<EthSpecId, String> {
|
|
self.config
|
|
.eth_spec_id()
|
|
.ok_or_else(|| "Config does not match any known preset".to_string())
|
|
}
|
|
|
|
/// Returns `true` if this configuration contains a `BeaconState`.
|
|
pub fn beacon_state_is_known(&self) -> bool {
|
|
self.genesis_state_bytes.is_some()
|
|
}
|
|
|
|
/// Construct a consolidated `ChainSpec` from the YAML config.
|
|
pub fn chain_spec<E: EthSpec>(&self) -> Result<ChainSpec, String> {
|
|
ChainSpec::from_config::<E>(&self.config).ok_or_else(|| {
|
|
format!(
|
|
"YAML configuration incompatible with spec constants for {}",
|
|
E::spec_name()
|
|
)
|
|
})
|
|
}
|
|
|
|
/// Attempts to deserialize `self.beacon_state`, returning an error if it's missing or invalid.
|
|
pub fn beacon_state<E: EthSpec>(&self) -> Result<BeaconState<E>, String> {
|
|
let spec = self.chain_spec::<E>()?;
|
|
let genesis_state_bytes = self
|
|
.genesis_state_bytes
|
|
.as_ref()
|
|
.ok_or("Genesis state is unknown")?;
|
|
|
|
BeaconState::from_ssz_bytes(genesis_state_bytes, &spec)
|
|
.map_err(|e| format!("Genesis state SSZ bytes are invalid: {:?}", e))
|
|
}
|
|
|
|
/// Write the files to the directory.
|
|
///
|
|
/// Overwrites files if specified to do so.
|
|
pub fn write_to_file(&self, base_dir: PathBuf, overwrite: bool) -> Result<(), String> {
|
|
if base_dir.exists() && !overwrite {
|
|
return Err("Network directory already exists".to_string());
|
|
}
|
|
|
|
self.force_write_to_file(base_dir)
|
|
}
|
|
|
|
/// Write the files to the directory, even if the directory already exists.
|
|
pub fn force_write_to_file(&self, base_dir: PathBuf) -> Result<(), String> {
|
|
create_dir_all(&base_dir)
|
|
.map_err(|e| format!("Unable to create testnet directory: {:?}", e))?;
|
|
|
|
macro_rules! write_to_yaml_file {
|
|
($file: ident, $variable: expr) => {
|
|
File::create(base_dir.join($file))
|
|
.map_err(|e| format!("Unable to create {}: {:?}", $file, e))
|
|
.and_then(|mut file| {
|
|
let yaml = serde_yaml::to_string(&$variable)
|
|
.map_err(|e| format!("Unable to YAML encode {}: {:?}", $file, e))?;
|
|
|
|
// Remove the doc header from the YAML file.
|
|
//
|
|
// This allows us to play nice with other clients that are expecting
|
|
// plain-text, not YAML.
|
|
let no_doc_header = if let Some(stripped) = yaml.strip_prefix("---\n") {
|
|
stripped
|
|
} else {
|
|
&yaml
|
|
};
|
|
|
|
file.write_all(no_doc_header.as_bytes())
|
|
.map_err(|e| format!("Unable to write {}: {:?}", $file, e))
|
|
})?;
|
|
};
|
|
}
|
|
|
|
write_to_yaml_file!(DEPLOY_BLOCK_FILE, self.deposit_contract_deploy_block);
|
|
|
|
if let Some(boot_enr) = &self.boot_enr {
|
|
write_to_yaml_file!(BOOT_ENR_FILE, boot_enr);
|
|
}
|
|
|
|
write_to_yaml_file!(BASE_CONFIG_FILE, &self.config);
|
|
|
|
// The genesis state is a special case because it uses SSZ, not YAML.
|
|
if let Some(genesis_state_bytes) = &self.genesis_state_bytes {
|
|
let file = base_dir.join(GENESIS_STATE_FILE);
|
|
|
|
File::create(&file)
|
|
.map_err(|e| format!("Unable to create {:?}: {:?}", file, e))
|
|
.and_then(|mut file| {
|
|
file.write_all(genesis_state_bytes)
|
|
.map_err(|e| format!("Unable to write {:?}: {:?}", file, e))
|
|
})?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn load(base_dir: PathBuf) -> Result<Self, String> {
|
|
macro_rules! load_from_file {
|
|
($file: ident) => {
|
|
File::open(base_dir.join($file))
|
|
.map_err(|e| format!("Unable to open {}: {:?}", $file, e))
|
|
.and_then(|file| {
|
|
serde_yaml::from_reader(file)
|
|
.map_err(|e| format!("Unable to parse {}: {:?}", $file, e))
|
|
})?
|
|
};
|
|
}
|
|
|
|
macro_rules! optional_load_from_file {
|
|
($file: ident) => {
|
|
if base_dir.join($file).exists() {
|
|
Some(load_from_file!($file))
|
|
} else {
|
|
None
|
|
}
|
|
};
|
|
}
|
|
|
|
let deposit_contract_deploy_block = load_from_file!(DEPLOY_BLOCK_FILE);
|
|
let boot_enr = optional_load_from_file!(BOOT_ENR_FILE);
|
|
let config: Config = load_from_file!(BASE_CONFIG_FILE);
|
|
|
|
// The genesis state is a special case because it uses SSZ, not YAML.
|
|
let genesis_file_path = base_dir.join(GENESIS_STATE_FILE);
|
|
let genesis_state_bytes = if genesis_file_path.exists() {
|
|
let mut bytes = vec![];
|
|
File::open(&genesis_file_path)
|
|
.map_err(|e| format!("Unable to open {:?}: {:?}", genesis_file_path, e))
|
|
.and_then(|mut file| {
|
|
file.read_to_end(&mut bytes)
|
|
.map_err(|e| format!("Unable to read {:?}: {:?}", file, e))
|
|
})?;
|
|
|
|
Some(bytes).filter(|bytes| !bytes.is_empty())
|
|
} 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,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use ssz::Encode;
|
|
use tempfile::Builder as TempBuilder;
|
|
use types::{Config, Eth1Data, GnosisEthSpec, Hash256, MainnetEthSpec};
|
|
|
|
type E = MainnetEthSpec;
|
|
|
|
#[test]
|
|
fn default_network_exists() {
|
|
assert!(HARDCODED_NET_NAMES.contains(&DEFAULT_HARDCODED_NETWORK));
|
|
}
|
|
|
|
#[test]
|
|
fn hardcoded_testnet_names() {
|
|
assert_eq!(HARDCODED_NET_NAMES.len(), HARDCODED_NETS.len());
|
|
for (name, net) in HARDCODED_NET_NAMES.iter().zip(HARDCODED_NETS.iter()) {
|
|
assert_eq!(name, &net.name);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn mainnet_config_eq_chain_spec() {
|
|
let config = Eth2NetworkConfig::from_hardcoded_net(&MAINNET).unwrap();
|
|
let spec = ChainSpec::mainnet();
|
|
assert_eq!(spec, config.chain_spec::<E>().unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn gnosis_config_eq_chain_spec() {
|
|
let config = Eth2NetworkConfig::from_hardcoded_net(&GNOSIS).unwrap();
|
|
let spec = ChainSpec::gnosis();
|
|
assert_eq!(spec, config.chain_spec::<GnosisEthSpec>().unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn mainnet_genesis_state() {
|
|
let config = Eth2NetworkConfig::from_hardcoded_net(&MAINNET).unwrap();
|
|
config.beacon_state::<E>().expect("beacon state can decode");
|
|
}
|
|
|
|
#[test]
|
|
fn prater_and_goerli_are_equal() {
|
|
let goerli = Eth2NetworkConfig::from_hardcoded_net(&GOERLI).unwrap();
|
|
let prater = Eth2NetworkConfig::from_hardcoded_net(&PRATER).unwrap();
|
|
assert_eq!(goerli, prater);
|
|
}
|
|
|
|
#[test]
|
|
fn hard_coded_nets_work() {
|
|
for net in HARDCODED_NETS {
|
|
let config = Eth2NetworkConfig::from_hardcoded_net(net)
|
|
.unwrap_or_else(|_| panic!("{:?}", net.name));
|
|
|
|
// Ensure we can parse the YAML config to a chain spec.
|
|
if net.name == types::GNOSIS {
|
|
config.chain_spec::<GnosisEthSpec>().unwrap();
|
|
} else {
|
|
config.chain_spec::<MainnetEthSpec>().unwrap();
|
|
}
|
|
|
|
assert_eq!(
|
|
config.genesis_state_bytes.is_some(),
|
|
net.genesis_is_known,
|
|
"{:?}",
|
|
net.name
|
|
);
|
|
assert_eq!(config.config.config_name, Some(net.config_dir.to_string()));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn round_trip() {
|
|
let spec = &E::default_spec();
|
|
|
|
let eth1_data = Eth1Data {
|
|
deposit_root: Hash256::zero(),
|
|
deposit_count: 0,
|
|
block_hash: Hash256::zero(),
|
|
};
|
|
|
|
// TODO: figure out how to generate ENR and add some here.
|
|
let boot_enr = None;
|
|
let genesis_state = Some(BeaconState::new(42, eth1_data, spec));
|
|
let config = Config::from_chain_spec::<E>(spec);
|
|
|
|
do_test::<E>(boot_enr, genesis_state, config.clone());
|
|
do_test::<E>(None, None, config);
|
|
}
|
|
|
|
fn do_test<E: EthSpec>(
|
|
boot_enr: Option<Vec<Enr<CombinedKey>>>,
|
|
genesis_state: Option<BeaconState<E>>,
|
|
config: Config,
|
|
) {
|
|
let temp_dir = TempBuilder::new()
|
|
.prefix("eth2_testnet_test")
|
|
.tempdir()
|
|
.expect("should create temp dir");
|
|
let base_dir = temp_dir.path().join("my_testnet");
|
|
let deposit_contract_deploy_block = 42;
|
|
|
|
let testnet = Eth2NetworkConfig {
|
|
deposit_contract_deploy_block,
|
|
boot_enr,
|
|
genesis_state_bytes: genesis_state.as_ref().map(Encode::as_ssz_bytes),
|
|
config,
|
|
kzg_trusted_setup: None,
|
|
};
|
|
|
|
testnet
|
|
.write_to_file(base_dir.clone(), false)
|
|
.expect("should write to file");
|
|
|
|
let decoded = Eth2NetworkConfig::load(base_dir).expect("should load struct");
|
|
|
|
assert_eq!(testnet, decoded, "should decode as encoded");
|
|
}
|
|
}
|