// Copyright 2019-2021 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 client as Substrate finality proof target.
use crate::{
finality::{
FinalitySyncPipelineAdapter, SubmitFinalityProofCallBuilder, SubstrateFinalitySyncPipeline,
},
finality_base::{best_synced_header_id, engine::Engine, SubstrateFinalityProof},
TransactionParams,
};
use async_trait::async_trait;
use bp_runtime::BlockNumberOf;
use finality_relay::TargetClient;
use relay_substrate_client::{
AccountKeyPairOf, Chain, Client, Error, HeaderIdOf, HeaderOf, SyncHeader, TransactionEra,
TransactionTracker, UnsignedTransaction,
};
use relay_utils::relay_loop::Client as RelayClient;
use sp_runtime::traits::Header;
/// Substrate client as Substrate finality target.
pub struct SubstrateFinalityTarget {
client: Client,
transaction_params: TransactionParams>,
}
impl SubstrateFinalityTarget
{
/// Create new Substrate headers target.
pub fn new(
client: Client,
transaction_params: TransactionParams>,
) -> Self {
SubstrateFinalityTarget { client, transaction_params }
}
/// Ensure that the bridge pallet at target chain is active.
pub async fn ensure_pallet_active(&self) -> Result<(), Error> {
let is_halted = P::FinalityEngine::is_halted(&self.client).await?;
if is_halted {
return Err(Error::BridgePalletIsHalted)
}
let is_initialized = P::FinalityEngine::is_initialized(&self.client).await?;
if !is_initialized {
return Err(Error::BridgePalletIsNotInitialized)
}
Ok(())
}
}
impl Clone for SubstrateFinalityTarget
{
type TransactionTracker = TransactionTracker>;
async fn best_finalized_source_block_id(&self) -> Result, Error> {
// we can't continue to relay finality if target node is out of sync, because
// it may have already received (some of) headers that we're going to relay
self.client.ensure_synced().await?;
// we can't relay finality if bridge pallet at target chain is halted
self.ensure_pallet_active().await?;
Ok(best_synced_header_id::(
&self.client,
self.client.best_header().await?.hash(),
)
.await?
.ok_or(Error::BridgePalletIsNotInitialized)?)
}
async fn free_source_headers_interval(
&self,
) -> Result