2022-01-19 00:24:19 +00:00
|
|
|
use crate::upgrade::{upgrade_to_altair, upgrade_to_bellatrix};
|
2020-05-17 11:16:48 +00:00
|
|
|
use crate::{per_epoch_processing::EpochProcessingSummary, *};
|
Remove saturating arith from state_processing (#1644)
## Issue Addressed
Resolves #1100
## Proposed Changes
* Implement the `SafeArith` trait for `Slot` and `Epoch`, so that methods like `safe_add` become available.
* Tweak the `SafeArith` trait to allow a different `Rhs` type (analagous to `std::ops::Add`, etc).
* Add a `legacy-arith` feature to `types` and `state_processing` that conditionally enables implementations of
the `std` ops with saturating semantics.
* Check compilation of `types` and `state_processing` _without_ `legacy-arith` on CI,
thus guaranteeing that they only use the `SafeArith` primitives :tada:
## Additional Info
The `legacy-arith` feature gets turned on by all higher-level crates that depend on `state_processing` or `types`, thus allowing the beacon chain, networking, and other components to continue to rely on the availability of ops like `+`, `-`, `*`, etc.
**This is a consensus-breaking change**, but brings us in line with the spec, and our incompatibilities shouldn't have been reachable with any valid configuration of Eth2 parameters.
2020-09-25 05:18:21 +00:00
|
|
|
use safe_arith::{ArithError, SafeArith};
|
2019-03-18 05:53:59 +00:00
|
|
|
use types::*;
|
2019-03-06 21:37:13 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
|
|
|
BeaconStateError(BeaconStateError),
|
|
|
|
EpochProcessingError(EpochProcessingError),
|
Remove saturating arith from state_processing (#1644)
## Issue Addressed
Resolves #1100
## Proposed Changes
* Implement the `SafeArith` trait for `Slot` and `Epoch`, so that methods like `safe_add` become available.
* Tweak the `SafeArith` trait to allow a different `Rhs` type (analagous to `std::ops::Add`, etc).
* Add a `legacy-arith` feature to `types` and `state_processing` that conditionally enables implementations of
the `std` ops with saturating semantics.
* Check compilation of `types` and `state_processing` _without_ `legacy-arith` on CI,
thus guaranteeing that they only use the `SafeArith` primitives :tada:
## Additional Info
The `legacy-arith` feature gets turned on by all higher-level crates that depend on `state_processing` or `types`, thus allowing the beacon chain, networking, and other components to continue to rely on the availability of ops like `+`, `-`, `*`, etc.
**This is a consensus-breaking change**, but brings us in line with the spec, and our incompatibilities shouldn't have been reachable with any valid configuration of Eth2 parameters.
2020-09-25 05:18:21 +00:00
|
|
|
ArithError(ArithError),
|
2021-07-09 06:15:32 +00:00
|
|
|
InconsistentStateFork(InconsistentFork),
|
Remove saturating arith from state_processing (#1644)
## Issue Addressed
Resolves #1100
## Proposed Changes
* Implement the `SafeArith` trait for `Slot` and `Epoch`, so that methods like `safe_add` become available.
* Tweak the `SafeArith` trait to allow a different `Rhs` type (analagous to `std::ops::Add`, etc).
* Add a `legacy-arith` feature to `types` and `state_processing` that conditionally enables implementations of
the `std` ops with saturating semantics.
* Check compilation of `types` and `state_processing` _without_ `legacy-arith` on CI,
thus guaranteeing that they only use the `SafeArith` primitives :tada:
## Additional Info
The `legacy-arith` feature gets turned on by all higher-level crates that depend on `state_processing` or `types`, thus allowing the beacon chain, networking, and other components to continue to rely on the availability of ops like `+`, `-`, `*`, etc.
**This is a consensus-breaking change**, but brings us in line with the spec, and our incompatibilities shouldn't have been reachable with any valid configuration of Eth2 parameters.
2020-09-25 05:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ArithError> for Error {
|
|
|
|
fn from(e: ArithError) -> Self {
|
|
|
|
Self::ArithError(e)
|
|
|
|
}
|
2019-03-06 21:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Advances a state forward by one slot, performing per-epoch processing if required.
|
|
|
|
///
|
2020-01-03 04:09:00 +00:00
|
|
|
/// If the root of the supplied `state` is known, then it can be passed as `state_root`. If
|
|
|
|
/// `state_root` is `None`, the root of `state` will be computed using a cached tree hash.
|
|
|
|
/// Providing the `state_root` makes this function several orders of magniude faster.
|
2019-05-10 04:47:09 +00:00
|
|
|
pub fn per_slot_processing<T: EthSpec>(
|
2019-05-08 05:36:02 +00:00
|
|
|
state: &mut BeaconState<T>,
|
2020-01-03 04:09:00 +00:00
|
|
|
state_root: Option<Hash256>,
|
2019-05-08 05:36:02 +00:00
|
|
|
spec: &ChainSpec,
|
2021-08-31 23:31:36 +00:00
|
|
|
) -> Result<Option<EpochProcessingSummary<T>>, Error> {
|
2021-07-09 06:15:32 +00:00
|
|
|
// Verify that the `BeaconState` instantiation matches the fork at `state.slot()`.
|
|
|
|
state
|
|
|
|
.fork_name(spec)
|
|
|
|
.map_err(Error::InconsistentStateFork)?;
|
|
|
|
|
2020-01-03 04:09:00 +00:00
|
|
|
cache_state(state, state_root)?;
|
2019-03-18 05:53:59 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
let summary = if state.slot() > spec.genesis_slot
|
|
|
|
&& state.slot().safe_add(1)?.safe_rem(T::slots_per_epoch())? == 0
|
2020-07-23 14:18:00 +00:00
|
|
|
{
|
|
|
|
Some(per_epoch_processing(state, spec)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-03-06 21:37:13 +00:00
|
|
|
|
2021-07-09 06:15:32 +00:00
|
|
|
state.slot_mut().safe_add_assign(1)?;
|
|
|
|
|
2021-09-08 18:45:22 +00:00
|
|
|
// Process fork upgrades here. Note that multiple upgrades can potentially run
|
|
|
|
// in sequence if they are scheduled in the same Epoch (common in testnets)
|
|
|
|
if state.slot().safe_rem(T::slots_per_epoch())? == 0 {
|
|
|
|
// If the Altair fork epoch is reached, perform an irregular state upgrade.
|
|
|
|
if spec.altair_fork_epoch == Some(state.current_epoch()) {
|
|
|
|
upgrade_to_altair(state, spec)?;
|
|
|
|
}
|
|
|
|
// If the Merge fork epoch is reached, perform an irregular state upgrade.
|
2022-01-19 00:24:19 +00:00
|
|
|
if spec.bellatrix_fork_epoch == Some(state.current_epoch()) {
|
|
|
|
upgrade_to_bellatrix(state, spec)?;
|
2021-09-08 18:45:22 +00:00
|
|
|
}
|
2021-07-09 06:15:32 +00:00
|
|
|
}
|
2019-03-06 21:37:13 +00:00
|
|
|
|
2020-05-17 11:16:48 +00:00
|
|
|
Ok(summary)
|
2019-03-18 05:53:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 04:09:00 +00:00
|
|
|
fn cache_state<T: EthSpec>(
|
|
|
|
state: &mut BeaconState<T>,
|
|
|
|
state_root: Option<Hash256>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let previous_state_root = if let Some(root) = state_root {
|
|
|
|
root
|
|
|
|
} else {
|
|
|
|
state.update_tree_hash_cache()?
|
|
|
|
};
|
2019-03-18 05:53:59 +00:00
|
|
|
|
|
|
|
// Note: increment the state slot here to allow use of our `state_root` and `block_root`
|
|
|
|
// getter/setter functions.
|
|
|
|
//
|
2021-06-17 02:10:46 +00:00
|
|
|
// This is a bit hacky, however it gets the job done safely without lots of code.
|
2021-07-09 06:15:32 +00:00
|
|
|
let previous_slot = state.slot();
|
|
|
|
state.slot_mut().safe_add_assign(1)?;
|
2019-03-18 05:53:59 +00:00
|
|
|
|
2019-04-17 07:17:43 +00:00
|
|
|
// Store the previous slot's post state transition root.
|
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.set_state_root(previous_slot, previous_state_root)?;
|
|
|
|
|
|
|
|
// Cache latest block header state root
|
2021-07-09 06:15:32 +00:00
|
|
|
if state.latest_block_header().state_root == Hash256::zero() {
|
|
|
|
state.latest_block_header_mut().state_root = previous_state_root;
|
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
|
|
|
}
|
2019-04-17 07:17:43 +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
|
|
|
// Cache block root
|
2021-07-09 06:15:32 +00:00
|
|
|
let latest_block_root = state.latest_block_header().canonical_root();
|
2019-05-08 05:36:02 +00:00
|
|
|
state.set_block_root(previous_slot, latest_block_root)?;
|
2019-03-18 05:53:59 +00:00
|
|
|
|
|
|
|
// Set the state slot back to what it should be.
|
2021-07-09 06:15:32 +00:00
|
|
|
state.slot_mut().safe_sub_assign(1)?;
|
2019-03-18 05:53:59 +00:00
|
|
|
|
2019-03-06 21:37:13 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BeaconStateError> for Error {
|
|
|
|
fn from(e: BeaconStateError) -> Error {
|
|
|
|
Error::BeaconStateError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EpochProcessingError> for Error {
|
|
|
|
fn from(e: EpochProcessingError) -> Error {
|
|
|
|
Error::EpochProcessingError(e)
|
|
|
|
}
|
|
|
|
}
|