Update beacon_state.rs to spec v0.4.0

This commit is contained in:
Paul Hauner 2019-03-05 18:22:37 +11:00
parent 38a1b94f61
commit 96ec53c6a8
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
7 changed files with 575 additions and 586 deletions

View File

@ -10,6 +10,7 @@ boolean-bitfield = { path = "../utils/boolean-bitfield" }
ethereum-types = "0.4.0" ethereum-types = "0.4.0"
hashing = { path = "../utils/hashing" } hashing = { path = "../utils/hashing" }
honey-badger-split = { path = "../utils/honey-badger-split" } honey-badger-split = { path = "../utils/honey-badger-split" }
int_to_bytes = { path = "../utils/int_to_bytes" }
log = "0.4" log = "0.4"
rayon = "1.0" rayon = "1.0"
rand = "0.5.5" rand = "0.5.5"

File diff suppressed because it is too large Load Diff

View File

View File

@ -93,8 +93,20 @@ pub struct ChainSpec {
pub domain_randao: u64, pub domain_randao: u64,
pub domain_transfer: u64, pub domain_transfer: u64,
} }
impl ChainSpec { impl ChainSpec {
/// Return the number of committees in one epoch.
///
/// Spec v0.4.0
pub fn get_epoch_committee_count(&self, active_validator_count: usize) -> u64 {
std::cmp::max(
1,
std::cmp::min(
self.shard_count / self.slots_per_epoch,
active_validator_count as u64 / self.slots_per_epoch / self.target_committee_size,
),
) * self.slots_per_epoch
}
/// Returns a `ChainSpec` compatible with the Ethereum Foundation specification. /// Returns a `ChainSpec` compatible with the Ethereum Foundation specification.
/// ///
/// Spec v0.4.0 /// Spec v0.4.0
@ -190,9 +202,7 @@ impl ChainSpec {
domain_transfer: 5, domain_transfer: 5,
} }
} }
}
impl ChainSpec {
/// Returns a `ChainSpec` compatible with the specification suitable for 8 validators. /// Returns a `ChainSpec` compatible with the specification suitable for 8 validators.
/// ///
/// Spec v0.4.0 /// Spec v0.4.0

View File

@ -16,6 +16,8 @@ pub struct Fork {
impl Fork { impl Fork {
/// Return the fork version of the given ``epoch``. /// Return the fork version of the given ``epoch``.
///
/// Spec v0.4.0
pub fn get_fork_version(&self, epoch: Epoch) -> u64 { pub fn get_fork_version(&self, epoch: Epoch) -> u64 {
if epoch < self.epoch { if epoch < self.epoch {
return self.previous_version; return self.previous_version;
@ -24,6 +26,8 @@ impl Fork {
} }
/// Get the domain number that represents the fork meta and signature domain. /// Get the domain number that represents the fork meta and signature domain.
///
/// Spec v0.4.0
pub fn get_domain(&self, epoch: Epoch, domain_type: u64) -> u64 { pub fn get_domain(&self, epoch: Epoch, domain_type: u64) -> u64 {
let fork_version = self.get_fork_version(epoch); let fork_version = self.get_fork_version(epoch);
fork_version * u64::pow(2, 32) + domain_type fork_version * u64::pow(2, 32) + domain_type

View File

@ -1,4 +1,4 @@
use crate::{BeaconState, Hash256, Slot}; use crate::{BeaconState, Slot};
use std::fmt::Debug; use std::fmt::Debug;
/// The `BeaconStateReader` provides interfaces for reading a subset of fields of a `BeaconState`. /// The `BeaconStateReader` provides interfaces for reading a subset of fields of a `BeaconState`.
@ -11,7 +11,6 @@ use std::fmt::Debug;
/// "future proofing". /// "future proofing".
pub trait BeaconStateReader: Debug + PartialEq { pub trait BeaconStateReader: Debug + PartialEq {
fn slot(&self) -> Slot; fn slot(&self) -> Slot;
fn canonical_root(&self) -> Hash256;
fn into_beacon_state(self) -> Option<BeaconState>; fn into_beacon_state(self) -> Option<BeaconState>;
} }
@ -20,10 +19,6 @@ impl BeaconStateReader for BeaconState {
self.slot self.slot
} }
fn canonical_root(&self) -> Hash256 {
self.canonical_root()
}
fn into_beacon_state(self) -> Option<BeaconState> { fn into_beacon_state(self) -> Option<BeaconState> {
Some(self) Some(self)
} }

View File

@ -4,6 +4,8 @@ use super::validator::*;
use crate::Epoch; use crate::Epoch;
/// Given an indexed sequence of `validators`, return the indices corresponding to validators that are active at `epoch`. /// Given an indexed sequence of `validators`, return the indices corresponding to validators that are active at `epoch`.
///
/// Spec v0.4.0
pub fn get_active_validator_indices(validators: &[Validator], epoch: Epoch) -> Vec<usize> { pub fn get_active_validator_indices(validators: &[Validator], epoch: Epoch) -> Vec<usize> {
validators validators
.iter() .iter()