Skip to content

Commit

Permalink
Switch cases for Trie and original SMT
Browse files Browse the repository at this point in the history
  • Loading branch information
Code Monad committed Nov 10, 2022
1 parent c4e8b90 commit f398d35
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 32 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
default: fmt clippy test bench-test check test-c-impl test-cxx-build
default: fmt clippy clippy-trie test test-trie bench-test bench-test-trie check test-c-impl test-cxx-build

test:
cargo test --all --features std,smtc

test-trie:
cargo test --all --all-features

bench-test:
cargo bench -- --test

bench-test-trie:
cargo bench --features trie -- --test

clippy:
cargo clippy --all --features std,smtc --all-targets

clippy-trie:
cargo clippy --all --all-features --all-targets

fmt:
Expand Down
2 changes: 1 addition & 1 deletion benches/store_counter_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use rand::{thread_rng, Rng};
use sparse_merkle_tree::{
blake2b::Blake2bHasher,
tree::{BranchKey, BranchNode},
default_store::DefaultStore,
error::Error,
traits::{StoreReadOps, StoreWriteOps},
tree::{BranchKey, BranchNode},
SparseMerkleTree, H256,
};

Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub mod merkle_proof;
#[cfg(test)]
mod tests;
pub mod traits;
#[cfg(not(feature = "trie"))]
pub mod tree;
#[cfg(feature = "trie")]
pub mod trie_tree;
Expand Down
6 changes: 3 additions & 3 deletions src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ impl MergeValue {
}
}


/// Helper function for get base_node
/// When call with MergeValue::Value(v), it would be v
pub fn base_node<H: Hasher + Default>(&self) -> H256 {
match self {
#[cfg(feature = "trie")]
MergeValue::ShortCut {
key,
value,
Expand All @@ -112,9 +112,9 @@ impl MergeValue {
}
}


/// Helper function for Shortcut node
/// Transform it into a MergeWithZero node
#[cfg(feature = "trie")]
pub fn into_merge_with_zero<H: Hasher + Default>(&self) -> MergeValue {
match self {
MergeValue::ShortCut { key, value, height } => {
Expand All @@ -131,7 +131,7 @@ impl MergeValue {
zero_bits,
zero_count: *height,
}
},
}
_ => {
unreachable!();
}
Expand Down
2 changes: 2 additions & 0 deletions src/merkle_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl MerkleProof {
buffer.extend_from_slice(zero_bits.as_slice());
(Some(0x51), Some(buffer))
}
#[cfg(feature = "trie")]
_ => unreachable!(),
}
} else {
Expand Down Expand Up @@ -502,6 +503,7 @@ impl CompiledMerkleProof {
sub_proof.extend(zero_bits.as_slice());
is_last_merge_zero = false;
}
#[cfg(feature = "trie")]
_ => {}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests/tree.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::*;
use crate::{
blake2b::Blake2bHasher, default_store::DefaultStore, error::Error, merge::MergeValue,
MerkleProof, SparseMerkleTree,
MerkleProof,
};
use proptest::prelude::*;
use rand::prelude::{Rng, SliceRandom};
Expand Down
62 changes: 37 additions & 25 deletions src/trie_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ impl<H, V, S> SparseMerkleTree<H, V, S> {
}
}

impl<H: Hasher + Default, V, S: StoreReadOps<V>> SparseMerkleTree<H, V, S> {
/// Build a merkle tree from store, the root will be calculated automatically
pub fn new_with_store(store: S) -> Result<SparseMerkleTree<H, V, S>> {
let root_branch_key = BranchKey::new(core::u8::MAX, H256::zero());
store
.get_branch(&root_branch_key)
.map(|branch_node| {
branch_node
.map(|n| {
merge::<H>(core::u8::MAX, &H256::zero(), &n.left, &n.right).hash::<H>()
})
.unwrap_or_default()
})
.map(|root| SparseMerkleTree::new(root, store))
}
}

impl<H: Hasher + Default, V: Value, S: StoreReadOps<V> + StoreWriteOps<V>>
SparseMerkleTree<H, V, S>
{
Expand Down Expand Up @@ -126,18 +143,16 @@ impl<H: Hasher + Default, V: Value, S: StoreReadOps<V> + StoreWriteOps<V>>
MergeValue::from_h256(node.hash::<H>()),
)
}
} else if last_height != 0 {
(
MergeValue::shortcut(key, node.hash::<H>(), last_height),
MergeValue::shortcut(this_key, value, last_height),
)
} else {
if last_height != 0 {
(
MergeValue::shortcut(key, node.hash::<H>(), last_height),
MergeValue::shortcut(this_key, value, last_height),
)
} else {
(
MergeValue::from_h256(node.hash::<H>()),
MergeValue::from_h256(value),
)
}
(
MergeValue::from_h256(node.hash::<H>()),
MergeValue::from_h256(value),
)
};

let next_branch_key =
Expand All @@ -156,7 +171,7 @@ impl<H: Hasher + Default, V: Value, S: StoreReadOps<V> + StoreWriteOps<V>>
_ => {
if target.is_zero() || last_height == 0 {
let insert_value = if last_height == 0 {
node.clone()
node
} else {
MergeValue::shortcut(key, node.hash::<H>(), last_height)
};
Expand Down Expand Up @@ -190,13 +205,11 @@ impl<H: Hasher + Default, V: Value, S: StoreReadOps<V> + StoreWriteOps<V>>
self.store
.insert_branch(branch_key, BranchNode { left, right })?;
break; // stop walking
} else if last_height != 0 {
last_height -= 1;
} else {
if last_height != 0 {
last_height -= 1;
} else {
// do nothing with a zero insertion
break;
}
// do nothing with a zero insertion
break;
}
}

Expand Down Expand Up @@ -415,11 +428,7 @@ impl<H: Hasher + Default, V: Value, S: StoreReadOps<V>> SparseMerkleTree<H, V, S
};

match current {
MergeValue::ShortCut {
key,
value,
..
} => {
MergeValue::ShortCut { key, value, .. } => {
if !key.eq(&leaf_key) {
let fork_at = key.fork_height(&leaf_key);
if fork_at == height {
Expand Down Expand Up @@ -454,8 +463,11 @@ impl<H: Hasher + Default, V: Value, S: StoreReadOps<V>> SparseMerkleTree<H, V, S
}

/// Helper function for a merkle_path insertion
fn push_result_maybe_shortcut<H: Hasher + Default>(proof_result: &mut Vec<MergeValue>, value: MergeValue) {
if value.is_shortcut(){
fn push_result_maybe_shortcut<H: Hasher + Default>(
proof_result: &mut Vec<MergeValue>,
value: MergeValue,
) {
if value.is_shortcut() {
proof_result.push(value.into_merge_with_zero::<H>())
} else {
proof_result.push(value)
Expand Down

0 comments on commit f398d35

Please sign in to comment.