Skip to content

Commit

Permalink
Feat: Refactor BMT and SMT interfaces to use anyhow::Result (#86)
Browse files Browse the repository at this point in the history
* Add anyhow to cargo

* Update SMT interface to use anyhow Result

* Update BMT to use anyhow result
  • Loading branch information
bvrooman authored May 11, 2022
1 parent 8797b79 commit 6f26c8a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 0 deletions fuel-merkle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/FuelLabs/fuel-merkle"
description = "Fuel Merkle tree libraries."

[dependencies]
anyhow = "1.0"
bytes = "1.0"
digest = "0.9"
fuel-storage = "0.1"
Expand Down
26 changes: 13 additions & 13 deletions fuel-merkle/src/binary/merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::marker::{Send, Sync};

use anyhow::{anyhow, Result};
use fuel_storage::Storage;

use crate::binary::{empty_sum, Node};
Expand All @@ -23,7 +26,7 @@ pub struct MerkleTree<'storage, StorageError> {

impl<'storage, StorageError> MerkleTree<'storage, StorageError>
where
StorageError: std::error::Error + 'static,
StorageError: std::error::Error + Send + Sync + 'static,
{
pub fn new(storage: &'storage mut StorageType<StorageError>) -> Self {
Self {
Expand All @@ -36,7 +39,7 @@ where
pub fn load(
storage: &'storage mut StorageType<StorageError>,
leaves_count: u64,
) -> Result<Self, Box<dyn std::error::Error>> {
) -> Result<Self> {
let mut tree = Self {
storage,
head: None,
Expand All @@ -48,7 +51,7 @@ where
Ok(tree)
}

pub fn root(&mut self) -> Result<Bytes32, Box<dyn std::error::Error>> {
pub fn root(&mut self) -> Result<Bytes32> {
let root_node = self.root_node()?;
let root = match root_node {
None => *empty_sum(),
Expand All @@ -58,12 +61,9 @@ where
Ok(root)
}

pub fn prove(
&mut self,
proof_index: u64,
) -> Result<(Bytes32, ProofSet), Box<dyn std::error::Error>> {
pub fn prove(&mut self, proof_index: u64) -> Result<(Bytes32, ProofSet)> {
if proof_index + 1 > self.leaves_count {
return Err(Box::new(MerkleTreeError::InvalidProofIndex(proof_index)));
return Err(anyhow!(MerkleTreeError::InvalidProofIndex(proof_index)));
}

let mut proof_set = ProofSet::new();
Expand Down Expand Up @@ -91,7 +91,7 @@ where
Ok((root, proof_set))
}

pub fn push(&mut self, data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
pub fn push(&mut self, data: &[u8]) -> Result<()> {
let node = Node::create_leaf(self.leaves_count, data);
self.storage.insert(&node.key(), &node)?;
let next = self.head.take();
Expand All @@ -108,7 +108,7 @@ where
// PRIVATE
//

fn build(&mut self) -> Result<(), Box<dyn std::error::Error>> {
fn build(&mut self) -> Result<()> {
let keys = (0..self.leaves_count).map(|i| Position::from_leaf_index(i).in_order_index());
for key in keys {
let node = self
Expand All @@ -125,7 +125,7 @@ where
Ok(())
}

fn root_node(&mut self) -> Result<Option<Node>, Box<dyn std::error::Error>> {
fn root_node(&mut self) -> Result<Option<Node>> {
let root_node = match self.head {
None => None,
Some(ref initial) => {
Expand All @@ -142,7 +142,7 @@ where
Ok(root_node)
}

fn join_all_subtrees(&mut self) -> Result<(), Box<dyn std::error::Error>> {
fn join_all_subtrees(&mut self) -> Result<()> {
loop {
let current = self.head.as_ref().unwrap();
if !(current.next().is_some()
Expand All @@ -168,7 +168,7 @@ where
&mut self,
lhs: &mut Subtree<Node>,
rhs: &mut Subtree<Node>,
) -> Result<Box<Subtree<Node>>, Box<dyn std::error::Error>> {
) -> Result<Box<Subtree<Node>>> {
let joined_node = Node::create_node(lhs.node(), rhs.node());
self.storage.insert(&joined_node.key(), &joined_node)?;
let joined_head = Subtree::new(joined_node, lhs.take_next());
Expand Down
30 changes: 14 additions & 16 deletions fuel-merkle/src/sparse/merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::common::{AsPathIterator, Bytes32};
use std::marker::{Send, Sync};

use anyhow::Result;
use fuel_storage::Storage;

use crate::common::{AsPathIterator, Bytes32};
use crate::sparse::hash::sum;
use crate::sparse::{zero_sum, Buffer, Node, StorageNode};

Expand All @@ -10,26 +13,25 @@ pub enum MerkleTreeError {
LoadError(String),
}

type StorageType<StorageError> = dyn Storage<Bytes32, Buffer, Error = StorageError>;

pub struct MerkleTree<'storage, StorageError> {
root_node: Node,
storage: &'storage mut dyn Storage<Bytes32, Buffer, Error = StorageError>,
storage: &'storage mut StorageType<StorageError>,
}

impl<'a, 'storage, StorageError> MerkleTree<'storage, StorageError>
where
StorageError: std::error::Error + Clone + 'static,
StorageError: std::error::Error + Send + Sync + Clone + 'static,
{
pub fn new(storage: &'storage mut dyn Storage<Bytes32, Buffer, Error = StorageError>) -> Self {
pub fn new(storage: &'storage mut StorageType<StorageError>) -> Self {
Self {
root_node: Node::create_placeholder(),
storage,
}
}

pub fn load(
storage: &'storage mut dyn Storage<Bytes32, Buffer, Error = StorageError>,
root: &Bytes32,
) -> Result<Self, Box<dyn std::error::Error>> {
pub fn load(storage: &'storage mut StorageType<StorageError>, root: &Bytes32) -> Result<Self> {
let buffer = storage
.get(root)?
.ok_or(MerkleTreeError::LoadError(hex::encode(root)))?
Expand All @@ -41,11 +43,7 @@ where
Ok(tree)
}

pub fn update(
&'a mut self,
key: &Bytes32,
data: &[u8],
) -> Result<(), Box<dyn std::error::Error>> {
pub fn update(&'a mut self, key: &Bytes32, data: &[u8]) -> Result<()> {
if data.is_empty() {
// If the data is empty, this signifies a delete operation for the given key.
self.delete(key)?;
Expand All @@ -68,7 +66,7 @@ where
Ok(())
}

pub fn delete(&'a mut self, key: &Bytes32) -> Result<(), Box<dyn std::error::Error>> {
pub fn delete(&'a mut self, key: &Bytes32) -> Result<()> {
if self.root() == *zero_sum() {
// The zero root signifies that all leaves are empty, including the given key.
return Ok(());
Expand Down Expand Up @@ -118,7 +116,7 @@ where
requested_leaf_node: &Node,
path_nodes: &[Node],
side_nodes: &[Node],
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<()> {
let path = requested_leaf_node.leaf_key();
let actual_leaf_node = &path_nodes[0];

Expand Down Expand Up @@ -178,7 +176,7 @@ where
requested_leaf_node: &Node,
path_nodes: &[Node],
side_nodes: &[Node],
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<()> {
for node in path_nodes {
self.storage.remove(&node.hash())?;
}
Expand Down

0 comments on commit 6f26c8a

Please sign in to comment.