-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Modify MMR generate_proof API to accept a vector of leaf indexes #10625
Modify MMR generate_proof API to accept a vector of leaf indexes #10625
Conversation
bin/node/runtime/src/lib.rs
Outdated
let leaves = { | ||
let mut decoded_leaves = Vec::new(); | ||
for l in &leaves { | ||
let leaf: mmr::Leaf = l.clone() | ||
.into_opaque_leaf() | ||
.try_decode() | ||
.ok_or(mmr::Error::Verify)?; | ||
decoded_leaves.push(leaf); | ||
} | ||
decoded_leaves | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust has a cool feature for this
let leaves = { | |
let mut decoded_leaves = Vec::new(); | |
for l in &leaves { | |
let leaf: mmr::Leaf = l.clone() | |
.into_opaque_leaf() | |
.try_decode() | |
.ok_or(mmr::Error::Verify)?; | |
decoded_leaves.push(leaf); | |
} | |
decoded_leaves | |
}; | |
let leaves = leaves.into_iter() | |
.map(|l| { | |
l.into_opaque_leaf() | |
.try_decode() | |
.ok_or(mmr::Error::Verify) | |
}) | |
.collect::<Result<Vec<mmr::Leaf>, mmr::Error>>()?; |
let is_valid = leaves.iter().all(|leaf| { | ||
let mut res = false; | ||
for pos in &positions { | ||
match p.verify(Node::Hash(root.clone()), vec![(*pos, leaf.clone())]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also modify primitives::Proof
to take an array of nodes
}; | ||
let leaf_count = self.leaves; | ||
self.mmr | ||
.gen_proof(vec![position]) | ||
.gen_proof(positions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chef's kiss
bin/node/runtime/src/lib.rs
Outdated
@@ -1603,30 +1603,31 @@ impl_runtime_apis! { | |||
Block, | |||
mmr::Hash, | |||
> for Runtime { | |||
fn generate_proof(leaf_index: pallet_mmr::primitives::LeafIndex) | |||
-> Result<(mmr::EncodableOpaqueLeaf, mmr::Proof<mmr::Hash>), mmr::Error> | |||
fn generate_proof(leaf_indexes: Vec<pallet_mmr::primitives::LeafIndex>) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the plural of index is 'indices', not 'indexes'
/// Generate MMR proof for a leaf under given index. | ||
fn generate_proof(leaf_index: LeafIndex) -> Result<(EncodableOpaqueLeaf, Proof<Hash>), Error>; | ||
/// Generate MMR proof for leaves under given indexes. | ||
fn generate_proof(leaf_indexes: Vec<LeafIndex>) -> Result<(Vec<(LeafIndex, EncodableOpaqueLeaf)>, Proof<Hash>), Error>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not backwards compatible. I recommend adding new functions instead of updating the old ones.
3a97c5f
to
daa4118
Compare
Description
This PR modifies the generate_proof MMR Runtime API to accept an array of leaf indexes, this would allow generating a proof for multiple leaf indexes in one run if needed.