mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-18 07:05:42 +00:00
Add pluggable BEEFY payload constructors (#12428)
* primitives/beefy: move Payload to its own file * primitives/beefy: add Payload tests * primitives/beefy: add MmrRootProvider as custom BEEFY payload provider * client/beefy: use generic BEEFY 'PayloadProvider' * primitives/beefy: rename Payload::new to Payload::from_single_entry for clarity * fix visibility * fix cargo doc
This commit is contained in:
@@ -17,8 +17,8 @@
|
||||
|
||||
//! BEEFY + MMR utilties.
|
||||
//!
|
||||
//! While BEEFY can be used completely indepentently as an additional consensus gadget,
|
||||
//! it is designed around a main use case of making bridging standalone networks together.
|
||||
//! While BEEFY can be used completely independently as an additional consensus gadget,
|
||||
//! it is designed around a main use case of bridging standalone networks together.
|
||||
//! For that use case it's common to use some aggregated data structure (like MMR) to be
|
||||
//! used in conjunction with BEEFY, to be able to efficiently prove any past blockchain data.
|
||||
//!
|
||||
@@ -26,9 +26,13 @@
|
||||
//! but we imagine they will be useful for other chains that either want to bridge with Polkadot
|
||||
//! or are completely standalone, but heavily inspired by Polkadot.
|
||||
|
||||
use crate::Vec;
|
||||
use crate::{crypto::AuthorityId, ConsensusLog, MmrRootHash, Vec, BEEFY_ENGINE_ID};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_runtime::{
|
||||
generic::OpaqueDigestItemId,
|
||||
traits::{Block, Header},
|
||||
};
|
||||
|
||||
/// A provider for extra data that gets added to the Mmr leaf
|
||||
pub trait BeefyDataProvider<ExtraData> {
|
||||
@@ -121,9 +125,78 @@ pub struct BeefyAuthoritySet<MerkleRoot> {
|
||||
/// Details of the next BEEFY authority set.
|
||||
pub type BeefyNextAuthoritySet<MerkleRoot> = BeefyAuthoritySet<MerkleRoot>;
|
||||
|
||||
/// Extract the MMR root hash from a digest in the given header, if it exists.
|
||||
pub fn find_mmr_root_digest<B: Block>(header: &B::Header) -> Option<MmrRootHash> {
|
||||
let id = OpaqueDigestItemId::Consensus(&BEEFY_ENGINE_ID);
|
||||
|
||||
let filter = |log: ConsensusLog<AuthorityId>| match log {
|
||||
ConsensusLog::MmrRoot(root) => Some(root),
|
||||
_ => None,
|
||||
};
|
||||
header.digest().convert_first(|l| l.try_to(id).and_then(filter))
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use mmr_root_provider::MmrRootProvider;
|
||||
#[cfg(feature = "std")]
|
||||
mod mmr_root_provider {
|
||||
use super::*;
|
||||
use crate::{known_payloads, payload::PayloadProvider, Payload};
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_mmr_primitives::MmrApi;
|
||||
use sp_runtime::generic::BlockId;
|
||||
use sp_std::{marker::PhantomData, sync::Arc};
|
||||
|
||||
/// A [`crate::Payload`] provider where payload is Merkle Mountain Range root hash.
|
||||
///
|
||||
/// Encoded payload contains a [`crate::MmrRootHash`] type (i.e. 32-bytes hash).
|
||||
pub struct MmrRootProvider<B, R> {
|
||||
runtime: Arc<R>,
|
||||
_phantom: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl<B, R> MmrRootProvider<B, R>
|
||||
where
|
||||
B: Block,
|
||||
R: ProvideRuntimeApi<B>,
|
||||
R::Api: MmrApi<B, MmrRootHash>,
|
||||
{
|
||||
/// Create new BEEFY Payload provider with MMR Root as payload.
|
||||
pub fn new(runtime: Arc<R>) -> Self {
|
||||
Self { runtime, _phantom: PhantomData }
|
||||
}
|
||||
|
||||
/// Simple wrapper that gets MMR root from header digests or from client state.
|
||||
fn mmr_root_from_digest_or_runtime(&self, header: &B::Header) -> Option<MmrRootHash> {
|
||||
find_mmr_root_digest::<B>(header).or_else(|| {
|
||||
self.runtime
|
||||
.runtime_api()
|
||||
.mmr_root(&BlockId::hash(header.hash()))
|
||||
.ok()
|
||||
.and_then(|r| r.ok())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Block, R> PayloadProvider<B> for MmrRootProvider<B, R>
|
||||
where
|
||||
B: Block,
|
||||
R: ProvideRuntimeApi<B>,
|
||||
R::Api: MmrApi<B, MmrRootHash>,
|
||||
{
|
||||
fn payload(&self, header: &B::Header) -> Option<Payload> {
|
||||
self.mmr_root_from_digest_or_runtime(header).map(|mmr_root| {
|
||||
Payload::from_single_entry(known_payloads::MMR_ROOT_ID, mmr_root.encode())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::H256;
|
||||
use sp_runtime::{traits::BlakeTwo256, Digest, DigestItem, OpaqueExtrinsic};
|
||||
|
||||
#[test]
|
||||
fn should_construct_version_correctly() {
|
||||
@@ -147,4 +220,30 @@ mod tests {
|
||||
fn should_panic_if_minor_too_large() {
|
||||
MmrLeafVersion::new(0, 32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_mmr_root_digest() {
|
||||
type Header = sp_runtime::generic::Header<u64, BlakeTwo256>;
|
||||
type Block = sp_runtime::generic::Block<Header, OpaqueExtrinsic>;
|
||||
let mut header = Header::new(
|
||||
1u64,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
Digest::default(),
|
||||
);
|
||||
|
||||
// verify empty digest shows nothing
|
||||
assert!(find_mmr_root_digest::<Block>(&header).is_none());
|
||||
|
||||
let mmr_root_hash = H256::random();
|
||||
header.digest_mut().push(DigestItem::Consensus(
|
||||
BEEFY_ENGINE_ID,
|
||||
ConsensusLog::<AuthorityId>::MmrRoot(mmr_root_hash).encode(),
|
||||
));
|
||||
|
||||
// verify validator set is correctly extracted from digest
|
||||
let extracted = find_mmr_root_digest::<Block>(&header);
|
||||
assert_eq!(extracted, Some(mmr_root_hash));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user