-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Infallible in-memory SMT (#100)
* In memory SMT * Format * In memory SMT tests * Tree ptr * Use Option for delayed SMT initialization * Use alloc box * Strongly typed self as Pin in interface * Use core phantompinned * Format * Use in-memory Merkle tree for data driven tests * Remove block * Relax constraint on Storage (#101) * Relax constraint on Storage * sum tree, and cleanup * relax storage for binary Co-authored-by: rakita <[email protected]>
- Loading branch information
Showing
6 changed files
with
172 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use crate::common::Bytes32; | ||
use crate::sparse::Buffer; | ||
use crate::{common, sparse}; | ||
|
||
type StorageMap = common::StorageMap<Bytes32, Buffer>; | ||
type SparseMerkleTree = sparse::MerkleTree<StorageMap>; | ||
|
||
pub struct MerkleTree { | ||
tree: SparseMerkleTree, | ||
} | ||
|
||
impl MerkleTree { | ||
pub fn new() -> Self { | ||
Self { | ||
tree: SparseMerkleTree::new(StorageMap::new()), | ||
} | ||
} | ||
|
||
pub fn update(&mut self, key: &Bytes32, data: &[u8]) { | ||
let _ = self.tree.update(key, data); | ||
} | ||
|
||
pub fn delete(&mut self, key: &Bytes32) { | ||
let _ = self.tree.delete(key); | ||
} | ||
|
||
pub fn root(&self) -> Bytes32 { | ||
self.tree.root() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use sparse::hash::sum; | ||
|
||
#[test] | ||
fn test_empty_root() { | ||
let tree = MerkleTree::new(); | ||
let root = tree.root(); | ||
let expected_root = "0000000000000000000000000000000000000000000000000000000000000000"; | ||
assert_eq!(hex::encode(root), expected_root); | ||
} | ||
|
||
#[test] | ||
fn test_update_1() { | ||
let mut tree = MerkleTree::new(); | ||
|
||
tree.update(&sum(b"\x00\x00\x00\x00"), b"DATA"); | ||
|
||
let root = tree.root(); | ||
let expected_root = "39f36a7cb4dfb1b46f03d044265df6a491dffc1034121bc1071a34ddce9bb14b"; | ||
assert_eq!(hex::encode(root), expected_root); | ||
} | ||
|
||
#[test] | ||
fn test_update_2() { | ||
let mut tree = MerkleTree::new(); | ||
|
||
tree.update(&sum(b"\x00\x00\x00\x00"), b"DATA"); | ||
tree.update(&sum(b"\x00\x00\x00\x01"), b"DATA"); | ||
|
||
let root = tree.root(); | ||
let expected_root = "8d0ae412ca9ca0afcb3217af8bcd5a673e798bd6fd1dfacad17711e883f494cb"; | ||
assert_eq!(hex::encode(root), expected_root); | ||
} | ||
|
||
#[test] | ||
fn test_update_3() { | ||
let mut tree = MerkleTree::new(); | ||
|
||
tree.update(&sum(b"\x00\x00\x00\x00"), b"DATA"); | ||
tree.update(&sum(b"\x00\x00\x00\x01"), b"DATA"); | ||
tree.update(&sum(b"\x00\x00\x00\x02"), b"DATA"); | ||
|
||
let root = tree.root(); | ||
let expected_root = "52295e42d8de2505fdc0cc825ff9fead419cbcf540d8b30c7c4b9c9b94c268b7"; | ||
assert_eq!(hex::encode(root), expected_root); | ||
} | ||
|
||
#[test] | ||
fn test_update_1_delete_1() { | ||
let mut tree = MerkleTree::new(); | ||
|
||
tree.update(&sum(b"\x00\x00\x00\x00"), b"DATA"); | ||
tree.delete(&sum(b"\x00\x00\x00\x00")); | ||
|
||
let root = tree.root(); | ||
let expected_root = "0000000000000000000000000000000000000000000000000000000000000000"; | ||
assert_eq!(hex::encode(root), expected_root); | ||
} | ||
|
||
#[test] | ||
fn test_update_2_delete_1() { | ||
let mut tree = MerkleTree::new(); | ||
|
||
tree.update(&sum(b"\x00\x00\x00\x00"), b"DATA"); | ||
tree.update(&sum(b"\x00\x00\x00\x01"), b"DATA"); | ||
tree.delete(&sum(b"\x00\x00\x00\x01")); | ||
|
||
let root = tree.root(); | ||
let expected_root = "39f36a7cb4dfb1b46f03d044265df6a491dffc1034121bc1071a34ddce9bb14b"; | ||
assert_eq!(hex::encode(root), expected_root); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.