diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 49d7680d8..a0c8505b8 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -2,11 +2,11 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Attestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -37,22 +37,11 @@ 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) - } -} - #[cfg(test)] 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 ec67e30b6..e23cdab46 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, TreeHash}; use test_random_derive::TestRandom; pub const SSZ_ATTESTION_DATA_LENGTH: usize = { @@ -17,7 +17,9 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 32 // justified_block_root }; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TestRandom)] +#[derive( + Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash, TestRandom, +)] pub struct AttestationData { pub slot: Slot, pub shard: u64, @@ -45,26 +47,11 @@ 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) - } -} - #[cfg(test)] 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..9175863ae 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, TreeHash}; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash)] 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 8ea6b39e8..2a1df9e0c 100644 --- a/eth2/types/src/attester_slashing.rs +++ b/eth2/types/src/attester_slashing.rs @@ -1,30 +1,20 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 e5dc9e238..cb4e6668b 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -3,11 +3,11 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct BeaconBlock { pub slot: Slot, pub parent_root: Hash256, @@ -61,25 +61,11 @@ 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) - } -} - #[cfg(test)] 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 b492ba747..2b343b970 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -2,11 +2,10 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub attester_slashings: Vec, @@ -15,23 +14,11 @@ 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) - } -} - #[cfg(test)] 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/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index bfc87e01d..cb1e46ee5 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -2,30 +2,20 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 8b0d2bc18..11fb3386d 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -2,11 +2,12 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TestRandom)] +#[derive( + Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash, TestRandom, +)] pub struct Crosslink { pub epoch: Epoch, pub shard_block_root: Hash256, @@ -22,20 +23,11 @@ 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) - } -} - #[cfg(test)] 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 2b126b900..02da32cfe 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -2,32 +2,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 0cce11cec..349207791 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -2,32 +2,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 d14ba68ac..1f3b22779 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -3,32 +3,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 e1b968303..8eabbabc7 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -2,31 +2,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 09d462999..fa30b9052 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -2,31 +2,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 c96319d55..5b41fcc7a 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -2,32 +2,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 dba04358e..5b13a2388 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -1,11 +1,10 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Fork { pub previous_version: u64, pub current_version: u64, @@ -28,21 +27,11 @@ impl Fork { } } -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) - } -} - #[cfg(test)] 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 3bf6ff1ad..84eb59207 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -2,11 +2,10 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct PendingAttestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -14,22 +13,11 @@ 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) - } -} - #[cfg(test)] 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 f3f369b7f..6f6048ffc 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -2,32 +2,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 08ae27abf..610017c0c 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -3,11 +3,10 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ProposerSlashing { pub proposer_index: u64, pub proposal_data_1: ProposalSignedData, @@ -16,23 +15,11 @@ 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) - } -} - #[cfg(test)] 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 eb67a10c2..f5dfa8676 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -1,32 +1,21 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] 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) - } -} - #[cfg(test)] 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 db1e7fe79..c4a12338a 100644 --- a/eth2/types/src/slashable_attestation.rs +++ b/eth2/types/src/slashable_attestation.rs @@ -1,11 +1,10 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct SlashableAttestation { pub validator_indices: Vec, pub data: AttestationData, @@ -13,22 +12,11 @@ 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) - } -} - #[cfg(test)] 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 0a79166da..31dd9e0a8 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -4,11 +4,10 @@ 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, TreeHash}; use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct SlashableVoteData { pub custody_bit_0_indices: Vec, pub custody_bit_1_indices: Vec, @@ -37,24 +36,13 @@ 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) - } -} - #[cfg(test)] mod tests { use super::*; 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 35316a356..0746875f0 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -2,12 +2,11 @@ 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, TreeHash}; use test_random_derive::TestRandom; // The information gathered from the PoW chain validator registration function. -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TestRandom)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ValidatorRegistryDeltaBlock { pub latest_registry_delta_root: Hash256, pub validator_index: u32, @@ -29,23 +28,11 @@ 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) - } -} - #[cfg(test)] 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..ac66526fe 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(TreeHash)]` //! //! 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(TreeHash)] +pub fn ssz_tree_hash_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() +}