From 5fefc79521b7f0aa9d09941aee618c73af0c60d4 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 12 Feb 2019 13:00:52 +1100 Subject: [PATCH] Update `BeaconChain` for spec v0.2.0 --- beacon_node/beacon_chain/Cargo.toml | 1 + .../src/attestation_aggregator.rs | 5 +- beacon_node/beacon_chain/src/beacon_chain.rs | 85 +++++++++---------- beacon_node/beacon_chain/src/lmd_ghost.rs | 6 +- eth2/state_processing/src/lib.rs | 5 +- 5 files changed, 53 insertions(+), 49 deletions(-) diff --git a/beacon_node/beacon_chain/Cargo.toml b/beacon_node/beacon_chain/Cargo.toml index b80d001a2..493984191 100644 --- a/beacon_node/beacon_chain/Cargo.toml +++ b/beacon_node/beacon_chain/Cargo.toml @@ -20,4 +20,5 @@ serde_derive = "1.0" serde_json = "1.0" slot_clock = { path = "../../eth2/utils/slot_clock" } ssz = { path = "../../eth2/utils/ssz" } +state_processing = { path = "../../eth2/state_processing" } types = { path = "../../eth2/types" } diff --git a/beacon_node/beacon_chain/src/attestation_aggregator.rs b/beacon_node/beacon_chain/src/attestation_aggregator.rs index 0f6aa388e..cfba01308 100644 --- a/beacon_node/beacon_chain/src/attestation_aggregator.rs +++ b/beacon_node/beacon_chain/src/attestation_aggregator.rs @@ -1,3 +1,4 @@ +use state_processing::validate_attestation_without_signature; use std::collections::{HashMap, HashSet}; use types::{ beacon_state::CommitteesError, AggregateSignature, Attestation, AttestationData, BeaconState, @@ -172,9 +173,7 @@ impl AttestationAggregator { self.store .values() .filter_map(|attestation| { - if state - .validate_attestation_without_signature(attestation, spec) - .is_ok() + if validate_attestation_without_signature(&state, attestation, spec).is_ok() && !known_attestation_data.contains(&attestation.data) { Some(attestation.clone()) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index cde4d7cf6..55212ab20 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1,24 +1,25 @@ -use db::{ - stores::{BeaconBlockStore, BeaconStateStore}, - ClientDB, DBError, -}; -use genesis::{genesis_beacon_block, genesis_beacon_state}; -use log::{debug, trace}; -use parking_lot::{RwLock, RwLockReadGuard}; -use slot_clock::SlotClock; -use ssz::ssz_encode; -use std::sync::Arc; -use types::{ - beacon_state::{BlockProcessingError, CommitteesError, SlotProcessingError}, - readers::{BeaconBlockReader, BeaconStateReader}, - AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, ChainSpec, Eth1Data, - FreeAttestation, Hash256, PublicKey, Signature, Slot, -}; - use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome}; use crate::attestation_targets::AttestationTargets; use crate::block_graph::BlockGraph; use crate::checkpoint::CheckPoint; +use db::{ + stores::{BeaconBlockStore, BeaconStateStore}, + ClientDB, DBError, +}; +use log::{debug, trace}; +use parking_lot::{RwLock, RwLockReadGuard}; +use slot_clock::SlotClock; +use ssz::ssz_encode; +use state_processing::{ + BlockProcessable, BlockProcessingError, SlotProcessable, SlotProcessingError, +}; +use std::sync::Arc; +use types::{ + beacon_state::CommitteesError, + readers::{BeaconBlockReader, BeaconStateReader}, + AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, ChainSpec, Crosslink, Deposit, + Epoch, Eth1Data, FreeAttestation, Hash256, PublicKey, Signature, Slot, +}; #[derive(Debug, PartialEq)] pub enum Error { @@ -82,17 +83,25 @@ where state_store: Arc>, block_store: Arc>, slot_clock: U, + genesis_time: u64, + latest_eth1_data: Eth1Data, + initial_validator_deposits: Vec, spec: ChainSpec, ) -> Result { - if spec.initial_validators.is_empty() { + if initial_validator_deposits.is_empty() { return Err(Error::InsufficientValidators); } - let genesis_state = genesis_beacon_state(&spec); + let genesis_state = BeaconState::genesis( + genesis_time, + initial_validator_deposits, + latest_eth1_data, + &spec, + ); let state_root = genesis_state.canonical_root(); state_store.put(&state_root, &ssz_encode(&genesis_state)[..])?; - let genesis_block = genesis_beacon_block(state_root, &spec); + let genesis_block = BeaconBlock::genesis(state_root, &spec); let block_root = genesis_block.canonical_root(); block_store.put(&block_root, &ssz_encode(&genesis_block)[..])?; @@ -225,19 +234,6 @@ where None } - /// Returns the number of slots the validator has been required to propose. - /// - /// Returns `None` if the `validator_index` is invalid. - /// - /// Information is retrieved from the present `beacon_state.validator_registry`. - pub fn proposer_slots(&self, validator_index: usize) -> Option { - if let Some(validator) = self.state.read().validator_registry.get(validator_index) { - Some(validator.proposer_slots) - } else { - None - } - } - /// Reads the slot clock, returns `None` if the slot is unavailable. /// /// The slot might be unavailable due to an error with the system clock, or if the present time @@ -277,8 +273,8 @@ where } /// Returns the justified slot for the present state. - pub fn justified_slot(&self) -> Slot { - self.state.read().justified_slot + pub fn justified_epoch(&self) -> Epoch { + self.state.read().justified_epoch } /// Returns the attestation slot and shard for a given validator index. @@ -302,11 +298,14 @@ where /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. pub fn produce_attestation_data(&self, shard: u64) -> Result { - let justified_slot = self.justified_slot(); + let justified_epoch = self.justified_epoch(); let justified_block_root = self .state .read() - .get_block_root(justified_slot, &self.spec) + .get_block_root( + justified_epoch.start_slot(self.spec.epoch_length), + &self.spec, + ) .ok_or_else(|| Error::BadRecentBlockRoots)? .clone(); @@ -326,8 +325,11 @@ where beacon_block_root: self.head().beacon_block_root.clone(), epoch_boundary_root, shard_block_root: Hash256::zero(), - latest_crosslink_root: Hash256::zero(), - justified_slot, + latest_crosslink: Crosslink { + epoch: self.state.read().slot.epoch(self.spec.epoch_length), + shard_block_root: Hash256::zero(), + }, + justified_epoch, justified_block_root, }) } @@ -550,11 +552,8 @@ where signature: self.spec.empty_signature.clone(), // To be completed by a validator. body: BeaconBlockBody { proposer_slashings: vec![], - casper_slashings: vec![], + attester_slashings: vec![], attestations: attestations, - custody_reseeds: vec![], - custody_challenges: vec![], - custody_responses: vec![], deposits: vec![], exits: vec![], }, diff --git a/beacon_node/beacon_chain/src/lmd_ghost.rs b/beacon_node/beacon_chain/src/lmd_ghost.rs index 1af10b5e0..2c6fbe95a 100644 --- a/beacon_node/beacon_chain/src/lmd_ghost.rs +++ b/beacon_node/beacon_chain/src/lmd_ghost.rs @@ -68,8 +68,10 @@ where .into_beacon_state() .ok_or(Error::InvalidBeaconState(start_state_root))?; - let active_validator_indices = - get_active_validator_indices(&state.validator_registry, start.slot()); + let active_validator_indices = get_active_validator_indices( + &state.validator_registry, + start.slot().epoch(self.spec.epoch_length), + ); let mut attestation_targets = Vec::with_capacity(active_validator_indices.len()); for i in active_validator_indices { diff --git a/eth2/state_processing/src/lib.rs b/eth2/state_processing/src/lib.rs index ff5ae8660..18d1f7554 100644 --- a/eth2/state_processing/src/lib.rs +++ b/eth2/state_processing/src/lib.rs @@ -2,6 +2,9 @@ mod block_processable; mod epoch_processable; mod slot_processable; -pub use block_processable::{BlockProcessable, Error as BlockProcessingError}; +pub use block_processable::{ + validate_attestation, validate_attestation_without_signature, BlockProcessable, + Error as BlockProcessingError, +}; pub use epoch_processable::{EpochProcessable, Error as EpochProcessingError}; pub use slot_processable::{Error as SlotProcessingError, SlotProcessable};