pallet-beefy-mmr: add API for BEEFY Authority Sets (#11406)

* pallet-beefy: add Config::OnNewValidatorSet type

Add a hook to pallet-beefy for doing specific work when
BEEFY validator set changes.

For example, this can be used by pallet-beefy-mmr to cache
a lightweight MMR root over validators and make it available
to light clients.

* pallet-beefy-mmr: implement OnNewValidatorSet

Implement pallet-beefy::OnNewValidatorSet to be notified of BEEFY
validator set changes. Use the notifications to compute and cache
a light weight 'BEEFY authority set' which is an MMR root over
BEEFY validator set plus some extra info.

Previously, pallet-beefy-mmr was interogating pallet-beefy about
validator set id on every block to find out when it needs to recompute
the authority set.
By using the event-driven approach in this commit, we also save one
extra state interogation per block.

* pallet-beefy-mmr: add new authority_set() API

Expose current and next BEEFY authority sets through runtime API.
These can be directly used by light clients to avoid having them
compute them themselves based on BEEFY validator sets.

Signed-off-by: acatangiu <adrian@parity.io>

* rename BeefyMmr exposed runtime api
This commit is contained in:
Adrian Catangiu
2022-06-22 21:39:24 +03:00
committed by GitHub
parent 7080662f8f
commit 3018051cdd
12 changed files with 205 additions and 38 deletions
@@ -13,6 +13,9 @@ hex = { version = "0.4", default-features = false, optional = true }
log = { version = "0.4", default-features = false, optional = true }
tiny-keccak = { version = "2.0.2", features = ["keccak"], optional = true }
beefy-primitives = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/beefy" }
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/api" }
[dev-dependencies]
env_logger = "0.9"
hex = "0.4"
@@ -22,4 +25,7 @@ hex-literal = "0.3"
debug = ["hex", "hex/std", "log"]
default = ["debug", "keccak", "std"]
keccak = ["tiny-keccak"]
std = []
std = [
"beefy-primitives/std",
"sp-api/std"
]
@@ -36,6 +36,8 @@ extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use beefy_primitives::mmr::{BeefyAuthoritySet, BeefyNextAuthoritySet};
/// Supported hashing output size.
///
/// The size is restricted to 32 bytes to allow for a more optimised implementation.
@@ -375,6 +377,21 @@ where
}
}
sp_api::decl_runtime_apis! {
/// API useful for BEEFY light clients.
pub trait BeefyMmrApi<H>
where
H: From<Hash> + Into<Hash>,
BeefyAuthoritySet<H>: sp_api::Decode,
{
/// Return the currently active BEEFY authority set proof.
fn authority_set_proof() -> BeefyAuthoritySet<H>;
/// Return the next/queued BEEFY authority set proof.
fn next_authority_set_proof() -> BeefyNextAuthoritySet<H>;
}
}
#[cfg(test)]
mod tests {
use super::*;