diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index be0881970..ff293d8bc 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -38,10 +38,7 @@ use std::collections::HashSet; use std::io::prelude::*; use std::sync::Arc; use std::time::{Duration, Instant}; -use store::iter::{ - BlockRootsIterator, ParentRootBlockIterator, ReverseBlockRootIterator, - ReverseStateRootIterator, StateRootsIterator, -}; +use store::iter::{BlockRootsIterator, ParentRootBlockIterator, StateRootsIterator}; use store::{Error as DBError, Store}; use types::*; @@ -322,17 +319,12 @@ impl BeaconChain { /// - Iterator returns `(Hash256, Slot)`. /// - As this iterator starts at the `head` of the chain (viz., the best block), the first slot /// returned may be earlier than the wall-clock slot. - pub fn rev_iter_block_roots( - &self, - ) -> Result, Error> { + pub fn rev_iter_block_roots(&self) -> Result, Error> { let head = self.head()?; let iter = BlockRootsIterator::owned(self.store.clone(), head.beacon_state); - Ok(ReverseBlockRootIterator::new( - (head.beacon_block_root, head.beacon_block.slot()), - iter, - )) + Ok(std::iter::once((head.beacon_block_root, head.beacon_block.slot())).chain(iter)) } pub fn forwards_iter_block_roots( @@ -362,7 +354,7 @@ impl BeaconChain { pub fn rev_iter_block_roots_from( &self, block_root: Hash256, - ) -> Result, Error> { + ) -> Result, Error> { let block = self .get_block(&block_root)? .ok_or_else(|| Error::MissingBeaconBlock(block_root))?; @@ -370,10 +362,7 @@ impl BeaconChain { .get_state(&block.state_root(), Some(block.slot()))? .ok_or_else(|| Error::MissingBeaconState(block.state_root()))?; let iter = BlockRootsIterator::owned(self.store.clone(), state); - Ok(ReverseBlockRootIterator::new( - (block_root, block.slot()), - iter, - )) + Ok(std::iter::once((block_root, block.slot())).chain(iter)) } /// Traverse backwards from `block_root` to find the root of the ancestor block at `slot`. @@ -397,18 +386,13 @@ impl BeaconChain { /// - Iterator returns `(Hash256, Slot)`. /// - As this iterator starts at the `head` of the chain (viz., the best block), the first slot /// returned may be earlier than the wall-clock slot. - pub fn rev_iter_state_roots( - &self, - ) -> Result, Error> { + pub fn rev_iter_state_roots(&self) -> Result, Error> { let head = self.head()?; let slot = head.beacon_state.slot; let iter = StateRootsIterator::owned(self.store.clone(), head.beacon_state); - Ok(ReverseStateRootIterator::new( - (head.beacon_state_root, slot), - iter, - )) + Ok(std::iter::once((head.beacon_state_root, slot)).chain(iter)) } /// Returns the block at the given slot, if any. Only returns blocks in the canonical chain. diff --git a/beacon_node/store/src/forwards_iter.rs b/beacon_node/store/src/forwards_iter.rs index 08b9d92a6..b105608a0 100644 --- a/beacon_node/store/src/forwards_iter.rs +++ b/beacon_node/store/src/forwards_iter.rs @@ -1,6 +1,6 @@ use crate::chunked_iter::ChunkedVectorIter; use crate::chunked_vector::BlockRoots; -use crate::iter::{BlockRootsIterator, ReverseBlockRootIterator}; +use crate::iter::BlockRootsIterator; use crate::{HotColdDB, Store}; use slog::error; use std::sync::Arc; @@ -65,13 +65,10 @@ impl SimpleForwardsBlockRootsIterator { end_block_root: Hash256, ) -> Self { // Iterate backwards from the end state, stopping at the start slot. + let iter = std::iter::once((end_block_root, end_state.slot)) + .chain(BlockRootsIterator::owned(store, end_state)); Self { - values: ReverseBlockRootIterator::new( - (end_block_root, end_state.slot), - BlockRootsIterator::owned(store, end_state), - ) - .take_while(|(_, slot)| *slot >= start_slot) - .collect(), + values: iter.take_while(|(_, slot)| *slot >= start_slot).collect(), } } } diff --git a/beacon_node/store/src/iter.rs b/beacon_node/store/src/iter.rs index 34d2d7e9d..58ec20094 100644 --- a/beacon_node/store/src/iter.rs +++ b/beacon_node/store/src/iter.rs @@ -294,52 +294,6 @@ fn slot_of_prev_restore_point(current_slot: Slot) -> Slot { (current_slot - 1) / slots_per_historical_root * slots_per_historical_root } -pub type ReverseBlockRootIterator<'a, E, S> = - ReverseHashAndSlotIterator>; -pub type ReverseStateRootIterator<'a, E, S> = - ReverseHashAndSlotIterator>; - -pub type ReverseHashAndSlotIterator = ReverseChainIterator<(Hash256, Slot), I>; - -/// Provides a wrapper for an iterator that returns a given `T` before it starts returning results of -/// the `Iterator`. -pub struct ReverseChainIterator { - first_value_used: bool, - first_value: T, - iter: I, -} - -impl ReverseChainIterator -where - T: Sized, - I: Iterator + Sized, -{ - pub fn new(first_value: T, iter: I) -> Self { - Self { - first_value_used: false, - first_value, - iter, - } - } -} - -impl Iterator for ReverseChainIterator -where - T: Clone, - I: Iterator, -{ - type Item = T; - - fn next(&mut self) -> Option { - if self.first_value_used { - self.iter.next() - } else { - self.first_value_used = true; - Some(self.first_value.clone()) - } - } -} - #[cfg(test)] mod test { use super::*;