Skip to content

Commit

Permalink
feat: better errors for storage proof
Browse files Browse the repository at this point in the history
  • Loading branch information
cchudant committed Oct 1, 2024
1 parent 956b537 commit f4c3492
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "std")]
use std::{error::Error, fmt::Display};

use crate::{bonsai_database::DBError, String};
use crate::{bonsai_database::DBError, BitVec, String};

/// All errors that can be returned by BonsaiStorage.
#[derive(Debug)]
Expand All @@ -22,7 +22,7 @@ where
/// Error when decoding a node
NodeDecodeError(parity_scale_codec::Error),
/// Error when creating a storage proof.
CreateProof(String),
CreateProofKeyNotInTree { key: BitVec },
/// Malformated trie key.
KeyLength { expected: usize, got: usize },
}
Expand Down Expand Up @@ -56,7 +56,9 @@ where
BonsaiStorageError::Merge(e) => write!(f, "Merge error: {}", e),
BonsaiStorageError::Database(e) => write!(f, "Database error: {}", e),
BonsaiStorageError::NodeDecodeError(e) => write!(f, "Node decode error: {}", e),
BonsaiStorageError::CreateProof(e) => write!(f, "Proof creation error: {}", e),
BonsaiStorageError::CreateProofKeyNotInTree { key } => {
write!(f, "Key not in tree: {key:b}")
}
BonsaiStorageError::KeyLength { expected, got } => {
write!(f, "Malformated key length: expected {expected}, got {got}")
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub mod id;

pub use bonsai_database::{BonsaiDatabase, BonsaiPersistentDatabase, DBError, DatabaseKey};
pub use error::BonsaiStorageError;
pub use trie::proof::Membership;
pub use trie::proof::{Membership, MultiProof, ProofNode};

#[cfg(test)]
mod tests;
Expand All @@ -153,7 +153,7 @@ impl<T: parity_scale_codec::Encode> EncodeExt for T {}
use changes::ChangeBatch;
use key_value_db::KeyValueDB;
use starknet_types_core::{felt::Felt, hash::StarkHash};
use trie::{proof::MultiProof, tree::bytes_to_bitvec, trees::MerkleTrees};
use trie::{tree::bytes_to_bitvec, trees::MerkleTrees};

/// Structure that contains the configuration for the BonsaiStorage.
/// A default implementation is provided with coherent values.
Expand Down
13 changes: 8 additions & 5 deletions src/trie/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::{
tree::MerkleTree,
};
use crate::{
format,
id::Id,
key_value_db::KeyValueDB,
trie::{
Expand Down Expand Up @@ -71,7 +70,7 @@ impl MultiProof {
key_values.into_iter().map(move |(k, v)| {
let k = k.as_ref();

if k.len() != tree_height as _ {
if k.len() != tree_height as usize {
return Membership::NonMember;
}

Expand Down Expand Up @@ -187,11 +186,15 @@ impl<H: StarkHash + Send + Sync> MerkleTree<H> {
got: key.len(),
});
}
log::debug!("go to = {key:b}");
iter.traverse_to(&mut visitor, key)?;

log::debug!("iter = {iter:?}");
// We should have found a leaf here.
iter.leaf_hash.ok_or_else(|| {
BonsaiStorageError::CreateProof(format!("Key {key:b} is not in the trie."))
})?;
iter.leaf_hash
.ok_or(BonsaiStorageError::CreateProofKeyNotInTree {
key: key.to_bitvec(),
})?;
}

Ok(visitor.0)
Expand Down

0 comments on commit f4c3492

Please sign in to comment.