use crate::{
equivocation::{
EquivocationDetectionPipelineAdapter, EquivocationProofOf, ReportEquivocationCallBuilder,
SubstrateEquivocationDetectionPipeline,
},
finality_base::{engine::Engine, finality_proofs, SubstrateFinalityProofsStream},
TransactionParams,
};
use async_trait::async_trait;
use bp_runtime::{HashOf, TransactionEra};
use equivocation_detector::SourceClient;
use finality_relay::SourceClientBase;
use relay_substrate_client::{
AccountKeyPairOf, Client, Error, TransactionTracker, UnsignedTransaction,
};
use relay_utils::relay_loop::Client as RelayClient;
pub struct SubstrateEquivocationSource<P: SubstrateEquivocationDetectionPipeline, SourceClnt> {
client: SourceClnt,
transaction_params: TransactionParams<AccountKeyPairOf<P::SourceChain>>,
}
impl<P: SubstrateEquivocationDetectionPipeline, SourceClnt: Client<P::SourceChain>>
SubstrateEquivocationSource<P, SourceClnt>
{
pub fn new(
client: SourceClnt,
transaction_params: TransactionParams<AccountKeyPairOf<P::SourceChain>>,
) -> Self {
Self { client, transaction_params }
}
}
impl<P: SubstrateEquivocationDetectionPipeline, SourceClnt: Client<P::SourceChain>> Clone
for SubstrateEquivocationSource<P, SourceClnt>
{
fn clone(&self) -> Self {
Self { client: self.client.clone(), transaction_params: self.transaction_params.clone() }
}
}
#[async_trait]
impl<P: SubstrateEquivocationDetectionPipeline, SourceClnt: Client<P::SourceChain>> RelayClient
for SubstrateEquivocationSource<P, SourceClnt>
{
type Error = Error;
async fn reconnect(&mut self) -> Result<(), Error> {
self.client.reconnect().await
}
}
#[async_trait]
impl<P: SubstrateEquivocationDetectionPipeline, SourceClnt: Client<P::SourceChain>>
SourceClientBase<EquivocationDetectionPipelineAdapter<P>>
for SubstrateEquivocationSource<P, SourceClnt>
{
type FinalityProofsStream = SubstrateFinalityProofsStream<P>;
async fn finality_proofs(&self) -> Result<Self::FinalityProofsStream, Error> {
finality_proofs::<P>(&self.client).await
}
}
#[async_trait]
impl<P: SubstrateEquivocationDetectionPipeline, SourceClnt: Client<P::SourceChain>>
SourceClient<EquivocationDetectionPipelineAdapter<P>>
for SubstrateEquivocationSource<P, SourceClnt>
{
type TransactionTracker = TransactionTracker<P::SourceChain, SourceClnt>;
async fn report_equivocation(
&self,
at: HashOf<P::SourceChain>,
equivocation: EquivocationProofOf<P>,
) -> Result<Self::TransactionTracker, Self::Error> {
let key_owner_proof =
P::FinalityEngine::generate_source_key_ownership_proof(&self.client, at, &equivocation)
.await?;
let mortality = self.transaction_params.mortality;
let call = P::ReportEquivocationCallBuilder::build_report_equivocation_call(
equivocation,
key_owner_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
}
}