Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Use SmallVec for TreeHash packed encoding #3581

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions consensus/ssz_types/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<Variable<N>> {
tree_hash::TreeHashType::List
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("List should never be packed.")
}

Expand All @@ -641,7 +641,7 @@ impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<Fixed<N>> {
tree_hash::TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("Vector should never be packed.")
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/ssz_types/src/fixed_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ where
tree_hash::TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("Vector should never be packed.")
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/ssz_types/src/variable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ where
tree_hash::TreeHashType::List
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("List should never be packed.")
}

Expand Down
30 changes: 15 additions & 15 deletions consensus/tree_hash/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ macro_rules! impl_for_bitsize {
TreeHashType::Basic
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
self.to_le_bytes().to_vec()
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
PackedEncoding::from_slice(&self.to_le_bytes())
}

fn tree_hash_packing_factor() -> usize {
Expand All @@ -41,7 +41,7 @@ impl TreeHash for bool {
TreeHashType::Basic
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
(*self as u8).tree_hash_packed_encoding()
}

Expand All @@ -62,7 +62,7 @@ macro_rules! impl_for_lt_32byte_u8_array {
TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
unreachable!("bytesN should never be packed.")
}

Expand All @@ -87,10 +87,10 @@ impl TreeHash for U128 {
TreeHashType::Basic
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 16];
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
let mut result = [0; 16];
self.to_little_endian(&mut result);
result
PackedEncoding::from_slice(&result)
}

fn tree_hash_packing_factor() -> usize {
Expand All @@ -109,10 +109,10 @@ impl TreeHash for U256 {
TreeHashType::Basic
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 32];
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
let mut result = [0; 32];
self.to_little_endian(&mut result);
result
PackedEncoding::from_slice(&result)
}

fn tree_hash_packing_factor() -> usize {
Expand All @@ -131,10 +131,10 @@ impl TreeHash for H160 {
TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 32];
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
let mut result = [0; 32];
result[0..20].copy_from_slice(self.as_bytes());
result
PackedEncoding::from_slice(&result)
}

