use crate::{
parachains::{
ParachainsPipelineAdapter, SubmitParachainHeadsCallBuilder, SubstrateParachainsPipeline,
},
TransactionParams,
};
use async_trait::async_trait;
use bp_parachains::{
ImportedParaHeadsKeyProvider, ParaInfo, ParaStoredHeaderData, ParasInfoKeyProvider,
};
use bp_polkadot_core::{
parachains::{ParaHash, ParaHeadsProof, ParaId},
BlockNumber as RelayBlockNumber,
};
use bp_runtime::{
Chain as ChainBase, HeaderId, HeaderIdProvider, StorageDoubleMapKeyProvider,
StorageMapKeyProvider,
};
use parachains_relay::parachains_loop::TargetClient;
use relay_substrate_client::{
AccountIdOf, AccountKeyPairOf, BlockNumberOf, Chain, Client, Error as SubstrateError,
HeaderIdOf, ParachainBase, RelayChain, TransactionEra, TransactionTracker, UnsignedTransaction,
};
use relay_utils::relay_loop::Client as RelayClient;
use sp_core::Pair;
use sp_runtime::traits::Header;
pub struct ParachainsTarget<P: SubstrateParachainsPipeline, SourceClnt, TargetClnt> {
source_client: SourceClnt,
target_client: TargetClnt,
transaction_params: TransactionParams<AccountKeyPairOf<P::TargetChain>>,
}
impl<
P: SubstrateParachainsPipeline,
SourceClnt: Client<P::SourceRelayChain>,
TargetClnt: Client<P::TargetChain>,
> ParachainsTarget<P, SourceClnt, TargetClnt>
{
pub fn new(
source_client: SourceClnt,
target_client: TargetClnt,
transaction_params: TransactionParams<AccountKeyPairOf<P::TargetChain>>,
) -> Self {
ParachainsTarget { source_client, target_client, transaction_params }
}
pub fn target_client(&self) -> &TargetClnt {
&self.target_client
}
}
impl<
P: SubstrateParachainsPipeline,
SourceClnt: Client<P::SourceRelayChain>,
TargetClnt: Clone,
> Clone for ParachainsTarget<P, SourceClnt, TargetClnt>
{
fn clone(&self) -> Self {
ParachainsTarget {
source_client: self.source_client.clone(),
target_client: self.target_client.clone(),
transaction_params: self.transaction_params.clone(),
}
}
}
#[async_trait]
impl<
P: SubstrateParachainsPipeline,
SourceClnt: Client<P::SourceRelayChain>,
TargetClnt: Client<P::TargetChain>,
> RelayClient for ParachainsTarget<P, SourceClnt, TargetClnt>
{
type Error = SubstrateError;
async fn reconnect(&mut self) -> Result<(), SubstrateError> {
self.target_client.reconnect().await?;
self.source_client.reconnect().await?;
Ok(())
}
}
#[async_trait]
impl<P, SourceClnt, TargetClnt> TargetClient<ParachainsPipelineAdapter<P>>
for ParachainsTarget<P, SourceClnt, TargetClnt>
where
P: SubstrateParachainsPipeline,
SourceClnt: Client<P::SourceRelayChain>,
TargetClnt: Client<P::TargetChain>,
AccountIdOf<P::TargetChain>: From<<AccountKeyPairOf<P::TargetChain> as Pair>::Public>,
P::SourceParachain: ChainBase<Hash = ParaHash>,
P::SourceRelayChain: ChainBase<BlockNumber = RelayBlockNumber>,
{
type TransactionTracker = TransactionTracker<P::TargetChain, TargetClnt>;
async fn best_block(&self) -> Result<HeaderIdOf<P::TargetChain>, Self::Error> {
let best_header = self.target_client.best_header().await?;
let best_id = best_header.id();
Ok(best_id)
}
async fn best_finalized_source_relay_chain_block(
&self,
at_block: &HeaderIdOf<P::TargetChain>,
) -> Result<HeaderIdOf<P::SourceRelayChain>, Self::Error> {
self.target_client
.state_call::<_, Option<HeaderIdOf<P::SourceRelayChain>>>(
at_block.hash(),
P::SourceRelayChain::BEST_FINALIZED_HEADER_ID_METHOD.into(),
(),
)
.await?
.map(Ok)
.unwrap_or(Err(SubstrateError::BridgePalletIsNotInitialized))
}
async fn free_source_relay_headers_interval(
&self,
) -> Result<Option<BlockNumberOf<P::SourceRelayChain>>, Self::Error> {
Ok(self
.target_client
.state_call(
self.target_client.best_header().await?.hash(),
P::SourceRelayChain::FREE_HEADERS_INTERVAL_METHOD.into(),
(),
)
.await
.unwrap_or_else(|e| {
log::info!(
target: "bridge",
"Call of {} at {} has failed with an error: {:?}. Treating as `None`",
P::SourceRelayChain::FREE_HEADERS_INTERVAL_METHOD,
P::TargetChain::NAME,
e,
);
None
}))
}
async fn parachain_head(
&self,
at_block: HeaderIdOf<P::TargetChain>,
) -> Result<
Option<(HeaderIdOf<P::SourceRelayChain>, HeaderIdOf<P::SourceParachain>)>,
Self::Error,
> {
let storage_key = ParasInfoKeyProvider::final_key(
P::SourceRelayChain::WITH_CHAIN_BRIDGE_PARACHAINS_PALLET_NAME,
&P::SourceParachain::PARACHAIN_ID.into(),
);
let storage_value: Option<ParaInfo> =
self.target_client.storage_value(at_block.hash(), storage_key).await?;
let para_info = match storage_value {
Some(para_info) => para_info,
None => return Ok(None),
};
let relay_header_id = self
.source_client
.header_by_number(para_info.best_head_hash.at_relay_block_number)
.await?
.id();
let storage_key = ImportedParaHeadsKeyProvider::final_key(
P::SourceRelayChain::WITH_CHAIN_BRIDGE_PARACHAINS_PALLET_NAME,
&P::SourceParachain::PARACHAIN_ID.into(),
¶_info.best_head_hash.head_hash,
);
let storage_value: Option<ParaStoredHeaderData> =
self.target_client.storage_value(at_block.hash(), storage_key).await?;
let para_head_number = match storage_value {
Some(para_head_data) =>
para_head_data.decode_parachain_head_data::<P::SourceParachain>()?.number,
None => return Ok(None),
};
let para_head_id = HeaderId(para_head_number, para_info.best_head_hash.head_hash);
Ok(Some((relay_header_id, para_head_id)))
}
async fn submit_parachain_head_proof(
&self,
at_relay_block: HeaderIdOf<P::SourceRelayChain>,
updated_head_hash: ParaHash,
proof: ParaHeadsProof,
is_free_execution_expected: bool,
) -> Result<Self::TransactionTracker, Self::Error> {
let transaction_params = self.transaction_params.clone();
let call = P::SubmitParachainHeadsCallBuilder::build_submit_parachain_heads_call(
at_relay_block,
vec![(ParaId(P::SourceParachain::PARACHAIN_ID), updated_head_hash)],
proof,
is_free_execution_expected,
);
self.target_client
.submit_and_watch_signed_extrinsic(
&transaction_params.signer,
move |best_block_id, transaction_nonce| {
Ok(UnsignedTransaction::new(call.into(), transaction_nonce)
.era(TransactionEra::new(best_block_id, transaction_params.mortality)))
},
)
.await
}
}