Merkle Mountain Range pallet improvements (#7891)

* Add stateless verification helper function.

* Split MMR primitives.

* Add RuntimeAPI

* RuntimeAPI with OpaqueLeaves

* Bump spec_version,.

* Move primitives back to frame.

* Fix OpaqueLeaf encoding.

* Add block number to frame_system implementation of LeafDataProvider.

* Relax leaf codec requirements and fix OpaqueLeaf

* Add root to debug line.

* Apply suggestions from code review

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* Typo.

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
This commit is contained in:
Tomasz Drwięga
2021-01-28 11:58:52 +01:00
committed by GitHub
parent fa23de2c03
commit 6c2dd28dfb
12 changed files with 373 additions and 93 deletions
+38 -1
View File
@@ -112,7 +112,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 261,
spec_version: 262,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
@@ -1079,6 +1079,20 @@ pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExt
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllModules>;
/// MMR helper types.
mod mmr {
use super::Runtime;
pub use pallet_mmr::primitives::*;
pub type Leaf = <
<Runtime as pallet_mmr::Config>::LeafData
as
LeafDataProvider
>::LeafData;
pub type Hash = <Runtime as pallet_mmr::Config>::Hash;
pub type Hashing = <Runtime as pallet_mmr::Config>::Hashing;
}
impl_runtime_apis! {
impl sp_api::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
@@ -1273,6 +1287,29 @@ impl_runtime_apis! {
}
}
impl pallet_mmr::primitives::MmrApi<
Block,
mmr::Leaf,
mmr::Hash,
> for Runtime {
fn generate_proof(leaf_index: u64) -> Result<(mmr::Leaf, mmr::Proof<mmr::Hash>), mmr::Error> {
Mmr::generate_proof(leaf_index)
}
fn verify_proof(leaf: mmr::Leaf, proof: mmr::Proof<mmr::Hash>) -> Result<(), mmr::Error> {
Mmr::verify_leaf(leaf, proof)
}
fn verify_proof_stateless(
root: mmr::Hash,
leaf: Vec<u8>,
proof: mmr::Proof<mmr::Hash>
) -> Result<(), mmr::Error> {
let node = mmr::DataOrHash::Data(mmr::OpaqueLeaf(leaf));
pallet_mmr::verify_leaf_proof::<mmr::Hashing, _>(root, node, proof)
}
}
impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)