Add TreeHash trait to all types and structs

This commit is contained in:
Kirk Baird
2019-01-25 12:22:56 +11:00
parent 407bf5e06d
commit 9c9b07c182
31 changed files with 642 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
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 crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -76,4 +87,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::Hash256;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -116,4 +131,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{Decodable, DecodeError, Encodable, hash, TreeHash, SszStream};
use rand::RngCore;
use crate::test_utils::TestRandom;
use super::AttestationData;
@@ -30,6 +30,16 @@ impl Decodable for AttestationDataAndCustodyBit {
}
}
impl TreeHash for AttestationDataAndCustodyBit {
fn hash_tree_root(&self) {
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -57,4 +67,16 @@ mod test {
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
}
}

View File

@@ -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 crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -91,4 +105,16 @@ mod tests {
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
}
}

View File

@@ -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 crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -92,4 +107,16 @@ mod tests {
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
}
}

View File

@@ -3,12 +3,12 @@ use super::eth1_data::Eth1Data;
use super::eth1_data_vote::Eth1DataVote;
use super::fork::Fork;
use super::pending_attestation::PendingAttestation;
use super::ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::validator::Validator;
use super::Hash256;
use crate::test_utils::TestRandom;
use hashing::canonical_hash;
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.
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -217,4 +252,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::SlashableVoteData;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -56,4 +65,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::Hash256;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -66,4 +75,16 @@ mod tests {
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
}
}

View File

@@ -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 crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -61,4 +71,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::DepositInput;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -61,4 +71,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::Hash256;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -62,4 +72,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::Hash256;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -57,4 +66,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::Eth1Data;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -57,4 +66,16 @@ mod tests {
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
}
}

View File

@@ -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 bls::Signature;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -61,4 +71,16 @@ mod tests {
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
}
}

View File

@@ -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 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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -60,4 +70,16 @@ mod tests {
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
}
}

View File

@@ -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 crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -66,4 +77,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::Hash256;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -61,4 +71,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::ProposalSignedData;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -72,4 +84,16 @@ mod tests {
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
}
}

View File

@@ -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 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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -49,4 +58,16 @@ mod tests {
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
}
}

View File

@@ -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 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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -60,4 +70,16 @@ mod tests {
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
}
}

View File

@@ -1,4 +1,4 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::AttestationData;
use crate::test_utils::TestRandom;
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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -67,4 +78,16 @@ mod tests {
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
}
}

View File

@@ -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.
///
@@ -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)]
mod tests {
use super::*;

View File

@@ -2,7 +2,7 @@ use super::bls::PublicKey;
use super::Hash256;
use crate::test_utils::TestRandom;
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_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 {
fn random_for_test(rng: &mut T) -> 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
}
}

View File

@@ -2,7 +2,7 @@ use super::Hash256;
use crate::test_utils::TestRandom;
use bls::PublicKey;
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.
#[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 {
fn random_for_test(rng: &mut T) -> Self {
Self {
@@ -86,4 +98,16 @@ mod tests {
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
}
}