GRANDPA module: store accepted justifications (#2298) (#2301)

Store accepted justifications in events.
This commit is contained in:
Serban Iorga
2023-07-27 15:50:17 +03:00
committed by Bastian Köcher
parent 5f73a456c5
commit 7807b9de93
27 changed files with 193 additions and 63 deletions
+9 -2
View File
@@ -16,9 +16,11 @@
//! Types used to connect to the Kusama chain.
use bp_kusama::AccountInfoStorageMapKeyProvider;
use bp_kusama::{AccountInfoStorageMapKeyProvider, KUSAMA_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD};
use bp_runtime::ChainId;
use relay_substrate_client::{Chain, ChainWithBalances, RelayChain, UnderlyingChainProvider};
use relay_substrate_client::{
Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider,
};
use sp_core::storage::StorageKey;
use std::time::Duration;
@@ -47,6 +49,11 @@ impl Chain for Kusama {
type Call = ();
}
impl ChainWithGrandpa for Kusama {
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str =
KUSAMA_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
}
impl ChainWithBalances for Kusama {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
AccountInfoStorageMapKeyProvider::final_key(account_id)
+9 -3
View File
@@ -17,12 +17,13 @@
//! Types used to connect to the Millau-Substrate chain.
use bp_messages::MessageNonce;
use bp_millau::MILLAU_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
use bp_runtime::ChainId;
use codec::{Compact, Decode, Encode};
use relay_substrate_client::{
BalanceOf, Chain, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
ChainWithUtilityPallet, Error as SubstrateError, FullRuntimeUtilityPallet, NonceOf, SignParam,
UnderlyingChainProvider, UnsignedTransaction,
BalanceOf, Chain, ChainWithBalances, ChainWithGrandpa, ChainWithMessages,
ChainWithTransactions, ChainWithUtilityPallet, Error as SubstrateError,
FullRuntimeUtilityPallet, NonceOf, SignParam, UnderlyingChainProvider, UnsignedTransaction,
};
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
@@ -65,6 +66,11 @@ impl Chain for Millau {
type Call = millau_runtime::RuntimeCall;
}
impl ChainWithGrandpa for Millau {
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str =
MILLAU_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
}
impl ChainWithBalances for Millau {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
use frame_support::storage::generator::StorageMap;
+11 -2
View File
@@ -16,9 +16,13 @@
//! Types used to connect to the Polkadot chain.
use bp_polkadot::AccountInfoStorageMapKeyProvider;
use bp_polkadot::{
AccountInfoStorageMapKeyProvider, POLKADOT_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD,
};
use bp_runtime::ChainId;
use relay_substrate_client::{Chain, ChainWithBalances, RelayChain, UnderlyingChainProvider};
use relay_substrate_client::{
Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider,
};
use sp_core::storage::StorageKey;
use std::time::Duration;
@@ -47,6 +51,11 @@ impl Chain for Polkadot {
type Call = ();
}
impl ChainWithGrandpa for Polkadot {
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str =
POLKADOT_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
}
impl ChainWithBalances for Polkadot {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
AccountInfoStorageMapKeyProvider::final_key(account_id)
+9 -3
View File
@@ -17,12 +17,13 @@
//! Types used to connect to the Rialto-Substrate chain.
use bp_messages::MessageNonce;
use bp_rialto::RIALTO_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
use bp_runtime::ChainId;
use codec::{Compact, Decode, Encode};
use relay_substrate_client::{
BalanceOf, Chain, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
Error as SubstrateError, NonceOf, RelayChain, SignParam, UnderlyingChainProvider,
UnsignedTransaction,
BalanceOf, Chain, ChainWithBalances, ChainWithGrandpa, ChainWithMessages,
ChainWithTransactions, Error as SubstrateError, NonceOf, RelayChain, SignParam,
UnderlyingChainProvider, UnsignedTransaction,
};
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
@@ -50,6 +51,11 @@ impl Chain for Rialto {
type Call = rialto_runtime::RuntimeCall;
}
impl ChainWithGrandpa for Rialto {
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str =
RIALTO_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
}
impl RelayChain for Rialto {
const PARAS_PALLET_NAME: &'static str = bp_rialto::PARAS_PALLET_NAME;
const PARACHAINS_FINALITY_PALLET_NAME: &'static str =
+9 -1
View File
@@ -16,8 +16,11 @@
//! Types used to connect to the Rococo-Substrate chain.
use bp_rococo::ROCOCO_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
use bp_runtime::ChainId;
use relay_substrate_client::{Chain, ChainWithBalances, RelayChain, UnderlyingChainProvider};
use relay_substrate_client::{
Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider,
};
use sp_core::storage::StorageKey;
use std::time::Duration;
@@ -46,6 +49,11 @@ impl Chain for Rococo {
type Call = ();
}
impl ChainWithGrandpa for Rococo {
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str =
ROCOCO_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
}
impl ChainWithBalances for Rococo {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
bp_rococo::AccountInfoStorageMapKeyProvider::final_key(account_id)
+7 -15
View File
@@ -16,6 +16,7 @@
use crate::calls::UtilityCall;
use bp_header_chain::UnderlyingChainWithGrandpaProvider;
use bp_messages::MessageNonce;
use bp_runtime::{
Chain as ChainBase, ChainId, EncodedOrDecodedCall, HashOf, Parachain as ParachainBase,
@@ -77,22 +78,13 @@ pub trait RelayChain: Chain {
///
/// Keep in mind that parachains are relying on relay chain GRANDPA, so they should not implement
/// this trait.
pub trait ChainWithGrandpa: Chain {
/// Name of the bridge GRANDPA pallet (used in `construct_runtime` macro call) that is deployed
/// at some other chain to bridge with this `ChainWithGrandpa`.
pub trait ChainWithGrandpa: Chain + UnderlyingChainWithGrandpaProvider {
/// Name of the runtime API method that is returning the GRANDPA justifications accepted
/// by the `submit_finality_proofs` extrinsic in the queried block.
///
/// We assume that all chains that are bridging with this `ChainWithGrandpa` are using
/// the same name.
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str;
}
impl<T> ChainWithGrandpa for T
where
T: Chain + UnderlyingChainProvider,
T::Chain: bp_header_chain::ChainWithGrandpa,
{
const WITH_CHAIN_GRANDPA_PALLET_NAME: &'static str =
<T::Chain as bp_header_chain::ChainWithGrandpa>::WITH_CHAIN_GRANDPA_PALLET_NAME;
/// Keep in mind that this method is normally provided by the other chain, which is
/// bridged with this chain.
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str;
}
/// Substrate-based parachain from minimal relay-client point of view.
+9 -1
View File
@@ -17,7 +17,10 @@
//! Types used to connect to the Westend chain.
use bp_runtime::ChainId;
use relay_substrate_client::{Chain, ChainWithBalances, RelayChain, UnderlyingChainProvider};
use bp_westend::WESTEND_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
use relay_substrate_client::{
Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider,
};
use sp_core::storage::StorageKey;
use std::time::Duration;
@@ -46,6 +49,11 @@ impl Chain for Westend {
type Call = ();
}
impl ChainWithGrandpa for Westend {
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str =
WESTEND_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
}
impl RelayChain for Westend {
const PARAS_PALLET_NAME: &'static str = bp_westend::PARAS_PALLET_NAME;
const PARACHAINS_FINALITY_PALLET_NAME: &'static str =
+9 -1
View File
@@ -17,7 +17,10 @@
//! Types used to connect to the Wococo-Substrate chain.
use bp_runtime::ChainId;
use relay_substrate_client::{Chain, ChainWithBalances, RelayChain, UnderlyingChainProvider};
use bp_wococo::WOCOCO_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
use relay_substrate_client::{
Chain, ChainWithBalances, ChainWithGrandpa, RelayChain, UnderlyingChainProvider,
};
use sp_core::storage::StorageKey;
use std::time::Duration;
@@ -46,6 +49,11 @@ impl Chain for Wococo {
type Call = ();
}
impl ChainWithGrandpa for Wococo {
const ACCEPTED_FINALITY_PROOFS_METHOD: &'static str =
WOCOCO_ACCEPTED_GRANDPA_FINALITY_PROOFS_METHOD;
}
impl ChainWithBalances for Wococo {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
bp_wococo::AccountInfoStorageMapKeyProvider::final_key(account_id)
@@ -20,7 +20,8 @@ use crate::error::Error;
use async_trait::async_trait;
use bp_header_chain::{
justification::{verify_and_optimize_justification, GrandpaJustification},
ConsensusLogReader, FinalityProof, GrandpaConsensusLogReader,
ChainWithGrandpa as ChainWithGrandpaBase, ConsensusLogReader, FinalityProof,
GrandpaConsensusLogReader,
};
use bp_runtime::{BasicOperatingMode, HeaderIdProvider, OperatingMode};
use codec::{Decode, Encode};
@@ -83,8 +84,10 @@ pub trait Engine<C: Chain>: Send {
}
/// A method to subscribe to encoded finality proofs, given source client.
async fn finality_proofs(client: &Client<C>) -> Result<Subscription<Bytes>, SubstrateError> {
client.subscribe_finality_justifications::<Self::FinalityClient>().await
async fn source_finality_proofs(
source_client: &Client<C>,
) -> Result<Subscription<Bytes>, SubstrateError> {
source_client.subscribe_finality_justifications::<Self::FinalityClient>().await
}
/// Optimize finality proof before sending it to the target node.
@@ -139,11 +142,15 @@ impl<C: ChainWithGrandpa> Engine<C> for Grandpa<C> {
type OperatingMode = BasicOperatingMode;
fn is_initialized_key() -> StorageKey {
bp_header_chain::storage_keys::best_finalized_key(C::WITH_CHAIN_GRANDPA_PALLET_NAME)
bp_header_chain::storage_keys::best_finalized_key(
C::ChainWithGrandpa::WITH_CHAIN_GRANDPA_PALLET_NAME,
)
}
fn pallet_operating_mode_key() -> StorageKey {
bp_header_chain::storage_keys::pallet_operating_mode_key(C::WITH_CHAIN_GRANDPA_PALLET_NAME)
bp_header_chain::storage_keys::pallet_operating_mode_key(
C::ChainWithGrandpa::WITH_CHAIN_GRANDPA_PALLET_NAME,
)
}
async fn optimize_proof<TargetChain: Chain>(
@@ -152,7 +159,7 @@ impl<C: ChainWithGrandpa> Engine<C> for Grandpa<C> {
proof: &mut Self::FinalityProof,
) -> Result<(), SubstrateError> {
let current_authority_set_key = bp_header_chain::storage_keys::current_authority_set_key(
C::WITH_CHAIN_GRANDPA_PALLET_NAME,
C::ChainWithGrandpa::WITH_CHAIN_GRANDPA_PALLET_NAME,
);
let (authority_set, authority_set_id): (
sp_consensus_grandpa::AuthorityList,
@@ -199,7 +206,7 @@ impl<C: ChainWithGrandpa> Engine<C> for Grandpa<C> {
// But now there are problems with this approach - `CurrentSetId` may return invalid value.
// So here we're waiting for the next justification, read the authorities set and then try
// to figure out the set id with bruteforce.
let justifications = Self::finality_proofs(&source_client)
let justifications = Self::source_finality_proofs(&source_client)
.await
.map_err(|err| Error::Subscribe(C::NAME, err))?;
// Read next justification - the header that it finalizes will be used as initial header.
@@ -234,7 +234,7 @@ impl<P: SubstrateFinalitySyncPipeline> SourceClient<FinalitySyncPipelineAdapter<
async fn finality_proofs(&self) -> Result<Self::FinalityProofsStream, Error> {
Ok(unfold(
P::FinalityEngine::finality_proofs(&self.client).await?,
P::FinalityEngine::source_finality_proofs(&self.client).await?,
move |subscription| async move {
loop {
let log_error = |err| {