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
@@ -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| {