Newer
Older
// 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 <http://www.gnu.org/licenses/>.
//! Parachain heads target.
use crate::{
parachains::{
ParachainsPipelineAdapter, SubmitParachainHeadsCallBuilder, SubstrateParachainsPipeline,
},
TransactionParams,
};
use bp_polkadot_core::parachains::{ParaHash, ParaHeadsProof, ParaId};
use codec::Decode;
Svyatoslav Nikolsky
committed
use parachains_relay::parachains_loop::TargetClient;
Svyatoslav Nikolsky
committed
AccountIdOf, AccountKeyPairOf, Chain, Client, Error as SubstrateError, HeaderIdOf,
ParachainBase, TransactionEra, TransactionTracker, UnsignedTransaction,
Svyatoslav Nikolsky
committed
use relay_utils::relay_loop::Client as RelayClient;
use sp_core::{Bytes, Pair};
/// Substrate client as parachain heads source.
pub struct ParachainsTarget<P: SubstrateParachainsPipeline> {
transaction_params: TransactionParams<AccountKeyPairOf<P::TargetChain>>,
impl<P: SubstrateParachainsPipeline> ParachainsTarget<P> {
/// Creates new parachains target client.
pub fn new(
client: Client<P::TargetChain>,
transaction_params: TransactionParams<AccountKeyPairOf<P::TargetChain>>,
ParachainsTarget { client, transaction_params }
}
/// Returns reference to the underlying RPC client.
pub fn client(&self) -> &Client<P::TargetChain> {
&self.client
impl<P: SubstrateParachainsPipeline> Clone for ParachainsTarget<P> {
fn clone(&self) -> Self {
ParachainsTarget {
client: self.client.clone(),
transaction_params: self.transaction_params.clone(),
}
}
}
#[async_trait]
impl<P: SubstrateParachainsPipeline> RelayClient for ParachainsTarget<P> {
type Error = SubstrateError;
async fn reconnect(&mut self) -> Result<(), SubstrateError> {
self.client.reconnect().await
}
}
#[async_trait]
impl<P> TargetClient<ParachainsPipelineAdapter<P>> for ParachainsTarget<P>
P: SubstrateParachainsPipeline,
AccountIdOf<P::TargetChain>: From<<AccountKeyPairOf<P::TargetChain> as Pair>::Public>,
type TransactionTracker = TransactionTracker<P::TargetChain, Client<P::TargetChain>>;
async fn best_block(&self) -> Result<HeaderIdOf<P::TargetChain>, Self::Error> {
let best_header = self.client.best_header().await?;
Svyatoslav Nikolsky
committed
async fn best_finalized_source_relay_chain_block(
&self,
at_block: &HeaderIdOf<P::TargetChain>,
) -> Result<HeaderIdOf<P::SourceRelayChain>, Self::Error> {
Svyatoslav Nikolsky
committed
self.client
.typed_state_call::<_, Option<HeaderIdOf<P::SourceRelayChain>>>(
P::SourceRelayChain::BEST_FINALIZED_HEADER_ID_METHOD.into(),
Svyatoslav Nikolsky
committed
(),
Svyatoslav Nikolsky
committed
.await?
.map(Ok)
.unwrap_or(Err(SubstrateError::BridgePalletIsNotInitialized))
}
async fn parachain_head(
&self,
at_block: HeaderIdOf<P::TargetChain>,
Svyatoslav Nikolsky
committed
) -> Result<Option<HeaderIdOf<P::SourceParachain>>, Self::Error> {
let encoded_best_finalized_source_para_block = self
Svyatoslav Nikolsky
committed
.state_call(
P::SourceParachain::BEST_FINALIZED_HEADER_ID_METHOD.into(),
Bytes(Vec::new()),
Svyatoslav Nikolsky
committed
.await?;
Svyatoslav Nikolsky
committed
Ok(Option::<HeaderIdOf<P::SourceParachain>>::decode(
&mut &encoded_best_finalized_source_para_block.0[..],
)
.map_err(SubstrateError::ResponseParseFailed)?)
Svyatoslav Nikolsky
committed
async fn submit_parachain_head_proof(
at_relay_block: HeaderIdOf<P::SourceRelayChain>,
Svyatoslav Nikolsky
committed
updated_head_hash: ParaHash,
) -> Result<Self::TransactionTracker, Self::Error> {
let transaction_params = self.transaction_params.clone();
let call = P::SubmitParachainHeadsCallBuilder::build_submit_parachain_heads_call(
Svyatoslav Nikolsky
committed
vec![(ParaId(P::SourceParachain::PARACHAIN_ID), updated_head_hash)],
.submit_and_watch_signed_extrinsic(
move |best_block_id, transaction_nonce| {
Ok(UnsignedTransaction::new(call.into(), transaction_nonce)
.era(TransactionEra::new(best_block_id, transaction_params.mortality)))