mirror of
https://github.com/pezkuwichain/merkle-mountain-range.git
synced 2026-07-09 04:47:27 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fb7605c6da | |||
| 92f32c6751 | |||
| 0e3b02c38f | |||
| d6cfbe6454 | |||
| c8ae8dad9d | |||
| 7796272361 |
+6
-6
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ckb-merkle-mountain-range"
|
name = "ckb-merkle-mountain-range"
|
||||||
version = "0.4.0"
|
version = "0.5.0"
|
||||||
authors = ["Nervos Core Dev <dev@nervos.org>"]
|
authors = ["Nervos Core Dev <dev@nervos.org>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@@ -12,15 +12,15 @@ default = ["std"]
|
|||||||
std = []
|
std = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cfg-if = "0.1"
|
cfg-if = "1.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
faster-hex = "0.3"
|
faster-hex = "0.6.1"
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
rand = "0.6"
|
rand = "0.8.5"
|
||||||
proptest = "0.9.4"
|
proptest = "1.0.0"
|
||||||
lazy_static = "1.3.0"
|
lazy_static = "1.3.0"
|
||||||
bytes = "0.4"
|
bytes = "1.2.0"
|
||||||
blake2b-rs = "0.1.4"
|
blake2b-rs = "0.1.4"
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
A generalized merkle mountain range implementation.
|
A generalized merkle mountain range implementation.
|
||||||
|
|
||||||
**Notice** this library is not stable yet, API and proof format may changes. Make sure you know what you do before using this library.
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* Leaves accumulation
|
* Leaves accumulation
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ fn bench(c: &mut Criterion) {
|
|||||||
c.bench_function("left_index_to_pos", |b| {
|
c.bench_function("left_index_to_pos", |b| {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let leaf_index = rng.gen_range(50_000_000_000, 70_000_000_000);
|
let leaf_index = rng.gen_range(50_000_000_000..70_000_000_000);
|
||||||
leaf_index_to_pos(leaf_index);
|
leaf_index_to_pos(leaf_index);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -18,7 +18,7 @@ fn bench(c: &mut Criterion) {
|
|||||||
c.bench_function("left_index_to_mmr_size", |b| {
|
c.bench_function("left_index_to_mmr_size", |b| {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let leaf_index = rng.gen_range(50_000_000_000, 70_000_000_000);
|
let leaf_index = rng.gen_range(50_000_000_000..70_000_000_000);
|
||||||
leaf_index_to_mmr_size(leaf_index);
|
leaf_index_to_mmr_size(leaf_index);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+1
-1
@@ -15,7 +15,7 @@ use core::fmt::Debug;
|
|||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
#[allow(clippy::upper_case_acronyms)]
|
||||||
pub struct MMR<T, M, S: MMRStore<T>> {
|
pub struct MMR<T, M, S> {
|
||||||
mmr_size: u64,
|
mmr_size: u64,
|
||||||
batch: MMRBatch<T, S>,
|
batch: MMRBatch<T, S>,
|
||||||
merge: PhantomData<M>,
|
merge: PhantomData<M>,
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
use crate::{vec::Vec, Result};
|
use crate::{vec::Vec, Result};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MMRBatch<Elem, Store: MMRStore<Elem>> {
|
pub struct MMRBatch<Elem, Store> {
|
||||||
memory_batch: Vec<(u64, Vec<Elem>)>,
|
memory_batch: Vec<(u64, Vec<Elem>)>,
|
||||||
store: Store,
|
store: Store,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use super::new_blake2b;
|
use super::new_blake2b;
|
||||||
use crate::{leaf_index_to_pos, util::MemStore, MMRStore, Merge, MerkleProof, Result, MMR};
|
use crate::{leaf_index_to_pos, util::MemStore, MMRStore, Merge, MerkleProof, Result, MMR};
|
||||||
use bytes::Bytes;
|
use bytes::{Bytes, BytesMut};
|
||||||
use std::fmt::{self, Debug};
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Header {
|
struct Header {
|
||||||
@@ -42,9 +42,9 @@ struct HashWithTD {
|
|||||||
|
|
||||||
impl HashWithTD {
|
impl HashWithTD {
|
||||||
fn serialize(&self) -> Bytes {
|
fn serialize(&self) -> Bytes {
|
||||||
let mut data = self.hash.clone();
|
let mut data = BytesMut::from(self.hash.as_ref());
|
||||||
data.extend(&self.td.to_le_bytes());
|
data.extend(&self.td.to_le_bytes());
|
||||||
data
|
data.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize(mut data: Bytes) -> Self {
|
fn deserialize(mut data: Bytes) -> Self {
|
||||||
@@ -57,12 +57,12 @@ impl HashWithTD {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for HashWithTD {
|
impl fmt::Debug for HashWithTD {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"HashWithTD {{ hash: {}, td: {} }}",
|
"HashWithTD {{ hash: {}, td: {} }}",
|
||||||
faster_hex::hex_string(&self.hash).unwrap(),
|
faster_hex::hex_string(&self.hash),
|
||||||
self.td
|
self.td
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ fn test_mmr_root() {
|
|||||||
mmr.push(NumberHash::from(i)).unwrap();
|
mmr.push(NumberHash::from(i)).unwrap();
|
||||||
});
|
});
|
||||||
let root = mmr.get_root().expect("get root");
|
let root = mmr.get_root().expect("get root");
|
||||||
let hex_root = hex_string(&root.0).unwrap();
|
let hex_root = hex_string(&root.0);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"f6794677f37a57df6a5ec36ce61036e43a36c1a009d05c81c9aa685dde1fd6e3",
|
"f6794677f37a57df6a5ec36ce61036e43a36c1a009d05c81c9aa685dde1fd6e3",
|
||||||
hex_root
|
hex_root
|
||||||
@@ -154,7 +154,7 @@ proptest! {
|
|||||||
let mut leaves: Vec<u32> = (0..count).collect();
|
let mut leaves: Vec<u32> = (0..count).collect();
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
leaves.shuffle(&mut rng);
|
leaves.shuffle(&mut rng);
|
||||||
let leaves_count = rng.gen_range(1, count - 1);
|
let leaves_count = rng.gen_range(1..count - 1);
|
||||||
leaves.truncate(leaves_count as usize);
|
leaves.truncate(leaves_count as usize);
|
||||||
test_mmr(count, leaves);
|
test_mmr(count, leaves);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ proptest! {
|
|||||||
let mut leaves: Vec<u32> = (0..count).collect();
|
let mut leaves: Vec<u32> = (0..count).collect();
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
leaves.shuffle(&mut rng);
|
leaves.shuffle(&mut rng);
|
||||||
let leaves_count = rng.gen_range(1, count - 1);
|
let leaves_count = rng.gen_range(1..count - 1);
|
||||||
leaves.truncate(leaves_count as usize);
|
leaves.truncate(leaves_count as usize);
|
||||||
test_sequence_sub_func(count, leaves);
|
test_sequence_sub_func(count, leaves);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user