referrerpolicy=no-referrer-when-downgrade

substrate_relay_helper/equivocation/
target.rs

1// Copyright 2019-2023 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Default generic implementation of equivocation source for basic Substrate client.
18
19use crate::{
20	equivocation::{
21		EquivocationDetectionPipelineAdapter, FinalityProoffOf, FinalityVerificationContextfOf,
22		SubstrateEquivocationDetectionPipeline,
23	},
24	finality_base::{best_synced_header_id, engine::Engine},
25};
26
27use async_trait::async_trait;
28use bp_header_chain::HeaderFinalityInfo;
29use bp_runtime::{BlockNumberOf, HashOf};
30use equivocation_detector::TargetClient;
31use relay_substrate_client::{Client, Error};
32use relay_utils::relay_loop::Client as RelayClient;
33use sp_runtime::traits::Header;
34use std::marker::PhantomData;
35
36/// Substrate node as equivocation source.
37pub struct SubstrateEquivocationTarget<P: SubstrateEquivocationDetectionPipeline, TargetClnt> {
38	client: TargetClnt,
39
40	_phantom: PhantomData<P>,
41}
42
43impl<P: SubstrateEquivocationDetectionPipeline, TargetClnt: Client<P::TargetChain>>
44	SubstrateEquivocationTarget<P, TargetClnt>
45{
46	/// Create new instance of `SubstrateEquivocationTarget`.
47	pub fn new(client: TargetClnt) -> Self {
48		Self { client, _phantom: Default::default() }
49	}
50}
51
52impl<P: SubstrateEquivocationDetectionPipeline, TargetClnt: Client<P::TargetChain>> Clone
53	for SubstrateEquivocationTarget<P, TargetClnt>
54{
55	fn clone(&self) -> Self {
56		Self { client: self.client.clone(), _phantom: Default::default() }
57	}
58}
59
60#[async_trait]
61impl<P: SubstrateEquivocationDetectionPipeline, TargetClnt: Client<P::TargetChain>> RelayClient
62	for SubstrateEquivocationTarget<P, TargetClnt>
63{
64	type Error = Error;
65
66	async fn reconnect(&mut self) -> Result<(), Error> {
67		self.client.reconnect().await
68	}
69}
70
71#[async_trait]
72impl<P: SubstrateEquivocationDetectionPipeline, TargetClnt: Client<P::TargetChain>>
73	TargetClient<EquivocationDetectionPipelineAdapter<P>>
74	for SubstrateEquivocationTarget<P, TargetClnt>
75{
76	async fn best_finalized_header_number(
77		&self,
78	) -> Result<BlockNumberOf<P::TargetChain>, Self::Error> {
79		self.client.best_finalized_header_number().await
80	}
81
82	async fn best_synced_header_hash(
83		&self,
84		at: BlockNumberOf<P::TargetChain>,
85	) -> Result<Option<HashOf<P::SourceChain>>, Self::Error> {
86		Ok(best_synced_header_id::<P::SourceChain, P::TargetChain>(
87			&self.client,
88			self.client.header_by_number(at).await?.hash(),
89		)
90		.await?
91		.map(|id| id.hash()))
92	}
93
94	async fn finality_verification_context(
95		&self,
96		at: BlockNumberOf<P::TargetChain>,
97	) -> Result<FinalityVerificationContextfOf<P>, Self::Error> {
98		P::FinalityEngine::finality_verification_context(
99			&self.client,
100			self.client.header_by_number(at).await?.hash(),
101		)
102		.await
103	}
104
105	async fn synced_headers_finality_info(
106		&self,
107		at: BlockNumberOf<P::TargetChain>,
108	) -> Result<
109		Vec<HeaderFinalityInfo<FinalityProoffOf<P>, FinalityVerificationContextfOf<P>>>,
110		Self::Error,
111	> {
112		P::FinalityEngine::synced_headers_finality_info(
113			&self.client,
114			self.client.header_by_number(at).await?.hash(),
115		)
116		.await
117	}
118}