Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Conversation

Wizdave97
Copy link
Contributor

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.

Comment on lines 1616 to 1626
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
};
Copy link
Contributor

@seunlanlege seunlanlege Jan 10, 2022

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

Suggested change
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())]) {
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chef's kiss

@@ -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>)
Copy link
Contributor

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>;
Copy link
Contributor

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.

@Wizdave97 Wizdave97 closed this Jan 11, 2022
@Wizdave97 Wizdave97 force-pushed the david/allow-mmr-proof-generation-for-multiple-leaf-indexes branch from 3a97c5f to daa4118 Compare January 11, 2022 13:00
@Wizdave97 Wizdave97 deleted the david/allow-mmr-proof-generation-for-multiple-leaf-indexes branch January 11, 2022 13:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants