Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Sparse Merkle Tree Generic Key Size #660

Closed
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

#### Changed

- [#660](https://github.com/FuelLabs/fuel-vm/pull/660): Added generic argument to the sparse Merkle tree to allow users to define the key space. The default export sparse Merkle tree is defined as the 32 byte SMT to ensure compatability.
- [#653](https://github.com/FuelLabs/fuel-vm/pull/653): `ECAL` opcode handler can now hold internal state.
- [#657](https://github.com/FuelLabs/fuel-vm/pull/657): Add debugger methods to remove or replace all breakpoints at
once.
Expand Down
1 change: 1 addition & 0 deletions fuel-merkle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ digest = { version = "0.10", default-features = false }
fuel-storage = { workspace = true, default-features = false }
hashbrown = "0.13"
hex = { version = "0.4", default-features = false, features = ["alloc"] }
once_cell = { version = "1.19", default-features = false, features = ["race"] }
serde = { version = "1.0", default-features = false, optional = true }
sha2 = { version = "0.10", default-features = false }

Expand Down
1 change: 1 addition & 0 deletions fuel-merkle/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub type Bytes4 = [u8; 4];
pub type Bytes8 = [u8; 8];
pub type Bytes16 = [u8; 16];
pub type Bytes32 = [u8; 32];
pub type Bytes<const N: usize> = [u8; N];

use alloc::vec::Vec;
pub type ProofSet = Vec<Bytes32>;
Expand Down
29 changes: 17 additions & 12 deletions fuel-merkle/src/common/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::common::{
Bytes32,
Bytes8,
};
use crate::common::Bytes;

use alloc::string::String;
use core::fmt;
Expand Down Expand Up @@ -56,15 +53,23 @@ where
}
}

impl KeyFormatting for Bytes8 {
type PrettyType = u64;
// impl KeyFormatting for Bytes8 {
// type PrettyType = u64;
//
// fn pretty(&self) -> Self::PrettyType {
// u64::from_be_bytes(*self)
// }
// }
//
// impl KeyFormatting for Bytes32 {
// type PrettyType = String;
//
// fn pretty(&self) -> Self::PrettyType {
// hex::encode(self)
// }
// }

fn pretty(&self) -> Self::PrettyType {
u64::from_be_bytes(*self)
}
}

impl KeyFormatting for Bytes32 {
impl<const N: usize> KeyFormatting for Bytes<N> {
type PrettyType = String;

fn pretty(&self) -> Self::PrettyType {
Expand Down
13 changes: 13 additions & 0 deletions fuel-merkle/src/sparse/generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub mod branch;
pub mod hash;
pub mod merkle_tree;
pub mod node;
pub mod primitive;

pub use merkle_tree::{
MerkleTree,
MerkleTreeError,
MerkleTreeKey,
};
pub(crate) use node::Node;
pub(crate) use primitive::Primitive;
29 changes: 29 additions & 0 deletions fuel-merkle/src/sparse/generic/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::common::Bytes;
use alloc::{
vec,
vec::Vec,
};
use once_cell::race::OnceBox;

use digest::Digest;
use sha2::Sha256 as Hash;

pub fn zero_sum<const N: usize>() -> &'static [u8; N] {
static ZERO: OnceBox<Vec<u8>> = OnceBox::new();
ZERO.get_or_init(|| Box::new(vec![0; N]))
.as_slice()
.try_into()
.expect("Expected valid zero sum")
}

pub fn sum<I, const N: usize>(data: I) -> Bytes<N>
where
I: AsRef<[u8]>,
{
let mut hash = Hash::new();
hash.update(data);
let hash = hash.finalize();
let mut vec = hash.as_slice().to_vec();
vec.truncate(N);
vec.try_into().unwrap()
}
Loading