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 {
fn clone(&self) -> Self {
SubstrateFinalityTarget {
client: self.client.clone(),
transaction_params: self.transaction_params.clone(),
}
}
}
#[async_trait]
impl RelayClient for SubstrateFinalityTarget {
type Error = Error;
async fn reconnect(&mut self) -> Result<(), Error> {
self.client.reconnect().await
}
}
#[async_trait]
impl TargetClient>
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 submit_finality_proof(
&self,
header: SyncHeader>,
mut proof: SubstrateFinalityProof,
) -> Result {
// runtime module at target chain may require optimized finality proof
P::FinalityEngine::optimize_proof(&self.client, &header, &mut proof).await?;
// now we may submit optimized finality proof
let mortality = self.transaction_params.mortality;
let call =
P::SubmitFinalityProofCallBuilder::build_submit_finality_proof_call(header, proof);
self.client
.submit_and_watch_signed_extrinsic(
&self.transaction_params.signer,
move |best_block_id, transaction_nonce| {
Ok(UnsignedTransaction::new(call.into(), transaction_nonce)
.era(TransactionEra::new(best_block_id, mortality)))
},
)
.await
}
}