From f95a0134e679e1f7ea191625dcced70f654f5d18 Mon Sep 17 00:00:00 2001 From: mjkeating Date: Fri, 22 Feb 2019 13:07:04 -0800 Subject: [PATCH 1/2] now using the Hashtree macro for most struct types --- eth2/types/src/attestation.rs | 19 ++------- eth2/types/src/attestation_data.rs | 23 ++--------- .../src/attestation_data_and_custody_bit.rs | 17 ++------ eth2/types/src/attester_slashing.rs | 16 ++------ eth2/types/src/beacon_block.rs | 22 ++-------- eth2/types/src/beacon_block_body.rs | 19 ++------- eth2/types/src/beacon_state.rs | 40 +------------------ eth2/types/src/casper_slashing.rs | 16 ++------ eth2/types/src/crosslink.rs | 16 ++------ eth2/types/src/deposit.rs | 17 ++------ eth2/types/src/deposit_data.rs | 17 ++------ eth2/types/src/deposit_input.rs | 17 ++------ eth2/types/src/eth1_data.rs | 16 ++------ eth2/types/src/eth1_data_vote.rs | 16 ++------ eth2/types/src/exit.rs | 17 ++------ eth2/types/src/fork.rs | 17 ++------ eth2/types/src/pending_attestation.rs | 18 ++------- eth2/types/src/proposal_signed_data.rs | 17 ++------ eth2/types/src/proposer_slashing.rs | 19 ++------- eth2/types/src/shard_reassignment_record.rs | 17 ++------ eth2/types/src/slashable_attestation.rs | 18 ++------- eth2/types/src/slashable_vote_data.rs | 18 ++------- .../src/validator_registry_delta_block.rs | 19 ++------- eth2/utils/ssz/src/impl_tree_hash.rs | 6 +++ eth2/utils/ssz/src/tree_hash.rs | 4 +- eth2/utils/ssz_derive/src/lib.rs | 32 +++++++++++++++ 26 files changed, 110 insertions(+), 363 deletions(-) diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 7388a8e49..a8523dfe8 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -2,10 +2,10 @@ use super::{AggregatePublicKey, AggregateSignature, AttestationData, Bitfield, H use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz::TreeHash; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, Hashtree)] pub struct Attestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -34,17 +34,6 @@ impl Attestation { } } -impl TreeHash for Attestation { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.aggregation_bitfield.hash_tree_root_internal()); - result.append(&mut self.data.hash_tree_root_internal()); - result.append(&mut self.custody_bitfield.hash_tree_root_internal()); - result.append(&mut self.aggregate_signature.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for Attestation { fn random_for_test(rng: &mut T) -> Self { Self { @@ -60,7 +49,7 @@ impl TestRandom for Attestation { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index 7edb0b72b..d2da31102 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -2,8 +2,8 @@ use crate::test_utils::TestRandom; use crate::{AttestationDataAndCustodyBit, Crosslink, Epoch, Hash256, Slot}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz::TreeHash; +use ssz_derive::{Decode, Encode, Hashtree}; pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 8 + // slot @@ -16,7 +16,7 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 32 // justified_block_root }; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, Hashtree)] pub struct AttestationData { pub slot: Slot, pub shard: u64, @@ -44,21 +44,6 @@ impl AttestationData { } } -impl TreeHash for AttestationData { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.slot.hash_tree_root_internal()); - result.append(&mut self.shard.hash_tree_root_internal()); - result.append(&mut self.beacon_block_root.hash_tree_root_internal()); - result.append(&mut self.epoch_boundary_root.hash_tree_root_internal()); - result.append(&mut self.shard_block_root.hash_tree_root_internal()); - result.append(&mut self.latest_crosslink.hash_tree_root_internal()); - result.append(&mut self.justified_epoch.hash_tree_root_internal()); - result.append(&mut self.justified_block_root.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for AttestationData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -78,7 +63,7 @@ impl TestRandom for AttestationData { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/attestation_data_and_custody_bit.rs b/eth2/types/src/attestation_data_and_custody_bit.rs index 3f107be82..33b7f3715 100644 --- a/eth2/types/src/attestation_data_and_custody_bit.rs +++ b/eth2/types/src/attestation_data_and_custody_bit.rs @@ -2,25 +2,14 @@ use super::AttestationData; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::TreeHash; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, Hashtree)] pub struct AttestationDataAndCustodyBit { pub data: AttestationData, pub custody_bit: bool, } -impl TreeHash for AttestationDataAndCustodyBit { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.data.hash_tree_root_internal()); - // TODO: add bool ssz - // result.append(custody_bit.hash_tree_root_internal()); - ssz::hash(&result) - } -} - impl TestRandom for AttestationDataAndCustodyBit { fn random_for_test(rng: &mut T) -> Self { Self { @@ -35,7 +24,7 @@ impl TestRandom for AttestationDataAndCustodyBit { mod test { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/attester_slashing.rs b/eth2/types/src/attester_slashing.rs index f84998324..76c0c2b2c 100644 --- a/eth2/types/src/attester_slashing.rs +++ b/eth2/types/src/attester_slashing.rs @@ -1,24 +1,14 @@ use crate::{test_utils::TestRandom, SlashableAttestation}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct AttesterSlashing { pub slashable_attestation_1: SlashableAttestation, pub slashable_attestation_2: SlashableAttestation, } -impl TreeHash for AttesterSlashing { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.slashable_attestation_1.hash_tree_root_internal()); - result.append(&mut self.slashable_attestation_2.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for AttesterSlashing { fn random_for_test(rng: &mut T) -> Self { Self { @@ -32,7 +22,7 @@ impl TestRandom for AttesterSlashing { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index c252d03f7..d1dcd58df 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -3,10 +3,10 @@ use crate::{BeaconBlockBody, ChainSpec, Eth1Data, Hash256, ProposalSignedData, S use bls::Signature; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz::TreeHash; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct BeaconBlock { pub slot: Slot, pub parent_root: Hash256, @@ -60,20 +60,6 @@ impl BeaconBlock { } } -impl TreeHash for BeaconBlock { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.slot.hash_tree_root_internal()); - result.append(&mut self.parent_root.hash_tree_root_internal()); - result.append(&mut self.state_root.hash_tree_root_internal()); - result.append(&mut self.randao_reveal.hash_tree_root_internal()); - result.append(&mut self.eth1_data.hash_tree_root_internal()); - result.append(&mut self.signature.hash_tree_root_internal()); - result.append(&mut self.body.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for BeaconBlock { fn random_for_test(rng: &mut T) -> Self { Self { @@ -92,7 +78,7 @@ impl TestRandom for BeaconBlock { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index e051f5940..62e8e1e90 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -2,10 +2,9 @@ use super::{Attestation, AttesterSlashing, Deposit, Exit, ProposerSlashing}; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub attester_slashings: Vec, @@ -14,18 +13,6 @@ pub struct BeaconBlockBody { pub exits: Vec, } -impl TreeHash for BeaconBlockBody { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.proposer_slashings.hash_tree_root_internal()); - result.append(&mut self.attester_slashings.hash_tree_root_internal()); - result.append(&mut self.attestations.hash_tree_root_internal()); - result.append(&mut self.deposits.hash_tree_root_internal()); - result.append(&mut self.exits.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for BeaconBlockBody { fn random_for_test(rng: &mut T) -> Self { Self { @@ -42,7 +29,7 @@ impl TestRandom for BeaconBlockBody { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 21deb6fe7..ee3d42d80 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -10,7 +10,7 @@ use log::trace; use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; use swap_or_not_shuffle::get_permutated_index; mod tests; @@ -52,7 +52,7 @@ macro_rules! safe_sub_assign { }; } -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] pub struct BeaconState { // Misc pub slot: Slot, @@ -968,42 +968,6 @@ impl From for InclusionError { } } -impl TreeHash for BeaconState { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.slot.hash_tree_root_internal()); - result.append(&mut self.genesis_time.hash_tree_root_internal()); - result.append(&mut self.fork.hash_tree_root_internal()); - result.append(&mut self.validator_registry.hash_tree_root_internal()); - result.append(&mut self.validator_balances.hash_tree_root_internal()); - result.append( - &mut self - .validator_registry_update_epoch - .hash_tree_root_internal(), - ); - result.append(&mut self.latest_randao_mixes.hash_tree_root_internal()); - result.append(&mut self.previous_epoch_start_shard.hash_tree_root_internal()); - result.append(&mut self.current_epoch_start_shard.hash_tree_root_internal()); - result.append(&mut self.previous_calculation_epoch.hash_tree_root_internal()); - result.append(&mut self.current_calculation_epoch.hash_tree_root_internal()); - result.append(&mut self.previous_epoch_seed.hash_tree_root_internal()); - result.append(&mut self.current_epoch_seed.hash_tree_root_internal()); - result.append(&mut self.previous_justified_epoch.hash_tree_root_internal()); - result.append(&mut self.justified_epoch.hash_tree_root_internal()); - result.append(&mut self.justification_bitfield.hash_tree_root_internal()); - result.append(&mut self.finalized_epoch.hash_tree_root_internal()); - result.append(&mut self.latest_crosslinks.hash_tree_root_internal()); - result.append(&mut self.latest_block_roots.hash_tree_root_internal()); - result.append(&mut self.latest_index_roots.hash_tree_root_internal()); - result.append(&mut self.latest_penalized_balances.hash_tree_root_internal()); - result.append(&mut self.latest_attestations.hash_tree_root_internal()); - result.append(&mut self.batched_block_roots.hash_tree_root_internal()); - result.append(&mut self.latest_eth1_data.hash_tree_root_internal()); - result.append(&mut self.eth1_data_votes.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for BeaconState { fn random_for_test(rng: &mut T) -> Self { Self { diff --git a/eth2/types/src/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index 6346db65c..5532a8b57 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -2,24 +2,14 @@ use super::SlashableVoteData; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct CasperSlashing { pub slashable_vote_data_1: SlashableVoteData, pub slashable_vote_data_2: SlashableVoteData, } -impl TreeHash for CasperSlashing { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.slashable_vote_data_1.hash_tree_root_internal()); - result.append(&mut self.slashable_vote_data_2.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for CasperSlashing { fn random_for_test(rng: &mut T) -> Self { Self { @@ -33,7 +23,7 @@ impl TestRandom for CasperSlashing { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index 19c71f604..45a92d787 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -2,10 +2,9 @@ use crate::test_utils::TestRandom; use crate::{Epoch, Hash256}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, Hashtree)] pub struct Crosslink { pub epoch: Epoch, pub shard_block_root: Hash256, @@ -21,15 +20,6 @@ impl Crosslink { } } -impl TreeHash for Crosslink { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.epoch.hash_tree_root_internal()); - result.append(&mut self.shard_block_root.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for Crosslink { fn random_for_test(rng: &mut T) -> Self { Self { @@ -43,7 +33,7 @@ impl TestRandom for Crosslink { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/deposit.rs b/eth2/types/src/deposit.rs index 78f43532a..5a738d4c5 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -2,26 +2,15 @@ use super::{DepositData, Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct Deposit { pub branch: Vec, pub index: u64, pub deposit_data: DepositData, } -impl TreeHash for Deposit { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.branch.hash_tree_root_internal()); - result.append(&mut self.index.hash_tree_root_internal()); - result.append(&mut self.deposit_data.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for Deposit { fn random_for_test(rng: &mut T) -> Self { Self { @@ -36,7 +25,7 @@ impl TestRandom for Deposit { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index 8f49deb3c..fa0bd78b6 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -2,26 +2,15 @@ use super::DepositInput; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct DepositData { pub amount: u64, pub timestamp: u64, pub deposit_input: DepositInput, } -impl TreeHash for DepositData { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.amount.hash_tree_root_internal()); - result.append(&mut self.timestamp.hash_tree_root_internal()); - result.append(&mut self.deposit_input.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for DepositData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -36,7 +25,7 @@ impl TestRandom for DepositData { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/deposit_input.rs b/eth2/types/src/deposit_input.rs index 7556fc2ca..24146ad44 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -3,26 +3,15 @@ use crate::test_utils::TestRandom; use bls::{PublicKey, Signature}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, pub proof_of_possession: Signature, } -impl TreeHash for DepositInput { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.pubkey.hash_tree_root_internal()); - result.append(&mut self.withdrawal_credentials.hash_tree_root_internal()); - result.append(&mut self.proof_of_possession.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for DepositInput { fn random_for_test(rng: &mut T) -> Self { Self { @@ -37,7 +26,7 @@ impl TestRandom for DepositInput { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/eth1_data.rs b/eth2/types/src/eth1_data.rs index b0dc14e7a..cdf746fae 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -2,25 +2,15 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] pub struct Eth1Data { pub deposit_root: Hash256, pub block_hash: Hash256, } -impl TreeHash for Eth1Data { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.deposit_root.hash_tree_root_internal()); - result.append(&mut self.block_hash.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for Eth1Data { fn random_for_test(rng: &mut T) -> Self { Self { @@ -34,7 +24,7 @@ impl TestRandom for Eth1Data { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/eth1_data_vote.rs b/eth2/types/src/eth1_data_vote.rs index eda6e6a6a..acf77ffac 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -2,25 +2,15 @@ use super::Eth1Data; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] pub struct Eth1DataVote { pub eth1_data: Eth1Data, pub vote_count: u64, } -impl TreeHash for Eth1DataVote { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.eth1_data.hash_tree_root_internal()); - result.append(&mut self.vote_count.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for Eth1DataVote { fn random_for_test(rng: &mut T) -> Self { Self { @@ -34,7 +24,7 @@ impl TestRandom for Eth1DataVote { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/exit.rs b/eth2/types/src/exit.rs index 18d743b83..c0af761fd 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -2,26 +2,15 @@ use crate::{test_utils::TestRandom, Epoch}; use bls::Signature; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct Exit { pub epoch: Epoch, pub validator_index: u64, pub signature: Signature, } -impl TreeHash for Exit { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.epoch.hash_tree_root_internal()); - result.append(&mut self.validator_index.hash_tree_root_internal()); - result.append(&mut self.signature.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for Exit { fn random_for_test(rng: &mut T) -> Self { Self { @@ -36,7 +25,7 @@ impl TestRandom for Exit { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index 85d530e19..c6c2b27ff 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -1,26 +1,15 @@ use crate::{test_utils::TestRandom, Epoch}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, Hashtree)] pub struct Fork { pub previous_version: u64, pub current_version: u64, pub epoch: Epoch, } -impl TreeHash for Fork { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.previous_version.hash_tree_root_internal()); - result.append(&mut self.current_version.hash_tree_root_internal()); - result.append(&mut self.epoch.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for Fork { fn random_for_test(rng: &mut T) -> Self { Self { @@ -35,7 +24,7 @@ impl TestRandom for Fork { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index 42f990210..b6f152321 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -2,10 +2,9 @@ use crate::test_utils::TestRandom; use crate::{AttestationData, Bitfield, Slot}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, Hashtree)] pub struct PendingAttestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -13,17 +12,6 @@ pub struct PendingAttestation { pub inclusion_slot: Slot, } -impl TreeHash for PendingAttestation { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.aggregation_bitfield.hash_tree_root_internal()); - result.append(&mut self.data.hash_tree_root_internal()); - result.append(&mut self.custody_bitfield.hash_tree_root_internal()); - result.append(&mut self.inclusion_slot.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for PendingAttestation { fn random_for_test(rng: &mut T) -> Self { Self { @@ -39,7 +27,7 @@ impl TestRandom for PendingAttestation { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/proposal_signed_data.rs b/eth2/types/src/proposal_signed_data.rs index 63c0f1ce6..53f04956b 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -2,26 +2,15 @@ use crate::test_utils::TestRandom; use crate::{Hash256, Slot}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] pub struct ProposalSignedData { pub slot: Slot, pub shard: u64, pub block_root: Hash256, } -impl TreeHash for ProposalSignedData { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.slot.hash_tree_root_internal()); - result.append(&mut self.shard.hash_tree_root_internal()); - result.append(&mut self.block_root.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for ProposalSignedData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -36,7 +25,7 @@ impl TestRandom for ProposalSignedData { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index b3a819a7f..739c5f84d 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -3,10 +3,9 @@ use crate::test_utils::TestRandom; use bls::Signature; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct ProposerSlashing { pub proposer_index: u64, pub proposal_data_1: ProposalSignedData, @@ -15,18 +14,6 @@ pub struct ProposerSlashing { pub proposal_signature_2: Signature, } -impl TreeHash for ProposerSlashing { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.proposer_index.hash_tree_root_internal()); - result.append(&mut self.proposal_data_1.hash_tree_root_internal()); - result.append(&mut self.proposal_signature_1.hash_tree_root_internal()); - result.append(&mut self.proposal_data_2.hash_tree_root_internal()); - result.append(&mut self.proposal_signature_2.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for ProposerSlashing { fn random_for_test(rng: &mut T) -> Self { Self { @@ -43,7 +30,7 @@ impl TestRandom for ProposerSlashing { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/shard_reassignment_record.rs b/eth2/types/src/shard_reassignment_record.rs index 511fe13ca..4fa685d26 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -1,26 +1,15 @@ use crate::{test_utils::TestRandom, Slot}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct ShardReassignmentRecord { pub validator_index: u64, pub shard: u64, pub slot: Slot, } -impl TreeHash for ShardReassignmentRecord { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.validator_index.hash_tree_root_internal()); - result.append(&mut self.shard.hash_tree_root_internal()); - result.append(&mut self.slot.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for ShardReassignmentRecord { fn random_for_test(rng: &mut T) -> Self { Self { @@ -35,7 +24,7 @@ impl TestRandom for ShardReassignmentRecord { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/slashable_attestation.rs b/eth2/types/src/slashable_attestation.rs index 676954ec2..64507994b 100644 --- a/eth2/types/src/slashable_attestation.rs +++ b/eth2/types/src/slashable_attestation.rs @@ -1,10 +1,9 @@ use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield}; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct SlashableAttestation { pub validator_indices: Vec, pub data: AttestationData, @@ -12,17 +11,6 @@ pub struct SlashableAttestation { pub aggregate_signature: AggregateSignature, } -impl TreeHash for SlashableAttestation { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.validator_indices.hash_tree_root_internal()); - result.append(&mut self.data.hash_tree_root_internal()); - result.append(&mut self.custody_bitfield.hash_tree_root_internal()); - result.append(&mut self.aggregate_signature.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for SlashableAttestation { fn random_for_test(rng: &mut T) -> Self { Self { @@ -38,7 +26,7 @@ impl TestRandom for SlashableAttestation { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index bdd1d0619..53742372c 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -4,10 +4,9 @@ use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] pub struct SlashableVoteData { pub custody_bit_0_indices: Vec, pub custody_bit_1_indices: Vec, @@ -36,17 +35,6 @@ impl SlashableVoteData { } } -impl TreeHash for SlashableVoteData { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.custody_bit_0_indices.hash_tree_root_internal()); - result.append(&mut self.custody_bit_1_indices.hash_tree_root_internal()); - result.append(&mut self.data.hash_tree_root_internal()); - result.append(&mut self.aggregate_signature.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for SlashableVoteData { fn random_for_test(rng: &mut T) -> Self { Self { @@ -64,7 +52,7 @@ mod tests { use crate::chain_spec::ChainSpec; use crate::slot_epoch::{Epoch, Slot}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_is_double_vote_true() { diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs index 14f9c6ce5..c42300cb4 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -2,11 +2,10 @@ use crate::{test_utils::TestRandom, Hash256, Slot}; use bls::PublicKey; use rand::RngCore; use serde_derive::Serialize; -use ssz::{hash, TreeHash}; -use ssz_derive::{Decode, Encode}; +use ssz_derive::{Decode, Encode, Hashtree}; // The information gathered from the PoW chain validator registration function. -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, Hashtree)] pub struct ValidatorRegistryDeltaBlock { pub latest_registry_delta_root: Hash256, pub validator_index: u32, @@ -28,18 +27,6 @@ impl Default for ValidatorRegistryDeltaBlock { } } -impl TreeHash for ValidatorRegistryDeltaBlock { - fn hash_tree_root_internal(&self) -> Vec { - let mut result: Vec = vec![]; - result.append(&mut self.latest_registry_delta_root.hash_tree_root_internal()); - result.append(&mut self.validator_index.hash_tree_root_internal()); - result.append(&mut self.pubkey.hash_tree_root_internal()); - result.append(&mut self.slot.hash_tree_root_internal()); - result.append(&mut self.flag.hash_tree_root_internal()); - hash(&result) - } -} - impl TestRandom for ValidatorRegistryDeltaBlock { fn random_for_test(rng: &mut T) -> Self { Self { @@ -56,7 +43,7 @@ impl TestRandom for ValidatorRegistryDeltaBlock { mod tests { use super::*; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{ssz_encode, Decodable}; + use ssz::{ssz_encode, Decodable, TreeHash}; #[test] pub fn test_ssz_round_trip() { diff --git a/eth2/utils/ssz/src/impl_tree_hash.rs b/eth2/utils/ssz/src/impl_tree_hash.rs index 7c3dae596..54bd7c139 100644 --- a/eth2/utils/ssz/src/impl_tree_hash.rs +++ b/eth2/utils/ssz/src/impl_tree_hash.rs @@ -32,6 +32,12 @@ impl TreeHash for usize { } } +impl TreeHash for bool { + fn hash_tree_root_internal(&self) -> Vec { + ssz_encode(self) + } +} + impl TreeHash for Address { fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) diff --git a/eth2/utils/ssz/src/tree_hash.rs b/eth2/utils/ssz/src/tree_hash.rs index bb05f01db..7c1ab35e9 100644 --- a/eth2/utils/ssz/src/tree_hash.rs +++ b/eth2/utils/ssz/src/tree_hash.rs @@ -7,9 +7,7 @@ pub trait TreeHash { fn hash_tree_root_internal(&self) -> Vec; fn hash_tree_root(&self) -> Vec { let mut result = self.hash_tree_root_internal(); - if result.len() < HASHSIZE { - zpad(&mut result, HASHSIZE); - } + zpad(&mut result, HASHSIZE); result } } diff --git a/eth2/utils/ssz_derive/src/lib.rs b/eth2/utils/ssz_derive/src/lib.rs index 1bc5caef1..09e1b2dc7 100644 --- a/eth2/utils/ssz_derive/src/lib.rs +++ b/eth2/utils/ssz_derive/src/lib.rs @@ -2,6 +2,7 @@ //! //! - `#[derive(Encode)]` //! - `#[derive(Decode)]` +//! - `#[derive(Hashtree)]` //! //! These macros provide SSZ encoding/decoding for a `struct`. Fields are encoded/decoded in the //! order they are defined. @@ -126,3 +127,34 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream { }; output.into() } + +/// Implements `ssz::TreeHash` for some `struct`. +/// +/// Fields are processed in the order they are defined. +#[proc_macro_derive(Hashtree)] +pub fn ssz_hashtree_derive(input: TokenStream) -> TokenStream { + let item = parse_macro_input!(input as DeriveInput); + + let name = &item.ident; + + let struct_data = match &item.data { + syn::Data::Struct(s) => s, + _ => panic!("ssz_derive only supports structs."), + }; + + let field_idents = get_named_field_idents(&struct_data); + + let output = quote! { + impl ssz::TreeHash for #name { + fn hash_tree_root_internal(&self) -> Vec { + let mut result: Vec = vec![]; + #( + result.append(&mut self.#field_idents.hash_tree_root_internal()); + )* + + ssz::hash(&result) + } + } + }; + output.into() +} From d7184345b89e073b99fd92f65e48ea3ce8fe9cf0 Mon Sep 17 00:00:00 2001 From: mjkeating Date: Mon, 25 Feb 2019 09:17:17 -0800 Subject: [PATCH 2/2] renamed the macro Hashtree to TreeHash --- eth2/types/src/attestation.rs | 4 ++-- eth2/types/src/attestation_data.rs | 4 ++-- eth2/types/src/attestation_data_and_custody_bit.rs | 4 ++-- eth2/types/src/attester_slashing.rs | 4 ++-- eth2/types/src/beacon_block.rs | 4 ++-- eth2/types/src/beacon_block_body.rs | 4 ++-- eth2/types/src/casper_slashing.rs | 4 ++-- eth2/types/src/crosslink.rs | 4 ++-- eth2/types/src/deposit.rs | 4 ++-- eth2/types/src/deposit_data.rs | 4 ++-- eth2/types/src/deposit_input.rs | 4 ++-- eth2/types/src/eth1_data.rs | 4 ++-- eth2/types/src/eth1_data_vote.rs | 4 ++-- eth2/types/src/exit.rs | 4 ++-- eth2/types/src/fork.rs | 4 ++-- eth2/types/src/pending_attestation.rs | 4 ++-- eth2/types/src/proposal_signed_data.rs | 4 ++-- eth2/types/src/proposer_slashing.rs | 4 ++-- eth2/types/src/shard_reassignment_record.rs | 4 ++-- eth2/types/src/slashable_attestation.rs | 4 ++-- eth2/types/src/slashable_vote_data.rs | 4 ++-- eth2/types/src/validator_registry_delta_block.rs | 4 ++-- eth2/utils/ssz_derive/src/lib.rs | 6 +++--- 23 files changed, 47 insertions(+), 47 deletions(-) diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index a8523dfe8..66140decb 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -3,9 +3,9 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; use ssz::TreeHash; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)] pub struct Attestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index d2da31102..868f9743a 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -3,7 +3,7 @@ use crate::{AttestationDataAndCustodyBit, Crosslink, Epoch, Hash256, Slot}; use rand::RngCore; use serde_derive::Serialize; use ssz::TreeHash; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 8 + // slot @@ -16,7 +16,7 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 32 // justified_block_root }; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, Hashtree)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash)] pub struct AttestationData { pub slot: Slot, pub shard: u64, diff --git a/eth2/types/src/attestation_data_and_custody_bit.rs b/eth2/types/src/attestation_data_and_custody_bit.rs index 33b7f3715..9175863ae 100644 --- a/eth2/types/src/attestation_data_and_custody_bit.rs +++ b/eth2/types/src/attestation_data_and_custody_bit.rs @@ -2,9 +2,9 @@ use super::AttestationData; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash)] pub struct AttestationDataAndCustodyBit { pub data: AttestationData, pub custody_bit: bool, diff --git a/eth2/types/src/attester_slashing.rs b/eth2/types/src/attester_slashing.rs index 76c0c2b2c..96204edc5 100644 --- a/eth2/types/src/attester_slashing.rs +++ b/eth2/types/src/attester_slashing.rs @@ -1,9 +1,9 @@ use crate::{test_utils::TestRandom, SlashableAttestation}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct AttesterSlashing { pub slashable_attestation_1: SlashableAttestation, pub slashable_attestation_2: SlashableAttestation, diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index d1dcd58df..9d769cbed 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -4,9 +4,9 @@ use bls::Signature; use rand::RngCore; use serde_derive::Serialize; use ssz::TreeHash; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct BeaconBlock { pub slot: Slot, pub parent_root: Hash256, diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index 62e8e1e90..915e6435c 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -2,9 +2,9 @@ use super::{Attestation, AttesterSlashing, Deposit, Exit, ProposerSlashing}; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub attester_slashings: Vec, diff --git a/eth2/types/src/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index 5532a8b57..76a6515a2 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -2,9 +2,9 @@ use super::SlashableVoteData; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct CasperSlashing { pub slashable_vote_data_1: SlashableVoteData, pub slashable_vote_data_2: SlashableVoteData, diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index 45a92d787..9c7b8eeed 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -2,9 +2,9 @@ use crate::test_utils::TestRandom; use crate::{Epoch, Hash256}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, Hashtree)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash)] pub struct Crosslink { pub epoch: Epoch, pub shard_block_root: Hash256, diff --git a/eth2/types/src/deposit.rs b/eth2/types/src/deposit.rs index 5a738d4c5..de485cd2b 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -2,9 +2,9 @@ use super::{DepositData, Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct Deposit { pub branch: Vec, pub index: u64, diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index fa0bd78b6..6ac40c0f8 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -2,9 +2,9 @@ use super::DepositInput; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct DepositData { pub amount: u64, pub timestamp: u64, diff --git a/eth2/types/src/deposit_input.rs b/eth2/types/src/deposit_input.rs index 24146ad44..5baa2226d 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -3,9 +3,9 @@ use crate::test_utils::TestRandom; use bls::{PublicKey, Signature}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, diff --git a/eth2/types/src/eth1_data.rs b/eth2/types/src/eth1_data.rs index cdf746fae..2cbfad770 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -2,10 +2,10 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] pub struct Eth1Data { pub deposit_root: Hash256, pub block_hash: Hash256, diff --git a/eth2/types/src/eth1_data_vote.rs b/eth2/types/src/eth1_data_vote.rs index acf77ffac..e2a98df3c 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -2,10 +2,10 @@ use super::Eth1Data; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] pub struct Eth1DataVote { pub eth1_data: Eth1Data, pub vote_count: u64, diff --git a/eth2/types/src/exit.rs b/eth2/types/src/exit.rs index c0af761fd..f564c8bb5 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -2,9 +2,9 @@ use crate::{test_utils::TestRandom, Epoch}; use bls::Signature; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct Exit { pub epoch: Epoch, pub validator_index: u64, diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index c6c2b27ff..35285d6ed 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -1,9 +1,9 @@ use crate::{test_utils::TestRandom, Epoch}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash)] pub struct Fork { pub previous_version: u64, pub current_version: u64, diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index b6f152321..53f1868d6 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -2,9 +2,9 @@ use crate::test_utils::TestRandom; use crate::{AttestationData, Bitfield, Slot}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)] pub struct PendingAttestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, diff --git a/eth2/types/src/proposal_signed_data.rs b/eth2/types/src/proposal_signed_data.rs index 53f04956b..164192cc9 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -2,9 +2,9 @@ use crate::test_utils::TestRandom; use crate::{Hash256, Slot}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] pub struct ProposalSignedData { pub slot: Slot, pub shard: u64, diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index 739c5f84d..fc5276dfe 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -3,9 +3,9 @@ use crate::test_utils::TestRandom; use bls::Signature; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct ProposerSlashing { pub proposer_index: u64, pub proposal_data_1: ProposalSignedData, diff --git a/eth2/types/src/shard_reassignment_record.rs b/eth2/types/src/shard_reassignment_record.rs index 4fa685d26..e7a22e1fa 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -1,9 +1,9 @@ use crate::{test_utils::TestRandom, Slot}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct ShardReassignmentRecord { pub validator_index: u64, pub shard: u64, diff --git a/eth2/types/src/slashable_attestation.rs b/eth2/types/src/slashable_attestation.rs index 64507994b..3315f1d35 100644 --- a/eth2/types/src/slashable_attestation.rs +++ b/eth2/types/src/slashable_attestation.rs @@ -1,9 +1,9 @@ use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield}; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct SlashableAttestation { pub validator_indices: Vec, pub data: AttestationData, diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index 53742372c..e8ac61dbe 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -4,9 +4,9 @@ use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] pub struct SlashableVoteData { pub custody_bit_0_indices: Vec, pub custody_bit_1_indices: Vec, diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs index c42300cb4..86f5718d6 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -2,10 +2,10 @@ use crate::{test_utils::TestRandom, Hash256, Slot}; use bls::PublicKey; use rand::RngCore; use serde_derive::Serialize; -use ssz_derive::{Decode, Encode, Hashtree}; +use ssz_derive::{Decode, Encode, TreeHash}; // The information gathered from the PoW chain validator registration function. -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, Hashtree)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)] pub struct ValidatorRegistryDeltaBlock { pub latest_registry_delta_root: Hash256, pub validator_index: u32, diff --git a/eth2/utils/ssz_derive/src/lib.rs b/eth2/utils/ssz_derive/src/lib.rs index 09e1b2dc7..ac66526fe 100644 --- a/eth2/utils/ssz_derive/src/lib.rs +++ b/eth2/utils/ssz_derive/src/lib.rs @@ -2,7 +2,7 @@ //! //! - `#[derive(Encode)]` //! - `#[derive(Decode)]` -//! - `#[derive(Hashtree)]` +//! - `#[derive(TreeHash)]` //! //! These macros provide SSZ encoding/decoding for a `struct`. Fields are encoded/decoded in the //! order they are defined. @@ -131,8 +131,8 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream { /// Implements `ssz::TreeHash` for some `struct`. /// /// Fields are processed in the order they are defined. -#[proc_macro_derive(Hashtree)] -pub fn ssz_hashtree_derive(input: TokenStream) -> TokenStream { +#[proc_macro_derive(TreeHash)] +pub fn ssz_tree_hash_derive(input: TokenStream) -> TokenStream { let item = parse_macro_input!(input as DeriveInput); let name = &item.ident;