Merge pull request #290 from mjkeating/tree_hash_spec_update
Updated TreeHash logic as per revised spec
This commit is contained in:
commit
123650efbb
@ -35,11 +35,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Attestation::random_for_test(&mut rng);
|
let original = Attestation::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -53,11 +53,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = AttestationData::random_for_test(&mut rng);
|
let original = AttestationData::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -42,11 +42,11 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = AttestationDataAndCustodyBit::random_for_test(&mut rng);
|
let original = AttestationDataAndCustodyBit::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -31,11 +31,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = AttesterSlashing::random_for_test(&mut rng);
|
let original = AttesterSlashing::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -85,11 +85,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = BeaconBlock::random_for_test(&mut rng);
|
let original = BeaconBlock::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -36,11 +36,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = BeaconBlockBody::random_for_test(&mut rng);
|
let original = BeaconBlockBody::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -1195,42 +1195,34 @@ impl Decodable for BeaconState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for BeaconState {
|
impl TreeHash for BeaconState {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
let mut result: Vec<u8> = vec![];
|
let mut result: Vec<u8> = vec![];
|
||||||
result.append(&mut self.slot.hash_tree_root_internal());
|
result.append(&mut self.slot.hash_tree_root());
|
||||||
result.append(&mut self.genesis_time.hash_tree_root_internal());
|
result.append(&mut self.genesis_time.hash_tree_root());
|
||||||
result.append(&mut self.fork.hash_tree_root_internal());
|
result.append(&mut self.fork.hash_tree_root());
|
||||||
result.append(&mut self.validator_registry.hash_tree_root_internal());
|
result.append(&mut self.validator_registry.hash_tree_root());
|
||||||
result.append(&mut self.validator_balances.hash_tree_root_internal());
|
result.append(&mut self.validator_balances.hash_tree_root());
|
||||||
result.append(
|
result.append(&mut self.validator_registry_update_epoch.hash_tree_root());
|
||||||
&mut self
|
result.append(&mut self.latest_randao_mixes.hash_tree_root());
|
||||||
.validator_registry_update_epoch
|
result.append(&mut self.previous_shuffling_start_shard.hash_tree_root());
|
||||||
.hash_tree_root_internal(),
|
result.append(&mut self.current_shuffling_start_shard.hash_tree_root());
|
||||||
);
|
result.append(&mut self.previous_shuffling_epoch.hash_tree_root());
|
||||||
result.append(&mut self.latest_randao_mixes.hash_tree_root_internal());
|
result.append(&mut self.current_shuffling_epoch.hash_tree_root());
|
||||||
result.append(
|
result.append(&mut self.previous_shuffling_seed.hash_tree_root());
|
||||||
&mut self
|
result.append(&mut self.current_shuffling_seed.hash_tree_root());
|
||||||
.previous_shuffling_start_shard
|
result.append(&mut self.previous_justified_epoch.hash_tree_root());
|
||||||
.hash_tree_root_internal(),
|
result.append(&mut self.justified_epoch.hash_tree_root());
|
||||||
);
|
result.append(&mut self.justification_bitfield.hash_tree_root());
|
||||||
result.append(&mut self.current_shuffling_start_shard.hash_tree_root_internal());
|
result.append(&mut self.finalized_epoch.hash_tree_root());
|
||||||
result.append(&mut self.previous_shuffling_epoch.hash_tree_root_internal());
|
result.append(&mut self.latest_crosslinks.hash_tree_root());
|
||||||
result.append(&mut self.current_shuffling_epoch.hash_tree_root_internal());
|
result.append(&mut self.latest_block_roots.hash_tree_root());
|
||||||
result.append(&mut self.previous_shuffling_seed.hash_tree_root_internal());
|
result.append(&mut self.latest_active_index_roots.hash_tree_root());
|
||||||
result.append(&mut self.current_shuffling_seed.hash_tree_root_internal());
|
result.append(&mut self.latest_slashed_balances.hash_tree_root());
|
||||||
result.append(&mut self.previous_justified_epoch.hash_tree_root_internal());
|
result.append(&mut self.latest_attestations.hash_tree_root());
|
||||||
result.append(&mut self.justified_epoch.hash_tree_root_internal());
|
result.append(&mut self.batched_block_roots.hash_tree_root());
|
||||||
result.append(&mut self.justification_bitfield.hash_tree_root_internal());
|
result.append(&mut self.latest_eth1_data.hash_tree_root());
|
||||||
result.append(&mut self.finalized_epoch.hash_tree_root_internal());
|
result.append(&mut self.eth1_data_votes.hash_tree_root());
|
||||||
result.append(&mut self.latest_crosslinks.hash_tree_root_internal());
|
result.append(&mut self.deposit_index.hash_tree_root());
|
||||||
result.append(&mut self.latest_block_roots.hash_tree_root_internal());
|
|
||||||
result.append(&mut self.latest_active_index_roots.hash_tree_root_internal());
|
|
||||||
result.append(&mut self.latest_slashed_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());
|
|
||||||
result.append(&mut self.deposit_index.hash_tree_root_internal());
|
|
||||||
hash(&result)
|
hash(&result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,11 +63,11 @@ pub fn test_ssz_round_trip() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = BeaconState::random_for_test(&mut rng);
|
let original = BeaconState::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -34,11 +34,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Crosslink::random_for_test(&mut rng);
|
let original = Crosslink::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -33,11 +33,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Deposit::random_for_test(&mut rng);
|
let original = Deposit::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -33,11 +33,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = DepositData::random_for_test(&mut rng);
|
let original = DepositData::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -81,11 +81,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = DepositInput::random_for_test(&mut rng);
|
let original = DepositInput::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -32,11 +32,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Eth1Data::random_for_test(&mut rng);
|
let original = Eth1Data::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -32,11 +32,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Eth1DataVote::random_for_test(&mut rng);
|
let original = Eth1DataVote::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -44,11 +44,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Fork::random_for_test(&mut rng);
|
let original = Fork::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -34,11 +34,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = PendingAttestation::random_for_test(&mut rng);
|
let original = PendingAttestation::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -37,11 +37,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Proposal::random_for_test(&mut rng);
|
let original = Proposal::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -33,11 +33,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = ProposerSlashing::random_for_test(&mut rng);
|
let original = ProposerSlashing::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -29,11 +29,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = ShardReassignmentRecord::random_for_test(&mut rng);
|
let original = ShardReassignmentRecord::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -132,11 +132,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = SlashableAttestation::random_for_test(&mut rng);
|
let original = SlashableAttestation::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -207,9 +207,9 @@ macro_rules! impl_ssz {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for $type {
|
impl TreeHash for $type {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
let mut result: Vec<u8> = vec![];
|
let mut result: Vec<u8> = vec![];
|
||||||
result.append(&mut self.0.hash_tree_root_internal());
|
result.append(&mut self.0.hash_tree_root());
|
||||||
hash(&result)
|
hash(&result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -543,11 +543,11 @@ macro_rules! ssz_tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = $type::random_for_test(&mut rng);
|
let original = $type::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -17,14 +17,14 @@ macro_rules! ssz_tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||||
use ssz::TreeHash;
|
use ssz::TreeHash;
|
||||||
|
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = $type::random_for_test(&mut rng);
|
let original = $type::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -39,11 +39,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Transfer::random_for_test(&mut rng);
|
let original = Transfer::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -91,11 +91,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = Validator::random_for_test(&mut rng);
|
let original = Validator::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -34,11 +34,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_hash_tree_root_internal() {
|
pub fn test_hash_tree_root() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
let original = VoluntaryExit::random_for_test(&mut rng);
|
let original = VoluntaryExit::random_for_test(&mut rng);
|
||||||
|
|
||||||
let result = original.hash_tree_root_internal();
|
let result = original.hash_tree_root();
|
||||||
|
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
// TODO: Add further tests
|
// TODO: Add further tests
|
||||||
|
@ -87,7 +87,7 @@ impl Serialize for AggregateSignature {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for AggregateSignature {
|
impl TreeHash for AggregateSignature {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
hash(&self.0.as_bytes())
|
hash(&self.0.as_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ impl<'de> Deserialize<'de> for PublicKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for PublicKey {
|
impl TreeHash for PublicKey {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
hash(&self.0.as_bytes())
|
hash(&self.0.as_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ impl<'de> Deserialize<'de> for SecretKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for SecretKey {
|
impl TreeHash for SecretKey {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
self.0.as_bytes().clone()
|
self.0.as_bytes().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ impl Decodable for Signature {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for Signature {
|
impl TreeHash for Signature {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
hash(&self.0.as_bytes())
|
hash(&self.0.as_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,8 +187,8 @@ impl Serialize for BooleanBitfield {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ssz::TreeHash for BooleanBitfield {
|
impl ssz::TreeHash for BooleanBitfield {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
self.to_bytes().hash_tree_root_internal()
|
self.to_bytes().hash_tree_root()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,55 +3,55 @@ use super::{merkle_hash, ssz_encode, TreeHash};
|
|||||||
use hashing::hash;
|
use hashing::hash;
|
||||||
|
|
||||||
impl TreeHash for u8 {
|
impl TreeHash for u8 {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for u16 {
|
impl TreeHash for u16 {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for u32 {
|
impl TreeHash for u32 {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for u64 {
|
impl TreeHash for u64 {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for usize {
|
impl TreeHash for usize {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for bool {
|
impl TreeHash for bool {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for Address {
|
impl TreeHash for Address {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for H256 {
|
impl TreeHash for H256 {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
ssz_encode(self)
|
ssz_encode(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TreeHash for [u8] {
|
impl TreeHash for [u8] {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
if self.len() > 32 {
|
if self.len() > 32 {
|
||||||
return hash(&self);
|
return hash(&self);
|
||||||
}
|
}
|
||||||
@ -63,12 +63,12 @@ impl<T> TreeHash for Vec<T>
|
|||||||
where
|
where
|
||||||
T: TreeHash,
|
T: TreeHash,
|
||||||
{
|
{
|
||||||
/// Returns the merkle_hash of a list of hash_tree_root_internal values created
|
/// Returns the merkle_hash of a list of hash_tree_root values created
|
||||||
/// from the given list.
|
/// from the given list.
|
||||||
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
|
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
|
||||||
/// to be handled properly (i.e. hashed) as byte array.
|
/// to be handled properly (i.e. hashed) as byte array.
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root_internal()).collect();
|
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect();
|
||||||
merkle_hash(&mut tree_hashes)
|
merkle_hash(&mut tree_hashes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,7 +79,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_impl_tree_hash_vec() {
|
fn test_impl_tree_hash_vec() {
|
||||||
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root_internal();
|
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root();
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,44 +1,31 @@
|
|||||||
use hashing::hash;
|
use hashing::hash;
|
||||||
|
|
||||||
const SSZ_CHUNK_SIZE: usize = 128;
|
const BYTES_PER_CHUNK: usize = 32;
|
||||||
const HASHSIZE: usize = 32;
|
const HASHSIZE: usize = 32;
|
||||||
|
|
||||||
pub trait TreeHash {
|
pub trait TreeHash {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8>;
|
fn hash_tree_root(&self) -> Vec<u8>;
|
||||||
fn hash_tree_root(&self) -> Vec<u8> {
|
|
||||||
let mut result = self.hash_tree_root_internal();
|
|
||||||
zpad(&mut result, HASHSIZE);
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
|
/// Returns a 32 byte hash of 'list' - a vector of byte vectors.
|
||||||
/// Note that this will consume 'list'.
|
/// Note that this will consume 'list'.
|
||||||
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
||||||
// flatten list
|
// flatten list
|
||||||
let (mut chunk_size, mut chunkz) = list_to_blob(list);
|
let mut chunkz = list_to_blob(list);
|
||||||
|
|
||||||
// get data_len as bytes. It will hashed will the merkle root
|
// get data_len as bytes. It will hashed will the merkle root
|
||||||
let mut datalen = list.len().to_le_bytes().to_vec();
|
let mut datalen = list.len().to_le_bytes().to_vec();
|
||||||
zpad(&mut datalen, 32);
|
zpad(&mut datalen, 32);
|
||||||
|
|
||||||
// Tree-hash
|
// merklelize
|
||||||
while chunkz.len() > HASHSIZE {
|
while chunkz.len() > HASHSIZE {
|
||||||
let mut new_chunkz: Vec<u8> = Vec::new();
|
let mut new_chunkz: Vec<u8> = Vec::new();
|
||||||
|
|
||||||
for two_chunks in chunkz.chunks(chunk_size * 2) {
|
for two_chunks in chunkz.chunks(BYTES_PER_CHUNK * 2) {
|
||||||
if two_chunks.len() == chunk_size {
|
// Hash two chuncks together
|
||||||
// Odd number of chunks
|
new_chunkz.append(&mut hash(two_chunks));
|
||||||
let mut c = two_chunks.to_vec();
|
|
||||||
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
|
||||||
new_chunkz.append(&mut hash(&c));
|
|
||||||
} else {
|
|
||||||
// Hash two chuncks together
|
|
||||||
new_chunkz.append(&mut hash(two_chunks));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chunk_size = HASHSIZE;
|
|
||||||
chunkz = new_chunkz;
|
chunkz = new_chunkz;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,17 +33,13 @@ pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
|||||||
hash(&chunkz)
|
hash(&chunkz)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
||||||
let chunk_size = if list.is_empty() || list[0].len() < SSZ_CHUNK_SIZE {
|
// pack - fit as many many items per chunk as we can and then
|
||||||
SSZ_CHUNK_SIZE
|
// right pad to BYTES_PER_CHUNCK
|
||||||
} else {
|
|
||||||
list[0].len()
|
|
||||||
};
|
|
||||||
|
|
||||||
let (items_per_chunk, chunk_count) = if list.is_empty() {
|
let (items_per_chunk, chunk_count) = if list.is_empty() {
|
||||||
(1, 1)
|
(1, 1)
|
||||||
} else {
|
} else {
|
||||||
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
|
let items_per_chunk = BYTES_PER_CHUNK / list[0].len();
|
||||||
let chunk_count = list.len() / items_per_chunk;
|
let chunk_count = list.len() / items_per_chunk;
|
||||||
(items_per_chunk, chunk_count)
|
(items_per_chunk, chunk_count)
|
||||||
};
|
};
|
||||||
@ -64,20 +47,20 @@ fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
|||||||
let mut chunkz = Vec::new();
|
let mut chunkz = Vec::new();
|
||||||
if list.is_empty() {
|
if list.is_empty() {
|
||||||
// handle and empty list
|
// handle and empty list
|
||||||
chunkz.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
chunkz.append(&mut vec![0; BYTES_PER_CHUNK * 2]);
|
||||||
} else if list[0].len() <= SSZ_CHUNK_SIZE {
|
} else if list[0].len() <= BYTES_PER_CHUNK {
|
||||||
// just create a blob here; we'll divide into
|
// just create a blob here; we'll divide into
|
||||||
// chunked slices when we merklize
|
// chunked slices when we merklize
|
||||||
let mut chunk = Vec::with_capacity(chunk_size);
|
let mut chunk = Vec::with_capacity(BYTES_PER_CHUNK);
|
||||||
let mut item_count_in_chunk = 0;
|
let mut item_count_in_chunk = 0;
|
||||||
chunkz.reserve(chunk_count * chunk_size);
|
chunkz.reserve(chunk_count * BYTES_PER_CHUNK);
|
||||||
for item in list.iter_mut() {
|
for item in list.iter_mut() {
|
||||||
item_count_in_chunk += 1;
|
item_count_in_chunk += 1;
|
||||||
chunk.append(item);
|
chunk.append(item);
|
||||||
|
|
||||||
// completed chunk?
|
// completed chunk?
|
||||||
if item_count_in_chunk == items_per_chunk {
|
if item_count_in_chunk == items_per_chunk {
|
||||||
zpad(&mut chunk, chunk_size);
|
zpad(&mut chunk, BYTES_PER_CHUNK);
|
||||||
chunkz.append(&mut chunk);
|
chunkz.append(&mut chunk);
|
||||||
item_count_in_chunk = 0;
|
item_count_in_chunk = 0;
|
||||||
}
|
}
|
||||||
@ -85,18 +68,18 @@ fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
|||||||
|
|
||||||
// left-over uncompleted chunk?
|
// left-over uncompleted chunk?
|
||||||
if item_count_in_chunk != 0 {
|
if item_count_in_chunk != 0 {
|
||||||
zpad(&mut chunk, chunk_size);
|
zpad(&mut chunk, BYTES_PER_CHUNK);
|
||||||
chunkz.append(&mut chunk);
|
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, chunkz)
|
// extend the number of chunks to a power of two if necessary
|
||||||
|
if !chunk_count.is_power_of_two() {
|
||||||
|
let zero_chunks_count = chunk_count.next_power_of_two() - chunk_count;
|
||||||
|
chunkz.append(&mut vec![0; zero_chunks_count * BYTES_PER_CHUNK]);
|
||||||
|
}
|
||||||
|
|
||||||
|
chunkz
|
||||||
}
|
}
|
||||||
|
|
||||||
/// right pads with zeros making 'bytes' 'size' in length
|
/// right pads with zeros making 'bytes' 'size' in length
|
||||||
@ -112,9 +95,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_merkle_hash() {
|
fn test_merkle_hash() {
|
||||||
let data1 = vec![1; 100];
|
let data1 = vec![1; 32];
|
||||||
let data2 = vec![2; 100];
|
let data2 = vec![2; 32];
|
||||||
let data3 = vec![3; 100];
|
let data3 = vec![3; 32];
|
||||||
let mut list = vec![data1, data2, data3];
|
let mut list = vec![data1, data2, data3];
|
||||||
let result = merkle_hash(&mut list);
|
let result = merkle_hash(&mut list);
|
||||||
|
|
||||||
|
@ -146,10 +146,10 @@ pub fn ssz_tree_hash_derive(input: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
impl ssz::TreeHash for #name {
|
impl ssz::TreeHash for #name {
|
||||||
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
let mut list: Vec<Vec<u8>> = Vec::new();
|
let mut list: Vec<Vec<u8>> = Vec::new();
|
||||||
#(
|
#(
|
||||||
list.push(self.#field_idents.hash_tree_root_internal());
|
list.push(self.#field_idents.hash_tree_root());
|
||||||
)*
|
)*
|
||||||
|
|
||||||
ssz::merkle_hash(&mut list)
|
ssz::merkle_hash(&mut list)
|
||||||
@ -224,7 +224,7 @@ pub fn ssz_signed_root_derive(input: TokenStream) -> TokenStream {
|
|||||||
fn signed_root(&self) -> Vec<u8> {
|
fn signed_root(&self) -> Vec<u8> {
|
||||||
let mut list: Vec<Vec<u8>> = Vec::new();
|
let mut list: Vec<Vec<u8>> = Vec::new();
|
||||||
#(
|
#(
|
||||||
list.push(self.#field_idents.hash_tree_root_internal());
|
list.push(self.#field_idents.hash_tree_root());
|
||||||
)*
|
)*
|
||||||
|
|
||||||
ssz::merkle_hash(&mut list)
|
ssz::merkle_hash(&mut list)
|
||||||
|
Loading…
Reference in New Issue
Block a user