Forwards block root iterators (#672)

* Implement forwards block root iterators

* Clean up errors and docs
This commit is contained in:
Michael Sproul
2019-12-06 18:52:11 +11:00
committed by Paul Hauner
parent 779873680b
commit bd1b61a5b1
23 changed files with 573 additions and 187 deletions

View File

@@ -10,7 +10,7 @@ pub type Result<T> = std::result::Result<T, String>;
// Note: the `PartialEq` bound is only required for testing. If it becomes a serious annoyance we
// can remove it.
pub trait LmdGhost<S: Store, E: EthSpec>: PartialEq + Send + Sync + Sized {
pub trait LmdGhost<S: Store<E>, E: EthSpec>: PartialEq + Send + Sync + Sized {
/// Create a new instance, with the given `store` and `finalized_root`.
fn new(store: Arc<S>, finalized_block: &BeaconBlock<E>, finalized_root: Hash256) -> Self;

View File

@@ -64,7 +64,7 @@ impl<T, E> PartialEq for ThreadSafeReducedTree<T, E> {
impl<T, E> LmdGhost<T, E> for ThreadSafeReducedTree<T, E>
where
T: Store,
T: Store<E>,
E: EthSpec,
{
fn new(store: Arc<T>, genesis_block: &BeaconBlock<E>, genesis_root: Hash256) -> Self {
@@ -218,7 +218,7 @@ impl<T, E> PartialEq for ReducedTree<T, E> {
impl<T, E> ReducedTree<T, E>
where
T: Store,
T: Store<E>,
E: EthSpec,
{
pub fn new(store: Arc<T>, genesis_block: &BeaconBlock<E>, genesis_root: Hash256) -> Self {
@@ -976,16 +976,15 @@ mod tests {
#[test]
fn test_reduced_tree_ssz() {
let store = Arc::new(MemoryStore::open());
let tree = ReducedTree::<MemoryStore, MinimalEthSpec>::new(
let store = Arc::new(MemoryStore::<MinimalEthSpec>::open());
let tree = ReducedTree::new(
store.clone(),
&BeaconBlock::empty(&MinimalEthSpec::default_spec()),
Hash256::zero(),
);
let ssz_tree = ReducedTreeSsz::from_reduced_tree(&tree);
let bytes = tree.as_bytes();
let recovered_tree =
ReducedTree::<MemoryStore, MinimalEthSpec>::from_bytes(&bytes, store.clone()).unwrap();
let recovered_tree = ReducedTree::from_bytes(&bytes, store.clone()).unwrap();
let recovered_ssz = ReducedTreeSsz::from_reduced_tree(&recovered_tree);
assert_eq!(ssz_tree, recovered_ssz);

View File

@@ -10,17 +10,14 @@ use beacon_chain::test_utils::{
use lmd_ghost::{LmdGhost, ThreadSafeReducedTree as BaseThreadSafeReducedTree};
use rand::{prelude::*, rngs::StdRng};
use std::sync::Arc;
use store::{
iter::{AncestorIter, BlockRootsIterator},
MemoryStore, Store,
};
use store::{iter::AncestorIter, MemoryStore, Store};
use types::{BeaconBlock, EthSpec, Hash256, MinimalEthSpec, Slot};
// Should ideally be divisible by 3.
pub const VALIDATOR_COUNT: usize = 3 * 8;
type TestEthSpec = MinimalEthSpec;
type ThreadSafeReducedTree = BaseThreadSafeReducedTree<MemoryStore, TestEthSpec>;
type ThreadSafeReducedTree = BaseThreadSafeReducedTree<MemoryStore<TestEthSpec>, TestEthSpec>;
type BeaconChainHarness = BaseBeaconChainHarness<HarnessType<TestEthSpec>>;
type RootAndSlot = (Hash256, Slot);
@@ -86,16 +83,14 @@ impl ForkedHarness {
faulty_fork_blocks,
);
let mut honest_roots =
get_ancestor_roots::<TestEthSpec, _>(harness.chain.store.clone(), honest_head);
let mut honest_roots = get_ancestor_roots(harness.chain.store.clone(), honest_head);
honest_roots.insert(
0,
(honest_head, get_slot_for_block_root(&harness, honest_head)),
);
let mut faulty_roots =
get_ancestor_roots::<TestEthSpec, _>(harness.chain.store.clone(), faulty_head);
let mut faulty_roots = get_ancestor_roots(harness.chain.store.clone(), faulty_head);
faulty_roots.insert(
0,
@@ -121,7 +116,7 @@ impl ForkedHarness {
}
}
pub fn store_clone(&self) -> MemoryStore {
pub fn store_clone(&self) -> MemoryStore<TestEthSpec> {
(*self.harness.chain.store).clone()
}
@@ -131,7 +126,7 @@ impl ForkedHarness {
//
// Taking a clone here ensures that each fork choice gets it's own store so there is no
// cross-contamination between tests.
let store: MemoryStore = self.store_clone();
let store: MemoryStore<TestEthSpec> = self.store_clone();
ThreadSafeReducedTree::new(
Arc::new(store),
@@ -155,7 +150,7 @@ impl ForkedHarness {
}
/// Helper: returns all the ancestor roots and slots for a given block_root.
fn get_ancestor_roots<E: EthSpec, U: Store>(
fn get_ancestor_roots<U: Store<TestEthSpec>>(
store: Arc<U>,
block_root: Hash256,
) -> Vec<(Hash256, Slot)> {
@@ -164,11 +159,9 @@ fn get_ancestor_roots<E: EthSpec, U: Store>(
.expect("block should exist")
.expect("store should not error");
<BeaconBlock<TestEthSpec> as AncestorIter<_, BlockRootsIterator<TestEthSpec, _>>>::try_iter_ancestor_roots(
&block, store,
)
.expect("should be able to create ancestor iter")
.collect()
<BeaconBlock<TestEthSpec> as AncestorIter<_, _, _>>::try_iter_ancestor_roots(&block, store)
.expect("should be able to create ancestor iter")
.collect()
}
/// Helper: returns the slot for some block_root.