From e93b27bdbdd6e0367b541a99347e4423b0a14145 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Fri, 26 Apr 2024 10:54:38 +0300 Subject: [PATCH 1/3] relay single header --- .../src/cli/relay_headers.rs | 39 ++++++++++++++++++- .../lib-substrate-relay/src/finality/mod.rs | 37 +++++++++++++++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs b/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs index cf1957c7323b..38d4622276a3 100644 --- a/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs +++ b/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs @@ -19,7 +19,10 @@ use async_trait::async_trait; use structopt::StructOpt; -use relay_utils::metrics::{GlobalMetrics, StandaloneMetric}; +use relay_utils::{ + metrics::{GlobalMetrics, StandaloneMetric}, + UniqueSaturatedInto, +}; use crate::{ cli::{bridge::*, chain_schema::*, PrometheusParams}, @@ -48,6 +51,21 @@ pub struct RelayHeadersParams { prometheus_params: PrometheusParams, } +/// Single header relaying params. +#[derive(StructOpt)] +pub struct RelayHeaderParams { + #[structopt(flatten)] + source: SourceConnectionParams, + #[structopt(flatten)] + target: TargetConnectionParams, + #[structopt(flatten)] + target_sign: TargetSigningParams, + /// Number of the source chain header that we want to relay. It must have a persistent + /// storage proof at the [`Self::source`] node, otherwise the command will fail. + #[structopt(long)] + number: u128, +} + impl RelayHeadersParams { fn headers_to_relay(&self) -> HeadersToRelay { match (self.only_mandatory_headers, self.only_free_headers) { @@ -89,4 +107,23 @@ pub trait HeadersRelayer: RelayToRelayHeadersCliBridge { ) .await } + + /// Relay single header. No any check are made to ensure that transaction will succeed. + async fn relay_header(data: RelayHeaderParams) -> anyhow::Result<()> { + let source_client = data.source.into_client::().await?; + let target_client = data.target.into_client::().await?; + let target_transactions_mortality = data.target_sign.target_transactions_mortality; + let target_sign = data.target_sign.to_keypair::()?; + + crate::finality::relay_single_header::( + source_client, + target_client, + crate::TransactionParams { + signer: target_sign, + mortality: target_transactions_mortality, + }, + data.number.unique_saturated_into(), + ) + .await + } } diff --git a/bridges/relays/lib-substrate-relay/src/finality/mod.rs b/bridges/relays/lib-substrate-relay/src/finality/mod.rs index a06857ae1d9b..4733caac86cd 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/mod.rs +++ b/bridges/relays/lib-substrate-relay/src/finality/mod.rs @@ -25,13 +25,15 @@ use crate::{ use async_trait::async_trait; use bp_header_chain::justification::{GrandpaJustification, JustificationVerificationContext}; -use finality_relay::{FinalityPipeline, FinalitySyncPipeline, HeadersToRelay}; +use finality_relay::{ + FinalityPipeline, FinalitySyncPipeline, HeadersToRelay, SourceClient, TargetClient, +}; use pallet_bridge_grandpa::{Call as BridgeGrandpaCall, Config as BridgeGrandpaConfig}; use relay_substrate_client::{ transaction_stall_timeout, AccountIdOf, AccountKeyPairOf, BlockNumberOf, CallOf, Chain, ChainWithTransactions, Client, HashOf, HeaderOf, SyncHeader, }; -use relay_utils::metrics::MetricsParams; +use relay_utils::{metrics::MetricsParams, TrackedTransactionStatus, TransactionTracker}; use sp_core::Pair; use std::{fmt::Debug, marker::PhantomData}; @@ -274,3 +276,34 @@ pub async fn run( .await .map_err(|e| anyhow::format_err!("{}", e)) } + +/// Relay single header. No any check are made to ensure that transaction will succeed. +pub async fn relay_single_header( + source_client: Client, + target_client: Client, + transaction_params: TransactionParams>, + header_number: BlockNumberOf, +) -> anyhow::Result<()> { + let finality_source = SubstrateFinalitySource::

::new(source_client, None); + let (header, proof) = finality_source.header_and_finality_proof(header_number).await?; + let Some(proof) = proof else { + return Err(anyhow::format_err!( + "Unable to submit {} header #{} to {}: no finality proof", + P::SourceChain::NAME, + header_number, + P::TargetChain::NAME, + )); + }; + + let finality_target = SubstrateFinalityTarget::

::new(target_client, transaction_params); + let tx_tracker = finality_target.submit_finality_proof(header, proof, false).await?; + match tx_tracker.wait().await { + TrackedTransactionStatus::Finalized(_) => Ok(()), + TrackedTransactionStatus::Lost => Err(anyhow::format_err!( + "Transaction with {} header #{} is considered lost at {}", + P::SourceChain::NAME, + header_number, + P::TargetChain::NAME, + )), + } +} From 37d9f717adbc98755e9fe496a4935a63f3d01fa0 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Fri, 26 Apr 2024 14:02:22 +0300 Subject: [PATCH 2/3] Update bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs Co-authored-by: Adrian Catangiu --- bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs b/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs index 38d4622276a3..093f98ef21ed 100644 --- a/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs +++ b/bridges/relays/lib-substrate-relay/src/cli/relay_headers.rs @@ -108,7 +108,7 @@ pub trait HeadersRelayer: RelayToRelayHeadersCliBridge { .await } - /// Relay single header. No any check are made to ensure that transaction will succeed. + /// Relay single header. No checks are made to ensure that transaction will succeed. async fn relay_header(data: RelayHeaderParams) -> anyhow::Result<()> { let source_client = data.source.into_client::().await?; let target_client = data.target.into_client::().await?; From 8c9d064ff479952a81a4df688dfb97e6b42d4434 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Fri, 26 Apr 2024 14:02:27 +0300 Subject: [PATCH 3/3] Update bridges/relays/lib-substrate-relay/src/finality/mod.rs Co-authored-by: Adrian Catangiu --- bridges/relays/lib-substrate-relay/src/finality/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/relays/lib-substrate-relay/src/finality/mod.rs b/bridges/relays/lib-substrate-relay/src/finality/mod.rs index 4733caac86cd..0293e1da224a 100644 --- a/bridges/relays/lib-substrate-relay/src/finality/mod.rs +++ b/bridges/relays/lib-substrate-relay/src/finality/mod.rs @@ -277,7 +277,7 @@ pub async fn run( .map_err(|e| anyhow::format_err!("{}", e)) } -/// Relay single header. No any check are made to ensure that transaction will succeed. +/// Relay single header. No checks are made to ensure that transaction will succeed. pub async fn relay_single_header( source_client: Client, target_client: Client,