a290a3c537
## Issue Addressed Successor to #2431 ## Proposed Changes * Add a `BlockReplayer` struct to abstract over the intricacies of calling `per_slot_processing` and `per_block_processing` while avoiding unnecessary tree hashing. * Add a variant of the forwards state root iterator that does not require an `end_state`. * Use the `BlockReplayer` when reconstructing states in the database. Use the efficient forwards iterator for frozen states. * Refactor the iterators to remove `Arc<HotColdDB>` (this seems to be neater than making _everything_ an `Arc<HotColdDB>` as I did in #2431). Supplying the state roots allow us to avoid building a tree hash cache at all when reconstructing historic states, which saves around 1 second flat (regardless of `slots-per-restore-point`). This is a small percentage of worst-case state load times with 200K validators and SPRP=2048 (~15s vs ~16s) but a significant speed-up for more frequent restore points: state loads with SPRP=32 should be now consistently <500ms instead of 1.5s (a ~3x speedup). ## Additional Info Required by https://github.com/sigp/lighthouse/pull/2628
246 lines
8.7 KiB
Rust
246 lines
8.7 KiB
Rust
use crate::chunked_iter::ChunkedVectorIter;
|
|
use crate::chunked_vector::{BlockRoots, Field, StateRoots};
|
|
use crate::errors::{Error, Result};
|
|
use crate::iter::{BlockRootsIterator, StateRootsIterator};
|
|
use crate::{HotColdDB, ItemStore};
|
|
use itertools::process_results;
|
|
use types::{BeaconState, ChainSpec, EthSpec, Hash256, Slot};
|
|
|
|
pub type HybridForwardsBlockRootsIterator<'a, E, Hot, Cold> =
|
|
HybridForwardsIterator<'a, E, BlockRoots, Hot, Cold>;
|
|
pub type HybridForwardsStateRootsIterator<'a, E, Hot, Cold> =
|
|
HybridForwardsIterator<'a, E, StateRoots, Hot, Cold>;
|
|
|
|
/// Trait unifying `BlockRoots` and `StateRoots` for forward iteration.
|
|
pub trait Root<E: EthSpec>: Field<E, Value = Hash256> {
|
|
fn simple_forwards_iterator<Hot: ItemStore<E>, Cold: ItemStore<E>>(
|
|
store: &HotColdDB<E, Hot, Cold>,
|
|
start_slot: Slot,
|
|
end_state: BeaconState<E>,
|
|
end_root: Hash256,
|
|
) -> Result<SimpleForwardsIterator>;
|
|
}
|
|
|
|
impl<E: EthSpec> Root<E> for BlockRoots {
|
|
fn simple_forwards_iterator<Hot: ItemStore<E>, Cold: ItemStore<E>>(
|
|
store: &HotColdDB<E, Hot, Cold>,
|
|
start_slot: Slot,
|
|
end_state: BeaconState<E>,
|
|
end_block_root: Hash256,
|
|
) -> Result<SimpleForwardsIterator> {
|
|
// Iterate backwards from the end state, stopping at the start slot.
|
|
let values = process_results(
|
|
std::iter::once(Ok((end_block_root, end_state.slot())))
|
|
.chain(BlockRootsIterator::owned(store, end_state)),
|
|
|iter| {
|
|
iter.take_while(|(_, slot)| *slot >= start_slot)
|
|
.collect::<Vec<_>>()
|
|
},
|
|
)?;
|
|
Ok(SimpleForwardsIterator { values })
|
|
}
|
|
}
|
|
|
|
impl<E: EthSpec> Root<E> for StateRoots {
|
|
fn simple_forwards_iterator<Hot: ItemStore<E>, Cold: ItemStore<E>>(
|
|
store: &HotColdDB<E, Hot, Cold>,
|
|
start_slot: Slot,
|
|
end_state: BeaconState<E>,
|
|
end_state_root: Hash256,
|
|
) -> Result<SimpleForwardsIterator> {
|
|
// Iterate backwards from the end state, stopping at the start slot.
|
|
let values = process_results(
|
|
std::iter::once(Ok((end_state_root, end_state.slot())))
|
|
.chain(StateRootsIterator::owned(store, end_state)),
|
|
|iter| {
|
|
iter.take_while(|(_, slot)| *slot >= start_slot)
|
|
.collect::<Vec<_>>()
|
|
},
|
|
)?;
|
|
Ok(SimpleForwardsIterator { values })
|
|
}
|
|
}
|
|
|
|
/// Forwards root iterator that makes use of a flat field table in the freezer DB.
|
|
pub struct FrozenForwardsIterator<'a, E: EthSpec, F: Root<E>, Hot: ItemStore<E>, Cold: ItemStore<E>>
|
|
{
|
|
inner: ChunkedVectorIter<'a, F, E, Hot, Cold>,
|
|
}
|
|
|
|
impl<'a, E: EthSpec, F: Root<E>, Hot: ItemStore<E>, Cold: ItemStore<E>>
|
|
FrozenForwardsIterator<'a, E, F, Hot, Cold>
|
|
{
|
|
pub fn new(
|
|
store: &'a HotColdDB<E, Hot, Cold>,
|
|
start_slot: Slot,
|
|
last_restore_point_slot: Slot,
|
|
spec: &ChainSpec,
|
|
) -> Self {
|
|
Self {
|
|
inner: ChunkedVectorIter::new(
|
|
store,
|
|
start_slot.as_usize(),
|
|
last_restore_point_slot,
|
|
spec,
|
|
),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, E: EthSpec, F: Root<E>, Hot: ItemStore<E>, Cold: ItemStore<E>> Iterator
|
|
for FrozenForwardsIterator<'a, E, F, Hot, Cold>
|
|
{
|
|
type Item = (Hash256, Slot);
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
self.inner
|
|
.next()
|
|
.map(|(slot, root)| (root, Slot::from(slot)))
|
|
}
|
|
}
|
|
|
|
/// Forwards root iterator that reverses a backwards iterator (only good for short ranges).
|
|
pub struct SimpleForwardsIterator {
|
|
// Values from the backwards iterator (in slot descending order)
|
|
values: Vec<(Hash256, Slot)>,
|
|
}
|
|
|
|
impl Iterator for SimpleForwardsIterator {
|
|
type Item = Result<(Hash256, Slot)>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
// Pop from the end of the vector to get the state roots in slot-ascending order.
|
|
Ok(self.values.pop()).transpose()
|
|
}
|
|
}
|
|
|
|
/// Fusion of the above two approaches to forwards iteration. Fast and efficient.
|
|
pub enum HybridForwardsIterator<'a, E: EthSpec, F: Root<E>, Hot: ItemStore<E>, Cold: ItemStore<E>> {
|
|
PreFinalization {
|
|
iter: Box<FrozenForwardsIterator<'a, E, F, Hot, Cold>>,
|
|
/// Data required by the `PostFinalization` iterator when we get to it.
|
|
continuation_data: Option<Box<(BeaconState<E>, Hash256)>>,
|
|
},
|
|
PostFinalizationLazy {
|
|
continuation_data: Option<Box<(BeaconState<E>, Hash256)>>,
|
|
store: &'a HotColdDB<E, Hot, Cold>,
|
|
start_slot: Slot,
|
|
},
|
|
PostFinalization {
|
|
iter: SimpleForwardsIterator,
|
|
},
|
|
}
|
|
|
|
impl<'a, E: EthSpec, F: Root<E>, Hot: ItemStore<E>, Cold: ItemStore<E>>
|
|
HybridForwardsIterator<'a, E, F, Hot, Cold>
|
|
{
|
|
/// Construct a new hybrid iterator.
|
|
///
|
|
/// The `get_state` closure should return a beacon state and final block/state root to backtrack
|
|
/// from in the case where the iterated range does not lie entirely within the frozen portion of
|
|
/// the database. If an `end_slot` is provided and it is before the database's latest restore
|
|
/// point slot then the `get_state` closure will not be called at all.
|
|
///
|
|
/// It is OK for `get_state` to hold a lock while this function is evaluated, as the returned
|
|
/// iterator is as lazy as possible and won't do any work apart from calling `get_state`.
|
|
///
|
|
/// Conversely, if `get_state` does extensive work (e.g. loading data from disk) then this
|
|
/// function may block for some time while `get_state` runs.
|
|
pub fn new(
|
|
store: &'a HotColdDB<E, Hot, Cold>,
|
|
start_slot: Slot,
|
|
end_slot: Option<Slot>,
|
|
get_state: impl FnOnce() -> (BeaconState<E>, Hash256),
|
|
spec: &ChainSpec,
|
|
) -> Result<Self> {
|
|
use HybridForwardsIterator::*;
|
|
|
|
let latest_restore_point_slot = store.get_latest_restore_point_slot();
|
|
|
|
let result = if start_slot < latest_restore_point_slot {
|
|
let iter = Box::new(FrozenForwardsIterator::new(
|
|
store,
|
|
start_slot,
|
|
latest_restore_point_slot,
|
|
spec,
|
|
));
|
|
|
|
// No continuation data is needed if the forwards iterator plans to halt before
|
|
// `end_slot`. If it tries to continue further a `NoContinuationData` error will be
|
|
// returned.
|
|
let continuation_data =
|
|
if end_slot.map_or(false, |end_slot| end_slot < latest_restore_point_slot) {
|
|
None
|
|
} else {
|
|
Some(Box::new(get_state()))
|
|
};
|
|
PreFinalization {
|
|
iter,
|
|
continuation_data,
|
|
}
|
|
} else {
|
|
PostFinalizationLazy {
|
|
continuation_data: Some(Box::new(get_state())),
|
|
store,
|
|
start_slot,
|
|
}
|
|
};
|
|
|
|
Ok(result)
|
|
}
|
|
|
|
fn do_next(&mut self) -> Result<Option<(Hash256, Slot)>> {
|
|
use HybridForwardsIterator::*;
|
|
|
|
match self {
|
|
PreFinalization {
|
|
iter,
|
|
continuation_data,
|
|
} => {
|
|
match iter.next() {
|
|
Some(x) => Ok(Some(x)),
|
|
// Once the pre-finalization iterator is consumed, transition
|
|
// to a post-finalization iterator beginning from the last slot
|
|
// of the pre iterator.
|
|
None => {
|
|
let continuation_data = continuation_data.take();
|
|
let store = iter.inner.store;
|
|
let start_slot = Slot::from(iter.inner.end_vindex);
|
|
|
|
*self = PostFinalizationLazy {
|
|
continuation_data,
|
|
store,
|
|
start_slot,
|
|
};
|
|
|
|
self.do_next()
|
|
}
|
|
}
|
|
}
|
|
PostFinalizationLazy {
|
|
continuation_data,
|
|
store,
|
|
start_slot,
|
|
} => {
|
|
let (end_state, end_root) =
|
|
*continuation_data.take().ok_or(Error::NoContinuationData)?;
|
|
*self = PostFinalization {
|
|
iter: F::simple_forwards_iterator(store, *start_slot, end_state, end_root)?,
|
|
};
|
|
self.do_next()
|
|
}
|
|
PostFinalization { iter } => iter.next().transpose(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a, E: EthSpec, F: Root<E>, Hot: ItemStore<E>, Cold: ItemStore<E>> Iterator
|
|
for HybridForwardsIterator<'a, E, F, Hot, Cold>
|
|
{
|
|
type Item = Result<(Hash256, Slot)>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
self.do_next().transpose()
|
|
}
|
|
}
|