mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-15 02:42:38 +00:00
Add progress on state_processing fixed-len update
This commit is contained in:
@@ -5,7 +5,7 @@ use cached_tree_hash::{Error as TreeHashCacheError, TreeHashCache};
|
||||
use int_to_bytes::int_to_bytes32;
|
||||
use pubkey_cache::PubkeyCache;
|
||||
|
||||
use fixed_len_vec::FixedLenVec;
|
||||
use fixed_len_vec::{typenum::Unsigned, FixedLenVec};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::{hash, ssz_encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
@@ -15,7 +15,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
|
||||
pub use beacon_state_types::{BeaconStateTypes, FewValidatorsBeaconState, FoundationBeaconState};
|
||||
|
||||
mod beacon_state_types;
|
||||
pub mod beacon_state_types;
|
||||
mod epoch_cache;
|
||||
mod pubkey_cache;
|
||||
mod tests;
|
||||
@@ -80,7 +80,7 @@ where
|
||||
pub validator_registry_update_epoch: Epoch,
|
||||
|
||||
// Randomness and committees
|
||||
pub latest_randao_mixes: FixedLenVec<Hash256, T::NumLatestRandaoMixes>,
|
||||
pub latest_randao_mixes: FixedLenVec<Hash256, T::LatestRandaoMixesLength>,
|
||||
pub previous_shuffling_start_shard: u64,
|
||||
pub current_shuffling_start_shard: u64,
|
||||
pub previous_shuffling_epoch: Epoch,
|
||||
@@ -100,11 +100,11 @@ where
|
||||
pub finalized_root: Hash256,
|
||||
|
||||
// Recent state
|
||||
pub latest_crosslinks: TreeHashVector<Crosslink>,
|
||||
pub latest_block_roots: TreeHashVector<Hash256>,
|
||||
latest_state_roots: TreeHashVector<Hash256>,
|
||||
latest_active_index_roots: TreeHashVector<Hash256>,
|
||||
latest_slashed_balances: TreeHashVector<u64>,
|
||||
pub latest_crosslinks: FixedLenVec<Crosslink, T::ShardCount>,
|
||||
pub latest_block_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
latest_state_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
latest_active_index_roots: FixedLenVec<Hash256, T::LatestActiveIndexRootsLength>,
|
||||
latest_slashed_balances: FixedLenVec<u64, T::LatestSlashedExitLength>,
|
||||
pub latest_block_header: BeaconBlockHeader,
|
||||
pub historical_roots: Vec<Hash256>,
|
||||
|
||||
@@ -169,8 +169,10 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
validator_registry_update_epoch: spec.genesis_epoch,
|
||||
|
||||
// Randomness and committees
|
||||
latest_randao_mixes: vec![spec.zero_hash; spec.latest_randao_mixes_length as usize]
|
||||
.into(),
|
||||
latest_randao_mixes: FixedLenVec::from(vec![
|
||||
spec.zero_hash;
|
||||
T::LatestRandaoMixesLength::to_usize()
|
||||
]),
|
||||
previous_shuffling_start_shard: spec.genesis_start_shard,
|
||||
current_shuffling_start_shard: spec.genesis_start_shard,
|
||||
previous_shuffling_epoch: spec.genesis_epoch,
|
||||
@@ -191,11 +193,21 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
|
||||
// Recent state
|
||||
latest_crosslinks: vec![initial_crosslink; spec.shard_count as usize].into(),
|
||||
latest_block_roots: vec![spec.zero_hash; spec.slots_per_historical_root].into(),
|
||||
latest_state_roots: vec![spec.zero_hash; spec.slots_per_historical_root].into(),
|
||||
latest_active_index_roots: vec![spec.zero_hash; spec.latest_active_index_roots_length]
|
||||
.into(),
|
||||
latest_slashed_balances: vec![0; spec.latest_slashed_exit_length].into(),
|
||||
latest_block_roots: FixedLenVec::from(vec![
|
||||
spec.zero_hash;
|
||||
T::SlotsPerHistoricalRoot::to_usize()
|
||||
]),
|
||||
latest_state_roots: FixedLenVec::from(vec![
|
||||
spec.zero_hash;
|
||||
T::SlotsPerHistoricalRoot::to_usize()
|
||||
]),
|
||||
latest_active_index_roots: FixedLenVec::from(
|
||||
vec![spec.zero_hash; T::LatestActiveIndexRootsLength::to_usize()],
|
||||
),
|
||||
latest_slashed_balances: FixedLenVec::from(vec![
|
||||
0;
|
||||
T::LatestSlashedExitLength::to_usize()
|
||||
]),
|
||||
latest_block_header: BeaconBlock::empty(spec).temporary_block_header(spec),
|
||||
historical_roots: vec![],
|
||||
|
||||
@@ -228,7 +240,7 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
Hash256::from_slice(&self.tree_hash_root()[..])
|
||||
}
|
||||
|
||||
pub fn historical_batch(&self) -> HistoricalBatch {
|
||||
pub fn historical_batch(&self) -> HistoricalBatch<T> {
|
||||
HistoricalBatch {
|
||||
block_roots: self.latest_block_roots.clone(),
|
||||
state_roots: self.latest_state_roots.clone(),
|
||||
@@ -386,14 +398,9 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
/// Safely obtains the index for latest block roots, given some `slot`.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn get_latest_block_roots_index(&self, slot: Slot, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + spec.slots_per_historical_root as u64) {
|
||||
let i = slot.as_usize() % spec.slots_per_historical_root;
|
||||
if i >= self.latest_block_roots.len() {
|
||||
Err(Error::InsufficientStateRoots)
|
||||
} else {
|
||||
Ok(i)
|
||||
}
|
||||
fn get_latest_block_roots_index(&self, slot: Slot) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + self.latest_block_roots.len() as u64) {
|
||||
Ok(slot.as_usize() % self.latest_block_roots.len())
|
||||
} else {
|
||||
Err(BeaconStateError::SlotOutOfBounds)
|
||||
}
|
||||
@@ -402,12 +409,8 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
/// Return the block root at a recent `slot`.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn get_block_root(
|
||||
&self,
|
||||
slot: Slot,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<&Hash256, BeaconStateError> {
|
||||
let i = self.get_latest_block_roots_index(slot, spec)?;
|
||||
pub fn get_block_root(&self, slot: Slot) -> Result<&Hash256, BeaconStateError> {
|
||||
let i = self.get_latest_block_roots_index(slot)?;
|
||||
Ok(&self.latest_block_roots[i])
|
||||
}
|
||||
|
||||
@@ -418,9 +421,8 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
&mut self,
|
||||
slot: Slot,
|
||||
block_root: Hash256,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
let i = self.get_latest_block_roots_index(slot, spec)?;
|
||||
let i = self.get_latest_block_roots_index(slot)?;
|
||||
self.latest_block_roots[i] = block_root;
|
||||
Ok(())
|
||||
}
|
||||
@@ -430,17 +432,10 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
/// Spec v0.5.1
|
||||
fn get_randao_mix_index(&self, epoch: Epoch, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
let current_epoch = self.current_epoch(spec);
|
||||
let len = T::LatestRandaoMixesLength::to_u64();
|
||||
|
||||
if (current_epoch - (spec.latest_randao_mixes_length as u64) < epoch)
|
||||
& (epoch <= current_epoch)
|
||||
{
|
||||
let i = epoch.as_usize() % spec.latest_randao_mixes_length;
|
||||
|
||||
if i < (&self.latest_randao_mixes[..]).len() {
|
||||
Ok(i)
|
||||
} else {
|
||||
Err(Error::InsufficientRandaoMixes)
|
||||
}
|
||||
if (current_epoch - len < epoch) & (epoch <= current_epoch) {
|
||||
Ok(epoch.as_usize() % len as usize)
|
||||
} else {
|
||||
Err(Error::EpochOutOfBounds)
|
||||
}
|
||||
@@ -459,7 +454,7 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
signature: &Signature,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let i = epoch.as_usize() % spec.latest_randao_mixes_length;
|
||||
let i = epoch.as_usize() % T::LatestRandaoMixesLength::to_usize();
|
||||
|
||||
let signature_hash = Hash256::from_slice(&hash(&ssz_encode(signature)));
|
||||
|
||||
@@ -496,17 +491,12 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
fn get_active_index_root_index(&self, epoch: Epoch, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
let current_epoch = self.current_epoch(spec);
|
||||
|
||||
if (current_epoch - spec.latest_active_index_roots_length as u64
|
||||
if (current_epoch - self.latest_active_index_roots.len() as u64
|
||||
+ spec.activation_exit_delay
|
||||
< epoch)
|
||||
& (epoch <= current_epoch + spec.activation_exit_delay)
|
||||
{
|
||||
let i = epoch.as_usize() % spec.latest_active_index_roots_length;
|
||||
if i < self.latest_active_index_roots.len() {
|
||||
Ok(i)
|
||||
} else {
|
||||
Err(Error::InsufficientIndexRoots)
|
||||
}
|
||||
Ok(epoch.as_usize() % self.latest_active_index_roots.len())
|
||||
} else {
|
||||
Err(Error::EpochOutOfBounds)
|
||||
}
|
||||
@@ -537,22 +527,17 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
/// Replace `active_index_roots` with clones of `index_root`.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn fill_active_index_roots_with(&mut self, index_root: Hash256, spec: &ChainSpec) {
|
||||
pub fn fill_active_index_roots_with(&mut self, index_root: Hash256) {
|
||||
self.latest_active_index_roots =
|
||||
vec![index_root; spec.latest_active_index_roots_length as usize].into()
|
||||
vec![index_root; self.latest_active_index_roots.len() as usize].into()
|
||||
}
|
||||
|
||||
/// Safely obtains the index for latest state roots, given some `slot`.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn get_latest_state_roots_index(&self, slot: Slot, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + spec.slots_per_historical_root as u64) {
|
||||
let i = slot.as_usize() % spec.slots_per_historical_root;
|
||||
if i >= self.latest_state_roots.len() {
|
||||
Err(Error::InsufficientStateRoots)
|
||||
} else {
|
||||
Ok(i)
|
||||
}
|
||||
fn get_latest_state_roots_index(&self, slot: Slot) -> Result<usize, Error> {
|
||||
if (slot < self.slot) && (self.slot <= slot + self.latest_state_roots.len() as u64) {
|
||||
Ok(slot.as_usize() % self.latest_state_roots.len())
|
||||
} else {
|
||||
Err(BeaconStateError::SlotOutOfBounds)
|
||||
}
|
||||
@@ -561,21 +546,16 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
/// Gets the state root for some slot.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn get_state_root(&mut self, slot: Slot, spec: &ChainSpec) -> Result<&Hash256, Error> {
|
||||
let i = self.get_latest_state_roots_index(slot, spec)?;
|
||||
pub fn get_state_root(&mut self, slot: Slot) -> Result<&Hash256, Error> {
|
||||
let i = self.get_latest_state_roots_index(slot)?;
|
||||
Ok(&self.latest_state_roots[i])
|
||||
}
|
||||
|
||||
/// Sets the latest state root for slot.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn set_state_root(
|
||||
&mut self,
|
||||
slot: Slot,
|
||||
state_root: Hash256,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let i = self.get_latest_state_roots_index(slot, spec)?;
|
||||
pub fn set_state_root(&mut self, slot: Slot, state_root: Hash256) -> Result<(), Error> {
|
||||
let i = self.get_latest_state_roots_index(slot)?;
|
||||
self.latest_state_roots[i] = state_root;
|
||||
Ok(())
|
||||
}
|
||||
@@ -583,8 +563,8 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
/// Safely obtains the index for `latest_slashed_balances`, given some `epoch`.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
fn get_slashed_balance_index(&self, epoch: Epoch, spec: &ChainSpec) -> Result<usize, Error> {
|
||||
let i = epoch.as_usize() % spec.latest_slashed_exit_length;
|
||||
fn get_slashed_balance_index(&self, epoch: Epoch) -> Result<usize, Error> {
|
||||
let i = epoch.as_usize() % self.latest_slashed_balances.len();
|
||||
|
||||
// NOTE: the validity of the epoch is not checked. It is not in the spec but it's probably
|
||||
// useful to have.
|
||||
@@ -598,21 +578,16 @@ impl<T: BeaconStateTypes> BeaconState<T> {
|
||||
/// Gets the total slashed balances for some epoch.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn get_slashed_balance(&self, epoch: Epoch, spec: &ChainSpec) -> Result<u64, Error> {
|
||||
let i = self.get_slashed_balance_index(epoch, spec)?;
|
||||
pub fn get_slashed_balance(&self, epoch: Epoch) -> Result<u64, Error> {
|
||||
let i = self.get_slashed_balance_index(epoch)?;
|
||||
Ok(self.latest_slashed_balances[i])
|
||||
}
|
||||
|
||||
/// Sets the total slashed balances for some epoch.
|
||||
///
|
||||
/// Spec v0.5.1
|
||||
pub fn set_slashed_balance(
|
||||
&mut self,
|
||||
epoch: Epoch,
|
||||
balance: u64,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let i = self.get_slashed_balance_index(epoch, spec)?;
|
||||
pub fn set_slashed_balance(&mut self, epoch: Epoch, balance: u64) -> Result<(), Error> {
|
||||
let i = self.get_slashed_balance_index(epoch)?;
|
||||
self.latest_slashed_balances[i] = balance;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
use crate::*;
|
||||
use fixed_len_vec::typenum::{Unsigned, U8192};
|
||||
use fixed_len_vec::typenum::{Unsigned, U1024, U8, U8192};
|
||||
|
||||
pub trait BeaconStateTypes {
|
||||
type NumLatestRandaoMixes: Unsigned + Clone + Sync + Send;
|
||||
type ShardCount: Unsigned + Clone + Sync + Send;
|
||||
type SlotsPerHistoricalRoot: Unsigned + Clone + Sync + Send;
|
||||
type LatestRandaoMixesLength: Unsigned + Clone + Sync + Send;
|
||||
type LatestActiveIndexRootsLength: Unsigned + Clone + Sync + Send;
|
||||
type LatestSlashedExitLength: Unsigned + Clone + Sync + Send;
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct FoundationStateParams;
|
||||
|
||||
impl BeaconStateTypes for FoundationStateParams {
|
||||
type NumLatestRandaoMixes = U8192;
|
||||
type ShardCount = U1024;
|
||||
type SlotsPerHistoricalRoot = U8192;
|
||||
type LatestRandaoMixesLength = U8192;
|
||||
type LatestActiveIndexRootsLength = U8192;
|
||||
type LatestSlashedExitLength = U8192;
|
||||
}
|
||||
|
||||
pub type FoundationBeaconState = BeaconState<FoundationStateParams>;
|
||||
@@ -18,7 +26,11 @@ pub type FoundationBeaconState = BeaconState<FoundationStateParams>;
|
||||
pub struct FewValidatorsStateParams;
|
||||
|
||||
impl BeaconStateTypes for FewValidatorsStateParams {
|
||||
type NumLatestRandaoMixes = U8192;
|
||||
type ShardCount = U8;
|
||||
type SlotsPerHistoricalRoot = U8192;
|
||||
type LatestRandaoMixesLength = U8192;
|
||||
type LatestActiveIndexRootsLength = U8192;
|
||||
type LatestSlashedExitLength = U8192;
|
||||
}
|
||||
|
||||
pub type FewValidatorsBeaconState = BeaconState<FewValidatorsStateParams>;
|
||||
|
||||
@@ -70,17 +70,9 @@ pub struct ChainSpec {
|
||||
pub min_seed_lookahead: Epoch,
|
||||
pub activation_exit_delay: u64,
|
||||
pub epochs_per_eth1_voting_period: u64,
|
||||
pub slots_per_historical_root: usize,
|
||||
pub min_validator_withdrawability_delay: Epoch,
|
||||
pub persistent_committee_period: u64,
|
||||
|
||||
/*
|
||||
* State list lengths
|
||||
*/
|
||||
pub latest_randao_mixes_length: usize,
|
||||
pub latest_active_index_roots_length: usize,
|
||||
pub latest_slashed_exit_length: usize,
|
||||
|
||||
/*
|
||||
* Reward and penalty quotients
|
||||
*/
|
||||
@@ -213,17 +205,9 @@ impl ChainSpec {
|
||||
min_seed_lookahead: Epoch::new(1),
|
||||
activation_exit_delay: 4,
|
||||
epochs_per_eth1_voting_period: 16,
|
||||
slots_per_historical_root: 8_192,
|
||||
min_validator_withdrawability_delay: Epoch::new(256),
|
||||
persistent_committee_period: 2_048,
|
||||
|
||||
/*
|
||||
* State list lengths
|
||||
*/
|
||||
latest_randao_mixes_length: 8_192,
|
||||
latest_active_index_roots_length: 8_192,
|
||||
latest_slashed_exit_length: 8_192,
|
||||
|
||||
/*
|
||||
* Reward and penalty quotients
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use crate::{Hash256, TreeHashVector};
|
||||
use crate::Hash256;
|
||||
|
||||
use crate::beacon_state::BeaconStateTypes;
|
||||
use fixed_len_vec::FixedLenVec;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use test_random_derive::TestRandom;
|
||||
@@ -21,15 +23,18 @@ use tree_hash_derive::{CachedTreeHash, TreeHash};
|
||||
CachedTreeHash,
|
||||
TestRandom,
|
||||
)]
|
||||
pub struct HistoricalBatch {
|
||||
pub block_roots: TreeHashVector<Hash256>,
|
||||
pub state_roots: TreeHashVector<Hash256>,
|
||||
pub struct HistoricalBatch<T: BeaconStateTypes> {
|
||||
pub block_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
pub state_roots: FixedLenVec<Hash256, T::SlotsPerHistoricalRoot>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::beacon_state::beacon_state_types::FoundationStateParams;
|
||||
|
||||
ssz_tests!(HistoricalBatch);
|
||||
cached_tree_hash_tests!(HistoricalBatch);
|
||||
pub type FoundationHistoricalBatch = HistoricalBatch<FoundationStateParams>;
|
||||
|
||||
ssz_tests!(FoundationHistoricalBatch);
|
||||
cached_tree_hash_tests!(FoundationHistoricalBatch);
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ pub type AttesterMap = HashMap<(u64, u64), Vec<usize>>;
|
||||
pub type ProposerMap = HashMap<u64, usize>;
|
||||
|
||||
pub use bls::{AggregatePublicKey, AggregateSignature, Keypair, PublicKey, SecretKey, Signature};
|
||||
pub use fixed_len_vec::FixedLenVec;
|
||||
pub use fixed_len_vec::{typenum::Unsigned, FixedLenVec};
|
||||
pub use libp2p::floodsub::{Topic, TopicBuilder, TopicHash};
|
||||
pub use libp2p::multiaddr;
|
||||
pub use libp2p::Multiaddr;
|
||||
|
||||
@@ -30,22 +30,22 @@ impl TestingAttestationDataBuilder {
|
||||
|
||||
let target_root = if is_previous_epoch {
|
||||
*state
|
||||
.get_block_root(previous_epoch.start_slot(spec.slots_per_epoch), spec)
|
||||
.get_block_root(previous_epoch.start_slot(spec.slots_per_epoch))
|
||||
.unwrap()
|
||||
} else {
|
||||
*state
|
||||
.get_block_root(current_epoch.start_slot(spec.slots_per_epoch), spec)
|
||||
.get_block_root(current_epoch.start_slot(spec.slots_per_epoch))
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let source_root = *state
|
||||
.get_block_root(source_epoch.start_slot(spec.slots_per_epoch), spec)
|
||||
.get_block_root(source_epoch.start_slot(spec.slots_per_epoch))
|
||||
.unwrap();
|
||||
|
||||
let data = AttestationData {
|
||||
// LMD GHOST vote
|
||||
slot,
|
||||
beacon_block_root: *state.get_block_root(slot, spec).unwrap(),
|
||||
beacon_block_root: *state.get_block_root(slot).unwrap(),
|
||||
|
||||
// FFG Vote
|
||||
source_epoch,
|
||||
|
||||
Reference in New Issue
Block a user