// Copyright 2019-2020 Parity Technologies (UK) Ltd. // This file is part of Parity Bridges Common. // Parity Bridges Common is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Parity Bridges Common is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Parity Bridges Common. If not, see . //! Substrate-to-Substrate headers sync entrypoint. use crate::{ headers_maintain::SubstrateHeadersToSubstrateMaintain, headers_target::{SubstrateHeadersSyncPipeline, SubstrateHeadersTarget}, }; use codec::Encode; use headers_relay::{ sync::{HeadersSyncParams, TargetTransactionMode}, sync_types::{HeadersSyncPipeline, QueuedHeader, SourceHeader}, }; use num_traits::Saturating; use relay_substrate_client::{headers_source::HeadersSource, BlockNumberOf, Chain, Client, HashOf}; use sp_runtime::Justification; use std::marker::PhantomData; /// Substrate-to-Substrate headers pipeline. #[derive(Debug, Clone)] pub struct SubstrateHeadersToSubstrate { /// Client for the target chain. pub(crate) target_client: Client, /// Data required to sign target chain transactions. pub(crate) target_sign: TargetSign, /// Unused generic arguments dump. _marker: PhantomData<(SourceChain, SourceSyncHeader)>, } impl SubstrateHeadersToSubstrate { /// Create new Substrate-to-Substrate headers pipeline. pub fn new(target_client: Client, target_sign: TargetSign) -> Self { SubstrateHeadersToSubstrate { target_client, target_sign, _marker: Default::default(), } } } impl HeadersSyncPipeline for SubstrateHeadersToSubstrate where SourceChain: Clone + Chain, BlockNumberOf: Saturating + Into, SourceSyncHeader: SourceHeader, BlockNumberOf> + std::ops::Deref, TargetChain: Clone + Chain, TargetSign: Clone + Send + Sync, { const SOURCE_NAME: &'static str = SourceChain::NAME; const TARGET_NAME: &'static str = TargetChain::NAME; type Hash = HashOf; type Number = BlockNumberOf; type Header = SourceSyncHeader; type Extra = (); type Completion = Justification; fn estimate_size(source: &QueuedHeader) -> usize { source.header().encode().len() } } /// Return sync parameters for Substrate-to-Substrate headers sync. pub fn sync_params() -> HeadersSyncParams { HeadersSyncParams { max_future_headers_to_download: 32, max_headers_in_submitted_status: 8, max_headers_in_single_submit: 1, max_headers_size_in_single_submit: 1024 * 1024, prune_depth: 256, target_tx_mode: TargetTransactionMode::Signed, } } /// Run Substrate-to-Substrate headers sync. pub async fn run( pipeline: P, source_client: Client, target_client: Client, metrics_params: Option, ) where P: SubstrateHeadersSyncPipeline< Hash = HashOf, Number = BlockNumberOf, Completion = Justification, Extra = (), >, P::Header: SourceHeader, BlockNumberOf>, SourceChain: Clone + Chain, SourceChain::Header: Into, BlockNumberOf: Into + Saturating, TargetChain: Clone + Chain, { let source_justifications = match source_client.clone().subscribe_justifications().await { Ok(source_justifications) => source_justifications, Err(error) => { log::warn!( target: "bridge", "Failed to subscribe to {} justifications: {:?}", SourceChain::NAME, error, ); return; } }; let sync_maintain = SubstrateHeadersToSubstrateMaintain::new(pipeline.clone(), source_client.clone(), source_justifications); headers_relay::sync_loop::run( HeadersSource::new(source_client), SourceChain::AVERAGE_BLOCK_INTERVAL, SubstrateHeadersTarget::new(target_client, pipeline), TargetChain::AVERAGE_BLOCK_INTERVAL, sync_maintain, sync_params(), metrics_params, futures::future::pending(), ); }