Fix per_epoch_processing so it compiles again

This commit is contained in:
Paul Hauner 2019-03-06 18:57:41 +11:00
parent 17210faf3a
commit 8a25fd48cf
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
5 changed files with 592 additions and 593 deletions

View File

@ -9,5 +9,6 @@ pub use per_block_processing::{
errors::{BlockInvalid, BlockProcessingError}, errors::{BlockInvalid, BlockProcessingError},
per_block_processing, per_block_processing_without_verifying_block_signature, per_block_processing, per_block_processing_without_verifying_block_signature,
}; };
pub use per_epoch_processing::{errors::EpochProcessingError, per_epoch_processing};
// pub use epoch_processable::{EpochProcessable, Error as EpochProcessingError}; // pub use epoch_processable::{EpochProcessable, Error as EpochProcessingError};
// pub use slot_processable::{Error as SlotProcessingError, SlotProcessable}; // pub use slot_processable::{Error as SlotProcessingError, SlotProcessable};

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,98 @@
use std::collections::HashSet;
use types::*;
#[derive(Default)]
pub struct Attesters {
pub indices: HashSet<usize>,
pub balance: u64,
}
impl Attesters {
fn add(&mut self, additional_indices: &[usize], additional_balance: u64) {
self.indices.reserve(additional_indices.len());
for i in additional_indices {
self.indices.insert(*i);
}
self.balance.saturating_add(additional_balance);
}
}
pub struct GroupedAttesters {
pub current_epoch: Attesters,
pub current_epoch_boundary: Attesters,
pub previous_epoch: Attesters,
pub previous_epoch_boundary: Attesters,
pub previous_epoch_head: Attesters,
}
impl GroupedAttesters {
pub fn new(state: &BeaconState, spec: &ChainSpec) -> Result<Self, BeaconStateError> {
let mut current_epoch = Attesters::default();
let mut current_epoch_boundary = Attesters::default();
let mut previous_epoch = Attesters::default();
let mut previous_epoch_boundary = Attesters::default();
let mut previous_epoch_head = Attesters::default();
for a in &state.latest_attestations {
let attesting_indices =
state.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
let attesting_balance = state.get_total_balance(&attesting_indices, spec);
if is_from_epoch(a, state.current_epoch(spec), spec) {
current_epoch.add(&attesting_indices, attesting_balance);
if has_common_epoch_boundary_root(a, state, state.current_epoch(spec), spec)? {
current_epoch_boundary.add(&attesting_indices, attesting_balance);
}
} else if is_from_epoch(a, state.previous_epoch(spec), spec) {
previous_epoch.add(&attesting_indices, attesting_balance);
if has_common_epoch_boundary_root(a, state, state.previous_epoch(spec), spec)? {
previous_epoch_boundary.add(&attesting_indices, attesting_balance);
}
if has_common_beacon_block_root(a, state, spec)? {
previous_epoch_head.add(&attesting_indices, attesting_balance);
}
}
}
Ok(Self {
current_epoch,
current_epoch_boundary,
previous_epoch,
previous_epoch_boundary,
previous_epoch_head,
})
}
}
fn is_from_epoch(a: &PendingAttestation, epoch: Epoch, spec: &ChainSpec) -> bool {
a.data.slot.epoch(spec.slots_per_epoch) == epoch
}
fn has_common_epoch_boundary_root(
a: &PendingAttestation,
state: &BeaconState,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
let slot = epoch.start_slot(spec.slots_per_epoch);
let state_boundary_root = *state
.get_block_root(slot, spec)
.ok_or_else(|| BeaconStateError::InsufficientBlockRoots)?;
Ok(a.data.epoch_boundary_root == state_boundary_root)
}
fn has_common_beacon_block_root(
a: &PendingAttestation,
state: &BeaconState,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
let state_block_root = *state
.get_block_root(a.data.slot, spec)
.ok_or_else(|| BeaconStateError::InsufficientBlockRoots)?;
Ok(a.data.beacon_block_root == state_block_root)
}

View File

@ -1,5 +1,5 @@
#![cfg(test)] #![cfg(test)]
use crate::EpochProcessable; use crate::per_epoch_processing;
use env_logger::{Builder, Env}; use env_logger::{Builder, Env};
use types::beacon_state::BeaconStateBuilder; use types::beacon_state::BeaconStateBuilder;
use types::*; use types::*;
@ -17,5 +17,5 @@ fn runs_without_error() {
let mut state = builder.cloned_state(); let mut state = builder.cloned_state();
let spec = &builder.spec; let spec = &builder.spec;
state.per_epoch_processing(spec).unwrap(); per_epoch_processing(&mut state, spec).unwrap();
} }

View File

@ -1,15 +1,16 @@
use super::WinningRootError; use super::errors::WinningRootError;
use std::collections::HashMap;
use types::*; use types::*;
#[derive(Clone)] #[derive(Clone)]
pub struct WinningRoot { pub struct WinningRoot {
pub shard_block_root: Hash256, pub crosslink_data_root: Hash256,
pub attesting_validator_indices: Vec<usize>, pub attesting_validator_indices: Vec<usize>,
pub total_balance: u64, pub total_balance: u64,
pub total_attesting_balance: u64, pub total_attesting_balance: u64,
} }
fn winning_root( pub fn winning_root(
state: &BeaconState, state: &BeaconState,
shard: u64, shard: u64,
current_epoch_attestations: &[&PendingAttestation], current_epoch_attestations: &[&PendingAttestation],
@ -28,16 +29,16 @@ fn winning_root(
continue; continue;
} }
let shard_block_root = &a.data.shard_block_root; let crosslink_data_root = &a.data.crosslink_data_root;
if candidates.contains_key(shard_block_root) { if candidates.contains_key(crosslink_data_root) {
continue; continue;
} }
let attesting_validator_indices = attestations let attesting_validator_indices = attestations
.iter() .iter()
.try_fold::<_, _, Result<_, BeaconStateError>>(vec![], |mut acc, a| { .try_fold::<_, _, Result<_, BeaconStateError>>(vec![], |mut acc, a| {
if (a.data.shard == shard) && (a.data.shard_block_root == *shard_block_root) { if (a.data.shard == shard) && (a.data.crosslink_data_root == *crosslink_data_root) {
acc.append(&mut state.get_attestation_participants( acc.append(&mut state.get_attestation_participants(
&a.data, &a.data,
&a.aggregation_bitfield, &a.aggregation_bitfield,
@ -60,13 +61,13 @@ fn winning_root(
} }
let candidate_root = WinningRoot { let candidate_root = WinningRoot {
shard_block_root: *shard_block_root, crosslink_data_root: *crosslink_data_root,
attesting_validator_indices, attesting_validator_indices,
total_attesting_balance, total_attesting_balance,
total_balance, total_balance,
}; };
candidates.insert(*shard_block_root, candidate_root); candidates.insert(*crosslink_data_root, candidate_root);
} }
Ok(candidates Ok(candidates
@ -78,7 +79,7 @@ fn winning_root(
None None
} }
}) })
.min_by_key(|candidate| candidate.shard_block_root) .min_by_key(|candidate| candidate.crosslink_data_root)
.ok_or_else(|| WinningRootError::NoWinningRoot)? .ok_or_else(|| WinningRootError::NoWinningRoot)?
// TODO: avoid clone. // TODO: avoid clone.
.clone()) .clone())