Skip to content

Commit

Permalink
Expose MMR root through runtime API - use it in BEEFY client (parityt…
Browse files Browse the repository at this point in the history
…ech#11183)

* beefy-gadget: allow custom runtime api provider

* beefy-gadget: use mock runtime api in tests

* pallet-mmr: expose mmr root from state through runtime API

* beefy-gadget: get mmr root from runtime state

* pallet-beefy-mmr: remove MmrRoot from header digests

* frame/mmr: move mmr primitives out of frame

* frame/mmr: completely move primitives out of frame

* address review comments

* beefy-mmr: bring back mmr root from header digest

* clippy fixes for rustc 1.60

* address review comments
  • Loading branch information
acatangiu authored and ark0f committed Feb 27, 2023
1 parent 529895b commit 8a9e69a
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 335 deletions.
37 changes: 18 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ members = [
"frame/lottery",
"frame/membership",
"frame/merkle-mountain-range",
"frame/merkle-mountain-range/primitives",
"frame/merkle-mountain-range/rpc",
"frame/multisig",
"frame/nicks",
Expand Down Expand Up @@ -172,6 +171,7 @@ members = [
"primitives/keyring",
"primitives/keystore",
"primitives/maybe-compressed-blob",
"primitives/merkle-mountain-range",
"primitives/npos-elections",
"primitives/npos-elections/fuzzer",
"primitives/offchain",
Expand Down
6 changes: 5 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ impl pallet_mmr::Config for Runtime {
const INDEXING_PREFIX: &'static [u8] = b"mmr";
type Hashing = <Runtime as frame_system::Config>::Hashing;
type Hash = <Runtime as frame_system::Config>::Hash;
type LeafData = frame_system::Pallet<Self>;
type LeafData = pallet_mmr::ParentNumberAndHash<Self>;
type OnNewRoot = ();
type WeightInfo = ();
}
Expand Down Expand Up @@ -1804,6 +1804,10 @@ impl_runtime_apis! {
let node = mmr::DataOrHash::Data(leaf.into_opaque_leaf());
pallet_mmr::verify_leaf_proof::<mmr::Hashing, _>(root, node, proof)
}

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

impl sp_session::SessionKeys<Block> for Runtime {
Expand Down
1 change: 1 addition & 0 deletions client/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
sp-consensus = { version = "0.10.0-dev", path = "../../primitives/consensus/common" }
sp-core = { version = "6.0.0", path = "../../primitives/core" }
sp-keystore = { version = "0.12.0", path = "../../primitives/keystore" }
sp-mmr-primitives = { version = "4.0.0-dev", path = "../../primitives/merkle-mountain-range" }
sp-runtime = { version = "6.0.0", path = "../../primitives/runtime" }

sc-chain-spec = { version = "4.0.0-dev", path = "../../client/chain-spec" }
Expand Down
22 changes: 14 additions & 8 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_consensus::SyncOracle;
use sp_keystore::SyncCryptoStorePtr;
use sp_mmr_primitives::MmrApi;
use sp_runtime::traits::Block;

use beefy_primitives::BeefyApi;
use beefy_primitives::{BeefyApi, MmrRootHash};

use crate::notification::{BeefyBestBlockSender, BeefySignedCommitmentSender};

Expand Down Expand Up @@ -87,7 +88,7 @@ pub fn beefy_peers_set_config(
/// of today, Rust does not allow a type alias to be used as a trait bound. Tracking
/// issue is <https://github.com/rust-lang/rust/issues/41517>.
pub trait Client<B, BE>:
BlockchainEvents<B> + HeaderBackend<B> + Finalizer<B, BE> + ProvideRuntimeApi<B> + Send + Sync
BlockchainEvents<B> + HeaderBackend<B> + Finalizer<B, BE> + Send + Sync
where
B: Block,
BE: Backend<B>,
Expand All @@ -110,18 +111,21 @@ where
}

/// BEEFY gadget initialization parameters.
pub struct BeefyParams<B, BE, C, N>
pub struct BeefyParams<B, BE, C, N, R>
where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
C::Api: BeefyApi<B>,
R: ProvideRuntimeApi<B>,
R::Api: BeefyApi<B> + MmrApi<B, MmrRootHash>,
N: GossipNetwork<B> + Clone + SyncOracle + Send + Sync + 'static,
{
/// BEEFY client
pub client: Arc<C>,
/// Client Backend
pub backend: Arc<BE>,
/// Runtime Api Provider
pub runtime: Arc<R>,
/// Local key store
pub key_store: Option<SyncCryptoStorePtr>,
/// Gossip network
Expand All @@ -138,21 +142,22 @@ where
pub protocol_name: std::borrow::Cow<'static, str>,
}

#[cfg(not(test))]
/// Start the BEEFY gadget.
///
/// This is a thin shim around running and awaiting a BEEFY worker.
pub async fn start_beefy_gadget<B, BE, C, N>(beefy_params: BeefyParams<B, BE, C, N>)
pub async fn start_beefy_gadget<B, BE, C, N, R>(beefy_params: BeefyParams<B, BE, C, N, R>)
where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
C::Api: BeefyApi<B>,
R: ProvideRuntimeApi<B>,
R::Api: BeefyApi<B> + MmrApi<B, MmrRootHash>,
N: GossipNetwork<B> + Clone + SyncOracle + Send + Sync + 'static,
{
let BeefyParams {
client,
backend,
runtime,
key_store,
network,
signed_commitment_sender,
Expand Down Expand Up @@ -188,6 +193,7 @@ where
let worker_params = worker::WorkerParams {
client,
backend,
runtime,
key_store: key_store.into(),
signed_commitment_sender,
beefy_best_block_sender,
Expand All @@ -198,7 +204,7 @@ where
sync_oracle,
};

let worker = worker::BeefyWorker::<_, _, _, _>::new(worker_params);
let worker = worker::BeefyWorker::<_, _, _, _, _>::new(worker_params);

worker.run().await
}
5 changes: 1 addition & 4 deletions client/beefy/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

//! BEEFY Prometheus metrics definition
#[cfg(not(test))]
use prometheus::{register, PrometheusError, Registry};
use prometheus::{Counter, Gauge, U64};
use prometheus::{register, Counter, Gauge, PrometheusError, Registry, U64};

/// BEEFY metrics exposed through Prometheus
pub(crate) struct Metrics {
Expand All @@ -39,7 +37,6 @@ pub(crate) struct Metrics {
}

impl Metrics {
#[cfg(not(test))]
pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
beefy_validator_set_id: register(
Expand Down
Loading

0 comments on commit 8a9e69a

Please sign in to comment.