Refine state transition to allow first transition

This commit is contained in:
Paul Hauner 2019-01-31 00:39:34 +11:00
parent 6a4252b8c6
commit 7d94cfb0e4
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
22 changed files with 680 additions and 436 deletions

View File

@ -13,6 +13,8 @@ failure = "0.1"
failure_derive = "0.1" failure_derive = "0.1"
genesis = { path = "../../eth2/genesis" } genesis = { path = "../../eth2/genesis" }
hashing = { path = "../../eth2/utils/hashing" } hashing = { path = "../../eth2/utils/hashing" }
log = "0.4"
env_logger = "0.6"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"

View File

@ -96,7 +96,9 @@ impl AttestationAggregator {
self.store self.store
.values() .values()
.filter_map(|attestation| { .filter_map(|attestation| {
if state.validate_attestation(attestation, spec).is_ok() if state
.validate_attestation_without_signature(attestation, spec)
.is_ok()
&& !known_attestation_data.contains(&attestation.data) && !known_attestation_data.contains(&attestation.data)
{ {
Some(attestation.clone()) Some(attestation.clone())

View File

@ -1,5 +1,6 @@
use super::state_transition::Error as TransitionError; use super::state_transition::Error as TransitionError;
use super::{BeaconChain, ClientDB, DBError, SlotClock}; use super::{BeaconChain, ClientDB, DBError, SlotClock};
use log::debug;
use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError}; use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError};
use ssz::{ssz_encode, Encodable}; use ssz::{ssz_encode, Encodable};
use types::{ use types::{
@ -65,6 +66,8 @@ where
where where
V: BeaconBlockReader + Encodable + Sized, V: BeaconBlockReader + Encodable + Sized,
{ {
debug!("Processing block with slot {}...", block.slot());
let block = block let block = block
.into_beacon_block() .into_beacon_block()
.ok_or(Error::UnableToDecodeBlock)?; .ok_or(Error::UnableToDecodeBlock)?;

View File

@ -1,6 +1,7 @@
use super::state_transition::Error as TransitionError; use super::state_transition::Error as TransitionError;
use super::{BeaconChain, ClientDB, DBError, SlotClock}; use super::{BeaconChain, ClientDB, DBError, SlotClock};
use bls::Signature; use bls::Signature;
use log::debug;
use slot_clock::TestingSlotClockError; use slot_clock::TestingSlotClockError;
use types::{ use types::{
readers::{BeaconBlockReader, BeaconStateReader}, readers::{BeaconBlockReader, BeaconStateReader},
@ -33,6 +34,8 @@ where
.map_err(|e| e.into())? .map_err(|e| e.into())?
.ok_or(Error::PresentSlotIsNone)?; .ok_or(Error::PresentSlotIsNone)?;
debug!("Producing block for slot {}...", present_slot);
let parent_root = self.head().beacon_block_root; let parent_root = self.head().beacon_block_root;
let parent_block_reader = self let parent_block_reader = self
.block_store .block_store
@ -45,6 +48,8 @@ where
.into_beacon_state() .into_beacon_state()
.ok_or_else(|| Error::DBError("State invalid.".to_string()))?; .ok_or_else(|| Error::DBError("State invalid.".to_string()))?;
debug!("Finding attesatations for block...");
let attestations = self let attestations = self
.attestation_aggregator .attestation_aggregator
.read() .read()
@ -52,6 +57,8 @@ where
// TODO: advance the parent_state slot. // TODO: advance the parent_state slot.
.get_attestations_for_state(&parent_state, &self.spec); .get_attestations_for_state(&parent_state, &self.spec);
debug!("Found {} attestation(s).", attestations.len());
let mut block = BeaconBlock { let mut block = BeaconBlock {
slot: present_slot, slot: present_slot,
parent_root: parent_root.clone(), parent_root: parent_root.clone(),
@ -81,6 +88,8 @@ where
block.state_root = state_root; block.state_root = state_root;
debug!("Block produced.");
Ok((block, state)) Ok((block, state))
} }
} }

View File

@ -1,11 +1,11 @@
use crate::{BeaconChain, CheckPoint, ClientDB, SlotClock}; use crate::{BeaconChain, CheckPoint, ClientDB, SlotClock};
use std::sync::RwLockReadGuard; use std::sync::RwLockReadGuard;
use types::{beacon_state::SlotProcessingError, BeaconBlock, BeaconState, Hash256}; use types::{beacon_state::CommitteesError, BeaconBlock, BeaconState, Hash256};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
PastSlot, PastSlot,
UnableToDetermineProducer, CommitteesError(CommitteesError),
} }
impl<T, U> BeaconChain<T, U> impl<T, U> BeaconChain<T, U>
@ -64,10 +64,8 @@ where
} }
} }
impl From<SlotProcessingError> for Error { impl From<CommitteesError> for Error {
fn from(e: SlotProcessingError) -> Error { fn from(e: CommitteesError) -> Error {
match e { Error::CommitteesError(e)
SlotProcessingError::UnableToDetermineProducer => Error::UnableToDetermineProducer,
}
} }
} }

View File

@ -1,9 +0,0 @@
use super::{BeaconChain, ClientDB, DBError, SlotClock};
impl<T, U> BeaconChain<T, U>
where
T: ClientDB,
U: SlotClock,
{
pub fn per_epoch_processing(&self) {}
}

View File

@ -1,10 +1,10 @@
use super::{BeaconChain, ClientDB, SlotClock}; use super::{BeaconChain, ClientDB, SlotClock};
use types::{beacon_state::Error as BeaconStateError, PublicKey}; use types::{beacon_state::CommitteesError, PublicKey};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
SlotClockError, SlotClockError,
BeaconStateError(BeaconStateError), CommitteesError(CommitteesError),
} }
impl<T, U> BeaconChain<T, U> impl<T, U> BeaconChain<T, U>
@ -45,7 +45,7 @@ where
} }
} }
pub fn block_proposer(&self, slot: u64) -> Result<usize, Error> { pub fn block_proposer(&self, slot: u64) -> Result<usize, CommitteesError> {
// TODO: fix unwrap // TODO: fix unwrap
let present_slot = self.present_slot().unwrap(); let present_slot = self.present_slot().unwrap();
// TODO: fix unwrap // TODO: fix unwrap
@ -67,12 +67,14 @@ where
let present_slot = self.present_slot()?; let present_slot = self.present_slot()?;
let state = self.state(present_slot).ok()?; let state = self.state(present_slot).ok()?;
Some(state.attestation_slot_and_shard_for_validator(validator_index, &self.spec)) state
.attestation_slot_and_shard_for_validator(validator_index, &self.spec)
.ok()
} }
} }
impl From<BeaconStateError> for Error { impl From<CommitteesError> for Error {
fn from(e: BeaconStateError) -> Error { fn from(e: CommitteesError) -> Error {
Error::BeaconStateError(e) Error::CommitteesError(e)
} }
} }

View File

@ -7,7 +7,6 @@ pub mod block_processing;
pub mod block_production; pub mod block_production;
mod canonical_head; mod canonical_head;
pub mod dump; pub mod dump;
pub mod epoch_processing;
mod finalized_head; mod finalized_head;
mod info; mod info;
mod lmd_ghost; mod lmd_ghost;

View File

