feat: return error instead of panic during MMR verification (#335)

This commit is contained in:
Andrey Khmuro
2024-10-17 17:23:29 +03:00
committed by GitHub
parent e214608c85
commit e82baa35bb
6 changed files with 85 additions and 40 deletions

View File

@@ -54,12 +54,17 @@ impl MerklePath {
/// Verifies the Merkle opening proof towards the provided root.
///
/// Returns `true` if `node` exists at `index` in a Merkle tree with `root`.
pub fn verify(&self, index: u64, node: RpoDigest, root: &RpoDigest) -> bool {
match self.compute_root(index, node) {
Ok(computed_root) => root == &computed_root,
Err(_) => false,
/// # Errors
/// Returns an error if:
/// - provided node index is invalid.
/// - root calculated during the verification differs from the provided one.
pub fn verify(&self, index: u64, node: RpoDigest, root: &RpoDigest) -> Result<(), MerkleError> {
let computed_root = self.compute_root(index, node)?;
if &computed_root != root {
return Err(MerkleError::ConflictingRoots(vec![computed_root, root.clone()]));
}
Ok(())
}
/// Returns an iterator over every inner node of this [MerklePath].