From 1097c8089b5baa6da8280cb722098d90228e9fcd Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 1 Mar 2019 16:54:59 +1100 Subject: [PATCH] Add naive deposit-handling to BeaconChain --- beacon_node/beacon_chain/src/beacon_chain.rs | 39 +++++++++++++++++-- .../state_processing/src/block_processable.rs | 28 ++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 1065f661d6..80cc793051 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -15,9 +15,7 @@ use state_processing::{ use std::sync::Arc; use types::{ readers::{BeaconBlockReader, BeaconStateReader}, - AttestationData, BeaconBlock, BeaconBlockBody, BeaconState, BeaconStateError, ChainSpec, - Crosslink, Deposit, Epoch, Eth1Data, FreeAttestation, Hash256, PublicKey, RelativeEpoch, - Signature, Slot, + *, }; #[derive(Debug, PartialEq)] @@ -66,6 +64,7 @@ pub struct BeaconChain { pub state_store: Arc>, pub slot_clock: U, pub attestation_aggregator: RwLock, + pub deposits_for_inclusion: RwLock>, canonical_head: RwLock, finalized_head: RwLock, pub state: RwLock, @@ -132,6 +131,7 @@ where state_store, slot_clock, attestation_aggregator, + deposits_for_inclusion: RwLock::new(vec![]), state: RwLock::new(genesis_state), finalized_head, canonical_head, @@ -364,6 +364,34 @@ where Ok(aggregation_outcome) } + pub fn receive_deposit_for_inclusion(&self, deposit: Deposit) { + // TODO: deposits are not check for validity; check them. + self.deposits_for_inclusion.write().push(deposit); + } + + pub fn get_deposits_for_block(&self) -> Vec { + // TODO: deposits are indiscriminately included; check them for validity. + self.deposits_for_inclusion.read().clone() + } + + pub fn mark_deposits_as_included(&self, included_deposits: &[Deposit]) { + // TODO: method does not take forks into account; consider this. + let mut indices_to_delete = vec![]; + + for included in included_deposits { + for (i, for_inclusion) in self.deposits_for_inclusion.read().iter().enumerate() { + if included == for_inclusion { + indices_to_delete.push(i); + } + } + } + + let deposits_for_inclusion = &mut self.deposits_for_inclusion.write(); + for i in indices_to_delete { + deposits_for_inclusion.remove(i); + } + } + /// Dumps the entire canonical chain, from the head to genesis to a vector for analysis. /// /// This could be a very expensive operation and should only be done in testing/analysis @@ -488,6 +516,9 @@ where self.block_store.put(&block_root, &ssz_encode(&block)[..])?; self.state_store.put(&state_root, &ssz_encode(&state)[..])?; + // Remove any included deposits from the for-inclusion queue + self.mark_deposits_as_included(&block.body.deposits[..]); + // run the fork_choice add_block logic self.fork_choice .write() @@ -544,7 +575,7 @@ where proposer_slashings: vec![], attester_slashings: vec![], attestations, - deposits: vec![], + deposits: self.get_deposits_for_block(), exits: vec![], }, }; diff --git a/eth2/state_processing/src/block_processable.rs b/eth2/state_processing/src/block_processable.rs index effaa65079..aab87c3ad2 100644 --- a/eth2/state_processing/src/block_processable.rs +++ b/eth2/state_processing/src/block_processable.rs @@ -3,10 +3,7 @@ use hashing::hash; use int_to_bytes::int_to_bytes32; use log::{debug, trace}; use ssz::{ssz_encode, TreeHash}; -use types::{ - AggregatePublicKey, Attestation, BeaconBlock, BeaconState, BeaconStateError, ChainSpec, - Crosslink, Epoch, Exit, Fork, Hash256, PendingAttestation, PublicKey, RelativeEpoch, Signature, -}; +use types::*; // TODO: define elsehwere. const DOMAIN_PROPOSAL: u64 = 2; @@ -35,6 +32,7 @@ pub enum Error { InvalidAttestation(AttestationValidationError), NoBlockRoot, MaxDepositsExceeded, + BadDeposit, MaxExitsExceeded, BadExit, BadCustodyReseeds, @@ -242,7 +240,27 @@ fn per_block_processing_signature_optional( Error::MaxDepositsExceeded ); - // TODO: process deposits. + // TODO: verify deposit merkle branches. + for deposit in &block.body.deposits { + debug!( + "Processing deposit for pubkey {:?}", + deposit.deposit_data.deposit_input.pubkey + ); + state + .process_deposit( + deposit.deposit_data.deposit_input.pubkey.clone(), + deposit.deposit_data.amount, + deposit + .deposit_data + .deposit_input + .proof_of_possession + .clone(), + deposit.deposit_data.deposit_input.withdrawal_credentials, + None, + spec, + ) + .map_err(|_| Error::BadDeposit)?; + } /* * Exits