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

Fix: Remove hashing from leaf keys in SMT Node #69

Merged
merged 5 commits into from
Feb 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions src/sparse/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ pub(crate) struct Node {
}

impl Node {
pub fn create_leaf(key: &[u8], data: &[u8]) -> Self {
pub fn create_leaf(key: &Bytes32, data: &[u8]) -> Self {
let buffer = Self::default_buffer();
let mut node = Self { buffer };
node.set_height(0);
node.set_bytes_prefix(&[LEAF]);
node.set_bytes_lo(&sum(key));
node.set_bytes_lo(key);
node.set_bytes_hi(&sum(data));
node
}
Expand Down Expand Up @@ -413,17 +413,17 @@ mod test_node {
use crate::sparse::hash::sum;
use crate::sparse::{zero_sum, Node};

fn leaf_hash(key: &[u8], data: &[u8]) -> Bytes32 {
fn leaf_hash(key: &Bytes32, data: &[u8]) -> Bytes32 {
let mut buffer = [0; 65];
buffer[0..1].clone_from_slice(&[LEAF]);
buffer[1..33].clone_from_slice(&sum(key));
buffer[1..33].clone_from_slice(key);
buffer[33..65].clone_from_slice(&sum(data));
sum(&buffer)
}

#[test]
fn test_create_leaf_returns_a_valid_leaf() {
let leaf = Node::create_leaf(b"LEAF", &[1u8; 32]);
let leaf = Node::create_leaf(&sum(b"LEAF"), &[1u8; 32]);
assert_eq!(leaf.is_leaf(), true);
assert_eq!(leaf.is_node(), false);
assert_eq!(leaf.height(), 0);
Expand All @@ -434,15 +434,18 @@ mod test_node {

#[test]
fn test_create_node_returns_a_valid_node() {
let left_child = Node::create_leaf(b"LEFT", &[1u8; 32]);
let right_child = Node::create_leaf(b"RIGHT", &[1u8; 32]);
let left_child = Node::create_leaf(&sum(b"LEFT"), &[1u8; 32]);
let right_child = Node::create_leaf(&sum(b"RIGHT"), &[1u8; 32]);
let node = Node::create_node(&left_child, &right_child);
assert_eq!(node.is_leaf(), false);
assert_eq!(node.is_node(), true);
assert_eq!(node.height(), 1);
assert_eq!(node.prefix(), NODE);
assert_eq!(node.left_child_key(), &leaf_hash(b"LEFT", &[1u8; 32]));
assert_eq!(node.right_child_key(), &leaf_hash(b"RIGHT", &[1u8; 32]));
assert_eq!(node.left_child_key(), &leaf_hash(&sum(b"LEFT"), &[1u8; 32]));
assert_eq!(
node.right_child_key(),
&leaf_hash(&sum(b"RIGHT"), &[1u8; 32])
);
}

#[test]
Expand Down Expand Up @@ -509,7 +512,7 @@ mod test_node {
expected_buffer[5..37].clone_from_slice(&sum(b"LEAF"));
expected_buffer[37..69].clone_from_slice(&sum(&[1u8; 32]));

let leaf = Node::create_leaf(b"LEAF", &[1u8; 32]);
let leaf = Node::create_leaf(&sum(b"LEAF"), &[1u8; 32]);
let buffer = leaf.buffer();

assert_eq!(buffer, expected_buffer);
Expand All @@ -522,11 +525,11 @@ mod test_node {
let mut expected_buffer = [0u8; 69];
expected_buffer[0..4].clone_from_slice(&1_u32.to_be_bytes());
expected_buffer[4..5].clone_from_slice(&[NODE]);
expected_buffer[5..37].clone_from_slice(&leaf_hash(b"LEFT", &[1u8; 32]));
expected_buffer[37..69].clone_from_slice(&leaf_hash(b"RIGHT", &[1u8; 32]));
expected_buffer[5..37].clone_from_slice(&leaf_hash(&sum(b"LEFT"), &[1u8; 32]));
expected_buffer[37..69].clone_from_slice(&leaf_hash(&sum(b"RIGHT"), &[1u8; 32]));

let left_child = Node::create_leaf(b"LEFT", &[1u8; 32]);
let right_child = Node::create_leaf(b"RIGHT", &[1u8; 32]);
let left_child = Node::create_leaf(&sum(b"LEFT"), &[1u8; 32]);
let right_child = Node::create_leaf(&sum(b"RIGHT"), &[1u8; 32]);
let node = Node::create_node(&left_child, &right_child);
let buffer = node.buffer();

Expand All @@ -543,7 +546,7 @@ mod test_node {
expected_buffer[33..65].clone_from_slice(&sum(&[1u8; 32]));
let expected_value = sum(&expected_buffer);

let node = Node::create_leaf(b"LEAF", &[1u8; 32]);
let node = Node::create_leaf(&sum(b"LEAF"), &[1u8; 32]);
let value = node.hash();

assert_eq!(value, expected_value);
Expand All @@ -555,12 +558,12 @@ mod test_node {
fn test_node_hash_returns_expected_hash_value() {
let mut expected_buffer = [0u8; 65];
expected_buffer[0..1].clone_from_slice(&[NODE]);
expected_buffer[1..33].clone_from_slice(&leaf_hash(b"LEFT", &[1u8; 32]));
expected_buffer[33..65].clone_from_slice(&leaf_hash(b"RIGHT", &[1u8; 32]));
expected_buffer[1..33].clone_from_slice(&leaf_hash(&sum(b"LEFT"), &[1u8; 32]));
expected_buffer[33..65].clone_from_slice(&leaf_hash(&sum(b"RIGHT"), &[1u8; 32]));
let expected_value = sum(&expected_buffer);

let left_child = Node::create_leaf(b"LEFT", &[1u8; 32]);
let right_child = Node::create_leaf(b"RIGHT", &[1u8; 32]);
let left_child = Node::create_leaf(&sum(b"LEFT"), &[1u8; 32]);
let right_child = Node::create_leaf(&sum(b"RIGHT"), &[1u8; 32]);
let node = Node::create_node(&left_child, &right_child);
let value = node.hash();

Expand All @@ -571,6 +574,7 @@ mod test_node {
#[cfg(test)]
mod test_storage_node {
use crate::common::{Bytes32, StorageError, StorageMap};
use crate::sparse::hash::sum;
use crate::sparse::node::Buffer;
use crate::sparse::{Node, StorageNode};
use fuel_storage::Storage;
Expand All @@ -579,10 +583,10 @@ mod test_storage_node {
fn test_node_left_child_returns_the_left_child() {
let mut s = StorageMap::<Bytes32, Buffer>::new();

let leaf_0 = Node::create_leaf(b"Hello World", &[1u8; 32]);
let leaf_0 = Node::create_leaf(&sum(b"Hello World"), &[1u8; 32]);
let _ = s.insert(&leaf_0.hash(), leaf_0.as_buffer());

let leaf_1 = Node::create_leaf(b"Goodbye World", &[1u8; 32]);
let leaf_1 = Node::create_leaf(&sum(b"Goodbye World"), &[1u8; 32]);
let _ = s.insert(&leaf_1.hash(), leaf_1.as_buffer());

let node_0 = Node::create_node(&leaf_0, &leaf_1);
Expand All @@ -598,10 +602,10 @@ mod test_storage_node {
fn test_node_right_child_returns_the_right_child() {
let mut s = StorageMap::<Bytes32, Buffer>::new();

let leaf_0 = Node::create_leaf(b"Hello World", &[1u8; 32]);
let leaf_0 = Node::create_leaf(&sum(b"Hello World"), &[1u8; 32]);
let _ = s.insert(&leaf_0.hash(), leaf_0.as_buffer());

let leaf_1 = Node::create_leaf(b"Goodbye World", &[1u8; 32]);
let leaf_1 = Node::create_leaf(&sum(b"Goodbye World"), &[1u8; 32]);
let _ = s.insert(&leaf_1.hash(), leaf_1.as_buffer());

let node_0 = Node::create_node(&leaf_0, &leaf_1);
Expand Down