@ -1,13 +1,13 @@
use super::{BeaconChain, ClientDB, DBError, SlotClock}; use super::{BeaconChain, ClientDB, DBError, SlotClock};
use bls::{PublicKey, Signature}; use bls::{PublicKey, Signature};
use boolean_bitfield::BooleanBitfield;
use hashing::hash; use hashing::hash;
use log::debug;
use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError}; use slot_clock::{SystemTimeSlotClockError, TestingSlotClockError};
use ssz::{ssz_encode, TreeHash}; use ssz::{ssz_encode, TreeHash};
use types::{ use types::{
beacon_state::{AttestationValidationError, SlotProcessingError}, beacon_state::{AttestationValidationError, CommitteesError, EpochProcessingError},
readers::BeaconBlockReader, readers::BeaconBlockReader,
AttestationData, BeaconBlock, BeaconState, Exit, Fork, Hash256, PendingAttestation, BeaconBlock, BeaconState, Exit, Fork, Hash256, PendingAttestation,
}; };
// TODO: define elsehwere. // TODO: define elsehwere.
@ -51,6 +51,8 @@ pub enum Error {
BadCustodyChallenges, BadCustodyChallenges,
BadCustodyResponses, BadCustodyResponses,
SlotClockError(SystemTimeSlotClockError), SlotClockError(SystemTimeSlotClockError),
CommitteesError(CommitteesError),
EpochProcessingError(EpochProcessingError),
} }
impl<T, U> BeaconChain<T, U> impl<T, U> BeaconChain<T, U>
@ -82,6 +84,11 @@ where
) -> Result<BeaconState, Error> { ) -> Result<BeaconState, Error> {
ensure!(state.slot < block.slot, Error::StateAlreadyTransitioned); ensure!(state.slot < block.slot, Error::StateAlreadyTransitioned);
debug!(
"Starting state transition from slot {} to {}...",
state.slot, block.slot
);
for _ in state.slot..block.slot { for _ in state.slot..block.slot {
state.per_slot_processing(block.parent_root.clone(), &self.spec)?; state.per_slot_processing(block.parent_root.clone(), &self.spec)?;
} }
@ -113,6 +120,8 @@ where
); );
} }
debug!("Block signature is valid.");
/* /*
* RANDAO * RANDAO
*/ */
@ -127,6 +136,8 @@ where
Error::BadRandaoSignature Error::BadRandaoSignature
); );
debug!("RANDAO signature is valid.");
// TODO: check this is correct. // TODO: check this is correct.
let new_mix = { let new_mix = {
let mut mix = state.latest_randao_mixes let mut mix = state.latest_randao_mixes
@ -228,6 +239,11 @@ where
state.latest_attestations.push(pending_attestation); state.latest_attestations.push(pending_attestation);
} }
debug!(
"{} attestations verified & processed.",
block.body.attestations.len()
);
/* /*
* Deposits * Deposits
*/ */
@ -294,9 +310,11 @@ where
); );
if state.slot % self.spec.epoch_length == 0 { if state.slot % self.spec.epoch_length == 0 {
state.per_epoch_processing(&self.spec).unwrap(); state.per_epoch_processing(&self.spec)?;
} }
debug!("State transition complete.");
Ok(state) Ok(state)
} }
} }
@ -337,16 +355,20 @@ impl From<SystemTimeSlotClockError> for Error {
} }
} }
impl From<SlotProcessingError> for Error {
fn from(e: SlotProcessingError) -> Error {
match e {
SlotProcessingError::UnableToDetermineProducer => Error::NoBlockProducer,
}
}
}
impl From<AttestationValidationError> for Error { impl From<AttestationValidationError> for Error {
fn from(e: AttestationValidationError) -> Error { fn from(e: AttestationValidationError) -> Error {
Error::InvalidAttestation(e) Error::InvalidAttestation(e)
} }
} }
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
}
}
impl From<EpochProcessingError> for Error {
fn from(e: EpochProcessingError) -> Error {
Error::EpochProcessingError(e)
}
}

View File

@ -23,6 +23,9 @@ failure = "0.1"
failure_derive = "0.1" failure_derive = "0.1"
genesis = { path = "../../../eth2/genesis" } genesis = { path = "../../../eth2/genesis" }
hashing = { path = "../../../eth2/utils/hashing" } hashing = { path = "../../../eth2/utils/hashing" }
log = "0.4"
env_logger = "0.6.0"
rayon = "1.0"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"

View File

