mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 22:04:44 +00:00
Publish ssz_types (and deps) to crates.io (#468)
* Rename `hashing` crate to `eth2_hashing` * Add license, desc to eth2_hashing Cargo.toml * Remove merkle root from eth2 hashing * Remove old benches folder (zombied from old branch) * Add docs to eth2_hashing * Prepare tree_hash for publishing on crates.io * Update deps to use crates.io instead of paths * Update all crates to pull ssz from crates.io * Remove cached_tree_hash, add patches to manifest * Fix compile error in benches * Remove unused code * Fix fake_crypto compile error
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
[package]
|
||||
name = "tree_hash"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
edition = "2018"
|
||||
license = "Apache-2.0"
|
||||
description = "Efficient Merkle-hashing as used in Ethereum 2.0"
|
||||
|
||||
[[bench]]
|
||||
name = "benches"
|
||||
@@ -11,11 +13,10 @@ harness = false
|
||||
[dev-dependencies]
|
||||
criterion = "0.2"
|
||||
rand = "0.7"
|
||||
tree_hash_derive = { path = "../tree_hash_derive" }
|
||||
tree_hash_derive = "0.2"
|
||||
types = { path = "../../types" }
|
||||
|
||||
[dependencies]
|
||||
ethereum-types = "0.6"
|
||||
hashing = { path = "../hashing" }
|
||||
int_to_bytes = { path = "../int_to_bytes" }
|
||||
eth2_hashing = "0.1"
|
||||
lazy_static = "0.1"
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
# Tree hashing
|
||||
|
||||
Provides both cached and non-cached tree hashing methods.
|
||||
|
||||
## Standard Tree Hash
|
||||
|
||||
```rust
|
||||
use tree_hash_derive::TreeHash;
|
||||
|
||||
#[derive(TreeHash)]
|
||||
struct Foo {
|
||||
a: u64,
|
||||
b: Vec<u64>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let foo = Foo {
|
||||
a: 42,
|
||||
b: vec![1, 2, 3]
|
||||
};
|
||||
|
||||
println!("root: {}", foo.tree_hash_root());
|
||||
}
|
||||
```
|
||||
|
||||
## Cached Tree Hash
|
||||
|
||||
|
||||
```rust
|
||||
use tree_hash_derive::{TreeHash, CachedTreeHash};
|
||||
|
||||
#[derive(TreeHash, CachedTreeHash)]
|
||||
struct Foo {
|
||||
a: u64,
|
||||
b: Vec<u64>,
|
||||
}
|
||||
|
||||
#[derive(TreeHash, CachedTreeHash)]
|
||||
struct Bar {
|
||||
a: Vec<Foo>,
|
||||
b: u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let bar = Bar {
|
||||
a: vec![
|
||||
Foo {
|
||||
a: 42,
|
||||
b: vec![1, 2, 3]
|
||||
}
|
||||
],
|
||||
b: 42
|
||||
};
|
||||
|
||||
let modified_bar = Bar {
|
||||
a: vec![
|
||||
Foo {
|
||||
a: 100,
|
||||
b: vec![1, 2, 3, 4, 5, 6]
|
||||
},
|
||||
Foo {
|
||||
a: 42,
|
||||
b: vec![]
|
||||
}
|
||||
],
|
||||
b: 99
|
||||
};
|
||||
|
||||
|
||||
let mut hasher = CachedTreeHasher::new(&bar).unwrap();
|
||||
hasher.update(&modified_bar).unwrap();
|
||||
|
||||
// Assert that the cached tree hash matches a standard tree hash.
|
||||
assert_eq!(hasher.tree_hash_root(), modified_bar.tree_hash_root());
|
||||
}
|
||||
```
|
||||
@@ -3,7 +3,6 @@ extern crate lazy_static;
|
||||
|
||||
use criterion::Criterion;
|
||||
use criterion::{black_box, criterion_group, criterion_main, Benchmark};
|
||||
use tree_hash::TreeHash;
|
||||
use types::test_utils::{generate_deterministic_keypairs, TestingBeaconStateBuilder};
|
||||
use types::{BeaconState, EthSpec, Keypair, MainnetEthSpec, MinimalEthSpec};
|
||||
|
||||
@@ -36,7 +35,12 @@ fn bench_suite<T: EthSpec>(c: &mut Criterion, spec_desc: &str, validator_count:
|
||||
Benchmark::new("genesis_state", move |b| {
|
||||
b.iter_batched_ref(
|
||||
|| state.clone(),
|
||||
|state| black_box(state.tree_hash_root()),
|
||||
// Note: `state.canonical_root()` uses whatever `tree_hash` that the `types` crate
|
||||
// uses, which is not necessarily this crate. If you want to ensure that types is
|
||||
// using this local version of `tree_hash`, ensure you add a workspace-level
|
||||
// [dependency
|
||||
// patch](https://doc.rust-lang.org/cargo/reference/manifest.html#the-patch-section).
|
||||
|state| black_box(state.canonical_root()),
|
||||
criterion::BatchSize::SmallInput,
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use tree_hash::TreeHash;
|
||||
use types::test_utils::TestingBeaconStateBuilder;
|
||||
use types::{BeaconState, EthSpec, MainnetEthSpec};
|
||||
|
||||
@@ -29,7 +28,7 @@ fn main() {
|
||||
let mut vec = Vec::with_capacity(TREE_HASH_LOOPS);
|
||||
|
||||
for _ in 0..TREE_HASH_LOOPS {
|
||||
let root = state.tree_hash_root();
|
||||
let root = state.canonical_root();
|
||||
vec.push(root[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use super::*;
|
||||
use ethereum_types::H256;
|
||||
use int_to_bytes::int_to_bytes32;
|
||||
|
||||
macro_rules! impl_for_bitsize {
|
||||
($type: ident, $bit_size: expr) => {
|
||||
@@ -122,6 +121,13 @@ macro_rules! impl_for_list {
|
||||
impl_for_list!(Vec<T>);
|
||||
impl_for_list!(&[T]);
|
||||
|
||||
/// Returns `int` as little-endian bytes with a length of 32.
|
||||
fn int_to_bytes32(int: u64) -> Vec<u8> {
|
||||
let mut vec = int.to_le_bytes().to_vec();
|
||||
vec.resize(32, 0);
|
||||
vec
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
@@ -137,4 +143,22 @@ mod test {
|
||||
assert_eq!(false.tree_hash_root(), false_bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn int_to_bytes() {
|
||||
assert_eq!(&int_to_bytes32(0), &[0; 32]);
|
||||
assert_eq!(
|
||||
&int_to_bytes32(1),
|
||||
&[
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
&int_to_bytes32(u64::max_value()),
|
||||
&[
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ mod test {
|
||||
let mut preimage = vec![42; BYTES_PER_CHUNK];
|
||||
preimage.append(&mut vec![42]);
|
||||
preimage.append(&mut vec![0; BYTES_PER_CHUNK - 1]);
|
||||
hashing::hash(&preimage)
|
||||
eth2_hashing::hash(&preimage)
|
||||
};
|
||||
|
||||
assert_eq!(mix_in_length(&[42; BYTES_PER_CHUNK], 42), hash);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::BYTES_PER_CHUNK;
|
||||
use hashing::hash;
|
||||
use eth2_hashing::hash;
|
||||
|
||||
/// The size of the cache that stores padding nodes for a given height.
|
||||
///
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::*;
|
||||
use hashing::hash;
|
||||
use eth2_hashing::hash;
|
||||
|
||||
/// Merkleizes bytes and returns the root, using a simple algorithm that does not optimize to avoid
|
||||
/// processing or storing padding bytes.
|
||||
|
||||
Reference in New Issue
Block a user