Update BeaconChain to spec v0.4.0

This commit is contained in:
Paul Hauner 2019-03-07 12:25:29 +11:00
parent e448882102
commit a4e604a41e
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 26 additions and 26 deletions

View File

@ -1,10 +1,8 @@
use log::trace; use log::trace;
use state_processing::validate_attestation_without_signature; use ssz::TreeHash;
use state_processing::per_block_processing::validate_attestation_without_signature;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use types::{ use types::*;
AggregateSignature, Attestation, AttestationData, BeaconState, BeaconStateError, Bitfield,
ChainSpec, FreeAttestation, Signature,
};
const PHASE_0_CUSTODY_BIT: bool = false; const PHASE_0_CUSTODY_BIT: bool = false;
@ -84,11 +82,11 @@ impl AttestationAggregator {
/// - The signature is verified against that of the validator at `validator_index`. /// - The signature is verified against that of the validator at `validator_index`.
pub fn process_free_attestation( pub fn process_free_attestation(
&mut self, &mut self,
cached_state: &BeaconState, state: &BeaconState,
free_attestation: &FreeAttestation, free_attestation: &FreeAttestation,
spec: &ChainSpec, spec: &ChainSpec,
) -> Result<Outcome, BeaconStateError> { ) -> Result<Outcome, BeaconStateError> {
let attestation_duties = match cached_state.attestation_slot_and_shard_for_validator( let attestation_duties = match state.attestation_slot_and_shard_for_validator(
free_attestation.validator_index as usize, free_attestation.validator_index as usize,
spec, spec,
) { ) {
@ -119,9 +117,13 @@ impl AttestationAggregator {
invalid_outcome!(Message::BadShard); invalid_outcome!(Message::BadShard);
} }
let signable_message = free_attestation.data.signable_message(PHASE_0_CUSTODY_BIT); let signable_message = AttestationDataAndCustodyBit {
data: free_attestation.data.clone(),
custody_bit: PHASE_0_CUSTODY_BIT,
}
.hash_tree_root();
let validator_record = match cached_state let validator_record = match state
.validator_registry .validator_registry
.get(free_attestation.validator_index as usize) .get(free_attestation.validator_index as usize)
{ {
@ -131,9 +133,7 @@ impl AttestationAggregator {
if !free_attestation.signature.verify( if !free_attestation.signature.verify(
&signable_message, &signable_message,
cached_state spec.get_domain(state.current_epoch(spec), Domain::Attestation, &state.fork),
.fork
.get_domain(cached_state.current_epoch(spec), spec.domain_attestation),
&validator_record.pubkey, &validator_record.pubkey,
) { ) {
invalid_outcome!(Message::BadSignature); invalid_outcome!(Message::BadSignature);

View File

@ -10,7 +10,8 @@ use parking_lot::{RwLock, RwLockReadGuard};
use slot_clock::SlotClock; use slot_clock::SlotClock;
use ssz::ssz_encode; use ssz::ssz_encode;
use state_processing::{ use state_processing::{
BlockProcessable, BlockProcessingError, SlotProcessable, SlotProcessingError, per_block_processing, per_block_processing_without_verifying_block_signature,
per_slot_processing, BlockProcessingError, SlotProcessingError,
}; };
use std::sync::Arc; use std::sync::Arc;
use types::{ use types::{
@ -65,7 +66,7 @@ pub struct BeaconChain<T: ClientDB + Sized, U: SlotClock, F: ForkChoice> {
pub slot_clock: U, pub slot_clock: U,
pub attestation_aggregator: RwLock<AttestationAggregator>, pub attestation_aggregator: RwLock<AttestationAggregator>,
pub deposits_for_inclusion: RwLock<Vec<Deposit>>, pub deposits_for_inclusion: RwLock<Vec<Deposit>>,
pub exits_for_inclusion: RwLock<Vec<Exit>>, pub exits_for_inclusion: RwLock<Vec<VoluntaryExit>>,
pub proposer_slashings_for_inclusion: RwLock<Vec<ProposerSlashing>>, pub proposer_slashings_for_inclusion: RwLock<Vec<ProposerSlashing>>,
pub attester_slashings_for_inclusion: RwLock<Vec<AttesterSlashing>>, pub attester_slashings_for_inclusion: RwLock<Vec<AttesterSlashing>>,
canonical_head: RwLock<CheckPoint>, canonical_head: RwLock<CheckPoint>,
@ -214,9 +215,7 @@ where
let state_slot = self.state.read().slot; let state_slot = self.state.read().slot;
let head_block_root = self.head().beacon_block_root; let head_block_root = self.head().beacon_block_root;
for _ in state_slot.as_u64()..slot.as_u64() { for _ in state_slot.as_u64()..slot.as_u64() {
self.state per_slot_processing(&mut *self.state.write(), head_block_root, &self.spec)?;
.write()
.per_slot_processing(head_block_root, &self.spec)?;
} }
Ok(()) Ok(())
} }
@ -333,10 +332,10 @@ where
shard, shard,
beacon_block_root: self.head().beacon_block_root, beacon_block_root: self.head().beacon_block_root,
epoch_boundary_root, epoch_boundary_root,
shard_block_root: Hash256::zero(), crosslink_data_root: Hash256::zero(),
latest_crosslink: Crosslink { latest_crosslink: Crosslink {
epoch: self.state.read().slot.epoch(self.spec.slots_per_epoch), epoch: self.state.read().slot.epoch(self.spec.slots_per_epoch),
shard_block_root: Hash256::zero(), crosslink_data_root: Hash256::zero(),
}, },
justified_epoch, justified_epoch,
justified_block_root, justified_block_root,
@ -411,7 +410,7 @@ where
} }
/// Accept some exit and queue it for inclusion in an appropriate block. /// Accept some exit and queue it for inclusion in an appropriate block.
pub fn receive_exit_for_inclusion(&self, exit: Exit) { pub fn receive_exit_for_inclusion(&self, exit: VoluntaryExit) {
// TODO: exits are not checked for validity; check them. // TODO: exits are not checked for validity; check them.
// //
// https://github.com/sigp/lighthouse/issues/276 // https://github.com/sigp/lighthouse/issues/276
@ -419,7 +418,7 @@ where
} }
/// Return a vec of exits suitable for inclusion in some block. /// Return a vec of exits suitable for inclusion in some block.
pub fn get_exits_for_block(&self) -> Vec<Exit> { pub fn get_exits_for_block(&self) -> Vec<VoluntaryExit> {
// TODO: exits are indiscriminately included; check them for validity. // TODO: exits are indiscriminately included; check them for validity.
// //
// https://github.com/sigp/lighthouse/issues/275 // https://github.com/sigp/lighthouse/issues/275
@ -430,7 +429,7 @@ where
/// inclusion queue. /// inclusion queue.
/// ///
/// This ensures that `Deposits` are not included twice in successive blocks. /// This ensures that `Deposits` are not included twice in successive blocks.
pub fn set_exits_as_included(&self, included_exits: &[Exit]) { pub fn set_exits_as_included(&self, included_exits: &[VoluntaryExit]) {
// TODO: method does not take forks into account; consider this. // TODO: method does not take forks into account; consider this.
let mut indices_to_delete = vec![]; let mut indices_to_delete = vec![];
@ -647,7 +646,7 @@ where
// Transition the parent state to the present slot. // Transition the parent state to the present slot.
let mut state = parent_state; let mut state = parent_state;
for _ in state.slot.as_u64()..present_slot.as_u64() { for _ in state.slot.as_u64()..present_slot.as_u64() {
if let Err(e) = state.per_slot_processing(parent_block_root, &self.spec) { if let Err(e) = per_slot_processing(&mut state, parent_block_root, &self.spec) {
return Ok(BlockProcessingOutcome::InvalidBlock( return Ok(BlockProcessingOutcome::InvalidBlock(
InvalidBlock::SlotProcessingError(e), InvalidBlock::SlotProcessingError(e),
)); ));
@ -656,7 +655,7 @@ where
// Apply the received block to its parent state (which has been transitioned into this // Apply the received block to its parent state (which has been transitioned into this
// slot). // slot).
if let Err(e) = state.per_block_processing(&block, &self.spec) { if let Err(e) = per_block_processing(&mut state, &block, &self.spec) {
return Ok(BlockProcessingOutcome::InvalidBlock( return Ok(BlockProcessingOutcome::InvalidBlock(
InvalidBlock::PerBlockProcessingError(e), InvalidBlock::PerBlockProcessingError(e),
)); ));
@ -736,14 +735,15 @@ where
attester_slashings: self.get_attester_slashings_for_block(), attester_slashings: self.get_attester_slashings_for_block(),
attestations, attestations,
deposits: self.get_deposits_for_block(), deposits: self.get_deposits_for_block(),
exits: self.get_exits_for_block(), voluntary_exits: self.get_exits_for_block(),
transfers: vec![],
}, },
}; };
trace!("BeaconChain::produce_block: updating state for new block.",); trace!("BeaconChain::produce_block: updating state for new block.",);
let result = let result =
state.per_block_processing_without_verifying_block_signature(&block, &self.spec); per_block_processing_without_verifying_block_signature(&mut state, &block, &self.spec);
debug!( debug!(
"BeaconNode::produce_block: state processing result: {:?}", "BeaconNode::produce_block: state processing result: {:?}",
result result