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

Consolidate and deduplicate MMR API methods #12530

Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2ccf7c0
histor. batch proof: make best block arg optional
Lederstrumpf Oct 17, 2022
019d5cd
correct testing range
Lederstrumpf Oct 17, 2022
deb12a4
make generate_batch_proof stub for historical
Lederstrumpf Oct 18, 2022
5ffff01
merge generate_{historical_}batch_proof functions
Lederstrumpf Oct 18, 2022
ed0f1fa
merge generate_{batch_}proof functions
Lederstrumpf Oct 18, 2022
0730e9a
merge verify_{batch_}proof functions
Lederstrumpf Oct 18, 2022
e617095
merge verify_{batch_}proof_stateless functions
Lederstrumpf Oct 18, 2022
f08f6f0
remove {Leaf}Proof
Lederstrumpf Oct 19, 2022
2657723
rename BatchProof->Proof
Lederstrumpf Oct 19, 2022
f0454e5
cleanup
Lederstrumpf Oct 19, 2022
e77bcd9
expose verify_proof rpc api
Lederstrumpf Nov 6, 2022
1c599ef
document verify_proof
Lederstrumpf Nov 6, 2022
159712e
expose verify_proof_stateless rpc api
Lederstrumpf Nov 6, 2022
d576d3a
add optional BlockHash to mmr_root rpc api
Lederstrumpf Nov 7, 2022
087c77e
fixup! expose verify_proof rpc api
Lederstrumpf Nov 7, 2022
39f3676
fix documentation phrasing
Lederstrumpf Nov 7, 2022
212161d
documentation grammar
Lederstrumpf Nov 7, 2022
5ae856a
Merge remote-tracking branch 'origin/master' into consolidate-and-ded…
Nov 8, 2022
d5f4cf7
define mmr error msgs together with error enum
Lederstrumpf Nov 8, 2022
a7069fd
fixup! define mmr error msgs together with error enum
Lederstrumpf Nov 8, 2022
63d7694
map decoding errors to CallError::InvalidParams
Lederstrumpf Nov 8, 2022
95aa689
fixup! map decoding errors to CallError::InvalidParams
Lederstrumpf Nov 8, 2022
bfaaa06
Merge remote-tracking branch 'origin/master' into consolidate-and-ded…
Nov 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 7 additions & 51 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2021,59 +2021,15 @@ impl_runtime_apis! {
mmr::Hash,
BlockNumber,
> for Runtime {
fn generate_proof(block_number: BlockNumber)
-> Result<(mmr::EncodableOpaqueLeaf, mmr::Proof<mmr::Hash>), mmr::Error>
{
Mmr::generate_batch_proof(vec![block_number]).and_then(|(leaves, proof)|
Ok((
mmr::EncodableOpaqueLeaf::from_leaf(&leaves[0]),
mmr::BatchProof::into_single_leaf_proof(proof)?
))
)
}

fn verify_proof(leaf: mmr::EncodableOpaqueLeaf, proof: mmr::Proof<mmr::Hash>)
-> Result<(), mmr::Error>
{
let leaf: mmr::Leaf = leaf
.into_opaque_leaf()
.try_decode()
.ok_or(mmr::Error::Verify)?;
Mmr::verify_leaves(vec![leaf], mmr::Proof::into_batch_proof(proof))
}

fn verify_proof_stateless(
root: mmr::Hash,
leaf: mmr::EncodableOpaqueLeaf,
proof: mmr::Proof<mmr::Hash>
) -> Result<(), mmr::Error> {
let node = mmr::DataOrHash::Data(leaf.into_opaque_leaf());
pallet_mmr::verify_leaves_proof::<mmr::Hashing, _>(root, vec![node], mmr::Proof::into_batch_proof(proof))
}

fn mmr_root() -> Result<mmr::Hash, mmr::Error> {
Ok(Mmr::mmr_root())
}

fn generate_batch_proof(
block_numbers: Vec<BlockNumber>,
) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::BatchProof<mmr::Hash>), mmr::Error> {
Mmr::generate_batch_proof(block_numbers).map(|(leaves, proof)| {
(
leaves
.into_iter()
.map(|leaf| mmr::EncodableOpaqueLeaf::from_leaf(&leaf))
.collect(),
proof,
)
})
}

fn generate_historical_batch_proof(
fn generate_proof(
block_numbers: Vec<BlockNumber>,
best_known_block_number: BlockNumber,
) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::BatchProof<mmr::Hash>), mmr::Error> {
Mmr::generate_historical_batch_proof(block_numbers, best_known_block_number).map(
best_known_block_number: Option<BlockNumber>,
) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::Proof<mmr::Hash>), mmr::Error> {
Mmr::generate_proof(block_numbers, best_known_block_number).map(
|(leaves, proof)| {
(
leaves
Expand All @@ -2086,7 +2042,7 @@ impl_runtime_apis! {
)
}

fn verify_batch_proof(leaves: Vec<mmr::EncodableOpaqueLeaf>, proof: mmr::BatchProof<mmr::Hash>)
fn verify_proof(leaves: Vec<mmr::EncodableOpaqueLeaf>, proof: mmr::Proof<mmr::Hash>)
-> Result<(), mmr::Error>
{
let leaves = leaves.into_iter().map(|leaf|
Expand All @@ -2096,10 +2052,10 @@ impl_runtime_apis! {
Mmr::verify_leaves(leaves, proof)
}

fn verify_batch_proof_stateless(
fn verify_proof_stateless(
root: mmr::Hash,
leaves: Vec<mmr::EncodableOpaqueLeaf>,
proof: mmr::BatchProof<mmr::Hash>
proof: mmr::Proof<mmr::Hash>
) -> Result<(), mmr::Error> {
let nodes = leaves.into_iter().map(|leaf|mmr::DataOrHash::Data(leaf.into_opaque_leaf())).collect();
pallet_mmr::verify_leaves_proof::<mmr::Hashing, _>(root, nodes, proof)
Expand Down
36 changes: 7 additions & 29 deletions client/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use beefy_primitives::{
KEY_TYPE as BeefyKeyType,
};
use sc_network::{config::RequestResponseConfig, ProtocolName};
use sp_mmr_primitives::{BatchProof, EncodableOpaqueLeaf, Error as MmrError, MmrApi, Proof};
use sp_mmr_primitives::{EncodableOpaqueLeaf, Error as MmrError, MmrApi, Proof};

use sp_api::{ApiRef, ProvideRuntimeApi};
use sp_consensus::BlockOrigin;
Expand Down Expand Up @@ -246,47 +246,25 @@ macro_rules! create_test_api {
}

impl MmrApi<Block, MmrRootHash, NumberFor<Block>> for RuntimeApi {
fn generate_proof(_block_number: u64)
-> Result<(EncodableOpaqueLeaf, Proof<MmrRootHash>), MmrError> {
unimplemented!()
}

fn verify_proof(_leaf: EncodableOpaqueLeaf, _proof: Proof<MmrRootHash>)
-> Result<(), MmrError> {
unimplemented!()
}

fn verify_proof_stateless(
_root: MmrRootHash,
_leaf: EncodableOpaqueLeaf,
_proof: Proof<MmrRootHash>
) -> Result<(), MmrError> {
unimplemented!()
}

fn mmr_root() -> Result<MmrRootHash, MmrError> {
Ok($mmr_root)
}

fn generate_batch_proof(_block_numbers: Vec<u64>) -> Result<(Vec<EncodableOpaqueLeaf>, BatchProof<MmrRootHash>), MmrError> {
unimplemented!()
}

fn generate_historical_batch_proof(
fn generate_proof(
_block_numbers: Vec<u64>,
_best_known_block_number: u64
) -> Result<(Vec<EncodableOpaqueLeaf>, BatchProof<MmrRootHash>), MmrError> {
_best_known_block_number: Option<u64>
) -> Result<(Vec<EncodableOpaqueLeaf>, Proof<MmrRootHash>), MmrError> {
unimplemented!()
}

fn verify_batch_proof(_leaves: Vec<EncodableOpaqueLeaf>, _proof: BatchProof<MmrRootHash>) -> Result<(), MmrError> {
fn verify_proof(_leaves: Vec<EncodableOpaqueLeaf>, _proof: Proof<MmrRootHash>) -> Result<(), MmrError> {
unimplemented!()
}

fn verify_batch_proof_stateless(
fn verify_proof_stateless(
_root: MmrRootHash,
_leaves: Vec<EncodableOpaqueLeaf>,
_proof: BatchProof<MmrRootHash>
_proof: Proof<MmrRootHash>
) -> Result<(), MmrError> {
unimplemented!()
}
Expand Down
2 changes: 1 addition & 1 deletion frame/beefy-mmr/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! Inner nodes are created by concatenating child hashes and hashing again. The implementation
//! does not perform any sorting of the input data (leaves) nor when inner nodes are created.
//!
//! If the number of leaves is not even, last leave (hash of) is promoted to the upper layer.
//! If the number of leaves is not even, last leaf (hash of) is promoted to the upper layer.

pub use sp_runtime::traits::Keccak256;
use sp_runtime::{app_crypto::sp_core, sp_std, traits::Hash as HashT};
Expand Down
Loading