Merge branch 'master' into lmd-ghost
This commit is contained in:
commit
6b4c0fe1a3
@ -2,7 +2,7 @@ use super::{AttestationData, Bitfield};
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::AggregateSignature;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Attestation {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
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};
|
||||
|
||||
pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
|
||||
8 + // slot
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::AttestationData;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct AttestationDataAndCustodyBit {
|
||||
@ -27,6 +27,16 @@ impl Decodable for AttestationDataAndCustodyBit {
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for AttestationDataAndCustodyBit {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
let mut 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 {
|
||||
@ -55,4 +65,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
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use crate::test_utils::TestRandom;
|
||||
use bls::Signature;
|
||||
use hashing::canonical_hash;
|
||||
use rand::RngCore;
|
||||
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
mod signing;
|
||||
|
||||
@ -63,6 +63,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 {
|
||||
@ -93,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 = 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,5 +1,5 @@
|
||||
use crate::{BeaconBlock, ChainSpec, Hash256, ProposalSignedData};
|
||||
use hashing::hash_tree_root;
|
||||
use ssz::TreeHash;
|
||||
|
||||
impl BeaconBlock {
|
||||
pub fn proposal_root(&self, spec: &ChainSpec) -> Hash256 {
|
||||
@ -14,6 +14,6 @@ impl BeaconBlock {
|
||||
shard: spec.beacon_chain_shard_number,
|
||||
block_root: block_without_signature_root,
|
||||
};
|
||||
Hash256::from_slice(&hash_tree_root(&proposal)[..])
|
||||
Hash256::from_slice(&proposal.hash_tree_root()[..])
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
// The following types are just dummy classes as they will not be defined until
|
||||
// Phase 1 (Sharding phase)
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use hashing::canonical_hash;
|
||||
use rand::RngCore;
|
||||
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
mod slot_advance;
|
||||
|
||||
@ -170,6 +170,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 {
|
||||
@ -221,4 +256,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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::SlashableVoteData;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct CasperSlashing {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
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};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Crosslink {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::{DepositData, Hash256};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Deposit {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::DepositInput;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct DepositData {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::{PublicKey, Signature};
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct DepositInput {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
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};
|
||||
|
||||
// Note: this is refer to as DepositRootVote in specs
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::Eth1Data;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
// Note: this is refer to as DepositRootVote in specs
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::Signature;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Exit {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Fork {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::{AttestationData, Bitfield};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PendingAttestation {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
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};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
pub struct ProposalSignedData {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use super::ProposalSignedData;
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::Signature;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct ProposerSlashing {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ShardCommittee {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct ShardReassignmentRecord {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use super::AttestationData;
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::AggregateSignature;
|
||||
use rand::RngCore;
|
||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct SlashableVoteData {
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use 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::*;
|
||||
|
@ -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};
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{AggregatePublicKey, Signature};
|
||||
use bls_aggregates::AggregateSignature as RawAggregateSignature;
|
||||
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
/// A BLS aggregate signature.
|
||||
///
|
||||
@ -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)]
|
||||
mod tests {
|
||||
use super::super::{Keypair, Signature};
|
||||
|
@ -1,7 +1,9 @@
|
||||
use super::SecretKey;
|
||||
use bls_aggregates::PublicKey as RawPublicKey;
|
||||
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::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 {
|
||||
fn eq(&self, other: &PublicKey) -> bool {
|
||||
ssz_encode(self) == ssz_encode(other)
|
||||
|
@ -1,5 +1,5 @@
|
||||
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.
|
||||
///
|
||||
@ -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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{PublicKey, SecretKey};
|
||||
use bls_aggregates::Signature as RawSignature;
|
||||
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||
use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
|
||||
|
||||
/// A single BLS signature.
|
||||
///
|
||||
@ -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)]
|
||||
mod tests {
|
||||
use super::super::Keypair;
|
||||
|
@ -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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -6,4 +6,3 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
tiny-keccak = "1.4.2"
|
||||
ssz = { path = "../ssz" }
|
||||
|
@ -1,4 +1,3 @@
|
||||
use ssz::{ssz_encode, Encodable as SszEncodable};
|
||||
use tiny_keccak::Keccak;
|
||||
|
||||
pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
|
||||
@ -9,10 +8,6 @@ pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
|
||||
result
|
||||
}
|
||||
|
||||
pub fn hash_tree_root<T: SszEncodable>(input: &T) -> Vec<u8> {
|
||||
canonical_hash(&ssz_encode(input))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -7,3 +7,4 @@ edition = "2018"
|
||||
[dependencies]
|
||||
bytes = "0.4.9"
|
||||
ethereum-types = "0.4.0"
|
||||
hashing = { path = "../hashing" }
|
||||
|
@ -79,6 +79,14 @@ mod tests {
|
||||
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]
|
||||
fn test_ssz_encode_u8() {
|
||||
let x: u8 = 0;
|
||||
|
78
eth2/utils/ssz/src/impl_tree_hash.rs
Normal file
78
eth2/utils/ssz/src/impl_tree_hash.rs
Normal file
@ -0,0 +1,78 @@
|
||||
use super::ethereum_types::{Address, H256};
|
||||
use super::{hash, merkle_hash, ssz_encode, TreeHash};
|
||||
|
||||
impl TreeHash for u8 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for u16 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for u32 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for u64 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for usize {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for Address {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for H256 {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
ssz_encode(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for [u8] {
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
if self.len() > 32 {
|
||||
return hash(&self);
|
||||
}
|
||||
self.to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> TreeHash for Vec<T>
|
||||
where
|
||||
T: TreeHash,
|
||||
{
|
||||
/// Returns the merkle_hash of a list of hash_tree_root values created
|
||||
/// from the given list.
|
||||
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
|
||||
/// to be handled properly (i.e. hashed) as byte array.
|
||||
fn hash_tree_root(&self) -> Vec<u8> {
|
||||
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect();
|
||||
merkle_hash(&mut tree_hashes)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_impl_tree_hash_vec() {
|
||||
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root();
|
||||
assert_eq!(result.len(), 32);
|
||||
}
|
||||
}
|
@ -12,12 +12,15 @@ extern crate ethereum_types;
|
||||
|
||||
pub mod decode;
|
||||
pub mod encode;
|
||||
pub mod tree_hash;
|
||||
|
||||
mod impl_decode;
|
||||
mod impl_encode;
|
||||
mod impl_tree_hash;
|
||||
|
||||
pub use crate::decode::{decode_ssz, decode_ssz_list, Decodable, DecodeError};
|
||||
pub use crate::encode::{Encodable, SszStream};
|
||||
pub use crate::tree_hash::{hash, merkle_hash, TreeHash};
|
||||
|
||||
pub const LENGTH_BYTES: usize = 4;
|
||||
pub const MAX_LIST_SIZE: usize = 1 << (4 * 8);
|
||||
|
86
eth2/utils/ssz/src/tree_hash.rs
Normal file
86
eth2/utils/ssz/src/tree_hash.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use hashing::canonical_hash;
|
||||
|
||||
const SSZ_CHUNK_SIZE: usize = 128;
|
||||
const HASHSIZE: usize = 32;
|
||||
|
||||
pub trait TreeHash {
|
||||
fn hash_tree_root(&self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
|
||||
/// Note that this will consume 'list'.
|
||||
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
||||
// flatten list
|
||||
let (mut chunk_size, mut chunkz) = list_to_blob(list);
|
||||
|
||||
// get data_len as bytes. It will hashed will the merkle root
|
||||
let datalen = list.len().to_le_bytes();
|
||||
|
||||
// Tree-hash
|
||||
while chunkz.len() > HASHSIZE {
|
||||
let mut new_chunkz: Vec<u8> = Vec::new();
|
||||
|
||||
for two_chunks in chunkz.chunks(chunk_size * 2) {
|
||||
if two_chunks.len() == chunk_size {
|
||||
// Odd number of chunks
|
||||
let mut c = two_chunks.to_vec();
|
||||
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
||||
new_chunkz.append(&mut hash(&c));
|
||||
} else {
|
||||
// Hash two chuncks together
|
||||
new_chunkz.append(&mut hash(two_chunks));
|
||||
}
|
||||
chunk_size = HASHSIZE;
|
||||
}
|
||||
chunkz = new_chunkz;
|
||||
}
|
||||
|
||||
chunkz.append(&mut datalen.to_vec());
|
||||
hash(&chunkz)
|
||||
}
|
||||
|
||||
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
||||
let chunk_size = if list.is_empty() {
|
||||
SSZ_CHUNK_SIZE
|
||||
} else if list[0].len() < SSZ_CHUNK_SIZE {
|
||||
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
|
||||
items_per_chunk * list[0].len()
|
||||
} else {
|
||||
list[0].len()
|
||||
};
|
||||
|
||||
let mut data = Vec::new();
|
||||
if list.is_empty() {
|
||||
// handle and empty list
|
||||
data.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
||||
} else {
|
||||
// just create a blob here; we'll divide into
|
||||
// chunked slices when we merklize
|
||||
data.reserve(list[0].len() * list.len());
|
||||
for item in list.iter_mut() {
|
||||
data.append(item);
|
||||
}
|
||||
}
|
||||
(chunk_size, data)
|
||||
}
|
||||
|
||||
pub fn hash(data: &[u8]) -> Vec<u8> {
|
||||
canonical_hash(data)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_merkle_hash() {
|
||||
let data1 = vec![1; 100];
|
||||
let data2 = vec![2; 100];
|
||||
let data3 = vec![3; 100];
|
||||
let mut list = vec![data1, data2, data3];
|
||||
let result = merkle_hash(&mut list);
|
||||
|
||||
//note: should test againt a known test hash value
|
||||
assert_eq!(HASHSIZE, result.len());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user