fn tree_hash_packing_factor() -> usize {
Expand All @@ -153,8 +153,8 @@ impl TreeHash for H256 {
TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
self.as_bytes().to_vec()
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
PackedEncoding::from_slice(self.as_bytes())
}

fn tree_hash_packing_factor() -> usize {
Expand Down
11 changes: 7 additions & 4 deletions consensus/tree_hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ pub use merkleize_padded::merkleize_padded;
pub use merkleize_standard::merkleize_standard;

use eth2_hashing::{hash_fixed, ZERO_HASHES, ZERO_HASHES_MAX_INDEX};
use smallvec::SmallVec;

pub const BYTES_PER_CHUNK: usize = 32;
pub const HASHSIZE: usize = 32;
pub const MERKLE_HASH_CHUNK: usize = 2 * BYTES_PER_CHUNK;
pub const MAX_UNION_SELECTOR: u8 = 127;
pub const SMALLVEC_SIZE: usize = 32;

pub type Hash256 = ethereum_types::H256;
pub type PackedEncoding = SmallVec<[u8; SMALLVEC_SIZE]>;

/// Convenience method for `MerkleHasher` which also provides some fast-paths for small trees.
///
Expand Down Expand Up @@ -109,7 +112,7 @@ pub enum TreeHashType {
pub trait TreeHash {
fn tree_hash_type() -> TreeHashType;

fn tree_hash_packed_encoding(&self) -> Vec<u8>;
fn tree_hash_packed_encoding(&self) -> PackedEncoding;

fn tree_hash_packing_factor() -> usize;

Expand All @@ -125,7 +128,7 @@ where
T::tree_hash_type()
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
T::tree_hash_packed_encoding(*self)
}

Expand All @@ -146,7 +149,7 @@ macro_rules! tree_hash_ssz_encoding_as_vector {
tree_hash::TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
unreachable!("Vector should never be packed.")
}

Expand All @@ -169,7 +172,7 @@ macro_rules! tree_hash_ssz_encoding_as_list {
tree_hash::TreeHashType::List
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
unreachable!("List should never be packed.")
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/tree_hash/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ssz_derive::Encode;
use tree_hash::{Hash256, MerkleHasher, TreeHash, BYTES_PER_CHUNK};
use tree_hash::{Hash256, MerkleHasher, PackedEncoding, TreeHash, BYTES_PER_CHUNK};
use tree_hash_derive::TreeHash;

#[derive(Encode)]
Expand All @@ -18,7 +18,7 @@ impl tree_hash::TreeHash for HashVec {
tree_hash::TreeHashType::List
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
unreachable!("List should never be packed.")
}

Expand Down
6 changes: 3 additions & 3 deletions consensus/tree_hash_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn tree_hash_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Toke
tree_hash::TreeHashType::Container
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("Struct should never be packed.")
}

Expand Down Expand Up @@ -231,7 +231,7 @@ fn tree_hash_derive_enum_transparent(
tree_hash::TreeHashType::Container
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("Enum should never be packed")
}

Expand Down Expand Up @@ -288,7 +288,7 @@ fn tree_hash_derive_enum_union(derive_input: &DeriveInput, enum_data: &DataEnum)
tree_hash::TreeHashType::Container
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("Enum should never be packed")
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/types/src/execution_block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl tree_hash::TreeHash for ExecutionBlockHash {
Hash256::tree_hash_type()
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
self.0.tree_hash_packed_encoding()
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/types/src/graffiti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use ssz::{Decode, DecodeError, Encode};
use std::fmt;
use std::str::FromStr;
use tree_hash::TreeHash;
use tree_hash::{PackedEncoding, TreeHash};

pub const GRAFFITI_BYTES_LEN: usize = 32;

Expand Down Expand Up @@ -159,7 +159,7 @@ impl TreeHash for Graffiti {
<[u8; GRAFFITI_BYTES_LEN]>::tree_hash_type()
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.0.tree_hash_packed_encoding()
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/types/src/participation_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use safe_arith::{ArithError, SafeArith};
use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use test_random_derive::TestRandom;
use tree_hash::{TreeHash, TreeHashType};
use tree_hash::{PackedEncoding, TreeHash, TreeHashType};

#[derive(Debug, Default, Clone, Copy, PartialEq, Deserialize, Serialize, TestRandom)]
#[serde(transparent)]
Expand Down Expand Up @@ -78,7 +78,7 @@ impl TreeHash for ParticipationFlags {
u8::tree_hash_type()
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.bits.tree_hash_packed_encoding()
}

Expand Down
6 changes: 3 additions & 3 deletions consensus/types/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::convert::TryFrom;
use std::fmt::Debug;
use std::hash::Hash;
use test_random_derive::TestRandom;
use tree_hash::TreeHash;
use tree_hash::{PackedEncoding, TreeHash};

#[derive(Debug)]
pub enum BlockType {
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<T: EthSpec> TreeHash for BlindedPayload<T> {
<ExecutionPayloadHeader<T>>::tree_hash_type()
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.execution_payload_header.tree_hash_packed_encoding()
}

Expand Down Expand Up @@ -244,7 +244,7 @@ impl<T: EthSpec> TreeHash for FullPayload<T> {
<ExecutionPayload<T>>::tree_hash_type()
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
self.execution_payload.tree_hash_packed_encoding()
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/types/src/slot_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{ChainSpec, SignedRoot};
use rand::RngCore;
use safe_arith::{ArithError, SafeArith};
use serde_derive::{Deserialize, Serialize};
use ssz::{ssz_encode, Decode, DecodeError, Encode};
use ssz::{Decode, DecodeError, Encode};
use std::fmt;
use std::hash::Hash;
use std::iter::Iterator;
Expand Down
4 changes: 2 additions & 2 deletions consensus/types/src/slot_epoch_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ macro_rules! impl_ssz {
tree_hash::TreeHashType::Basic
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
ssz_encode(self)
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
self.0.tree_hash_packed_encoding()
}

fn tree_hash_packing_factor() -> usize {
Expand Down
2 changes: 1 addition & 1 deletion crypto/bls/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro_rules! impl_tree_hash {
tree_hash::TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
unreachable!("Vector should never be packed.")
}

Expand Down
2 changes: 1 addition & 1 deletion testing/ef_tests/src/cases/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ macro_rules! uint_wrapper {
<$wrapped_type>::tree_hash_type()
}

fn tree_hash_packed_encoding(&self) -> Vec<u8> {
fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
self.x.tree_hash_packed_encoding()
}

Expand Down