703c33bdc7
## Issue Addressed Closes #1557 ## Proposed Changes Modify the pruning algorithm so that it mutates the head-tracker _before_ committing the database transaction to disk, and _only if_ all the heads to be removed are still present in the head-tracker (i.e. no concurrent mutations). In the process of writing and testing this I also had to make a few other changes: * Use internal mutability for all `BeaconChainHarness` functions (namely the RNG and the graffiti), in order to enable parallel calls (see testing section below). * Disable logging in harness tests unless the `test_logger` feature is turned on And chose to make some clean-ups: * Delete the `NullMigrator` * Remove type-based configuration for the migrator in favour of runtime config (simpler, less duplicated code) * Use the non-blocking migrator unless the blocking migrator is required. In the store tests we need the blocking migrator because some tests make asserts about the state of the DB after the migration has run. * Rename `validators_keypairs` -> `validator_keypairs` in the `BeaconChainHarness` ## Testing To confirm that the fix worked, I wrote a test using [Hiatus](https://crates.io/crates/hiatus), which can be found here: https://github.com/michaelsproul/lighthouse/tree/hiatus-issue-1557 That test can't be merged because it inserts random breakpoints everywhere, but if you check out that branch you can run the test with: ``` $ cd beacon_node/beacon_chain $ cargo test --release --test parallel_tests --features test_logger ``` It should pass, and the log output should show: ``` WARN Pruning deferred because of a concurrent mutation, message: this is expected only very rarely! ``` ## Additional Info This is a backwards-compatible change with no impact on consensus.
164 lines
5.2 KiB
Rust
164 lines
5.2 KiB
Rust
#[macro_use]
|
|
extern crate clap;
|
|
|
|
mod cli;
|
|
mod config;
|
|
|
|
pub use beacon_chain;
|
|
pub use cli::cli_app;
|
|
pub use client::{Client, ClientBuilder, ClientConfig, ClientGenesis};
|
|
pub use config::{get_config, get_data_dir, get_eth2_testnet_config, set_network_config};
|
|
pub use eth2_config::Eth2Config;
|
|
|
|
use beacon_chain::events::TeeEventHandler;
|
|
use beacon_chain::store::LevelDB;
|
|
use beacon_chain::{
|
|
builder::Witness, eth1_chain::CachingEth1Backend, slot_clock::SystemTimeSlotClock,
|
|
};
|
|
use clap::ArgMatches;
|
|
use environment::RuntimeContext;
|
|
use slog::{info, warn};
|
|
use std::ops::{Deref, DerefMut};
|
|
use types::EthSpec;
|
|
|
|
/// A type-alias to the tighten the definition of a production-intended `Client`.
|
|
pub type ProductionClient<E> = Client<
|
|
Witness<
|
|
SystemTimeSlotClock,
|
|
CachingEth1Backend<E>,
|
|
E,
|
|
TeeEventHandler<E>,
|
|
LevelDB<E>,
|
|
LevelDB<E>,
|
|
>,
|
|
>;
|
|
|
|
/// The beacon node `Client` that will be used in production.
|
|
///
|
|
/// Generic over some `EthSpec`.
|
|
///
|
|
/// ## Notes:
|
|
///
|
|
/// Despite being titled `Production...`, this code is not ready for production. The name
|
|
/// demonstrates an intention, not a promise.
|
|
pub struct ProductionBeaconNode<E: EthSpec>(ProductionClient<E>);
|
|
|
|
impl<E: EthSpec> ProductionBeaconNode<E> {
|
|
/// Starts a new beacon node `Client` in the given `environment`.
|
|
///
|
|
/// Identical to `start_from_client_config`, however the `client_config` is generated from the
|
|
/// given `matches` and potentially configuration files on the local filesystem or other
|
|
/// configurations hosted remotely.
|
|
pub async fn new_from_cli(
|
|
context: RuntimeContext<E>,
|
|
matches: ArgMatches<'static>,
|
|
) -> Result<Self, String> {
|
|
let client_config = get_config::<E>(
|
|
&matches,
|
|
&context.eth2_config.spec_constants,
|
|
&context.eth2_config().spec,
|
|
context.log().clone(),
|
|
)?;
|
|
Self::new(context, client_config).await
|
|
}
|
|
|
|
/// Starts a new beacon node `Client` in the given `environment`.
|
|
///
|
|
/// Client behaviour is defined by the given `client_config`.
|
|
pub async fn new(
|
|
context: RuntimeContext<E>,
|
|
mut client_config: ClientConfig,
|
|
) -> Result<Self, String> {
|
|
let spec = context.eth2_config().spec.clone();
|
|
let client_config_1 = client_config.clone();
|
|
let client_genesis = client_config.genesis.clone();
|
|
let store_config = client_config.store.clone();
|
|
let log = context.log().clone();
|
|
|
|
let db_path = client_config.create_db_path()?;
|
|
let freezer_db_path_res = client_config.create_freezer_db_path();
|
|
|
|
let executor = context.executor.clone();
|
|
|
|
let builder = ClientBuilder::new(context.eth_spec_instance.clone())
|
|
.runtime_context(context)
|
|
.chain_spec(spec)
|
|
.disk_store(&db_path, &freezer_db_path_res?, store_config)?;
|
|
|
|
let builder = builder
|
|
.beacon_chain_builder(client_genesis, client_config_1)
|
|
.await?;
|
|
let builder = if client_config.sync_eth1_chain && !client_config.dummy_eth1_backend {
|
|
info!(
|
|
log,
|
|
"Block production enabled";
|
|
"endpoint" => &client_config.eth1.endpoint,
|
|
"method" => "json rpc via http"
|
|
);
|
|
builder
|
|
.caching_eth1_backend(client_config.eth1.clone())
|
|
.await?
|
|
} else if client_config.dummy_eth1_backend {
|
|
warn!(
|
|
log,
|
|
"Block production impaired";
|
|
"reason" => "dummy eth1 backend is enabled"
|
|
);
|
|
builder.dummy_eth1_backend()?
|
|
} else {
|
|
info!(
|
|
log,
|
|
"Block production disabled";
|
|
"reason" => "no eth1 backend configured"
|
|
);
|
|
builder.no_eth1_backend()?
|
|
};
|
|
|
|
let (builder, _events) = builder
|
|
.system_time_slot_clock()?
|
|
.tee_event_handler(client_config.websocket_server.clone())?;
|
|
|
|
// Inject the executor into the discv5 network config.
|
|
let discv5_executor = Discv5Executor(executor);
|
|
client_config.network.discv5_config.executor = Some(Box::new(discv5_executor));
|
|
|
|
builder
|
|
.build_beacon_chain()?
|
|
.network(&client_config.network)
|
|
.await?
|
|
.notifier()?
|
|
.http_api_config(client_config.http_api.clone())
|
|
.http_metrics_config(client_config.http_metrics.clone())
|
|
.build()
|
|
.map(Self)
|
|
}
|
|
|
|
pub fn into_inner(self) -> ProductionClient<E> {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl<E: EthSpec> Deref for ProductionBeaconNode<E> {
|
|
type Target = ProductionClient<E>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<E: EthSpec> DerefMut for ProductionBeaconNode<E> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.0
|
|
}
|
|
}
|
|
|
|
// Implements the Discv5 Executor trait over our global executor
|
|
#[derive(Clone)]
|
|
struct Discv5Executor(task_executor::TaskExecutor);
|
|
|
|
impl eth2_libp2p::discv5::Executor for Discv5Executor {
|
|
fn spawn(&self, future: std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>) {
|
|
self.0.spawn(future, "discv5")
|
|
}
|
|
}
|