More vector

This commit is contained in:
Michael Sproul
2022-01-28 15:45:44 +11:00
parent 96bdc29419
commit b2063c3e21
5 changed files with 26 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
use super::*; use super::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use types::{EthSpec, FixedVector, Transaction, Unsigned, VariableList}; use ssz_types::FixedVector;
use types::{EthSpec, Transaction, Unsigned, VariableList};
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]

View File

@@ -323,7 +323,7 @@ field!(
|_| OncePerNSlots { |_| OncePerNSlots {
n: T::SlotsPerHistoricalRoot::to_u64() n: T::SlotsPerHistoricalRoot::to_u64()
}, },
|state: &BeaconState<_>, index, _| safe_modulo_index_tree(state.historical_roots(), index) |state: &BeaconState<_>, index, _| safe_modulo_index(state.historical_roots(), index)
); );
field!( field!(
@@ -530,7 +530,7 @@ pub fn load_vector_from_db<F: FixedLengthField<E>, E: EthSpec, S: KeyValueStore<
default, default,
)?; )?;
Ok(result.into()) Ok(FixedVector::new(result)?)
} }
/// The historical roots are stored in vector chunks, despite not actually being a vector. /// The historical roots are stored in vector chunks, despite not actually being a vector.
@@ -546,7 +546,7 @@ pub fn load_variable_list_from_db<F: VariableLengthField<E>, E: EthSpec, S: KeyV
let chunks: Vec<Chunk<F::Value>> = range_query(store, F::column(), start_cindex, end_cindex)?; let chunks: Vec<Chunk<F::Value>> = range_query(store, F::column(), start_cindex, end_cindex)?;
let mut result = VList::empty(); let mut result = VList::empty()?;
for (chunk_index, chunk) in chunks.into_iter().enumerate() { for (chunk_index, chunk) in chunks.into_iter().enumerate() {
for (i, value) in chunk.values.into_iter().enumerate() { for (i, value) in chunk.values.into_iter().enumerate() {
@@ -562,6 +562,7 @@ pub fn load_variable_list_from_db<F: VariableLengthField<E>, E: EthSpec, S: KeyV
} }
/// Index into a field of the state, avoiding out of bounds and division by 0. /// Index into a field of the state, avoiding out of bounds and division by 0.
#[cfg(not(feature = "milhouse"))]
fn safe_modulo_index<T: Copy>(values: &[T], index: u64) -> Result<T, ChunkError> { fn safe_modulo_index<T: Copy>(values: &[T], index: u64) -> Result<T, ChunkError> {
if values.is_empty() { if values.is_empty() {
Err(ChunkError::ZeroLengthVector) Err(ChunkError::ZeroLengthVector)
@@ -570,11 +571,8 @@ fn safe_modulo_index<T: Copy>(values: &[T], index: u64) -> Result<T, ChunkError>
} }
} }
#[cfg(not(feature = "milhouse"))]
use safe_modulo_index as safe_modulo_index_tree;
#[cfg(feature = "milhouse")] #[cfg(feature = "milhouse")]
fn safe_modulo_index_tree<V: ImmList<T>, T: Copy>(values: &V, index: u64) -> Result<T, ChunkError> { fn safe_modulo_index<V: ImmList<T>, T: Copy>(values: &V, index: u64) -> Result<T, ChunkError> {
if values.is_empty() { if values.is_empty() {
Err(ChunkError::ZeroLengthVector) Err(ChunkError::ZeroLengthVector)
} else { } else {

View File

@@ -265,7 +265,18 @@ impl<T: EthSpec> PartialBeaconState<T> {
// Patch the value for the current slot into the index for the current epoch // Patch the value for the current slot into the index for the current epoch
let current_epoch = self.slot().epoch(T::slots_per_epoch()); let current_epoch = self.slot().epoch(T::slots_per_epoch());
let len = randao_mixes.len(); let len = randao_mixes.len();
randao_mixes[current_epoch.as_usize() % len] = *self.latest_randao_value();
#[cfg(feature = "milhouse")]
{
use milhouse::interface::MutList;
randao_mixes
.replace(current_epoch.as_usize() % len, *self.latest_randao_value())?;
}
#[cfg(not(feature = "milhouse"))]
{
randao_mixes[current_epoch.as_usize() % len] = *self.latest_randao_value();
}
*self.randao_mixes_mut() = Some(randao_mixes) *self.randao_mixes_mut() = Some(randao_mixes)
} }

View File

@@ -13,7 +13,7 @@ pub fn process_eth1_data_reset<T: EthSpec>(
.safe_rem(T::SlotsPerEth1VotingPeriod::to_u64())? .safe_rem(T::SlotsPerEth1VotingPeriod::to_u64())?
== 0 == 0
{ {
*state.eth1_data_votes_mut() = VList::empty(); *state.eth1_data_votes_mut() = VList::empty()?;
} }
Ok(()) Ok(())
} }

View File

@@ -386,16 +386,16 @@ impl<T: EthSpec> BeaconState<T> {
latest_block_header: BeaconBlock::<T>::empty(spec).temporary_block_header(), latest_block_header: BeaconBlock::<T>::empty(spec).temporary_block_header(),
block_roots: FixedVector::from_elem(Hash256::zero()), block_roots: FixedVector::from_elem(Hash256::zero()),
state_roots: FixedVector::from_elem(Hash256::zero()), state_roots: FixedVector::from_elem(Hash256::zero()),
historical_roots: VList::empty(), historical_roots: VList::default(),
// Eth1 // Eth1
eth1_data, eth1_data,
eth1_data_votes: VList::empty(), eth1_data_votes: VList::default(),
eth1_deposit_index: 0, eth1_deposit_index: 0,
// Validator registry // Validator registry
validators: VList::empty(), // Set later. validators: VList::default(), // Set later.
balances: VList::empty(), // Set later. balances: VList::default(), // Set later.
// Randomness // Randomness
randao_mixes: FixedVector::from_elem(Hash256::zero()), randao_mixes: FixedVector::from_elem(Hash256::zero()),
@@ -404,8 +404,8 @@ impl<T: EthSpec> BeaconState<T> {
slashings: FixedVector::from_elem(0), slashings: FixedVector::from_elem(0),
// Attestations // Attestations
previous_epoch_attestations: VList::empty(), previous_epoch_attestations: VList::default(),
current_epoch_attestations: VList::empty(), current_epoch_attestations: VList::default(),
// Finality // Finality
justification_bits: BitVector::new(), justification_bits: BitVector::new(),