SubstrateFinalityTarget {
/// Create new Substrate headers target.
pub fn new(
client: Client,
transaction_params: TransactionParams>,
) -> Self {
SubstrateFinalityTarget { client, transaction_params }
}
/// Ensure that the GRANDPA pallet at target chain is active.
async fn ensure_pallet_active(&self) -> Result<(), Error> {
let is_halted = self
.client
.storage_value(is_halted_key(P::SourceChain::WITH_CHAIN_GRANDPA_PALLET_NAME), None)
.await?;
if is_halted.unwrap_or(false) {
Err(Error::BridgePalletIsHalted)
} else {
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
where
AccountIdOf: From< as Pair>::Public>,
P::TransactionSignScheme: TransactionSignScheme,
{
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 GRANDPA pallet at target chain is halted
self.ensure_pallet_active().await?;
Ok(crate::messages_source::read_client_state::(
&self.client,
None,
P::SourceChain::BEST_FINALIZED_HEADER_ID_METHOD,
)
.await?
.best_finalized_peer_at_best_self)
}
async fn submit_finality_proof(
&self,
header: SyncHeader>,
proof: GrandpaJustification>,
) -> Result<(), Error> {
let genesis_hash = *self.client.genesis_hash();
let transaction_params = self.transaction_params.clone();
let call =
P::SubmitFinalityProofCallBuilder::build_submit_finality_proof_call(header, proof);
let (spec_version, transaction_version) = self.client.simple_runtime_version().await?;
self.client
.submit_signed_extrinsic(
self.transaction_params.signer.public().into(),
move |best_block_id, transaction_nonce| {
Ok(Bytes(
P::TransactionSignScheme::sign_transaction(SignParam {
spec_version,
transaction_version,
genesis_hash,
signer: transaction_params.signer.clone(),
era: TransactionEra::new(best_block_id, transaction_params.mortality),
unsigned: UnsignedTransaction::new(call.into(), transaction_nonce),
})?
.encode(),
))
},
)
.await
.map(drop)
}
}