mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 10:22:38 +00:00
Adds ENR "eth2" field and Fork logic to networking (#953)
* Merge #913 * Correct release tests * Completed release test corrections * Initial work on upgrading discovery * Updates discovery to latest version * Update ENR initialisation logic * Remove debug statements * Shifts timing units to slots * Initial work * Add initial fork versioning and EnrForkId * Correct linking for EnrForkId * Adds eth2 field to local ENR * Initial work to eth2 field integration * Integrate eth2 field into discovery * temp commit * Add a timer to adjust fork versions during a hard fork for the ENR
This commit is contained in:
@@ -14,6 +14,7 @@ use crate::snapshot_cache::SnapshotCache;
|
||||
use crate::timeout_rw_lock::TimeoutRwLock;
|
||||
use crate::validator_pubkey_cache::ValidatorPubkeyCache;
|
||||
use crate::BeaconSnapshot;
|
||||
use ::fork::{next_fork_epoch, next_fork_version};
|
||||
use operation_pool::{OperationPool, PersistedOperationPool};
|
||||
use slog::{crit, debug, error, info, trace, warn, Logger};
|
||||
use slot_clock::SlotClock;
|
||||
@@ -160,6 +161,8 @@ pub struct BeaconChain<T: BeaconChainTypes> {
|
||||
pub(crate) shuffling_cache: TimeoutRwLock<ShufflingCache>,
|
||||
/// Caches a map of `validator_index -> validator_pubkey`.
|
||||
pub(crate) validator_pubkey_cache: TimeoutRwLock<ValidatorPubkeyCache>,
|
||||
/// A list of any hard-coded forks that have been disabled.
|
||||
pub disabled_forks: Vec<String>,
|
||||
/// Logging to CLI, etc.
|
||||
pub(crate) log: Logger,
|
||||
}
|
||||
@@ -2044,6 +2047,54 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
|
||||
Ok(dump)
|
||||
}
|
||||
|
||||
/// Gets the current EnrForkId.
|
||||
///
|
||||
/// v0.11
|
||||
pub fn enr_fork_id(&self) -> Result<EnrForkId, Error> {
|
||||
Ok(EnrForkId {
|
||||
// TODO: To be implemented with v0.11 updates
|
||||
fork_digest: [0, 0, 0, 0],
|
||||
next_fork_version: next_fork_version(self.slot()?, &self.disabled_forks),
|
||||
next_fork_epoch: next_fork_epoch::<T::EthSpec>(
|
||||
&self.spec,
|
||||
self.slot()?,
|
||||
&self.disabled_forks,
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
/// Calculates the duration (in millis) to the next fork, if one exists.
|
||||
///
|
||||
/// This is required by the network thread to instantiate timeouts to update networking
|
||||
/// constants
|
||||
pub fn duration_to_next_fork(&self) -> Result<Option<tokio::timer::Delay>, Error> {
|
||||
let current_slot = self.slot()?;
|
||||
let next_fork_epoch =
|
||||
next_fork_epoch::<T::EthSpec>(&self.spec, current_slot, &self.disabled_forks);
|
||||
if next_fork_epoch != self.spec.far_future_epoch {
|
||||
// There is an upcoming fork
|
||||
let current_epoch = self.slot()?.epoch(T::EthSpec::slots_per_epoch());
|
||||
let epochs_until_fork = next_fork_epoch
|
||||
.saturating_sub(current_epoch)
|
||||
.saturating_sub(1u64);
|
||||
let millis_until_fork = T::EthSpec::slots_per_epoch()
|
||||
* self.spec.milliseconds_per_slot
|
||||
* epochs_until_fork.as_u64();
|
||||
Ok(Some(tokio::timer::Delay::new(
|
||||
Instant::now()
|
||||
+ self
|
||||
.slot_clock
|
||||
.duration_to_next_epoch(T::EthSpec::slots_per_epoch())
|
||||
.unwrap_or_else(|| Duration::from_secs(0))
|
||||
+ Duration::from_millis(millis_until_fork)
|
||||
// add a short timeout to start within the new fork period
|
||||
+ Duration::from_millis(200),
|
||||
)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: BeaconChainTypes> Drop for BeaconChain<T> {
|
||||
|
||||
@@ -87,6 +87,7 @@ pub struct BeaconChainBuilder<T: BeaconChainTypes> {
|
||||
pubkey_cache_path: Option<PathBuf>,
|
||||
validator_pubkey_cache: Option<ValidatorPubkeyCache>,
|
||||
spec: ChainSpec,
|
||||
disabled_forks: Vec<String>,
|
||||
log: Option<Logger>,
|
||||
}
|
||||
|
||||
@@ -121,6 +122,7 @@ where
|
||||
head_tracker: None,
|
||||
pubkey_cache_path: None,
|
||||
data_dir: None,
|
||||
disabled_forks: Vec::new(),
|
||||
validator_pubkey_cache: None,
|
||||
spec: TEthSpec::default_spec(),
|
||||
log: None,
|
||||
@@ -167,6 +169,12 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets a list of hard-coded forks that will not be activated.
|
||||
pub fn disabled_forks(mut self, disabled_forks: Vec<String>) -> Self {
|
||||
self.disabled_forks = disabled_forks;
|
||||
self
|
||||
}
|
||||
|
||||
/// Attempt to load an existing eth1 cache from the builder's `Store`.
|
||||
pub fn get_persisted_eth1_backend(&self) -> Result<Option<SszEth1>, String> {
|
||||
let store = self
|
||||
@@ -425,6 +433,7 @@ where
|
||||
)),
|
||||
shuffling_cache: TimeoutRwLock::new(ShufflingCache::new()),
|
||||
validator_pubkey_cache: TimeoutRwLock::new(validator_pubkey_cache),
|
||||
disabled_forks: self.disabled_forks,
|
||||
log: log.clone(),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user