Skip to content

Commit

Permalink
Merge pull request #1 from sigp/master
Browse files Browse the repository at this point in the history
Merged from master project
  • Loading branch information
Langers authored Feb 23, 2019
2 parents 0800091 + c040ed7 commit 7842b0b
Show file tree
Hide file tree
Showing 79 changed files with 1,837 additions and 1,417 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
"eth2/attester",
"eth2/block_producer",
"eth2/block_proposer",
"eth2/fork_choice",
"eth2/state_processing",
"eth2/types",
Expand All @@ -12,6 +12,7 @@ members = [
"eth2/utils/int_to_bytes",
"eth2/utils/slot_clock",
"eth2/utils/ssz",
"eth2/utils/ssz_derive",
"eth2/utils/swap_or_not_shuffle",
"eth2/utils/fisher_yates_shuffle",
"beacon_node",
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"

[dependencies]
block_producer = { path = "../../eth2/block_producer" }
block_proposer = { path = "../../eth2/block_proposer" }
bls = { path = "../../eth2/utils/bls" }
boolean-bitfield = { path = "../../eth2/utils/boolean-bitfield" }
db = { path = "../db" }
Expand Down
8 changes: 5 additions & 3 deletions beacon_node/beacon_chain/src/attestation_aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cached_beacon_state::CachedBeaconState;
use state_processing::validate_attestation_without_signature;
use std::collections::{HashMap, HashSet};
use types::{
Expand Down Expand Up @@ -76,12 +77,12 @@ impl AttestationAggregator {
/// - The signature is verified against that of the validator at `validator_index`.
pub fn process_free_attestation(
&mut self,
state: &BeaconState,
cached_state: &CachedBeaconState,
free_attestation: &FreeAttestation,
spec: &ChainSpec,
) -> Result<Outcome, BeaconStateError> {
let (slot, shard, committee_index) = some_or_invalid!(
state.attestation_slot_and_shard_for_validator(
cached_state.attestation_slot_and_shard_for_validator(
free_attestation.validator_index as usize,
spec,
)?,
Expand All @@ -104,7 +105,8 @@ impl AttestationAggregator {
let signable_message = free_attestation.data.signable_message(PHASE_0_CUSTODY_BIT);

let validator_record = some_or_invalid!(
state
cached_state
.state
.validator_registry
.get(free_attestation.validator_index as usize),
Message::BadValidatorIndex
Expand Down
45 changes: 36 additions & 9 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome};
use crate::cached_beacon_state::CachedBeaconState;
use crate::checkpoint::CheckPoint;
use db::{
stores::{BeaconBlockStore, BeaconStateStore},
Expand Down Expand Up @@ -69,6 +70,7 @@ pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock, F: ForkChoice> {
canonical_head: RwLock<CheckPoint>,
finalized_head: RwLock<CheckPoint>,
pub state: RwLock<BeaconState>,
pub cached_state: RwLock<CachedBeaconState>,
pub spec: ChainSpec,
pub fork_choice: RwLock<F>,
}
Expand Down Expand Up @@ -107,6 +109,11 @@ where
let block_root = genesis_block.canonical_root();
block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?;

let cached_state = RwLock::new(CachedBeaconState::from_beacon_state(
genesis_state.clone(),
spec.clone(),
)?);

let finalized_head = RwLock::new(CheckPoint::new(
genesis_block.clone(),
block_root,
Expand All @@ -127,6 +134,7 @@ where
slot_clock,
attestation_aggregator,
state: RwLock::new(genesis_state.clone()),
cached_state,
finalized_head,
canonical_head,
spec,
Expand Down Expand Up @@ -253,6 +261,7 @@ where
/// Information is read from the present `beacon_state` shuffling, so only information from the
/// present and prior epoch is available.
pub fn block_proposer(&self, slot: Slot) -> Result<usize, BeaconStateError> {
trace!("BeaconChain::block_proposer: slot: {}", slot);
let index = self
.state
.read()
Expand All @@ -274,8 +283,12 @@ where
&self,
validator_index: usize,
) -> Result<Option<(Slot, u64)>, BeaconStateError> {
trace!(
"BeaconChain::validator_attestion_slot_and_shard: validator_index: {}",
validator_index
);
if let Some((slot, shard, _committee)) = self
.state
.cached_state
.read()
.attestation_slot_and_shard_for_validator(validator_index, &self.spec)?
{
Expand All @@ -287,6 +300,7 @@ where

/// Produce an `AttestationData` that is valid for the present `slot` and given `shard`.
pub fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, Error> {
trace!("BeaconChain::produce_attestation_data: shard: {}", shard);
let justified_epoch = self.justified_epoch();
let justified_block_root = *self
.state
Expand Down Expand Up @@ -332,9 +346,7 @@ where
let aggregation_outcome = self
.attestation_aggregator
.write()
.process_free_attestation(&self.state.read(), &free_attestation, &self.spec)?;
// TODO: Check this comment
//.map_err(|e| e.into())?;
.process_free_attestation(&self.cached_state.read(), &free_attestation, &self.spec)?;

// return if the attestation is invalid
if !aggregation_outcome.valid {
Expand All @@ -345,6 +357,7 @@ where
self.fork_choice.write().add_attestation(
free_attestation.validator_index,
&free_attestation.data.beacon_block_root,
&self.spec,
)?;
Ok(aggregation_outcome)
}
Expand Down Expand Up @@ -474,7 +487,9 @@ where
self.state_store.put(&state_root, &ssz_encode(&state)[..])?;

// run the fork_choice add_block logic
self.fork_choice.write().add_block(&block, &block_root)?;
self.fork_choice
.write()
.add_block(&block, &block_root, &self.spec)?;

// If the parent block was the parent_block, automatically update the canonical head.
//
Expand All @@ -489,6 +504,9 @@ where
);
// Update the local state variable.
*self.state.write() = state.clone();
// Update the cached state variable.
*self.cached_state.write() =
CachedBeaconState::from_beacon_state(state.clone(), self.spec.clone())?;
}

Ok(BlockProcessingOutcome::ValidBlock(ValidBlock::Processed))
Expand Down Expand Up @@ -537,9 +555,15 @@ where
},
};

state
.per_block_processing_without_verifying_block_signature(&block, &self.spec)
.ok()?;
trace!("BeaconChain::produce_block: updating state for new block.",);

let result =
state.per_block_processing_without_verifying_block_signature(&block, &self.spec);
trace!(
"BeaconNode::produce_block: state processing result: {:?}",
result
);
result.ok()?;

let state_root = state.canonical_root();

Expand All @@ -554,7 +578,10 @@ where
pub fn fork_choice(&self) -> Result<(), Error> {
let present_head = self.finalized_head().beacon_block_root;

let new_head = self.fork_choice.write().find_head(&present_head)?;
let new_head = self
.fork_choice
.write()
.find_head(&present_head, &self.spec)?;

if new_head != present_head {
let block = self
Expand Down
150 changes: 150 additions & 0 deletions beacon_node/beacon_chain/src/cached_beacon_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use log::{debug, trace};
use std::collections::HashMap;
use types::{beacon_state::BeaconStateError, BeaconState, ChainSpec, Epoch, Slot};

pub const CACHE_PREVIOUS: bool = false;
pub const CACHE_CURRENT: bool = true;
pub const CACHE_NEXT: bool = false;

pub type CrosslinkCommittees = Vec<(Vec<usize>, u64)>;
pub type Shard = u64;
pub type CommitteeIndex = u64;
pub type AttestationDuty = (Slot, Shard, CommitteeIndex);
pub type AttestationDutyMap = HashMap<u64, AttestationDuty>;

// TODO: CachedBeaconState is presently duplicating `BeaconState` and `ChainSpec`. This is a
// massive memory waste, switch them to references.

pub struct CachedBeaconState {
pub state: BeaconState,
committees: Vec<Vec<CrosslinkCommittees>>,
attestation_duties: Vec<AttestationDutyMap>,
next_epoch: Epoch,
current_epoch: Epoch,
previous_epoch: Epoch,
spec: ChainSpec,
}

impl CachedBeaconState {
pub fn from_beacon_state(
state: BeaconState,
spec: ChainSpec,
) -> Result<Self, BeaconStateError> {
let current_epoch = state.current_epoch(&spec);
let previous_epoch = if current_epoch == spec.genesis_epoch {
current_epoch
} else {
current_epoch.saturating_sub(1_u64)
};
let next_epoch = state.next_epoch(&spec);

let mut committees: Vec<Vec<CrosslinkCommittees>> = Vec::with_capacity(3);
let mut attestation_duties: Vec<AttestationDutyMap> = Vec::with_capacity(3);

if CACHE_PREVIOUS {
debug!("from_beacon_state: building previous epoch cache.");
let cache = build_epoch_cache(&state, previous_epoch, &spec)?;
committees.push(cache.committees);
attestation_duties.push(cache.attestation_duty_map);
} else {
committees.push(vec![]);
attestation_duties.push(HashMap::new());
}
if CACHE_CURRENT {
debug!("from_beacon_state: building current epoch cache.");
let cache = build_epoch_cache(&state, current_epoch, &spec)?;
committees.push(cache.committees);
attestation_duties.push(cache.attestation_duty_map);
} else {
committees.push(vec![]);
attestation_duties.push(HashMap::new());
}
if CACHE_NEXT {
debug!("from_beacon_state: building next epoch cache.");
let cache = build_epoch_cache(&state, next_epoch, &spec)?;
committees.push(cache.committees);
attestation_duties.push(cache.attestation_duty_map);
} else {
committees.push(vec![]);
attestation_duties.push(HashMap::new());
}

Ok(Self {
state,
committees,
attestation_duties,
next_epoch,
current_epoch,
previous_epoch,
spec,
})
}

fn slot_to_cache_index(&self, slot: Slot) -> Option<usize> {
trace!("slot_to_cache_index: cache lookup");
match slot.epoch(self.spec.epoch_length) {
epoch if (epoch == self.previous_epoch) & CACHE_PREVIOUS => Some(0),
epoch if (epoch == self.current_epoch) & CACHE_CURRENT => Some(1),
epoch if (epoch == self.next_epoch) & CACHE_NEXT => Some(2),
_ => None,
}
}

/// Returns the `slot`, `shard` and `committee_index` for which a validator must produce an
/// attestation.
///
/// Cached method.
///
/// Spec v0.2.0
pub fn attestation_slot_and_shard_for_validator(
&self,
validator_index: usize,
_spec: &ChainSpec,
) -> Result<Option<(Slot, u64, u64)>, BeaconStateError> {
// Get the result for this epoch.
let cache_index = self
.slot_to_cache_index(self.state.slot)
.expect("Current epoch should always have a cache index.");

let duties = self.attestation_duties[cache_index]
.get(&(validator_index as u64))
.and_then(|tuple| Some(*tuple));

Ok(duties)
}
}

struct EpochCacheResult {
committees: Vec<CrosslinkCommittees>,
attestation_duty_map: AttestationDutyMap,
}

fn build_epoch_cache(
state: &BeaconState,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<EpochCacheResult, BeaconStateError> {
let mut epoch_committees: Vec<CrosslinkCommittees> =
Vec::with_capacity(spec.epoch_length as usize);
let mut attestation_duty_map: AttestationDutyMap = HashMap::new();

for slot in epoch.slot_iter(spec.epoch_length) {
let slot_committees = state.get_crosslink_committees_at_slot(slot, false, spec)?;

for (committee, shard) in slot_committees {
for (committee_index, validator_index) in committee.iter().enumerate() {
attestation_duty_map.insert(
*validator_index as u64,
(slot, shard, committee_index as u64),
);
}
}

epoch_committees.push(state.get_crosslink_committees_at_slot(slot, false, spec)?)
}

Ok(EpochCacheResult {
committees: epoch_committees,
attestation_duty_map,
})
}
3 changes: 2 additions & 1 deletion beacon_node/beacon_chain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod attestation_aggregator;
mod beacon_chain;
mod cached_beacon_state;
mod checkpoint;

pub use self::beacon_chain::{BeaconChain, Error};
pub use self::checkpoint::CheckPoint;
pub use fork_choice::{ForkChoice, ForkChoiceAlgorithms, ForkChoiceError};
pub use fork_choice::{ForkChoice, ForkChoiceAlgorithm, ForkChoiceError};
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/test_harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ criterion = "0.2"
[dependencies]
attester = { path = "../../../eth2/attester" }
beacon_chain = { path = "../../beacon_chain" }
block_producer = { path = "../../../eth2/block_producer" }
block_proposer = { path = "../../../eth2/block_proposer" }
bls = { path = "../../../eth2/utils/bls" }
boolean-bitfield = { path = "../../../eth2/utils/boolean-bitfield" }
db = { path = "../../db" }
Expand Down
Loading

0 comments on commit 7842b0b

Please sign in to comment.