Skip to content

Commit

Permalink
renaming bbm proof error type
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeantonio21 committed Mar 9, 2023
1 parent 65d1354 commit c235301
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions base_layer/mmr/src/balanced_binary_merkle_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use thiserror::Error;

use crate::{common::hash_together, BalancedBinaryMerkleTree, Hash};

pub(crate) fn cast_to_u32(value: usize) -> Result<u32, MergedBalancedBinaryMerkleProofError> {
u32::try_from(value).map_err(|_| MergedBalancedBinaryMerkleProofError::MathOverflow)
pub(crate) fn cast_to_u32(value: usize) -> Result<u32, BalancedBinaryMerkleProofError> {
u32::try_from(value).map_err(|_| BalancedBinaryMerkleProofError::MathOverflow)
}

#[derive(BorshDeserialize, BorshSerialize, Deserialize, Serialize, Clone, Debug, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -68,7 +68,7 @@ where D: Digest + DomainDigest
pub fn generate_proof(
tree: &BalancedBinaryMerkleTree<D>,
leaf_index: usize,
) -> Result<Self, MergedBalancedBinaryMerkleProofError> {
) -> Result<Self, BalancedBinaryMerkleProofError> {
let mut node_index = tree.get_node_index(leaf_index);
let mut proof = Vec::new();
while node_index > 0 {
Expand All @@ -89,7 +89,7 @@ where D: Digest + DomainDigest
}

#[derive(Debug, Error)]
pub enum MergedBalancedBinaryMerkleProofError {
pub enum BalancedBinaryMerkleProofError {
#[error("Can't merge zero proofs.")]
CantMergeZeroProofs,
#[error("Bad proof semantics")]
Expand Down Expand Up @@ -119,15 +119,15 @@ where D: Digest + DomainDigest
{
pub fn create_from_proofs(
proofs: Vec<BalancedBinaryMerkleProof<D>>,
) -> Result<Self, MergedBalancedBinaryMerkleProofError> {
) -> Result<Self, BalancedBinaryMerkleProofError> {
let heights = proofs
.iter()
.map(|proof| cast_to_u32(proof.path.len()))
.collect::<Result<Vec<_>, _>>()?;
let max_height = heights
.iter()
.max()
.ok_or(MergedBalancedBinaryMerkleProofError::CantMergeZeroProofs)?;
.ok_or(BalancedBinaryMerkleProofError::CantMergeZeroProofs)?;
let mut indices = proofs.iter().map(|proof| proof.node_index).collect::<Vec<_>>();
let mut paths = vec![Vec::new(); proofs.len()];
let mut join_indices = vec![None; proofs.len()];
Expand Down Expand Up @@ -167,19 +167,19 @@ where D: Digest + DomainDigest
mut self,
root: &Hash,
leaves_hashes: Vec<Hash>,
) -> Result<bool, MergedBalancedBinaryMerkleProofError> {
) -> Result<bool, BalancedBinaryMerkleProofError> {
// Check that the proof and verifier data match
let n = self.node_indices.len(); // number of merged proofs
if self.paths.len() != n || leaves_hashes.len() != n {
return Err(MergedBalancedBinaryMerkleProofError::BadProofSemantics);
return Err(BalancedBinaryMerkleProofError::BadProofSemantics);
}

let mut computed_hashes = leaves_hashes;
let max_height = self
.heights
.iter()
.max()
.ok_or(MergedBalancedBinaryMerkleProofError::CantMergeZeroProofs)?;
.ok_or(BalancedBinaryMerkleProofError::CantMergeZeroProofs)?;

// We need to compute the hashes row by row to be sure they are processed correctly.
for height in (0..*max_height).rev() {
Expand All @@ -195,14 +195,14 @@ where D: Digest + DomainDigest
.1
.as_bytes()
.try_into()
.map_err(|_| MergedBalancedBinaryMerkleProofError::BadProofSemantics)?,
.map_err(|_| BalancedBinaryMerkleProofError::BadProofSemantics)?,
);

// The index must also point to one of the proofs
if index < hashes.len() {
&hashes[index]
} else {
return Err(MergedBalancedBinaryMerkleProofError::BadProofSemantics);
return Err(BalancedBinaryMerkleProofError::BadProofSemantics);
}
},
MergedBalancedBinaryMerkleDataType::Hash => &hash_or_index.1,
Expand Down
2 changes: 1 addition & 1 deletion base_layer/mmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ pub mod pruned_hashset;
pub use backend::{ArrayLike, ArrayLikeExt};
pub use balanced_binary_merkle_proof::{
BalancedBinaryMerkleProof,
BalancedBinaryMerkleProofError,
MergedBalancedBinaryMerkleProof,
MergedBalancedBinaryMerkleProofError,
};
pub use balanced_binary_merkle_tree::{BalancedBinaryMerkleTree, BalancedBinaryMerkleTreeError};
/// MemBackendVec is a shareable, memory only, vector that can be be used with MmrCache to store checkpoints.
Expand Down

0 comments on commit c235301

Please sign in to comment.