Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Revert #115 (#116)
Browse files Browse the repository at this point in the history
Revert "debt: Remove first leaf from BMT proof se] t (#115)"

This reverts commit bf20199.
  • Loading branch information
bvrooman authored Sep 15, 2022
1 parent bf20199 commit ade288e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 66 deletions.
51 changes: 29 additions & 22 deletions src/binary/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ mod test {
}

#[test]
fn prove_returns_the_merkle_root_and_empty_proof_set_for_1_leaf() {
fn prove_returns_the_merkle_root_and_proof_set_for_1_leaf() {
let mut tree = MerkleTree::new();

let data = &TEST_DATA[0..1]; // 1 leaf
Expand All @@ -144,7 +144,7 @@ mod test {
let set = proof.1;

assert_eq!(root, leaf_0);
assert_eq!(set, ProofSet::default());
assert_eq!(set[0], leaf_0);
}
}

Expand Down Expand Up @@ -193,68 +193,75 @@ mod test {
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], node_5);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], leaf_1);
assert_eq!(set[2], node_5);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(1).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], node_5);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], leaf_0);
assert_eq!(set[2], node_5);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(2).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], node_1);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], leaf_3);
assert_eq!(set[2], node_1);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(3).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], node_1);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], leaf_2);
assert_eq!(set[2], node_1);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(4).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_5);
assert_eq!(set[1], leaf_6);
assert_eq!(set[2], node_3);
assert_eq!(set[0], leaf_4);
assert_eq!(set[1], leaf_5);
assert_eq!(set[2], leaf_6);
assert_eq!(set[3], node_3);
}
{
let proof = tree.prove(5).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_4);
assert_eq!(set[1], leaf_6);
assert_eq!(set[2], node_3);
assert_eq!(set[0], leaf_5);
assert_eq!(set[1], leaf_4);
assert_eq!(set[2], leaf_6);
assert_eq!(set[3], node_3);
}
{
let proof = tree.prove(6).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], node_9);
assert_eq!(set[1], node_3);
assert_eq!(set[0], leaf_6);
assert_eq!(set[1], node_9);
assert_eq!(set[2], node_3);
}
}
}
107 changes: 63 additions & 44 deletions src/binary/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ where
let root_node = self.root_node()?.unwrap();
let root_position = root_node.position();
let leaf_position = Position::from_leaf_index(proof_index);
let leaf_node = self.storage.get(&leaf_position.in_order_index())?.unwrap();
proof_set.push(*leaf_node.hash());

let (_, mut side_positions): (Vec<_>, Vec<_>) = root_position
.path(&leaf_position, self.leaves_count)
.iter()
Expand Down Expand Up @@ -204,7 +207,7 @@ where
mod test {
use super::{MerkleTree, NodesTable};
use crate::binary::{empty_sum, leaf_sum, node_sum};
use crate::common::{ProofSet, StorageMap};
use crate::common::StorageMap;
use fuel_merkle_test_helpers::TEST_DATA;
use fuel_storage::StorageInspect;

Expand Down Expand Up @@ -404,7 +407,7 @@ mod test {
}

#[test]
fn prove_returns_the_merkle_root_and_empty_proof_set_for_1_leaf() {
fn prove_returns_the_merkle_root_and_proof_set_for_1_leaf() {
let mut storage_map = StorageMap::<NodesTable>::new();
let mut tree = MerkleTree::new(&mut storage_map);

Expand All @@ -421,7 +424,7 @@ mod test {
let set = proof.1;

assert_eq!(root, leaf_0);
assert_eq!(set, ProofSet::default());
assert_eq!(set[0], leaf_0);
}
}

Expand Down Expand Up @@ -458,35 +461,39 @@ mod test {
let set = proof.1;

assert_eq!(root, node_3);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], node_5);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], leaf_1);
assert_eq!(set[2], node_5);
}
{
let proof = tree.prove(1).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_3);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], node_5);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], leaf_0);
assert_eq!(set[2], node_5);
}
{
let proof = tree.prove(2).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_3);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], node_1);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], leaf_3);
assert_eq!(set[2], node_1);
}
{
let proof = tree.prove(3).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_3);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], node_1);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], leaf_2);
assert_eq!(set[2], node_1);
}
}

Expand Down Expand Up @@ -528,47 +535,52 @@ mod test {
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], node_5);
assert_eq!(set[2], leaf_4);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], leaf_1);
assert_eq!(set[2], node_5);
assert_eq!(set[3], leaf_4);
}
{
let proof = tree.prove(1).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], node_5);
assert_eq!(set[2], leaf_4);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], leaf_0);
assert_eq!(set[2], node_5);
assert_eq!(set[3], leaf_4);
}
{
let proof = tree.prove(2).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], node_1);
assert_eq!(set[2], leaf_4);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], leaf_3);
assert_eq!(set[2], node_1);
assert_eq!(set[3], leaf_4);
}
{
let proof = tree.prove(3).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], node_1);
assert_eq!(set[2], leaf_4);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], leaf_2);
assert_eq!(set[2], node_1);
assert_eq!(set[3], leaf_4);
}
{
let proof = tree.prove(4).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], node_3);
assert_eq!(set[0], leaf_4);
assert_eq!(set[1], node_3);
}
}

Expand Down Expand Up @@ -618,68 +630,75 @@ mod test {
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], node_5);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], leaf_1);
assert_eq!(set[2], node_5);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(1).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_0);
assert_eq!(set[1], node_5);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_1);
assert_eq!(set[1], leaf_0);
assert_eq!(set[2], node_5);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(2).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], node_1);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], leaf_3);
assert_eq!(set[2], node_1);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(3).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_2);
assert_eq!(set[1], node_1);
assert_eq!(set[2], node_11);
assert_eq!(set[0], leaf_3);
assert_eq!(set[1], leaf_2);
assert_eq!(set[2], node_1);
assert_eq!(set[3], node_11);
}
{
let proof = tree.prove(4).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_5);
assert_eq!(set[1], leaf_6);
assert_eq!(set[2], node_3);
assert_eq!(set[0], leaf_4);
assert_eq!(set[1], leaf_5);
assert_eq!(set[2], leaf_6);
assert_eq!(set[3], node_3);
}
{
let proof = tree.prove(5).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], leaf_4);
assert_eq!(set[1], leaf_6);
assert_eq!(set[2], node_3);
assert_eq!(set[0], leaf_5);
assert_eq!(set[1], leaf_4);
assert_eq!(set[2], leaf_6);
assert_eq!(set[3], node_3);
}
{
let proof = tree.prove(6).unwrap();
let root = proof.0;
let set = proof.1;

assert_eq!(root, node_7);
assert_eq!(set[0], node_9);
assert_eq!(set[1], node_3);
assert_eq!(set[0], leaf_6);
assert_eq!(set[1], node_9);
assert_eq!(set[2], node_3);
}
}
}

0 comments on commit ade288e

Please sign in to comment.