2019-06-15 13:56:41 +00:00
|
|
|
use crate::Store;
|
2019-06-18 15:47:21 +00:00
|
|
|
use std::borrow::Cow;
|
2019-12-06 03:29:06 +00:00
|
|
|
use std::marker::PhantomData;
|
2019-06-15 13:56:41 +00:00
|
|
|
use std::sync::Arc;
|
2019-12-06 03:29:06 +00:00
|
|
|
use types::{
|
|
|
|
typenum::Unsigned, BeaconBlock, BeaconState, BeaconStateError, EthSpec, Hash256, Slot,
|
|
|
|
};
|
2019-06-15 13:56:41 +00:00
|
|
|
|
2019-07-29 02:08:52 +00:00
|
|
|
/// Implemented for types that have ancestors (e.g., blocks, states) that may be iterated over.
|
2019-08-08 02:28:10 +00:00
|
|
|
///
|
|
|
|
/// ## Note
|
|
|
|
///
|
|
|
|
/// It is assumed that all ancestors for this object are stored in the database. If this is not the
|
|
|
|
/// case, the iterator will start returning `None` prior to genesis.
|
2019-07-29 02:08:52 +00:00
|
|
|
pub trait AncestorIter<U: Store, I: Iterator> {
|
|
|
|
/// Returns an iterator over the roots of the ancestors of `self`.
|
|
|
|
fn try_iter_ancestor_roots(&self, store: Arc<U>) -> Option<I>;
|
|
|
|
}
|
|
|
|
|
2019-08-08 02:28:10 +00:00
|
|
|
impl<'a, U: Store, E: EthSpec> AncestorIter<U, BlockRootsIterator<'a, E, U>> for BeaconBlock<E> {
|
2019-08-14 00:55:24 +00:00
|
|
|
/// Iterates across all available prior block roots of `self`, starting at the most recent and ending
|
2019-07-29 02:08:52 +00:00
|
|
|
/// at genesis.
|
2019-08-08 02:28:10 +00:00
|
|
|
fn try_iter_ancestor_roots(&self, store: Arc<U>) -> Option<BlockRootsIterator<'a, E, U>> {
|
2019-11-26 23:54:46 +00:00
|
|
|
let state = store.get_state(&self.state_root, Some(self.slot)).ok()??;
|
2019-07-29 02:08:52 +00:00
|
|
|
|
2019-08-14 00:55:24 +00:00
|
|
|
Some(BlockRootsIterator::owned(store, state))
|
2019-07-29 02:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 07:15:15 +00:00
|
|
|
impl<'a, U: Store, E: EthSpec> AncestorIter<U, StateRootsIterator<'a, E, U>> for BeaconState<E> {
|
2019-08-14 00:55:24 +00:00
|
|
|
/// Iterates across all available prior state roots of `self`, starting at the most recent and ending
|
2019-08-10 07:15:15 +00:00
|
|
|
/// at genesis.
|
|
|
|
fn try_iter_ancestor_roots(&self, store: Arc<U>) -> Option<StateRootsIterator<'a, E, U>> {
|
|
|
|
// The `self.clone()` here is wasteful.
|
2019-08-14 00:55:24 +00:00
|
|
|
Some(StateRootsIterator::owned(store, self.clone()))
|
2019-08-10 07:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
pub struct StateRootsIterator<'a, T: EthSpec, U> {
|
|
|
|
store: Arc<U>,
|
|
|
|
beacon_state: Cow<'a, BeaconState<T>>,
|
|
|
|
slot: Slot,
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:54:46 +00:00
|
|
|
impl<'a, T: EthSpec, U> Clone for StateRootsIterator<'a, T, U> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
store: self.store.clone(),
|
|
|
|
beacon_state: self.beacon_state.clone(),
|
|
|
|
slot: self.slot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
impl<'a, T: EthSpec, U: Store> StateRootsIterator<'a, T, U> {
|
2019-08-14 00:55:24 +00:00
|
|
|
pub fn new(store: Arc<U>, beacon_state: &'a BeaconState<T>) -> Self {
|
2019-06-18 15:47:21 +00:00
|
|
|
Self {
|
|
|
|
store,
|
2019-08-14 00:55:24 +00:00
|
|
|
slot: beacon_state.slot,
|
2019-06-18 15:47:21 +00:00
|
|
|
beacon_state: Cow::Borrowed(beacon_state),
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 16:06:23 +00:00
|
|
|
|
2019-08-14 00:55:24 +00:00
|
|
|
pub fn owned(store: Arc<U>, beacon_state: BeaconState<T>) -> Self {
|
2019-06-18 16:06:23 +00:00
|
|
|
Self {
|
|
|
|
store,
|
2019-08-14 00:55:24 +00:00
|
|
|
slot: beacon_state.slot,
|
2019-07-16 07:28:15 +00:00
|
|
|
beacon_state: Cow::Owned(beacon_state),
|
2019-06-18 16:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 15:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: EthSpec, U: Store> Iterator for StateRootsIterator<'a, T, U> {
|
|
|
|
type Item = (Hash256, Slot);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2019-11-26 23:54:46 +00:00
|
|
|
if self.slot == 0 || self.slot > self.beacon_state.slot {
|
2019-06-18 15:47:21 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.slot -= 1;
|
|
|
|
|
|
|
|
match self.beacon_state.get_state_root(self.slot) {
|
|
|
|
Ok(root) => Some((*root, self.slot)),
|
|
|
|
Err(BeaconStateError::SlotOutOfBounds) => {
|
2019-12-06 03:29:06 +00:00
|
|
|
// Read a `BeaconState` from the store that has access to prior historical roots.
|
|
|
|
let beacon_state =
|
|
|
|
next_historical_root_backtrack_state(&*self.store, &self.beacon_state)?;
|
2019-06-18 15:47:21 +00:00
|
|
|
|
|
|
|
self.beacon_state = Cow::Owned(beacon_state);
|
|
|
|
|
|
|
|
let root = self.beacon_state.get_state_root(self.slot).ok()?;
|
|
|
|
|
|
|
|
Some((*root, self.slot))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:29:06 +00:00
|
|
|
/// Block iterator that uses the `parent_root` of each block to backtrack.
|
|
|
|
pub struct ParentRootBlockIterator<'a, E: EthSpec, S: Store> {
|
|
|
|
store: &'a S,
|
|
|
|
next_block_root: Hash256,
|
|
|
|
_phantom: PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, E: EthSpec, S: Store> ParentRootBlockIterator<'a, E, S> {
|
|
|
|
pub fn new(store: &'a S, start_block_root: Hash256) -> Self {
|
|
|
|
Self {
|
|
|
|
store,
|
|
|
|
next_block_root: start_block_root,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, E: EthSpec, S: Store> Iterator for ParentRootBlockIterator<'a, E, S> {
|
|
|
|
type Item = BeaconBlock<E>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
// Stop once we reach the zero parent, otherwise we'll keep returning the genesis
|
|
|
|
// block forever.
|
|
|
|
if self.next_block_root.is_zero() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let block: BeaconBlock<E> = self.store.get(&self.next_block_root).ok()??;
|
|
|
|
self.next_block_root = block.parent_root;
|
|
|
|
Some(block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
#[derive(Clone)]
|
2019-06-15 13:56:41 +00:00
|
|
|
/// Extends `BlockRootsIterator`, returning `BeaconBlock` instances, instead of their roots.
|
2019-06-18 15:47:21 +00:00
|
|
|
pub struct BlockIterator<'a, T: EthSpec, U> {
|
|
|
|
roots: BlockRootsIterator<'a, T, U>,
|
2019-06-15 13:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
impl<'a, T: EthSpec, U: Store> BlockIterator<'a, T, U> {
|
2019-06-15 13:56:41 +00:00
|
|
|
/// Create a new iterator over all blocks in the given `beacon_state` and prior states.
|
2019-08-14 00:55:24 +00:00
|
|
|
pub fn new(store: Arc<U>, beacon_state: &'a BeaconState<T>) -> Self {
|
2019-06-15 13:56:41 +00:00
|
|
|
Self {
|
2019-08-14 00:55:24 +00:00
|
|
|
roots: BlockRootsIterator::new(store, beacon_state),
|
2019-06-15 13:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 16:06:23 +00:00
|
|
|
|
|
|
|
/// Create a new iterator over all blocks in the given `beacon_state` and prior states.
|
2019-08-14 00:55:24 +00:00
|
|
|
pub fn owned(store: Arc<U>, beacon_state: BeaconState<T>) -> Self {
|
2019-06-18 16:06:23 +00:00
|
|
|
Self {
|
2019-08-14 00:55:24 +00:00
|
|
|
roots: BlockRootsIterator::owned(store, beacon_state),
|
2019-06-18 16:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 13:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
impl<'a, T: EthSpec, U: Store> Iterator for BlockIterator<'a, T, U> {
|
Update to frozen spec ❄️ (v0.8.1) (#444)
* types: first updates for v0.8
* state_processing: epoch processing v0.8.0
* state_processing: block processing v0.8.0
* tree_hash_derive: support generics in SignedRoot
* types v0.8: update to use ssz_types
* state_processing v0.8: use ssz_types
* ssz_types: add bitwise methods and from_elem
* types: fix v0.8 FIXMEs
* ssz_types: add bitfield shift_up
* ssz_types: iterators and DerefMut for VariableList
* types,state_processing: use VariableList
* ssz_types: fix BitVector Decode impl
Fixed a typo in the implementation of ssz::Decode for BitVector, which caused it
to be considered variable length!
* types: fix test modules for v0.8 update
* types: remove slow type-level arithmetic
* state_processing: fix tests for v0.8
* op_pool: update for v0.8
* ssz_types: Bitfield difference length-independent
Allow computing the difference of two bitfields of different lengths.
* Implement compact committee support
* epoch_processing: committee & active index roots
* state_processing: genesis state builder v0.8
* state_processing: implement v0.8.1
* Further improve tree_hash
* Strip examples, tests from cached_tree_hash
* Update TreeHash, un-impl CachedTreeHash
* Update bitfield TreeHash, un-impl CachedTreeHash
* Update FixedLenVec TreeHash, unimpl CachedTreeHash
* Update update tree_hash_derive for new TreeHash
* Fix TreeHash, un-impl CachedTreeHash for ssz_types
* Remove fixed_len_vec, ssz benches
SSZ benches relied upon fixed_len_vec -- it is easier to just delete
them and rebuild them later (when necessary)
* Remove boolean_bitfield crate
* Fix fake_crypto BLS compile errors
* Update ef_tests for new v.8 type params
* Update ef_tests submodule to v0.8.1 tag
* Make fixes to support parsing ssz ef_tests
* `compact_committee...` to `compact_committees...`
* Derive more traits for `CompactCommittee`
* Flip bitfield byte-endianness
* Fix tree_hash for bitfields
* Modify CLI output for ef_tests
* Bump ssz crate version
* Update ssz_types doc comment
* Del cached tree hash tests from ssz_static tests
* Tidy SSZ dependencies
* Rename ssz_types crate to eth2_ssz_types
* validator_client: update for v0.8
* ssz_types: update union/difference for bit order swap
* beacon_node: update for v0.8, EthSpec
* types: disable cached tree hash, update min spec
* state_processing: fix slot bug in committee update
* tests: temporarily disable fork choice harness test
See #447
* committee cache: prevent out-of-bounds access
In the case where we tried to access the committee of a shard that didn't have a committee in the
current epoch, we were accessing elements beyond the end of the shuffling vector and panicking! This
commit adds a check to make the failure safe and explicit.
* fix bug in get_indexed_attestation and simplify
There was a bug in our implementation of get_indexed_attestation whereby
incorrect "committee indices" were used to index into the custody bitfield. The
bug was only observable in the case where some bits of the custody bitfield were
set to 1. The implementation has been simplified to remove the bug, and a test
added.
* state_proc: workaround for compact committees bug
https://github.com/ethereum/eth2.0-specs/issues/1315
* v0.8: updates to make the EF tests pass
* Remove redundant max operation checks.
* Always supply both messages when checking attestation signatures -- allowing
verification of an attestation with no signatures.
* Swap the order of the fork and domain constant in `get_domain`, to match
the spec.
* rustfmt
* ef_tests: add new epoch processing tests
* Integrate v0.8 into master (compiles)
* Remove unused crates, fix clippy lints
* Replace v0.6.3 tags w/ v0.8.1
* Remove old comment
* Ensure lmd ghost tests only run in release
* Update readme
2019-07-30 02:44:51 +00:00
|
|
|
type Item = BeaconBlock<T>;
|
2019-06-15 13:56:41 +00:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let (root, _slot) = self.roots.next()?;
|
|
|
|
self.roots.store.get(&root).ok()?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 07:28:15 +00:00
|
|
|
/// Iterates backwards through block roots. If any specified slot is unable to be retrieved, the
|
|
|
|
/// iterator returns `None` indefinitely.
|
2019-06-15 13:56:41 +00:00
|
|
|
///
|
Update to frozen spec ❄️ (v0.8.1) (#444)
* types: first updates for v0.8
* state_processing: epoch processing v0.8.0
* state_processing: block processing v0.8.0
* tree_hash_derive: support generics in SignedRoot
* types v0.8: update to use ssz_types
* state_processing v0.8: use ssz_types
* ssz_types: add bitwise methods and from_elem
* types: fix v0.8 FIXMEs
* ssz_types: add bitfield shift_up
* ssz_types: iterators and DerefMut for VariableList
* types,state_processing: use VariableList
* ssz_types: fix BitVector Decode impl
Fixed a typo in the implementation of ssz::Decode for BitVector, which caused it
to be considered variable length!
* types: fix test modules for v0.8 update
* types: remove slow type-level arithmetic
* state_processing: fix tests for v0.8
* op_pool: update for v0.8
* ssz_types: Bitfield difference length-independent
Allow computing the difference of two bitfields of different lengths.
* Implement compact committee support
* epoch_processing: committee & active index roots
* state_processing: genesis state builder v0.8
* state_processing: implement v0.8.1
* Further improve tree_hash
* Strip examples, tests from cached_tree_hash
* Update TreeHash, un-impl CachedTreeHash
* Update bitfield TreeHash, un-impl CachedTreeHash
* Update FixedLenVec TreeHash, unimpl CachedTreeHash
* Update update tree_hash_derive for new TreeHash
* Fix TreeHash, un-impl CachedTreeHash for ssz_types
* Remove fixed_len_vec, ssz benches
SSZ benches relied upon fixed_len_vec -- it is easier to just delete
them and rebuild them later (when necessary)
* Remove boolean_bitfield crate
* Fix fake_crypto BLS compile errors
* Update ef_tests for new v.8 type params
* Update ef_tests submodule to v0.8.1 tag
* Make fixes to support parsing ssz ef_tests
* `compact_committee...` to `compact_committees...`
* Derive more traits for `CompactCommittee`
* Flip bitfield byte-endianness
* Fix tree_hash for bitfields
* Modify CLI output for ef_tests
* Bump ssz crate version
* Update ssz_types doc comment
* Del cached tree hash tests from ssz_static tests
* Tidy SSZ dependencies
* Rename ssz_types crate to eth2_ssz_types
* validator_client: update for v0.8
* ssz_types: update union/difference for bit order swap
* beacon_node: update for v0.8, EthSpec
* types: disable cached tree hash, update min spec
* state_processing: fix slot bug in committee update
* tests: temporarily disable fork choice harness test
See #447
* committee cache: prevent out-of-bounds access
In the case where we tried to access the committee of a shard that didn't have a committee in the
current epoch, we were accessing elements beyond the end of the shuffling vector and panicking! This
commit adds a check to make the failure safe and explicit.
* fix bug in get_indexed_attestation and simplify
There was a bug in our implementation of get_indexed_attestation whereby
incorrect "committee indices" were used to index into the custody bitfield. The
bug was only observable in the case where some bits of the custody bitfield were
set to 1. The implementation has been simplified to remove the bug, and a test
added.
* state_proc: workaround for compact committees bug
https://github.com/ethereum/eth2.0-specs/issues/1315
* v0.8: updates to make the EF tests pass
* Remove redundant max operation checks.
* Always supply both messages when checking attestation signatures -- allowing
verification of an attestation with no signatures.
* Swap the order of the fork and domain constant in `get_domain`, to match
the spec.
* rustfmt
* ef_tests: add new epoch processing tests
* Integrate v0.8 into master (compiles)
* Remove unused crates, fix clippy lints
* Replace v0.6.3 tags w/ v0.8.1
* Remove old comment
* Ensure lmd ghost tests only run in release
* Update readme
2019-07-30 02:44:51 +00:00
|
|
|
/// Uses the `block_roots` field of `BeaconState` to as the source of block roots and will
|
|
|
|
/// perform a lookup on the `Store` for a prior `BeaconState` if `block_roots` has been
|
2019-06-15 13:56:41 +00:00
|
|
|
/// exhausted.
|
|
|
|
///
|
|
|
|
/// Returns `None` for roots prior to genesis or when there is an error reading from `Store`.
|
2019-06-18 15:47:21 +00:00
|
|
|
pub struct BlockRootsIterator<'a, T: EthSpec, U> {
|
2019-06-15 13:56:41 +00:00
|
|
|
store: Arc<U>,
|
2019-06-18 15:47:21 +00:00
|
|
|
beacon_state: Cow<'a, BeaconState<T>>,
|
2019-06-15 13:56:41 +00:00
|
|
|
slot: Slot,
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:54:46 +00:00
|
|
|
impl<'a, T: EthSpec, U> Clone for BlockRootsIterator<'a, T, U> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
store: self.store.clone(),
|
|
|
|
beacon_state: self.beacon_state.clone(),
|
|
|
|
slot: self.slot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
impl<'a, T: EthSpec, U: Store> BlockRootsIterator<'a, T, U> {
|
2019-06-15 13:56:41 +00:00
|
|
|
/// Create a new iterator over all block roots in the given `beacon_state` and prior states.
|
2019-08-14 00:55:24 +00:00
|
|
|
pub fn new(store: Arc<U>, beacon_state: &'a BeaconState<T>) -> Self {
|
2019-06-15 13:56:41 +00:00
|
|
|
Self {
|
|
|
|
store,
|
2019-08-14 00:55:24 +00:00
|
|
|
slot: beacon_state.slot,
|
2019-07-16 07:28:15 +00:00
|
|
|
beacon_state: Cow::Borrowed(beacon_state),
|
2019-06-15 13:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-18 16:06:23 +00:00
|
|
|
|
|
|
|
/// Create a new iterator over all block roots in the given `beacon_state` and prior states.
|
2019-08-14 00:55:24 +00:00
|
|
|
pub fn owned(store: Arc<U>, beacon_state: BeaconState<T>) -> Self {
|
2019-06-18 16:06:23 +00:00
|
|
|
Self {
|
|
|
|
store,
|
2019-08-14 00:55:24 +00:00
|
|
|
slot: beacon_state.slot,
|
2019-07-16 07:28:15 +00:00
|
|
|
beacon_state: Cow::Owned(beacon_state),
|
2019-06-18 16:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 13:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
impl<'a, T: EthSpec, U: Store> Iterator for BlockRootsIterator<'a, T, U> {
|
2019-06-15 13:56:41 +00:00
|
|
|
type Item = (Hash256, Slot);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2019-12-06 03:29:06 +00:00
|
|
|
if self.slot == 0 || self.slot > self.beacon_state.slot {
|
2019-06-15 13:56:41 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.slot -= 1;
|
|
|
|
|
|
|
|
match self.beacon_state.get_block_root(self.slot) {
|
|
|
|
Ok(root) => Some((*root, self.slot)),
|
|
|
|
Err(BeaconStateError::SlotOutOfBounds) => {
|
2019-12-06 03:29:06 +00:00
|
|
|
// Read a `BeaconState` from the store that has access to prior historical roots.
|
|
|
|
let beacon_state =
|
|
|
|
next_historical_root_backtrack_state(&*self.store, &self.beacon_state)?;
|
2019-06-15 13:56:41 +00:00
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
self.beacon_state = Cow::Owned(beacon_state);
|
|
|
|
|
2019-06-15 13:56:41 +00:00
|
|
|
let root = self.beacon_state.get_block_root(self.slot).ok()?;
|
|
|
|
|
|
|
|
Some((*root, self.slot))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:29:06 +00:00
|
|
|
/// Fetch the next state to use whilst backtracking in `*RootsIterator`.
|
|
|
|
fn next_historical_root_backtrack_state<E: EthSpec, S: Store>(
|
|
|
|
store: &S,
|
|
|
|
current_state: &BeaconState<E>,
|
|
|
|
) -> Option<BeaconState<E>> {
|
|
|
|
// For compatibility with the freezer database's restore points, we load a state at
|
|
|
|
// a restore point slot (thus avoiding replaying blocks). In the case where we're
|
|
|
|
// not frozen, this just means we might not jump back by the maximum amount on
|
|
|
|
// our first jump (i.e. at most 1 extra state load).
|
|
|
|
let new_state_slot = slot_of_prev_restore_point::<E>(current_state.slot);
|
|
|
|
let new_state_root = current_state.get_state_root(new_state_slot).ok()?;
|
|
|
|
store.get_state(new_state_root, Some(new_state_slot)).ok()?
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compute the slot of the last guaranteed restore point in the freezer database.
|
|
|
|
fn slot_of_prev_restore_point<E: EthSpec>(current_slot: Slot) -> Slot {
|
|
|
|
let slots_per_historical_root = E::SlotsPerHistoricalRoot::to_u64();
|
|
|
|
(current_slot - 1) / slots_per_historical_root * slots_per_historical_root
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:54:46 +00:00
|
|
|
pub type ReverseBlockRootIterator<'a, E, S> =
|
|
|
|
ReverseHashAndSlotIterator<BlockRootsIterator<'a, E, S>>;
|
|
|
|
pub type ReverseStateRootIterator<'a, E, S> =
|
|
|
|
ReverseHashAndSlotIterator<StateRootsIterator<'a, E, S>>;
|
|
|
|
|
|
|
|
pub type ReverseHashAndSlotIterator<I> = 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<T, I> {
|
|
|
|
first_value_used: bool,
|
|
|
|
first_value: T,
|
|
|
|
iter: I,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, I> ReverseChainIterator<T, I>
|
|
|
|
where
|
|
|
|
T: Sized,
|
|
|
|
I: Iterator<Item = T> + Sized,
|
|
|
|
{
|
|
|
|
pub fn new(first_value: T, iter: I) -> Self {
|
|
|
|
Self {
|
|
|
|
first_value_used: false,
|
|
|
|
first_value,
|
|
|
|
iter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, I> Iterator for ReverseChainIterator<T, I>
|
|
|
|
where
|
|
|
|
T: Clone,
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.first_value_used {
|
|
|
|
self.iter.next()
|
|
|
|
} else {
|
|
|
|
self.first_value_used = true;
|
|
|
|
Some(self.first_value.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 13:56:41 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use crate::MemoryStore;
|
|
|
|
use types::{test_utils::TestingBeaconStateBuilder, Keypair, MainnetEthSpec};
|
|
|
|
|
|
|
|
fn get_state<T: EthSpec>() -> BeaconState<T> {
|
|
|
|
let builder = TestingBeaconStateBuilder::from_single_keypair(
|
|
|
|
0,
|
|
|
|
&Keypair::random(),
|
|
|
|
&T::default_spec(),
|
|
|
|
);
|
|
|
|
let (state, _keypairs) = builder.build();
|
|
|
|
state
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-06-18 15:47:21 +00:00
|
|
|
fn block_root_iter() {
|
2019-06-15 13:56:41 +00:00
|
|
|
let store = Arc::new(MemoryStore::open());
|
|
|
|
let slots_per_historical_root = MainnetEthSpec::slots_per_historical_root();
|
|
|
|
|
|
|
|
let mut state_a: BeaconState<MainnetEthSpec> = get_state();
|
|
|
|
let mut state_b: BeaconState<MainnetEthSpec> = get_state();
|
|
|
|
|
|
|
|
state_a.slot = Slot::from(slots_per_historical_root);
|
|
|
|
state_b.slot = Slot::from(slots_per_historical_root * 2);
|
|
|
|
|
2019-09-30 03:58:45 +00:00
|
|
|
let mut hashes = (0..).map(Hash256::from_low_u64_be);
|
2019-06-15 13:56:41 +00:00
|
|
|
|
Update to frozen spec ❄️ (v0.8.1) (#444)
* types: first updates for v0.8
* state_processing: epoch processing v0.8.0
* state_processing: block processing v0.8.0
* tree_hash_derive: support generics in SignedRoot
* types v0.8: update to use ssz_types
* state_processing v0.8: use ssz_types
* ssz_types: add bitwise methods and from_elem
* types: fix v0.8 FIXMEs
* ssz_types: add bitfield shift_up
* ssz_types: iterators and DerefMut for VariableList
* types,state_processing: use VariableList
* ssz_types: fix BitVector Decode impl
Fixed a typo in the implementation of ssz::Decode for BitVector, which caused it
to be considered variable length!
* types: fix test modules for v0.8 update
* types: remove slow type-level arithmetic
* state_processing: fix tests for v0.8
* op_pool: update for v0.8
* ssz_types: Bitfield difference length-independent
Allow computing the difference of two bitfields of different lengths.
* Implement compact committee support
* epoch_processing: committee & active index roots
* state_processing: genesis state builder v0.8
* state_processing: implement v0.8.1
* Further improve tree_hash
* Strip examples, tests from cached_tree_hash
* Update TreeHash, un-impl CachedTreeHash
* Update bitfield TreeHash, un-impl CachedTreeHash
* Update FixedLenVec TreeHash, unimpl CachedTreeHash
* Update update tree_hash_derive for new TreeHash
* Fix TreeHash, un-impl CachedTreeHash for ssz_types
* Remove fixed_len_vec, ssz benches
SSZ benches relied upon fixed_len_vec -- it is easier to just delete
them and rebuild them later (when necessary)
* Remove boolean_bitfield crate
* Fix fake_crypto BLS compile errors
* Update ef_tests for new v.8 type params
* Update ef_tests submodule to v0.8.1 tag
* Make fixes to support parsing ssz ef_tests
* `compact_committee...` to `compact_committees...`
* Derive more traits for `CompactCommittee`
* Flip bitfield byte-endianness
* Fix tree_hash for bitfields
* Modify CLI output for ef_tests
* Bump ssz crate version
* Update ssz_types doc comment
* Del cached tree hash tests from ssz_static tests
* Tidy SSZ dependencies
* Rename ssz_types crate to eth2_ssz_types
* validator_client: update for v0.8
* ssz_types: update union/difference for bit order swap
* beacon_node: update for v0.8, EthSpec
* types: disable cached tree hash, update min spec
* state_processing: fix slot bug in committee update
* tests: temporarily disable fork choice harness test
See #447
* committee cache: prevent out-of-bounds access
In the case where we tried to access the committee of a shard that didn't have a committee in the
current epoch, we were accessing elements beyond the end of the shuffling vector and panicking! This
commit adds a check to make the failure safe and explicit.
* fix bug in get_indexed_attestation and simplify
There was a bug in our implementation of get_indexed_attestation whereby
incorrect "committee indices" were used to index into the custody bitfield. The
bug was only observable in the case where some bits of the custody bitfield were
set to 1. The implementation has been simplified to remove the bug, and a test
added.
* state_proc: workaround for compact committees bug
https://github.com/ethereum/eth2.0-specs/issues/1315
* v0.8: updates to make the EF tests pass
* Remove redundant max operation checks.
* Always supply both messages when checking attestation signatures -- allowing
verification of an attestation with no signatures.
* Swap the order of the fork and domain constant in `get_domain`, to match
the spec.
* rustfmt
* ef_tests: add new epoch processing tests
* Integrate v0.8 into master (compiles)
* Remove unused crates, fix clippy lints
* Replace v0.6.3 tags w/ v0.8.1
* Remove old comment
* Ensure lmd ghost tests only run in release
* Update readme
2019-07-30 02:44:51 +00:00
|
|
|
for root in &mut state_a.block_roots[..] {
|
2019-06-15 13:56:41 +00:00
|
|
|
*root = hashes.next().unwrap()
|
|
|
|
}
|
Update to frozen spec ❄️ (v0.8.1) (#444)
* types: first updates for v0.8
* state_processing: epoch processing v0.8.0
* state_processing: block processing v0.8.0
* tree_hash_derive: support generics in SignedRoot
* types v0.8: update to use ssz_types
* state_processing v0.8: use ssz_types
* ssz_types: add bitwise methods and from_elem
* types: fix v0.8 FIXMEs
* ssz_types: add bitfield shift_up
* ssz_types: iterators and DerefMut for VariableList
* types,state_processing: use VariableList
* ssz_types: fix BitVector Decode impl
Fixed a typo in the implementation of ssz::Decode for BitVector, which caused it
to be considered variable length!
* types: fix test modules for v0.8 update
* types: remove slow type-level arithmetic
* state_processing: fix tests for v0.8
* op_pool: update for v0.8
* ssz_types: Bitfield difference length-independent
Allow computing the difference of two bitfields of different lengths.
* Implement compact committee support
* epoch_processing: committee & active index roots
* state_processing: genesis state builder v0.8
* state_processing: implement v0.8.1
* Further improve tree_hash
* Strip examples, tests from cached_tree_hash
* Update TreeHash, un-impl CachedTreeHash
* Update bitfield TreeHash, un-impl CachedTreeHash
* Update FixedLenVec TreeHash, unimpl CachedTreeHash
* Update update tree_hash_derive for new TreeHash
* Fix TreeHash, un-impl CachedTreeHash for ssz_types
* Remove fixed_len_vec, ssz benches
SSZ benches relied upon fixed_len_vec -- it is easier to just delete
them and rebuild them later (when necessary)
* Remove boolean_bitfield crate
* Fix fake_crypto BLS compile errors
* Update ef_tests for new v.8 type params
* Update ef_tests submodule to v0.8.1 tag
* Make fixes to support parsing ssz ef_tests
* `compact_committee...` to `compact_committees...`
* Derive more traits for `CompactCommittee`
* Flip bitfield byte-endianness
* Fix tree_hash for bitfields
* Modify CLI output for ef_tests
* Bump ssz crate version
* Update ssz_types doc comment
* Del cached tree hash tests from ssz_static tests
* Tidy SSZ dependencies
* Rename ssz_types crate to eth2_ssz_types
* validator_client: update for v0.8
* ssz_types: update union/difference for bit order swap
* beacon_node: update for v0.8, EthSpec
* types: disable cached tree hash, update min spec
* state_processing: fix slot bug in committee update
* tests: temporarily disable fork choice harness test
See #447
* committee cache: prevent out-of-bounds access
In the case where we tried to access the committee of a shard that didn't have a committee in the
current epoch, we were accessing elements beyond the end of the shuffling vector and panicking! This
commit adds a check to make the failure safe and explicit.
* fix bug in get_indexed_attestation and simplify
There was a bug in our implementation of get_indexed_attestation whereby
incorrect "committee indices" were used to index into the custody bitfield. The
bug was only observable in the case where some bits of the custody bitfield were
set to 1. The implementation has been simplified to remove the bug, and a test
added.
* state_proc: workaround for compact committees bug
https://github.com/ethereum/eth2.0-specs/issues/1315
* v0.8: updates to make the EF tests pass
* Remove redundant max operation checks.
* Always supply both messages when checking attestation signatures -- allowing
verification of an attestation with no signatures.
* Swap the order of the fork and domain constant in `get_domain`, to match
the spec.
* rustfmt
* ef_tests: add new epoch processing tests
* Integrate v0.8 into master (compiles)
* Remove unused crates, fix clippy lints
* Replace v0.6.3 tags w/ v0.8.1
* Remove old comment
* Ensure lmd ghost tests only run in release
* Update readme
2019-07-30 02:44:51 +00:00
|
|
|
for root in &mut state_b.block_roots[..] {
|
2019-06-15 13:56:41 +00:00
|
|
|
*root = hashes.next().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
let state_a_root = hashes.next().unwrap();
|
Update to frozen spec ❄️ (v0.8.1) (#444)
* types: first updates for v0.8
* state_processing: epoch processing v0.8.0
* state_processing: block processing v0.8.0
* tree_hash_derive: support generics in SignedRoot
* types v0.8: update to use ssz_types
* state_processing v0.8: use ssz_types
* ssz_types: add bitwise methods and from_elem
* types: fix v0.8 FIXMEs
* ssz_types: add bitfield shift_up
* ssz_types: iterators and DerefMut for VariableList
* types,state_processing: use VariableList
* ssz_types: fix BitVector Decode impl
Fixed a typo in the implementation of ssz::Decode for BitVector, which caused it
to be considered variable length!
* types: fix test modules for v0.8 update
* types: remove slow type-level arithmetic
* state_processing: fix tests for v0.8
* op_pool: update for v0.8
* ssz_types: Bitfield difference length-independent
Allow computing the difference of two bitfields of different lengths.
* Implement compact committee support
* epoch_processing: committee & active index roots
* state_processing: genesis state builder v0.8
* state_processing: implement v0.8.1
* Further improve tree_hash
* Strip examples, tests from cached_tree_hash
* Update TreeHash, un-impl CachedTreeHash
* Update bitfield TreeHash, un-impl CachedTreeHash
* Update FixedLenVec TreeHash, unimpl CachedTreeHash
* Update update tree_hash_derive for new TreeHash
* Fix TreeHash, un-impl CachedTreeHash for ssz_types
* Remove fixed_len_vec, ssz benches
SSZ benches relied upon fixed_len_vec -- it is easier to just delete
them and rebuild them later (when necessary)
* Remove boolean_bitfield crate
* Fix fake_crypto BLS compile errors
* Update ef_tests for new v.8 type params
* Update ef_tests submodule to v0.8.1 tag
* Make fixes to support parsing ssz ef_tests
* `compact_committee...` to `compact_committees...`
* Derive more traits for `CompactCommittee`
* Flip bitfield byte-endianness
* Fix tree_hash for bitfields
* Modify CLI output for ef_tests
* Bump ssz crate version
* Update ssz_types doc comment
* Del cached tree hash tests from ssz_static tests
* Tidy SSZ dependencies
* Rename ssz_types crate to eth2_ssz_types
* validator_client: update for v0.8
* ssz_types: update union/difference for bit order swap
* beacon_node: update for v0.8, EthSpec
* types: disable cached tree hash, update min spec
* state_processing: fix slot bug in committee update
* tests: temporarily disable fork choice harness test
See #447
* committee cache: prevent out-of-bounds access
In the case where we tried to access the committee of a shard that didn't have a committee in the
current epoch, we were accessing elements beyond the end of the shuffling vector and panicking! This
commit adds a check to make the failure safe and explicit.
* fix bug in get_indexed_attestation and simplify
There was a bug in our implementation of get_indexed_attestation whereby
incorrect "committee indices" were used to index into the custody bitfield. The
bug was only observable in the case where some bits of the custody bitfield were
set to 1. The implementation has been simplified to remove the bug, and a test
added.
* state_proc: workaround for compact committees bug
https://github.com/ethereum/eth2.0-specs/issues/1315
* v0.8: updates to make the EF tests pass
* Remove redundant max operation checks.
* Always supply both messages when checking attestation signatures -- allowing
verification of an attestation with no signatures.
* Swap the order of the fork and domain constant in `get_domain`, to match
the spec.
* rustfmt
* ef_tests: add new epoch processing tests
* Integrate v0.8 into master (compiles)
* Remove unused crates, fix clippy lints
* Replace v0.6.3 tags w/ v0.8.1
* Remove old comment
* Ensure lmd ghost tests only run in release
* Update readme
2019-07-30 02:44:51 +00:00
|
|
|
state_b.state_roots[0] = state_a_root;
|
2019-11-26 23:54:46 +00:00
|
|
|
store.put_state(&state_a_root, &state_a).unwrap();
|
2019-06-15 13:56:41 +00:00
|
|
|
|
2019-08-14 00:55:24 +00:00
|
|
|
let iter = BlockRootsIterator::new(store.clone(), &state_b);
|
2019-06-18 15:47:21 +00:00
|
|
|
|
|
|
|
assert!(
|
2019-09-30 03:58:45 +00:00
|
|
|
iter.clone().any(|(_root, slot)| slot == 0),
|
2019-06-18 15:47:21 +00:00
|
|
|
"iter should contain zero slot"
|
|
|
|
);
|
|
|
|
|
2019-06-15 13:56:41 +00:00
|
|
|
let mut collected: Vec<(Hash256, Slot)> = iter.collect();
|
|
|
|
collected.reverse();
|
|
|
|
|
2019-07-16 07:28:15 +00:00
|
|
|
let expected_len = 2 * MainnetEthSpec::slots_per_historical_root();
|
|
|
|
|
|
|
|
assert_eq!(collected.len(), expected_len);
|
|
|
|
|
2019-09-30 03:58:45 +00:00
|
|
|
for (i, item) in collected.iter().enumerate() {
|
|
|
|
assert_eq!(item.0, Hash256::from_low_u64_be(i as u64));
|
2019-08-05 06:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 15:47:21 +00:00
|
|
|
#[test]
|
|
|
|
fn state_root_iter() {
|
|
|
|
let store = Arc::new(MemoryStore::open());
|
|
|
|
let slots_per_historical_root = MainnetEthSpec::slots_per_historical_root();
|
|
|
|
|
|
|
|
let mut state_a: BeaconState<MainnetEthSpec> = get_state();
|
|
|
|
let mut state_b: BeaconState<MainnetEthSpec> = get_state();
|
|
|
|
|
|
|
|
state_a.slot = Slot::from(slots_per_historical_root);
|
|
|
|
state_b.slot = Slot::from(slots_per_historical_root * 2);
|
|
|
|
|
2019-09-30 03:58:45 +00:00
|
|
|
let mut hashes = (0..).map(Hash256::from_low_u64_be);
|
2019-06-18 15:47:21 +00:00
|
|
|
|
|
|
|
for slot in 0..slots_per_historical_root {
|
|
|
|
state_a
|
|
|
|
.set_state_root(Slot::from(slot), hashes.next().unwrap())
|
2019-09-30 03:58:45 +00:00
|
|
|
.unwrap_or_else(|_| panic!("should set state_a slot {}", slot));
|
2019-06-18 15:47:21 +00:00
|
|
|
}
|
|
|
|
for slot in slots_per_historical_root..slots_per_historical_root * 2 {
|
|
|
|
state_b
|
|
|
|
.set_state_root(Slot::from(slot), hashes.next().unwrap())
|
2019-09-30 03:58:45 +00:00
|
|
|
.unwrap_or_else(|_| panic!("should set state_b slot {}", slot));
|
2019-06-18 15:47:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 04:41:42 +00:00
|
|
|
let state_a_root = Hash256::from_low_u64_be(slots_per_historical_root as u64);
|
|
|
|
let state_b_root = Hash256::from_low_u64_be(slots_per_historical_root as u64 * 2);
|
2019-06-18 15:47:21 +00:00
|
|
|
|
2019-11-26 23:54:46 +00:00
|
|
|
store.put_state(&state_a_root, &state_a).unwrap();
|
|
|
|
store.put_state(&state_b_root, &state_b).unwrap();
|
2019-06-18 15:47:21 +00:00
|
|
|
|
2019-08-14 00:55:24 +00:00
|
|
|
let iter = StateRootsIterator::new(store.clone(), &state_b);
|
2019-06-18 15:47:21 +00:00
|
|
|
|
|
|
|
assert!(
|
2019-09-30 03:58:45 +00:00
|
|
|
iter.clone().any(|(_root, slot)| slot == 0),
|
2019-06-18 15:47:21 +00:00
|
|
|
"iter should contain zero slot"
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut collected: Vec<(Hash256, Slot)> = iter.collect();
|
|
|
|
collected.reverse();
|
|
|
|
|
2019-07-16 07:28:15 +00:00
|
|
|
let expected_len = MainnetEthSpec::slots_per_historical_root() * 2;
|
2019-06-18 15:47:21 +00:00
|
|
|
|
|
|
|
assert_eq!(collected.len(), expected_len, "collection length incorrect");
|
|
|
|
|
2019-09-30 03:58:45 +00:00
|
|
|
for (i, item) in collected.iter().enumerate() {
|
|
|
|
let (hash, slot) = *item;
|
2019-06-18 15:47:21 +00:00
|
|
|
|
|
|
|
assert_eq!(slot, i as u64, "slot mismatch at {}: {} vs {}", i, slot, i);
|
|
|
|
|
2019-08-06 04:41:42 +00:00
|
|
|
assert_eq!(
|
|
|
|
hash,
|
|
|
|
Hash256::from_low_u64_be(i as u64),
|
|
|
|
"hash mismatch at {}",
|
|
|
|
i
|
|
|
|
);
|
2019-06-18 15:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 13:56:41 +00:00
|
|
|
}
|