From 7434ff47badd69746b0ac9d6b2c3f5788fee29eb Mon Sep 17 00:00:00 2001 From: thojest Date: Thu, 14 Feb 2019 13:28:42 +0100 Subject: [PATCH 1/7] added is_double_vote and is_surround_vote (lighthouse-150) --- eth2/types/src/slashable_vote_data.rs | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index acffca26d..31433a0b1 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -1,4 +1,5 @@ use super::AttestationData; +use crate::spec::ChainSpec; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; @@ -13,6 +14,21 @@ pub struct SlashableVoteData { pub aggregate_signature: AggregateSignature, } +impl SlashableVoteData { + pub fn is_double_vote(&self, other: &SlashableVoteData, spec: &ChainSpec) -> bool { + self.data.slot.epoch(spec.epoch_length) == other.data.slot.epoch(spec.epoch_length) + } + + pub fn is_surround_vote(&self, other: &SlashableVoteData, spec: &ChainSpec) -> bool { + let source_epoch_1 = self.data.justified_epoch; + let source_epoch_2 = other.data.justified_epoch; + let target_epoch_1 = self.data.slot.epoch(spec.epoch_length); + let target_epoch_2 = other.data.slot.epoch(spec.epoch_length); + + (source_epoch_1 < source_epoch_2) && (target_epoch_2 < target_epoch_1) + } +} + impl Encodable for SlashableVoteData { fn ssz_append(&self, s: &mut SszStream) { s.append_vec(&self.custody_bit_0_indices); @@ -66,9 +82,71 @@ impl TestRandom for SlashableVoteData { #[cfg(test)] mod tests { use super::*; + use crate::slot_epoch_height::{Epoch, Slot}; + use crate::spec::ChainSpec; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use ssz::ssz_encode; + #[test] + pub fn test_is_double_vote_true() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(1, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(1, 1, &spec); + + assert_eq!( + slashable_vote_first.is_double_vote(&slashable_vote_second, &spec), + true + ) + } + + #[test] + pub fn test_is_double_vote_false() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(1, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(2, 1, &spec); + + assert_eq!( + slashable_vote_first.is_double_vote(&slashable_vote_second, &spec), + false + ); + } + + #[test] + pub fn test_is_surround_vote_true() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(2, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(1, 2, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + true + ); + } + + #[test] + pub fn test_is_surround_vote_false_source_epoch_fails() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(2, 2, &spec); + let slashable_vote_second = create_slashable_vote_data(1, 1, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + false + ); + } + + #[test] + pub fn test_is_surround_vote_false_target_epoch_fails() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(1, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(2, 2, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + false + ); + } + #[test] pub fn test_ssz_round_trip() { let mut rng = XorShiftRng::from_seed([42; 16]); @@ -91,4 +169,17 @@ mod tests { // TODO: Add further tests // https://github.com/sigp/lighthouse/issues/170 } + + fn create_slashable_vote_data( + slot_factor: u64, + justified_epoch: u64, + spec: &ChainSpec, + ) -> SlashableVoteData { + let mut rng = XorShiftRng::from_seed([42; 16]); + let mut slashable_vote = SlashableVoteData::random_for_test(&mut rng); + + slashable_vote.data.slot = Slot::new(slot_factor * spec.epoch_length); + slashable_vote.data.justified_epoch = Epoch::new(justified_epoch); + slashable_vote + } } From 203f3b37f2b9672d4746a771f93ac6f2c3d04288 Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 15 Feb 2019 12:25:59 +0100 Subject: [PATCH 2/7] adapted import due to renaming of crate slot_epoch_height -> slot_epoch (lighthouse-150) --- eth2/types/src/slashable_vote_data.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index 31433a0b1..170cd635e 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -82,7 +82,7 @@ impl TestRandom for SlashableVoteData { #[cfg(test)] mod tests { use super::*; - use crate::slot_epoch_height::{Epoch, Slot}; + use crate::slot_epoch::{Epoch, Slot}; use crate::spec::ChainSpec; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use ssz::ssz_encode; From 6fa141181b997746cfa7b675b2519040eb27d5f7 Mon Sep 17 00:00:00 2001 From: mjkeating Date: Sun, 17 Feb 2019 09:30:18 -0800 Subject: [PATCH 3/7] Updated TreeHash to spec - added padding --- eth2/types/src/attestation.rs | 14 ++-- eth2/types/src/attestation_data.rs | 22 +++---- .../src/attestation_data_and_custody_bit.rs | 10 +-- eth2/types/src/attester_slashing.rs | 10 +-- eth2/types/src/beacon_block.rs | 20 +++--- eth2/types/src/beacon_block_body.rs | 16 ++--- eth2/types/src/beacon_state.rs | 56 ++++++++-------- eth2/types/src/beacon_state/tests.rs | 4 +- eth2/types/src/casper_slashing.rs | 10 +-- eth2/types/src/crosslink.rs | 10 +-- eth2/types/src/deposit.rs | 12 ++-- eth2/types/src/deposit_data.rs | 12 ++-- eth2/types/src/deposit_input.rs | 12 ++-- eth2/types/src/eth1_data.rs | 10 +-- eth2/types/src/eth1_data_vote.rs | 10 +-- eth2/types/src/exit.rs | 12 ++-- eth2/types/src/fork.rs | 12 ++-- eth2/types/src/pending_attestation.rs | 14 ++-- eth2/types/src/proposal_signed_data.rs | 12 ++-- eth2/types/src/proposer_slashing.rs | 16 ++--- eth2/types/src/shard_reassignment_record.rs | 12 ++-- eth2/types/src/slashable_attestation.rs | 14 ++-- eth2/types/src/slashable_vote_data.rs | 14 ++-- eth2/types/src/slot_epoch_macros.rs | 8 +-- eth2/types/src/validator.rs | 22 ++++--- .../src/validator_registry_delta_block.rs | 16 ++--- eth2/utils/bls/src/aggregate_signature.rs | 2 +- eth2/utils/bls/src/public_key.rs | 2 +- eth2/utils/bls/src/secret_key.rs | 2 +- eth2/utils/bls/src/signature.rs | 2 +- eth2/utils/boolean-bitfield/src/lib.rs | 4 +- eth2/utils/ssz/src/impl_tree_hash.rs | 24 +++---- eth2/utils/ssz/src/tree_hash.rs | 64 +++++++++++++++---- 33 files changed, 262 insertions(+), 218 deletions(-) diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index eb375d490..50e0949e1 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -60,12 +60,12 @@ impl Decodable for Attestation { } impl TreeHash for Attestation { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.aggregation_bitfield.hash_tree_root()); - result.append(&mut self.data.hash_tree_root()); - result.append(&mut self.custody_bitfield.hash_tree_root()); - result.append(&mut self.aggregate_signature.hash_tree_root()); + 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) } } @@ -99,11 +99,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Attestation::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index 702bba416..6f98c557c 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -82,16 +82,16 @@ impl Decodable for AttestationData { } impl TreeHash for AttestationData { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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.hash_tree_root()); - result.append(&mut self.justified_epoch.hash_tree_root()); - result.append(&mut self.justified_block_root.hash_tree_root()); + 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) } } @@ -129,11 +129,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = AttestationData::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/attestation_data_and_custody_bit.rs b/eth2/types/src/attestation_data_and_custody_bit.rs index 4e93dd893..48add3d21 100644 --- a/eth2/types/src/attestation_data_and_custody_bit.rs +++ b/eth2/types/src/attestation_data_and_custody_bit.rs @@ -29,11 +29,11 @@ impl Decodable for AttestationDataAndCustodyBit { } impl TreeHash for AttestationDataAndCustodyBit { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.data.hash_tree_root()); + result.append(&mut self.data.hash_tree_root_internal()); // TODO: add bool ssz - // result.append(custody_bit.hash_tree_root()); + // result.append(custody_bit.hash_tree_root_internal()); ssz::hash(&result) } } @@ -68,11 +68,11 @@ mod test { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = AttestationDataAndCustodyBit::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/attester_slashing.rs b/eth2/types/src/attester_slashing.rs index 0b27d2030..80fa673a7 100644 --- a/eth2/types/src/attester_slashing.rs +++ b/eth2/types/src/attester_slashing.rs @@ -32,10 +32,10 @@ impl Decodable for AttesterSlashing { } impl TreeHash for AttesterSlashing { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.slashable_attestation_1.hash_tree_root()); - result.append(&mut self.slashable_attestation_2.hash_tree_root()); + result.append(&mut self.slashable_attestation_1.hash_tree_root_internal()); + result.append(&mut self.slashable_attestation_2.hash_tree_root_internal()); hash(&result) } } @@ -67,11 +67,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = AttesterSlashing::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index f6977595a..f0661a1c9 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -97,15 +97,15 @@ impl Decodable for BeaconBlock { } impl TreeHash for BeaconBlock { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -142,11 +142,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = BeaconBlock::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index d3a61f7ba..9b25c919a 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -45,13 +45,13 @@ impl Decodable for BeaconBlockBody { } impl TreeHash for BeaconBlockBody { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.proposer_slashings.hash_tree_root()); - result.append(&mut self.attester_slashings.hash_tree_root()); - result.append(&mut self.attestations.hash_tree_root()); - result.append(&mut self.deposits.hash_tree_root()); - result.append(&mut self.exits.hash_tree_root()); + 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) } } @@ -86,11 +86,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = BeaconBlockBody::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 22885adfe..24023d298 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -1020,33 +1020,37 @@ impl Decodable for BeaconState { } impl TreeHash for BeaconState { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.slot.hash_tree_root()); - result.append(&mut self.genesis_time.hash_tree_root()); - result.append(&mut self.fork.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_epoch.hash_tree_root()); - result.append(&mut self.latest_randao_mixes.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_calculation_epoch.hash_tree_root()); - result.append(&mut self.current_calculation_epoch.hash_tree_root()); - result.append(&mut self.previous_epoch_seed.hash_tree_root()); - result.append(&mut self.current_epoch_seed.hash_tree_root()); - result.append(&mut self.previous_justified_epoch.hash_tree_root()); - result.append(&mut self.justified_epoch.hash_tree_root()); - result.append(&mut self.justification_bitfield.hash_tree_root()); - result.append(&mut self.finalized_epoch.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_index_roots.hash_tree_root()); - result.append(&mut self.latest_penalized_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()); + 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) } } diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index e069e462e..2b7c5b539 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -85,11 +85,11 @@ pub fn test_ssz_round_trip() { } #[test] -pub fn test_hash_tree_root() { +pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = BeaconState::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index 0eab069b4..f9dc2f178 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -33,10 +33,10 @@ impl Decodable for CasperSlashing { } impl TreeHash for CasperSlashing { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.slashable_vote_data_1.hash_tree_root()); - result.append(&mut self.slashable_vote_data_2.hash_tree_root()); + 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) } } @@ -68,11 +68,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = CasperSlashing::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index 3cb857ef4..90ee5dc9b 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -43,10 +43,10 @@ impl Decodable for Crosslink { } impl TreeHash for Crosslink { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.epoch.hash_tree_root()); - result.append(&mut self.shard_block_root.hash_tree_root()); + result.append(&mut self.epoch.hash_tree_root_internal()); + result.append(&mut self.shard_block_root.hash_tree_root_internal()); hash(&result) } } @@ -78,11 +78,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Crosslink::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/deposit.rs b/eth2/types/src/deposit.rs index 62349cbc1..eebcdb5d0 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -37,11 +37,11 @@ impl Decodable for Deposit { } impl TreeHash for Deposit { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.branch.hash_tree_root()); - result.append(&mut self.index.hash_tree_root()); - result.append(&mut self.deposit_data.hash_tree_root()); + 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) } } @@ -74,11 +74,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Deposit::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index 5c8c302f4..443c371e3 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -37,11 +37,11 @@ impl Decodable for DepositData { } impl TreeHash for DepositData { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -74,11 +74,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = DepositData::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/deposit_input.rs b/eth2/types/src/deposit_input.rs index fc53baae9..440fcba1c 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -38,11 +38,11 @@ impl Decodable for DepositInput { } impl TreeHash for DepositInput { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -75,11 +75,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = DepositInput::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/eth1_data.rs b/eth2/types/src/eth1_data.rs index 6e9bb7d26..d68e611dd 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -34,10 +34,10 @@ impl Decodable for Eth1Data { } impl TreeHash for Eth1Data { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.deposit_root.hash_tree_root()); - result.append(&mut self.block_hash.hash_tree_root()); + result.append(&mut self.deposit_root.hash_tree_root_internal()); + result.append(&mut self.block_hash.hash_tree_root_internal()); hash(&result) } } @@ -69,11 +69,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Eth1Data::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/eth1_data_vote.rs b/eth2/types/src/eth1_data_vote.rs index 2bfee4d02..a09b0888d 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -34,10 +34,10 @@ impl Decodable for Eth1DataVote { } impl TreeHash for Eth1DataVote { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.eth1_data.hash_tree_root()); - result.append(&mut self.vote_count.hash_tree_root()); + result.append(&mut self.eth1_data.hash_tree_root_internal()); + result.append(&mut self.vote_count.hash_tree_root_internal()); hash(&result) } } @@ -69,11 +69,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Eth1DataVote::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/exit.rs b/eth2/types/src/exit.rs index cd7746919..a1dcd6122 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -37,11 +37,11 @@ impl Decodable for Exit { } impl TreeHash for Exit { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.epoch.hash_tree_root()); - result.append(&mut self.validator_index.hash_tree_root()); - result.append(&mut self.signature.hash_tree_root()); + 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) } } @@ -74,11 +74,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Exit::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index 1c96a34ac..829d7d4a9 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -36,11 +36,11 @@ impl Decodable for Fork { } impl TreeHash for Fork { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.previous_version.hash_tree_root()); - result.append(&mut self.current_version.hash_tree_root()); - result.append(&mut self.epoch.hash_tree_root()); + 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) } } @@ -73,11 +73,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Fork::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index 25ec109d7..b84a63297 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -41,12 +41,12 @@ impl Decodable for PendingAttestation { } impl TreeHash for PendingAttestation { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.aggregation_bitfield.hash_tree_root()); - result.append(&mut self.data.hash_tree_root()); - result.append(&mut self.custody_bitfield.hash_tree_root()); - result.append(&mut self.inclusion_slot.hash_tree_root()); + 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) } } @@ -80,11 +80,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = PendingAttestation::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/proposal_signed_data.rs b/eth2/types/src/proposal_signed_data.rs index c57eb1e2a..3069a7431 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -37,11 +37,11 @@ impl Decodable for ProposalSignedData { } impl TreeHash for ProposalSignedData { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -74,11 +74,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = ProposalSignedData::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index 417d23dbc..c76609335 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -46,13 +46,13 @@ impl Decodable for ProposerSlashing { } impl TreeHash for ProposerSlashing { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -87,11 +87,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = ProposerSlashing::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/shard_reassignment_record.rs b/eth2/types/src/shard_reassignment_record.rs index 61f68ac05..374499a13 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -36,11 +36,11 @@ impl Decodable for ShardReassignmentRecord { } impl TreeHash for ShardReassignmentRecord { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -73,11 +73,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = ShardReassignmentRecord::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/slashable_attestation.rs b/eth2/types/src/slashable_attestation.rs index 6d83ad147..862d93c60 100644 --- a/eth2/types/src/slashable_attestation.rs +++ b/eth2/types/src/slashable_attestation.rs @@ -40,12 +40,12 @@ impl Decodable for SlashableAttestation { } impl TreeHash for SlashableAttestation { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.validator_indices.hash_tree_root()); - result.append(&mut self.data.hash_tree_root()); - result.append(&mut self.custody_bitfield.hash_tree_root()); - result.append(&mut self.aggregate_signature.hash_tree_root()); + 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) } } @@ -79,11 +79,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = SlashableAttestation::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index acffca26d..3eb0e0c6f 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -42,12 +42,12 @@ impl Decodable for SlashableVoteData { } impl TreeHash for SlashableVoteData { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -81,11 +81,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = SlashableVoteData::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/slot_epoch_macros.rs b/eth2/types/src/slot_epoch_macros.rs index 48bc219da..54a8f2ce9 100644 --- a/eth2/types/src/slot_epoch_macros.rs +++ b/eth2/types/src/slot_epoch_macros.rs @@ -224,9 +224,9 @@ macro_rules! impl_ssz { } impl TreeHash for $type { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.0.hash_tree_root()); + result.append(&mut self.0.hash_tree_root_internal()); hash(&result) } } @@ -560,11 +560,11 @@ macro_rules! ssz_tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = $type::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/validator.rs b/eth2/types/src/validator.rs index 047817a86..b832283a0 100644 --- a/eth2/types/src/validator.rs +++ b/eth2/types/src/validator.rs @@ -122,15 +122,17 @@ impl Decodable for Validator { } impl TreeHash for Validator { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = vec![]; - result.append(&mut self.pubkey.hash_tree_root()); - result.append(&mut self.withdrawal_credentials.hash_tree_root()); - result.append(&mut self.activation_epoch.hash_tree_root()); - result.append(&mut self.exit_epoch.hash_tree_root()); - result.append(&mut self.withdrawal_epoch.hash_tree_root()); - result.append(&mut self.penalized_epoch.hash_tree_root()); - result.append(&mut u64::from(status_flag_to_byte(self.status_flags)).hash_tree_root()); + result.append(&mut self.pubkey.hash_tree_root_internal()); + result.append(&mut self.withdrawal_credentials.hash_tree_root_internal()); + result.append(&mut self.activation_epoch.hash_tree_root_internal()); + result.append(&mut self.exit_epoch.hash_tree_root_internal()); + result.append(&mut self.withdrawal_epoch.hash_tree_root_internal()); + result.append(&mut self.penalized_epoch.hash_tree_root_internal()); + result.append( + &mut u64::from(status_flag_to_byte(self.status_flags)).hash_tree_root_internal(), + ); hash(&result) } } @@ -190,11 +192,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = Validator::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs index 3142e0263..13d2e0059 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -59,13 +59,13 @@ impl Decodable for ValidatorRegistryDeltaBlock { } impl TreeHash for ValidatorRegistryDeltaBlock { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { let mut result: Vec = 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()); + 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) } } @@ -100,11 +100,11 @@ mod tests { } #[test] - pub fn test_hash_tree_root() { + pub fn test_hash_tree_root_internal() { let mut rng = XorShiftRng::from_seed([42; 16]); let original = ValidatorRegistryDeltaBlock::random_for_test(&mut rng); - let result = original.hash_tree_root(); + let result = original.hash_tree_root_internal(); assert_eq!(result.len(), 32); // TODO: Add further tests diff --git a/eth2/utils/bls/src/aggregate_signature.rs b/eth2/utils/bls/src/aggregate_signature.rs index 6fed183f0..b606a5ebd 100644 --- a/eth2/utils/bls/src/aggregate_signature.rs +++ b/eth2/utils/bls/src/aggregate_signature.rs @@ -57,7 +57,7 @@ impl Serialize for AggregateSignature { } impl TreeHash for AggregateSignature { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { hash(&self.0.as_bytes()) } } diff --git a/eth2/utils/bls/src/public_key.rs b/eth2/utils/bls/src/public_key.rs index 0c2ad81bb..c7fd526a0 100644 --- a/eth2/utils/bls/src/public_key.rs +++ b/eth2/utils/bls/src/public_key.rs @@ -66,7 +66,7 @@ impl Serialize for PublicKey { } impl TreeHash for PublicKey { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { hash(&self.0.as_bytes()) } } diff --git a/eth2/utils/bls/src/secret_key.rs b/eth2/utils/bls/src/secret_key.rs index 4ff9f8684..f2d54f4ac 100644 --- a/eth2/utils/bls/src/secret_key.rs +++ b/eth2/utils/bls/src/secret_key.rs @@ -41,7 +41,7 @@ impl Decodable for SecretKey { } impl TreeHash for SecretKey { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { self.0.as_bytes().clone() } } diff --git a/eth2/utils/bls/src/signature.rs b/eth2/utils/bls/src/signature.rs index 396e4eab7..511681957 100644 --- a/eth2/utils/bls/src/signature.rs +++ b/eth2/utils/bls/src/signature.rs @@ -61,7 +61,7 @@ impl Decodable for Signature { } impl TreeHash for Signature { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { hash(&self.0.as_bytes()) } } diff --git a/eth2/utils/boolean-bitfield/src/lib.rs b/eth2/utils/boolean-bitfield/src/lib.rs index a0fce1f0a..fb3a78e7a 100644 --- a/eth2/utils/boolean-bitfield/src/lib.rs +++ b/eth2/utils/boolean-bitfield/src/lib.rs @@ -187,8 +187,8 @@ impl Serialize for BooleanBitfield { } impl ssz::TreeHash for BooleanBitfield { - fn hash_tree_root(&self) -> Vec { - self.to_bytes().hash_tree_root() + fn hash_tree_root_internal(&self) -> Vec { + self.to_bytes().hash_tree_root_internal() } } diff --git a/eth2/utils/ssz/src/impl_tree_hash.rs b/eth2/utils/ssz/src/impl_tree_hash.rs index 578977eec..7c3dae596 100644 --- a/eth2/utils/ssz/src/impl_tree_hash.rs +++ b/eth2/utils/ssz/src/impl_tree_hash.rs @@ -3,49 +3,49 @@ use super::{merkle_hash, ssz_encode, TreeHash}; use hashing::hash; impl TreeHash for u8 { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) } } impl TreeHash for u16 { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) } } impl TreeHash for u32 { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) } } impl TreeHash for u64 { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) } } impl TreeHash for usize { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) } } impl TreeHash for Address { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) } } impl TreeHash for H256 { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { ssz_encode(self) } } impl TreeHash for [u8] { - fn hash_tree_root(&self) -> Vec { + fn hash_tree_root_internal(&self) -> Vec { if self.len() > 32 { return hash(&self); } @@ -57,12 +57,12 @@ impl TreeHash for Vec where T: TreeHash, { - /// Returns the merkle_hash of a list of hash_tree_root values created + /// Returns the merkle_hash of a list of hash_tree_root_internal values created /// from the given list. /// Note: A byte vector, Vec, must be converted to a slice (as_slice()) /// to be handled properly (i.e. hashed) as byte array. - fn hash_tree_root(&self) -> Vec { - let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect(); + fn hash_tree_root_internal(&self) -> Vec { + let mut tree_hashes = self.iter().map(|x| x.hash_tree_root_internal()).collect(); merkle_hash(&mut tree_hashes) } } @@ -73,7 +73,7 @@ mod tests { #[test] fn test_impl_tree_hash_vec() { - let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root(); + let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root_internal(); assert_eq!(result.len(), 32); } } diff --git a/eth2/utils/ssz/src/tree_hash.rs b/eth2/utils/ssz/src/tree_hash.rs index a9ab0f467..3f190e32e 100644 --- a/eth2/utils/ssz/src/tree_hash.rs +++ b/eth2/utils/ssz/src/tree_hash.rs @@ -4,7 +4,14 @@ const SSZ_CHUNK_SIZE: usize = 128; const HASHSIZE: usize = 32; pub trait TreeHash { - fn hash_tree_root(&self) -> Vec; + 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); + } + result + } } /// Returns a 32 byte hash of 'list' - a vector of byte vectors. @@ -14,7 +21,8 @@ pub fn merkle_hash(list: &mut Vec>) -> Vec { 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(); + let mut datalen = list.len().to_le_bytes().to_vec(); + zpad(&mut datalen, 32); // Tree-hash while chunkz.len() > HASHSIZE { @@ -36,33 +44,63 @@ pub fn merkle_hash(list: &mut Vec>) -> Vec { chunkz = new_chunkz; } - chunkz.append(&mut datalen.to_vec()); + chunkz.append(&mut datalen); hash(&chunkz) } fn list_to_blob(list: &mut Vec>) -> (usize, Vec) { - let chunk_size = if list.is_empty() { + let chunk_size = if list.is_empty() || list[0].len() < SSZ_CHUNK_SIZE { 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(); + let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len(); + let chunk_count = list.len() / items_per_chunk; + + let mut chunkz = Vec::new(); if list.is_empty() { // handle and empty list - data.append(&mut vec![0; SSZ_CHUNK_SIZE]); - } else { + chunkz.append(&mut vec![0; SSZ_CHUNK_SIZE]); + } else if list[0].len() <= SSZ_CHUNK_SIZE { // just create a blob here; we'll divide into // chunked slices when we merklize - data.reserve(list[0].len() * list.len()); + let mut chunk = Vec::with_capacity(chunk_size); + let mut item_count_in_chunk = 0; + chunkz.reserve(chunk_count * chunk_size); for item in list.iter_mut() { - data.append(item); + item_count_in_chunk += 1; + chunk.append(item); + + // completed chunk? + if item_count_in_chunk == items_per_chunk { + zpad(&mut chunk, chunk_size); + chunkz.append(&mut chunk); + item_count_in_chunk = 0; + } + } + + // left-over uncompleted chunk? + if item_count_in_chunk != 0 { + zpad(&mut chunk, chunk_size); + chunkz.append(&mut chunk); + } + } else { + // chunks larger than SSZ_CHUNK_SIZE + chunkz.reserve(chunk_count * chunk_size); + for item in list.iter_mut() { + chunkz.append(item); } } - (chunk_size, data) + + (chunk_size, chunkz) +} + +/// right pads with zeros making 'bytes' 'size' in length +fn zpad(bytes: &mut Vec, size: usize) { + if bytes.len() < size { + bytes.resize(size, 0); + } } #[cfg(test)] From f88155625c709efd6dc42c7faae537e6bff486c6 Mon Sep 17 00:00:00 2001 From: thojest Date: Mon, 18 Feb 2019 12:12:01 +0100 Subject: [PATCH 4/7] added comment to indicate highest spec version of implemented functions; added realistic test scenario for is_surround_vote (lighthouse-150) --- eth2/types/src/slashable_vote_data.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index 170cd635e..ff9e8b658 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -1,5 +1,5 @@ use super::AttestationData; -use crate::spec::ChainSpec; +use crate::chain_spec::ChainSpec; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; @@ -15,10 +15,16 @@ pub struct SlashableVoteData { } impl SlashableVoteData { + /// Check if ``attestation_data_1`` and ``attestation_data_2`` have the same target. + /// + /// Spec v0.3.0 pub fn is_double_vote(&self, other: &SlashableVoteData, spec: &ChainSpec) -> bool { self.data.slot.epoch(spec.epoch_length) == other.data.slot.epoch(spec.epoch_length) } + /// Check if ``attestation_data_1`` surrounds ``attestation_data_2``. + /// + /// Spec v0.3.0 pub fn is_surround_vote(&self, other: &SlashableVoteData, spec: &ChainSpec) -> bool { let source_epoch_1 = self.data.justified_epoch; let source_epoch_2 = other.data.justified_epoch; @@ -82,8 +88,8 @@ impl TestRandom for SlashableVoteData { #[cfg(test)] mod tests { use super::*; + use crate::chain_spec::ChainSpec; use crate::slot_epoch::{Epoch, Slot}; - use crate::spec::ChainSpec; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use ssz::ssz_encode; @@ -123,6 +129,18 @@ mod tests { ); } + #[test] + pub fn test_is_surround_vote_true_realistic() { + let spec = ChainSpec::foundation(); + let slashable_vote_first = create_slashable_vote_data(4, 1, &spec); + let slashable_vote_second = create_slashable_vote_data(3, 2, &spec); + + assert_eq!( + slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec), + true + ); + } + #[test] pub fn test_is_surround_vote_false_source_epoch_fails() { let spec = ChainSpec::foundation(); From 1bdce182a94fa45efafd8ad365924613823fc372 Mon Sep 17 00:00:00 2001 From: Feng94 <8751189+Feng94@users.noreply.github.com> Date: Tue, 19 Feb 2019 00:03:35 +1100 Subject: [PATCH 5/7] Rename block_producer crate to block_proposer and change references inside it to block_proposer --- eth2/{block_producer => block_proposer}/Cargo.toml | 2 +- eth2/{block_producer => block_proposer}/src/lib.rs | 12 ++++++------ .../src/test_utils/epoch_map.rs | 0 .../src/test_utils/local_signer.rs | 0 .../src/test_utils/mod.rs | 0 .../src/test_utils/simulated_beacon_node.rs | 0 .../{block_producer => block_proposer}/src/traits.rs | 0 7 files changed, 7 insertions(+), 7 deletions(-) rename eth2/{block_producer => block_proposer}/Cargo.toml (91%) rename eth2/{block_producer => block_proposer}/src/lib.rs (97%) rename eth2/{block_producer => block_proposer}/src/test_utils/epoch_map.rs (100%) rename eth2/{block_producer => block_proposer}/src/test_utils/local_signer.rs (100%) rename eth2/{block_producer => block_proposer}/src/test_utils/mod.rs (100%) rename eth2/{block_producer => block_proposer}/src/test_utils/simulated_beacon_node.rs (100%) rename eth2/{block_producer => block_proposer}/src/traits.rs (100%) diff --git a/eth2/block_producer/Cargo.toml b/eth2/block_proposer/Cargo.toml similarity index 91% rename from eth2/block_producer/Cargo.toml rename to eth2/block_proposer/Cargo.toml index 15d1343cc..81f1ccc28 100644 --- a/eth2/block_producer/Cargo.toml +++ b/eth2/block_proposer/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "block_producer" +name = "block_proposer" version = "0.1.0" authors = ["Paul Hauner "] edition = "2018" diff --git a/eth2/block_producer/src/lib.rs b/eth2/block_proposer/src/lib.rs similarity index 97% rename from eth2/block_producer/src/lib.rs rename to eth2/block_proposer/src/lib.rs index 7b15eb4e9..cf71edd99 100644 --- a/eth2/block_producer/src/lib.rs +++ b/eth2/block_proposer/src/lib.rs @@ -236,7 +236,7 @@ mod tests { epoch_map.map.insert(produce_epoch, produce_slot); let epoch_map = Arc::new(epoch_map); - let mut block_producer = BlockProducer::new( + let mut block_proposer = BlockProducer::new( spec.clone(), epoch_map.clone(), slot_clock.clone(), @@ -251,28 +251,28 @@ mod tests { // One slot before production slot... slot_clock.set_slot(produce_slot.as_u64() - 1); assert_eq!( - block_producer.poll(), + block_proposer.poll(), Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1)) ); // On the produce slot... slot_clock.set_slot(produce_slot.as_u64()); assert_eq!( - block_producer.poll(), + block_proposer.poll(), Ok(PollOutcome::BlockProduced(produce_slot.into())) ); // Trying the same produce slot again... slot_clock.set_slot(produce_slot.as_u64()); assert_eq!( - block_producer.poll(), + block_proposer.poll(), Ok(PollOutcome::SlotAlreadyProcessed(produce_slot)) ); // One slot after the produce slot... slot_clock.set_slot(produce_slot.as_u64() + 1); assert_eq!( - block_producer.poll(), + block_proposer.poll(), Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1)) ); @@ -280,7 +280,7 @@ mod tests { let slot = (produce_epoch.as_u64() + 1) * spec.epoch_length; slot_clock.set_slot(slot); assert_eq!( - block_producer.poll(), + block_proposer.poll(), Ok(PollOutcome::ProducerDutiesUnknown(Slot::new(slot))) ); } diff --git a/eth2/block_producer/src/test_utils/epoch_map.rs b/eth2/block_proposer/src/test_utils/epoch_map.rs similarity index 100% rename from eth2/block_producer/src/test_utils/epoch_map.rs rename to eth2/block_proposer/src/test_utils/epoch_map.rs diff --git a/eth2/block_producer/src/test_utils/local_signer.rs b/eth2/block_proposer/src/test_utils/local_signer.rs similarity index 100% rename from eth2/block_producer/src/test_utils/local_signer.rs rename to eth2/block_proposer/src/test_utils/local_signer.rs diff --git a/eth2/block_producer/src/test_utils/mod.rs b/eth2/block_proposer/src/test_utils/mod.rs similarity index 100% rename from eth2/block_producer/src/test_utils/mod.rs rename to eth2/block_proposer/src/test_utils/mod.rs diff --git a/eth2/block_producer/src/test_utils/simulated_beacon_node.rs b/eth2/block_proposer/src/test_utils/simulated_beacon_node.rs similarity index 100% rename from eth2/block_producer/src/test_utils/simulated_beacon_node.rs rename to eth2/block_proposer/src/test_utils/simulated_beacon_node.rs diff --git a/eth2/block_producer/src/traits.rs b/eth2/block_proposer/src/traits.rs similarity index 100% rename from eth2/block_producer/src/traits.rs rename to eth2/block_proposer/src/traits.rs From 7ed606eca126567845eea0c333f1254a8be5a3e0 Mon Sep 17 00:00:00 2001 From: Feng94 <8751189+Feng94@users.noreply.github.com> Date: Tue, 19 Feb 2019 00:21:23 +1100 Subject: [PATCH 6/7] Modify block_producer references in other crates where needed for compilation --- Cargo.toml | 2 +- beacon_node/beacon_chain/Cargo.toml | 2 +- beacon_node/beacon_chain/test_harness/Cargo.toml | 2 +- .../test_harness/src/validator_harness/direct_beacon_node.rs | 2 +- .../test_harness/src/validator_harness/direct_duties.rs | 2 +- .../test_harness/src/validator_harness/local_signer.rs | 2 +- .../beacon_chain/test_harness/src/validator_harness/mod.rs | 4 ++-- validator_client/Cargo.toml | 2 +- .../src/block_producer_service/beacon_block_grpc_client.rs | 2 +- validator_client/src/block_producer_service/mod.rs | 2 +- validator_client/src/duties/epoch_duties.rs | 2 +- validator_client/src/main.rs | 2 +- 12 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5ab0ba847..302c04154 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ "eth2/attester", - "eth2/block_producer", + "eth2/block_proposer", "eth2/fork_choice", "eth2/state_processing", "eth2/types", diff --git a/beacon_node/beacon_chain/Cargo.toml b/beacon_node/beacon_chain/Cargo.toml index 36d7b3721..4ce894477 100644 --- a/beacon_node/beacon_chain/Cargo.toml +++ b/beacon_node/beacon_chain/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Paul Hauner "] edition = "2018" [dependencies] -block_producer = { path = "../../eth2/block_producer" } +block_proposer = { path = "../../eth2/block_proposer" } bls = { path = "../../eth2/utils/bls" } boolean-bitfield = { path = "../../eth2/utils/boolean-bitfield" } db = { path = "../db" } diff --git a/beacon_node/beacon_chain/test_harness/Cargo.toml b/beacon_node/beacon_chain/test_harness/Cargo.toml index bb335c152..77b52ccf6 100644 --- a/beacon_node/beacon_chain/test_harness/Cargo.toml +++ b/beacon_node/beacon_chain/test_harness/Cargo.toml @@ -14,7 +14,7 @@ criterion = "0.2" [dependencies] attester = { path = "../../../eth2/attester" } beacon_chain = { path = "../../beacon_chain" } -block_producer = { path = "../../../eth2/block_producer" } +block_proposer = { path = "../../../eth2/block_proposer" } bls = { path = "../../../eth2/utils/bls" } boolean-bitfield = { path = "../../../eth2/utils/boolean-bitfield" } db = { path = "../../db" } diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs index be71b9abd..06d3e7c72 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_beacon_node.rs @@ -3,7 +3,7 @@ use attester::{ PublishOutcome as AttestationPublishOutcome, }; use beacon_chain::BeaconChain; -use block_producer::{ +use block_proposer::{ BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError, PublishOutcome as BlockPublishOutcome, }; diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_duties.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_duties.rs index 66b9d650c..5bed59531 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_duties.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/direct_duties.rs @@ -2,7 +2,7 @@ use attester::{ DutiesReader as AttesterDutiesReader, DutiesReaderError as AttesterDutiesReaderError, }; use beacon_chain::BeaconChain; -use block_producer::{ +use block_proposer::{ DutiesReader as ProducerDutiesReader, DutiesReaderError as ProducerDutiesReaderError, }; use db::ClientDB; diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/local_signer.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/local_signer.rs index 8e901b057..aa46a1c9a 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/local_signer.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/local_signer.rs @@ -1,5 +1,5 @@ use attester::Signer as AttesterSigner; -use block_producer::Signer as BlockProposerSigner; +use block_proposer::Signer as BlockProposerSigner; use std::sync::RwLock; use types::{Keypair, Signature}; diff --git a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs index 3df32fa64..f48309541 100644 --- a/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs +++ b/beacon_node/beacon_chain/test_harness/src/validator_harness/mod.rs @@ -5,8 +5,8 @@ mod local_signer; use attester::PollOutcome as AttestationPollOutcome; use attester::{Attester, Error as AttestationPollError}; use beacon_chain::BeaconChain; -use block_producer::PollOutcome as BlockPollOutcome; -use block_producer::{BlockProducer, Error as BlockPollError}; +use block_proposer::PollOutcome as BlockPollOutcome; +use block_proposer::{BlockProducer, Error as BlockPollError}; use db::MemoryDB; use direct_beacon_node::DirectBeaconNode; use direct_duties::DirectDuties; diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index 8ab515e15..f76772f28 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Paul Hauner "] edition = "2018" [dependencies] -block_producer = { path = "../eth2/block_producer" } +block_proposer = { path = "../eth2/block_proposer" } bls = { path = "../eth2/utils/bls" } clap = "2.32.0" dirs = "1.0.3" diff --git a/validator_client/src/block_producer_service/beacon_block_grpc_client.rs b/validator_client/src/block_producer_service/beacon_block_grpc_client.rs index 39ef7fcdc..6bf3005d4 100644 --- a/validator_client/src/block_producer_service/beacon_block_grpc_client.rs +++ b/validator_client/src/block_producer_service/beacon_block_grpc_client.rs @@ -1,4 +1,4 @@ -use block_producer::{BeaconNode, BeaconNodeError, PublishOutcome}; +use block_proposer::{BeaconNode, BeaconNodeError, PublishOutcome}; use protos::services::{ BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, }; diff --git a/validator_client/src/block_producer_service/mod.rs b/validator_client/src/block_producer_service/mod.rs index 82c3f2537..bd1e691cb 100644 --- a/validator_client/src/block_producer_service/mod.rs +++ b/validator_client/src/block_producer_service/mod.rs @@ -1,7 +1,7 @@ mod beacon_block_grpc_client; // mod block_producer_service; -use block_producer::{ +use block_proposer::{ BeaconNode, BlockProducer, DutiesReader, PollOutcome as BlockProducerPollOutcome, Signer, }; use slog::{error, info, warn, Logger}; diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index b555eee28..54a882f8d 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -1,4 +1,4 @@ -use block_producer::{DutiesReader, DutiesReaderError}; +use block_proposer::{DutiesReader, DutiesReaderError}; use std::collections::HashMap; use std::sync::RwLock; use types::{Epoch, Slot}; diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 98be9159a..c835300b5 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -1,7 +1,7 @@ use self::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use self::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; use crate::config::ClientConfig; -use block_producer::{test_utils::LocalSigner, BlockProducer}; +use block_proposer::{test_utils::LocalSigner, BlockProducer}; use bls::Keypair; use clap::{App, Arg}; use grpcio::{ChannelBuilder, EnvBuilder}; From cd676d5621069050d6ceebef3c562ee46fd18de4 Mon Sep 17 00:00:00 2001 From: mjkeating Date: Mon, 18 Feb 2019 11:55:44 -0800 Subject: [PATCH 7/7] fixed a bug in TreeHash.rs/list_to_blob --- eth2/utils/ssz/src/tree_hash.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/eth2/utils/ssz/src/tree_hash.rs b/eth2/utils/ssz/src/tree_hash.rs index 3f190e32e..bb05f01db 100644 --- a/eth2/utils/ssz/src/tree_hash.rs +++ b/eth2/utils/ssz/src/tree_hash.rs @@ -55,8 +55,13 @@ fn list_to_blob(list: &mut Vec>) -> (usize, Vec) { list[0].len() }; - let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len(); - let chunk_count = list.len() / items_per_chunk; + let (items_per_chunk, chunk_count) = if list.is_empty() { + (1, 1) + } else { + let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len(); + let chunk_count = list.len() / items_per_chunk; + (items_per_chunk, chunk_count) + }; let mut chunkz = Vec::new(); if list.is_empty() {