Skip to content

Commit ce10db1

Browse files
authored
Remove code duplicating stdlib (#1239)
* Get rid of superfluous ReverseBlockRootIterator * Get rid of superfluous ReverseStateRootIterator and ReverseChainIterator * cargo fmt
1 parent a214032 commit ce10db1

File tree

3 files changed

+11
-76
lines changed

3 files changed

+11
-76
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ use std::collections::HashSet;
3838
use std::io::prelude::*;
3939
use std::sync::Arc;
4040
use std::time::{Duration, Instant};
41-
use store::iter::{
42-
BlockRootsIterator, ParentRootBlockIterator, ReverseBlockRootIterator,
43-
ReverseStateRootIterator, StateRootsIterator,
44-
};
41+
use store::iter::{BlockRootsIterator, ParentRootBlockIterator, StateRootsIterator};
4542
use store::{Error as DBError, Store};
4643
use types::*;
4744

@@ -322,17 +319,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
322319
/// - Iterator returns `(Hash256, Slot)`.
323320
/// - As this iterator starts at the `head` of the chain (viz., the best block), the first slot
324321
/// returned may be earlier than the wall-clock slot.
325-
pub fn rev_iter_block_roots(
326-
&self,
327-
) -> Result<ReverseBlockRootIterator<T::EthSpec, T::Store>, Error> {
322+
pub fn rev_iter_block_roots(&self) -> Result<impl Iterator<Item = (Hash256, Slot)>, Error> {
328323
let head = self.head()?;
329324

330325
let iter = BlockRootsIterator::owned(self.store.clone(), head.beacon_state);
331326

332-
Ok(ReverseBlockRootIterator::new(
333-
(head.beacon_block_root, head.beacon_block.slot()),
334-
iter,
335-
))
327+
Ok(std::iter::once((head.beacon_block_root, head.beacon_block.slot())).chain(iter))
336328
}
337329

338330
pub fn forwards_iter_block_roots(
@@ -362,18 +354,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
362354
pub fn rev_iter_block_roots_from(
363355
&self,
364356
block_root: Hash256,
365-
) -> Result<ReverseBlockRootIterator<T::EthSpec, T::Store>, Error> {
357+
) -> Result<impl Iterator<Item = (Hash256, Slot)>, Error> {
366358
let block = self
367359
.get_block(&block_root)?
368360
.ok_or_else(|| Error::MissingBeaconBlock(block_root))?;
369361
let state = self
370362
.get_state(&block.state_root(), Some(block.slot()))?
371363
.ok_or_else(|| Error::MissingBeaconState(block.state_root()))?;
372364
let iter = BlockRootsIterator::owned(self.store.clone(), state);
373-
Ok(ReverseBlockRootIterator::new(
374-
(block_root, block.slot()),
375-
iter,
376-
))
365+
Ok(std::iter::once((block_root, block.slot())).chain(iter))
377366
}
378367

379368
/// Traverse backwards from `block_root` to find the root of the ancestor block at `slot`.
@@ -397,18 +386,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
397386
/// - Iterator returns `(Hash256, Slot)`.
398387
/// - As this iterator starts at the `head` of the chain (viz., the best block), the first slot
399388
/// returned may be earlier than the wall-clock slot.
400-
pub fn rev_iter_state_roots(
401-
&self,
402-
) -> Result<ReverseStateRootIterator<T::EthSpec, T::Store>, Error> {
389+
pub fn rev_iter_state_roots(&self) -> Result<impl Iterator<Item = (Hash256, Slot)>, Error> {
403390
let head = self.head()?;
404391
let slot = head.beacon_state.slot;
405392

406393
let iter = StateRootsIterator::owned(self.store.clone(), head.beacon_state);
407394

408-
Ok(ReverseStateRootIterator::new(
409-
(head.beacon_state_root, slot),
410-
iter,
411-
))
395+
Ok(std::iter::once((head.beacon_state_root, slot)).chain(iter))
412396
}
413397

414398
/// Returns the block at the given slot, if any. Only returns blocks in the canonical chain.

beacon_node/store/src/forwards_iter.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::chunked_iter::ChunkedVectorIter;
22
use crate::chunked_vector::BlockRoots;
3-
use crate::iter::{BlockRootsIterator, ReverseBlockRootIterator};
3+
use crate::iter::BlockRootsIterator;
44
use crate::{HotColdDB, Store};
55
use slog::error;
66
use std::sync::Arc;
@@ -65,13 +65,10 @@ impl SimpleForwardsBlockRootsIterator {
6565
end_block_root: Hash256,
6666
) -> Self {
6767
// Iterate backwards from the end state, stopping at the start slot.
68+
let iter = std::iter::once((end_block_root, end_state.slot))
69+
.chain(BlockRootsIterator::owned(store, end_state));
6870
Self {
69-
values: ReverseBlockRootIterator::new(
70-
(end_block_root, end_state.slot),
71-
BlockRootsIterator::owned(store, end_state),
72-
)
73-
.take_while(|(_, slot)| *slot >= start_slot)
74-
.collect(),
71+
values: iter.take_while(|(_, slot)| *slot >= start_slot).collect(),
7572
}
7673
}
7774
}

beacon_node/store/src/iter.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -294,52 +294,6 @@ fn slot_of_prev_restore_point<E: EthSpec>(current_slot: Slot) -> Slot {
294294
(current_slot - 1) / slots_per_historical_root * slots_per_historical_root
295295
}
296296

297-
pub type ReverseBlockRootIterator<'a, E, S> =
298-
ReverseHashAndSlotIterator<BlockRootsIterator<'a, E, S>>;
299-
pub type ReverseStateRootIterator<'a, E, S> =
300-
ReverseHashAndSlotIterator<StateRootsIterator<'a, E, S>>;
301-
302-
pub type ReverseHashAndSlotIterator<I> = ReverseChainIterator<(Hash256, Slot), I>;
303-
304-
/// Provides a wrapper for an iterator that returns a given `T` before it starts returning results of
305-
/// the `Iterator`.
306-
pub struct ReverseChainIterator<T, I> {
307-
first_value_used: bool,
308-
first_value: T,
309-
iter: I,
310-
}
311-
312-
impl<T, I> ReverseChainIterator<T, I>
313-
where
314-
T: Sized,
315-
I: Iterator<Item = T> + Sized,
316-
{
317-
pub fn new(first_value: T, iter: I) -> Self {
318-
Self {
319-
first_value_used: false,
320-
first_value,
321-
iter,
322-
}
323-
}
324-
}
325-
326-
impl<T, I> Iterator for ReverseChainIterator<T, I>
327-
where
328-
T: Clone,
329-
I: Iterator<Item = T>,
330-
{
331-
type Item = T;
332-
333-
fn next(&mut self) -> Option<Self::Item> {
334-
if self.first_value_used {
335-
self.iter.next()
336-
} else {
337-
self.first_value_used = true;
338-
Some(self.first_value.clone())
339-
}
340-
}
341-
}
342-
343297
#[cfg(test)]
344298
mod test {
345299
use super::*;

0 commit comments

Comments
 (0)