From 5a21e19a31adc2cf37f59d0beb643dc4c425d87d Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 7 Mar 2019 12:53:15 +1100 Subject: [PATCH] Fix all compile errors from v0.4.0 update --- beacon_node/beacon_chain/src/beacon_chain.rs | 34 ++++++------------- beacon_node/beacon_chain/src/errors.rs | 33 ++++++++++++++++++ beacon_node/beacon_chain/src/lib.rs | 6 ++-- .../test_harness/src/beacon_chain_harness.rs | 13 +++---- .../test_harness/src/test_case.rs | 19 ++++------- .../test_harness/src/test_case/state_check.rs | 4 +-- .../validator_harness/direct_beacon_node.rs | 4 +-- beacon_node/src/main.rs | 2 +- eth2/fork_choice/tests/tests.rs | 9 ++--- eth2/types/src/attester_slashing/builder.rs | 6 +--- .../beacon_block_grpc_client.rs | 3 +- validator_client/src/main.rs | 6 ++-- 12 files changed, 74 insertions(+), 65 deletions(-) create mode 100644 beacon_node/beacon_chain/src/errors.rs diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 93a806864..653b2900d 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1,5 +1,6 @@ use crate::attestation_aggregator::{AttestationAggregator, Outcome as AggregationOutcome}; use crate::checkpoint::CheckPoint; +use crate::errors::{BeaconChainError as Error, BlockProductionError}; use db::{ stores::{BeaconBlockStore, BeaconStateStore}, ClientDB, DBError, @@ -19,18 +20,6 @@ use types::{ *, }; -#[derive(Debug, PartialEq)] -pub enum Error { - InsufficientValidators, - BadRecentBlockRoots, - BeaconStateError(BeaconStateError), - DBInconsistent(String), - DBError(String), - ForkChoiceError(ForkChoiceError), - MissingBeaconBlock(Hash256), - MissingBeaconState(Hash256), -} - #[derive(Debug, PartialEq)] pub enum ValidBlock { /// The block was successfully processed. @@ -700,7 +689,10 @@ where /// /// The produced block will not be inherently valid, it must be signed by a block producer. /// Block signing is out of the scope of this function and should be done by a separate program. - pub fn produce_block(&self, randao_reveal: Signature) -> Option<(BeaconBlock, BeaconState)> { + pub fn produce_block( + &self, + randao_reveal: Signature, + ) -> Result<(BeaconBlock, BeaconState), BlockProductionError> { debug!("Producing block at slot {}...", self.state.read().slot); let mut state = self.state.read().clone(); @@ -717,7 +709,9 @@ where attestations.len() ); - let parent_root = *state.get_block_root(state.slot.saturating_sub(1_u64), &self.spec)?; + let parent_root = *state + .get_block_root(state.slot.saturating_sub(1_u64), &self.spec) + .ok_or_else(|| BlockProductionError::UnableToGetBlockRootFromState)?; let mut block = BeaconBlock { slot: state.slot, @@ -742,21 +736,13 @@ where trace!("BeaconChain::produce_block: updating state for new block.",); - let result = - per_block_processing_without_verifying_block_signature(&mut state, &block, &self.spec); - debug!( - "BeaconNode::produce_block: state processing result: {:?}", - result - ); - result.ok()?; + per_block_processing_without_verifying_block_signature(&mut state, &block, &self.spec)?; let state_root = state.canonical_root(); block.state_root = state_root; - trace!("Block produced."); - - Some((block, state)) + Ok((block, state)) } // TODO: Left this as is, modify later diff --git a/beacon_node/beacon_chain/src/errors.rs b/beacon_node/beacon_chain/src/errors.rs new file mode 100644 index 000000000..58c3f87ae --- /dev/null +++ b/beacon_node/beacon_chain/src/errors.rs @@ -0,0 +1,33 @@ +use fork_choice::ForkChoiceError; +use state_processing::BlockProcessingError; +use types::*; + +macro_rules! easy_from_to { + ($from: ident, $to: ident) => { + impl From<$from> for $to { + fn from(e: $from) -> $to { + $to::$from(e) + } + } + }; +} + +#[derive(Debug, PartialEq)] +pub enum BeaconChainError { + InsufficientValidators, + BadRecentBlockRoots, + BeaconStateError(BeaconStateError), + DBInconsistent(String), + DBError(String), + ForkChoiceError(ForkChoiceError), + MissingBeaconBlock(Hash256), + MissingBeaconState(Hash256), +} + +#[derive(Debug, PartialEq)] +pub enum BlockProductionError { + UnableToGetBlockRootFromState, + BlockProcessingError(BlockProcessingError), +} + +easy_from_to!(BlockProcessingError, BlockProductionError); diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index bd3ee0788..0e879a415 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -1,9 +1,9 @@ mod attestation_aggregator; mod beacon_chain; mod checkpoint; +mod errors; -pub use self::beacon_chain::{ - BeaconChain, BlockProcessingOutcome, Error, InvalidBlock, ValidBlock, -}; +pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; pub use self::checkpoint::CheckPoint; +pub use self::errors::BeaconChainError; pub use fork_choice::{ForkChoice, ForkChoiceAlgorithm, ForkChoiceError}; diff --git a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs index 105a6d6e1..38c41e38c 100644 --- a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs +++ b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs @@ -1,6 +1,6 @@ use super::ValidatorHarness; use beacon_chain::{BeaconChain, BlockProcessingOutcome}; -pub use beacon_chain::{CheckPoint, Error as BeaconChainError}; +pub use beacon_chain::{BeaconChainError, CheckPoint}; use bls::create_proof_of_possession; use db::{ stores::{BeaconBlockStore, BeaconStateStore}, @@ -250,16 +250,13 @@ impl BeaconChainHarness { validator_index: usize, message: &[u8], epoch: Epoch, - domain_type: u64, + domain_type: Domain, ) -> Option { let validator = self.validators.get(validator_index)?; let domain = self - .beacon_chain - .state - .read() - .fork - .get_domain(epoch, domain_type); + .spec + .get_domain(epoch, domain_type, &self.beacon_chain.state.read().fork); Some(Signature::new(message, domain, &validator.keypair.sk)) } @@ -285,7 +282,7 @@ impl BeaconChainHarness { /// Note: the `ValidatorHarness` for this validator continues to exist. Once it is exited it /// will stop receiving duties from the beacon chain and just do nothing when prompted to /// produce/attest. - pub fn add_exit(&mut self, exit: Exit) { + pub fn add_exit(&mut self, exit: VoluntaryExit) { self.beacon_chain.receive_exit_for_inclusion(exit); } diff --git a/beacon_node/beacon_chain/test_harness/src/test_case.rs b/beacon_node/beacon_chain/test_harness/src/test_case.rs index 1a5ebfbe1..cde3f55a8 100644 --- a/beacon_node/beacon_chain/test_harness/src/test_case.rs +++ b/beacon_node/beacon_chain/test_harness/src/test_case.rs @@ -200,14 +200,14 @@ impl TestCase { } } -fn build_exit(harness: &BeaconChainHarness, validator_index: u64) -> Exit { +fn build_exit(harness: &BeaconChainHarness, validator_index: u64) -> VoluntaryExit { let epoch = harness .beacon_chain .state .read() .current_epoch(&harness.spec); - let mut exit = Exit { + let mut exit = VoluntaryExit { epoch, validator_index, signature: Signature::empty_signature(), @@ -216,13 +216,8 @@ fn build_exit(harness: &BeaconChainHarness, validator_index: u64) -> Exit { let message = exit.hash_tree_root(); exit.signature = harness - .validator_sign( - validator_index as usize, - &message[..], - epoch, - harness.spec.domain_exit, - ) - .expect("Unable to sign Exit"); + .validator_sign(validator_index as usize, &message[..], epoch, Domain::Exit) + .expect("Unable to sign VoluntaryExit"); exit } @@ -234,20 +229,20 @@ fn build_double_vote_attester_slashing( harness: &BeaconChainHarness, validator_indices: &[u64], ) -> AttesterSlashing { - let signer = |validator_index: u64, message: &[u8], epoch: Epoch, domain: u64| { + let signer = |validator_index: u64, message: &[u8], epoch: Epoch, domain: Domain| { harness .validator_sign(validator_index as usize, message, epoch, domain) .expect("Unable to sign AttesterSlashing") }; - AttesterSlashingBuilder::double_vote(validator_indices, signer, &harness.spec) + AttesterSlashingBuilder::double_vote(validator_indices, signer) } /// Builds an `ProposerSlashing` for some `validator_index`. /// /// Signs the message using a `BeaconChainHarness`. fn build_proposer_slashing(harness: &BeaconChainHarness, validator_index: u64) -> ProposerSlashing { - let signer = |validator_index: u64, message: &[u8], epoch: Epoch, domain: u64| { + let signer = |validator_index: u64, message: &[u8], epoch: Epoch, domain: Domain| { harness .validator_sign(validator_index as usize, message, epoch, domain) .expect("Unable to sign AttesterSlashing") diff --git a/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs b/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs index b52dc7d6b..6fa75364a 100644 --- a/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs +++ b/beacon_node/beacon_chain/test_harness/src/test_case/state_check.rs @@ -66,7 +66,7 @@ impl StateCheck { .iter() .enumerate() .filter_map(|(i, validator)| { - if validator.is_penalized_at(state_epoch) { + if validator.slashed { Some(i as u64) } else { None @@ -108,7 +108,7 @@ impl StateCheck { .iter() .enumerate() .filter_map(|(i, validator)| { - if validator.has_initiated_exit() { + if validator.initiated_exit { Some(i as u64) } else { None diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs index 06d3e7c72..d2de354d7 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs @@ -80,8 +80,8 @@ impl BeaconBlockNode for DirectBeaconN let (block, _state) = self .beacon_chain .produce_block(randao_reveal.clone()) - .ok_or_else(|| { - BeaconBlockNodeError::RemoteFailure("Did not produce block.".to_string()) + .map_err(|e| { + BeaconBlockNodeError::RemoteFailure(format!("Did not produce block: {:?}", e)) })?; if block.slot == slot { diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index b9ef2c8a7..072315b6b 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -78,7 +78,7 @@ fn main() { // Slot clock let genesis_time = 1_549_935_547; // 12th Feb 2018 (arbitrary value in the past). - let slot_clock = SystemTimeSlotClock::new(genesis_time, spec.slot_duration) + let slot_clock = SystemTimeSlotClock::new(genesis_time, spec.seconds_per_slot) .expect("Unable to load SystemTimeSlotClock"); // Choose the fork choice let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); diff --git a/eth2/fork_choice/tests/tests.rs b/eth2/fork_choice/tests/tests.rs index 1d93cd0db..a3cab6a7c 100644 --- a/eth2/fork_choice/tests/tests.rs +++ b/eth2/fork_choice/tests/tests.rs @@ -81,7 +81,8 @@ fn test_yaml_vectors( attester_slashings: vec![], attestations: vec![], deposits: vec![], - exits: vec![], + voluntary_exits: vec![], + transfers: vec![], }; // process the tests @@ -249,9 +250,9 @@ fn setup_inital_state( withdrawal_credentials: zero_hash, activation_epoch: Epoch::from(0u64), exit_epoch: spec.far_future_epoch, - withdrawal_epoch: spec.far_future_epoch, - penalized_epoch: spec.far_future_epoch, - status_flags: None, + withdrawable_epoch: spec.far_future_epoch, + initiated_exit: false, + slashed: false, }; // activate the validators for _ in 0..no_validators { diff --git a/eth2/types/src/attester_slashing/builder.rs b/eth2/types/src/attester_slashing/builder.rs index 46dcaf174..05301f30b 100644 --- a/eth2/types/src/attester_slashing/builder.rs +++ b/eth2/types/src/attester_slashing/builder.rs @@ -15,11 +15,7 @@ impl AttesterSlashingBuilder { /// - `domain: Domain` /// /// Where domain is a domain "constant" (e.g., `spec.domain_attestation`). - pub fn double_vote( - validator_indices: &[u64], - signer: F, - spec: &ChainSpec, - ) -> AttesterSlashing + pub fn double_vote(validator_indices: &[u64], signer: F) -> AttesterSlashing where F: Fn(u64, &[u8], Epoch, Domain) -> Signature, { diff --git a/validator_client/src/block_producer_service/beacon_block_grpc_client.rs b/validator_client/src/block_producer_service/beacon_block_grpc_client.rs index 6bf3005d4..6ce5c0fa0 100644 --- a/validator_client/src/block_producer_service/beacon_block_grpc_client.rs +++ b/validator_client/src/block_producer_service/beacon_block_grpc_client.rs @@ -63,7 +63,8 @@ impl BeaconNode for BeaconBlockGrpcClient { attester_slashings: vec![], attestations: vec![], deposits: vec![], - exits: vec![], + voluntary_exits: vec![], + transfers: vec![], }, })) } else { diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index eea213a45..ebab8538c 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -111,13 +111,13 @@ fn main() { let genesis_time = 1_549_935_547; let slot_clock = { info!(log, "Genesis time"; "unix_epoch_seconds" => genesis_time); - let clock = SystemTimeSlotClock::new(genesis_time, spec.slot_duration) + let clock = SystemTimeSlotClock::new(genesis_time, spec.seconds_per_slot) .expect("Unable to instantiate SystemTimeSlotClock."); Arc::new(clock) }; - let poll_interval_millis = spec.slot_duration * 1000 / 10; // 10% epoch time precision. - info!(log, "Starting block producer service"; "polls_per_epoch" => spec.slot_duration * 1000 / poll_interval_millis); + let poll_interval_millis = spec.seconds_per_slot * 1000 / 10; // 10% epoch time precision. + info!(log, "Starting block producer service"; "polls_per_epoch" => spec.seconds_per_slot * 1000 / poll_interval_millis); /* * Start threads.