Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 2b4818a

Browse files
authored
Expose MMR root through runtime API - use it in BEEFY client (paritytech#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
1 parent de8b34c commit 2b4818a

21 files changed

Lines changed: 403 additions & 335 deletions

File tree

Cargo.lock

Lines changed: 18 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ members = [
101101
"frame/lottery",
102102
"frame/membership",
103103
"frame/merkle-mountain-range",
104-
"frame/merkle-mountain-range/primitives",
105104
"frame/merkle-mountain-range/rpc",
106105
"frame/multisig",
107106
"frame/nicks",
@@ -172,6 +171,7 @@ members = [
172171
"primitives/keyring",
173172
"primitives/keystore",
174173
"primitives/maybe-compressed-blob",
174+
"primitives/merkle-mountain-range",
175175
"primitives/npos-elections",
176176
"primitives/npos-elections/fuzzer",
177177
"primitives/offchain",

bin/node/runtime/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ impl pallet_mmr::Config for Runtime {
12611261
const INDEXING_PREFIX: &'static [u8] = b"mmr";
12621262
type Hashing = <Runtime as frame_system::Config>::Hashing;
12631263
type Hash = <Runtime as frame_system::Config>::Hash;
1264-
type LeafData = frame_system::Pallet<Self>;
1264+
type LeafData = pallet_mmr::ParentNumberAndHash<Self>;
12651265
type OnNewRoot = ();
12661266
type WeightInfo = ();
12671267
}
@@ -1804,6 +1804,10 @@ impl_runtime_apis! {
18041804
let node = mmr::DataOrHash::Data(leaf.into_opaque_leaf());
18051805
pallet_mmr::verify_leaf_proof::<mmr::Hashing, _>(root, node, proof)
18061806
}
1807+
1808+
fn mmr_root() -> Result<mmr::Hash, mmr::Error> {
1809+
Ok(Mmr::mmr_root())
1810+
}
18071811
}
18081812

18091813
impl sp_session::SessionKeys<Block> for Runtime {

client/beefy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
2727
sp-consensus = { version = "0.10.0-dev", path = "../../primitives/consensus/common" }
2828
sp-core = { version = "6.0.0", path = "../../primitives/core" }
2929
sp-keystore = { version = "0.12.0", path = "../../primitives/keystore" }
30+
sp-mmr-primitives = { version = "4.0.0-dev", path = "../../primitives/merkle-mountain-range" }
3031
sp-runtime = { version = "6.0.0", path = "../../primitives/runtime" }
3132

3233
sc-chain-spec = { version = "4.0.0-dev", path = "../../client/chain-spec" }

client/beefy/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ use sp_api::ProvideRuntimeApi;
2727
use sp_blockchain::HeaderBackend;
2828
use sp_consensus::SyncOracle;
2929
use sp_keystore::SyncCryptoStorePtr;
30+
use sp_mmr_primitives::MmrApi;
3031
use sp_runtime::traits::Block;
3132

32-
use beefy_primitives::BeefyApi;
33+
use beefy_primitives::{BeefyApi, MmrRootHash};
3334

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

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

112113
/// BEEFY gadget initialization parameters.
113-
pub struct BeefyParams<B, BE, C, N>
114+
pub struct BeefyParams<B, BE, C, N, R>
114115
where
115116
B: Block,
116117
BE: Backend<B>,
117118
C: Client<B, BE>,
118-
C::Api: BeefyApi<B>,
119+
R: ProvideRuntimeApi<B>,
120+
R::Api: BeefyApi<B> + MmrApi<B, MmrRootHash>,
119121
N: GossipNetwork<B> + Clone + SyncOracle + Send + Sync + 'static,
120122
{
121123
/// BEEFY client
122124
pub client: Arc<C>,
123125
/// Client Backend
124126
pub backend: Arc<BE>,
127+
/// Runtime Api Provider
128+
pub runtime: Arc<R>,
125129
/// Local key store
126130
pub key_store: Option<SyncCryptoStorePtr>,
127131
/// Gossip network
@@ -138,21 +142,22 @@ where
138142
pub protocol_name: std::borrow::Cow<'static, str>,
139143
}
140144

141-
#[cfg(not(test))]
142145
/// Start the BEEFY gadget.
143146
///
144147
/// This is a thin shim around running and awaiting a BEEFY worker.
145-
pub async fn start_beefy_gadget<B, BE, C, N>(beefy_params: BeefyParams<B, BE, C, N>)
148+
pub async fn start_beefy_gadget<B, BE, C, N, R>(beefy_params: BeefyParams<B, BE, C, N, R>)
146149
where
147150
B: Block,
148151
BE: Backend<B>,
149152
C: Client<B, BE>,
150-
C::Api: BeefyApi<B>,
153+
R: ProvideRuntimeApi<B>,
154+
R::Api: BeefyApi<B> + MmrApi<B, MmrRootHash>,
151155
N: GossipNetwork<B> + Clone + SyncOracle + Send + Sync + 'static,
152156
{
153157
let BeefyParams {
154158
client,
155159
backend,
160+
runtime,
156161
key_store,
157162
network,
158163
signed_commitment_sender,
@@ -188,6 +193,7 @@ where
188193
let worker_params = worker::WorkerParams {
189194
client,
190195
backend,
196+
runtime,
191197
key_store: key_store.into(),
192198
signed_commitment_sender,
193199
beefy_best_block_sender,
@@ -198,7 +204,7 @@ where
198204
sync_oracle,
199205
};
200206

201-
let worker = worker::BeefyWorker::<_, _, _, _>::new(worker_params);
207+
let worker = worker::BeefyWorker::<_, _, _, _, _>::new(worker_params);
202208

203209
worker.run().await
204210
}

client/beefy/src/metrics.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
//! BEEFY Prometheus metrics definition
2020
21-
#[cfg(not(test))]
22-
use prometheus::{register, PrometheusError, Registry};
23-
use prometheus::{Counter, Gauge, U64};
21+
use prometheus::{register, Counter, Gauge, PrometheusError, Registry, U64};
2422

2523
/// BEEFY metrics exposed through Prometheus
2624
pub(crate) struct Metrics {
@@ -39,7 +37,6 @@ pub(crate) struct Metrics {
3937
}
4038

4139
impl Metrics {
42-
#[cfg(not(test))]
4340
pub(crate) fn register(registry: &Registry) -> Result<Self, PrometheusError> {
4441
Ok(Self {
4542
beefy_validator_set_id: register(

0 commit comments

Comments
 (0)