mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 08:52:54 +00:00
Merge interop branch
This commit is contained in:
@@ -6,11 +6,14 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
eth2_config = { path = "../eth2/utils/eth2_config" }
|
||||
lighthouse_bootstrap = { path = "../eth2/utils/lighthouse_bootstrap" }
|
||||
beacon_chain = { path = "beacon_chain" }
|
||||
types = { path = "../eth2/types" }
|
||||
store = { path = "./store" }
|
||||
client = { path = "client" }
|
||||
version = { path = "version" }
|
||||
clap = "2.32.0"
|
||||
rand = "0.7"
|
||||
slog = { version = "^2.2.3" , features = ["max_level_trace", "release_max_level_trace"] }
|
||||
slog-term = "^2.4.0"
|
||||
slog-async = "^2.3.0"
|
||||
|
||||
@@ -5,18 +5,23 @@ authors = ["Paul Hauner <paul@paulhauner.com>", "Age Manning <Age@AgeManning.com
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
eth2_config = { path = "../../eth2/utils/eth2_config" }
|
||||
merkle_proof = { path = "../../eth2/utils/merkle_proof" }
|
||||
store = { path = "../store" }
|
||||
parking_lot = "0.7"
|
||||
lazy_static = "1.3.0"
|
||||
lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
|
||||
lighthouse_bootstrap = { path = "../../eth2/utils/lighthouse_bootstrap" }
|
||||
log = "0.4"
|
||||
operation_pool = { path = "../../eth2/operation_pool" }
|
||||
rayon = "1.0"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_yaml = "0.8"
|
||||
slog = { version = "^2.2.3" , features = ["max_level_trace"] }
|
||||
sloggers = { version = "^0.3" }
|
||||
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
||||
eth2_hashing = { path = "../../eth2/utils/eth2_hashing" }
|
||||
eth2_ssz = "0.1"
|
||||
eth2_ssz_derive = "0.1"
|
||||
state_processing = { path = "../../eth2/state_processing" }
|
||||
|
||||
@@ -5,7 +5,6 @@ use crate::iter::{ReverseBlockRootIterator, ReverseStateRootIterator};
|
||||
use crate::metrics;
|
||||
use crate::persisted_beacon_chain::{PersistedBeaconChain, BEACON_CHAIN_DB_KEY};
|
||||
use lmd_ghost::LmdGhost;
|
||||
use log::trace;
|
||||
use operation_pool::DepositInsertStatus;
|
||||
use operation_pool::{OperationPool, PersistedOperationPool};
|
||||
use parking_lot::{RwLock, RwLockReadGuard};
|
||||
@@ -22,6 +21,7 @@ use state_processing::{
|
||||
per_block_processing, per_slot_processing, BlockProcessingError, BlockSignatureStrategy,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use store::iter::{BlockRootsIterator, StateRootsIterator};
|
||||
use store::{Error as DBError, Store};
|
||||
use tree_hash::TreeHash;
|
||||
@@ -76,6 +76,29 @@ pub enum AttestationProcessingOutcome {
|
||||
Invalid(AttestationValidationError),
|
||||
}
|
||||
|
||||
pub enum StateCow<'a, T: EthSpec> {
|
||||
Borrowed(RwLockReadGuard<'a, CheckPoint<T>>),
|
||||
Owned(BeaconState<T>),
|
||||
}
|
||||
|
||||
impl<'a, T: EthSpec> AsRef<BeaconState<T>> for StateCow<'a, T> {
|
||||
fn as_ref(&self) -> &BeaconState<T> {
|
||||
match self {
|
||||
StateCow::Borrowed(checkpoint) => &checkpoint.beacon_state,
|
||||
StateCow::Owned(state) => &state,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: EthSpec> StateCow<'a, T> {
|
||||
pub fn as_mut_ref(&mut self) -> Option<&mut BeaconState<T>> {
|
||||
match self {
|
||||
StateCow::Borrowed(_) => None,
|
||||
StateCow::Owned(ref mut state) => Some(state),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BeaconChainTypes: Send + Sync + 'static {
|
||||
type Store: store::Store;
|
||||
type SlotClock: slot_clock::SlotClock;
|
||||
@@ -96,10 +119,6 @@ pub struct BeaconChain<T: BeaconChainTypes> {
|
||||
pub op_pool: OperationPool<T::EthSpec>,
|
||||
/// Stores a "snapshot" of the chain at the time the head-of-the-chain block was received.
|
||||
canonical_head: RwLock<CheckPoint<T::EthSpec>>,
|
||||
/// The same state from `self.canonical_head`, but updated at the start of each slot with a
|
||||
/// skip slot if no block is received. This is effectively a cache that avoids repeating calls
|
||||
/// to `per_slot_processing`.
|
||||
state: RwLock<BeaconState<T::EthSpec>>,
|
||||
/// The root of the genesis block.
|
||||
pub genesis_block_root: Hash256,
|
||||
/// A state-machine that is updated with information from the network and chooses a canonical
|
||||
@@ -113,7 +132,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
/// Instantiate a new Beacon Chain, from genesis.
|
||||
pub fn from_genesis(
|
||||
store: Arc<T::Store>,
|
||||
slot_clock: T::SlotClock,
|
||||
mut genesis_state: BeaconState<T::EthSpec>,
|
||||
mut genesis_block: BeaconBlock<T::EthSpec>,
|
||||
spec: ChainSpec,
|
||||
@@ -140,17 +158,24 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
genesis_state_root,
|
||||
));
|
||||
|
||||
info!(log, "BeaconChain init";
|
||||
"genesis_validator_count" => genesis_state.validators.len(),
|
||||
"genesis_state_root" => format!("{}", genesis_state_root),
|
||||
"genesis_block_root" => format!("{}", genesis_block_root),
|
||||
// Slot clock
|
||||
let slot_clock = T::SlotClock::from_eth2_genesis(
|
||||
spec.genesis_slot,
|
||||
genesis_state.genesis_time,
|
||||
Duration::from_millis(spec.milliseconds_per_slot),
|
||||
)
|
||||
.ok_or_else(|| Error::SlotClockDidNotStart)?;
|
||||
|
||||
info!(log, "Beacon chain initialized from genesis";
|
||||
"validator_count" => genesis_state.validators.len(),
|
||||
"state_root" => format!("{}", genesis_state_root),
|
||||
"block_root" => format!("{}", genesis_block_root),
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
spec,
|
||||
slot_clock,
|
||||
op_pool: OperationPool::new(),
|
||||
state: RwLock::new(genesis_state),
|
||||
canonical_head,
|
||||
genesis_block_root,
|
||||
fork_choice: ForkChoice::new(store.clone(), &genesis_block, genesis_block_root),
|
||||
@@ -172,16 +197,26 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
Ok(Some(p)) => p,
|
||||
};
|
||||
|
||||
let slot_clock = T::SlotClock::new(
|
||||
let state = &p.canonical_head.beacon_state;
|
||||
|
||||
let slot_clock = T::SlotClock::from_eth2_genesis(
|
||||
spec.genesis_slot,
|
||||
p.state.genesis_time,
|
||||
spec.seconds_per_slot,
|
||||
);
|
||||
state.genesis_time,
|
||||
Duration::from_millis(spec.milliseconds_per_slot),
|
||||
)
|
||||
.ok_or_else(|| Error::SlotClockDidNotStart)?;
|
||||
|
||||
let last_finalized_root = p.canonical_head.beacon_state.finalized_checkpoint.root;
|
||||
let last_finalized_block = &p.canonical_head.beacon_block;
|
||||
|
||||
let op_pool = p.op_pool.into_operation_pool(&p.state, &spec);
|
||||
let op_pool = p.op_pool.into_operation_pool(state, &spec);
|
||||
|
||||
info!(log, "Beacon chain initialized from store";
|
||||
"head_root" => format!("{}", p.canonical_head.beacon_block_root),
|
||||
"head_epoch" => format!("{}", p.canonical_head.beacon_block.slot.epoch(T::EthSpec::slots_per_epoch())),
|
||||
"finalized_root" => format!("{}", last_finalized_root),
|
||||
"finalized_epoch" => format!("{}", last_finalized_block.slot.epoch(T::EthSpec::slots_per_epoch())),
|
||||
);
|
||||
|
||||
Ok(Some(BeaconChain {
|
||||
spec,
|
||||
@@ -189,7 +224,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
fork_choice: ForkChoice::new(store.clone(), last_finalized_block, last_finalized_root),
|
||||
op_pool,
|
||||
canonical_head: RwLock::new(p.canonical_head),
|
||||
state: RwLock::new(p.state),
|
||||
genesis_block_root: p.genesis_block_root,
|
||||
store,
|
||||
log,
|
||||
@@ -204,7 +238,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
canonical_head: self.canonical_head.read().clone(),
|
||||
op_pool: PersistedOperationPool::from_operation_pool(&self.op_pool),
|
||||
genesis_block_root: self.genesis_block_root,
|
||||
state: self.state.read().clone(),
|
||||
};
|
||||
|
||||
let key = Hash256::from_slice(&BEACON_CHAIN_DB_KEY.as_bytes());
|
||||
@@ -215,6 +248,25 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the slot _right now_ according to `self.slot_clock`. Returns `Err` if the slot is
|
||||
/// unavailable.
|
||||
///
|
||||
/// The slot might be unavailable due to an error with the system clock, or if the present time
|
||||
/// is before genesis (i.e., a negative slot).
|
||||
pub fn slot(&self) -> Result<Slot, Error> {
|
||||
self.slot_clock.now().ok_or_else(|| Error::UnableToReadSlot)
|
||||
}
|
||||
|
||||
/// Returns the epoch _right now_ according to `self.slot_clock`. Returns `Err` if the epoch is
|
||||
/// unavailable.
|
||||
///
|
||||
/// The epoch might be unavailable due to an error with the system clock, or if the present time
|
||||
/// is before genesis (i.e., a negative epoch).
|
||||
pub fn epoch(&self) -> Result<Epoch, Error> {
|
||||
self.slot()
|
||||
.map(|slot| slot.epoch(T::EthSpec::slots_per_epoch()))
|
||||
}
|
||||
|
||||
/// Returns the beacon block body for each beacon block root in `roots`.
|
||||
///
|
||||
/// Fails if any root in `roots` does not have a corresponding block.
|
||||
@@ -300,12 +352,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
Ok(self.store.get(block_root)?)
|
||||
}
|
||||
|
||||
/// Returns a read-lock guarded `BeaconState` which is the `canonical_head` that has been
|
||||
/// updated to match the current slot clock.
|
||||
pub fn speculative_state(&self) -> Result<RwLockReadGuard<BeaconState<T::EthSpec>>, Error> {
|
||||
Ok(self.state.read())
|
||||
}
|
||||
|
||||
/// Returns a read-lock guarded `CheckPoint` struct for reading the head (as chosen by the
|
||||
/// fork-choice rule).
|
||||
///
|
||||
@@ -316,46 +362,74 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
self.canonical_head.read()
|
||||
}
|
||||
|
||||
/// Returns the `BeaconState` at the given slot.
|
||||
///
|
||||
/// May return:
|
||||
///
|
||||
/// - A new state loaded from the database (for states prior to the head)
|
||||
/// - A reference to the head state (note: this keeps a read lock on the head, try to use
|
||||
/// sparingly).
|
||||
/// - The head state, but with skipped slots (for states later than the head).
|
||||
///
|
||||
/// Returns `None` when the state is not found in the database or there is an error skipping
|
||||
/// to a future state.
|
||||
pub fn state_at_slot(&self, slot: Slot) -> Result<StateCow<T::EthSpec>, Error> {
|
||||
let head_state = &self.head().beacon_state;
|
||||
|
||||
if slot == head_state.slot {
|
||||
Ok(StateCow::Borrowed(self.head()))
|
||||
} else if slot > head_state.slot {
|
||||
let head_state_slot = head_state.slot;
|
||||
let mut state = head_state.clone();
|
||||
drop(head_state);
|
||||
while state.slot < slot {
|
||||
match per_slot_processing(&mut state, &self.spec) {
|
||||
Ok(()) => (),
|
||||
Err(e) => {
|
||||
warn!(
|
||||
self.log,
|
||||
"Unable to load state at slot";
|
||||
"error" => format!("{:?}", e),
|
||||
"head_slot" => head_state_slot,
|
||||
"requested_slot" => slot
|
||||
);
|
||||
return Err(Error::NoStateForSlot(slot));
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(StateCow::Owned(state))
|
||||
} else {
|
||||
let state_root = self
|
||||
.rev_iter_state_roots()
|
||||
.find(|(_root, s)| *s == slot)
|
||||
.map(|(root, _slot)| root)
|
||||
.ok_or_else(|| Error::NoStateForSlot(slot))?;
|
||||
|
||||
Ok(StateCow::Owned(
|
||||
self.store
|
||||
.get(&state_root)?
|
||||
.ok_or_else(|| Error::NoStateForSlot(slot))?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `BeaconState` the current slot (viz., `self.slot()`).
|
||||
///
|
||||
/// - A reference to the head state (note: this keeps a read lock on the head, try to use
|
||||
/// sparingly).
|
||||
/// - The head state, but with skipped slots (for states later than the head).
|
||||
///
|
||||
/// Returns `None` when there is an error skipping to a future state or the slot clock cannot
|
||||
/// be read.
|
||||
pub fn state_now(&self) -> Result<StateCow<T::EthSpec>, Error> {
|
||||
self.state_at_slot(self.slot()?)
|
||||
}
|
||||
|
||||
/// Returns the slot of the highest block in the canonical chain.
|
||||
pub fn best_slot(&self) -> Slot {
|
||||
self.canonical_head.read().beacon_block.slot
|
||||
}
|
||||
|
||||
/// Ensures the current canonical `BeaconState` has been transitioned to match the `slot_clock`.
|
||||
pub fn catchup_state(&self) -> Result<(), Error> {
|
||||
let spec = &self.spec;
|
||||
|
||||
let present_slot = match self.slot_clock.present_slot() {
|
||||
Ok(Some(slot)) => slot,
|
||||
_ => return Err(Error::UnableToReadSlot),
|
||||
};
|
||||
|
||||
if self.state.read().slot < present_slot {
|
||||
let mut state = self.state.write();
|
||||
|
||||
// If required, transition the new state to the present slot.
|
||||
for _ in state.slot.as_u64()..present_slot.as_u64() {
|
||||
// Ensure the next epoch state caches are built in case of an epoch transition.
|
||||
state.build_committee_cache(RelativeEpoch::Next, spec)?;
|
||||
|
||||
per_slot_processing(&mut *state, spec)?;
|
||||
}
|
||||
|
||||
state.build_all_caches(spec)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build all of the caches on the current state.
|
||||
///
|
||||
/// Ideally this shouldn't be required, however we leave it here for testing.
|
||||
pub fn ensure_state_caches_are_built(&self) -> Result<(), Error> {
|
||||
self.state.write().build_all_caches(&self.spec)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the validator index (if any) for the given public key.
|
||||
///
|
||||
/// Information is retrieved from the present `beacon_state.validators`.
|
||||
@@ -368,26 +442,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Reads the slot clock, returns `None` if the slot is unavailable.
|
||||
///
|
||||
/// The slot might be unavailable due to an error with the system clock, or if the present time
|
||||
/// is before genesis (i.e., a negative slot).
|
||||
///
|
||||
/// This is distinct to `present_slot`, which simply reads the latest state. If a
|
||||
/// call to `read_slot_clock` results in a higher slot than a call to `present_slot`,
|
||||
/// `self.state` should undergo per slot processing.
|
||||
pub fn read_slot_clock(&self) -> Option<Slot> {
|
||||
match self.slot_clock.present_slot() {
|
||||
Ok(Some(some_slot)) => Some(some_slot),
|
||||
Ok(None) => None,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the slot clock (see `self.read_slot_clock()` and returns the number of slots since
|
||||
/// genesis.
|
||||
pub fn slots_since_genesis(&self) -> Option<SlotHeight> {
|
||||
let now = self.read_slot_clock()?;
|
||||
let now = self.slot().ok()?;
|
||||
let genesis_slot = self.spec.genesis_slot;
|
||||
|
||||
if now < genesis_slot {
|
||||
@@ -397,32 +455,36 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns slot of the present state.
|
||||
///
|
||||
/// This is distinct to `read_slot_clock`, which reads from the actual system clock. If
|
||||
/// `self.state` has not been transitioned it is possible for the system clock to be on a
|
||||
/// different slot to what is returned from this call.
|
||||
pub fn present_slot(&self) -> Slot {
|
||||
self.state.read().slot
|
||||
}
|
||||
|
||||
/// Returns the block proposer for a given slot.
|
||||
///
|
||||
/// Information is read from the present `beacon_state` shuffling, only information from the
|
||||
/// present epoch is available.
|
||||
pub fn block_proposer(&self, slot: Slot) -> Result<usize, Error> {
|
||||
// Ensures that the present state has been advanced to the present slot, skipping slots if
|
||||
// blocks are not present.
|
||||
self.catchup_state()?;
|
||||
let epoch = |slot: Slot| slot.epoch(T::EthSpec::slots_per_epoch());
|
||||
let head_state = &self.head().beacon_state;
|
||||
|
||||
// TODO: permit lookups of the proposer at any slot.
|
||||
let index = self.state.read().get_beacon_proposer_index(
|
||||
slot,
|
||||
RelativeEpoch::Current,
|
||||
&self.spec,
|
||||
)?;
|
||||
let mut state = if epoch(slot) == epoch(head_state.slot) {
|
||||
StateCow::Borrowed(self.head())
|
||||
} else {
|
||||
self.state_at_slot(slot)?
|
||||
};
|
||||
|
||||
Ok(index)
|
||||
if let Some(state) = state.as_mut_ref() {
|
||||
state.build_committee_cache(RelativeEpoch::Current, &self.spec)?;
|
||||
}
|
||||
|
||||
if epoch(state.as_ref().slot) != epoch(slot) {
|
||||
return Err(Error::InvariantViolated(format!(
|
||||
"Epochs in consistent in proposer lookup: state: {}, requested: {}",
|
||||
epoch(state.as_ref().slot),
|
||||
epoch(slot)
|
||||
)));
|
||||
}
|
||||
|
||||
state
|
||||
.as_ref()
|
||||
.get_beacon_proposer_index(slot, RelativeEpoch::Current, &self.spec)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Returns the attestation slot and shard for a given validator index.
|
||||
@@ -432,14 +494,31 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
pub fn validator_attestation_slot_and_shard(
|
||||
&self,
|
||||
validator_index: usize,
|
||||
) -> Result<Option<(Slot, u64)>, BeaconStateError> {
|
||||
trace!(
|
||||
"BeaconChain::validator_attestation_slot_and_shard: validator_index: {}",
|
||||
validator_index
|
||||
);
|
||||
if let Some(attestation_duty) = self
|
||||
.state
|
||||
.read()
|
||||
epoch: Epoch,
|
||||
) -> Result<Option<(Slot, u64)>, Error> {
|
||||
let as_epoch = |slot: Slot| slot.epoch(T::EthSpec::slots_per_epoch());
|
||||
let head_state = &self.head().beacon_state;
|
||||
|
||||
let mut state = if epoch == as_epoch(head_state.slot) {
|
||||
StateCow::Borrowed(self.head())
|
||||
} else {
|
||||
self.state_at_slot(epoch.start_slot(T::EthSpec::slots_per_epoch()))?
|
||||
};
|
||||
|
||||
if let Some(state) = state.as_mut_ref() {
|
||||
state.build_committee_cache(RelativeEpoch::Current, &self.spec)?;
|
||||
}
|
||||
|
||||
if as_epoch(state.as_ref().slot) != epoch {
|
||||
return Err(Error::InvariantViolated(format!(
|
||||
"Epochs in consistent in attestation duties lookup: state: {}, requested: {}",
|
||||
as_epoch(state.as_ref().slot),
|
||||
epoch
|
||||
)));
|
||||
}
|
||||
|
||||
if let Some(attestation_duty) = state
|
||||
.as_ref()
|
||||
.get_attestation_duties(validator_index, RelativeEpoch::Current)?
|
||||
{
|
||||
Ok(Some((attestation_duty.slot, attestation_duty.shard)))
|
||||
@@ -448,15 +527,25 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
|
||||
/// Produce an `AttestationData` that is valid for the given `slot` `shard`.
|
||||
///
|
||||
/// Attests to the canonical chain.
|
||||
pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> {
|
||||
let state = self.state.read();
|
||||
/// Always attests to the canonical chain.
|
||||
pub fn produce_attestation_data(
|
||||
&self,
|
||||
shard: u64,
|
||||
slot: Slot,
|
||||
) -> Result<AttestationData, Error> {
|
||||
let state = self.state_at_slot(slot)?;
|
||||
|
||||
let head_block_root = self.head().beacon_block_root;
|
||||
let head_block_slot = self.head().beacon_block.slot;
|
||||
|
||||
self.produce_attestation_data_for_block(shard, head_block_root, head_block_slot, &*state)
|
||||
self.produce_attestation_data_for_block(
|
||||
shard,
|
||||
head_block_root,
|
||||
head_block_slot,
|
||||
state.as_ref(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Produce an `AttestationData` that attests to the chain denoted by `block_root` and `state`.
|
||||
@@ -738,8 +827,19 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
} else {
|
||||
// Provide the attestation to fork choice, updating the validator latest messages but
|
||||
// _without_ finding and updating the head.
|
||||
self.fork_choice
|
||||
.process_attestation(&state, &attestation, block)?;
|
||||
if let Err(e) = self
|
||||
.fork_choice
|
||||
.process_attestation(&state, &attestation, block)
|
||||
{
|
||||
error!(
|
||||
self.log,
|
||||
"Add attestation to fork choice failed";
|
||||
"fork_choice_integrity" => format!("{:?}", self.fork_choice.verify_integrity()),
|
||||
"beacon_block_root" => format!("{}", attestation.data.beacon_block_root),
|
||||
"error" => format!("{:?}", e)
|
||||
);
|
||||
return Err(e.into());
|
||||
}
|
||||
|
||||
// Provide the valid attestation to op pool, which may choose to retain the
|
||||
// attestation for inclusion in a future block.
|
||||
@@ -764,14 +864,38 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
|
||||
/// Accept some exit and queue it for inclusion in an appropriate block.
|
||||
pub fn process_voluntary_exit(&self, exit: VoluntaryExit) -> Result<(), ExitValidationError> {
|
||||
self.op_pool
|
||||
.insert_voluntary_exit(exit, &*self.state.read(), &self.spec)
|
||||
match self.state_now() {
|
||||
Ok(state) => self
|
||||
.op_pool
|
||||
.insert_voluntary_exit(exit, state.as_ref(), &self.spec),
|
||||
Err(e) => {
|
||||
error!(
|
||||
&self.log,
|
||||
"Unable to process voluntary exit";
|
||||
"error" => format!("{:?}", e),
|
||||
"reason" => "no state"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept some transfer and queue it for inclusion in an appropriate block.
|
||||
pub fn process_transfer(&self, transfer: Transfer) -> Result<(), TransferValidationError> {
|
||||
self.op_pool
|
||||
.insert_transfer(transfer, &*self.state.read(), &self.spec)
|
||||
match self.state_now() {
|
||||
Ok(state) => self
|
||||
.op_pool
|
||||
.insert_transfer(transfer, state.as_ref(), &self.spec),
|
||||
Err(e) => {
|
||||
error!(
|
||||
&self.log,
|
||||
"Unable to process transfer";
|
||||
"error" => format!("{:?}", e),
|
||||
"reason" => "no state"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept some proposer slashing and queue it for inclusion in an appropriate block.
|
||||
@@ -779,8 +903,21 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
&self,
|
||||
proposer_slashing: ProposerSlashing,
|
||||
) -> Result<(), ProposerSlashingValidationError> {
|
||||
self.op_pool
|
||||
.insert_proposer_slashing(proposer_slashing, &*self.state.read(), &self.spec)
|
||||
match self.state_now() {
|
||||
Ok(state) => {
|
||||
self.op_pool
|
||||
.insert_proposer_slashing(proposer_slashing, state.as_ref(), &self.spec)
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
&self.log,
|
||||
"Unable to process proposer slashing";
|
||||
"error" => format!("{:?}", e),
|
||||
"reason" => "no state"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept some attester slashing and queue it for inclusion in an appropriate block.
|
||||
@@ -788,8 +925,21 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
&self,
|
||||
attester_slashing: AttesterSlashing<T::EthSpec>,
|
||||
) -> Result<(), AttesterSlashingValidationError> {
|
||||
self.op_pool
|
||||
.insert_attester_slashing(attester_slashing, &*self.state.read(), &self.spec)
|
||||
match self.state_now() {
|
||||
Ok(state) => {
|
||||
self.op_pool
|
||||
.insert_attester_slashing(attester_slashing, state.as_ref(), &self.spec)
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
&self.log,
|
||||
"Unable to process attester slashing";
|
||||
"error" => format!("{:?}", e),
|
||||
"reason" => "no state"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept some block and attempt to add it to block DAG.
|
||||
@@ -803,8 +953,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
let full_timer = metrics::start_timer(&metrics::BLOCK_PROCESSING_TIMES);
|
||||
|
||||
let finalized_slot = self
|
||||
.state
|
||||
.read()
|
||||
.head()
|
||||
.beacon_state
|
||||
.finalized_checkpoint
|
||||
.epoch
|
||||
.start_slot(T::EthSpec::slots_per_epoch());
|
||||
@@ -827,9 +977,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
return Ok(BlockProcessingOutcome::GenesisBlock);
|
||||
}
|
||||
|
||||
let present_slot = self
|
||||
.read_slot_clock()
|
||||
.ok_or_else(|| Error::UnableToReadSlot)?;
|
||||
let present_slot = self.slot()?;
|
||||
|
||||
if block.slot > present_slot {
|
||||
return Ok(BlockProcessingOutcome::FutureSlot {
|
||||
@@ -952,10 +1100,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
if let Err(e) = self.fork_choice.process_block(&state, &block, block_root) {
|
||||
error!(
|
||||
self.log,
|
||||
"fork choice failed to process_block";
|
||||
"error" => format!("{:?}", e),
|
||||
"Add block to fork choice failed";
|
||||
"fork_choice_integrity" => format!("{:?}", self.fork_choice.verify_integrity()),
|
||||
"block_root" => format!("{}", block_root),
|
||||
"block_slot" => format!("{}", block.slot)
|
||||
"error" => format!("{:?}", e),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -988,20 +1136,20 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
Ok(BlockProcessingOutcome::Processed { block_root })
|
||||
}
|
||||
|
||||
/// Produce a new block at the present slot.
|
||||
/// Produce a new block at the given `slot`.
|
||||
///
|
||||
/// The produced block will not be inherently valid, it must be signed by a block producer.
|
||||
/// Block signing is out of the scope of this function and should be done by a separate program.
|
||||
pub fn produce_block(
|
||||
&self,
|
||||
randao_reveal: Signature,
|
||||
slot: Slot,
|
||||
) -> Result<(BeaconBlock<T::EthSpec>, BeaconState<T::EthSpec>), BlockProductionError> {
|
||||
let state = self.state.read().clone();
|
||||
let slot = self
|
||||
.read_slot_clock()
|
||||
.ok_or_else(|| BlockProductionError::UnableToReadSlot)?;
|
||||
let state = self
|
||||
.state_at_slot(slot - 1)
|
||||
.map_err(|_| BlockProductionError::UnableToProduceAtSlot(slot))?;
|
||||
|
||||
self.produce_block_on_state(state, slot, randao_reveal)
|
||||
self.produce_block_on_state(state.as_ref().clone(), slot, randao_reveal)
|
||||
}
|
||||
|
||||
/// Produce a block for some `slot` upon the given `state`.
|
||||
@@ -1170,32 +1318,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
}
|
||||
|
||||
/// Update the canonical head to `new_head`.
|
||||
fn update_canonical_head(&self, new_head: CheckPoint<T::EthSpec>) -> Result<(), Error> {
|
||||
fn update_canonical_head(&self, mut new_head: CheckPoint<T::EthSpec>) -> Result<(), Error> {
|
||||
let timer = metrics::start_timer(&metrics::UPDATE_HEAD_TIMES);
|
||||
|
||||
new_head.beacon_state.build_all_caches(&self.spec)?;
|
||||
|
||||
// Update the checkpoint that stores the head of the chain at the time it received the
|
||||
// block.
|
||||
*self.canonical_head.write() = new_head;
|
||||
|
||||
// Update the always-at-the-present-slot state we keep around for performance gains.
|
||||
*self.state.write() = {
|
||||
let mut state = self.canonical_head.read().beacon_state.clone();
|
||||
|
||||
let present_slot = match self.slot_clock.present_slot() {
|
||||
Ok(Some(slot)) => slot,
|
||||
_ => return Err(Error::UnableToReadSlot),
|
||||
};
|
||||
|
||||
// If required, transition the new state to the present slot.
|
||||
for _ in state.slot.as_u64()..present_slot.as_u64() {
|
||||
per_slot_processing(&mut state, &self.spec)?;
|
||||
}
|
||||
|
||||
state.build_all_caches(&self.spec)?;
|
||||
|
||||
state
|
||||
};
|
||||
|
||||
// Save `self` to `self.store`.
|
||||
self.persist()?;
|
||||
|
||||
|
||||
296
beacon_node/beacon_chain/src/beacon_chain_builder.rs
Normal file
296
beacon_node/beacon_chain/src/beacon_chain_builder.rs
Normal file
@@ -0,0 +1,296 @@
|
||||
use crate::{BeaconChain, BeaconChainTypes};
|
||||
use eth2_hashing::hash;
|
||||
use lighthouse_bootstrap::Bootstrapper;
|
||||
use merkle_proof::MerkleTree;
|
||||
use rayon::prelude::*;
|
||||
use slog::Logger;
|
||||
use ssz::Encode;
|
||||
use state_processing::initialize_beacon_state_from_eth1;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
use tree_hash::{SignedRoot, TreeHash};
|
||||
use types::{
|
||||
test_utils::generate_deterministic_keypairs, BeaconBlock, BeaconState, ChainSpec, Deposit,
|
||||
DepositData, Domain, EthSpec, Fork, Hash256, PublicKey, Signature,
|
||||
};
|
||||
|
||||
enum BuildStrategy<T: BeaconChainTypes> {
|
||||
FromGenesis {
|
||||
genesis_state: Box<BeaconState<T::EthSpec>>,
|
||||
genesis_block: Box<BeaconBlock<T::EthSpec>>,
|
||||
},
|
||||
LoadFromStore,
|
||||
}
|
||||
|
||||
pub struct BeaconChainBuilder<T: BeaconChainTypes> {
|
||||
build_strategy: BuildStrategy<T>,
|
||||
spec: ChainSpec,
|
||||
log: Logger,
|
||||
}
|
||||
|
||||
impl<T: BeaconChainTypes> BeaconChainBuilder<T> {
|
||||
pub fn recent_genesis(
|
||||
validator_count: usize,
|
||||
minutes: u64,
|
||||
spec: ChainSpec,
|
||||
log: Logger,
|
||||
) -> Result<Self, String> {
|
||||
Self::quick_start(recent_genesis_time(minutes), validator_count, spec, log)
|
||||
}
|
||||
|
||||
pub fn quick_start(
|
||||
genesis_time: u64,
|
||||
validator_count: usize,
|
||||
spec: ChainSpec,
|
||||
log: Logger,
|
||||
) -> Result<Self, String> {
|
||||
let genesis_state = interop_genesis_state(validator_count, genesis_time, &spec)?;
|
||||
|
||||
Ok(Self::from_genesis_state(genesis_state, spec, log))
|
||||
}
|
||||
|
||||
pub fn yaml_state(file: &PathBuf, spec: ChainSpec, log: Logger) -> Result<Self, String> {
|
||||
let file = File::open(file.clone())
|
||||
.map_err(|e| format!("Unable to open YAML genesis state file {:?}: {:?}", file, e))?;
|
||||
|
||||
let genesis_state = serde_yaml::from_reader(file)
|
||||
.map_err(|e| format!("Unable to parse YAML genesis state file: {:?}", e))?;
|
||||
|
||||
Ok(Self::from_genesis_state(genesis_state, spec, log))
|
||||
}
|
||||
|
||||
pub fn http_bootstrap(server: &str, spec: ChainSpec, log: Logger) -> Result<Self, String> {
|
||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())
|
||||
.map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?;
|
||||
|
||||
let (genesis_state, genesis_block) = bootstrapper
|
||||
.genesis()
|
||||
.map_err(|e| format!("Failed to bootstrap genesis state: {}", e))?;
|
||||
|
||||
Ok(Self {
|
||||
build_strategy: BuildStrategy::FromGenesis {
|
||||
genesis_block: Box::new(genesis_block),
|
||||
genesis_state: Box::new(genesis_state),
|
||||
},
|
||||
spec,
|
||||
log,
|
||||
})
|
||||
}
|
||||
|
||||
fn from_genesis_state(
|
||||
genesis_state: BeaconState<T::EthSpec>,
|
||||
spec: ChainSpec,
|
||||
log: Logger,
|
||||
) -> Self {
|
||||
Self {
|
||||
build_strategy: BuildStrategy::FromGenesis {
|
||||
genesis_block: Box::new(genesis_block(&genesis_state, &spec)),
|
||||
genesis_state: Box::new(genesis_state),
|
||||
},
|
||||
spec,
|
||||
log,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_store(spec: ChainSpec, log: Logger) -> Self {
|
||||
Self {
|
||||
build_strategy: BuildStrategy::LoadFromStore,
|
||||
spec,
|
||||
log,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(self, store: Arc<T::Store>) -> Result<BeaconChain<T>, String> {
|
||||
Ok(match self.build_strategy {
|
||||
BuildStrategy::LoadFromStore => BeaconChain::from_store(store, self.spec, self.log)
|
||||
.map_err(|e| format!("Error loading BeaconChain from database: {:?}", e))?
|
||||
.ok_or_else(|| format!("Unable to find exising BeaconChain in database."))?,
|
||||
BuildStrategy::FromGenesis {
|
||||
genesis_block,
|
||||
genesis_state,
|
||||
} => BeaconChain::from_genesis(
|
||||
store,
|
||||
genesis_state.as_ref().clone(),
|
||||
genesis_block.as_ref().clone(),
|
||||
self.spec,
|
||||
self.log,
|
||||
)
|
||||
.map_err(|e| format!("Failed to initialize new beacon chain: {:?}", e))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn genesis_block<T: EthSpec>(genesis_state: &BeaconState<T>, spec: &ChainSpec) -> BeaconBlock<T> {
|
||||
let mut genesis_block = BeaconBlock::empty(&spec);
|
||||
|
||||
genesis_block.state_root = genesis_state.canonical_root();
|
||||
|
||||
genesis_block
|
||||
}
|
||||
|
||||
/// Builds a genesis state as defined by the Eth2 interop procedure (see below).
|
||||
///
|
||||
/// Reference:
|
||||
/// https://github.com/ethereum/eth2.0-pm/tree/6e41fcf383ebeb5125938850d8e9b4e9888389b4/interop/mocked_start
|
||||
fn interop_genesis_state<T: EthSpec>(
|
||||
validator_count: usize,
|
||||
genesis_time: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<BeaconState<T>, String> {
|
||||
let keypairs = generate_deterministic_keypairs(validator_count);
|
||||
let eth1_block_hash = Hash256::from_slice(&[42; 32]);
|
||||
let eth1_timestamp = 2_u64.pow(40);
|
||||
let amount = spec.max_effective_balance;
|
||||
|
||||
let withdrawal_credentials = |pubkey: &PublicKey| {
|
||||
let mut credentials = hash(&pubkey.as_ssz_bytes());
|
||||
credentials[0] = spec.bls_withdrawal_prefix_byte;
|
||||
Hash256::from_slice(&credentials)
|
||||
};
|
||||
|
||||
let datas = keypairs
|
||||
.into_par_iter()
|
||||
.map(|keypair| {
|
||||
let mut data = DepositData {
|
||||
withdrawal_credentials: withdrawal_credentials(&keypair.pk),
|
||||
pubkey: keypair.pk.into(),
|
||||
amount,
|
||||
signature: Signature::empty_signature().into(),
|
||||
};
|
||||
|
||||
let domain = spec.get_domain(
|
||||
spec.genesis_slot.epoch(T::slots_per_epoch()),
|
||||
Domain::Deposit,
|
||||
&Fork::default(),
|
||||
);
|
||||
data.signature = Signature::new(&data.signed_root()[..], domain, &keypair.sk).into();
|
||||
|
||||
data
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let deposit_root_leaves = datas
|
||||
.par_iter()
|
||||
.map(|data| Hash256::from_slice(&data.tree_hash_root()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut proofs = vec![];
|
||||
for i in 1..=deposit_root_leaves.len() {
|
||||
// Note: this implementation is not so efficient.
|
||||
//
|
||||
// If `MerkleTree` had a push method, we could just build one tree and sample it instead of
|
||||
// rebuilding the tree for each deposit.
|
||||
let tree = MerkleTree::create(
|
||||
&deposit_root_leaves[0..i],
|
||||
spec.deposit_contract_tree_depth as usize,
|
||||
);
|
||||
|
||||
let (_, mut proof) = tree.generate_proof(i - 1, spec.deposit_contract_tree_depth as usize);
|
||||
proof.push(Hash256::from_slice(&int_to_bytes32(i)));
|
||||
|
||||
assert_eq!(
|
||||
proof.len(),
|
||||
spec.deposit_contract_tree_depth as usize + 1,
|
||||
"Deposit proof should be correct len"
|
||||
);
|
||||
|
||||
proofs.push(proof);
|
||||
}
|
||||
|
||||
let deposits = datas
|
||||
.into_par_iter()
|
||||
.zip(proofs.into_par_iter())
|
||||
.map(|(data, proof)| (data, proof.into()))
|
||||
.map(|(data, proof)| Deposit { proof, data })
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut state =
|
||||
initialize_beacon_state_from_eth1(eth1_block_hash, eth1_timestamp, deposits, spec)
|
||||
.map_err(|e| format!("Unable to initialize genesis state: {:?}", e))?;
|
||||
|
||||
state.genesis_time = genesis_time;
|
||||
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
/// Returns `int` as little-endian bytes with a length of 32.
|
||||
fn int_to_bytes32(int: usize) -> Vec<u8> {
|
||||
let mut vec = int.to_le_bytes().to_vec();
|
||||
vec.resize(32, 0);
|
||||
vec
|
||||
}
|
||||
|
||||
/// Returns the system time, mod 30 minutes.
|
||||
///
|
||||
/// Used for easily creating testnets.
|
||||
fn recent_genesis_time(minutes: u64) -> u64 {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
let secs_after_last_period = now.checked_rem(minutes * 60).unwrap_or(0);
|
||||
now - secs_after_last_period
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use types::{EthSpec, MinimalEthSpec};
|
||||
|
||||
type TestEthSpec = MinimalEthSpec;
|
||||
|
||||
#[test]
|
||||
fn interop_state() {
|
||||
let validator_count = 16;
|
||||
let genesis_time = 42;
|
||||
let spec = &TestEthSpec::default_spec();
|
||||
|
||||
let state = interop_genesis_state::<TestEthSpec>(validator_count, genesis_time, spec)
|
||||
.expect("should build state");
|
||||
|
||||
assert_eq!(
|
||||
state.eth1_data.block_hash,
|
||||
Hash256::from_slice(&[42; 32]),
|
||||
"eth1 block hash should be co-ordinated junk"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
state.genesis_time, genesis_time,
|
||||
"genesis time should be as specified"
|
||||
);
|
||||
|
||||
for b in &state.balances {
|
||||
assert_eq!(
|
||||
*b, spec.max_effective_balance,
|
||||
"validator balances should be max effective balance"
|
||||
);
|
||||
}
|
||||
|
||||
for v in &state.validators {
|
||||
let creds = v.withdrawal_credentials.as_bytes();
|
||||
assert_eq!(
|
||||
creds[0], spec.bls_withdrawal_prefix_byte,
|
||||
"first byte of withdrawal creds should be bls prefix"
|
||||
);
|
||||
assert_eq!(
|
||||
&creds[1..],
|
||||
&hash(&v.pubkey.as_ssz_bytes())[1..],
|
||||
"rest of withdrawal creds should be pubkey hash"
|
||||
)
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
state.balances.len(),
|
||||
validator_count,
|
||||
"validator balances len should be correct"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
state.validators.len(),
|
||||
validator_count,
|
||||
"validator count should be correct"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,8 @@ pub enum BeaconChainError {
|
||||
previous_epoch: Epoch,
|
||||
new_epoch: Epoch,
|
||||
},
|
||||
SlotClockDidNotStart,
|
||||
NoStateForSlot(Slot),
|
||||
UnableToFindTargetRoot(Slot),
|
||||
BeaconStateError(BeaconStateError),
|
||||
DBInconsistent(String),
|
||||
@@ -35,6 +37,8 @@ pub enum BeaconChainError {
|
||||
beacon_block_root: Hash256,
|
||||
},
|
||||
AttestationValidationError(AttestationValidationError),
|
||||
/// Returned when an internal check fails, indicating corrupt data.
|
||||
InvariantViolated(String),
|
||||
}
|
||||
|
||||
easy_from_to!(SlotProcessingError, BeaconChainError);
|
||||
@@ -43,6 +47,7 @@ easy_from_to!(SlotProcessingError, BeaconChainError);
|
||||
pub enum BlockProductionError {
|
||||
UnableToGetBlockRootFromState,
|
||||
UnableToReadSlot,
|
||||
UnableToProduceAtSlot(Slot),
|
||||
SlotProcessingError(SlotProcessingError),
|
||||
BlockProcessingError(BlockProcessingError),
|
||||
BeaconStateError(BeaconStateError),
|
||||
|
||||
@@ -199,6 +199,14 @@ impl<T: BeaconChainTypes> ForkChoice<T> {
|
||||
self.backend.latest_message(validator_index)
|
||||
}
|
||||
|
||||
/// Runs an integrity verification function on the underlying fork choice algorithm.
|
||||
///
|
||||
/// Returns `Ok(())` if the underlying fork choice has maintained it's integrity,
|
||||
/// `Err(description)` otherwise.
|
||||
pub fn verify_integrity(&self) -> core::result::Result<(), String> {
|
||||
self.backend.verify_integrity()
|
||||
}
|
||||
|
||||
/// Inform the fork choice that the given block (and corresponding root) have been finalized so
|
||||
/// it may prune it's storage.
|
||||
///
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
extern crate lazy_static;
|
||||
|
||||
mod beacon_chain;
|
||||
mod beacon_chain_builder;
|
||||
mod checkpoint;
|
||||
mod errors;
|
||||
mod fork_choice;
|
||||
@@ -16,6 +17,7 @@ pub use self::beacon_chain::{
|
||||
};
|
||||
pub use self::checkpoint::CheckPoint;
|
||||
pub use self::errors::{BeaconChainError, BlockProductionError};
|
||||
pub use beacon_chain_builder::BeaconChainBuilder;
|
||||
pub use lmd_ghost;
|
||||
pub use metrics::scrape_for_metrics;
|
||||
pub use parking_lot;
|
||||
|
||||
@@ -3,7 +3,7 @@ use operation_pool::PersistedOperationPool;
|
||||
use ssz::{Decode, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use store::{DBColumn, Error as StoreError, StoreItem};
|
||||
use types::{BeaconState, Hash256};
|
||||
use types::Hash256;
|
||||
|
||||
/// 32-byte key for accessing the `PersistedBeaconChain`.
|
||||
pub const BEACON_CHAIN_DB_KEY: &str = "PERSISTEDBEACONCHAINPERSISTEDBEA";
|
||||
@@ -13,7 +13,6 @@ pub struct PersistedBeaconChain<T: BeaconChainTypes> {
|
||||
pub canonical_head: CheckPoint<T::EthSpec>,
|
||||
pub op_pool: PersistedOperationPool<T::EthSpec>,
|
||||
pub genesis_block_root: Hash256,
|
||||
pub state: BeaconState<T::EthSpec>,
|
||||
}
|
||||
|
||||
impl<T: BeaconChainTypes> StoreItem for PersistedBeaconChain<T> {
|
||||
|
||||
@@ -2,7 +2,6 @@ use crate::{BeaconChain, BeaconChainTypes, BlockProcessingOutcome};
|
||||
use lmd_ghost::LmdGhost;
|
||||
use rayon::prelude::*;
|
||||
use sloggers::{null::NullLoggerBuilder, Build};
|
||||
use slot_clock::SlotClock;
|
||||
use slot_clock::TestingSlotClock;
|
||||
use state_processing::per_slot_processing;
|
||||
use std::marker::PhantomData;
|
||||
@@ -115,22 +114,9 @@ where
|
||||
let builder = NullLoggerBuilder;
|
||||
let log = builder.build().expect("logger should build");
|
||||
|
||||
// Slot clock
|
||||
let slot_clock = TestingSlotClock::new(
|
||||
spec.genesis_slot,
|
||||
genesis_state.genesis_time,
|
||||
spec.seconds_per_slot,
|
||||
);
|
||||
|
||||
let chain = BeaconChain::from_genesis(
|
||||
store,
|
||||
slot_clock,
|
||||
genesis_state,
|
||||
genesis_block,
|
||||
spec.clone(),
|
||||
log,
|
||||
)
|
||||
.expect("Terminate if beacon chain generation fails");
|
||||
let chain =
|
||||
BeaconChain::from_genesis(store, genesis_state, genesis_block, spec.clone(), log)
|
||||
.expect("Terminate if beacon chain generation fails");
|
||||
|
||||
Self {
|
||||
chain,
|
||||
@@ -144,7 +130,6 @@ where
|
||||
/// Does not produce blocks or attestations.
|
||||
pub fn advance_slot(&self) {
|
||||
self.chain.slot_clock.advance_slot();
|
||||
self.chain.catchup_state().expect("should catchup state");
|
||||
}
|
||||
|
||||
/// Extend the `BeaconChain` with some blocks and attestations. Returns the root of the
|
||||
@@ -166,7 +151,7 @@ where
|
||||
// Determine the slot for the first block (or skipped block).
|
||||
let state_slot = match block_strategy {
|
||||
BlockStrategy::OnCanonicalHead => {
|
||||
self.chain.read_slot_clock().expect("should know slot") - 1
|
||||
self.chain.slot().expect("should have a slot") - 1
|
||||
}
|
||||
BlockStrategy::ForkCanonicalChainAt { previous_slot, .. } => previous_slot,
|
||||
};
|
||||
@@ -176,16 +161,14 @@ where
|
||||
|
||||
// Determine the first slot where a block should be built.
|
||||
let mut slot = match block_strategy {
|
||||
BlockStrategy::OnCanonicalHead => {
|
||||
self.chain.read_slot_clock().expect("should know slot")
|
||||
}
|
||||
BlockStrategy::OnCanonicalHead => self.chain.slot().expect("should have a slot"),
|
||||
BlockStrategy::ForkCanonicalChainAt { first_slot, .. } => first_slot,
|
||||
};
|
||||
|
||||
let mut head_block_root = None;
|
||||
|
||||
for _ in 0..num_blocks {
|
||||
while self.chain.read_slot_clock().expect("should have a slot") < slot {
|
||||
while self.chain.slot().expect("should have a slot") < slot {
|
||||
self.advance_slot();
|
||||
}
|
||||
|
||||
|
||||
@@ -322,7 +322,9 @@ fn roundtrip_operation_pool() {
|
||||
let p: PersistedBeaconChain<CommonTypes<TestForkChoice, MinimalEthSpec>> =
|
||||
harness.chain.store.get(&key).unwrap().unwrap();
|
||||
|
||||
let restored_op_pool = p.op_pool.into_operation_pool(&p.state, &harness.spec);
|
||||
let restored_op_pool = p
|
||||
.op_pool
|
||||
.into_operation_pool(&p.canonical_head.beacon_state, &harness.spec);
|
||||
|
||||
assert_eq!(harness.chain.op_pool, restored_op_pool);
|
||||
}
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
use crate::bootstrapper::Bootstrapper;
|
||||
use crate::error::Result;
|
||||
use crate::{config::GenesisState, ClientConfig};
|
||||
use beacon_chain::{
|
||||
lmd_ghost::{LmdGhost, ThreadSafeReducedTree},
|
||||
slot_clock::SystemTimeSlotClock,
|
||||
store::Store,
|
||||
BeaconChain, BeaconChainTypes,
|
||||
};
|
||||
use slog::{crit, info, Logger};
|
||||
use slot_clock::SlotClock;
|
||||
use std::fs::File;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use std::time::SystemTime;
|
||||
use tree_hash::TreeHash;
|
||||
use types::{
|
||||
test_utils::TestingBeaconStateBuilder, BeaconBlock, BeaconState, ChainSpec, EthSpec, Hash256,
|
||||
};
|
||||
|
||||
/// Provides a new, initialized `BeaconChain`
|
||||
pub trait InitialiseBeaconChain<T: BeaconChainTypes> {
|
||||
fn initialise_beacon_chain(
|
||||
store: Arc<T::Store>,
|
||||
config: &ClientConfig,
|
||||
spec: ChainSpec,
|
||||
log: Logger,
|
||||
) -> Result<BeaconChain<T>> {
|
||||
maybe_load_from_store_for_testnet::<_, T::Store, T::EthSpec>(store, config, spec, log)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ClientType<S: Store, E: EthSpec> {
|
||||
_phantom_t: PhantomData<S>,
|
||||
_phantom_u: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<S, E> BeaconChainTypes for ClientType<S, E>
|
||||
where
|
||||
S: Store + 'static,
|
||||
E: EthSpec,
|
||||
{
|
||||
type Store = S;
|
||||
type SlotClock = SystemTimeSlotClock;
|
||||
type LmdGhost = ThreadSafeReducedTree<S, E>;
|
||||
type EthSpec = E;
|
||||
}
|
||||
impl<T: Store, E: EthSpec, X: BeaconChainTypes> InitialiseBeaconChain<X> for ClientType<T, E> {}
|
||||
|
||||
/// Loads a `BeaconChain` from `store`, if it exists. Otherwise, create a new chain from genesis.
|
||||
fn maybe_load_from_store_for_testnet<T, U: Store, V: EthSpec>(
|
||||
store: Arc<U>,
|
||||
config: &ClientConfig,
|
||||
spec: ChainSpec,
|
||||
log: Logger,
|
||||
) -> Result<BeaconChain<T>>
|
||||
where
|
||||
T: BeaconChainTypes<Store = U, EthSpec = V>,
|
||||
T::LmdGhost: LmdGhost<U, V>,
|
||||
{
|
||||
let genesis_state = match &config.genesis_state {
|
||||
GenesisState::Mainnet => {
|
||||
crit!(log, "This release does not support mainnet genesis state.");
|
||||
return Err("Mainnet is unsupported".into());
|
||||
}
|
||||
GenesisState::RecentGenesis { validator_count } => {
|
||||
generate_testnet_genesis_state(*validator_count, recent_genesis_time(), &spec)
|
||||
}
|
||||
GenesisState::Generated {
|
||||
validator_count,
|
||||
genesis_time,
|
||||
} => generate_testnet_genesis_state(*validator_count, *genesis_time, &spec),
|
||||
GenesisState::Yaml { file } => {
|
||||
let file = File::open(file).map_err(|e| {
|
||||
format!("Unable to open YAML genesis state file {:?}: {:?}", file, e)
|
||||
})?;
|
||||
|
||||
serde_yaml::from_reader(file)
|
||||
.map_err(|e| format!("Unable to parse YAML genesis state file: {:?}", e))?
|
||||
}
|
||||
GenesisState::HttpBootstrap { server } => {
|
||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())
|
||||
.map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?;
|
||||
|
||||
let (state, _block) = bootstrapper
|
||||
.genesis()
|
||||
.map_err(|e| format!("Failed to bootstrap genesis state: {}", e))?;
|
||||
|
||||
state
|
||||
}
|
||||
};
|
||||
|
||||
let mut genesis_block = BeaconBlock::empty(&spec);
|
||||
genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root());
|
||||
let genesis_block_root = genesis_block.canonical_root();
|
||||
|
||||
// Slot clock
|
||||
let slot_clock = T::SlotClock::new(
|
||||
spec.genesis_slot,
|
||||
genesis_state.genesis_time,
|
||||
spec.seconds_per_slot,
|
||||
);
|
||||
|
||||
// Try load an existing `BeaconChain` from the store. If unable, create a new one.
|
||||
if let Ok(Some(beacon_chain)) =
|
||||
BeaconChain::from_store(store.clone(), spec.clone(), log.clone())
|
||||
{
|
||||
// Here we check to ensure that the `BeaconChain` loaded from store has the expected
|
||||
// genesis block.
|
||||
//
|
||||
// Without this check, it's possible that there will be an existing DB with a `BeaconChain`
|
||||
// that has different parameters than provided to this executable.
|
||||
if beacon_chain.genesis_block_root == genesis_block_root {
|
||||
info!(
|
||||
log,
|
||||
"Loaded BeaconChain from store";
|
||||
"slot" => beacon_chain.head().beacon_state.slot,
|
||||
"best_slot" => beacon_chain.best_slot(),
|
||||
);
|
||||
|
||||
Ok(beacon_chain)
|
||||
} else {
|
||||
crit!(
|
||||
log,
|
||||
"The BeaconChain loaded from disk has an incorrect genesis root. \
|
||||
This may be caused by an old database in located in datadir."
|
||||
);
|
||||
Err("Incorrect genesis root".into())
|
||||
}
|
||||
} else {
|
||||
BeaconChain::from_genesis(
|
||||
store,
|
||||
slot_clock,
|
||||
genesis_state,
|
||||
genesis_block,
|
||||
spec,
|
||||
log.clone(),
|
||||
)
|
||||
.map_err(|e| format!("Failed to initialize new beacon chain: {:?}", e).into())
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_testnet_genesis_state<E: EthSpec>(
|
||||
validator_count: usize,
|
||||
genesis_time: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> BeaconState<E> {
|
||||
let (mut genesis_state, _keypairs) =
|
||||
TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, spec)
|
||||
.build();
|
||||
|
||||
genesis_state.genesis_time = genesis_time;
|
||||
|
||||
genesis_state
|
||||
}
|
||||
|
||||
/// Returns the system time, mod 30 minutes.
|
||||
///
|
||||
/// Used for easily creating testnets.
|
||||
fn recent_genesis_time() -> u64 {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
let secs_after_last_period = now.checked_rem(30 * 60).unwrap_or(0);
|
||||
// genesis is now the last 30 minute block.
|
||||
now - secs_after_last_period
|
||||
}
|
||||
@@ -1,210 +0,0 @@
|
||||
use eth2_libp2p::{
|
||||
multiaddr::{Multiaddr, Protocol},
|
||||
Enr,
|
||||
};
|
||||
use reqwest::{Error as HttpError, Url};
|
||||
use serde::Deserialize;
|
||||
use std::borrow::Cow;
|
||||
use std::net::Ipv4Addr;
|
||||
use types::{BeaconBlock, BeaconState, Checkpoint, EthSpec, Hash256, Slot};
|
||||
use url::Host;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Error {
|
||||
InvalidUrl,
|
||||
HttpError(HttpError),
|
||||
}
|
||||
|
||||
impl From<HttpError> for Error {
|
||||
fn from(e: HttpError) -> Error {
|
||||
Error::HttpError(e)
|
||||
}
|
||||
}
|
||||
|
||||
/// Used to load "bootstrap" information from the HTTP API of another Lighthouse beacon node.
|
||||
///
|
||||
/// Bootstrapping information includes things like genesis and finalized states and blocks, and
|
||||
/// libp2p connection details.
|
||||
pub struct Bootstrapper {
|
||||
url: Url,
|
||||
}
|
||||
|
||||
impl Bootstrapper {
|
||||
/// Parses the given `server` as a URL, instantiating `Self`.
|
||||
pub fn from_server_string(server: String) -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
url: Url::parse(&server).map_err(|e| format!("Invalid bootstrap server url: {}", e))?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Build a multiaddr using the HTTP server URL that is not guaranteed to be correct.
|
||||
///
|
||||
/// The address is created by querying the HTTP server for its listening libp2p addresses.
|
||||
/// Then, we find the first TCP port in those addresses and combine the port with the URL of
|
||||
/// the server.
|
||||
///
|
||||
/// For example, the server `http://192.168.0.1` might end up with a `best_effort_multiaddr` of
|
||||
/// `/ipv4/192.168.0.1/tcp/9000` if the server advertises a listening address of
|
||||
/// `/ipv4/172.0.0.1/tcp/9000`.
|
||||
pub fn best_effort_multiaddr(&self) -> Option<Multiaddr> {
|
||||
let tcp_port = self.listen_port().ok()?;
|
||||
|
||||
let mut multiaddr = Multiaddr::with_capacity(2);
|
||||
|
||||
match self.url.host()? {
|
||||
Host::Ipv4(addr) => multiaddr.push(Protocol::Ip4(addr)),
|
||||
Host::Domain(s) => multiaddr.push(Protocol::Dns4(Cow::Borrowed(s))),
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
multiaddr.push(Protocol::Tcp(tcp_port));
|
||||
|
||||
Some(multiaddr)
|
||||
}
|
||||
|
||||
/// Returns the IPv4 address of the server URL, unless it contains a FQDN.
|
||||
pub fn server_ipv4_addr(&self) -> Option<Ipv4Addr> {
|
||||
match self.url.host()? {
|
||||
Host::Ipv4(addr) => Some(addr),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the servers ENR address.
|
||||
pub fn enr(&self) -> Result<Enr, String> {
|
||||
get_enr(self.url.clone()).map_err(|e| format!("Unable to get ENR: {:?}", e))
|
||||
}
|
||||
|
||||
/// Returns the servers listening libp2p addresses.
|
||||
pub fn listen_port(&self) -> Result<u16, String> {
|
||||
get_listen_port(self.url.clone()).map_err(|e| format!("Unable to get listen port: {:?}", e))
|
||||
}
|
||||
|
||||
/// Returns the genesis block and state.
|
||||
pub fn genesis<T: EthSpec>(&self) -> Result<(BeaconState<T>, BeaconBlock<T>), String> {
|
||||
let genesis_slot = Slot::new(0);
|
||||
|
||||
let block = get_block(self.url.clone(), genesis_slot)
|
||||
.map_err(|e| format!("Unable to get genesis block: {:?}", e))?
|
||||
.beacon_block;
|
||||
let state = get_state(self.url.clone(), genesis_slot)
|
||||
.map_err(|e| format!("Unable to get genesis state: {:?}", e))?
|
||||
.beacon_state;
|
||||
|
||||
Ok((state, block))
|
||||
}
|
||||
|
||||
/// Returns the most recent finalized state and block.
|
||||
pub fn finalized<T: EthSpec>(&self) -> Result<(BeaconState<T>, BeaconBlock<T>), String> {
|
||||
let slots_per_epoch = get_slots_per_epoch(self.url.clone())
|
||||
.map_err(|e| format!("Unable to get slots per epoch: {:?}", e))?;
|
||||
let finalized_slot = get_finalized_slot(self.url.clone(), slots_per_epoch.as_u64())
|
||||
.map_err(|e| format!("Unable to get finalized slot: {:?}", e))?;
|
||||
|
||||
let block = get_block(self.url.clone(), finalized_slot)
|
||||
.map_err(|e| format!("Unable to get finalized block: {:?}", e))?
|
||||
.beacon_block;
|
||||
let state = get_state(self.url.clone(), finalized_slot)
|
||||
.map_err(|e| format!("Unable to get finalized state: {:?}", e))?
|
||||
.beacon_state;
|
||||
|
||||
Ok((state, block))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_slots_per_epoch(mut url: Url) -> Result<Slot, Error> {
|
||||
url.path_segments_mut()
|
||||
.map(|mut url| {
|
||||
url.push("spec").push("slots_per_epoch");
|
||||
})
|
||||
.map_err(|_| Error::InvalidUrl)?;
|
||||
|
||||
reqwest::get(url)?
|
||||
.error_for_status()?
|
||||
.json()
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn get_finalized_slot(mut url: Url, slots_per_epoch: u64) -> Result<Slot, Error> {
|
||||
url.path_segments_mut()
|
||||
.map(|mut url| {
|
||||
url.push("beacon").push("latest_finalized_checkpoint");
|
||||
})
|
||||
.map_err(|_| Error::InvalidUrl)?;
|
||||
|
||||
let checkpoint: Checkpoint = reqwest::get(url)?.error_for_status()?.json()?;
|
||||
|
||||
Ok(checkpoint.epoch.start_slot(slots_per_epoch))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(bound = "T: EthSpec")]
|
||||
pub struct StateResponse<T: EthSpec> {
|
||||
pub root: Hash256,
|
||||
pub beacon_state: BeaconState<T>,
|
||||
}
|
||||
|
||||
fn get_state<T: EthSpec>(mut url: Url, slot: Slot) -> Result<StateResponse<T>, Error> {
|
||||
url.path_segments_mut()
|
||||
.map(|mut url| {
|
||||
url.push("beacon").push("state");
|
||||
})
|
||||
.map_err(|_| Error::InvalidUrl)?;
|
||||
|
||||
url.query_pairs_mut()
|
||||
.append_pair("slot", &format!("{}", slot.as_u64()));
|
||||
|
||||
reqwest::get(url)?
|
||||
.error_for_status()?
|
||||
.json()
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(bound = "T: EthSpec")]
|
||||
pub struct BlockResponse<T: EthSpec> {
|
||||
pub root: Hash256,
|
||||
pub beacon_block: BeaconBlock<T>,
|
||||
}
|
||||
|
||||
fn get_block<T: EthSpec>(mut url: Url, slot: Slot) -> Result<BlockResponse<T>, Error> {
|
||||
url.path_segments_mut()
|
||||
.map(|mut url| {
|
||||
url.push("beacon").push("block");
|
||||
})
|
||||
.map_err(|_| Error::InvalidUrl)?;
|
||||
|
||||
url.query_pairs_mut()
|
||||
.append_pair("slot", &format!("{}", slot.as_u64()));
|
||||
|
||||
reqwest::get(url)?
|
||||
.error_for_status()?
|
||||
.json()
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn get_enr(mut url: Url) -> Result<Enr, Error> {
|
||||
url.path_segments_mut()
|
||||
.map(|mut url| {
|
||||
url.push("network").push("enr");
|
||||
})
|
||||
.map_err(|_| Error::InvalidUrl)?;
|
||||
|
||||
reqwest::get(url)?
|
||||
.error_for_status()?
|
||||
.json()
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn get_listen_port(mut url: Url) -> Result<u16, Error> {
|
||||
url.path_segments_mut()
|
||||
.map(|mut url| {
|
||||
url.push("network").push("listen_port");
|
||||
})
|
||||
.map_err(|_| Error::InvalidUrl)?;
|
||||
|
||||
reqwest::get(url)?
|
||||
.error_for_status()?
|
||||
.json()
|
||||
.map_err(Into::into)
|
||||
}
|
||||
@@ -1,15 +1,11 @@
|
||||
use crate::{Bootstrapper, Eth2Config};
|
||||
use clap::ArgMatches;
|
||||
use network::NetworkConfig;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use slog::{info, o, warn, Drain};
|
||||
use slog::{info, o, Drain};
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
/// The number initial validators when starting the `Minimal`.
|
||||
const TESTNET_VALIDATOR_COUNT: usize = 16;
|
||||
|
||||
/// The number initial validators when starting the `Minimal`.
|
||||
const TESTNET_SPEC_CONSTANTS: &str = "minimal";
|
||||
|
||||
@@ -21,33 +17,52 @@ pub struct Config {
|
||||
db_name: String,
|
||||
pub log_file: PathBuf,
|
||||
pub spec_constants: String,
|
||||
pub genesis_state: GenesisState,
|
||||
/// Defines how we should initialize a BeaconChain instances.
|
||||
///
|
||||
/// This field is not serialized, there for it will not be written to (or loaded from) config
|
||||
/// files. It can only be configured via the CLI.
|
||||
#[serde(skip)]
|
||||
pub beacon_chain_start_method: BeaconChainStartMethod,
|
||||
pub network: network::NetworkConfig,
|
||||
pub rpc: rpc::RPCConfig,
|
||||
pub rest_api: rest_api::ApiConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum GenesisState {
|
||||
/// Use the mainnet genesis state.
|
||||
///
|
||||
/// Mainnet genesis state is not presently known, so this is a place-holder.
|
||||
/// Defines how the client should initialize a BeaconChain.
|
||||
///
|
||||
/// In general, there are two methods:
|
||||
/// - resuming a new chain, or
|
||||
/// - initializing a new one.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BeaconChainStartMethod {
|
||||
/// Resume from an existing BeaconChain, loaded from the existing local database.
|
||||
Resume,
|
||||
/// Resume from an existing BeaconChain, loaded from the existing local database.
|
||||
Mainnet,
|
||||
/// Generate a state with `validator_count` validators, all with well-known secret keys.
|
||||
/// Create a new beacon chain that can connect to mainnet.
|
||||
///
|
||||
/// Set the genesis time to be the start of the previous 30-minute window.
|
||||
RecentGenesis { validator_count: usize },
|
||||
/// Generate a state with `genesis_time` and `validator_count` validators, all with well-known
|
||||
RecentGenesis {
|
||||
validator_count: usize,
|
||||
minutes: u64,
|
||||
},
|
||||
/// Create a new beacon chain with `genesis_time` and `validator_count` validators, all with well-known
|
||||
/// secret keys.
|
||||
Generated {
|
||||
validator_count: usize,
|
||||
genesis_time: u64,
|
||||
},
|
||||
/// Load a YAML-encoded genesis state from a file.
|
||||
/// Create a new beacon chain by loading a YAML-encoded genesis state from a file.
|
||||
Yaml { file: PathBuf },
|
||||
/// Use a HTTP server (running our REST-API) to load genesis and finalized states and blocks.
|
||||
HttpBootstrap { server: String },
|
||||
/// Create a new beacon chain by using a HTTP server (running our REST-API) to load genesis and
|
||||
/// finalized states and blocks.
|
||||
HttpBootstrap { server: String, port: Option<u16> },
|
||||
}
|
||||
|
||||
impl Default for BeaconChainStartMethod {
|
||||
fn default() -> Self {
|
||||
BeaconChainStartMethod::Resume
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
@@ -58,12 +73,10 @@ impl Default for Config {
|
||||
db_type: "disk".to_string(),
|
||||
db_name: "chain_db".to_string(),
|
||||
network: NetworkConfig::new(),
|
||||
rpc: rpc::RPCConfig::default(),
|
||||
rest_api: rest_api::ApiConfig::default(),
|
||||
rpc: <_>::default(),
|
||||
rest_api: <_>::default(),
|
||||
spec_constants: TESTNET_SPEC_CONSTANTS.into(),
|
||||
genesis_state: GenesisState::RecentGenesis {
|
||||
validator_count: TESTNET_VALIDATOR_COUNT,
|
||||
},
|
||||
beacon_chain_start_method: <_>::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,6 +89,8 @@ impl Config {
|
||||
}
|
||||
|
||||
/// Returns the core path for the client.
|
||||
///
|
||||
/// Creates the directory if it does not exist.
|
||||
pub fn data_dir(&self) -> Option<PathBuf> {
|
||||
let path = dirs::home_dir()?.join(&self.data_dir);
|
||||
fs::create_dir_all(&path).ok()?;
|
||||
@@ -127,15 +142,6 @@ impl Config {
|
||||
self.data_dir = PathBuf::from(dir);
|
||||
};
|
||||
|
||||
if let Some(default_spec) = args.value_of("default-spec") {
|
||||
match default_spec {
|
||||
"mainnet" => self.spec_constants = Eth2Config::mainnet().spec_constants,
|
||||
"minimal" => self.spec_constants = Eth2Config::minimal().spec_constants,
|
||||
"interop" => self.spec_constants = Eth2Config::interop().spec_constants,
|
||||
_ => {} // not supported
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(dir) = args.value_of("db") {
|
||||
self.db_type = dir.to_string();
|
||||
};
|
||||
@@ -149,40 +155,6 @@ impl Config {
|
||||
self.update_logger(log)?;
|
||||
};
|
||||
|
||||
// If the `--bootstrap` flag is provided, overwrite the default configuration.
|
||||
if let Some(server) = args.value_of("bootstrap") {
|
||||
do_bootstrapping(self, server.to_string(), &log)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform the HTTP bootstrapping procedure, reading an ENR and multiaddr from the HTTP server and
|
||||
/// adding them to the `config`.
|
||||
fn do_bootstrapping(config: &mut Config, server: String, log: &slog::Logger) -> Result<(), String> {
|
||||
// Set the genesis state source.
|
||||
config.genesis_state = GenesisState::HttpBootstrap {
|
||||
server: server.to_string(),
|
||||
};
|
||||
|
||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())?;
|
||||
|
||||
config.network.boot_nodes.push(bootstrapper.enr()?);
|
||||
|
||||
if let Some(server_multiaddr) = bootstrapper.best_effort_multiaddr() {
|
||||
info!(
|
||||
log,
|
||||
"Estimated bootstrapper libp2p address";
|
||||
"multiaddr" => format!("{:?}", server_multiaddr)
|
||||
);
|
||||
config.network.libp2p_nodes.push(server_multiaddr);
|
||||
} else {
|
||||
warn!(
|
||||
log,
|
||||
"Unable to estimate a bootstrapper libp2p address, this node may not find any peers."
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,31 +1,47 @@
|
||||
extern crate slog;
|
||||
|
||||
mod beacon_chain_types;
|
||||
mod bootstrapper;
|
||||
mod config;
|
||||
|
||||
pub mod error;
|
||||
pub mod notifier;
|
||||
|
||||
use beacon_chain::BeaconChain;
|
||||
use beacon_chain::{
|
||||
lmd_ghost::ThreadSafeReducedTree, slot_clock::SystemTimeSlotClock, store::Store, BeaconChain,
|
||||
BeaconChainBuilder,
|
||||
};
|
||||
use exit_future::Signal;
|
||||
use futures::{future::Future, Stream};
|
||||
use network::Service as NetworkService;
|
||||
use slog::{error, info, o};
|
||||
use slog::{crit, error, info, o};
|
||||
use slot_clock::SlotClock;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::runtime::TaskExecutor;
|
||||
use tokio::timer::Interval;
|
||||
use types::EthSpec;
|
||||
|
||||
pub use beacon_chain::BeaconChainTypes;
|
||||
pub use beacon_chain_types::ClientType;
|
||||
pub use beacon_chain_types::InitialiseBeaconChain;
|
||||
pub use bootstrapper::Bootstrapper;
|
||||
pub use config::{Config as ClientConfig, GenesisState};
|
||||
pub use config::{BeaconChainStartMethod, Config as ClientConfig};
|
||||
pub use eth2_config::Eth2Config;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ClientType<S: Store, E: EthSpec> {
|
||||
_phantom_t: PhantomData<S>,
|
||||
_phantom_u: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<S, E> BeaconChainTypes for ClientType<S, E>
|
||||
where
|
||||
S: Store + 'static,
|
||||
E: EthSpec,
|
||||
{
|
||||
type Store = S;
|
||||
type SlotClock = SystemTimeSlotClock;
|
||||
type LmdGhost = ThreadSafeReducedTree<S, E>;
|
||||
type EthSpec = E;
|
||||
}
|
||||
|
||||
/// Main beacon node client service. This provides the connection and initialisation of the clients
|
||||
/// sub-services in multiple threads.
|
||||
pub struct Client<T: BeaconChainTypes> {
|
||||
@@ -49,7 +65,7 @@ pub struct Client<T: BeaconChainTypes> {
|
||||
|
||||
impl<T> Client<T>
|
||||
where
|
||||
T: BeaconChainTypes + InitialiseBeaconChain<T> + Clone,
|
||||
T: BeaconChainTypes + Clone,
|
||||
{
|
||||
/// Generate an instance of the client. Spawn and link all internal sub-processes.
|
||||
pub fn new(
|
||||
@@ -60,39 +76,90 @@ where
|
||||
executor: &TaskExecutor,
|
||||
) -> error::Result<Self> {
|
||||
let store = Arc::new(store);
|
||||
let seconds_per_slot = eth2_config.spec.seconds_per_slot;
|
||||
let milliseconds_per_slot = eth2_config.spec.milliseconds_per_slot;
|
||||
|
||||
// Load a `BeaconChain` from the store, or create a new one if it does not exist.
|
||||
let beacon_chain = Arc::new(T::initialise_beacon_chain(
|
||||
store,
|
||||
&client_config,
|
||||
eth2_config.spec.clone(),
|
||||
log.clone(),
|
||||
)?);
|
||||
let spec = ð2_config.spec.clone();
|
||||
|
||||
if beacon_chain.read_slot_clock().is_none() {
|
||||
let beacon_chain_builder = match &client_config.beacon_chain_start_method {
|
||||
BeaconChainStartMethod::Resume => {
|
||||
info!(
|
||||
log,
|
||||
"Starting beacon chain";
|
||||
"method" => "resume"
|
||||
);
|
||||
BeaconChainBuilder::from_store(spec.clone(), log.clone())
|
||||
}
|
||||
BeaconChainStartMethod::Mainnet => {
|
||||
crit!(log, "No mainnet beacon chain startup specification.");
|
||||
return Err("Mainnet launch is not yet announced.".into());
|
||||
}
|
||||
BeaconChainStartMethod::RecentGenesis {
|
||||
validator_count,
|
||||
minutes,
|
||||
} => {
|
||||
info!(
|
||||
log,
|
||||
"Starting beacon chain";
|
||||
"validator_count" => validator_count,
|
||||
"minutes" => minutes,
|
||||
"method" => "recent"
|
||||
);
|
||||
BeaconChainBuilder::recent_genesis(
|
||||
*validator_count,
|
||||
*minutes,
|
||||
spec.clone(),
|
||||
log.clone(),
|
||||
)?
|
||||
}
|
||||
BeaconChainStartMethod::Generated {
|
||||
validator_count,
|
||||
genesis_time,
|
||||
} => {
|
||||
info!(
|
||||
log,
|
||||
"Starting beacon chain";
|
||||
"validator_count" => validator_count,
|
||||
"genesis_time" => genesis_time,
|
||||
"method" => "quick"
|
||||
);
|
||||
BeaconChainBuilder::quick_start(
|
||||
*genesis_time,
|
||||
*validator_count,
|
||||
spec.clone(),
|
||||
log.clone(),
|
||||
)?
|
||||
}
|
||||
BeaconChainStartMethod::Yaml { file } => {
|
||||
info!(
|
||||
log,
|
||||
"Starting beacon chain";
|
||||
"file" => format!("{:?}", file),
|
||||
"method" => "yaml"
|
||||
);
|
||||
BeaconChainBuilder::yaml_state(file, spec.clone(), log.clone())?
|
||||
}
|
||||
BeaconChainStartMethod::HttpBootstrap { server, port } => {
|
||||
info!(
|
||||
log,
|
||||
"Starting beacon chain";
|
||||
"port" => port,
|
||||
"server" => server,
|
||||
"method" => "bootstrap"
|
||||
);
|
||||
BeaconChainBuilder::http_bootstrap(server, spec.clone(), log.clone())?
|
||||
}
|
||||
};
|
||||
|
||||
let beacon_chain: Arc<BeaconChain<T>> = Arc::new(
|
||||
beacon_chain_builder
|
||||
.build(store)
|
||||
.map_err(error::Error::from)?,
|
||||
);
|
||||
|
||||
if beacon_chain.slot().is_err() {
|
||||
panic!("Cannot start client before genesis!")
|
||||
}
|
||||
|
||||
// Block starting the client until we have caught the state up to the current slot.
|
||||
//
|
||||
// If we don't block here we create an initial scenario where we're unable to process any
|
||||
// blocks and we're basically useless.
|
||||
{
|
||||
let state_slot = beacon_chain.head().beacon_state.slot;
|
||||
let wall_clock_slot = beacon_chain.read_slot_clock().unwrap();
|
||||
let slots_since_genesis = beacon_chain.slots_since_genesis().unwrap();
|
||||
info!(
|
||||
log,
|
||||
"BeaconState cache init";
|
||||
"state_slot" => state_slot,
|
||||
"wall_clock_slot" => wall_clock_slot,
|
||||
"slots_since_genesis" => slots_since_genesis,
|
||||
"catchup_distance" => wall_clock_slot - state_slot,
|
||||
);
|
||||
}
|
||||
do_state_catchup(&beacon_chain, &log);
|
||||
|
||||
let network_config = &client_config.network;
|
||||
let (network, network_send) =
|
||||
NetworkService::new(beacon_chain.clone(), network_config, executor, log.clone())?;
|
||||
@@ -118,6 +185,7 @@ where
|
||||
beacon_chain.clone(),
|
||||
network.clone(),
|
||||
client_config.db_path().expect("unable to read datadir"),
|
||||
eth2_config.clone(),
|
||||
&log,
|
||||
) {
|
||||
Ok(s) => Some(s),
|
||||
@@ -131,11 +199,11 @@ where
|
||||
};
|
||||
|
||||
let (slot_timer_exit_signal, exit) = exit_future::signal();
|
||||
if let Ok(Some(duration_to_next_slot)) = beacon_chain.slot_clock.duration_to_next_slot() {
|
||||
if let Some(duration_to_next_slot) = beacon_chain.slot_clock.duration_to_next_slot() {
|
||||
// set up the validator work interval - start at next slot and proceed every slot
|
||||
let interval = {
|
||||
// Set the interval to start at the next slot, and every slot after
|
||||
let slot_duration = Duration::from_secs(seconds_per_slot);
|
||||
let slot_duration = Duration::from_millis(milliseconds_per_slot);
|
||||
//TODO: Handle checked add correctly
|
||||
Interval::new(Instant::now() + duration_to_next_slot, slot_duration)
|
||||
};
|
||||
@@ -146,7 +214,7 @@ where
|
||||
exit.until(
|
||||
interval
|
||||
.for_each(move |_| {
|
||||
do_state_catchup(&chain, &log);
|
||||
log_new_slot(&chain, &log);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
@@ -176,35 +244,19 @@ impl<T: BeaconChainTypes> Drop for Client<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fn do_state_catchup<T: BeaconChainTypes>(chain: &Arc<BeaconChain<T>>, log: &slog::Logger) {
|
||||
// Only attempt to `catchup_state` if we can read the slot clock.
|
||||
if let Some(current_slot) = chain.read_slot_clock() {
|
||||
let state_catchup_result = chain.catchup_state();
|
||||
fn log_new_slot<T: BeaconChainTypes>(chain: &Arc<BeaconChain<T>>, log: &slog::Logger) {
|
||||
let best_slot = chain.head().beacon_block.slot;
|
||||
let latest_block_root = chain.head().beacon_block_root;
|
||||
|
||||
let best_slot = chain.head().beacon_block.slot;
|
||||
let latest_block_root = chain.head().beacon_block_root;
|
||||
|
||||
let common = o!(
|
||||
if let Ok(current_slot) = chain.slot() {
|
||||
info!(
|
||||
log,
|
||||
"Slot start";
|
||||
"skip_slots" => current_slot.saturating_sub(best_slot),
|
||||
"best_block_root" => format!("{}", latest_block_root),
|
||||
"best_block_slot" => best_slot,
|
||||
"slot" => current_slot,
|
||||
);
|
||||
|
||||
if let Err(e) = state_catchup_result {
|
||||
error!(
|
||||
log,
|
||||
"State catchup failed";
|
||||
"error" => format!("{:?}", e),
|
||||
common
|
||||
)
|
||||
} else {
|
||||
info!(
|
||||
log,
|
||||
"Slot start";
|
||||
common
|
||||
)
|
||||
}
|
||||
)
|
||||
} else {
|
||||
error!(
|
||||
log,
|
||||
|
||||
@@ -341,9 +341,13 @@ fn save_enr_to_disc(dir: &Path, enr: &Enr, log: &slog::Logger) {
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(
|
||||
log,
|
||||
"Could not write ENR to file"; "file" => format!("{:?}{:?}",dir, ENR_FILENAME), "error" => format!("{}", e)
|
||||
);
|
||||
log,
|
||||
<<<<<<< HEAD
|
||||
"Could not write ENR to file"; "file" => format!("{:?}{:?}",dir, ENR_FILENAME), "error" => format!("{}", e)
|
||||
=======
|
||||
"Could not write ENR to file"; "File" => format!("{:?}{:?}",dir, ENR_FILENAME), "Error" => format!("{}", e)
|
||||
>>>>>>> interop
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,10 +82,17 @@ impl Service {
|
||||
// attempt to connect to user-input libp2p nodes
|
||||
for multiaddr in config.libp2p_nodes {
|
||||
match Swarm::dial_addr(&mut swarm, multiaddr.clone()) {
|
||||
<<<<<<< HEAD
|
||||
Ok(()) => debug!(log, "Dialing libp2p peer"; "address" => format!("{}", multiaddr)),
|
||||
Err(err) => debug!(
|
||||
log,
|
||||
"Could not connect to peer"; "address" => format!("{}", multiaddr), "Error" => format!("{:?}", err)
|
||||
=======
|
||||
Ok(()) => debug!(log, "Dialing libp2p peer"; "Address" => format!("{}", multiaddr)),
|
||||
Err(err) => debug!(
|
||||
log,
|
||||
"Could not connect to peer"; "Address" => format!("{}", multiaddr), "Error" => format!("{:?}", err)
|
||||
>>>>>>> interop
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -122,6 +129,7 @@ impl Service {
|
||||
let mut subscribed_topics = vec![];
|
||||
for topic in topics {
|
||||
if swarm.subscribe(topic.clone()) {
|
||||
<<<<<<< HEAD
|
||||
trace!(log, "Subscribed to topic"; "topic" => format!("{}", topic));
|
||||
subscribed_topics.push(topic);
|
||||
} else {
|
||||
@@ -129,6 +137,15 @@ impl Service {
|
||||
}
|
||||
}
|
||||
info!(log, "Subscribed to topics"; "topics" => format!("{:?}", subscribed_topics.iter().map(|t| format!("{}", t)).collect::<Vec<String>>()));
|
||||
=======
|
||||
trace!(log, "Subscribed to topic"; "Topic" => format!("{}", topic));
|
||||
subscribed_topics.push(topic);
|
||||
} else {
|
||||
warn!(log, "Could not subscribe to topic"; "Topic" => format!("{}", topic));
|
||||
}
|
||||
}
|
||||
info!(log, "Subscribed to topics"; "Topics" => format!("{:?}", subscribed_topics.iter().map(|t| format!("{}", t)).collect::<Vec<String>>()));
|
||||
>>>>>>> interop
|
||||
|
||||
Ok(Service {
|
||||
local_peer_id,
|
||||
|
||||
@@ -28,6 +28,7 @@ exit-future = "0.1.3"
|
||||
tokio = "0.1.17"
|
||||
url = "2.0"
|
||||
lazy_static = "1.3.0"
|
||||
eth2_config = { path = "../../eth2/utils/eth2_config" }
|
||||
lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
|
||||
slot_clock = { path = "../../eth2/utils/slot_clock" }
|
||||
hex = "0.3.2"
|
||||
|
||||
@@ -16,7 +16,7 @@ pub struct Config {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
enabled: true, // rest_api enabled by default
|
||||
enabled: true,
|
||||
listen_address: Ipv4Addr::new(127, 0, 0, 1),
|
||||
port: 5052,
|
||||
}
|
||||
@@ -25,8 +25,8 @@ impl Default for Config {
|
||||
|
||||
impl Config {
|
||||
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), &'static str> {
|
||||
if args.is_present("api") {
|
||||
self.enabled = true;
|
||||
if args.is_present("no-api") {
|
||||
self.enabled = false;
|
||||
}
|
||||
|
||||
if let Some(rpc_address) = args.value_of("api-address") {
|
||||
|
||||
@@ -110,8 +110,8 @@ pub fn state_root_at_slot<T: BeaconChainTypes>(
|
||||
) -> Result<Hash256, ApiError> {
|
||||
let head_state = &beacon_chain.head().beacon_state;
|
||||
let current_slot = beacon_chain
|
||||
.read_slot_clock()
|
||||
.ok_or_else(|| ApiError::ServerError("Unable to read slot clock".to_string()))?;
|
||||
.slot()
|
||||
.map_err(|_| ApiError::ServerError("Unable to read slot clock".to_string()))?;
|
||||
|
||||
// There are four scenarios when obtaining a state for a given slot:
|
||||
//
|
||||
|
||||
@@ -14,6 +14,7 @@ mod validator;
|
||||
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use client_network::Service as NetworkService;
|
||||
use eth2_config::Eth2Config;
|
||||
use hyper::rt::Future;
|
||||
use hyper::service::service_fn_ok;
|
||||
use hyper::{Body, Method, Response, Server, StatusCode};
|
||||
@@ -80,6 +81,7 @@ pub fn start_server<T: BeaconChainTypes>(
|
||||
beacon_chain: Arc<BeaconChain<T>>,
|
||||
network_service: Arc<NetworkService<T>>,
|
||||
db_path: PathBuf,
|
||||
eth2_config: Eth2Config,
|
||||
log: &slog::Logger,
|
||||
) -> Result<exit_future::Signal, hyper::Error> {
|
||||
let log = log.new(o!("Service" => "Api"));
|
||||
@@ -101,12 +103,14 @@ pub fn start_server<T: BeaconChainTypes>(
|
||||
// Clone our stateful objects, for use in service closure.
|
||||
let server_log = log.clone();
|
||||
let server_bc = beacon_chain.clone();
|
||||
let eth2_config = Arc::new(eth2_config);
|
||||
|
||||
let service = move || {
|
||||
let log = server_log.clone();
|
||||
let beacon_chain = server_bc.clone();
|
||||
let db_path = db_path.clone();
|
||||
let network_service = network_service.clone();
|
||||
let eth2_config = eth2_config.clone();
|
||||
|
||||
// Create a simple handler for the router, inject our stateful objects into the request.
|
||||
service_fn_ok(move |mut req| {
|
||||
@@ -119,6 +123,8 @@ pub fn start_server<T: BeaconChainTypes>(
|
||||
req.extensions_mut().insert::<DBPath>(db_path.clone());
|
||||
req.extensions_mut()
|
||||
.insert::<Arc<NetworkService<T>>>(network_service.clone());
|
||||
req.extensions_mut()
|
||||
.insert::<Arc<Eth2Config>>(eth2_config.clone());
|
||||
|
||||
let path = req.uri().path().to_string();
|
||||
|
||||
@@ -161,15 +167,6 @@ pub fn start_server<T: BeaconChainTypes>(
|
||||
(&Method::GET, "/node/syncing") => helpers::implementation_pending_response(req),
|
||||
(&Method::GET, "/node/fork") => helpers::implementation_pending_response(req),
|
||||
|
||||
// Methods for Network
|
||||
(&Method::GET, "/network/enr") => network::get_enr::<T>(req),
|
||||
(&Method::GET, "/network/peer_count") => network::get_peer_count::<T>(req),
|
||||
(&Method::GET, "/network/peer_id") => network::get_peer_id::<T>(req),
|
||||
(&Method::GET, "/network/peers") => network::get_peer_list::<T>(req),
|
||||
(&Method::GET, "/network/listen_addresses") => {
|
||||
network::get_listen_addresses::<T>(req)
|
||||
}
|
||||
|
||||
// Methods for Validator
|
||||
(&Method::GET, "/validator/duties") => validator::get_validator_duties::<T>(req),
|
||||
(&Method::GET, "/validator/block") => helpers::implementation_pending_response(req),
|
||||
@@ -185,6 +182,7 @@ pub fn start_server<T: BeaconChainTypes>(
|
||||
|
||||
(&Method::GET, "/spec") => spec::get_spec::<T>(req),
|
||||
(&Method::GET, "/spec/slots_per_epoch") => spec::get_slots_per_epoch::<T>(req),
|
||||
(&Method::GET, "/spec/eth2_config") => spec::get_eth2_config::<T>(req),
|
||||
|
||||
_ => Err(ApiError::NotFound(
|
||||
"Request path and/or method not found.".to_owned(),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use super::{success_response, ApiResult};
|
||||
use crate::ApiError;
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use eth2_config::Eth2Config;
|
||||
use hyper::{Body, Request};
|
||||
use std::sync::Arc;
|
||||
use types::EthSpec;
|
||||
@@ -18,6 +19,19 @@ pub fn get_spec<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
|
||||
Ok(success_response(Body::from(json)))
|
||||
}
|
||||
|
||||
/// HTTP handler to return the full Eth2Config object.
|
||||
pub fn get_eth2_config<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
|
||||
let eth2_config = req
|
||||
.extensions()
|
||||
.get::<Arc<Eth2Config>>()
|
||||
.ok_or_else(|| ApiError::ServerError("Eth2Config extension missing".to_string()))?;
|
||||
|
||||
let json: String = serde_json::to_string(eth2_config.as_ref())
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to serialize Eth2Config: {:?}", e)))?;
|
||||
|
||||
Ok(success_response(Body::from(json)))
|
||||
}
|
||||
|
||||
/// HTTP handler to return the full spec object.
|
||||
pub fn get_slots_per_epoch<T: BeaconChainTypes + 'static>(_req: Request<Body>) -> ApiResult {
|
||||
let json: String = serde_json::to_string(&T::EthSpec::slots_per_epoch())
|
||||
|
||||
@@ -39,12 +39,7 @@ pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -
|
||||
.extensions()
|
||||
.get::<Arc<BeaconChain<T>>>()
|
||||
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".to_string()))?;
|
||||
let _ = beacon_chain
|
||||
.ensure_state_caches_are_built()
|
||||
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
|
||||
let head_state = beacon_chain
|
||||
.speculative_state()
|
||||
.expect("This is legacy code and should be removed.");
|
||||
let head_state = &beacon_chain.head().beacon_state;
|
||||
|
||||
// Parse and check query parameters
|
||||
let query = UrlQuery::from_request(&req)?;
|
||||
|
||||
@@ -14,7 +14,7 @@ use slog::{error, info, trace, warn};
|
||||
use ssz::{ssz_encode, Decode, Encode};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
use types::Attestation;
|
||||
use types::{Attestation, Slot};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AttestationServiceInstance<T: BeaconChainTypes> {
|
||||
@@ -37,49 +37,13 @@ impl<T: BeaconChainTypes> AttestationService for AttestationServiceInstance<T> {
|
||||
req.get_slot()
|
||||
);
|
||||
|
||||
// verify the slot, drop lock on state afterwards
|
||||
{
|
||||
let slot_requested = req.get_slot();
|
||||
// TODO: this whole module is legacy and not maintained well.
|
||||
let state = &self
|
||||
.chain
|
||||
.speculative_state()
|
||||
.expect("This is legacy code and should be removed");
|
||||
|
||||
// Start by performing some checks
|
||||
// Check that the AttestationData is for the current slot (otherwise it will not be valid)
|
||||
if slot_requested > state.slot.as_u64() {
|
||||
let log_clone = self.log.clone();
|
||||
let f = sink
|
||||
.fail(RpcStatus::new(
|
||||
RpcStatusCode::OutOfRange,
|
||||
Some(
|
||||
"AttestationData request for a slot that is in the future.".to_string(),
|
||||
),
|
||||
))
|
||||
.map_err(move |e| {
|
||||
error!(log_clone, "Failed to reply with failure {:?}: {:?}", req, e)
|
||||
});
|
||||
return ctx.spawn(f);
|
||||
}
|
||||
// currently cannot handle past slots. TODO: Handle this case
|
||||
else if slot_requested < state.slot.as_u64() {
|
||||
let log_clone = self.log.clone();
|
||||
let f = sink
|
||||
.fail(RpcStatus::new(
|
||||
RpcStatusCode::InvalidArgument,
|
||||
Some("AttestationData request for a slot that is in the past.".to_string()),
|
||||
))
|
||||
.map_err(move |e| {
|
||||
error!(log_clone, "Failed to reply with failure {:?}: {:?}", req, e)
|
||||
});
|
||||
return ctx.spawn(f);
|
||||
}
|
||||
}
|
||||
|
||||
// Then get the AttestationData from the beacon chain
|
||||
let shard = req.get_shard();
|
||||
let attestation_data = match self.chain.produce_attestation_data(shard) {
|
||||
let slot_requested = req.get_slot();
|
||||
let attestation_data = match self
|
||||
.chain
|
||||
.produce_attestation_data(shard, Slot::from(slot_requested))
|
||||
{
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
// Could not produce an attestation
|
||||
|
||||
@@ -34,8 +34,7 @@ impl<T: BeaconChainTypes> BeaconBlockService for BeaconBlockServiceInstance<T> {
|
||||
trace!(self.log, "Generating a beacon block"; "req" => format!("{:?}", req));
|
||||
|
||||
// decode the request
|
||||
// TODO: requested slot currently unused, see: https://github.com/sigp/lighthouse/issues/336
|
||||
let _requested_slot = Slot::from(req.get_slot());
|
||||
let requested_slot = Slot::from(req.get_slot());
|
||||
let randao_reveal = match Signature::from_ssz_bytes(req.get_randao_reveal()) {
|
||||
Ok(reveal) => reveal,
|
||||
Err(_) => {
|
||||
@@ -51,7 +50,7 @@ impl<T: BeaconChainTypes> BeaconBlockService for BeaconBlockServiceInstance<T> {
|
||||
}
|
||||
};
|
||||
|
||||
let produced_block = match self.chain.produce_block(randao_reveal) {
|
||||
let produced_block = match self.chain.produce_block(randao_reveal, requested_slot) {
|
||||
Ok((block, _state)) => block,
|
||||
Err(e) => {
|
||||
// could not produce a block
|
||||
@@ -67,6 +66,11 @@ impl<T: BeaconChainTypes> BeaconBlockService for BeaconBlockServiceInstance<T> {
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
produced_block.slot, requested_slot,
|
||||
"should produce at the requested slot"
|
||||
);
|
||||
|
||||
let mut block = BeaconBlockProto::new();
|
||||
block.set_ssz(ssz_encode(&produced_block));
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ pub struct Config {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
enabled: false, // rpc disabled by default
|
||||
enabled: true,
|
||||
listen_address: Ipv4Addr::new(127, 0, 0, 1),
|
||||
port: 5051,
|
||||
}
|
||||
@@ -25,8 +25,8 @@ impl Default for Config {
|
||||
|
||||
impl Config {
|
||||
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), &'static str> {
|
||||
if args.is_present("rpc") {
|
||||
self.enabled = true;
|
||||
if args.is_present("no-grpc") {
|
||||
self.enabled = false;
|
||||
}
|
||||
|
||||
if let Some(rpc_address) = args.value_of("rpc-address") {
|
||||
|
||||
@@ -80,7 +80,12 @@ pub fn start_server<T: BeaconChainTypes + Clone + 'static>(
|
||||
let spawn_rpc = {
|
||||
server.start();
|
||||
for &(ref host, port) in server.bind_addrs() {
|
||||
info!(log, "gRPC listening on {}:{}", host, port);
|
||||
info!(
|
||||
log,
|
||||
"gRPC API started";
|
||||
"port" => port,
|
||||
"host" => host,
|
||||
);
|
||||
}
|
||||
rpc_exit.and_then(move |_| {
|
||||
info!(log, "RPC Server shutting down");
|
||||
|
||||
@@ -28,36 +28,38 @@ impl<T: BeaconChainTypes> ValidatorService for ValidatorServiceInstance<T> {
|
||||
let validators = req.get_validators();
|
||||
trace!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch());
|
||||
|
||||
let spec = &self.chain.spec;
|
||||
// TODO: this whole module is legacy and not maintained well.
|
||||
let state = &self
|
||||
.chain
|
||||
.speculative_state()
|
||||
.expect("This is legacy code and should be removed");
|
||||
let epoch = Epoch::from(req.get_epoch());
|
||||
let slot = epoch.start_slot(T::EthSpec::slots_per_epoch());
|
||||
|
||||
let mut state = if let Ok(state) = self.chain.state_at_slot(slot) {
|
||||
state.as_ref().clone()
|
||||
} else {
|
||||
let log_clone = self.log.clone();
|
||||
let f = sink
|
||||
.fail(RpcStatus::new(
|
||||
RpcStatusCode::FailedPrecondition,
|
||||
Some("No state".to_string()),
|
||||
))
|
||||
.map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e));
|
||||
return ctx.spawn(f);
|
||||
};
|
||||
|
||||
let _ = state.build_all_caches(&self.chain.spec);
|
||||
|
||||
assert_eq!(
|
||||
state.current_epoch(),
|
||||
epoch,
|
||||
"Retrieved state should be from the same epoch"
|
||||
);
|
||||
|
||||
let mut resp = GetDutiesResponse::new();
|
||||
let resp_validators = resp.mut_active_validators();
|
||||
|
||||
let relative_epoch =
|
||||
match RelativeEpoch::from_epoch(state.slot.epoch(T::EthSpec::slots_per_epoch()), epoch)
|
||||
{
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
// incorrect epoch
|
||||
let log_clone = self.log.clone();
|
||||
let f = sink
|
||||
.fail(RpcStatus::new(
|
||||
RpcStatusCode::FailedPrecondition,
|
||||
Some(format!("Invalid epoch: {:?}", e)),
|
||||
))
|
||||
.map_err(move |e| warn!(log_clone, "failed to reply {:?}: {:?}", req, e));
|
||||
return ctx.spawn(f);
|
||||
}
|
||||
};
|
||||
|
||||
let validator_proposers: Result<Vec<usize>, _> = epoch
|
||||
.slot_iter(T::EthSpec::slots_per_epoch())
|
||||
.map(|slot| state.get_beacon_proposer_index(slot, relative_epoch, &spec))
|
||||
.map(|slot| {
|
||||
state.get_beacon_proposer_index(slot, RelativeEpoch::Current, &self.chain.spec)
|
||||
})
|
||||
.collect();
|
||||
let validator_proposers = match validator_proposers {
|
||||
Ok(v) => v,
|
||||
|
||||
495
beacon_node/src/config.rs
Normal file
495
beacon_node/src/config.rs
Normal file
@@ -0,0 +1,495 @@
|
||||
use clap::ArgMatches;
|
||||
use client::{BeaconChainStartMethod, ClientConfig, Eth2Config};
|
||||
use eth2_config::{read_from_file, write_to_file};
|
||||
use lighthouse_bootstrap::Bootstrapper;
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
use slog::{crit, info, warn, Logger};
|
||||
use std::fs;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub const DEFAULT_DATA_DIR: &str = ".lighthouse";
|
||||
pub const CLIENT_CONFIG_FILENAME: &str = "beacon-node.toml";
|
||||
pub const ETH2_CONFIG_FILENAME: &str = "eth2-spec.toml";
|
||||
|
||||
type Result<T> = std::result::Result<T, String>;
|
||||
type Config = (ClientConfig, Eth2Config);
|
||||
|
||||
/// Gets the fully-initialized global client and eth2 configuration objects.
|
||||
///
|
||||
/// The top-level `clap` arguments should be provied as `cli_args`.
|
||||
///
|
||||
/// The output of this function depends primarily upon the given `cli_args`, however it's behaviour
|
||||
/// may be influenced by other external services like the contents of the file system or the
|
||||
/// response of some remote server.
|
||||
pub fn get_configs(cli_args: &ArgMatches, log: &Logger) -> Result<Config> {
|
||||
let mut builder = ConfigBuilder::new(cli_args, log)?;
|
||||
|
||||
match cli_args.subcommand() {
|
||||
("testnet", Some(sub_cmd_args)) => {
|
||||
process_testnet_subcommand(&mut builder, sub_cmd_args, log)?
|
||||
}
|
||||
// No sub-command assumes a resume operation.
|
||||
_ => {
|
||||
info!(
|
||||
log,
|
||||
"Resuming from existing datadir";
|
||||
"path" => format!("{:?}", builder.client_config.data_dir)
|
||||
);
|
||||
|
||||
// If no primary subcommand was given, start the beacon chain from an existing
|
||||
// database.
|
||||
builder.set_beacon_chain_start_method(BeaconChainStartMethod::Resume);
|
||||
|
||||
// Whilst there is no large testnet or mainnet force the user to specify how they want
|
||||
// to start a new chain (e.g., from a genesis YAML file, another node, etc).
|
||||
if !builder.client_config.data_dir.exists() {
|
||||
return Err(
|
||||
"No datadir found. To start a new beacon chain, see `testnet --help`. \
|
||||
Use `--datadir` to specify a different directory"
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
// If the `testnet` command was not provided, attempt to load an existing datadir and
|
||||
// continue with an existing chain.
|
||||
builder.load_from_datadir()?;
|
||||
}
|
||||
};
|
||||
|
||||
builder.build(cli_args)
|
||||
}
|
||||
|
||||
/// Process the `testnet` CLI subcommand arguments, updating the `builder`.
|
||||
fn process_testnet_subcommand(
|
||||
builder: &mut ConfigBuilder,
|
||||
cli_args: &ArgMatches,
|
||||
log: &Logger,
|
||||
) -> Result<()> {
|
||||
if cli_args.is_present("random-datadir") {
|
||||
builder.set_random_datadir()?;
|
||||
}
|
||||
|
||||
let is_bootstrap = cli_args.subcommand_name() == Some("bootstrap");
|
||||
|
||||
if let Some(path_string) = cli_args.value_of("eth2-config") {
|
||||
if is_bootstrap {
|
||||
return Err("Cannot supply --eth2-config when using bootsrap".to_string());
|
||||
}
|
||||
|
||||
let path = path_string
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Unable to parse eth2-config path: {:?}", e))?;
|
||||
builder.load_eth2_config(path)?;
|
||||
} else {
|
||||
builder.update_spec_from_subcommand(&cli_args)?;
|
||||
}
|
||||
|
||||
if let Some(path_string) = cli_args.value_of("client-config") {
|
||||
let path = path_string
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Unable to parse client config path: {:?}", e))?;
|
||||
builder.load_client_config(path)?;
|
||||
}
|
||||
|
||||
if cli_args.is_present("force") {
|
||||
builder.clean_datadir()?;
|
||||
}
|
||||
|
||||
info!(
|
||||
log,
|
||||
"Creating new datadir";
|
||||
"path" => format!("{:?}", builder.client_config.data_dir)
|
||||
);
|
||||
|
||||
// When using the testnet command we listen on all addresses.
|
||||
builder.set_listen_addresses("0.0.0.0".into())?;
|
||||
warn!(log, "All services listening on 0.0.0.0");
|
||||
|
||||
// Start matching on the second subcommand (e.g., `testnet bootstrap ...`).
|
||||
match cli_args.subcommand() {
|
||||
("bootstrap", Some(cli_args)) => {
|
||||
let server = cli_args
|
||||
.value_of("server")
|
||||
.ok_or_else(|| "No bootstrap server specified")?;
|
||||
let port: Option<u16> = cli_args
|
||||
.value_of("libp2p-port")
|
||||
.and_then(|s| s.parse::<u16>().ok());
|
||||
|
||||
builder.import_bootstrap_libp2p_address(server, port)?;
|
||||
builder.import_bootstrap_eth2_config(server)?;
|
||||
|
||||
builder.set_beacon_chain_start_method(BeaconChainStartMethod::HttpBootstrap {
|
||||
server: server.to_string(),
|
||||
port,
|
||||
})
|
||||
}
|
||||
("recent", Some(cli_args)) => {
|
||||
let validator_count = cli_args
|
||||
.value_of("validator_count")
|
||||
.ok_or_else(|| "No validator_count specified")?
|
||||
.parse::<usize>()
|
||||
.map_err(|e| format!("Unable to parse validator_count: {:?}", e))?;
|
||||
|
||||
let minutes = cli_args
|
||||
.value_of("minutes")
|
||||
.ok_or_else(|| "No recent genesis minutes supplied")?
|
||||
.parse::<u64>()
|
||||
.map_err(|e| format!("Unable to parse minutes: {:?}", e))?;
|
||||
|
||||
builder.set_beacon_chain_start_method(BeaconChainStartMethod::RecentGenesis {
|
||||
validator_count,
|
||||
minutes,
|
||||
})
|
||||
}
|
||||
("quick", Some(cli_args)) => {
|
||||
let validator_count = cli_args
|
||||
.value_of("validator_count")
|
||||
.ok_or_else(|| "No validator_count specified")?
|
||||
.parse::<usize>()
|
||||
.map_err(|e| format!("Unable to parse validator_count: {:?}", e))?;
|
||||
|
||||
let genesis_time = cli_args
|
||||
.value_of("genesis_time")
|
||||
.ok_or_else(|| "No genesis time supplied")?
|
||||
.parse::<u64>()
|
||||
.map_err(|e| format!("Unable to parse genesis time: {:?}", e))?;
|
||||
|
||||
builder.set_beacon_chain_start_method(BeaconChainStartMethod::Generated {
|
||||
validator_count,
|
||||
genesis_time,
|
||||
})
|
||||
}
|
||||
_ => return Err("No testnet method specified. See 'testnet --help'.".into()),
|
||||
};
|
||||
|
||||
builder.write_configs_to_new_datadir()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Allows for building a set of configurations based upon `clap` arguments.
|
||||
struct ConfigBuilder<'a> {
|
||||
log: &'a Logger,
|
||||
eth2_config: Eth2Config,
|
||||
client_config: ClientConfig,
|
||||
}
|
||||
|
||||
impl<'a> ConfigBuilder<'a> {
|
||||
/// Create a new builder with default settings.
|
||||
pub fn new(cli_args: &'a ArgMatches, log: &'a Logger) -> Result<Self> {
|
||||
// Read the `--datadir` flag.
|
||||
//
|
||||
// If it's not present, try and find the home directory (`~`) and push the default data
|
||||
// directory onto it.
|
||||
let data_dir: PathBuf = cli_args
|
||||
.value_of("datadir")
|
||||
.map(|string| PathBuf::from(string))
|
||||
.or_else(|| {
|
||||
dirs::home_dir().map(|mut home| {
|
||||
home.push(DEFAULT_DATA_DIR);
|
||||
home
|
||||
})
|
||||
})
|
||||
.ok_or_else(|| "Unable to find a home directory for the datadir".to_string())?;
|
||||
|
||||
let mut client_config = ClientConfig::default();
|
||||
client_config.data_dir = data_dir;
|
||||
|
||||
Ok(Self {
|
||||
log,
|
||||
eth2_config: Eth2Config::minimal(),
|
||||
client_config,
|
||||
})
|
||||
}
|
||||
|
||||
/// Clears any configuration files that would interfere with writing new configs.
|
||||
///
|
||||
/// Moves the following files in `data_dir` into a backup directory:
|
||||
///
|
||||
/// - Client config
|
||||
/// - Eth2 config
|
||||
/// - The entire database directory
|
||||
pub fn clean_datadir(&mut self) -> Result<()> {
|
||||
let backup_dir = {
|
||||
let mut s = String::from("backup_");
|
||||
s.push_str(&random_string(6));
|
||||
self.client_config.data_dir.join(s)
|
||||
};
|
||||
|
||||
fs::create_dir_all(&backup_dir)
|
||||
.map_err(|e| format!("Unable to create config backup dir: {:?}", e))?;
|
||||
|
||||
let move_to_backup_dir = |path: &Path| -> Result<()> {
|
||||
let file_name = path
|
||||
.file_name()
|
||||
.ok_or_else(|| "Invalid path found during datadir clean (no filename).")?;
|
||||
|
||||
let mut new = path.to_path_buf();
|
||||
new.pop();
|
||||
new.push(backup_dir.clone());
|
||||
new.push(file_name);
|
||||
|
||||
let _ = fs::rename(path, new);
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
||||
move_to_backup_dir(&self.client_config.data_dir.join(CLIENT_CONFIG_FILENAME))?;
|
||||
move_to_backup_dir(&self.client_config.data_dir.join(ETH2_CONFIG_FILENAME))?;
|
||||
|
||||
if let Some(db_path) = self.client_config.db_path() {
|
||||
move_to_backup_dir(&db_path)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the method for starting the beacon chain.
|
||||
pub fn set_beacon_chain_start_method(&mut self, method: BeaconChainStartMethod) {
|
||||
self.client_config.beacon_chain_start_method = method;
|
||||
}
|
||||
|
||||
/// Import the libp2p address for `server` into the list of bootnodes in `self`.
|
||||
///
|
||||
/// If `port` is `Some`, it is used as the port for the `Multiaddr`. If `port` is `None`,
|
||||
/// attempts to connect to the `server` via HTTP and retrieve it's libp2p listen port.
|
||||
pub fn import_bootstrap_libp2p_address(
|
||||
&mut self,
|
||||
server: &str,
|
||||
port: Option<u16>,
|
||||
) -> Result<()> {
|
||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())?;
|
||||
|
||||
if let Some(server_multiaddr) = bootstrapper.best_effort_multiaddr(port) {
|
||||
info!(
|
||||
self.log,
|
||||
"Estimated bootstrapper libp2p address";
|
||||
"multiaddr" => format!("{:?}", server_multiaddr)
|
||||
);
|
||||
|
||||
self.client_config
|
||||
.network
|
||||
.libp2p_nodes
|
||||
.push(server_multiaddr);
|
||||
} else {
|
||||
warn!(
|
||||
self.log,
|
||||
"Unable to estimate a bootstrapper libp2p address, this node may not find any peers."
|
||||
);
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set the config data_dir to be an random directory.
|
||||
///
|
||||
/// Useful for easily spinning up ephemeral testnets.
|
||||
pub fn set_random_datadir(&mut self) -> Result<()> {
|
||||
self.client_config
|
||||
.data_dir
|
||||
.push(format!("random_{}", random_string(6)));
|
||||
self.client_config.network.network_dir = self.client_config.data_dir.join("network");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Imports an `Eth2Config` from `server`, returning an error if this fails.
|
||||
pub fn import_bootstrap_eth2_config(&mut self, server: &str) -> Result<()> {
|
||||
let bootstrapper = Bootstrapper::from_server_string(server.to_string())?;
|
||||
|
||||
self.update_eth2_config(bootstrapper.eth2_config()?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update_eth2_config(&mut self, eth2_config: Eth2Config) {
|
||||
self.eth2_config = eth2_config;
|
||||
}
|
||||
|
||||
/// Reads the subcommand and tries to update `self.eth2_config` based up on the `--spec` flag.
|
||||
///
|
||||
/// Returns an error if the `--spec` flag is not present in the given `cli_args`.
|
||||
pub fn update_spec_from_subcommand(&mut self, cli_args: &ArgMatches) -> Result<()> {
|
||||
// Re-initialise the `Eth2Config`.
|
||||
//
|
||||
// If a CLI parameter is set, overwrite any config file present.
|
||||
// If a parameter is not set, use either the config file present or default to minimal.
|
||||
let eth2_config = match cli_args.value_of("spec") {
|
||||
Some("mainnet") => Eth2Config::mainnet(),
|
||||
Some("minimal") => Eth2Config::minimal(),
|
||||
Some("interop") => Eth2Config::interop(),
|
||||
_ => return Err("Unable to determine specification type.".into()),
|
||||
};
|
||||
|
||||
self.client_config.spec_constants = cli_args
|
||||
.value_of("spec")
|
||||
.expect("Guarded by prior match statement")
|
||||
.to_string();
|
||||
self.eth2_config = eth2_config;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes the configs in `self` to `self.data_dir`.
|
||||
///
|
||||
/// Returns an error if `self.data_dir` already exists.
|
||||
pub fn write_configs_to_new_datadir(&mut self) -> Result<()> {
|
||||
let db_exists = self
|
||||
.client_config
|
||||
.db_path()
|
||||
.map(|d| d.exists())
|
||||
.unwrap_or_else(|| false);
|
||||
|
||||
// Do not permit creating a new config when the datadir exists.
|
||||
if db_exists {
|
||||
return Err("Database already exists. See `-f` or `-r` in `testnet --help`".into());
|
||||
}
|
||||
|
||||
// Create `datadir` and any non-existing parent directories.
|
||||
fs::create_dir_all(&self.client_config.data_dir).map_err(|e| {
|
||||
crit!(self.log, "Failed to initialize data dir"; "error" => format!("{}", e));
|
||||
format!("{}", e)
|
||||
})?;
|
||||
|
||||
let client_config_file = self.client_config.data_dir.join(CLIENT_CONFIG_FILENAME);
|
||||
if client_config_file.exists() {
|
||||
return Err(format!(
|
||||
"Datadir is not clean, {} exists. See `-f` in `testnet --help`.",
|
||||
CLIENT_CONFIG_FILENAME
|
||||
));
|
||||
} else {
|
||||
// Write the onfig to a TOML file in the datadir.
|
||||
write_to_file(
|
||||
self.client_config.data_dir.join(CLIENT_CONFIG_FILENAME),
|
||||
&self.client_config,
|
||||
)
|
||||
.map_err(|e| format!("Unable to write {} file: {:?}", CLIENT_CONFIG_FILENAME, e))?;
|
||||
}
|
||||
|
||||
let eth2_config_file = self.client_config.data_dir.join(ETH2_CONFIG_FILENAME);
|
||||
if eth2_config_file.exists() {
|
||||
return Err(format!(
|
||||
"Datadir is not clean, {} exists. See `-f` in `testnet --help`.",
|
||||
ETH2_CONFIG_FILENAME
|
||||
));
|
||||
} else {
|
||||
// Write the config to a TOML file in the datadir.
|
||||
write_to_file(
|
||||
self.client_config.data_dir.join(ETH2_CONFIG_FILENAME),
|
||||
&self.eth2_config,
|
||||
)
|
||||
.map_err(|e| format!("Unable to write {} file: {:?}", ETH2_CONFIG_FILENAME, e))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Attempts to load the client and eth2 configs from `self.data_dir`.
|
||||
///
|
||||
/// Returns an error if any files are not found or are invalid.
|
||||
pub fn load_from_datadir(&mut self) -> Result<()> {
|
||||
// Check to ensure the datadir exists.
|
||||
//
|
||||
// For now we return an error. In the future we may decide to boot a default (e.g.,
|
||||
// public testnet or mainnet).
|
||||
if !self.client_config.data_dir.exists() {
|
||||
return Err(
|
||||
"No datadir found. Either create a new testnet or specify a different `--datadir`."
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
// If there is a path to a databse in the config, ensure it exists.
|
||||
if !self
|
||||
.client_config
|
||||
.db_path()
|
||||
.map(|path| path.exists())
|
||||
.unwrap_or_else(|| true)
|
||||
{
|
||||
return Err(
|
||||
"No database found in datadir. Use 'testnet -f' to overwrite the existing \
|
||||
datadir, or specify a different `--datadir`."
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
self.load_eth2_config(self.client_config.data_dir.join(ETH2_CONFIG_FILENAME))?;
|
||||
self.load_client_config(self.client_config.data_dir.join(CLIENT_CONFIG_FILENAME))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Attempts to load the client config from `path`.
|
||||
///
|
||||
/// Returns an error if any files are not found or are invalid.
|
||||
pub fn load_client_config(&mut self, path: PathBuf) -> Result<()> {
|
||||
self.client_config = read_from_file::<ClientConfig>(path.clone())
|
||||
.map_err(|e| format!("Unable to parse {:?} file: {:?}", path, e))?
|
||||
.ok_or_else(|| format!("{:?} file does not exist", path))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Attempts to load the eth2 config from `path`.
|
||||
///
|
||||
/// Returns an error if any files are not found or are invalid.
|
||||
pub fn load_eth2_config(&mut self, path: PathBuf) -> Result<()> {
|
||||
self.eth2_config = read_from_file::<Eth2Config>(path.clone())
|
||||
.map_err(|e| format!("Unable to parse {:?} file: {:?}", path, e))?
|
||||
.ok_or_else(|| format!("{:?} file does not exist", path))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets all listening addresses to the given `addr`.
|
||||
pub fn set_listen_addresses(&mut self, addr: String) -> Result<()> {
|
||||
let addr = addr
|
||||
.parse::<Ipv4Addr>()
|
||||
.map_err(|e| format!("Unable to parse default listen address: {:?}", e))?;
|
||||
|
||||
self.client_config.network.listen_address = addr.clone().into();
|
||||
self.client_config.rpc.listen_address = addr.clone();
|
||||
self.client_config.rest_api.listen_address = addr.clone();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Consumes self, returning the configs.
|
||||
///
|
||||
/// The supplied `cli_args` should be the base-level `clap` cli_args (i.e., not a subcommand
|
||||
/// cli_args).
|
||||
pub fn build(mut self, cli_args: &ArgMatches) -> Result<Config> {
|
||||
self.eth2_config.apply_cli_args(cli_args)?;
|
||||
self.client_config
|
||||
.apply_cli_args(cli_args, &mut self.log.clone())?;
|
||||
|
||||
if let Some(bump) = cli_args.value_of("port-bump") {
|
||||
let bump = bump
|
||||
.parse::<u16>()
|
||||
.map_err(|e| format!("Unable to parse port bump: {}", e))?;
|
||||
|
||||
self.client_config.network.libp2p_port += bump;
|
||||
self.client_config.network.discovery_port += bump;
|
||||
self.client_config.rpc.port += bump;
|
||||
self.client_config.rest_api.port += bump;
|
||||
}
|
||||
|
||||
if self.eth2_config.spec_constants != self.client_config.spec_constants {
|
||||
crit!(self.log, "Specification constants do not match.";
|
||||
"client_config" => format!("{}", self.client_config.spec_constants),
|
||||
"eth2_config" => format!("{}", self.eth2_config.spec_constants)
|
||||
);
|
||||
return Err("Specification constant mismatch".into());
|
||||
}
|
||||
|
||||
Ok((self.client_config, self.eth2_config))
|
||||
}
|
||||
}
|
||||
|
||||
fn random_string(len: usize) -> String {
|
||||
rand::thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(len)
|
||||
.collect::<String>()
|
||||
}
|
||||
@@ -1,12 +1,10 @@
|
||||
mod config;
|
||||
mod run;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use client::{ClientConfig, Eth2Config};
|
||||
use clap::{App, Arg, SubCommand};
|
||||
use config::get_configs;
|
||||
use env_logger::{Builder, Env};
|
||||
use eth2_config::{read_from_file, write_to_file};
|
||||
use slog::{crit, o, warn, Drain, Level};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub const DEFAULT_DATA_DIR: &str = ".lighthouse";
|
||||
pub const CLIENT_CONFIG_FILENAME: &str = "beacon-node.toml";
|
||||
@@ -30,6 +28,7 @@ fn main() {
|
||||
.value_name("DIR")
|
||||
.help("Data directory for keys and databases.")
|
||||
.takes_value(true)
|
||||
.global(true)
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("logfile")
|
||||
@@ -44,22 +43,34 @@ fn main() {
|
||||
.value_name("NETWORK-DIR")
|
||||
.help("Data directory for network keys.")
|
||||
.takes_value(true)
|
||||
.global(true)
|
||||
)
|
||||
/*
|
||||
* Network parameters.
|
||||
*/
|
||||
.arg(
|
||||
Arg::with_name("port-bump")
|
||||
.long("port-bump")
|
||||
.short("b")
|
||||
.value_name("INCREMENT")
|
||||
.help("Sets all listening TCP/UDP ports to default values, but with each port increased by \
|
||||
INCREMENT. Useful when starting multiple nodes on a single machine. Using increments \
|
||||
in multiples of 10 is recommended.")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("listen-address")
|
||||
.long("listen-address")
|
||||
.value_name("ADDRESS")
|
||||
.help("The address lighthouse will listen for UDP and TCP connections. (default 127.0.0.1).")
|
||||
.takes_value(true),
|
||||
.takes_value(true)
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("port")
|
||||
.long("port")
|
||||
.value_name("PORT")
|
||||
.help("The TCP/UDP port to listen on. The UDP port can be modified by the --discovery-port flag.")
|
||||
.conflicts_with("port-bump")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
@@ -81,6 +92,7 @@ fn main() {
|
||||
.long("disc-port")
|
||||
.value_name("PORT")
|
||||
.help("The discovery UDP port.")
|
||||
.conflicts_with("port-bump")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
@@ -108,10 +120,9 @@ fn main() {
|
||||
* gRPC parameters.
|
||||
*/
|
||||
.arg(
|
||||
Arg::with_name("rpc")
|
||||
.long("rpc")
|
||||
.value_name("RPC")
|
||||
.help("Enable the RPC server.")
|
||||
Arg::with_name("no-grpc")
|
||||
.long("no-grpc")
|
||||
.help("Disable the gRPC server.")
|
||||
.takes_value(false),
|
||||
)
|
||||
.arg(
|
||||
@@ -125,14 +136,14 @@ fn main() {
|
||||
Arg::with_name("rpc-port")
|
||||
.long("rpc-port")
|
||||
.help("Listen port for RPC endpoint.")
|
||||
.conflicts_with("port-bump")
|
||||
.takes_value(true),
|
||||
)
|
||||
/* Client related arguments */
|
||||
.arg(
|
||||
Arg::with_name("api")
|
||||
.long("api")
|
||||
.value_name("API")
|
||||
.help("Enable the RESTful HTTP API server.")
|
||||
Arg::with_name("no-api")
|
||||
.long("no-api")
|
||||
.help("Disable RESTful HTTP API server.")
|
||||
.takes_value(false),
|
||||
)
|
||||
.arg(
|
||||
@@ -147,6 +158,7 @@ fn main() {
|
||||
.long("api-port")
|
||||
.value_name("APIPORT")
|
||||
.help("Set the listen TCP port for the RESTful HTTP API server.")
|
||||
.conflicts_with("port-bump")
|
||||
.takes_value(true),
|
||||
)
|
||||
|
||||
@@ -160,25 +172,7 @@ fn main() {
|
||||
.help("Type of database to use.")
|
||||
.takes_value(true)
|
||||
.possible_values(&["disk", "memory"])
|
||||
.default_value("memory"),
|
||||
)
|
||||
/*
|
||||
* Specification/testnet params.
|
||||
*/
|
||||
.arg(
|
||||
Arg::with_name("default-spec")
|
||||
.long("default-spec")
|
||||
.value_name("TITLE")
|
||||
.short("default-spec")
|
||||
.help("Specifies the default eth2 spec to be used. This will override any spec written to disk and will therefore be used by default in future instances.")
|
||||
.takes_value(true)
|
||||
.possible_values(&["mainnet", "minimal", "interop"])
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("recent-genesis")
|
||||
.long("recent-genesis")
|
||||
.short("r")
|
||||
.help("When present, genesis will be within 30 minutes prior. Only for testing"),
|
||||
.default_value("disk"),
|
||||
)
|
||||
/*
|
||||
* Logging.
|
||||
@@ -200,14 +194,124 @@ fn main() {
|
||||
.takes_value(true),
|
||||
)
|
||||
/*
|
||||
* Bootstrap.
|
||||
* The "testnet" sub-command.
|
||||
*
|
||||
* Allows for creating a new datadir with testnet-specific configs.
|
||||
*/
|
||||
.arg(
|
||||
Arg::with_name("bootstrap")
|
||||
.long("bootstrap")
|
||||
.value_name("HTTP_SERVER")
|
||||
.help("Load the genesis state and libp2p address from the HTTP API of another Lighthouse node.")
|
||||
.takes_value(true)
|
||||
.subcommand(SubCommand::with_name("testnet")
|
||||
.about("Create a new Lighthouse datadir using a testnet strategy.")
|
||||
.arg(
|
||||
Arg::with_name("spec")
|
||||
.short("s")
|
||||
.long("spec")
|
||||
.value_name("TITLE")
|
||||
.help("Specifies the default eth2 spec type. Only effective when creating a new datadir.")
|
||||
.takes_value(true)
|
||||
.required(true)
|
||||
.possible_values(&["mainnet", "minimal", "interop"])
|
||||
.default_value("minimal")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("eth2-config")
|
||||
.long("eth2-config")
|
||||
.value_name("TOML_FILE")
|
||||
.help("A existing eth2_spec TOML file (e.g., eth2_spec.toml).")
|
||||
.takes_value(true)
|
||||
.conflicts_with("spec")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("client-config")
|
||||
.long("client-config")
|
||||
.value_name("TOML_FILE")
|
||||
.help("An existing beacon_node TOML file (e.g., beacon_node.toml).")
|
||||
.takes_value(true)
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("random-datadir")
|
||||
.long("random-datadir")
|
||||
.short("r")
|
||||
.help("If present, append a random string to the datadir path. Useful for fast development \
|
||||
iteration.")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("force")
|
||||
.long("force")
|
||||
.short("f")
|
||||
.help("If present, will create new config and database files and move the any existing to a \
|
||||
backup directory.")
|
||||
.conflicts_with("random-datadir")
|
||||
)
|
||||
/*
|
||||
* `boostrap`
|
||||
*
|
||||
* Start a new node by downloading genesis and network info from another node via the
|
||||
* HTTP API.
|
||||
*/
|
||||
.subcommand(SubCommand::with_name("bootstrap")
|
||||
.about("Connects to the given HTTP server, downloads a genesis state and attempts to peer with it.")
|
||||
.arg(Arg::with_name("server")
|
||||
.value_name("HTTP_SERVER")
|
||||
.required(true)
|
||||
.help("A HTTP server, with a http:// prefix"))
|
||||
.arg(Arg::with_name("libp2p-port")
|
||||
.short("p")
|
||||
.long("port")
|
||||
.value_name("TCP_PORT")
|
||||
.help("A libp2p listen port used to peer with the bootstrap server. This flag is useful \
|
||||
when port-fowarding is used: you may connect using a different port than \
|
||||
the one the server is immediately listening on."))
|
||||
)
|
||||
/*
|
||||
* `recent`
|
||||
*
|
||||
* Start a new node, with a specified number of validators with a genesis time in the last
|
||||
* 30-minutes.
|
||||
*/
|
||||
.subcommand(SubCommand::with_name("recent")
|
||||
.about("Creates a new genesis state where the genesis time was at the previous \
|
||||
MINUTES boundary (e.g., when MINUTES == 30; 12:00, 12:30, 13:00, etc.)")
|
||||
.arg(Arg::with_name("validator_count")
|
||||
.value_name("VALIDATOR_COUNT")
|
||||
.required(true)
|
||||
.help("The number of validators in the genesis state"))
|
||||
.arg(Arg::with_name("minutes")
|
||||
.long("minutes")
|
||||
.short("m")
|
||||
.value_name("MINUTES")
|
||||
.required(true)
|
||||
.default_value("15")
|
||||
.help("The maximum number of minutes that will have elapsed before genesis"))
|
||||
)
|
||||
/*
|
||||
* `quick`
|
||||
*
|
||||
* Start a new node, specifying the number of validators and genesis time
|
||||
*/
|
||||
.subcommand(SubCommand::with_name("quick")
|
||||
.about("Creates a new genesis state from the specified validator count and genesis time. \
|
||||
Compatible with the `quick-start genesis` defined in the eth2.0-pm repo.")
|
||||
.arg(Arg::with_name("validator_count")
|
||||
.value_name("VALIDATOR_COUNT")
|
||||
.required(true)
|
||||
.help("The number of validators in the genesis state"))
|
||||
.arg(Arg::with_name("genesis_time")
|
||||
.value_name("UNIX_EPOCH_SECONDS")
|
||||
.required(true)
|
||||
.help("The genesis time for the given state."))
|
||||
)
|
||||
/*
|
||||
* `yaml`
|
||||
*
|
||||
* Start a new node, using a genesis state loaded from a YAML file
|
||||
*/
|
||||
.subcommand(SubCommand::with_name("yaml")
|
||||
.about("Creates a new datadir where the genesis state is read from YAML. Will fail to parse \
|
||||
a YAML state that was generated to a different spec than that specified by --spec.")
|
||||
.arg(Arg::with_name("file")
|
||||
.value_name("YAML_FILE")
|
||||
.required(true)
|
||||
.help("A YAML file from which to read the state"))
|
||||
)
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
@@ -227,143 +331,31 @@ fn main() {
|
||||
_ => unreachable!("guarded by clap"),
|
||||
};
|
||||
|
||||
let mut log = slog::Logger::root(drain.fuse(), o!());
|
||||
let drain = match matches.occurrences_of("verbosity") {
|
||||
0 => drain.filter_level(Level::Info),
|
||||
1 => drain.filter_level(Level::Debug),
|
||||
2 => drain.filter_level(Level::Trace),
|
||||
_ => drain.filter_level(Level::Trace),
|
||||
};
|
||||
|
||||
let log = slog::Logger::root(drain.fuse(), o!());
|
||||
|
||||
warn!(
|
||||
log,
|
||||
"Ethereum 2.0 is pre-release. This software is experimental."
|
||||
);
|
||||
|
||||
let data_dir = match matches
|
||||
.value_of("datadir")
|
||||
.and_then(|v| Some(PathBuf::from(v)))
|
||||
{
|
||||
Some(v) => v,
|
||||
None => {
|
||||
// use the default
|
||||
let mut default_dir = match dirs::home_dir() {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
crit!(log, "Failed to find a home directory");
|
||||
return;
|
||||
}
|
||||
};
|
||||
default_dir.push(DEFAULT_DATA_DIR);
|
||||
default_dir
|
||||
}
|
||||
};
|
||||
|
||||
// create the directory if needed
|
||||
match fs::create_dir_all(&data_dir) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
crit!(log, "Failed to initialize data dir"; "error" => format!("{}", e));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let client_config_path = data_dir.join(CLIENT_CONFIG_FILENAME);
|
||||
|
||||
// Attempt to load the `ClientConfig` from disk.
|
||||
// Load the process-wide configuration.
|
||||
//
|
||||
// If file doesn't exist, create a new, default one.
|
||||
let mut client_config = match read_from_file::<ClientConfig>(client_config_path.clone()) {
|
||||
Ok(Some(c)) => c,
|
||||
Ok(None) => {
|
||||
let default = ClientConfig::default();
|
||||
if let Err(e) = write_to_file(client_config_path, &default) {
|
||||
crit!(log, "Failed to write default ClientConfig to file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
default
|
||||
}
|
||||
// May load this from disk or create a new configuration, depending on the CLI flags supplied.
|
||||
let (client_config, eth2_config) = match get_configs(&matches, &log) {
|
||||
Ok(configs) => configs,
|
||||
Err(e) => {
|
||||
crit!(log, "Failed to load a ChainConfig file"; "error" => format!("{:?}", e));
|
||||
crit!(log, "Failed to load configuration"; "error" => e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Ensure the `data_dir` in the config matches that supplied to the CLI.
|
||||
client_config.data_dir = data_dir.clone();
|
||||
|
||||
// Update the client config with any CLI args.
|
||||
match client_config.apply_cli_args(&matches, &mut log) {
|
||||
Ok(()) => (),
|
||||
Err(s) => {
|
||||
crit!(log, "Failed to parse ClientConfig CLI arguments"; "error" => s);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let eth2_config_path = data_dir.join(ETH2_CONFIG_FILENAME);
|
||||
|
||||
// Initialise the `Eth2Config`.
|
||||
//
|
||||
// If a CLI parameter is set, overwrite any config file present.
|
||||
// If a parameter is not set, use either the config file present or default to minimal.
|
||||
let cli_config = match matches.value_of("default-spec") {
|
||||
Some("mainnet") => Some(Eth2Config::mainnet()),
|
||||
Some("minimal") => Some(Eth2Config::minimal()),
|
||||
Some("interop") => Some(Eth2Config::interop()),
|
||||
_ => None,
|
||||
};
|
||||
// if a CLI flag is specified, write the new config if it doesn't exist,
|
||||
// otherwise notify the user that the file will not be written.
|
||||
let eth2_config_from_file = match read_from_file::<Eth2Config>(eth2_config_path.clone()) {
|
||||
Ok(config) => config,
|
||||
Err(e) => {
|
||||
crit!(log, "Failed to read the Eth2Config from file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut eth2_config = {
|
||||
if let Some(cli_config) = cli_config {
|
||||
if eth2_config_from_file.is_none() {
|
||||
// write to file if one doesn't exist
|
||||
if let Err(e) = write_to_file(eth2_config_path, &cli_config) {
|
||||
crit!(log, "Failed to write default Eth2Config to file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
warn!(
|
||||
log,
|
||||
"Eth2Config file exists. Configuration file is ignored, using default"
|
||||
);
|
||||
}
|
||||
cli_config
|
||||
} else {
|
||||
// CLI config not specified, read from disk
|
||||
match eth2_config_from_file {
|
||||
Some(config) => config,
|
||||
None => {
|
||||
// set default to minimal
|
||||
let eth2_config = Eth2Config::minimal();
|
||||
if let Err(e) = write_to_file(eth2_config_path, ð2_config) {
|
||||
crit!(log, "Failed to write default Eth2Config to file"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
}
|
||||
eth2_config
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Update the eth2 config with any CLI flags.
|
||||
match eth2_config.apply_cli_args(&matches) {
|
||||
Ok(()) => (),
|
||||
Err(s) => {
|
||||
crit!(log, "Failed to parse Eth2Config CLI arguments"; "error" => s);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// check to ensure the spec constants between the client and eth2_config match
|
||||
if eth2_config.spec_constants != client_config.spec_constants {
|
||||
crit!(log, "Specification constants do not match."; "client_config" => format!("{}", client_config.spec_constants), "eth2_config" => format!("{}", eth2_config.spec_constants));
|
||||
return;
|
||||
}
|
||||
|
||||
// Start the node using a `tokio` executor.
|
||||
match run::run_beacon_node(client_config, eth2_config, &log) {
|
||||
Ok(_) => {}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use client::{
|
||||
error, notifier, BeaconChainTypes, Client, ClientConfig, ClientType, Eth2Config,
|
||||
InitialiseBeaconChain,
|
||||
};
|
||||
use client::{error, notifier, BeaconChainTypes, Client, ClientConfig, ClientType, Eth2Config};
|
||||
use futures::sync::oneshot;
|
||||
use futures::Future;
|
||||
use slog::{error, info};
|
||||
@@ -44,12 +41,10 @@ pub fn run_beacon_node(
|
||||
|
||||
info!(
|
||||
log,
|
||||
"BeaconNode init";
|
||||
"p2p_listen_address" => format!("{:?}", &other_client_config.network.listen_address),
|
||||
"data_dir" => format!("{:?}", other_client_config.data_dir()),
|
||||
"network_dir" => format!("{:?}", other_client_config.network.network_dir),
|
||||
"spec_constants" => &spec_constants,
|
||||
"Starting beacon node";
|
||||
"p2p_listen_address" => format!("{}", &other_client_config.network.listen_address),
|
||||
"db_type" => &other_client_config.db_type,
|
||||
"spec_constants" => &spec_constants,
|
||||
);
|
||||
|
||||
match (db_type.as_str(), spec_constants.as_str()) {
|
||||
@@ -118,7 +113,7 @@ fn run<T>(
|
||||
log: &slog::Logger,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
T: BeaconChainTypes + InitialiseBeaconChain<T> + Clone,
|
||||
T: BeaconChainTypes + Clone,
|
||||
T::Store: OpenDatabase,
|
||||
{
|
||||
let store = T::Store::open_database(&db_path)?;
|
||||
|
||||
Reference in New Issue
Block a user