lighthouse/beacon_node/fork/src/lib.rs
Age Manning 58111cddb2
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
2020-03-24 21:45:53 +11:00

76 lines
2.2 KiB
Rust

///! Maintains a hard-coded list of known forks and their slots at which they were activated.
use types::{ChainSpec, Epoch, EthSpec, Slot};
mod forks;
/// A state-less function that provides the fork version given a set of active forks and a slot
/// number.
///
/// The disabled_forks parameter select which forks are disabled by their name.
pub fn current_fork_version(slot: Slot, disabled_forks: &[String]) -> [u8; 4] {
let mut version = [0, 0, 0, 0];
for (fork_name, fork_slot_no, fork_version) in forks::KNOWN_FORKS.iter() {
if *fork_slot_no <= slot.as_u64() {
if disabled_forks
.iter()
.find(|fork| **fork == String::from(*fork_name))
.is_none()
{
version = fork_version.clone();
}
} else {
break;
}
}
version
}
pub fn next_fork_version(slot: Slot, disabled_forks: &[String]) -> [u8; 4] {
let mut version = None;
for (fork_name, fork_slot_no, fork_version) in forks::KNOWN_FORKS.iter() {
if *fork_slot_no > slot.as_u64() {
if disabled_forks
.iter()
.find(|fork| **fork == String::from(*fork_name))
.is_none()
{
version = Some(fork_version.clone());
break;
}
}
}
if let Some(result_version) = version {
result_version
} else {
// if there is no next fork, use the current fork version
current_fork_version(slot, disabled_forks)
}
}
pub fn next_fork_epoch<T: EthSpec>(
spec: &ChainSpec,
slot: Slot,
disabled_forks: &[String],
) -> Epoch {
let mut next_fork_slot = None;
for (fork_name, fork_slot_no, _fork_version) in forks::KNOWN_FORKS.iter() {
if *fork_slot_no > slot.as_u64() {
if disabled_forks
.iter()
.find(|fork| **fork == String::from(*fork_name))
.is_none()
{
next_fork_slot = Some(Slot::new(*fork_slot_no));
break;
}
}
}
if let Some(fork_slot) = next_fork_slot {
fork_slot.epoch(T::slots_per_epoch())
} else {
Epoch::from(spec.far_future_epoch)
}
}