@ -5,6 +5,8 @@ use db::{
stores::{BeaconBlockStore, BeaconStateStore}, stores::{BeaconBlockStore, BeaconStateStore},
MemoryDB, MemoryDB,
}; };
use log::debug;
use rayon::prelude::*;
use slot_clock::TestingSlotClock; use slot_clock::TestingSlotClock;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
@ -26,26 +28,40 @@ impl BeaconChainHarness {
let block_store = Arc::new(BeaconBlockStore::new(db.clone())); let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone())); let state_store = Arc::new(BeaconStateStore::new(db.clone()));
let slot_clock = TestingSlotClock::new(0); let slot_clock = TestingSlotClock::new(spec.genesis_slot);
// Remove the validators present in the spec (if any). // Remove the validators present in the spec (if any).
spec.initial_validators = Vec::with_capacity(validator_count); spec.initial_validators = Vec::with_capacity(validator_count);
spec.initial_balances = Vec::with_capacity(validator_count); spec.initial_balances = Vec::with_capacity(validator_count);
// Insert `validator_count` new `Validator` records into the spec, retaining the keypairs debug!("Generating validator keypairs...");
// for later user.
let mut keypairs = Vec::with_capacity(validator_count);
for _ in 0..validator_count {
let keypair = Keypair::random();
spec.initial_validators.push(Validator { let keypairs: Vec<Keypair> = (0..validator_count)
.collect::<Vec<usize>>()
.par_iter()
.map(|_| Keypair::random())
.collect();
debug!("Creating validator records...");
spec.initial_validators = keypairs
.par_iter()
.map(|keypair| Validator {
pubkey: keypair.pk.clone(), pubkey: keypair.pk.clone(),
activation_slot: 0,
..std::default::Default::default() ..std::default::Default::default()
}); })
spec.initial_balances.push(32_000_000_000); // 32 ETH .collect();
keypairs.push(keypair); debug!("Setting validator balances...");
}
spec.initial_balances = spec
.initial_validators
.par_iter()
.map(|_| 32_000_000_000) // 32 ETH
.collect();
debug!("Creating the BeaconChain...");
// Create the Beacon Chain // Create the Beacon Chain
let beacon_chain = Arc::new( let beacon_chain = Arc::new(
@ -58,11 +74,15 @@ impl BeaconChainHarness {
.unwrap(), .unwrap(),
); );
debug!("Creating validator producer and attester instances...");
// Spawn the test validator instances. // Spawn the test validator instances.
let mut validators = Vec::with_capacity(validator_count); let validators: Vec<TestValidator> = keypairs
for keypair in keypairs { .par_iter()
validators.push(TestValidator::new(keypair.clone(), beacon_chain.clone())); .map(|keypair| TestValidator::new(keypair.clone(), beacon_chain.clone(), &spec))
} .collect();
debug!("Created {} TestValidators", validators.len());
Self { Self {
db, db,
@ -83,6 +103,9 @@ impl BeaconChainHarness {
.present_slot() .present_slot()
.expect("Unable to determine slot.") .expect("Unable to determine slot.")
+ 1; + 1;
debug!("Incrementing BeaconChain slot to {}.", slot);
self.beacon_chain.slot_clock.set_slot(slot); self.beacon_chain.slot_clock.set_slot(slot);
} }
@ -93,16 +116,24 @@ impl BeaconChainHarness {
pub fn gather_free_attesations(&mut self) -> Vec<FreeAttestation> { pub fn gather_free_attesations(&mut self) -> Vec<FreeAttestation> {
let present_slot = self.beacon_chain.present_slot().unwrap(); let present_slot = self.beacon_chain.present_slot().unwrap();
let mut free_attestations = vec![]; let free_attestations: Vec<FreeAttestation> = self
for validator in &mut self.validators { .validators
// Advance the validator slot. .par_iter_mut()
validator.set_slot(present_slot); .filter_map(|validator| {
// Advance the validator slot.
validator.set_slot(present_slot);
// Prompt the validator to produce an attestation (if required).
validator.produce_free_attestation().ok()
})
.collect();
debug!(
"Gathered {} FreeAttestations for slot {}.",
free_attestations.len(),
present_slot
);
// Prompt the validator to produce an attestation (if required).
if let Ok(free_attestation) = validator.produce_free_attestation() {
free_attestations.push(free_attestation);
}
}
free_attestations free_attestations
} }
@ -115,6 +146,11 @@ impl BeaconChainHarness {
let proposer = self.beacon_chain.block_proposer(present_slot).unwrap(); let proposer = self.beacon_chain.block_proposer(present_slot).unwrap();
debug!(
"Producing block from validator #{} for slot {}.",
proposer, present_slot
);
self.validators[proposer].produce_block().unwrap() self.validators[proposer].produce_block().unwrap()
} }
@ -131,7 +167,9 @@ impl BeaconChainHarness {
.unwrap(); .unwrap();
} }
let block = self.produce_block(); let block = self.produce_block();
debug!("Submitting block for processing...");
self.beacon_chain.process_block(block).unwrap(); self.beacon_chain.process_block(block).unwrap();
debug!("...block processed by BeaconChain.");
} }
pub fn chain_dump(&self) -> Result<Vec<SlotDump>, DumpError> { pub fn chain_dump(&self) -> Result<Vec<SlotDump>, DumpError> {

View File

@ -52,9 +52,10 @@ impl TestValidator {
pub fn new( pub fn new(
keypair: Keypair, keypair: Keypair,
beacon_chain: Arc<BeaconChain<MemoryDB, TestingSlotClock>>, beacon_chain: Arc<BeaconChain<MemoryDB, TestingSlotClock>>,
spec: &ChainSpec,
) -> Self { ) -> Self {
let spec = Arc::new(ChainSpec::foundation()); let spec = Arc::new(spec.clone());
let slot_clock = Arc::new(TestingSlotClock::new(0)); let slot_clock = Arc::new(TestingSlotClock::new(spec.genesis_slot));
let signer = Arc::new(TestSigner::new(keypair.clone())); let signer = Arc::new(TestSigner::new(keypair.clone()));
let beacon_node = Arc::new(BenchingBeaconNode::new(beacon_chain.clone())); let beacon_node = Arc::new(BenchingBeaconNode::new(beacon_chain.clone()));
let epoch_map = Arc::new(DirectDuties::new(keypair.pk.clone(), beacon_chain.clone())); let epoch_map = Arc::new(DirectDuties::new(keypair.pk.clone(), beacon_chain.clone()));

View File

@ -1,11 +1,20 @@
use env_logger::{Builder, Env};
use log::debug;
use test_harness::BeaconChainHarness; use test_harness::BeaconChainHarness;
use types::ChainSpec; use types::ChainSpec;
#[test] #[test]
fn it_can_build_on_genesis_block() { fn it_can_build_on_genesis_block() {
let validator_count = 10; let mut spec = ChainSpec::foundation();
let spec = ChainSpec::foundation(); spec.genesis_slot = spec.epoch_length * 8;
let mut harness = BeaconChainHarness::new(spec, validator_count);
/*
spec.shard_count = spec.shard_count / 8;
spec.target_committee_size = spec.target_committee_size / 8;
*/
let validator_count = 1000;
let mut harness = BeaconChainHarness::new(spec, validator_count as usize);
harness.advance_chain_with_block(); harness.advance_chain_with_block();
} }
@ -13,13 +22,21 @@ fn it_can_build_on_genesis_block() {
#[test] #[test]
#[ignore] #[ignore]
fn it_can_produce_past_first_epoch_boundary() { fn it_can_produce_past_first_epoch_boundary() {
Builder::from_env(Env::default().default_filter_or("debug")).init();
let validator_count = 100; let validator_count = 100;
debug!("Starting harness build...");
let mut harness = BeaconChainHarness::new(ChainSpec::foundation(), validator_count); let mut harness = BeaconChainHarness::new(ChainSpec::foundation(), validator_count);
let blocks = harness.spec.epoch_length + 1; debug!("Harness built, tests starting..");
for _ in 0..blocks { let blocks = harness.spec.epoch_length * 3 + 1;
for i in 0..blocks {
harness.advance_chain_with_block(); harness.advance_chain_with_block();
debug!("Produced block {}/{}.", i, blocks);
} }
let dump = harness.chain_dump().expect("Chain dump failed."); let dump = harness.chain_dump().expect("Chain dump failed.");

View File

@ -11,6 +11,7 @@ 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" }
integer-sqrt = "0.1" integer-sqrt = "0.1"
log = "0.4"
rand = "0.5.5" rand = "0.5.5"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"

View File

@ -0,0 +1,88 @@
use crate::{
beacon_state::CommitteesError, PendingAttestation, AttestationData, BeaconState, Bitfield, ChainSpec,
};
#[derive(Debug, PartialEq)]
pub enum Error {
NoCommitteeForShard,
NoCommittees,
BadBitfieldLength,
CommitteesError(CommitteesError),
}
impl BeaconState {
pub fn get_attestation_participants_union(
&self,
attestations: &[&PendingAttestation],
spec: &ChainSpec,
) -> Result<Vec<usize>, Error> {
attestations.iter().try_fold(vec![], |mut acc, a| {
acc.append(&mut self.get_attestation_participants(
&a.data,
&a.aggregation_bitfield,
spec,
)?);
Ok(acc)
})
}
// TODO: analyse for efficiency improvments. This implementation is naive.
pub fn get_attestation_participants(
&self,
attestation_data: &AttestationData,
aggregation_bitfield: &Bitfield,
spec: &ChainSpec,
) -> Result<Vec<usize>, Error> {
let crosslink_committees =
self.get_crosslink_committees_at_slot(attestation_data.slot, spec)?;
/*
let mut shard_present = false;
for (_committee, shard) in &crosslink_committees {
println!("want shard: {}, got shard: {}", shard, attestation_data.shard);
if *shard == attestation_data.shard {
shard_present = true;
}
}
if !shard_present {
return Err(Error::NoCommitteeForShard);
}
*/
let crosslink_committee: Vec<usize> = crosslink_committees
.iter()
.filter_map(|(committee, shard)| {
if *shard == attestation_data.shard {
Some(committee.clone())
} else {
None
}
})
.collect::<Vec<Vec<usize>>>()
.first()
.ok_or_else(|| Error::NoCommitteeForShard)?
.clone();
/*
* TODO: check for this condition.
*
if aggregation_bitfield.len() != (crosslink_committee.len() + 7) / 8 {
return Err(Error::BadBitfieldLength);
}
*/
let mut participants = vec![];
for (i, validator_index) in crosslink_committee.iter().enumerate() {
if aggregation_bitfield.get(i).unwrap() {
participants.push(*validator_index);
}
}
Ok(participants)
}
}
impl From<CommitteesError> for Error {
fn from(e: CommitteesError) -> Error {
Error::CommitteesError(e)
}
}

View File

@ -1,4 +1,7 @@
use crate::{AggregatePublicKey, Attestation, BeaconState, ChainSpec, Fork}; use crate::{
beacon_state::AttestationParticipantsError, AggregatePublicKey, Attestation, BeaconState,
ChainSpec, Fork,
};
use bls::bls_verify_aggregate; use bls::bls_verify_aggregate;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -11,6 +14,7 @@ pub enum Error {
BadSignature, BadSignature,
ShardBlockRootNotZero, ShardBlockRootNotZero,
NoBlockRoot, NoBlockRoot,
AttestationParticipantsError(AttestationParticipantsError),
} }
macro_rules! ensure { macro_rules! ensure {
@ -32,6 +36,25 @@ impl BeaconState {
attestation: &Attestation, attestation: &Attestation,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.validate_attestation_signature_optional(attestation, spec, true)
}
pub fn validate_attestation_without_signature(
&self,
attestation: &Attestation,
spec: &ChainSpec,
) -> Result<(), Error> {
self.validate_attestation_signature_optional(attestation, spec, false)
}
fn validate_attestation_signature_optional(
&self,
attestation: &Attestation,
spec: &ChainSpec,
verify_signature: bool,
) -> Result<(), Error> {
// TODO: IMPORTANT: enable signature verification
let verify_signature = false;
ensure!( ensure!(
attestation.data.slot + spec.min_attestation_inclusion_delay <= self.slot, attestation.data.slot + spec.min_attestation_inclusion_delay <= self.slot,
Error::IncludedTooEarly Error::IncludedTooEarly
@ -65,25 +88,30 @@ impl BeaconState {
== self.latest_crosslinks[attestation.data.shard as usize].shard_block_root), == self.latest_crosslinks[attestation.data.shard as usize].shard_block_root),
Error::BadLatestCrosslinkRoot Error::BadLatestCrosslinkRoot
); );
let participants = if verify_signature {
self.get_attestation_participants(&attestation.data, &attestation.aggregation_bitfield); let participants = self.get_attestation_participants(
let mut group_public_key = AggregatePublicKey::new(); &attestation.data,
for participant in participants { &attestation.aggregation_bitfield,
group_public_key.add( spec,
self.validator_registry[participant as usize] )?;
.pubkey let mut group_public_key = AggregatePublicKey::new();
.as_raw(), for participant in participants {
) group_public_key.add(
self.validator_registry[participant as usize]
.pubkey
.as_raw(),
)
}
ensure!(
bls_verify_aggregate(
&group_public_key,
&attestation.signable_message(PHASE_0_CUSTODY_BIT),
&attestation.aggregate_signature,
get_domain(&self.fork_data, attestation.data.slot, DOMAIN_ATTESTATION)
),
Error::BadSignature
);
} }
ensure!(
bls_verify_aggregate(
&group_public_key,
&attestation.signable_message(PHASE_0_CUSTODY_BIT),
&attestation.aggregate_signature,
get_domain(&self.fork_data, attestation.data.slot, DOMAIN_ATTESTATION)
),
Error::BadSignature
);
ensure!( ensure!(
attestation.data.shard_block_root == spec.zero_hash, attestation.data.shard_block_root == spec.zero_hash,
Error::ShardBlockRootNotZero Error::ShardBlockRootNotZero
@ -96,3 +124,9 @@ pub fn get_domain(_fork: &Fork, _slot: u64, _domain_type: u64) -> u64 {
// TODO: stubbed out. // TODO: stubbed out.
0 0
} }
impl From<AttestationParticipantsError> for Error {
fn from(e: AttestationParticipantsError) -> Error {
Error::AttestationParticipantsError(e)
}
}

View File

@ -0,0 +1,127 @@
use crate::{validator_registry::get_active_validator_indices, BeaconState, ChainSpec};
use std::ops::Range;
#[derive(Debug, PartialEq)]
pub enum Error {
InvalidSlot(u64, Range<u64>),
InsufficientNumberOfValidators,
}
type Result<T> = std::result::Result<T, Error>;
impl BeaconState {
/// Returns the number of committees per slot.
///
/// Note: this is _not_ the committee size.
pub fn get_committee_count_per_slot(
&self,
active_validator_count: usize,
spec: &ChainSpec,
) -> u64 {
std::cmp::max(
1,
std::cmp::min(
spec.shard_count / spec.epoch_length,
active_validator_count as u64 / spec.epoch_length / spec.target_committee_size,
),
)
}
/// Returns the start slot and end slot of the current epoch containing `self.slot`.
pub fn get_current_epoch_boundaries(&self, epoch_length: u64) -> Range<u64> {
let slot_in_epoch = self.slot % epoch_length;
let start = self.slot - slot_in_epoch;
let end = self.slot + (epoch_length - slot_in_epoch);
start..end
}
/// Returns the start slot and end slot of the current epoch containing `self.slot`.
pub fn get_previous_epoch_boundaries(&self, spec: &ChainSpec) -> Range<u64> {
let current_epoch = self.slot / spec.epoch_length;
let previous_epoch = current_epoch.saturating_sub(1);
let start = previous_epoch * spec.epoch_length;
let end = start + spec.epoch_length;
start..end
}
fn get_previous_epoch_committee_count_per_slot(&self, spec: &ChainSpec) -> u64 {
let previous_active_validators = get_active_validator_indices(
&self.validator_registry,
self.previous_epoch_calculation_slot,
);
self.get_committee_count_per_slot(previous_active_validators.len(), spec) as u64
}
pub fn get_current_epoch_committee_count_per_slot(&self, spec: &ChainSpec) -> u64 {
let current_active_validators = get_active_validator_indices(
&self.validator_registry,
self.current_epoch_calculation_slot,
);
self.get_committee_count_per_slot(current_active_validators.len(), spec)
}
pub fn get_crosslink_committees_at_slot(
&self,
slot: u64,
spec: &ChainSpec,
) -> Result<Vec<(Vec<usize>, u64)>> {
/*
let previous_epoch_range = self.get_current_epoch_boundaries(spec.epoch_length);
let current_epoch_range = self.get_current_epoch_boundaries(spec.epoch_length);
if !range_contains(&current_epoch_range, slot) {
return Err(Error::InvalidSlot(slot, current_epoch_range));
}
*/
let epoch = slot / spec.epoch_length;
let current_epoch = self.slot / spec.epoch_length;
let previous_epoch = if current_epoch == spec.genesis_slot {
current_epoch
} else {
current_epoch.saturating_sub(1)
};
let next_epoch = current_epoch + 1;
if !((previous_epoch <= epoch) & (epoch < next_epoch)) {
return Err(Error::InvalidSlot(slot, previous_epoch..current_epoch));
}
let offset = slot % spec.epoch_length;
let (committees_per_slot, shuffling, slot_start_shard) = if epoch < current_epoch {
let committees_per_slot = self.get_previous_epoch_committee_count_per_slot(spec);
let shuffling = self.get_shuffling(
self.previous_epoch_seed,
self.previous_epoch_calculation_slot,
spec,
);
let slot_start_shard =
(self.previous_epoch_start_shard + committees_per_slot * offset) % spec.shard_count;
(committees_per_slot, shuffling, slot_start_shard)
} else {
let committees_per_slot = self.get_current_epoch_committee_count_per_slot(spec);
let shuffling = self.get_shuffling(
self.current_epoch_seed,
self.current_epoch_calculation_slot,
spec,
);
let slot_start_shard =
(self.current_epoch_start_shard + committees_per_slot * offset) % spec.shard_count;
(committees_per_slot, shuffling, slot_start_shard)
};
let mut crosslinks_at_slot = vec![];
for i in 0..committees_per_slot {
let tuple = (
shuffling[(committees_per_slot * offset + i) as usize].clone(),
(slot_start_shard + i) % spec.shard_count,
);
crosslinks_at_slot.push(tuple)
}
Ok(crosslinks_at_slot)
}
}
/// Utility function pending this functionality being stabilized on the `Range` type.
fn range_contains<T: PartialOrd>(range: &Range<T>, target: T) -> bool {
range.start <= target && target < range.end
}

View File

@ -1,9 +1,12 @@
use super::winning_root::WinningRoot; use super::winning_root::{Error as WinningRootError, WinningRoot};
use crate::{ use crate::{
validator::StatusFlags, validator_registry::get_active_validator_indices, AttestationData, beacon_state::{AttestationParticipantsError, CommitteesError},
BeaconState, Bitfield, ChainSpec, Crosslink, Hash256, PendingAttestation, validator::StatusFlags,
validator_registry::get_active_validator_indices,
BeaconState, ChainSpec, Crosslink, Hash256, PendingAttestation,
}; };
use integer_sqrt::IntegerSquareRoot; use integer_sqrt::IntegerSquareRoot;
use log::debug;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::iter::FromIterator; use std::iter::FromIterator;
@ -11,8 +14,17 @@ use std::iter::FromIterator;
pub enum Error { pub enum Error {
UnableToDetermineProducer, UnableToDetermineProducer,
NoBlockRoots, NoBlockRoots,
UnableToGetCrosslinkCommittees,
BaseRewardQuotientIsZero, BaseRewardQuotientIsZero,
CommitteesError(CommitteesError),
AttestationParticipantsError(AttestationParticipantsError),
InclusionError(InclusionError),
WinningRootError(WinningRootError),
}
#[derive(Debug, PartialEq)]
pub enum InclusionError {
NoIncludedAttestations,
AttestationParticipantsError(AttestationParticipantsError),
} }
macro_rules! safe_add_assign { macro_rules! safe_add_assign {
@ -28,14 +40,19 @@ macro_rules! safe_sub_assign {
impl BeaconState { impl BeaconState {
pub fn per_epoch_processing(&mut self, spec: &ChainSpec) -> Result<(), Error> { pub fn per_epoch_processing(&mut self, spec: &ChainSpec) -> Result<(), Error> {
debug!("Starting per-epoch processing...");
/* /*
* All Validators * All Validators
*/ */
let active_validator_indices = let active_validator_indices =
get_active_validator_indices(&self.validator_registry, self.slot); get_active_validator_indices(&self.validator_registry, self.slot);
let total_balance: u64 = active_validator_indices let total_balance = self.get_effective_balances(&active_validator_indices[..], spec);
.iter()
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec)); debug!(
"{} validators with a total balance of {} wei.",
active_validator_indices.len(),
total_balance
);
let current_epoch_attestations: Vec<&PendingAttestation> = self let current_epoch_attestations: Vec<&PendingAttestation> = self
.latest_attestations .latest_attestations
@ -53,42 +70,26 @@ impl BeaconState {
let current_epoch_boundary_attestations: Vec<&PendingAttestation> = let current_epoch_boundary_attestations: Vec<&PendingAttestation> =
current_epoch_attestations current_epoch_attestations
.iter() .iter()
// `filter_map` is used to avoid a double borrow (`&&..`). .filter(|a| {
.filter_map(|a| {
// TODO: ensure this saturating sub is correct. // TODO: ensure this saturating sub is correct.
let block_root = match self match self.get_block_root(self.slot.saturating_sub(spec.epoch_length), spec) {
.get_block_root(self.slot.saturating_sub(spec.epoch_length), spec) Some(block_root) => {
{ (a.data.epoch_boundary_root == *block_root)
Some(root) => root, && (a.data.justified_slot == self.justified_slot)
}
// Protected by a check that latest_block_roots isn't empty. // Protected by a check that latest_block_roots isn't empty.
// //
// TODO: provide detailed reasoning. // TODO: provide detailed reasoning.
None => unreachable!(), None => unreachable!(),
};
if (a.data.epoch_boundary_root == *block_root)
&& (a.data.justified_slot == self.justified_slot)
{
Some(*a)
} else {
None
} }
}) })
.cloned()
.collect(); .collect();
let current_epoch_boundary_attester_indices: Vec<usize> = let current_epoch_boundary_attester_indices = self
current_epoch_boundary_attestations .get_attestation_participants_union(&current_epoch_boundary_attestations[..], spec)?;
.iter() let current_epoch_boundary_attesting_balance =
.fold(vec![], |mut acc, a| { self.get_effective_balances(&current_epoch_boundary_attester_indices[..], spec);
acc.append(
&mut self.get_attestation_participants(&a.data, &a.aggregation_bitfield),
);
acc
});
let current_epoch_boundary_attesting_balance = current_epoch_boundary_attester_indices
.iter()
.fold(0_u64, |acc, i| acc + self.get_effective_balance(*i, spec));
/* /*
* Validators attesting during the previous epoch * Validators attesting during the previous epoch
@ -107,15 +108,8 @@ impl BeaconState {
}) })
.collect(); .collect();
let previous_epoch_attester_indices: Vec<usize> = let previous_epoch_attester_indices =
previous_epoch_attestations self.get_attestation_participants_union(&previous_epoch_attestations[..], spec)?;
.iter()
.fold(vec![], |mut acc, a| {
acc.append(
&mut self.get_attestation_participants(&a.data, &a.aggregation_bitfield),
);
acc
});
/* /*
* Validators targetting the previous justified slot * Validators targetting the previous justified slot
@ -123,43 +117,22 @@ impl BeaconState {
let previous_epoch_justified_attestations: Vec<&PendingAttestation> = { let previous_epoch_justified_attestations: Vec<&PendingAttestation> = {
let mut a: Vec<&PendingAttestation> = current_epoch_attestations let mut a: Vec<&PendingAttestation> = current_epoch_attestations
.iter() .iter()
// `filter_map` is used to avoid a double borrow (`&&..`). .filter(|a| a.data.justified_slot == self.previous_justified_slot)
.filter_map(|a| { .cloned()
if a.data.justified_slot == self.previous_justified_slot {
Some(*a)
} else {
None
}
})
.collect(); .collect();
let mut b: Vec<&PendingAttestation> = previous_epoch_attestations let mut b: Vec<&PendingAttestation> = previous_epoch_attestations
.iter() .iter()
// `filter_map` is used to avoid a double borrow (`&&..`). .filter(|a| a.data.justified_slot == self.previous_justified_slot)
.filter_map(|a| { .cloned()
if a.data.justified_slot == self.previous_justified_slot {
Some(*a)
} else {
None
}
})
.collect(); .collect();
a.append(&mut b); a.append(&mut b);
a a
}; };
let previous_epoch_justified_attester_indices: Vec<usize> = let previous_epoch_justified_attester_indices = self
previous_epoch_justified_attestations .get_attestation_participants_union(&previous_epoch_justified_attestations[..], spec)?;
.iter() let previous_epoch_justified_attesting_balance =
.fold(vec![], |mut acc, a| { self.get_effective_balances(&previous_epoch_justified_attester_indices[..], spec);
acc.append(
&mut self.get_attestation_participants(&a.data, &a.aggregation_bitfield),
);
acc
});
let previous_epoch_justified_attesting_balance = previous_epoch_justified_attester_indices
.iter()
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec));
/* /*
* Validators justifying the epoch boundary block at the start of the previous epoch * Validators justifying the epoch boundary block at the start of the previous epoch
@ -167,41 +140,24 @@ impl BeaconState {
let previous_epoch_boundary_attestations: Vec<&PendingAttestation> = let previous_epoch_boundary_attestations: Vec<&PendingAttestation> =
previous_epoch_justified_attestations previous_epoch_justified_attestations
.iter() .iter()
// `filter_map` is used to avoid a double borrow (`&&..`). .filter(|a| {
.filter_map(|a| {
// TODO: ensure this saturating sub is correct. // TODO: ensure this saturating sub is correct.
let block_root = match self match self.get_block_root(self.slot.saturating_sub(2 * spec.epoch_length), spec)
.get_block_root(self.slot.saturating_sub(2 * spec.epoch_length), spec)
{ {
Some(root) => root, Some(block_root) => a.data.epoch_boundary_root == *block_root,
// Protected by a check that latest_block_roots isn't empty. // Protected by a check that latest_block_roots isn't empty.
// //
// TODO: provide detailed reasoning. // TODO: provide detailed reasoning.
None => unreachable!(), None => unreachable!(),
};
if a.data.epoch_boundary_root == *block_root {
Some(*a)
} else {
None
} }
}) })
.cloned()
.collect(); .collect();
let previous_epoch_boundary_attester_indices: Vec<usize> = let previous_epoch_boundary_attester_indices = self
previous_epoch_boundary_attestations .get_attestation_participants_union(&previous_epoch_boundary_attestations[..], spec)?;
.iter() let previous_epoch_boundary_attesting_balance =
.fold(vec![], |mut acc, a| { self.get_effective_balances(&previous_epoch_boundary_attester_indices[..], spec);
acc.append(
&mut self.get_attestation_participants(&a.data, &a.aggregation_bitfield),
);
acc
});
let previous_epoch_boundary_attesting_balance: u64 =
previous_epoch_boundary_attester_indices
.iter()
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec));
/* /*
* Validators attesting to the expected beacon chain head during the previous epoch. * Validators attesting to the expected beacon chain head during the previous epoch.
@ -209,37 +165,23 @@ impl BeaconState {
let previous_epoch_head_attestations: Vec<&PendingAttestation> = let previous_epoch_head_attestations: Vec<&PendingAttestation> =
previous_epoch_attestations previous_epoch_attestations
.iter() .iter()
.filter_map(|a| { .filter(|a| {
let block_root = match self match self.get_block_root(self.slot.saturating_sub(2 * spec.epoch_length), spec)
.get_block_root(self.slot.saturating_sub(2 * spec.epoch_length), spec)
{ {
Some(root) => root, Some(block_root) => a.data.beacon_block_root == *block_root,
// Protected by a check that latest_block_roots isn't empty. // Protected by a check that latest_block_roots isn't empty.
// //
// TODO: provide detailed reasoning. // TODO: provide detailed reasoning.
None => unreachable!(), None => unreachable!(),
};
if a.data.beacon_block_root == *block_root {
Some(*a)
} else {
None
} }
}) })
.cloned()
.collect(); .collect();
let previous_epoch_head_attester_indices: Vec<usize> = previous_epoch_head_attestations let previous_epoch_head_attester_indices =
.iter() self.get_attestation_participants_union(&previous_epoch_head_attestations[..], spec)?;
.fold(vec![], |mut acc, a| { let previous_epoch_head_attesting_balance =
acc.append( self.get_effective_balances(&previous_epoch_head_attester_indices[..], spec);
&mut self.get_attestation_participants(&a.data, &a.aggregation_bitfield),
);
acc
});
let previous_epoch_head_attesting_balance: u64 = previous_epoch_head_attester_indices
.iter()
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec));
/* /*
* Eth1 Data * Eth1 Data
@ -295,17 +237,22 @@ impl BeaconState {
self.finalized_slot = self.previous_justified_slot; self.finalized_slot = self.previous_justified_slot;
} }
debug!(
"Finalized slot {}, justified slot {}.",
self.finalized_slot, self.justified_slot
);
/* /*
* Crosslinks * Crosslinks
*/ */
// Cached for later lookups. // Cached for later lookups.
let mut winning_root_for_shards: HashMap<u64, WinningRoot> = HashMap::new(); let mut winning_root_for_shards: HashMap<u64, Result<WinningRoot, WinningRootError>> =
HashMap::new();
for slot in self.slot.saturating_sub(2 * spec.epoch_length)..self.slot { // for slot in self.slot.saturating_sub(2 * spec.epoch_length)..self.slot {
let crosslink_committees_at_slot = self for slot in self.get_previous_epoch_boundaries(spec) {
.get_crosslink_committees_at_slot(slot, spec) let crosslink_committees_at_slot = self.get_crosslink_committees_at_slot(slot, spec)?;
.map_err(|_| Error::UnableToGetCrosslinkCommittees)?;
for (crosslink_committee, shard) in crosslink_committees_at_slot { for (crosslink_committee, shard) in crosslink_committees_at_slot {
let shard = shard as u64; let shard = shard as u64;
@ -317,12 +264,9 @@ impl BeaconState {
spec, spec,
); );
if let Some(winning_root) = winning_root { if let Ok(winning_root) = &winning_root {
let total_committee_balance: u64 = crosslink_committee let total_committee_balance =
.iter() self.get_effective_balances(&crosslink_committee[..], spec);
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec));
winning_root_for_shards.insert(shard, winning_root.clone());
if (3 * winning_root.total_attesting_balance) >= (2 * total_committee_balance) { if (3 * winning_root.total_attesting_balance) >= (2 * total_committee_balance) {
self.latest_crosslinks[shard as usize] = Crosslink { self.latest_crosslinks[shard as usize] = Crosslink {
@ -331,9 +275,15 @@ impl BeaconState {
} }
} }
} }
winning_root_for_shards.insert(shard, winning_root);
} }
} }
debug!(
"Found {} winning shard roots.",
winning_root_for_shards.len()
);
/* /*
* Rewards and Penalities * Rewards and Penalities
*/ */
@ -342,13 +292,6 @@ impl BeaconState {
return Err(Error::BaseRewardQuotientIsZero); return Err(Error::BaseRewardQuotientIsZero);
} }
/*
let base_reward = |i| match self.get_effective_balance(i, spec) {
Some(effective_balance) => effective_balance / base_reward_quotient / 5,
None => unreachable!(),
};
*/
/* /*
* Justification and finalization * Justification and finalization
*/ */
@ -398,10 +341,7 @@ impl BeaconState {
for index in previous_epoch_attester_indices { for index in previous_epoch_attester_indices {
let base_reward = self.base_reward(index, base_reward_quotient, spec); let base_reward = self.base_reward(index, base_reward_quotient, spec);
let inclusion_distance = match self.inclusion_distance(index) { let inclusion_distance = self.inclusion_distance(index, spec)?;
Some(distance) => distance,
None => unreachable!(),
};
safe_add_assign!( safe_add_assign!(
self.validator_balances[index], self.validator_balances[index],
@ -432,10 +372,7 @@ impl BeaconState {
for index in previous_epoch_attester_indices { for index in previous_epoch_attester_indices {
let base_reward = self.base_reward(index, base_reward_quotient, spec); let base_reward = self.base_reward(index, base_reward_quotient, spec);
let inclusion_distance = match self.inclusion_distance(index) { let inclusion_distance = self.inclusion_distance(index, spec)?;
Some(distance) => distance,
None => unreachable!(),
};
safe_sub_assign!( safe_sub_assign!(
self.validator_balances[index], self.validator_balances[index],
@ -445,14 +382,13 @@ impl BeaconState {
} }
} }
debug!("Processed validator justification and finalization rewards/penalities.");
/* /*
* Attestation inclusion * Attestation inclusion
*/ */
for index in previous_epoch_attester_indices_hashset { for index in previous_epoch_attester_indices_hashset {
let inclusion_slot = match self.inclusion_slot(index) { let inclusion_slot = self.inclusion_slot(index, spec)?;
Some(slot) => slot,
None => unreachable!(),
};
let proposer_index = self let proposer_index = self
.get_beacon_proposer_index(inclusion_slot, spec) .get_beacon_proposer_index(inclusion_slot, spec)
.map_err(|_| Error::UnableToDetermineProducer)?; .map_err(|_| Error::UnableToDetermineProducer)?;
@ -463,45 +399,46 @@ impl BeaconState {
); );
} }
debug!("Processed validator attestation inclusdion rewards.");
/* /*
* Crosslinks * Crosslinks
*/ */
for slot in self.slot.saturating_sub(2 * spec.epoch_length)..self.slot { for slot in self.get_previous_epoch_boundaries(spec) {
let crosslink_committees_at_slot = self let crosslink_committees_at_slot = self.get_crosslink_committees_at_slot(slot, spec)?;
.get_crosslink_committees_at_slot(slot, spec)
.map_err(|_| Error::UnableToGetCrosslinkCommittees)?;
for (_crosslink_committee, shard) in crosslink_committees_at_slot { for (_crosslink_committee, shard) in crosslink_committees_at_slot {
let shard = shard as u64; let shard = shard as u64;
let winning_root = winning_root_for_shards.get(&shard).expect("unreachable"); if let Some(Ok(winning_root)) = winning_root_for_shards.get(&shard) {
// TODO: remove the map.
let attesting_validator_indices: HashSet<usize> = HashSet::from_iter(
winning_root.attesting_validator_indices.iter().map(|i| *i),
);
// TODO: remove the map. for index in 0..self.validator_balances.len() {
let attesting_validator_indices: HashSet<usize> = let base_reward = self.base_reward(index, base_reward_quotient, spec);
HashSet::from_iter(winning_root.attesting_validator_indices.iter().map(|i| *i));
for index in 0..self.validator_balances.len() { if attesting_validator_indices.contains(&index) {
let base_reward = self.base_reward(index, base_reward_quotient, spec); safe_add_assign!(
self.validator_balances[index],
base_reward * winning_root.total_attesting_balance
/ winning_root.total_balance
);
} else {
safe_sub_assign!(self.validator_balances[index], base_reward);
}
}
if attesting_validator_indices.contains(&index) { for index in &winning_root.attesting_validator_indices {
let base_reward = self.base_reward(*index, base_reward_quotient, spec);
safe_add_assign!( safe_add_assign!(
self.validator_balances[index], self.validator_balances[*index],
base_reward * winning_root.total_attesting_balance base_reward * winning_root.total_attesting_balance
/ winning_root.total_balance / winning_root.total_balance
); );
} else {
safe_sub_assign!(self.validator_balances[index], base_reward);
} }
} }
for index in &winning_root.attesting_validator_indices {
let base_reward = self.base_reward(*index, base_reward_quotient, spec);
safe_add_assign!(
self.validator_balances[*index],
base_reward * winning_root.total_attesting_balance
/ winning_root.total_balance
);
}
} }
} }
@ -562,13 +499,8 @@ impl BeaconState {
self.latest_attestations = self self.latest_attestations = self
.latest_attestations .latest_attestations
.iter() .iter()
.filter_map(|a| { .filter(|a| a.data.slot < self.slot - spec.epoch_length)
if a.data.slot < self.slot - spec.epoch_length { .cloned()
Some(a.clone())
} else {
None
}
})
.collect(); .collect();
Ok(()) Ok(())
@ -577,9 +509,7 @@ impl BeaconState {
fn process_penalties_and_exits(&mut self, spec: &ChainSpec) { fn process_penalties_and_exits(&mut self, spec: &ChainSpec) {
let active_validator_indices = let active_validator_indices =
get_active_validator_indices(&self.validator_registry, self.slot); get_active_validator_indices(&self.validator_registry, self.slot);
let total_balance = active_validator_indices let total_balance = self.get_effective_balances(&active_validator_indices[..], spec);
.iter()
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec));
for index in 0..self.validator_balances.len() { for index in 0..self.validator_balances.len() {
let validator = &self.validator_registry[index]; let validator = &self.validator_registry[index];
@ -640,9 +570,7 @@ impl BeaconState {
fn update_validator_registry(&mut self, spec: &ChainSpec) { fn update_validator_registry(&mut self, spec: &ChainSpec) {
let active_validator_indices = let active_validator_indices =
get_active_validator_indices(&self.validator_registry, self.slot); get_active_validator_indices(&self.validator_registry, self.slot);
let total_balance = active_validator_indices let total_balance = self.get_effective_balances(&active_validator_indices[..], spec);
.iter()
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec));
let max_balance_churn = std::cmp::max( let max_balance_churn = std::cmp::max(
spec.max_deposit, spec.max_deposit,
@ -726,30 +654,64 @@ impl BeaconState {
+ effective_balance * epochs_since_finality / spec.inactivity_penalty_quotient / 2 + effective_balance * epochs_since_finality / spec.inactivity_penalty_quotient / 2
} }
fn inclusion_distance(&self, validator_index: usize) -> Option<u64> { fn inclusion_distance(
let attestation = self.earliest_included_attestation(validator_index)?; &self,
Some( validator_index: usize,
attestation spec: &ChainSpec,
.slot_included ) -> Result<u64, InclusionError> {
.saturating_sub(attestation.data.slot), let attestation = self.earliest_included_attestation(validator_index, spec)?;
) Ok(attestation
.slot_included
.saturating_sub(attestation.data.slot))
} }
fn inclusion_slot(&self, validator_index: usize) -> Option<u64> { fn inclusion_slot(
let attestation = self.earliest_included_attestation(validator_index)?; &self,
Some(attestation.slot_included) validator_index: usize,
spec: &ChainSpec,
) -> Result<u64, InclusionError> {
let attestation = self.earliest_included_attestation(validator_index, spec)?;
Ok(attestation.slot_included)
} }
fn earliest_included_attestation(&self, validator_index: usize) -> Option<&PendingAttestation> { fn earliest_included_attestation(
&self,
validator_index: usize,
spec: &ChainSpec,
) -> Result<&PendingAttestation, InclusionError> {
let mut included_attestations = vec![];
for a in &self.latest_attestations {
let participants =
self.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
if participants
.iter()
.find(|i| **i == validator_index)
.is_some()
{
included_attestations.push(a);
}
}
Ok(included_attestations
.iter()
.min_by_key(|a| a.slot_included)
.and_then(|x| Some(*x))
.ok_or_else(|| InclusionError::NoIncludedAttestations)?)
/*
self.latest_attestations self.latest_attestations
.iter() .iter()
.filter(|a| { .try_for_each(|a| {
self.get_attestation_participants(&a.data, &a.aggregation_bitfield) self.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)
})?
.filter(|participants| {
participants
.iter() .iter()
.find(|i| **i == validator_index) .find(|i| **i == validator_index)
.is_some() .is_some()
}) })
.min_by_key(|a| a.slot_included) .min_by_key(|a| a.slot_included)
*/
} }
fn base_reward( fn base_reward(
@ -761,6 +723,12 @@ impl BeaconState {
self.get_effective_balance(validator_index, spec) / base_reward_quotient / 5 self.get_effective_balance(validator_index, spec) / base_reward_quotient / 5
} }
pub fn get_effective_balances(&self, validator_indices: &[usize], spec: &ChainSpec) -> u64 {
validator_indices
.iter()
.fold(0, |acc, i| acc + self.get_effective_balance(*i, spec))
}
pub fn get_effective_balance(&self, validator_index: usize, spec: &ChainSpec) -> u64 { pub fn get_effective_balance(&self, validator_index: usize, spec: &ChainSpec) -> u64 {
std::cmp::min(self.validator_balances[validator_index], spec.max_deposit) std::cmp::min(self.validator_balances[validator_index], spec.max_deposit)
} }
@ -773,13 +741,34 @@ impl BeaconState {
None None
} }
} }
}
pub fn get_attestation_participants( impl From<CommitteesError> for Error {
&self, fn from(e: CommitteesError) -> Error {
_attestation_data: &AttestationData, Error::CommitteesError(e)
_aggregation_bitfield: &Bitfield, }
) -> Vec<usize> { }
// TODO: stubbed out.
vec![0, 1] impl From<AttestationParticipantsError> for Error {
fn from(e: AttestationParticipantsError) -> Error {
Error::AttestationParticipantsError(e)
}
}
impl From<InclusionError> for Error {
fn from(e: InclusionError) -> Error {
Error::InclusionError(e)
}
}
impl From<AttestationParticipantsError> for InclusionError {
fn from(e: AttestationParticipantsError) -> InclusionError {
InclusionError::AttestationParticipantsError(e)
}
}
impl From<WinningRootError> for Error {
fn from(e: WinningRootError) -> Error {
Error::WinningRootError(e)
} }
} }

View File

@ -10,21 +10,18 @@ use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
mod attestation_participants;
mod attestation_validation; mod attestation_validation;
mod committees;
mod epoch_processing; mod epoch_processing;
mod shuffling; mod shuffling;
mod slot_processing; mod slot_processing;
mod winning_root; mod winning_root;
pub use self::attestation_participants::Error as AttestationParticipantsError;
pub use self::attestation_validation::Error as AttestationValidationError; pub use self::attestation_validation::Error as AttestationValidationError;
pub use self::committees::Error as CommitteesError;
pub use self::epoch_processing::Error as EpochProcessingError; pub use self::epoch_processing::Error as EpochProcessingError;
pub use self::slot_processing::Error as SlotProcessingError;
#[derive(Debug, PartialEq)]
pub enum Error {
InvalidSlot,
InsufficientNumberOfValidators,
}
// Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used. // Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used.
type CustodyChallenge = usize; type CustodyChallenge = usize;

View File

@ -1,16 +1,8 @@
use super::Error; use super::CommitteesError;
use crate::{validator_registry::get_active_validator_indices, BeaconState, ChainSpec, Hash256}; use crate::{validator_registry::get_active_validator_indices, BeaconState, ChainSpec, Hash256};
use honey_badger_split::SplitExt; use honey_badger_split::SplitExt;
use std::ops::Range;
use vec_shuffle::shuffle; use vec_shuffle::shuffle;
// utility function pending this functionality being stabilized on the `Range` type.
fn range_contains<T: PartialOrd>(range: &Range<T>, target: T) -> bool {
range.start <= target && target < range.end
}
type Result<T> = std::result::Result<T, Error>;
impl BeaconState { impl BeaconState {
pub fn get_shuffling(&self, seed: Hash256, slot: u64, spec: &ChainSpec) -> Vec<Vec<usize>> { pub fn get_shuffling(&self, seed: Hash256, slot: u64, spec: &ChainSpec) -> Vec<Vec<usize>> {
let slot = slot - (slot % spec.epoch_length); let slot = slot - (slot % spec.epoch_length);
@ -31,112 +23,21 @@ impl BeaconState {
.collect() .collect()
} }
pub fn get_committee_count_per_slot( /// Returns the beacon proposer index for the `slot`.
&self, /// If the state does not contain an index for a beacon proposer at the requested `slot`, then `None` is returned.
active_validator_count: usize, pub fn get_beacon_proposer_index(
spec: &ChainSpec,
) -> u64 {
std::cmp::max(
1,
std::cmp::min(
spec.shard_count / spec.epoch_length,
active_validator_count as u64 / spec.epoch_length / spec.target_committee_size,
),
)
}
/// Returns the start slot and end slot of the current epoch containing `self.slot`.
fn get_current_epoch_boundaries(&self, epoch_length: u64) -> Range<u64> {
let slot_in_epoch = self.slot % epoch_length;
let start = self.slot - slot_in_epoch;
let end = self.slot + (epoch_length - slot_in_epoch);
start..end
}
fn get_previous_epoch_committee_count_per_slot(
&self,
spec: &ChainSpec,
/*
shard_count: u64,
epoch_length: u64,
target_committee_size: u64,
*/
) -> u64 {
let previous_active_validators = get_active_validator_indices(
&self.validator_registry,
self.previous_epoch_calculation_slot,
);
self.get_committee_count_per_slot(previous_active_validators.len(), spec) as u64
}
pub fn get_current_epoch_committee_count_per_slot(&self, spec: &ChainSpec) -> u64 {
let current_active_validators = get_active_validator_indices(
&self.validator_registry,
self.current_epoch_calculation_slot,
);
self.get_committee_count_per_slot(current_active_validators.len(), spec)
}
pub fn get_crosslink_committees_at_slot(
&self, &self,
slot: u64, slot: u64,
spec: &ChainSpec, spec: &ChainSpec,
/* ) -> Result<usize, CommitteesError> {
epoch_length: u64,
shard_count: u64,
target_committee_size: u64,
*/
) -> Result<Vec<(Vec<usize>, u64)>> {
let current_epoch_range = self.get_current_epoch_boundaries(spec.epoch_length);
if !range_contains(&current_epoch_range, slot) {
return Err(Error::InvalidSlot);
}
let state_epoch_slot = current_epoch_range.start;
let offset = slot % spec.epoch_length;
let (committees_per_slot, shuffling, slot_start_shard) = if slot < state_epoch_slot {
let committees_per_slot = self.get_previous_epoch_committee_count_per_slot(spec);
let shuffling = self.get_shuffling(
self.previous_epoch_seed,
self.previous_epoch_calculation_slot,
spec,
);
let slot_start_shard =
(self.previous_epoch_start_shard + committees_per_slot * offset) % spec.shard_count;
(committees_per_slot, shuffling, slot_start_shard)
} else {
let committees_per_slot = self.get_current_epoch_committee_count_per_slot(spec);
let shuffling = self.get_shuffling(
self.current_epoch_seed,
self.current_epoch_calculation_slot,
spec,
);
let slot_start_shard =
(self.current_epoch_start_shard + committees_per_slot * offset) % spec.shard_count;
(committees_per_slot, shuffling, slot_start_shard)
};
let shard_range = slot_start_shard..;
Ok(shuffling
.into_iter()
.skip((committees_per_slot * offset) as usize)
.zip(shard_range.into_iter())
.take(committees_per_slot as usize)
.map(|(committees, shard_number)| (committees, shard_number % spec.shard_count))
.collect::<Vec<_>>())
}
/// Returns the beacon proposer index for the `slot`.
/// If the state does not contain an index for a beacon proposer at the requested `slot`, then `None` is returned.
pub fn get_beacon_proposer_index(&self, slot: u64, spec: &ChainSpec) -> Result<usize> {
let committees = self.get_crosslink_committees_at_slot(slot, spec)?; let committees = self.get_crosslink_committees_at_slot(slot, spec)?;
committees committees
.first() .first()
.ok_or(Error::InsufficientNumberOfValidators) .ok_or(CommitteesError::InsufficientNumberOfValidators)
.and_then(|(first_committee, _)| { .and_then(|(first_committee, _)| {
let index = (slot as usize) let index = (slot as usize)
.checked_rem(first_committee.len()) .checked_rem(first_committee.len())
.ok_or(Error::InsufficientNumberOfValidators)?; .ok_or(CommitteesError::InsufficientNumberOfValidators)?;
// NOTE: next index will not panic as we have already returned if this is the case // NOTE: next index will not panic as we have already returned if this is the case
Ok(first_committee[index]) Ok(first_committee[index])
}) })

View File

@ -1,20 +1,14 @@
use crate::{BeaconState, ChainSpec, Hash256}; use crate::{beacon_state::CommitteesError, BeaconState, ChainSpec, Hash256};
pub enum Error {
UnableToDetermineProducer,
}
impl BeaconState { impl BeaconState {
pub fn per_slot_processing( pub fn per_slot_processing(
&mut self, &mut self,
previous_block_root: Hash256, previous_block_root: Hash256,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<(), Error> { ) -> Result<(), CommitteesError> {
self.slot += 1; self.slot += 1;
let block_proposer = self let block_proposer = self.get_beacon_proposer_index(self.slot, spec)?;
.get_beacon_proposer_index(self.slot, spec)
.map_err(|_| Error::UnableToDetermineProducer)?;
self.validator_registry[block_proposer].proposer_slots += 1; self.validator_registry[block_proposer].proposer_slots += 1;
self.latest_randao_mixes[(self.slot % spec.latest_randao_mixes_length) as usize] = self.latest_randao_mixes[(self.slot % spec.latest_randao_mixes_length) as usize] =
@ -35,7 +29,18 @@ impl BeaconState {
&self, &self,
validator_index: usize, validator_index: usize,
spec: &ChainSpec, spec: &ChainSpec,
) -> (u64, u64) { ) -> Result<(u64, u64), CommitteesError> {
let mut result = None;
for slot in self.get_current_epoch_boundaries(spec.epoch_length) {
for (committee, shard) in self.get_crosslink_committees_at_slot(slot, spec)? {
if committee.iter().find(|i| **i == validator_index).is_some() {
result = Some(Ok((slot, shard)));
}
}
}
result.unwrap()
/*
// TODO: this is a stub; implement it properly. // TODO: this is a stub; implement it properly.
let validator_index = validator_index as u64; let validator_index = validator_index as u64;
@ -43,6 +48,7 @@ impl BeaconState {
let shard = validator_index % spec.shard_count; let shard = validator_index % spec.shard_count;
(slot, shard) (slot, shard)
*/
} }
} }

View File

@ -1,11 +1,12 @@
use crate::{BeaconState, ChainSpec, Hash256, PendingAttestation}; use crate::{
beacon_state::AttestationParticipantsError, BeaconState, ChainSpec, Hash256, PendingAttestation,
};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
UnableToDetermineProducer, NoWinningRoot,
NoBlockRoots, AttestationParticipantsError(AttestationParticipantsError),
UnableToGetCrosslinkCommittees,
BaseRewardQuotientIsZero,
} }
#[derive(Clone)] #[derive(Clone)]
@ -23,7 +24,7 @@ impl BeaconState {
current_epoch_attestations: &[&PendingAttestation], current_epoch_attestations: &[&PendingAttestation],
previous_epoch_attestations: &[&PendingAttestation], previous_epoch_attestations: &[&PendingAttestation],
spec: &ChainSpec, spec: &ChainSpec,
) -> Option<WinningRoot> { ) -> Result<WinningRoot, Error> {
let mut attestations = current_epoch_attestations.to_vec(); let mut attestations = current_epoch_attestations.to_vec();
attestations.append(&mut previous_epoch_attestations.to_vec()); attestations.append(&mut previous_epoch_attestations.to_vec());
@ -42,14 +43,23 @@ impl BeaconState {
continue; continue;
} }
let attesting_validator_indices = attestations.iter().fold(vec![], |mut acc, a| { // TODO: `cargo fmt` makes this rather ugly; tidy up.
if (a.data.shard == shard) && (a.data.shard_block_root == *shard_block_root) { let attesting_validator_indices = attestations.iter().try_fold::<_, _, Result<
acc.append( _,
&mut self.get_attestation_participants(&a.data, &a.aggregation_bitfield), AttestationParticipantsError,
); >>(
} vec![],
acc |mut acc, a| {
}); if (a.data.shard == shard) && (a.data.shard_block_root == *shard_block_root) {
acc.append(&mut self.get_attestation_participants(
&a.data,
&a.aggregation_bitfield,
spec,
)?);
}
Ok(acc)
},
)?;
let total_balance: u64 = attesting_validator_indices let total_balance: u64 = attesting_validator_indices
.iter() .iter()
@ -73,7 +83,7 @@ impl BeaconState {
candidates.insert(*shard_block_root, candidate_root); candidates.insert(*shard_block_root, candidate_root);
} }
let winner = candidates Ok(candidates
.iter() .iter()
.filter_map(|(_hash, candidate)| { .filter_map(|(_hash, candidate)| {
if candidate.total_attesting_balance == highest_seen_balance { if candidate.total_attesting_balance == highest_seen_balance {
@ -82,11 +92,15 @@ impl BeaconState {
None None
} }
}) })
.min_by_key(|candidate| candidate.shard_block_root); .min_by_key(|candidate| candidate.shard_block_root)
.ok_or_else(|| Error::NoWinningRoot)?
match winner { // TODO: avoid clone.
Some(winner) => Some(winner.clone()), .clone())
None => None, }
} }
impl From<AttestationParticipantsError> for Error {
fn from (e: AttestationParticipantsError) -> Error {
Error::AttestationParticipantsError(e)
} }
} }