Expand description
Constructs a new SparseMerkleTree<H, V, S>.
§Examples
use nam_sparse_merkle_tree::{
blake2b::Blake2bHasher, default_store::DefaultStore,
error::Error, MerkleProof,
SparseMerkleTree, traits::Value, H256, Hash,
};
use blake2b_rs::{Blake2b, Blake2bBuilder};
// define SMT
type SMT = SparseMerkleTree<Blake2bHasher, Hash, Word, DefaultStore<Hash, Word, 32>, 32>;
// define SMT value
#[derive(Default, Clone, PartialEq)]
pub struct Word(String, H256);
impl Value for Word {
fn as_slice(&self) -> &[u8] {
self.1.as_slice()
}
fn zero() -> Self {
Default::default()
}
}
// helper function
fn new_blake2b() -> Blake2b {
Blake2bBuilder::new(32).personal(b"SMT").build()
}
fn construct_smt() {
let mut tree = SMT::default();
for (i, word) in "The quick brown fox jumps over the lazy dog"
.split_whitespace()
.enumerate()
{
let key: Hash = {
let mut buf = [0u8; 32];
let mut hasher = new_blake2b();
hasher.update(&(i as u32).to_le_bytes());
hasher.finalize(&mut buf);
buf.into()
};
let hash: H256 = {
let mut buf = [0u8; 32];
if !word.is_empty() {
let mut hasher = new_blake2b();
hasher.update(word.as_bytes());
hasher.finalize(&mut buf);
}
buf.into()
};
let value = Word(word.to_string(), hash);
// insert key value into tree
tree.update(key, value).expect("update");
}
println!("SMT root is {:?} ", tree.root());
}Re-exports§
pub use h256::Hash;pub use h256::H256;pub use internal_key::InternalKey;pub use merkle_proof::CompiledMerkleProof;pub use merkle_proof::MerkleProof;pub use traits::Key;pub use tree::SparseMerkleTree;
Modules§
Constants§
- EXPECTED_
PATH_ SIZE - Expected path size: log2(256) * 2, used for hint vector capacity
- KEY_
LIMIT - Key limit size
- TREE_
HEIGHT - Height of sparse merkle tree