Updates all fork-choices to use ChainSpec for consts.

This commit is contained in:
Age Manning 2019-02-18 17:49:05 +11:00
parent 6e6d451978
commit 4eddb47fd0
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
4 changed files with 44 additions and 24 deletions

View File

@ -357,6 +357,7 @@ where
self.fork_choice.write().add_attestation( self.fork_choice.write().add_attestation(
free_attestation.validator_index, free_attestation.validator_index,
&free_attestation.data.beacon_block_root, &free_attestation.data.beacon_block_root,
&self.spec,
)?; )?;
Ok(aggregation_outcome) Ok(aggregation_outcome)
} }
@ -486,7 +487,9 @@ where
self.state_store.put(&state_root, &ssz_encode(&state)[..])?; self.state_store.put(&state_root, &ssz_encode(&state)[..])?;
// run the fork_choice add_block logic // 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. // If the parent block was the parent_block, automatically update the canonical head.
// //
@ -575,7 +578,10 @@ where
pub fn fork_choice(&self) -> Result<(), Error> { pub fn fork_choice(&self) -> Result<(), Error> {
let present_head = self.finalized_head().beacon_block_root; 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 { if new_head != present_head {
let block = self let block = self

View File

@ -1,7 +1,7 @@
use crate::{ForkChoice, ForkChoiceError}; use crate::{ForkChoice, ForkChoiceError};
use db::{stores::BeaconBlockStore, ClientDB}; use db::{stores::BeaconBlockStore, ClientDB};
use std::sync::Arc; use std::sync::Arc;
use types::{BeaconBlock, Hash256, Slot}; use types::{BeaconBlock, ChainSpec, Hash256, Slot};
pub struct LongestChain<T> pub struct LongestChain<T>
where where
@ -30,6 +30,7 @@ impl<T: ClientDB + Sized> ForkChoice for LongestChain<T> {
&mut self, &mut self,
block: &BeaconBlock, block: &BeaconBlock,
block_hash: &Hash256, block_hash: &Hash256,
spec: &ChainSpec,
) -> Result<(), ForkChoiceError> { ) -> Result<(), ForkChoiceError> {
// add the block hash to head_block_hashes removing the parent if it exists // add the block hash to head_block_hashes removing the parent if it exists
self.head_block_hashes self.head_block_hashes
@ -38,12 +39,17 @@ impl<T: ClientDB + Sized> ForkChoice for LongestChain<T> {
Ok(()) Ok(())
} }
fn add_attestation(&mut self, _: u64, _: &Hash256) -> Result<(), ForkChoiceError> { fn add_attestation(
&mut self,
_: u64,
_: &Hash256,
_: &ChainSpec,
) -> Result<(), ForkChoiceError> {
// do nothing // do nothing
Ok(()) Ok(())
} }
fn find_head(&mut self, _: &Hash256) -> Result<Hash256, ForkChoiceError> { fn find_head(&mut self, _: &Hash256, _: &ChainSpec) -> Result<Hash256, ForkChoiceError> {
let mut head_blocks: Vec<(usize, BeaconBlock)> = vec![]; let mut head_blocks: Vec<(usize, BeaconBlock)> = vec![];
/* /*
* Load all the head_block hashes from the DB as SszBeaconBlocks. * Load all the head_block hashes from the DB as SszBeaconBlocks.

View File

@ -347,7 +347,7 @@ impl<T: ClientDB + Sized> ForkChoice for OptimisedLMDGhost<T> {
let mut current_head = *justified_block_start; let mut current_head = *justified_block_start;
let mut latest_votes = self.get_latest_votes(&state_root, block_slot)?; let mut latest_votes = self.get_latest_votes(&state_root, &block_slot, spec)?;
// remove any votes that don't relate to our current head. // remove any votes that don't relate to our current head.
latest_votes latest_votes
@ -370,6 +370,7 @@ impl<T: ClientDB + Sized> ForkChoice for OptimisedLMDGhost<T> {
if let Some(clear_winner) = self.get_clear_winner( if let Some(clear_winner) = self.get_clear_winner(
&latest_votes, &latest_votes,
block_height - (block_height % u64::from(step)) + u64::from(step), block_height - (block_height % u64::from(step)) + u64::from(step),
spec,
) { ) {
current_head = clear_winner; current_head = clear_winner;
break; break;

View File

@ -29,17 +29,11 @@ use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use types::{ use types::{
readers::BeaconBlockReader, validator_registry::get_active_validator_indices, BeaconBlock, readers::BeaconBlockReader, validator_registry::get_active_validator_indices, BeaconBlock,
Hash256, Slot, ChainSpec, Hash256, Slot,
}; };
//TODO: Pruning and syncing //TODO: Pruning and syncing
//TODO: Sort out global constants
const GENESIS_SLOT: u64 = 0;
const FORK_CHOICE_BALANCE_INCREMENT: u64 = 1e9 as u64;
const MAX_DEPOSIT_AMOUNT: u64 = 32e9 as u64;
const EPOCH_LENGTH: u64 = 64;
pub struct SlowLMDGhost<T: ClientDB + Sized> { pub struct SlowLMDGhost<T: ClientDB + Sized> {
/// The latest attestation targets as a map of validator index to block hash. /// The latest attestation targets as a map of validator index to block hash.
//TODO: Could this be a fixed size vec //TODO: Could this be a fixed size vec
@ -70,12 +64,14 @@ where
pub fn get_latest_votes( pub fn get_latest_votes(
&self, &self,
state_root: &Hash256, state_root: &Hash256,
block_slot: Slot, block_slot: &Slot,
spec: &ChainSpec,
) -> Result<HashMap<Hash256, u64>, ForkChoiceError> { ) -> Result<HashMap<Hash256, u64>, ForkChoiceError> {
// get latest votes // get latest votes
// Note: Votes are weighted by min(balance, MAX_DEPOSIT_AMOUNT) // // Note: Votes are weighted by min(balance, MAX_DEPOSIT_AMOUNT) //
// FORK_CHOICE_BALANCE_INCREMENT // FORK_CHOICE_BALANCE_INCREMENT
// build a hashmap of block_hash to weighted votes // build a hashmap of block_hash to weighted votes
trace!("FORKCHOICE: Getting the latest votes");
let mut latest_votes: HashMap<Hash256, u64> = HashMap::new(); let mut latest_votes: HashMap<Hash256, u64> = HashMap::new();
// gets the current weighted votes // gets the current weighted votes
let current_state = self let current_state = self
@ -84,21 +80,26 @@ where
.ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?; .ok_or_else(|| ForkChoiceError::MissingBeaconState(*state_root))?;
let active_validator_indices = get_active_validator_indices( let active_validator_indices = get_active_validator_indices(
&current_state.validator_registry, &current_state.validator_registry[..],
block_slot.epoch(EPOCH_LENGTH), block_slot.epoch(spec.epoch_length),
);
trace!(
"FORKCHOICE: Active validator indicies: {:?}",
active_validator_indices
); );
for index in active_validator_indices { for index in active_validator_indices {
let balance = let balance = std::cmp::min(
std::cmp::min(current_state.validator_balances[index], MAX_DEPOSIT_AMOUNT) current_state.validator_balances[index],
/ FORK_CHOICE_BALANCE_INCREMENT; spec.max_deposit_amount,
) / spec.fork_choice_balance_increment;
if balance > 0 { if balance > 0 {
if let Some(target) = self.latest_attestation_targets.get(&(index as u64)) { if let Some(target) = self.latest_attestation_targets.get(&(index as u64)) {
*latest_votes.entry(*target).or_insert_with(|| 0) += balance; *latest_votes.entry(*target).or_insert_with(|| 0) += balance;
} }
} }
} }
trace!("FORKCHOICE: Latest votes: {:?}", latest_votes);
Ok(latest_votes) Ok(latest_votes)
} }
@ -136,6 +137,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
&mut self, &mut self,
block: &BeaconBlock, block: &BeaconBlock,
block_hash: &Hash256, block_hash: &Hash256,
_: &ChainSpec,
) -> Result<(), ForkChoiceError> { ) -> Result<(), ForkChoiceError> {
// build the children hashmap // build the children hashmap
// add the new block to the children of parent // add the new block to the children of parent
@ -153,6 +155,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
&mut self, &mut self,
validator_index: u64, validator_index: u64,
target_block_root: &Hash256, target_block_root: &Hash256,
spec: &ChainSpec,
) -> Result<(), ForkChoiceError> { ) -> Result<(), ForkChoiceError> {
// simply add the attestation to the latest_attestation_target if the block_height is // simply add the attestation to the latest_attestation_target if the block_height is
// larger // larger
@ -168,7 +171,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
.get_deserialized(&target_block_root)? .get_deserialized(&target_block_root)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*target_block_root))? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*target_block_root))?
.slot() .slot()
.height(Slot::from(GENESIS_SLOT)); .height(Slot::from(spec.genesis_slot));
// get the height of the past target block // get the height of the past target block
let past_block_height = self let past_block_height = self
@ -176,7 +179,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
.get_deserialized(&attestation_target)? .get_deserialized(&attestation_target)?
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))? .ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*attestation_target))?
.slot() .slot()
.height(Slot::from(GENESIS_SLOT)); .height(Slot::from(spec.genesis_slot));
// update the attestation only if the new target is higher // update the attestation only if the new target is higher
if past_block_height < block_height { if past_block_height < block_height {
*attestation_target = *target_block_root; *attestation_target = *target_block_root;
@ -186,7 +189,11 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
} }
/// A very inefficient implementation of LMD ghost. /// A very inefficient implementation of LMD ghost.
fn find_head(&mut self, justified_block_start: &Hash256) -> Result<Hash256, ForkChoiceError> { fn find_head(
&mut self,
justified_block_start: &Hash256,
spec: &ChainSpec,
) -> Result<Hash256, ForkChoiceError> {
let start = self let start = self
.block_store .block_store
.get_deserialized(&justified_block_start)? .get_deserialized(&justified_block_start)?
@ -194,7 +201,7 @@ impl<T: ClientDB + Sized> ForkChoice for SlowLMDGhost<T> {
let start_state_root = start.state_root(); let start_state_root = start.state_root();
let latest_votes = self.get_latest_votes(&start_state_root, start.slot())?; let latest_votes = self.get_latest_votes(&start_state_root, &start.slot(), spec)?;
let mut head_hash = Hash256::zero(); let mut head_hash = Hash256::zero();