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

Commit

Permalink
rename BatchProof->Proof
Browse files Browse the repository at this point in the history
no need to qualify if only one universal proof type.
  • Loading branch information
Lederstrumpf committed Oct 19, 2022
1 parent f08f6f0 commit 2657723
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 60 deletions.
6 changes: 3 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2028,7 +2028,7 @@ impl_runtime_apis! {
fn generate_proof(
block_numbers: Vec<BlockNumber>,
best_known_block_number: Option<BlockNumber>,
) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::BatchProof<mmr::Hash>), mmr::Error> {
) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::Proof<mmr::Hash>), mmr::Error> {
Mmr::generate_proof(block_numbers, best_known_block_number).map(
|(leaves, proof)| {
(
Expand All @@ -2042,7 +2042,7 @@ impl_runtime_apis! {
)
}

fn verify_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 @@ -2055,7 +2055,7 @@ impl_runtime_apis! {
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
8 changes: 4 additions & 4 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 @@ -253,18 +253,18 @@ macro_rules! create_test_api {
fn generate_proof(
_block_numbers: Vec<u64>,
_best_known_block_number: Option<u64>
) -> Result<(Vec<EncodableOpaqueLeaf>, BatchProof<MmrRootHash>), MmrError> {
) -> Result<(Vec<EncodableOpaqueLeaf>, Proof<MmrRootHash>), MmrError> {
unimplemented!()
}

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

fn verify_proof_stateless(
_root: MmrRootHash,
_leaves: Vec<EncodableOpaqueLeaf>,
_proof: BatchProof<MmrRootHash>
_proof: Proof<MmrRootHash>
) -> Result<(), MmrError> {
unimplemented!()
}
Expand Down
18 changes: 9 additions & 9 deletions frame/merkle-mountain-range/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use serde::{Deserialize, Serialize};
use sp_api::{NumberFor, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_mmr_primitives::{BatchProof, Error as MmrError, Proof};
use sp_mmr_primitives::{Error as MmrError, Proof};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};

pub use sp_mmr_primitives::MmrApi as MmrRuntimeApi;
Expand All @@ -51,17 +51,17 @@ pub struct LeavesProof<BlockHash> {
pub block_hash: BlockHash,
/// SCALE-encoded vector of `LeafData`.
pub leaves: Bytes,
/// SCALE-encoded proof data. See [sp_mmr_primitives::BatchProof].
/// SCALE-encoded proof data. See [sp_mmr_primitives::Proof].
pub proof: Bytes,
}

impl<BlockHash> LeavesProof<BlockHash> {
/// Create new `LeavesProof` from a given vector of `Leaf` and a
/// [sp_mmr_primitives::BatchProof].
/// [sp_mmr_primitives::Proof].
pub fn new<Leaf, MmrHash>(
block_hash: BlockHash,
leaves: Vec<Leaf>,
proof: BatchProof<MmrHash>,
proof: Proof<MmrHash>,
) -> Self
where
Leaf: Encode,
Expand Down Expand Up @@ -93,7 +93,7 @@ pub trait MmrApi<BlockHash, BlockNumber> {
/// the leaves). Both parameters are SCALE-encoded.
/// The order of entries in the `leaves` field of the returned struct
/// is the same as the order of the entries in `block_numbers` supplied
#[method(name = "mmr_generateHistoricalBatchProof")]
#[method(name = "mmr_generateProof")]
fn generate_proof(
&self,
block_numbers: Vec<BlockNumber>,
Expand Down Expand Up @@ -185,7 +185,7 @@ mod tests {
fn should_serialize_leaf_proof() {
// given
let leaf = vec![1_u8, 2, 3, 4];
let proof = BatchProof {
let proof = Proof {
leaf_indices: vec![1],
leaf_count: 9,
items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
Expand All @@ -208,7 +208,7 @@ mod tests {
// given
let leaf_a = vec![1_u8, 2, 3, 4];
let leaf_b = vec![2_u8, 2, 3, 4];
let proof = BatchProof {
let proof = Proof {
leaf_indices: vec![1, 2],
leaf_count: 9,
items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
Expand All @@ -233,7 +233,7 @@ mod tests {
block_hash: H256::repeat_byte(0),
leaves: Bytes(vec![vec![1_u8, 2, 3, 4]].encode()),
proof: Bytes(
BatchProof {
Proof {
leaf_indices: vec![1],
leaf_count: 9,
items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
Expand All @@ -260,7 +260,7 @@ mod tests {
block_hash: H256::repeat_byte(0),
leaves: Bytes(vec![vec![1_u8, 2, 3, 4], vec![2_u8, 2, 3, 4]].encode()),
proof: Bytes(
BatchProof {
Proof {
leaf_indices: vec![1, 2],
leaf_count: 9,
items: vec![H256::repeat_byte(1), H256::repeat_byte(2)],
Expand Down
13 changes: 5 additions & 8 deletions frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,15 @@ pub mod pallet {

/// Stateless MMR proof verification for batch of leaves.
///
/// This function can be used to verify received MMR [primitives::BatchProof] (`proof`)
/// This function can be used to verify received MMR [primitives::Proof] (`proof`)
/// for given leaves set (`leaves`) against a known MMR root hash (`root`).
/// Note, the leaves should be sorted such that corresponding leaves and leaf indices have the
/// same position in both the `leaves` vector and the `leaf_indices` vector contained in the
/// [primitives::BatchProof].
/// [primitives::Proof].
pub fn verify_leaves_proof<H, L>(
root: H::Output,
leaves: Vec<mmr::Node<H, L>>,
proof: primitives::BatchProof<H::Output>,
proof: primitives::Proof<H::Output>,
) -> Result<(), primitives::Error>
where
H: traits::Hash,
Expand Down Expand Up @@ -393,10 +393,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn generate_proof(
block_numbers: Vec<T::BlockNumber>,
best_known_block_number: Option<T::BlockNumber>,
) -> Result<
(Vec<LeafOf<T, I>>, primitives::BatchProof<<T as Config<I>>::Hash>),
primitives::Error,
> {
) -> Result<(Vec<LeafOf<T, I>>, primitives::Proof<<T as Config<I>>::Hash>), primitives::Error> {
// check whether best_known_block_number provided, else use current best block
let best_known_block_number =
best_known_block_number.unwrap_or_else(|| <frame_system::Pallet<T>>::block_number());
Expand Down Expand Up @@ -429,7 +426,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// or the proof is invalid.
pub fn verify_leaves(
leaves: Vec<LeafOf<T, I>>,
proof: primitives::BatchProof<<T as Config<I>>::Hash>,
proof: primitives::Proof<<T as Config<I>>::Hash>,
) -> Result<(), primitives::Error> {
if proof.leaf_count > Self::mmr_leaves() ||
proof.leaf_count == 0 ||
Expand Down
12 changes: 6 additions & 6 deletions frame/merkle-mountain-range/src/mmr/mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use sp_std::prelude::*;
/// Stateless verification of the proof for a batch of leaves.
/// Note, the leaves should be sorted such that corresponding leaves and leaf indices have the
/// same position in both the `leaves` vector and the `leaf_indices` vector contained in the
/// [primitives::BatchProof]
/// [primitives::Proof]
pub fn verify_leaves_proof<H, L>(
root: H::Output,
leaves: Vec<Node<H, L>>,
proof: primitives::BatchProof<H::Output>,
proof: primitives::Proof<H::Output>,
) -> Result<bool, Error>
where
H: sp_runtime::traits::Hash,
Expand Down Expand Up @@ -91,11 +91,11 @@ where
/// Verify proof for a set of leaves.
/// Note, the leaves should be sorted such that corresponding leaves and leaf indices have
/// the same position in both the `leaves` vector and the `leaf_indices` vector contained in the
/// [primitives::BatchProof]
/// [primitives::Proof]
pub fn verify_leaves_proof(
&self,
leaves: Vec<L>,
proof: primitives::BatchProof<<T as Config<I>>::Hash>,
proof: primitives::Proof<<T as Config<I>>::Hash>,
) -> Result<bool, Error> {
let p = mmr_lib::MerkleProof::<NodeOf<T, I, L>, Hasher<HashingOf<T, I>, L>>::new(
self.mmr.mmr_size(),
Expand Down Expand Up @@ -166,7 +166,7 @@ where
pub fn generate_proof(
&self,
leaf_indices: Vec<NodeIndex>,
) -> Result<(Vec<L>, primitives::BatchProof<<T as Config<I>>::Hash>), Error> {
) -> Result<(Vec<L>, primitives::Proof<<T as Config<I>>::Hash>), Error> {
let positions = leaf_indices
.iter()
.map(|index| mmr_lib::leaf_index_to_pos(*index))
Expand All @@ -184,7 +184,7 @@ where
self.mmr
.gen_proof(positions)
.map_err(|e| Error::GenerateProof.log_error(e))
.map(|p| primitives::BatchProof {
.map(|p| primitives::Proof {
leaf_indices,
leaf_count,
items: p.proof_items().iter().map(|x| x.hash()).collect(),
Expand Down
22 changes: 11 additions & 11 deletions frame/merkle-mountain-range/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sp_core::{
offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt},
H256,
};
use sp_mmr_primitives::{BatchProof, Compact};
use sp_mmr_primitives::{Compact, Proof};

pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
Expand Down Expand Up @@ -261,7 +261,7 @@ fn should_generate_proofs_correctly() {
proofs[0],
(
vec![Compact::new(((0, H256::repeat_byte(1)).into(), LeafData::new(1).into(),))],
BatchProof {
Proof {
leaf_indices: vec![0],
leaf_count: 7,
items: vec![
Expand All @@ -276,7 +276,7 @@ fn should_generate_proofs_correctly() {
historical_proofs[0][0],
(
vec![Compact::new(((0, H256::repeat_byte(1)).into(), LeafData::new(1).into(),))],
BatchProof { leaf_indices: vec![0], leaf_count: 1, items: vec![] }
Proof { leaf_indices: vec![0], leaf_count: 1, items: vec![] }
)
);

Expand All @@ -292,7 +292,7 @@ fn should_generate_proofs_correctly() {
proofs[2],
(
vec![Compact::new(((2, H256::repeat_byte(3)).into(), LeafData::new(3).into(),))],
BatchProof {
Proof {
leaf_indices: vec![2],
leaf_count: 7,
items: vec![
Expand All @@ -312,7 +312,7 @@ fn should_generate_proofs_correctly() {
historical_proofs[2][0],
(
vec![Compact::new(((2, H256::repeat_byte(3)).into(), LeafData::new(3).into(),))],
BatchProof {
Proof {
leaf_indices: vec![2],
leaf_count: 3,
items: vec![hex(
Expand All @@ -332,7 +332,7 @@ fn should_generate_proofs_correctly() {
historical_proofs[2][2],
(
vec![Compact::new(((2, H256::repeat_byte(3)).into(), LeafData::new(3).into(),))],
BatchProof {
Proof {
leaf_indices: vec![2],
leaf_count: 5,
items: vec![
Expand All @@ -350,7 +350,7 @@ fn should_generate_proofs_correctly() {
(
// NOTE: the leaf index is equivalent to the block number(in this case 5) - 1
vec![Compact::new(((4, H256::repeat_byte(5)).into(), LeafData::new(5).into(),))],
BatchProof {
Proof {
leaf_indices: vec![4],
leaf_count: 7,
items: vec![
Expand All @@ -365,7 +365,7 @@ fn should_generate_proofs_correctly() {
historical_proofs[4][0],
(
vec![Compact::new(((4, H256::repeat_byte(5)).into(), LeafData::new(5).into(),))],
BatchProof {
Proof {
leaf_indices: vec![4],
leaf_count: 5,
items: vec![hex(
Expand All @@ -380,7 +380,7 @@ fn should_generate_proofs_correctly() {
proofs[6],
(
vec![Compact::new(((6, H256::repeat_byte(7)).into(), LeafData::new(7).into(),))],
BatchProof {
Proof {
leaf_indices: vec![6],
leaf_count: 7,
items: vec![
Expand Down Expand Up @@ -411,7 +411,7 @@ fn should_generate_batch_proof_correctly() {
// then
assert_eq!(
proof,
BatchProof {
Proof {
// the leaf indices are equivalent to the above specified block numbers - 1.
leaf_indices: vec![0, 4, 5],
leaf_count: 7,
Expand All @@ -429,7 +429,7 @@ fn should_generate_batch_proof_correctly() {
// then
assert_eq!(
historical_proof,
BatchProof {
Proof {
leaf_indices: vec![0, 4, 5],
leaf_count: 6,
items: vec![
Expand Down
27 changes: 8 additions & 19 deletions primitives/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@ impl<Hash> OnNewRoot<Hash> for () {
fn on_new_root(_root: &Hash) {}
}

/// A MMR proof data for one of the leaves.
#[derive(codec::Encode, codec::Decode, RuntimeDebug, Clone, PartialEq, Eq, TypeInfo)]
pub struct Proof<Hash> {
/// The index of the leaf the proof is for.
pub leaf_index: LeafIndex,
/// Number of leaves in MMR, when the proof was generated.
pub leaf_count: NodeIndex,
/// Proof elements (hashes of siblings of inner nodes on the path to the leaf).
pub items: Vec<Hash>,
}

/// A full leaf content stored in the offchain-db.
pub trait FullLeaf: Clone + PartialEq + fmt::Debug {
/// Encode the leaf either in its full or compact form.
Expand Down Expand Up @@ -354,7 +343,7 @@ impl_leaf_data_for_tuple!(A:0, B:1, C:2, D:3, E:4);

/// A MMR proof data for a group of leaves.
#[derive(codec::Encode, codec::Decode, RuntimeDebug, Clone, PartialEq, Eq, TypeInfo)]
pub struct BatchProof<Hash> {
pub struct Proof<Hash> {
/// The indices of the leaves the proof is for.
pub leaf_indices: Vec<LeafIndex>,
/// Number of leaves in MMR, when the proof was generated.
Expand Down Expand Up @@ -424,23 +413,23 @@ sp_api::decl_runtime_apis! {
fn generate_proof(
block_numbers: Vec<BlockNumber>,
best_known_block_number: Option<BlockNumber>
) -> Result<(Vec<EncodableOpaqueLeaf>, BatchProof<Hash>), Error>;
) -> Result<(Vec<EncodableOpaqueLeaf>, Proof<Hash>), Error>;

/// Verify MMR proof against on-chain MMR for a batch of leaves.
///
/// Note this function will use on-chain MMR root hash and check if the proof matches the hash.
/// Note, the leaves should be sorted such that corresponding leaves and leaf indices have the
/// same position in both the `leaves` vector and the `leaf_indices` vector contained in the [BatchProof]
fn verify_proof(leaves: Vec<EncodableOpaqueLeaf>, proof: BatchProof<Hash>) -> Result<(), Error>;
/// same position in both the `leaves` vector and the `leaf_indices` vector contained in the [Proof]
fn verify_proof(leaves: Vec<EncodableOpaqueLeaf>, proof: Proof<Hash>) -> Result<(), Error>;

/// Verify MMR proof against given root hash for a batch of leaves.
///
/// Note this function does not require any on-chain storage - the
/// proof is verified against given MMR root hash.
///
/// Note, the leaves should be sorted such that corresponding leaves and leaf indices have the
/// same position in both the `leaves` vector and the `leaf_indices` vector contained in the [BatchProof]
fn verify_proof_stateless(root: Hash, leaves: Vec<EncodableOpaqueLeaf>, proof: BatchProof<Hash>)
/// same position in both the `leaves` vector and the `leaf_indices` vector contained in the [Proof]
fn verify_proof_stateless(root: Hash, leaves: Vec<EncodableOpaqueLeaf>, proof: Proof<Hash>)
-> Result<(), Error>;
}
}
Expand All @@ -459,12 +448,12 @@ mod tests {

type Test = DataOrHash<Keccak256, String>;
type TestCompact = Compact<Keccak256, (Test, Test)>;
type TestProof = BatchProof<<Keccak256 as traits::Hash>::Output>;
type TestProof = Proof<<Keccak256 as traits::Hash>::Output>;

#[test]
fn should_encode_decode_proof() {
// given
let proof: TestProof = BatchProof {
let proof: TestProof = Proof {
leaf_indices: vec![5],
leaf_count: 10,
items: vec![
Expand Down

0 comments on commit 2657723

Please sign in to comment.