Skip to content

Commit

Permalink
Merge branch 'feature/minimal-merkle-diff'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Jan 11, 2024
2 parents 5d21f1a + 8d23863 commit dd080eb
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 25 deletions.
30 changes: 28 additions & 2 deletions src/merkle/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl MerkleProof {
pub struct MerkleProofBuilder<'a, F> {
root: &'a DynCell,
filter: F,
allow_different_root: bool,
}

impl<'a, F> MerkleProofBuilder<'a, F>
Expand All @@ -208,7 +209,17 @@ where
/// Creates a new Merkle proof builder for the tree with the specified root,
/// using cells determined by filter.
pub fn new(root: &'a DynCell, f: F) -> Self {
Self { root, filter: f }
Self {
root,
filter: f,
allow_different_root: false,
}
}

/// Mark whether the different root is ok for this proof.
pub fn allow_different_root(mut self, allow: bool) -> Self {
self.allow_different_root = allow;
self
}

/// Extends the builder to additionally save all hashes
Expand All @@ -217,6 +228,7 @@ where
MerkleProofExtBuilder {
root: self.root,
filter: self.filter,
allow_different_root: self.allow_different_root,
}
}

Expand All @@ -239,6 +251,7 @@ where
cells: Default::default(),
pruned_branches: None,
context,
allow_different_root: self.allow_different_root,
}
.build()
}
Expand All @@ -258,6 +271,15 @@ where
pub struct MerkleProofExtBuilder<'a, F> {
root: &'a DynCell,
filter: F,
allow_different_root: bool,
}

impl<'a, F> MerkleProofExtBuilder<'a, F> {
/// Mark whether the different root is ok for this proof.
pub fn allow_different_root(mut self, allow: bool) -> Self {
self.allow_different_root = allow;
self
}
}

impl<'a, F> MerkleProofExtBuilder<'a, F>
Expand All @@ -276,6 +298,7 @@ where
cells: Default::default(),
pruned_branches: Some(&mut pruned_branches),
context,
allow_different_root: self.allow_different_root,
};
let cell = ok!(builder.build());
Ok((cell, pruned_branches))
Expand All @@ -288,6 +311,7 @@ struct BuilderImpl<'a, 'b, S = ahash::RandomState> {
cells: HashMap<&'a HashBytes, Cell, S>,
pruned_branches: Option<&'b mut HashMap<&'a HashBytes, bool, S>>,
context: &'b mut dyn CellContext,
allow_different_root: bool,
}

impl<'a, 'b, S> BuilderImpl<'a, 'b, S>
Expand All @@ -302,7 +326,9 @@ where
children: CellRefsBuilder,
}

if self.filter.check(self.root.repr_hash()) == FilterAction::Skip {
if !self.allow_different_root
&& self.filter.check(self.root.repr_hash()) == FilterAction::Skip
{
return Err(Error::EmptyProof);
}

Expand Down
Loading

0 comments on commit dd080eb

Please sign in to comment.