Skip to content

Commit

Permalink
Improve single-node testnet support and Arc NetworkConfig/ChainSpec (s…
Browse files Browse the repository at this point in the history
…igp#6396)

* Arc ChainSpec and NetworkConfig

* Fix release tests

* Fix lint

* Merge remote-tracking branch 'origin/unstable' into single-node-testnet
  • Loading branch information
michaelsproul authored Sep 24, 2024
1 parent d84df57 commit 1447eeb
Show file tree
Hide file tree
Showing 66 changed files with 342 additions and 252 deletions.
4 changes: 3 additions & 1 deletion beacon_node/beacon_chain/src/beacon_block_streamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ mod tests {
use crate::beacon_block_streamer::{BeaconBlockStreamer, CheckCaches};
use crate::test_utils::{test_spec, BeaconChainHarness, EphemeralHarnessType};
use execution_layer::test_utils::Block;
use std::sync::Arc;
use std::sync::LazyLock;
use tokio::sync::mpsc;
use types::{
Expand All @@ -725,7 +726,7 @@ mod tests {

fn get_harness(
validator_count: usize,
spec: ChainSpec,
spec: Arc<ChainSpec>,
) -> BeaconChainHarness<EphemeralHarnessType<MinimalEthSpec>> {
let harness = BeaconChainHarness::builder(MinimalEthSpec)
.spec(spec)
Expand Down Expand Up @@ -756,6 +757,7 @@ mod tests {
spec.capella_fork_epoch = Some(Epoch::new(capella_fork_epoch as u64));
spec.deneb_fork_epoch = Some(Epoch::new(deneb_fork_epoch as u64));
spec.electra_fork_epoch = Some(Epoch::new(electra_fork_epoch as u64));
let spec = Arc::new(spec);

let harness = get_harness(VALIDATOR_COUNT, spec.clone());
// go to bellatrix fork
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ type ReqRespPreImportCache<E> = HashMap<Hash256, Arc<SignedBeaconBlock<E>>>;
/// Represents the "Beacon Chain" component of Ethereum 2.0. Allows import of blocks and block
/// operations and chooses a canonical head.
pub struct BeaconChain<T: BeaconChainTypes> {
pub spec: ChainSpec,
pub spec: Arc<ChainSpec>,
/// Configuration for `BeaconChain` runtime behaviour.
pub config: ChainConfig,
/// Persistent storage for blocks, states, etc. Typically an on-disk store, such as LevelDB.
Expand Down
14 changes: 9 additions & 5 deletions beacon_node/beacon_chain/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct BeaconChainBuilder<T: BeaconChainTypes> {
light_client_server_tx: Option<Sender<LightClientProducerEvent<T::EthSpec>>>,
head_tracker: Option<HeadTracker>,
validator_pubkey_cache: Option<ValidatorPubkeyCache<T>>,
spec: ChainSpec,
spec: Arc<ChainSpec>,
chain_config: ChainConfig,
log: Option<Logger>,
beacon_graffiti: GraffitiOrigin,
Expand Down Expand Up @@ -137,7 +137,7 @@ where
light_client_server_tx: None,
head_tracker: None,
validator_pubkey_cache: None,
spec: E::default_spec(),
spec: Arc::new(E::default_spec()),
chain_config: ChainConfig::default(),
log: None,
beacon_graffiti: GraffitiOrigin::default(),
Expand All @@ -154,7 +154,7 @@ where
///
/// This method should generally be called immediately after `Self::new` to ensure components
/// are started with a consistent spec.
pub fn custom_spec(mut self, spec: ChainSpec) -> Self {
pub fn custom_spec(mut self, spec: Arc<ChainSpec>) -> Self {
self.spec = spec;
self
}
Expand Down Expand Up @@ -1183,8 +1183,12 @@ mod test {
MinimalEthSpec,
MemoryStore<MinimalEthSpec>,
MemoryStore<MinimalEthSpec>,
> = HotColdDB::open_ephemeral(StoreConfig::default(), ChainSpec::minimal(), log.clone())
.unwrap();
> = HotColdDB::open_ephemeral(
StoreConfig::default(),
ChainSpec::minimal().into(),
log.clone(),
)
.unwrap();
let spec = MinimalEthSpec::default_spec();

let genesis_state = interop_genesis_state(
Expand Down
3 changes: 1 addition & 2 deletions beacon_node/beacon_chain/src/data_availability_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
kzg: Arc<Kzg>,
store: BeaconStore<T>,
import_all_data_columns: bool,
spec: ChainSpec,
spec: Arc<ChainSpec>,
) -> Result<Self, AvailabilityCheckError> {
let spec = Arc::new(spec);
let custody_subnet_count = if import_all_data_columns {
spec.data_column_sidecar_subnet_count as usize
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ mod test {

fn get_store_with_spec<E: EthSpec>(
db_path: &TempDir,
spec: ChainSpec,
spec: Arc<ChainSpec>,
log: Logger,
) -> Arc<HotColdDB<E, LevelDB<E>, LevelDB<E>>> {
let hot_path = db_path.path().join("hot_db");
Expand Down Expand Up @@ -741,6 +741,7 @@ mod test {
spec.bellatrix_fork_epoch = Some(bellatrix_fork_epoch);
spec.capella_fork_epoch = Some(capella_fork_epoch);
spec.deneb_fork_epoch = Some(deneb_fork_epoch);
let spec = Arc::new(spec);

let chain_store = get_store_with_spec::<E>(db_path, spec.clone(), log.clone());
let validators_keypairs =
Expand Down Expand Up @@ -884,7 +885,7 @@ mod test {
let log = test_logger();
let chain_db_path = tempdir().expect("should get temp dir");
let harness = get_deneb_chain(log.clone(), &chain_db_path).await;
let spec = Arc::new(harness.spec.clone());
let spec = harness.spec.clone();
let test_store = harness.chain.store.clone();
let capacity_non_zero = new_non_zero_usize(capacity);
let cache = Arc::new(
Expand Down
14 changes: 8 additions & 6 deletions beacon_node/beacon_chain/src/eth1_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use state_processing::per_block_processing::get_new_eth1_data;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use store::{DBColumn, Error as StoreError, StoreItem};
use task_executor::TaskExecutor;
Expand Down Expand Up @@ -284,7 +285,7 @@ where
ssz_container: &SszEth1,
config: Eth1Config,
log: &Logger,
spec: ChainSpec,
spec: Arc<ChainSpec>,
) -> Result<Self, String> {
let backend =
Eth1ChainBackend::from_bytes(&ssz_container.backend_bytes, config, log.clone(), spec)?;
Expand Down Expand Up @@ -355,7 +356,7 @@ pub trait Eth1ChainBackend<E: EthSpec>: Sized + Send + Sync {
bytes: &[u8],
config: Eth1Config,
log: Logger,
spec: ChainSpec,
spec: Arc<ChainSpec>,
) -> Result<Self, String>;
}

Expand Down Expand Up @@ -413,7 +414,7 @@ impl<E: EthSpec> Eth1ChainBackend<E> for DummyEth1ChainBackend<E> {
_bytes: &[u8],
_config: Eth1Config,
_log: Logger,
_spec: ChainSpec,
_spec: Arc<ChainSpec>,
) -> Result<Self, String> {
Ok(Self(PhantomData))
}
Expand Down Expand Up @@ -441,7 +442,7 @@ impl<E: EthSpec> CachingEth1Backend<E> {
/// Instantiates `self` with empty caches.
///
/// Does not connect to the eth1 node or start any tasks to keep the cache updated.
pub fn new(config: Eth1Config, log: Logger, spec: ChainSpec) -> Result<Self, String> {
pub fn new(config: Eth1Config, log: Logger, spec: Arc<ChainSpec>) -> Result<Self, String> {
Ok(Self {
core: HttpService::new(config, log.clone(), spec)
.map_err(|e| format!("Failed to create eth1 http service: {:?}", e))?,
Expand Down Expand Up @@ -596,7 +597,7 @@ impl<E: EthSpec> Eth1ChainBackend<E> for CachingEth1Backend<E> {
bytes: &[u8],
config: Eth1Config,
log: Logger,
spec: ChainSpec,
spec: Arc<ChainSpec>,
) -> Result<Self, String> {
let inner = HttpService::from_bytes(bytes, config, log.clone(), spec)?;
Ok(Self {
Expand Down Expand Up @@ -752,7 +753,8 @@ mod test {

let log = test_logger();
Eth1Chain::new(
CachingEth1Backend::new(eth1_config, log, MainnetEthSpec::default_spec()).unwrap(),
CachingEth1Backend::new(eth1_config, log, Arc::new(MainnetEthSpec::default_spec()))
.unwrap(),
)
}

Expand Down
9 changes: 5 additions & 4 deletions beacon_node/beacon_chain/src/graffiti_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ mod tests {
use execution_layer::test_utils::{DEFAULT_CLIENT_VERSION, DEFAULT_ENGINE_CAPABILITIES};
use execution_layer::EngineCapabilities;
use slog::info;
use std::sync::Arc;
use std::sync::LazyLock;
use std::time::Duration;
use types::{ChainSpec, Graffiti, Keypair, MinimalEthSpec, GRAFFITI_BYTES_LEN};
Expand All @@ -253,7 +254,7 @@ mod tests {

fn get_harness(
validator_count: usize,
spec: ChainSpec,
spec: Arc<ChainSpec>,
chain_config: Option<ChainConfig>,
) -> BeaconChainHarness<EphemeralHarnessType<MinimalEthSpec>> {
let harness = BeaconChainHarness::builder(MinimalEthSpec)
Expand All @@ -272,7 +273,7 @@ mod tests {

#[tokio::test]
async fn check_graffiti_without_el_version_support() {
let spec = test_spec::<MinimalEthSpec>();
let spec = Arc::new(test_spec::<MinimalEthSpec>());
let harness = get_harness(VALIDATOR_COUNT, spec, None);
// modify execution engine so it doesn't support engine_getClientVersionV1 method
let mock_execution_layer = harness.mock_execution_layer.as_ref().unwrap();
Expand Down Expand Up @@ -313,7 +314,7 @@ mod tests {

#[tokio::test]
async fn check_graffiti_with_el_version_support() {
let spec = test_spec::<MinimalEthSpec>();
let spec = Arc::new(test_spec::<MinimalEthSpec>());
let harness = get_harness(VALIDATOR_COUNT, spec, None);

let found_graffiti_bytes = harness.chain.graffiti_calculator.get_graffiti(None).await.0;
Expand Down Expand Up @@ -355,7 +356,7 @@ mod tests {

#[tokio::test]
async fn check_graffiti_with_validator_specified_value() {
let spec = test_spec::<MinimalEthSpec>();
let spec = Arc::new(test_spec::<MinimalEthSpec>());
let harness = get_harness(VALIDATOR_COUNT, spec, None);

let graffiti_str = "nice graffiti bro";
Expand Down
9 changes: 5 additions & 4 deletions beacon_node/beacon_chain/src/observed_data_sidecars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use crate::observed_block_producers::ProposalKey;
use std::collections::{HashMap, HashSet};
use std::marker::PhantomData;
use std::sync::Arc;
use types::{BlobSidecar, ChainSpec, DataColumnSidecar, EthSpec, Slot};

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -74,13 +75,13 @@ pub struct ObservedDataSidecars<T: ObservableDataSidecar> {
finalized_slot: Slot,
/// Stores all received data indices for a given `(ValidatorIndex, Slot)` tuple.
items: HashMap<ProposalKey, HashSet<u64>>,
spec: ChainSpec,
spec: Arc<ChainSpec>,
_phantom: PhantomData<T>,
}

impl<T: ObservableDataSidecar> ObservedDataSidecars<T> {
/// Instantiates `Self` with `finalized_slot == 0`.
pub fn new(spec: ChainSpec) -> Self {
pub fn new(spec: Arc<ChainSpec>) -> Self {
Self {
finalized_slot: Slot::new(0),
items: HashMap::new(),
Expand Down Expand Up @@ -167,7 +168,7 @@ mod tests {

#[test]
fn pruning() {
let spec = test_spec::<E>();
let spec = Arc::new(test_spec::<E>());
let mut cache = ObservedDataSidecars::<BlobSidecar<E>>::new(spec);

assert_eq!(cache.finalized_slot, 0, "finalized slot is zero");
Expand Down Expand Up @@ -306,7 +307,7 @@ mod tests {

#[test]
fn simple_observations() {
let spec = test_spec::<E>();
let spec = Arc::new(test_spec::<E>());
let mut cache = ObservedDataSidecars::<BlobSidecar<E>>::new(spec);

// Slot 0, index 0
Expand Down
10 changes: 5 additions & 5 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub fn test_spec<E: EthSpec>() -> ChainSpec {

pub struct Builder<T: BeaconChainTypes> {
eth_spec_instance: T::EthSpec,
spec: Option<ChainSpec>,
spec: Option<Arc<ChainSpec>>,
validator_keypairs: Option<Vec<Keypair>>,
withdrawal_keypairs: Vec<Option<Keypair>>,
chain_config: Option<ChainConfig>,
Expand Down Expand Up @@ -395,12 +395,12 @@ where
self.spec_or_default(None)
}

pub fn spec(self, spec: ChainSpec) -> Self {
pub fn spec(self, spec: Arc<ChainSpec>) -> Self {
self.spec_or_default(Some(spec))
}

pub fn spec_or_default(mut self, spec: Option<ChainSpec>) -> Self {
self.spec = Some(spec.unwrap_or_else(test_spec::<E>));
pub fn spec_or_default(mut self, spec: Option<Arc<ChainSpec>>) -> Self {
self.spec = Some(spec.unwrap_or_else(|| Arc::new(test_spec::<E>())));
self
}

Expand Down Expand Up @@ -648,7 +648,7 @@ pub struct BeaconChainHarness<T: BeaconChainTypes> {
pub withdrawal_keypairs: Vec<Option<Keypair>>,

pub chain: Arc<BeaconChain<T>>,
pub spec: ChainSpec,
pub spec: Arc<ChainSpec>,
pub shutdown_receiver: Arc<Mutex<Receiver<ShutdownReason>>>,
pub runtime: TestRuntime,

Expand Down
3 changes: 2 additions & 1 deletion beacon_node/beacon_chain/src/validator_pubkey_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ mod test {

fn get_store() -> BeaconStore<T> {
Arc::new(
HotColdDB::open_ephemeral(<_>::default(), E::default_spec(), test_logger()).unwrap(),
HotColdDB::open_ephemeral(<_>::default(), Arc::new(E::default_spec()), test_logger())
.unwrap(),
)
}

Expand Down
6 changes: 4 additions & 2 deletions beacon_node/beacon_chain/tests/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ssz_types::BitVector;
use state_processing::{
per_block_processing::errors::AttestationValidationError, per_slot_processing,
};
use std::sync::LazyLock;
use std::sync::{Arc, LazyLock};
use tree_hash::TreeHash;
use types::{
signed_aggregate_and_proof::SignedAggregateAndProofRefMut,
Expand Down Expand Up @@ -47,6 +47,7 @@ fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessTyp
// A kind-of arbitrary number that ensures that _some_ validators are aggregators, but
// not all.
spec.target_aggregators_per_committee = 4;
let spec = Arc::new(spec);

let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
Expand All @@ -68,11 +69,12 @@ fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessTyp
/// all genesis validators start with BLS withdrawal credentials.
fn get_harness_capella_spec(
validator_count: usize,
) -> (BeaconChainHarness<EphemeralHarnessType<E>>, ChainSpec) {
) -> (BeaconChainHarness<EphemeralHarnessType<E>>, Arc<ChainSpec>) {
let mut spec = E::default_spec();
spec.altair_fork_epoch = Some(Epoch::new(0));
spec.bellatrix_fork_epoch = Some(Epoch::new(0));
spec.capella_fork_epoch = Some(Epoch::new(CAPELLA_FORK_EPOCH as u64));
let spec = Arc::new(spec);

let validator_keypairs = KEYPAIRS[0..validator_count].to_vec();
let genesis_state = interop_genesis_state(
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/tests/bellatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn merge_with_terminal_block_hash_override() {
spec.terminal_block_hash = genesis_pow_block_hash;

let harness = BeaconChainHarness::builder(E::default())
.spec(spec)
.spec(spec.into())
.logger(logging::test_logger())
.deterministic_keypairs(VALIDATOR_COUNT)
.fresh_ephemeral_store()
Expand Down Expand Up @@ -106,7 +106,7 @@ async fn base_altair_bellatrix_with_terminal_block_after_fork() {
let mut execution_payloads = vec![];

let harness = BeaconChainHarness::builder(E::default())
.spec(spec)
.spec(spec.into())
.logger(logging::test_logger())
.deterministic_keypairs(VALIDATOR_COUNT)
.fresh_ephemeral_store()
Expand Down
6 changes: 3 additions & 3 deletions beacon_node/beacon_chain/tests/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ async fn add_base_block_to_altair_chain() {
spec.altair_fork_epoch = Some(Epoch::new(1));

let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
.spec(spec.into())
.keypairs(KEYPAIRS[..].to_vec())
.fresh_ephemeral_store()
.mock_execution_layer()
Expand Down Expand Up @@ -1489,7 +1489,7 @@ async fn add_altair_block_to_base_chain() {
spec.altair_fork_epoch = None;

let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
.spec(spec.into())
.keypairs(KEYPAIRS[..].to_vec())
.fresh_ephemeral_store()
.mock_execution_layer()
Expand Down Expand Up @@ -1622,7 +1622,7 @@ async fn import_duplicate_block_unrealized_justification() {
let spec = MainnetEthSpec::default_spec();

let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
.spec(spec.into())
.keypairs(KEYPAIRS[..].to_vec())
.fresh_ephemeral_store()
.mock_execution_layer()
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/tests/capella.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn base_altair_bellatrix_capella() {
spec.capella_fork_epoch = Some(capella_fork_epoch);

let harness = BeaconChainHarness::builder(E::default())
.spec(spec)
.spec(spec.into())
.logger(logging::test_logger())
.deterministic_keypairs(VALIDATOR_COUNT)
.fresh_ephemeral_store()
Expand Down
Loading

0 comments on commit 1447eeb

Please sign in to comment.