2019-02-14 07:09:09 +00:00
|
|
|
use crate::{ForkChoice, ForkChoiceError};
|
|
|
|
use db::{stores::BeaconBlockStore, ClientDB};
|
2019-02-14 01:09:18 +00:00
|
|
|
use std::sync::Arc;
|
2019-02-18 06:49:05 +00:00
|
|
|
use types::{BeaconBlock, ChainSpec, Hash256, Slot};
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-02-14 07:09:09 +00:00
|
|
|
pub struct LongestChain<T>
|
|
|
|
where
|
|
|
|
T: ClientDB + Sized,
|
|
|
|
{
|
|
|
|
/// List of head block hashes
|
|
|
|
head_block_hashes: Vec<Hash256>,
|
|
|
|
/// Block storage access.
|
|
|
|
block_store: Arc<BeaconBlockStore<T>>,
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 07:09:09 +00:00
|
|
|
impl<T> LongestChain<T>
|
2019-02-14 01:09:18 +00:00
|
|
|
where
|
|
|
|
T: ClientDB + Sized,
|
|
|
|
{
|
2019-02-14 07:09:09 +00:00
|
|
|
pub fn new(block_store: Arc<BeaconBlockStore<T>>) -> Self {
|
|
|
|
LongestChain {
|
|
|
|
head_block_hashes: Vec::new(),
|
|
|
|
block_store,
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
2019-02-14 07:09:09 +00:00
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-02-14 07:09:09 +00:00
|
|
|
impl<T: ClientDB + Sized> ForkChoice for LongestChain<T> {
|
|
|
|
fn add_block(
|
|
|
|
&mut self,
|
|
|
|
block: &BeaconBlock,
|
|
|
|
block_hash: &Hash256,
|
2019-02-19 00:58:17 +00:00
|
|
|
_: &ChainSpec,
|
2019-02-14 07:09:09 +00:00
|
|
|
) -> Result<(), ForkChoiceError> {
|
|
|
|
// add the block hash to head_block_hashes removing the parent if it exists
|
|
|
|
self.head_block_hashes
|
|
|
|
.retain(|hash| *hash != block.parent_root);
|
|
|
|
self.head_block_hashes.push(*block_hash);
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-02-18 06:49:05 +00:00
|
|
|
fn add_attestation(
|
|
|
|
&mut self,
|
|
|
|
_: u64,
|
|
|
|
_: &Hash256,
|
|
|
|
_: &ChainSpec,
|
|
|
|
) -> Result<(), ForkChoiceError> {
|
2019-02-14 07:09:09 +00:00
|
|
|
// do nothing
|
|
|
|
Ok(())
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 06:49:05 +00:00
|
|
|
fn find_head(&mut self, _: &Hash256, _: &ChainSpec) -> Result<Hash256, ForkChoiceError> {
|
2019-02-14 07:09:09 +00:00
|
|
|
let mut head_blocks: Vec<(usize, BeaconBlock)> = vec![];
|
|
|
|
/*
|
|
|
|
* Load all the head_block hashes from the DB as SszBeaconBlocks.
|
|
|
|
*/
|
|
|
|
for (index, block_hash) in self.head_block_hashes.iter().enumerate() {
|
|
|
|
let block = self
|
|
|
|
.block_store
|
|
|
|
.get_deserialized(&block_hash)?
|
2019-02-15 02:32:37 +00:00
|
|
|
.ok_or_else(|| ForkChoiceError::MissingBeaconBlock(*block_hash))?;
|
2019-02-14 07:09:09 +00:00
|
|
|
head_blocks.push((index, block));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop through all the head blocks and find the highest slot.
|
|
|
|
*/
|
|
|
|
let highest_slot = head_blocks
|
|
|
|
.iter()
|
|
|
|
.fold(Slot::from(0u64), |highest, (_, block)| {
|
|
|
|
std::cmp::max(block.slot, highest)
|
|
|
|
});
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-02-14 07:09:09 +00:00
|
|
|
// if we find no blocks, return Error
|
|
|
|
if highest_slot == 0 {
|
|
|
|
return Err(ForkChoiceError::HeadNotFound);
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 07:09:09 +00:00
|
|
|
/*
|
|
|
|
* Loop through all the highest blocks and sort them by highest hash.
|
|
|
|
*
|
|
|
|
* Ultimately, the index of the head_block hash with the highest slot and highest block
|
|
|
|
* hash will be the winner.
|
|
|
|
*/
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-02-14 07:09:09 +00:00
|
|
|
let head_index: Option<usize> =
|
|
|
|
head_blocks
|
|
|
|
.iter()
|
|
|
|
.fold(None, |smallest_index, (index, block)| {
|
|
|
|
if block.slot == highest_slot {
|
|
|
|
if smallest_index.is_none() {
|
|
|
|
return Some(*index);
|
|
|
|
}
|
|
|
|
return Some(std::cmp::min(
|
|
|
|
*index,
|
|
|
|
smallest_index.expect("Cannot be None"),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
smallest_index
|
|
|
|
});
|
|
|
|
|
|
|
|
if head_index.is_none() {
|
|
|
|
return Err(ForkChoiceError::HeadNotFound);
|
|
|
|
}
|
2019-02-14 01:09:18 +00:00
|
|
|
|
2019-02-14 07:09:09 +00:00
|
|
|
Ok(self.head_block_hashes[head_index.unwrap()])
|
2019-02-14 01:09:18 +00:00
|
|
|
}
|
|
|
|
}
|