substrate_relay_helper/cli/bridge.rs
1// Copyright 2019-2021 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//! Basic traits for exposing bridges in the CLI.
18
19use crate::{
20 equivocation::SubstrateEquivocationDetectionPipeline,
21 finality::SubstrateFinalitySyncPipeline,
22 messages::{MessagesRelayLimits, SubstrateMessageLane},
23 parachains::SubstrateParachainsPipeline,
24};
25use bp_parachains::{RelayBlockHash, RelayBlockHasher, RelayBlockNumber};
26use relay_substrate_client::{
27 Chain, ChainWithRuntimeVersion, ChainWithTransactions, Parachain, RelayChain,
28};
29
30/// Minimal bridge representation that can be used from the CLI.
31/// It connects a source chain to a target chain.
32pub trait CliBridgeBase: Sized {
33 /// The source chain.
34 type Source: Chain + ChainWithRuntimeVersion;
35 /// The target chain.
36 type Target: ChainWithTransactions + ChainWithRuntimeVersion;
37}
38
39/// Bridge representation that can be used from the CLI for relaying headers
40/// from a relay chain to a relay chain.
41pub trait RelayToRelayHeadersCliBridge: CliBridgeBase {
42 /// Finality proofs synchronization pipeline.
43 type Finality: SubstrateFinalitySyncPipeline<
44 SourceChain = Self::Source,
45 TargetChain = Self::Target,
46 >;
47}
48
49/// Convenience trait that adds bounds to `CliBridgeBase`.
50pub trait RelayToRelayEquivocationDetectionCliBridgeBase: CliBridgeBase {
51 /// The source chain with extra bounds.
52 type BoundedSource: ChainWithTransactions;
53}
54
55impl<T> RelayToRelayEquivocationDetectionCliBridgeBase for T
56where
57 T: CliBridgeBase,
58 T::Source: ChainWithTransactions,
59{
60 type BoundedSource = T::Source;
61}
62
63/// Bridge representation that can be used from the CLI for detecting equivocations
64/// in the headers synchronized from a relay chain to a relay chain.
65pub trait RelayToRelayEquivocationDetectionCliBridge:
66 RelayToRelayEquivocationDetectionCliBridgeBase
67{
68 /// Equivocation detection pipeline.
69 type Equivocation: SubstrateEquivocationDetectionPipeline<
70 SourceChain = Self::Source,
71 TargetChain = Self::Target,
72 >;
73}
74
75/// Bridge representation that can be used from the CLI for relaying headers
76/// from a parachain to a relay chain.
77pub trait ParachainToRelayHeadersCliBridge: CliBridgeBase
78where
79 Self::Source: Parachain,
80{
81 /// The `CliBridgeBase` type represents the parachain in this situation.
82 /// We need to add an extra type for the relay chain.
83 type SourceRelay: Chain<BlockNumber = RelayBlockNumber, Hash = RelayBlockHash, Hasher = RelayBlockHasher>
84 + ChainWithRuntimeVersion
85 + RelayChain;
86 /// Finality proofs synchronization pipeline (source parachain -> target).
87 type ParachainFinality: SubstrateParachainsPipeline<
88 SourceRelayChain = Self::SourceRelay,
89 SourceParachain = Self::Source,
90 TargetChain = Self::Target,
91 >;
92 /// Finality proofs synchronization pipeline (source relay chain -> target).
93 type RelayFinality: SubstrateFinalitySyncPipeline<
94 SourceChain = Self::SourceRelay,
95 TargetChain = Self::Target,
96 >;
97}
98
99/// Bridge representation that can be used from the CLI for relaying messages.
100pub trait MessagesCliBridge: CliBridgeBase {
101 /// The Source -> Destination messages synchronization pipeline.
102 type MessagesLane: SubstrateMessageLane<SourceChain = Self::Source, TargetChain = Self::Target>;
103
104 /// Optional messages delivery transaction limits that the messages relay is going
105 /// to use. If it returns `None`, limits are estimated using `TransactionPayment` API
106 /// at the target chain.
107 fn maybe_messages_limits() -> Option<MessagesRelayLimits> {
108 None
109 }
110}
111
112/// An alias for lane identifier type.
113pub type MessagesLaneIdOf<B> =
114 <<B as MessagesCliBridge>::MessagesLane as SubstrateMessageLane>::LaneId;