commit
078700235a
@ -1,5 +1,5 @@
|
|||||||
use super::bls::AggregateSignature;
|
use super::bls::AggregateSignature;
|
||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::{AttestationData, Bitfield};
|
use super::{AttestationData, Bitfield};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -49,6 +49,17 @@ impl Attestation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Attestation {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.data.hash_tree_root());
|
||||||
|
result.append(&mut self.aggregation_bitfield.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_bitfield.hash_tree_root());
|
||||||
|
result.append(&mut self.aggregate_signature.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Attestation {
|
impl<T: RngCore> TestRandom<T> for Attestation {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -76,4 +87,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Attestation::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -85,6 +85,21 @@ impl Decodable for AttestationData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for AttestationData {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
result.append(&mut self.shard.hash_tree_root());
|
||||||
|
result.append(&mut self.beacon_block_root.hash_tree_root());
|
||||||
|
result.append(&mut self.epoch_boundary_root.hash_tree_root());
|
||||||
|
result.append(&mut self.shard_block_root.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_crosslink_root.hash_tree_root());
|
||||||
|
result.append(&mut self.justified_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.justified_block_root.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for AttestationData {
|
impl<T: RngCore> TestRandom<T> for AttestationData {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -116,4 +131,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = AttestationData::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{Decodable, DecodeError, Encodable, hash, TreeHash, SszStream};
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use super::AttestationData;
|
use super::AttestationData;
|
||||||
@ -30,6 +30,16 @@ impl Decodable for AttestationDataAndCustodyBit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for AttestationDataAndCustodyBit {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.data.hash_tree_root());
|
||||||
|
// TODO: add bool ssz
|
||||||
|
// result.append(custody_bit.hash_tree_root());
|
||||||
|
ssz::hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for AttestationDataAndCustodyBit {
|
impl<T: RngCore> TestRandom<T> for AttestationDataAndCustodyBit {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -57,4 +67,16 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = AttestationDataAndCustodyBit::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::{BeaconBlockBody, Eth1Data, Hash256};
|
use super::{BeaconBlockBody, Eth1Data, Hash256};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use bls::Signature;
|
use bls::Signature;
|
||||||
@ -61,6 +61,20 @@ impl Decodable for BeaconBlock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for BeaconBlock {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
result.append(&mut self.parent_root.hash_tree_root());
|
||||||
|
result.append(&mut self.state_root.hash_tree_root());
|
||||||
|
result.append(&mut self.randao_reveal.hash_tree_root());
|
||||||
|
result.append(&mut self.eth1_data.hash_tree_root());
|
||||||
|
result.append(&mut self.signature.hash_tree_root());
|
||||||
|
result.append(&mut self.body.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for BeaconBlock {
|
impl<T: RngCore> TestRandom<T> for BeaconBlock {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -91,4 +105,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = BeaconBlock::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing};
|
use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -61,6 +61,21 @@ impl Decodable for BeaconBlockBody {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for BeaconBlockBody {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.proposer_slashings.hash_tree_root());
|
||||||
|
result.append(&mut self.casper_slashings.hash_tree_root());
|
||||||
|
result.append(&mut self.attestations.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_reseeds.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_challenges.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_responses.hash_tree_root());
|
||||||
|
result.append(&mut self.deposits.hash_tree_root());
|
||||||
|
result.append(&mut self.exits.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for BeaconBlockBody {
|
impl<T: RngCore> TestRandom<T> for BeaconBlockBody {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -92,4 +107,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = BeaconBlockBody::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@ use super::eth1_data::Eth1Data;
|
|||||||
use super::eth1_data_vote::Eth1DataVote;
|
use super::eth1_data_vote::Eth1DataVote;
|
||||||
use super::fork::Fork;
|
use super::fork::Fork;
|
||||||
use super::pending_attestation::PendingAttestation;
|
use super::pending_attestation::PendingAttestation;
|
||||||
|
use super::ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::validator::Validator;
|
use super::validator::Validator;
|
||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use hashing::canonical_hash;
|
use hashing::canonical_hash;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
|
||||||
|
|
||||||
// Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used.
|
// Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used.
|
||||||
type CustodyChallenge = usize;
|
type CustodyChallenge = usize;
|
||||||
@ -166,6 +166,41 @@ impl Decodable for BeaconState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for BeaconState {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
result.append(&mut self.genesis_time.hash_tree_root());
|
||||||
|
result.append(&mut self.fork_data.hash_tree_root());
|
||||||
|
result.append(&mut self.validator_registry.hash_tree_root());
|
||||||
|
result.append(&mut self.validator_balances.hash_tree_root());
|
||||||
|
result.append(&mut self.validator_registry_update_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.validator_registry_exit_count.hash_tree_root());
|
||||||
|
result.append(&mut self.validator_registry_delta_chain_tip.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_randao_mixes.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_vdf_outputs.hash_tree_root());
|
||||||
|
result.append(&mut self.previous_epoch_start_shard.hash_tree_root());
|
||||||
|
result.append(&mut self.current_epoch_start_shard.hash_tree_root());
|
||||||
|
result.append(&mut self.previous_epoch_calculation_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.current_epoch_calculation_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.previous_epoch_randao_mix.hash_tree_root());
|
||||||
|
result.append(&mut self.current_epoch_randao_mix.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_challenges.hash_tree_root());
|
||||||
|
result.append(&mut self.previous_justified_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.justified_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.justification_bitfield.hash_tree_root());
|
||||||
|
result.append(&mut self.finalized_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_crosslinks.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_block_roots.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_penalized_exit_balances.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_attestations.hash_tree_root());
|
||||||
|
result.append(&mut self.batched_block_roots.hash_tree_root());
|
||||||
|
result.append(&mut self.latest_eth1_data.hash_tree_root());
|
||||||
|
result.append(&mut self.eth1_data_votes.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for BeaconState {
|
impl<T: RngCore> TestRandom<T> for BeaconState {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -217,4 +252,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = BeaconState::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::SlashableVoteData;
|
use super::SlashableVoteData;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -31,6 +31,15 @@ impl Decodable for CasperSlashing {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for CasperSlashing {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.slashable_vote_data_1.hash_tree_root());
|
||||||
|
result.append(&mut self.slashable_vote_data_2.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for CasperSlashing {
|
impl<T: RngCore> TestRandom<T> for CasperSlashing {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -56,4 +65,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = CasperSlashing::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -41,6 +41,15 @@ impl Decodable for Crosslink {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Crosslink {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
result.append(&mut self.shard_block_root.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Crosslink {
|
impl<T: RngCore> TestRandom<T> for Crosslink {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -66,4 +75,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Crosslink::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::{DepositData, Hash256};
|
use super::{DepositData, Hash256};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -35,6 +35,16 @@ impl Decodable for Deposit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Deposit {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.merkle_branch.hash_tree_root());
|
||||||
|
result.append(&mut self.merkle_tree_index.hash_tree_root());
|
||||||
|
result.append(&mut self.deposit_data.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Deposit {
|
impl<T: RngCore> TestRandom<T> for Deposit {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -61,4 +71,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Deposit::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::DepositInput;
|
use super::DepositInput;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -35,6 +35,16 @@ impl Decodable for DepositData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for DepositData {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.amount.hash_tree_root());
|
||||||
|
result.append(&mut self.timestamp.hash_tree_root());
|
||||||
|
result.append(&mut self.deposit_input.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for DepositData {
|
impl<T: RngCore> TestRandom<T> for DepositData {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -61,4 +71,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = DepositData::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use bls::{PublicKey, Signature};
|
use bls::{PublicKey, Signature};
|
||||||
@ -36,6 +36,16 @@ impl Decodable for DepositInput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for DepositInput {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.pubkey.hash_tree_root());
|
||||||
|
result.append(&mut self.withdrawal_credentials.hash_tree_root());
|
||||||
|
result.append(&mut self.proof_of_possession.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for DepositInput {
|
impl<T: RngCore> TestRandom<T> for DepositInput {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -62,4 +72,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = DepositInput::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -32,6 +32,15 @@ impl Decodable for Eth1Data {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Eth1Data {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.deposit_root.hash_tree_root());
|
||||||
|
result.append(&mut self.block_hash.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Eth1Data {
|
impl<T: RngCore> TestRandom<T> for Eth1Data {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -57,4 +66,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Eth1Data::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::Eth1Data;
|
use super::Eth1Data;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -32,6 +32,15 @@ impl Decodable for Eth1DataVote {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Eth1DataVote {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.eth1_data.hash_tree_root());
|
||||||
|
result.append(&mut self.vote_count.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Eth1DataVote {
|
impl<T: RngCore> TestRandom<T> for Eth1DataVote {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -57,4 +66,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Eth1DataVote::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use bls::Signature;
|
use bls::Signature;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -35,6 +35,16 @@ impl Decodable for Exit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Exit {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
result.append(&mut self.validator_index.hash_tree_root());
|
||||||
|
result.append(&mut self.signature.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Exit {
|
impl<T: RngCore> TestRandom<T> for Exit {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -61,4 +71,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Exit::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
@ -34,6 +34,16 @@ impl Decodable for Fork {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Fork {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.pre_fork_version.hash_tree_root());
|
||||||
|
result.append(&mut self.post_fork_version.hash_tree_root());
|
||||||
|
result.append(&mut self.fork_slot.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Fork {
|
impl<T: RngCore> TestRandom<T> for Fork {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -60,4 +70,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Fork::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::{AttestationData, Bitfield};
|
use super::{AttestationData, Bitfield};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -39,6 +39,17 @@ impl Decodable for PendingAttestation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for PendingAttestation {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.data.hash_tree_root());
|
||||||
|
result.append(&mut self.aggregation_bitfield.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_bitfield.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_bitfield.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for PendingAttestation {
|
impl<T: RngCore> TestRandom<T> for PendingAttestation {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -66,4 +77,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = PendingAttestation::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -35,6 +35,16 @@ impl Decodable for ProposalSignedData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for ProposalSignedData {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
result.append(&mut self.shard.hash_tree_root());
|
||||||
|
result.append(&mut self.block_root.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for ProposalSignedData {
|
impl<T: RngCore> TestRandom<T> for ProposalSignedData {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -61,4 +71,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = ProposalSignedData::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::ProposalSignedData;
|
use super::ProposalSignedData;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use bls::Signature;
|
use bls::Signature;
|
||||||
@ -44,6 +44,18 @@ impl Decodable for ProposerSlashing {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for ProposerSlashing {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.proposer_index.hash_tree_root());
|
||||||
|
result.append(&mut self.proposal_data_1.hash_tree_root());
|
||||||
|
result.append(&mut self.proposal_signature_1.hash_tree_root());
|
||||||
|
result.append(&mut self.proposal_data_2.hash_tree_root());
|
||||||
|
result.append(&mut self.proposal_signature_2.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for ProposerSlashing {
|
impl<T: RngCore> TestRandom<T> for ProposerSlashing {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -72,4 +84,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = ProposerSlashing::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
@ -24,6 +24,15 @@ impl Decodable for ShardCommittee {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for ShardCommittee {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.shard.hash_tree_root());
|
||||||
|
result.append(&mut self.committee.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for ShardCommittee {
|
impl<T: RngCore> TestRandom<T> for ShardCommittee {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -49,4 +58,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = ShardCommittee::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
@ -34,6 +34,16 @@ impl Decodable for ShardReassignmentRecord {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for ShardReassignmentRecord {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.validator_index.hash_tree_root());
|
||||||
|
result.append(&mut self.shard.hash_tree_root());
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for ShardReassignmentRecord {
|
impl<T: RngCore> TestRandom<T> for ShardReassignmentRecord {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -60,4 +70,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = ShardReassignmentRecord::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::AttestationData;
|
use super::AttestationData;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use bls::AggregateSignature;
|
use bls::AggregateSignature;
|
||||||
@ -40,6 +40,17 @@ impl Decodable for SlashableVoteData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for SlashableVoteData {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.custody_bit_0_indices.hash_tree_root());
|
||||||
|
result.append(&mut self.custody_bit_1_indices.hash_tree_root());
|
||||||
|
result.append(&mut self.data.hash_tree_root());
|
||||||
|
result.append(&mut self.aggregate_signature.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for SlashableVoteData {
|
impl<T: RngCore> TestRandom<T> for SlashableVoteData {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -67,4 +78,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = SlashableVoteData::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
|
|
||||||
/// The value of the "type" field of SpecialRecord.
|
/// The value of the "type" field of SpecialRecord.
|
||||||
///
|
///
|
||||||
@ -71,6 +71,15 @@ impl Decodable for SpecialRecord {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for SpecialRecord {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.kind.hash_tree_root());
|
||||||
|
result.append(&mut self.data.as_slice().hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -2,7 +2,7 @@ use super::bls::PublicKey;
|
|||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
|
|
||||||
const STATUS_FLAG_INITIATED_EXIT: u8 = 1;
|
const STATUS_FLAG_INITIATED_EXIT: u8 = 1;
|
||||||
const STATUS_FLAG_WITHDRAWABLE: u8 = 2;
|
const STATUS_FLAG_WITHDRAWABLE: u8 = 2;
|
||||||
@ -142,6 +142,24 @@ impl Decodable for Validator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Validator {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.pubkey.hash_tree_root());
|
||||||
|
result.append(&mut self.withdrawal_credentials.hash_tree_root());
|
||||||
|
result.append(&mut self.proposer_slots.hash_tree_root());
|
||||||
|
result.append(&mut self.activation_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.exit_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.withdrawal_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.penalized_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.exit_count.hash_tree_root());
|
||||||
|
result.append(&mut (status_flag_to_byte(self.status_flags) as u64).hash_tree_root());
|
||||||
|
result.append(&mut self.latest_custody_reseed_slot.hash_tree_root());
|
||||||
|
result.append(&mut self.penultimate_custody_reseed_slot.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for Validator {
|
impl<T: RngCore> TestRandom<T> for Validator {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -198,4 +216,16 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = Validator::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use super::Hash256;
|
|||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use bls::PublicKey;
|
use bls::PublicKey;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
|
|
||||||
// The information gathered from the PoW chain validator registration function.
|
// The information gathered from the PoW chain validator registration function.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
@ -58,6 +58,18 @@ impl Decodable for ValidatorRegistryDeltaBlock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for ValidatorRegistryDeltaBlock {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
let mut result: Vec<u8> = vec![];
|
||||||
|
result.append(&mut self.latest_registry_delta_root.hash_tree_root());
|
||||||
|
result.append(&mut self.validator_index.hash_tree_root());
|
||||||
|
result.append(&mut self.pubkey.hash_tree_root());
|
||||||
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
|
result.append(&mut self.flag.hash_tree_root());
|
||||||
|
hash(&result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for ValidatorRegistryDeltaBlock {
|
impl<T: RngCore> TestRandom<T> for ValidatorRegistryDeltaBlock {
|
||||||
fn random_for_test(rng: &mut T) -> Self {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -86,4 +98,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_hash_tree_root() {
|
||||||
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
let original = ValidatorRegistryDeltaBlock::random_for_test(&mut rng);
|
||||||
|
|
||||||
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
|
assert_eq!(result.len(), 32);
|
||||||
|
// TODO: Add further tests
|
||||||
|
// https://github.com/sigp/lighthouse/issues/170
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::{AggregatePublicKey, Signature};
|
use super::{AggregatePublicKey, Signature};
|
||||||
use bls_aggregates::AggregateSignature as RawAggregateSignature;
|
use bls_aggregates::AggregateSignature as RawAggregateSignature;
|
||||||
|
|
||||||
@ -44,6 +44,12 @@ impl Decodable for AggregateSignature {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for AggregateSignature {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
hash(&self.0.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::super::ssz::ssz_encode;
|
use super::super::ssz::ssz_encode;
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
use super::SecretKey;
|
use super::SecretKey;
|
||||||
use bls_aggregates::PublicKey as RawPublicKey;
|
use bls_aggregates::PublicKey as RawPublicKey;
|
||||||
use hex::encode as hex_encode;
|
use hex::encode as hex_encode;
|
||||||
use ssz::{decode_ssz_list, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
use ssz::{
|
||||||
|
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
|
||||||
|
};
|
||||||
use std::default;
|
use std::default;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
@ -53,6 +55,12 @@ impl Decodable for PublicKey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for PublicKey {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
hash(&self.0.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PartialEq for PublicKey {
|
impl PartialEq for PublicKey {
|
||||||
fn eq(&self, other: &PublicKey) -> bool {
|
fn eq(&self, other: &PublicKey) -> bool {
|
||||||
ssz_encode(self) == ssz_encode(other)
|
ssz_encode(self) == ssz_encode(other)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey};
|
use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey};
|
||||||
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
|
|
||||||
/// A single BLS signature.
|
/// A single BLS signature.
|
||||||
///
|
///
|
||||||
@ -40,6 +40,12 @@ impl Decodable for SecretKey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for SecretKey {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
self.0.as_bytes().clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::super::ssz::ssz_encode;
|
use super::super::ssz::ssz_encode;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||||
use super::{PublicKey, SecretKey};
|
use super::{PublicKey, SecretKey};
|
||||||
use bls_aggregates::Signature as RawSignature;
|
use bls_aggregates::Signature as RawSignature;
|
||||||
|
|
||||||
@ -57,6 +57,12 @@ impl Decodable for Signature {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TreeHash for Signature {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
hash(&self.0.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::super::ssz::ssz_encode;
|
use super::super::ssz::ssz_encode;
|
||||||
|
@ -149,6 +149,12 @@ impl ssz::Decodable for BooleanBitfield {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ssz::TreeHash for BooleanBitfield {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
self.to_bytes().hash_tree_root()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -79,6 +79,14 @@ mod tests {
|
|||||||
assert_eq!(ssz.drain(), vec![0; 32]);
|
assert_eq!(ssz.drain(), vec![0; 32]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ssz_encode_address() {
|
||||||
|
let h = Address::zero();
|
||||||
|
let mut ssz = SszStream::new();
|
||||||
|
ssz.append(&h);
|
||||||
|
assert_eq!(ssz.drain(), vec![0; 20]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ssz_encode_u8() {
|
fn test_ssz_encode_u8() {
|
||||||
let x: u8 = 0;
|
let x: u8 = 0;
|
||||||
|
@ -1,51 +1,54 @@
|
|||||||
extern crate hashing;
|
|
||||||
|
|
||||||
use self::hashing::canonical_hash;
|
|
||||||
use super::ethereum_types::{Address, H256};
|
use super::ethereum_types::{Address, H256};
|
||||||
use super::{merkle_hash, ssz_encode, TreeHash};
|
use super::{hash, merkle_hash, ssz_encode, TreeHash};
|
||||||
use std::cmp::Ord;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::hash::Hash;
|
|
||||||
|
|
||||||
impl TreeHash for u8 {
|
impl TreeHash for u8 {
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for u16 {
|
impl TreeHash for u16 {
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for u32 {
|
impl TreeHash for u32 {
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for u64 {
|
impl TreeHash for u64 {
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
|
ssz_encode(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TreeHash for usize {
|
||||||
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for Address {
|
impl TreeHash for Address {
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for H256 {
|
impl TreeHash for H256 {
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for [u8] {
|
impl TreeHash for [u8] {
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
hash(&self)
|
if self.len() > 32 {
|
||||||
|
return hash(&self);
|
||||||
|
}
|
||||||
|
self.to_vec()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,71 +56,23 @@ impl<T> TreeHash for Vec<T>
|
|||||||
where
|
where
|
||||||
T: TreeHash,
|
T: TreeHash,
|
||||||
{
|
{
|
||||||
/// Returns the merkle_hash of a list of tree_hash values created
|
/// Returns the merkle_hash of a list of hash_tree_root values created
|
||||||
/// from the given list.
|
/// from the given list.
|
||||||
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
|
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
|
||||||
/// to be handled properly (i.e. hashed) as byte array.
|
/// to be handled properly (i.e. hashed) as byte array.
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
let mut tree_hashes = self.iter().map(|x| x.tree_hash()).collect();
|
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect();
|
||||||
merkle_hash(&mut tree_hashes)
|
merkle_hash(&mut tree_hashes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K, V> TreeHash for HashMap<K, V>
|
|
||||||
where
|
|
||||||
K: Eq,
|
|
||||||
K: Hash,
|
|
||||||
K: Ord,
|
|
||||||
V: TreeHash,
|
|
||||||
{
|
|
||||||
/// Appends the tree_hash for each value of 'self, sorted by key,
|
|
||||||
/// into a byte array and returns the hash of said byte array
|
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
|
||||||
let mut items: Vec<_> = self.iter().collect();
|
|
||||||
items.sort_by(|a, b| a.0.cmp(b.0));
|
|
||||||
let mut result = Vec::new();
|
|
||||||
for item in items {
|
|
||||||
result.append(&mut item.1.tree_hash());
|
|
||||||
}
|
|
||||||
|
|
||||||
hash(&result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hash(data: &[u8]) -> Vec<u8> {
|
|
||||||
canonical_hash(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_impl_tree_hash_vec() {
|
fn test_impl_tree_hash_vec() {
|
||||||
let result = vec![1u32, 2, 3, 4, 5, 6, 7].tree_hash();
|
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root();
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_impl_tree_hash_hashmap() {
|
|
||||||
let mut map = HashMap::new();
|
|
||||||
map.insert("c", 3);
|
|
||||||
map.insert("b", 2);
|
|
||||||
map.insert("g", 7);
|
|
||||||
map.insert("d", 6);
|
|
||||||
map.insert("e", 4);
|
|
||||||
map.insert("a", 1u32);
|
|
||||||
map.insert("f", 5);
|
|
||||||
let result = map.tree_hash();
|
|
||||||
|
|
||||||
// TODO: create tests that tie-out to an offical result
|
|
||||||
assert_eq!(
|
|
||||||
result,
|
|
||||||
[
|
|
||||||
130, 215, 165, 255, 224, 6, 144, 225, 14, 139, 67, 238, 205, 240, 20, 173, 53, 0,
|
|
||||||
105, 62, 49, 174, 244, 160, 114, 92, 232, 11, 102, 200, 112, 24
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ mod impl_tree_hash;
|
|||||||
|
|
||||||
pub use crate::decode::{decode_ssz, decode_ssz_list, Decodable, DecodeError};
|
pub use crate::decode::{decode_ssz, decode_ssz_list, Decodable, DecodeError};
|
||||||
pub use crate::encode::{Encodable, SszStream};
|
pub use crate::encode::{Encodable, SszStream};
|
||||||
pub use crate::tree_hash::{merkle_hash, TreeHash};
|
pub use crate::tree_hash::{hash, merkle_hash, TreeHash};
|
||||||
|
|
||||||
pub const LENGTH_BYTES: usize = 4;
|
pub const LENGTH_BYTES: usize = 4;
|
||||||
pub const MAX_LIST_SIZE: usize = 1 << (4 * 8);
|
pub const MAX_LIST_SIZE: usize = 1 << (4 * 8);
|
||||||
|
@ -1,48 +1,41 @@
|
|||||||
|
use hashing::canonical_hash;
|
||||||
|
|
||||||
const SSZ_CHUNK_SIZE: usize = 128;
|
const SSZ_CHUNK_SIZE: usize = 128;
|
||||||
const HASHSIZE: usize = 32;
|
const HASHSIZE: usize = 32;
|
||||||
|
|
||||||
pub trait TreeHash {
|
pub trait TreeHash {
|
||||||
fn tree_hash(&self) -> Vec<u8>;
|
fn hash_tree_root(&self) -> Vec<u8>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
|
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
|
||||||
/// Note that this will consume 'list'.
|
/// Note that this will consume 'list'.
|
||||||
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
||||||
// flatten list
|
// flatten list
|
||||||
let (chunk_size, mut data) = list_to_blob(list);
|
let (chunk_size, mut chunkz) = list_to_blob(list);
|
||||||
|
|
||||||
// get data_len as bytes. It will hashed will the merkle root
|
// get data_len as bytes. It will hashed will the merkle root
|
||||||
let dlen = list.len() as u64;
|
let datalen = list.len().to_le_bytes();
|
||||||
let data_len_bytes = &mut dlen.tree_hash();
|
|
||||||
data_len_bytes.resize(32, 0);
|
|
||||||
|
|
||||||
// merklize
|
// Tree-hash
|
||||||
let mut mhash = hash_level(&mut data, chunk_size);
|
while chunkz.len() > HASHSIZE {
|
||||||
while mhash.len() > HASHSIZE {
|
let mut new_chunkz: Vec<u8> = Vec::new();
|
||||||
mhash = hash_level(&mut mhash, HASHSIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
mhash.append(data_len_bytes);
|
for two_chunks in chunkz.chunks(chunk_size * 2) {
|
||||||
mhash.as_slice().tree_hash()
|
if two_chunks.len() == chunk_size {
|
||||||
}
|
// Odd number of chunks
|
||||||
|
|
||||||
/// Takes a flat vector of bytes. It then hashes 'chunk_size * 2' slices into
|
|
||||||
/// a byte vector of hashes, divisible by HASHSIZE
|
|
||||||
fn hash_level(data: &mut Vec<u8>, chunk_size: usize) -> Vec<u8> {
|
|
||||||
let mut result: Vec<u8> = Vec::new();
|
|
||||||
for two_chunks in data.chunks(chunk_size * 2) {
|
|
||||||
if two_chunks.len() == chunk_size && data.len() > chunk_size {
|
|
||||||
// if there is only one chunk here, hash it with a zero-byte
|
|
||||||
// SSZ_CHUNK_SIZE vector
|
|
||||||
let mut c = two_chunks.to_vec();
|
let mut c = two_chunks.to_vec();
|
||||||
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
||||||
result.append(&mut c.as_slice().tree_hash());
|
new_chunkz.append(&mut hash(&c));
|
||||||
} else {
|
} else {
|
||||||
result.append(&mut two_chunks.tree_hash());
|
// Hash two chuncks together
|
||||||
|
new_chunkz.append(&mut hash(two_chunks));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
chunkz = new_chunkz;
|
||||||
|
}
|
||||||
|
|
||||||
result
|
chunkz.append(&mut datalen.to_vec());
|
||||||
|
hash(&chunkz)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
||||||
@ -67,10 +60,13 @@ fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
|||||||
data.append(item);
|
data.append(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(chunk_size, data)
|
(chunk_size, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn hash(data: &[u8]) -> Vec<u8> {
|
||||||
|
canonical_hash(data)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user