Skip to content

Commit

Permalink
GRANDPA module: store accepted justifications (paritytech#2298) (pari…
Browse files Browse the repository at this point in the history
…tytech#2301)

Store accepted justifications in events.
  • Loading branch information
serban300 authored Jul 27, 2023
1 parent f1783a9 commit 81df7da
Show file tree
Hide file tree
Showing 27 changed files with 193 additions and 63 deletions.
1 change: 1 addition & 0 deletions bin/millau/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ scale-info = { version = "2.9.0", default-features = false, features = ["derive"

# Bridge dependencies

bp-header-chain = { path = "../../../primitives/header-chain", default-features = false }
bp-messages = { path = "../../../primitives/messages", default-features = false }
bp-millau = { path = "../../../primitives/chain-millau", default-features = false }
bp-parachains = { path = "../../../primitives/parachains", default-features = false }
Expand Down
10 changes: 10 additions & 0 deletions bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,22 @@ impl_runtime_apis! {
fn best_finalized() -> Option<HeaderId<bp_rialto::Hash, bp_rialto::BlockNumber>> {
BridgeRialtoGrandpa::best_finalized()
}

fn accepted_grandpa_finality_proofs(
) -> Vec<bp_header_chain::justification::GrandpaJustification<bp_rialto::Header>> {
BridgeRialtoGrandpa::accepted_finality_proofs()
}
}

impl bp_westend::WestendFinalityApi<Block> for Runtime {
fn best_finalized() -> Option<HeaderId<bp_westend::Hash, bp_westend::BlockNumber>> {
BridgeWestendGrandpa::best_finalized()
}

fn accepted_grandpa_finality_proofs(
) -> Vec<bp_header_chain::justification::GrandpaJustification<bp_westend::Header>> {
BridgeWestendGrandpa::accepted_finality_proofs()
}
}

impl bp_westend::AssetHubWestendFinalityApi<Block> for Runtime {
Expand Down
1 change: 1 addition & 0 deletions bin/rialto-parachain/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ scale-info = { version = "2.9.0", default-features = false, features = ["derive"

# Bridge depedencies

bp-header-chain = { path = "../../../primitives/header-chain", default-features = false }
bp-messages = { path = "../../../primitives/messages", default-features = false }
bp-millau = { path = "../../../primitives/chain-millau", default-features = false }
bp-relayers = { path = "../../../primitives/relayers", default-features = false }
Expand Down
5 changes: 5 additions & 0 deletions bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,11 @@ impl_runtime_apis! {
fn best_finalized() -> Option<HeaderId<bp_millau::Hash, bp_millau::BlockNumber>> {
BridgeMillauGrandpa::best_finalized()
}

fn accepted_grandpa_finality_proofs(
) -> Vec<bp_header_chain::justification::GrandpaJustification<bp_millau::Header>> {
BridgeMillauGrandpa::accepted_finality_proofs()
}
}

impl bp_millau::ToMillauOutboundLaneApi<Block> for Runtime {
Expand Down
1 change: 1 addition & 0 deletions bin/rialto/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ scale-info = { version = "2.9.0", default-features = false, features = ["derive"

# Bridge dependencies

bp-header-chain = { path = "../../../primitives/header-chain", default-features = false }
bp-messages = { path = "../../../primitives/messages", default-features = false }
bp-millau = { path = "../../../primitives/chain-millau", default-features = false }
bp-relayers = { path = "../../../primitives/relayers", default-features = false }
Expand Down
5 changes: 5 additions & 0 deletions bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,11 @@ impl_runtime_apis! {
fn best_finalized() -> Option<HeaderId<bp_millau::Hash, bp_millau::BlockNumber>> {
BridgeMillauGrandpa::best_finalized()
}

fn accepted_grandpa_finality_proofs(
) -> Vec<bp_header_chain::justification::GrandpaJustification<bp_millau::Header>> {
BridgeMillauGrandpa::accepted_finality_proofs()
}
}

impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
Expand Down
28 changes: 22 additions & 6 deletions modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use sp_runtime::{
traits::{Header as HeaderT, Zero},
SaturatedConversion,
};
use sp_std::{boxed::Box, convert::TryInto};
use sp_std::{boxed::Box, convert::TryInto, prelude::*};

mod call_ext;
#[cfg(test)]
Expand Down Expand Up @@ -237,7 +237,7 @@ pub mod pallet {
let actual_weight = pre_dispatch_weight
.set_proof_size(pre_dispatch_weight.proof_size().saturating_sub(unused_proof_size));

Self::deposit_event(Event::UpdatedBestFinalizedHeader { number, hash });
Self::deposit_event(Event::UpdatedBestFinalizedHeader { number, hash, justification });

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee })
}
Expand Down Expand Up @@ -402,6 +402,8 @@ pub mod pallet {
UpdatedBestFinalizedHeader {
number: BridgedBlockNumber<T, I>,
hash: BridgedBlockHash<T, I>,
/// Justification.
justification: GrandpaJustification<BridgedHeader<T, I>>,
},
}

Expand Down Expand Up @@ -603,10 +605,22 @@ pub mod pallet {
}
}

impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Get the best finalized block number.
pub fn best_finalized_number() -> Option<BridgedBlockNumber<T, I>> {
BestFinalized::<T, I>::get().map(|id| id.number())
impl<T: Config<I>, I: 'static> Pallet<T, I>
where
<T as frame_system::Config>::RuntimeEvent: TryInto<Event<T, I>>,
{
/// Get the GRANDPA justifications accepted in the current block.
pub fn accepted_finality_proofs() -> Vec<GrandpaJustification<BridgedHeader<T, I>>> {
frame_system::Pallet::<T>::read_events_no_consensus()
.filter_map(|event| {
if let Event::<T, I>::UpdatedBestFinalizedHeader { justification, .. } =
event.event.try_into().ok()?
{
return Some(justification)
}
None
})
.collect()
}
}

Expand Down Expand Up @@ -913,10 +927,12 @@ mod tests {
event: TestEvent::Grandpa(Event::UpdatedBestFinalizedHeader {
number: *header.number(),
hash: header.hash(),
justification: justification.clone(),
}),
topics: vec![],
}],
);
assert_eq!(Pallet::<TestRuntime>::accepted_finality_proofs(), vec![justification]);
})
}

Expand Down
24 changes: 16 additions & 8 deletions modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,13 @@ pub(crate) mod tests {
use super::*;
use crate::mock::{
run_test, test_relay_header, BigParachainHeader, RegularParachainHasher,
RegularParachainHeader, RuntimeEvent as TestEvent, RuntimeOrigin, TestRuntime,
UNTRACKED_PARACHAIN_ID,
RegularParachainHeader, RelayBlockHeader, RuntimeEvent as TestEvent, RuntimeOrigin,
TestRuntime, UNTRACKED_PARACHAIN_ID,
};
use bp_test_utils::prepare_parachain_heads_proof;
use codec::Encode;

use bp_header_chain::justification::GrandpaJustification;
use bp_parachains::{
BestParaHeadHash, BridgeParachainCall, ImportedParaHeadsKeyProvider, ParasInfoKeyProvider,
};
Expand Down Expand Up @@ -740,7 +741,10 @@ pub(crate) mod tests {
test_relay_header(0, state_root).hash()
}

fn proceed(num: RelayBlockNumber, state_root: RelayBlockHash) -> ParaHash {
fn proceed(
num: RelayBlockNumber,
state_root: RelayBlockHash,
) -> (ParaHash, GrandpaJustification<RelayBlockHeader>) {
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::on_initialize(
0,
);
Expand All @@ -752,11 +756,11 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Pallet::<TestRuntime, BridgesGrandpaPalletInstance>::submit_finality_proof(
RuntimeOrigin::signed(1),
Box::new(header),
justification,
justification.clone(),
)
);

hash
(hash, justification)
}

fn initial_best_head(parachain: u32) -> ParaInfo {
Expand Down Expand Up @@ -993,7 +997,7 @@ pub(crate) mod tests {
);

// import head#10 of parachain#1 at relay block #1
let relay_1_hash = proceed(1, state_root_10);
let (relay_1_hash, justification) = proceed(1, state_root_10);
assert_ok!(import_parachain_1_head(1, state_root_10, parachains_10, proof_10));
assert_eq!(
ParasInfo::<TestRuntime>::get(ParaId(1)),
Expand Down Expand Up @@ -1032,6 +1036,7 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
justification
}
),
topics: vec![],
Expand Down Expand Up @@ -1149,7 +1154,7 @@ pub(crate) mod tests {

// try to import head#0 of parachain#1 at relay block#1
// => call succeeds, but nothing is changed
let relay_1_hash = proceed(1, state_root);
let (relay_1_hash, justification) = proceed(1, state_root);
assert_ok!(import_parachain_1_head(1, state_root, parachains, proof));
assert_eq!(ParasInfo::<TestRuntime>::get(ParaId(1)), Some(initial_best_head(1)));
assert_eq!(
Expand All @@ -1169,6 +1174,7 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
justification
}
),
topics: vec![],
Expand Down Expand Up @@ -1197,7 +1203,7 @@ pub(crate) mod tests {
initialize(state_root_5);

// head#10 of parachain#1 at relay block#1
let relay_1_hash = proceed(1, state_root_10);
let (relay_1_hash, justification) = proceed(1, state_root_10);
assert_ok!(import_parachain_1_head(1, state_root_10, parachains_10, proof_10));
assert_eq!(
ParasInfo::<TestRuntime>::get(ParaId(1)),
Expand All @@ -1218,6 +1224,7 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
justification: justification.clone()
}
),
topics: vec![],
Expand Down Expand Up @@ -1255,6 +1262,7 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
justification
}
),
topics: vec![],
Expand Down
2 changes: 1 addition & 1 deletion primitives/chain-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ pub const WITH_KUSAMA_GRANDPA_PALLET_NAME: &str = "BridgeKusamaGrandpa";
/// reserve.
pub const MAX_NESTED_PARACHAIN_HEAD_DATA_SIZE: u32 = 128;

decl_bridge_finality_runtime_apis!(kusama);
decl_bridge_finality_runtime_apis!(kusama, grandpa);
4 changes: 2 additions & 2 deletions primitives/chain-millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use bp_header_chain::ChainWithGrandpa;
use bp_messages::{
InboundMessageDetails, LaneId, MessageNonce, MessagePayload, OutboundMessageDetails,
};
use bp_runtime::{decl_bridge_runtime_apis, Chain};
use bp_runtime::{decl_bridge_finality_runtime_apis, decl_bridge_runtime_apis, Chain};
use frame_support::{
dispatch::DispatchClass,
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, IdentityFee, Weight},
Expand Down Expand Up @@ -244,4 +244,4 @@ pub const WITH_MILLAU_MESSAGES_PALLET_NAME: &str = "BridgeMillauMessages";
/// Name of the transaction payment pallet at the Millau runtime.
pub const TRANSACTION_PAYMENT_PALLET_NAME: &str = "TransactionPayment";

decl_bridge_runtime_apis!(millau);
decl_bridge_runtime_apis!(millau, grandpa);
2 changes: 1 addition & 1 deletion primitives/chain-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ pub const WITH_POLKADOT_GRANDPA_PALLET_NAME: &str = "BridgePolkadotGrandpa";
/// reserve.
pub const MAX_NESTED_PARACHAIN_HEAD_DATA_SIZE: u32 = 128;

decl_bridge_finality_runtime_apis!(polkadot);
decl_bridge_finality_runtime_apis!(polkadot, grandpa);
4 changes: 2 additions & 2 deletions primitives/chain-rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use bp_header_chain::ChainWithGrandpa;
use bp_messages::{
InboundMessageDetails, LaneId, MessageNonce, MessagePayload, OutboundMessageDetails,
};
use bp_runtime::{decl_bridge_runtime_apis, Chain};
use bp_runtime::{decl_bridge_finality_runtime_apis, decl_bridge_runtime_apis, Chain};
use frame_support::{
dispatch::DispatchClass,
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, IdentityFee, Weight},
Expand Down Expand Up @@ -211,4 +211,4 @@ pub const PARAS_REGISTRAR_PALLET_NAME: &str = "Registrar";
/// Name of the parachains pallet in the Rialto runtime.
pub const PARAS_PALLET_NAME: &str = "Paras";

decl_bridge_runtime_apis!(rialto);
decl_bridge_runtime_apis!(rialto, grandpa);
2 changes: 1 addition & 1 deletion primitives/chain-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ pub const WITH_ROCOCO_GRANDPA_PALLET_NAME: &str = "BridgeRococoGrandpa";
/// reserve.
pub const MAX_NESTED_PARACHAIN_HEAD_DATA_SIZE: u32 = 128;

decl_bridge_finality_runtime_apis!(rococo);
decl_bridge_finality_runtime_apis!(rococo, grandpa);
3 changes: 2 additions & 1 deletion primitives/chain-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![allow(clippy::too_many_arguments)]

pub use bp_polkadot_core::*;
use frame_support::sp_std::prelude::*;

use bp_header_chain::ChainWithGrandpa;
use bp_runtime::{decl_bridge_finality_runtime_apis, Chain, Parachain};
Expand Down Expand Up @@ -103,6 +104,6 @@ pub const MAX_NESTED_PARACHAIN_HEAD_DATA_SIZE: u32 = 128;
/// Identifier of `AssetHubWestend` parachain at the Westend relay chain.
pub const ASSET_HUB_WESTEND_PARACHAIN_ID: u32 = 1000;

decl_bridge_finality_runtime_apis!(westend);
decl_bridge_finality_runtime_apis!(westend, grandpa);

decl_bridge_finality_runtime_apis!(AssetHubWestend);
2 changes: 1 addition & 1 deletion primitives/chain-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ impl ChainWithGrandpa for Wococo {
/// Name of the With-Wococo GRANDPA pallet instance that is deployed at bridged chains.
pub const WITH_WOCOCO_GRANDPA_PALLET_NAME: &str = "BridgeWococoGrandpa";

decl_bridge_finality_runtime_apis!(wococo);
decl_bridge_finality_runtime_apis!(wococo, grandpa);
16 changes: 15 additions & 1 deletion primitives/header-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use bp_runtime::{
BasicOperatingMode, Chain, HashOf, HasherOf, HeaderOf, RawStorageProof, StorageProofChecker,
StorageProofError,
StorageProofError, UnderlyingChainProvider,
};
use codec::{Codec, Decode, Encode, EncodeLike, MaxEncodedLen};
use core::{clone::Clone, cmp::Eq, default::Default, fmt::Debug};
Expand Down Expand Up @@ -234,3 +234,17 @@ pub trait ChainWithGrandpa: Chain {
/// refund amount and doing calls which exceed the limit, may be costly to submitter.
const AVERAGE_HEADER_SIZE_IN_JUSTIFICATION: u32;
}

/// A trait that provides the type of the underlying `ChainWithGrandpa`.
pub trait UnderlyingChainWithGrandpaProvider: UnderlyingChainProvider {
/// Underlying `ChainWithGrandpa` type.
type ChainWithGrandpa: ChainWithGrandpa;
}

impl<T> UnderlyingChainWithGrandpaProvider for T
where
T: UnderlyingChainProvider,
T::Chain: ChainWithGrandpa,
{
type ChainWithGrandpa = T::Chain;
}
23 changes: 20 additions & 3 deletions primitives/runtime/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,11 @@ pub type TransactionEraOf<C> = crate::TransactionEra<BlockNumberOf<C>, HashOf<C>
/// - `<ThisChain>FinalityApi`
/// - constants that are stringified names of runtime API methods:
/// - `BEST_FINALIZED_<THIS_CHAIN>_HEADER_METHOD`
/// - `<THIS_CHAIN>_ACCEPTED_<CONSENSUS>_FINALITY_PROOFS_METHOD`
/// The name of the chain has to be specified in snake case (e.g. `rialto_parachain`).
#[macro_export]
macro_rules! decl_bridge_finality_runtime_apis {
($chain: ident) => {
($chain: ident $(, $consensus: ident => $justification_type: ty)?) => {
bp_runtime::paste::item! {
mod [<$chain _finality_api>] {
use super::*;
Expand All @@ -291,6 +292,13 @@ macro_rules! decl_bridge_finality_runtime_apis {
pub const [<BEST_FINALIZED_ $chain:upper _HEADER_METHOD>]: &str =
stringify!([<$chain:camel FinalityApi_best_finalized>]);

$(
/// Name of the `<ThisChain>FinalityApi::accepted_<consensus>_finality_proofs`
/// runtime method.
pub const [<$chain:upper _ACCEPTED_ $consensus:upper _FINALITY_PROOFS_METHOD>]: &str =
stringify!([<$chain:camel FinalityApi_accepted_ $consensus:lower _finality_proofs>]);
)?

sp_api::decl_runtime_apis! {
/// API for querying information about the finalized chain headers.
///
Expand All @@ -299,13 +307,22 @@ macro_rules! decl_bridge_finality_runtime_apis {
pub trait [<$chain:camel FinalityApi>] {
/// Returns number and hash of the best finalized header known to the bridge module.
fn best_finalized() -> Option<bp_runtime::HeaderId<Hash, BlockNumber>>;

$(
/// Returns the justifications accepted in the current block.
fn [<accepted_ $consensus:lower _finality_proofs>](
) -> Vec<$justification_type>;
)?
}
}
}

pub use [<$chain _finality_api>]::*;
}
};
($chain: ident, grandpa) => {
decl_bridge_finality_runtime_apis!($chain, grandpa => bp_header_chain::justification::GrandpaJustification<Header>);
};
}

/// Convenience macro that declares bridge messages runtime apis and related constants for a chain.
Expand Down Expand Up @@ -376,8 +393,8 @@ macro_rules! decl_bridge_messages_runtime_apis {
/// The name of the chain has to be specified in snake case (e.g. `rialto_parachain`).
#[macro_export]
macro_rules! decl_bridge_runtime_apis {
($chain: ident) => {
bp_runtime::decl_bridge_finality_runtime_apis!($chain);
($chain: ident $(, $consensus: ident)?) => {
bp_runtime::decl_bridge_finality_runtime_apis!($chain $(, $consensus)?);
bp_runtime::decl_bridge_messages_runtime_apis!($chain);
};
}
Loading

0 comments on commit 81df7da

Please sign in to comment.