Use GrandpaJustification instead of Vec<u8> in Pallet API (#847)

* Stop passing raw encoded justifications to pallet API

By having the API accept a struct-ified justification we are able to
better utilize the justifications fields for weight calculations.

* Update relayer code to use decoded justifications

* Add justification to `expect()` statement

* Fix some imports

* Make justification wrapper contain decoded justification

* Rename some fields

* Get rid of warnings

* Appease Clippy

* Only decode justification once at init time

* Remove unnecessary method

* Remove justification wrapper

This became kinda unnecessary since we could implement the FinalityProof
trait on GrandpaJustification directly.
This commit is contained in:
Hernando Castano
2021-04-01 07:08:28 -04:00
committed by Bastian Köcher
parent 904b9f4da5
commit 67cdca8aa4
14 changed files with 92 additions and 117 deletions
@@ -18,11 +18,9 @@
use crate::finality_target::SubstrateFinalityTarget;
use bp_header_chain::justification::GrandpaJustification;
use finality_relay::{FinalitySyncParams, FinalitySyncPipeline};
use relay_substrate_client::{
finality_source::{FinalitySource, Justification},
BlockNumberOf, Chain, Client, HashOf, SyncHeader,
};
use relay_substrate_client::{finality_source::FinalitySource, BlockNumberOf, Chain, Client, HashOf, SyncHeader};
use relay_utils::BlockNumberBase;
use sp_core::Bytes;
use std::{fmt::Debug, marker::PhantomData, time::Duration};
@@ -101,7 +99,7 @@ where
type Hash = HashOf<SourceChain>;
type Number = BlockNumberOf<SourceChain>;
type Header = SyncHeader<SourceChain::Header>;
type FinalityProof = Justification<SourceChain::BlockNumber>;
type FinalityProof = GrandpaJustification<SourceChain::Header>;
}
/// Run Substrate-to-Substrate finality sync.
@@ -116,7 +114,7 @@ where
Hash = HashOf<SourceChain>,
Number = BlockNumberOf<SourceChain>,
Header = SyncHeader<SourceChain::Header>,
FinalityProof = Justification<SourceChain::BlockNumber>,
FinalityProof = GrandpaJustification<SourceChain::Header>,
TargetChain = TargetChain,
>,
SourceChain: Clone + Chain,
@@ -23,7 +23,7 @@
use bp_header_chain::{
find_grandpa_authorities_scheduled_change,
justification::{decode_justification_target, verify_justification},
justification::{verify_justification, GrandpaJustification},
};
use codec::Decode;
use finality_grandpa::voter_set::VoterSet;
@@ -116,9 +116,12 @@ async fn prepare_initialization_data<SourceChain: Chain>(
})?;
// Read initial header.
let justification: GrandpaJustification<SourceChain::Header> = Decode::decode(&mut &justification.0[..])
.map_err(|err| format!("Failed to decode {} justification: {:?}", SourceChain::NAME, err))?;
let (initial_header_hash, initial_header_number) =
decode_justification_target::<SourceChain::Header>(&justification.0)
.map_err(|err| format!("Failed to decode {} justification: {:?}", SourceChain::NAME, err))?;
(justification.commit.target_hash, justification.commit.target_number);
let initial_header = source_header(&source_client, initial_header_hash).await?;
log::trace!(target: "bridge", "Selected {} initial header: {}/{}",
SourceChain::NAME,
@@ -176,9 +179,10 @@ async fn prepare_initialization_data<SourceChain: Chain>(
(initial_header_hash, initial_header_number),
initial_authorities_set_id,
&authorities_for_verification,
&justification.0,
&justification,
)
.is_ok();
if is_valid_set_id {
break;
}
@@ -18,10 +18,11 @@
use crate::finality_pipeline::{SubstrateFinalitySyncPipeline, SubstrateFinalityToSubstrate};
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
use relay_millau_client::{Millau, SyncHeader as MillauSyncHeader};
use relay_rialto_client::{Rialto, SigningParams as RialtoSigningParams};
use relay_substrate_client::{finality_source::Justification, Chain, TransactionSignScheme};
use relay_substrate_client::{Chain, TransactionSignScheme};
use sp_core::{Bytes, Pair};
/// Millau-to-Rialto finality sync pipeline.
@@ -40,11 +41,9 @@ impl SubstrateFinalitySyncPipeline for MillauFinalityToRialto {
&self,
transaction_nonce: <Rialto as Chain>::Index,
header: MillauSyncHeader,
proof: Justification<bp_millau::BlockNumber>,
proof: GrandpaJustification<bp_millau::Header>,
) -> Bytes {
let call =
rialto_runtime::BridgeGrandpaMillauCall::submit_finality_proof(header.into_inner(), proof.into_inner())
.into();
let call = rialto_runtime::BridgeGrandpaMillauCall::submit_finality_proof(header.into_inner(), proof).into();
let genesis_hash = *self.target_client.genesis_hash();
let transaction = Rialto::sign_transaction(genesis_hash, &self.target_sign, transaction_nonce, call);
@@ -18,10 +18,11 @@
use crate::finality_pipeline::{SubstrateFinalitySyncPipeline, SubstrateFinalityToSubstrate};
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
use relay_millau_client::{Millau, SigningParams as MillauSigningParams};
use relay_rialto_client::{Rialto, SyncHeader as RialtoSyncHeader};
use relay_substrate_client::{finality_source::Justification, Chain, TransactionSignScheme};
use relay_substrate_client::{Chain, TransactionSignScheme};
use sp_core::{Bytes, Pair};
/// Rialto-to-Millau finality sync pipeline.
@@ -40,12 +41,12 @@ impl SubstrateFinalitySyncPipeline for RialtoFinalityToMillau {
&self,
transaction_nonce: <Millau as Chain>::Index,
header: RialtoSyncHeader,
proof: Justification<bp_rialto::BlockNumber>,
proof: GrandpaJustification<bp_rialto::Header>,
) -> Bytes {
let call = millau_runtime::BridgeGrandpaRialtoCall::<
millau_runtime::Runtime,
millau_runtime::RialtoGrandpaInstance,
>::submit_finality_proof(header.into_inner(), proof.into_inner())
>::submit_finality_proof(header.into_inner(), proof)
.into();
let genesis_hash = *self.target_client.genesis_hash();
@@ -18,9 +18,10 @@
use crate::finality_pipeline::{SubstrateFinalitySyncPipeline, SubstrateFinalityToSubstrate};
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
use relay_millau_client::{Millau, SigningParams as MillauSigningParams};
use relay_substrate_client::{finality_source::Justification, Chain, TransactionSignScheme};
use relay_substrate_client::{Chain, TransactionSignScheme};
use relay_westend_client::{SyncHeader as WestendSyncHeader, Westend};
use sp_core::{Bytes, Pair};
@@ -40,12 +41,12 @@ impl SubstrateFinalitySyncPipeline for WestendFinalityToMillau {
&self,
transaction_nonce: <Millau as Chain>::Index,
header: WestendSyncHeader,
proof: Justification<bp_westend::BlockNumber>,
proof: GrandpaJustification<bp_westend::Header>,
) -> Bytes {
let call = millau_runtime::BridgeGrandpaWestendCall::<
millau_runtime::Runtime,
millau_runtime::WestendGrandpaInstance,
>::submit_finality_proof(header.into_inner(), proof.into_inner())
>::submit_finality_proof(header.into_inner(), proof)
.into();
let genesis_hash = *self.target_client.genesis_hash();
@@ -22,35 +22,14 @@ use crate::error::Error;
use crate::sync_header::SyncHeader;
use async_trait::async_trait;
use bp_header_chain::justification::decode_justification_target;
use finality_relay::{FinalityProof, FinalitySyncPipeline, SourceClient, SourceHeader};
use bp_header_chain::justification::GrandpaJustification;
use codec::Decode;
use finality_relay::{FinalitySyncPipeline, SourceClient, SourceHeader};
use futures::stream::{unfold, Stream, StreamExt};
use relay_utils::relay_loop::Client as RelayClient;
use sp_runtime::traits::Header as HeaderT;
use std::{marker::PhantomData, pin::Pin};
/// Wrapped raw Justification.
#[derive(Debug, Clone)]
pub struct Justification<Number> {
/// Header number decoded from the [`raw_justification`].
target_header_number: Number,
/// Raw, encoded justification bytes.
raw_justification: sp_runtime::Justification,
}
impl<Number> Justification<Number> {
/// Extract raw justification.
pub fn into_inner(self) -> sp_runtime::Justification {
self.raw_justification
}
}
impl<Number: relay_utils::BlockNumberBase> FinalityProof<Number> for Justification<Number> {
fn target_header_number(&self) -> Number {
self.target_header_number
}
}
/// Substrate node as finality source.
pub struct FinalitySource<C: Chain, P> {
client: Client<C>,
@@ -94,11 +73,11 @@ where
Hash = C::Hash,
Number = C::BlockNumber,
Header = SyncHeader<C::Header>,
FinalityProof = Justification<C::BlockNumber>,
FinalityProof = GrandpaJustification<C::Header>,
>,
P::Header: SourceHeader<C::BlockNumber>,
{
type FinalityProofsStream = Pin<Box<dyn Stream<Item = Justification<C::BlockNumber>> + Send>>;
type FinalityProofsStream = Pin<Box<dyn Stream<Item = GrandpaJustification<C::Header>> + Send>>;
async fn best_finalized_block_number(&self) -> Result<P::Number, Error> {
// we **CAN** continue to relay finality proofs if source node is out of sync, because
@@ -114,16 +93,14 @@ where
) -> Result<(P::Header, Option<P::FinalityProof>), Error> {
let header_hash = self.client.block_hash_by_number(number).await?;
let signed_block = self.client.get_block(Some(header_hash)).await?;
Ok((
signed_block.header().into(),
signed_block
.justification()
.cloned()
.map(|raw_justification| Justification {
target_header_number: number,
raw_justification,
}),
))
let justification = signed_block
.justification()
.map(|raw_justification| GrandpaJustification::<C::Header>::decode(&mut raw_justification.as_slice()))
.transpose()
.map_err(Error::ResponseParseFailed)?;
Ok((signed_block.header().into(), justification))
}
async fn finality_proofs(&self) -> Result<Self::FinalityProofsStream, Error> {
@@ -132,9 +109,11 @@ where
move |mut subscription| async move {
loop {
let next_justification = subscription.next().await?;
let decoded_target = decode_justification_target::<C::Header>(&next_justification.0);
let target_header_number = match decoded_target {
Ok((_, number)) => number,
let decoded_justification =
GrandpaJustification::<C::Header>::decode(&mut &next_justification.0[..]);
let justification = match decoded_justification {
Ok(j) => j,
Err(err) => {
log::error!(
target: "bridge",
@@ -147,13 +126,7 @@ where
}
};
return Some((
Justification {
target_header_number,
raw_justification: next_justification.0,
},
subscription,
));
return Some((justification, subscription));
}
},
)
+1
View File
@@ -10,6 +10,7 @@ description = "Finality proofs relay"
async-std = "1.6.5"
async-trait = "0.1.40"
backoff = "0.2"
bp-header-chain = { path = "../../primitives/header-chain" }
futures = "0.3.5"
headers-relay = { path = "../headers" }
log = "0.4.11"
+1 -6
View File
@@ -21,6 +21,7 @@
pub use crate::finality_loop::{run, FinalitySyncParams, SourceClient, TargetClient};
use bp_header_chain::FinalityProof;
use std::fmt::Debug;
mod finality_loop;
@@ -50,9 +51,3 @@ pub trait SourceHeader<Number>: Clone + Debug + PartialEq + Send + Sync {
/// Returns true if this header needs to be submitted to target node.
fn is_mandatory(&self) -> bool;
}
/// Abstract finality proof that is justifying block finality.
pub trait FinalityProof<Number>: Clone + Send + Sync + Debug {
/// Return number of header that this proof is generated for.
fn target_header_number(&self) -> Number;
}