Add vector type to tree hashing

This commit is contained in:
Paul Hauner
2019-04-17 10:57:36 +10:00
parent 49d066015b
commit af39f096e7
11 changed files with 211 additions and 72 deletions

View File

@@ -60,7 +60,7 @@ pub struct BeaconState {
pub validator_registry_update_epoch: Epoch,
// Randomness and committees
pub latest_randao_mixes: Vec<Hash256>,
pub latest_randao_mixes: TreeHashVector<Hash256>,
pub previous_shuffling_start_shard: u64,
pub current_shuffling_start_shard: u64,
pub previous_shuffling_epoch: Epoch,
@@ -80,11 +80,11 @@ pub struct BeaconState {
pub finalized_root: Hash256,
// Recent state
pub latest_crosslinks: Vec<Crosslink>,
latest_block_roots: Vec<Hash256>,
latest_state_roots: Vec<Hash256>,
latest_active_index_roots: Vec<Hash256>,
latest_slashed_balances: Vec<u64>,
pub latest_crosslinks: TreeHashVector<Crosslink>,
latest_block_roots: TreeHashVector<Hash256>,
latest_state_roots: TreeHashVector<Hash256>,
latest_active_index_roots: TreeHashVector<Hash256>,
latest_slashed_balances: TreeHashVector<u64>,
pub latest_block_header: BeaconBlockHeader,
pub historical_roots: Vec<Hash256>,
@@ -139,7 +139,8 @@ impl BeaconState {
validator_registry_update_epoch: spec.genesis_epoch,
// Randomness and committees
latest_randao_mixes: vec![spec.zero_hash; spec.latest_randao_mixes_length as usize],
latest_randao_mixes: vec![spec.zero_hash; spec.latest_randao_mixes_length as usize]
.into(),
previous_shuffling_start_shard: spec.genesis_start_shard,
current_shuffling_start_shard: spec.genesis_start_shard,
previous_shuffling_epoch: spec.genesis_epoch,
@@ -159,11 +160,12 @@ impl BeaconState {
finalized_root: spec.zero_hash,
// Recent state
latest_crosslinks: vec![initial_crosslink; spec.shard_count as usize],
latest_block_roots: vec![spec.zero_hash; spec.slots_per_historical_root],
latest_state_roots: vec![spec.zero_hash; spec.slots_per_historical_root],
latest_active_index_roots: vec![spec.zero_hash; spec.latest_active_index_roots_length],
latest_slashed_balances: vec![0; spec.latest_slashed_exit_length],
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_header: BeaconBlock::empty(spec).temporary_block_header(spec),
historical_roots: vec![],
@@ -505,7 +507,7 @@ impl BeaconState {
/// Spec v0.5.0
pub fn fill_active_index_roots_with(&mut self, index_root: Hash256, spec: &ChainSpec) {
self.latest_active_index_roots =
vec![index_root; spec.latest_active_index_roots_length as usize]
vec![index_root; spec.latest_active_index_roots_length as usize].into()
}
/// Safely obtains the index for latest state roots, given some `slot`.

View File

@@ -1,5 +1,5 @@
use crate::test_utils::TestRandom;
use crate::Hash256;
use crate::{Hash256, TreeHashVector};
use rand::RngCore;
use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
@@ -11,8 +11,8 @@ use tree_hash_derive::TreeHash;
/// Spec v0.5.0
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
pub struct HistoricalBatch {
pub block_roots: Vec<Hash256>,
pub state_roots: Vec<Hash256>,
pub block_roots: TreeHashVector<Hash256>,
pub state_roots: TreeHashVector<Hash256>,
}
#[cfg(test)]

View File

@@ -27,6 +27,7 @@ pub mod pending_attestation;
pub mod proposer_slashing;
pub mod slashable_attestation;
pub mod transfer;
pub mod tree_hash_vector;
pub mod voluntary_exit;
#[macro_use]
pub mod slot_epoch_macros;
@@ -65,6 +66,7 @@ pub use crate::slashable_attestation::SlashableAttestation;
pub use crate::slot_epoch::{Epoch, Slot};
pub use crate::slot_height::SlotHeight;
pub use crate::transfer::Transfer;
pub use crate::tree_hash_vector::TreeHashVector;
pub use crate::validator::Validator;
pub use crate::voluntary_exit::VoluntaryExit;

View File

@@ -17,7 +17,10 @@ mod testing_voluntary_exit_builder;
pub use generate_deterministic_keypairs::generate_deterministic_keypairs;
pub use keypairs_file::KeypairsFile;
pub use rand::{prng::XorShiftRng, SeedableRng};
pub use rand::{
RngCore,
{prng::XorShiftRng, SeedableRng},
};
pub use serde_utils::{fork_from_hex_str, u8_from_hex_str};
pub use test_random::TestRandom;
pub use testing_attestation_builder::TestingAttestationBuilder;

View File

@@ -0,0 +1,82 @@
use crate::test_utils::{RngCore, TestRandom};
use serde_derive::{Deserialize, Serialize};
use ssz::{Decodable, DecodeError, Encodable, SszStream};
use std::ops::{Deref, DerefMut};
use tree_hash::TreeHash;
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct TreeHashVector<T>(Vec<T>);
impl<T> From<Vec<T>> for TreeHashVector<T> {
fn from(vec: Vec<T>) -> TreeHashVector<T> {
TreeHashVector(vec)
}
}
impl<T> Into<Vec<T>> for TreeHashVector<T> {
fn into(self) -> Vec<T> {
self.0
}
}
impl<T> Deref for TreeHashVector<T> {
type Target = Vec<T>;
fn deref(&self) -> &Vec<T> {
&self.0
}
}
impl<T> DerefMut for TreeHashVector<T> {
fn deref_mut(&mut self) -> &mut Vec<T> {
&mut self.0
}
}
impl<T> tree_hash::TreeHash for TreeHashVector<T>
where
T: TreeHash,
{
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::Vector
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
unreachable!("Vector should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
unreachable!("Vector should never be packed.")
}
fn tree_hash_root(&self) -> Vec<u8> {
tree_hash::standard_tree_hash::vec_tree_hash_root(self)
}
}
impl<T> Encodable for TreeHashVector<T>
where
T: Encodable,
{
fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(self)
}
}
impl<T> Decodable for TreeHashVector<T>
where
T: Decodable,
{
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
ssz::decode_ssz_list(bytes, index).and_then(|(vec, i)| Ok((vec.into(), i)))
}
}
impl<T: RngCore, U> TestRandom<T> for TreeHashVector<U>
where
U: TestRandom<T>,
{
fn random_for_test(rng: &mut T) -> Self {
Vec::random_for_test(rng).into()
}
}