Skip to content

Commit

Permalink
FinalityEngine in substrate relay (paritytech#1374)
Browse files Browse the repository at this point in the history
* introduce FinalityEngine in relay code

* add FinalityEngine to relay

* spelling

* fix test compilation

* Update relays/lib-substrate-relay/src/finality/source.rs

Co-authored-by: Adrian Catangiu <[email protected]>

Co-authored-by: Adrian Catangiu <[email protected]>
  • Loading branch information
2 people authored and serban300 committed Apr 9, 2024
1 parent e41a7ba commit 42add7c
Show file tree
Hide file tree
Showing 23 changed files with 469 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use async_trait::async_trait;
use relay_polkadot_client::Polkadot;
use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams};
use substrate_relay_helper::{
finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline},
TransactionParams,
};

/// Maximal saturating difference between `balance(now)` and `balance(now-24h)` to treat
/// relay as gone wild.
Expand Down Expand Up @@ -47,6 +50,7 @@ impl SubstrateFinalitySyncPipeline for KusamaFinalityToPolkadot {
type SourceChain = relay_kusama_client::Kusama;
type TargetChain = Polkadot;

type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = KusamaFinalityToPolkadotCallBuilder;
type TransactionSignScheme = Polkadot;

Expand All @@ -55,7 +59,7 @@ impl SubstrateFinalitySyncPipeline for KusamaFinalityToPolkadot {
transaction_params: &TransactionParams<sp_core::sr25519::Pair>,
enable_version_guard: bool,
) -> relay_substrate_client::Result<()> {
substrate_relay_helper::finality_guards::start::<Polkadot, Polkadot>(
substrate_relay_helper::finality::guards::start::<Polkadot, Polkadot>(
target_client,
transaction_params,
enable_version_guard,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

//! Millau-to-Rialto headers sync entrypoint.

use substrate_relay_helper::finality_pipeline::{
DirectSubmitFinalityProofCallBuilder, SubstrateFinalitySyncPipeline,
use substrate_relay_helper::finality::{
engine::Grandpa as GrandpaFinalityEngine, DirectSubmitGrandpaFinalityProofCallBuilder,
SubstrateFinalitySyncPipeline,
};

/// Description of Millau -> Rialto finalized headers bridge.
Expand All @@ -28,7 +29,8 @@ impl SubstrateFinalitySyncPipeline for MillauFinalityToRialto {
type SourceChain = relay_millau_client::Millau;
type TargetChain = relay_rialto_client::Rialto;

type SubmitFinalityProofCallBuilder = DirectSubmitFinalityProofCallBuilder<
type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = DirectSubmitGrandpaFinalityProofCallBuilder<
Self,
rialto_runtime::Runtime,
rialto_runtime::MillauGrandpaInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use async_trait::async_trait;
use relay_kusama_client::Kusama;
use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams};
use substrate_relay_helper::{
finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline},
TransactionParams,
};

/// Maximal saturating difference between `balance(now)` and `balance(now-24h)` to treat
/// relay as gone wild.
Expand Down Expand Up @@ -47,6 +50,7 @@ impl SubstrateFinalitySyncPipeline for PolkadotFinalityToKusama {
type SourceChain = relay_polkadot_client::Polkadot;
type TargetChain = Kusama;

type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = PolkadotFinalityToKusamaCallBuilder;
type TransactionSignScheme = Kusama;

Expand All @@ -55,7 +59,7 @@ impl SubstrateFinalitySyncPipeline for PolkadotFinalityToKusama {
transaction_params: &TransactionParams<sp_core::sr25519::Pair>,
enable_version_guard: bool,
) -> relay_substrate_client::Result<()> {
substrate_relay_helper::finality_guards::start::<Kusama, Kusama>(
substrate_relay_helper::finality::guards::start::<Kusama, Kusama>(
target_client,
transaction_params,
enable_version_guard,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

//! Rialto-to-Millau headers sync entrypoint.

use substrate_relay_helper::finality_pipeline::{
DirectSubmitFinalityProofCallBuilder, SubstrateFinalitySyncPipeline,
use substrate_relay_helper::finality::{
engine::Grandpa as GrandpaFinalityEngine, DirectSubmitGrandpaFinalityProofCallBuilder,
SubstrateFinalitySyncPipeline,
};

/// Description of Millau -> Rialto finalized headers bridge.
Expand All @@ -28,7 +29,8 @@ impl SubstrateFinalitySyncPipeline for RialtoFinalityToMillau {
type SourceChain = relay_rialto_client::Rialto;
type TargetChain = relay_millau_client::Millau;

type SubmitFinalityProofCallBuilder = DirectSubmitFinalityProofCallBuilder<
type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = DirectSubmitGrandpaFinalityProofCallBuilder<
Self,
millau_runtime::Runtime,
millau_runtime::RialtoGrandpaInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use crate::chains::wococo_headers_to_rococo::MAXIMAL_BALANCE_DECREASE_PER_DAY;

use async_trait::async_trait;
use relay_wococo_client::Wococo;
use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams};
use substrate_relay_helper::{
finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline},
TransactionParams,
};

/// Description of Rococo -> Wococo finalized headers bridge.
#[derive(Clone, Debug)]
Expand All @@ -37,6 +40,7 @@ impl SubstrateFinalitySyncPipeline for RococoFinalityToWococo {
type SourceChain = relay_rococo_client::Rococo;
type TargetChain = Wococo;

type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = RococoFinalityToWococoCallBuilder;
type TransactionSignScheme = Wococo;

Expand All @@ -45,7 +49,7 @@ impl SubstrateFinalitySyncPipeline for RococoFinalityToWococo {
transaction_params: &TransactionParams<sp_core::sr25519::Pair>,
enable_version_guard: bool,
) -> relay_substrate_client::Result<()> {
substrate_relay_helper::finality_guards::start::<Wococo, Wococo>(
substrate_relay_helper::finality::guards::start::<Wococo, Wococo>(
target_client,
transaction_params,
enable_version_guard,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

//! Westend-to-Millau headers sync entrypoint.

use substrate_relay_helper::finality_pipeline::{
DirectSubmitFinalityProofCallBuilder, SubstrateFinalitySyncPipeline,
use substrate_relay_helper::finality::{
engine::Grandpa as GrandpaFinalityEngine, DirectSubmitGrandpaFinalityProofCallBuilder,
SubstrateFinalitySyncPipeline,
};

/// Description of Westend -> Millau finalized headers bridge.
Expand All @@ -28,7 +29,8 @@ impl SubstrateFinalitySyncPipeline for WestendFinalityToMillau {
type SourceChain = relay_westend_client::Westend;
type TargetChain = relay_millau_client::Millau;

type SubmitFinalityProofCallBuilder = DirectSubmitFinalityProofCallBuilder<
type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = DirectSubmitGrandpaFinalityProofCallBuilder<
Self,
millau_runtime::Runtime,
millau_runtime::WestendGrandpaInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use async_trait::async_trait;
use relay_rococo_client::Rococo;
use substrate_relay_helper::{finality_pipeline::SubstrateFinalitySyncPipeline, TransactionParams};
use substrate_relay_helper::{
finality::{engine::Grandpa as GrandpaFinalityEngine, SubstrateFinalitySyncPipeline},
TransactionParams,
};

/// Maximal saturating difference between `balance(now)` and `balance(now-24h)` to treat
/// relay as gone wild.
Expand All @@ -42,6 +45,7 @@ impl SubstrateFinalitySyncPipeline for WococoFinalityToRococo {
type SourceChain = relay_wococo_client::Wococo;
type TargetChain = Rococo;

type FinalityEngine = GrandpaFinalityEngine<Self::SourceChain>;
type SubmitFinalityProofCallBuilder = WococoFinalityToRococoCallBuilder;
type TransactionSignScheme = Rococo;

Expand All @@ -50,7 +54,7 @@ impl SubstrateFinalitySyncPipeline for WococoFinalityToRococo {
transaction_params: &TransactionParams<sp_core::sr25519::Pair>,
enable_version_guard: bool,
) -> relay_substrate_client::Result<()> {
substrate_relay_helper::finality_guards::start::<Rococo, Rococo>(
substrate_relay_helper::finality::guards::start::<Rococo, Rococo>(
target_client,
transaction_params,
enable_version_guard,
Expand Down
10 changes: 9 additions & 1 deletion bridges/relays/bin-substrate/src/cli/init_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use relay_substrate_client::{Chain, SignParam, TransactionSignScheme, UnsignedTr
use sp_core::{Bytes, Pair};
use structopt::StructOpt;
use strum::{EnumString, EnumVariantNames, VariantNames};
use substrate_relay_helper::finality::engine::Grandpa as GrandpaFinalityEngine;

/// Initialize bridge pallet.
#[derive(StructOpt)]
Expand Down Expand Up @@ -56,6 +57,7 @@ macro_rules! select_bridge {
InitBridgeName::MillauToRialto => {
type Source = relay_millau_client::Millau;
type Target = relay_rialto_client::Rialto;
type Engine = GrandpaFinalityEngine<Source>;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
Expand All @@ -74,6 +76,7 @@ macro_rules! select_bridge {
InitBridgeName::RialtoToMillau => {
type Source = relay_rialto_client::Rialto;
type Target = relay_millau_client::Millau;
type Engine = GrandpaFinalityEngine<Source>;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
Expand All @@ -92,6 +95,7 @@ macro_rules! select_bridge {
InitBridgeName::WestendToMillau => {
type Source = relay_westend_client::Westend;
type Target = relay_millau_client::Millau;
type Engine = GrandpaFinalityEngine<Source>;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
Expand All @@ -114,6 +118,7 @@ macro_rules! select_bridge {
InitBridgeName::RococoToWococo => {
type Source = relay_rococo_client::Rococo;
type Target = relay_wococo_client::Wococo;
type Engine = GrandpaFinalityEngine<Source>;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
Expand All @@ -130,6 +135,7 @@ macro_rules! select_bridge {
InitBridgeName::WococoToRococo => {
type Source = relay_wococo_client::Wococo;
type Target = relay_rococo_client::Rococo;
type Engine = GrandpaFinalityEngine<Source>;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
Expand All @@ -146,6 +152,7 @@ macro_rules! select_bridge {
InitBridgeName::KusamaToPolkadot => {
type Source = relay_kusama_client::Kusama;
type Target = relay_polkadot_client::Polkadot;
type Engine = GrandpaFinalityEngine<Source>;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
Expand All @@ -162,6 +169,7 @@ macro_rules! select_bridge {
InitBridgeName::PolkadotToKusama => {
type Source = relay_polkadot_client::Polkadot;
type Target = relay_kusama_client::Kusama;
type Engine = GrandpaFinalityEngine<Source>;

fn encode_init_bridge(
init_data: InitializationData<<Source as ChainBase>::Header>,
Expand Down Expand Up @@ -189,7 +197,7 @@ impl InitBridge {

let (spec_version, transaction_version) =
target_client.simple_runtime_version().await?;
substrate_relay_helper::headers_initialize::initialize(
substrate_relay_helper::finality::initialize::initialize::<Engine, _, _, _>(
source_client,
target_client.clone(),
target_sign.public().into(),
Expand Down
12 changes: 8 additions & 4 deletions bridges/relays/bin-substrate/src/cli/reinit_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::{
TargetConnectionParams, TargetSigningParams,
},
};
use bp_header_chain::justification::GrandpaJustification;
use bp_runtime::Chain;
use codec::Encode;
use finality_relay::{SourceClient, SourceHeader};
Expand All @@ -40,8 +39,12 @@ use std::convert::{TryFrom, TryInto};
use structopt::StructOpt;
use strum::{EnumString, EnumVariantNames, VariantNames};
use substrate_relay_helper::{
finality_pipeline::SubstrateFinalitySyncPipeline, finality_source::SubstrateFinalitySource,
finality_target::SubstrateFinalityTarget, messages_source::read_client_state,
finality::{
source::{SubstrateFinalityProof, SubstrateFinalitySource},
target::SubstrateFinalityTarget,
SubstrateFinalitySyncPipeline,
},
messages_source::read_client_state,
TransactionParams,
};

Expand Down Expand Up @@ -299,7 +302,7 @@ impl ReinitBridge {
/// Mandatory header and its finality proof.
type HeaderAndProof<P> = (
SyncHeader<HeaderOf<<P as SubstrateFinalitySyncPipeline>::SourceChain>>,
GrandpaJustification<HeaderOf<<P as SubstrateFinalitySyncPipeline>::SourceChain>>,
SubstrateFinalityProof<P>,
);
/// Vector of mandatory headers and their finality proofs.
type HeadersAndProofs<P> = Vec<HeaderAndProof<P>>;
Expand Down Expand Up @@ -425,6 +428,7 @@ fn make_mandatory_headers_batches<
mod tests {
use super::*;
use crate::cli::{RuntimeVersionType, SourceRuntimeVersionParams, TargetRuntimeVersionParams};
use bp_header_chain::justification::GrandpaJustification;
use bp_test_utils::{make_default_justification, test_header};
use relay_polkadot_client::Polkadot;
use sp_runtime::{traits::Header as _, DigestItem};
Expand Down
4 changes: 2 additions & 2 deletions bridges/relays/bin-substrate/src/cli/relay_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use structopt::StructOpt;
use strum::{EnumString, EnumVariantNames, VariantNames};

use relay_utils::metrics::{GlobalMetrics, StandaloneMetric};
use substrate_relay_helper::finality_pipeline::SubstrateFinalitySyncPipeline;
use substrate_relay_helper::finality::SubstrateFinalitySyncPipeline;

use crate::cli::{
PrometheusParams, SourceConnectionParams, TargetConnectionParams, TargetSigningParams,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl RelayHeaders {
)
.await?;

substrate_relay_helper::finality_pipeline::run::<Finality>(
substrate_relay_helper::finality::run::<Finality>(
source_client,
target_client,
self.only_mandatory_headers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use relay_substrate_client::{
use relay_utils::metrics::MetricsParams;
use sp_core::{Bytes, Pair};
use substrate_relay_helper::{
finality_pipeline::SubstrateFinalitySyncPipeline, messages_lane::MessagesRelayParams,
finality::SubstrateFinalitySyncPipeline, messages_lane::MessagesRelayParams,
on_demand_headers::OnDemandHeadersRelay, TransactionParams,
};

Expand Down
4 changes: 2 additions & 2 deletions bridges/relays/client-substrate/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,8 @@ impl<C: Chain> Client<C> {
.await
}

/// Return new justifications stream.
pub async fn subscribe_justifications(&self) -> Result<Subscription<Bytes>> {
/// Return new GRANDPA justifications stream.
pub async fn subscribe_grandpa_justifications(&self) -> Result<Subscription<Bytes>> {
let subscription = self
.jsonrpsee_execute(move |client| async move {
Ok(client
Expand Down
6 changes: 3 additions & 3 deletions bridges/relays/client-substrate/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub enum Error {
/// Account does not exist on the chain.
#[error("Account does not exist on the chain.")]
AccountDoesNotExist,
/// Runtime storage is missing mandatory ":code:" entry.
#[error("Mandatory :code: entry is missing from runtime storage.")]
MissingMandatoryCodeEntry,
/// Runtime storage is missing some mandatory value.
#[error("Mandatory storage value is missing from the runtime storage.")]
MissingMandatoryStorageValue,
/// The client we're connected to is not synced, so we can't rely on its state.
#[error("Substrate client is not synced {0}.")]
ClientNotSynced(Health),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<C: Chain> StorageProofOverheadMetric<C> {
let maybe_encoded_storage_value =
storage_value_reader.read_value(CODE).map_err(Error::StorageProofError)?;
let encoded_storage_value_size =
maybe_encoded_storage_value.ok_or(Error::MissingMandatoryCodeEntry)?.len();
maybe_encoded_storage_value.ok_or(Error::MissingMandatoryStorageValue)?.len();

Ok(storage_proof_size - encoded_storage_value_size)
}
Expand Down
Loading

0 comments on commit 42add7c

Please sign in to comment.