referrerpolicy=no-referrer-when-downgrade

substrate_relay_helper/
lib.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//! The library of substrate relay. contains some public codes to provide to substrate relay.
18
19#![warn(missing_docs)]
20
21use relay_substrate_client::{Chain, ChainWithUtilityPallet, UtilityPallet};
22
23use std::marker::PhantomData;
24
25// to avoid `finality_relay` dependency in other crates
26pub use finality_relay::HeadersToRelay;
27
28pub mod cli;
29pub mod equivocation;
30pub mod error;
31pub mod finality;
32pub mod finality_base;
33pub mod messages;
34pub mod on_demand;
35pub mod parachains;
36
37/// Transaction creation parameters.
38#[derive(Clone, Debug)]
39pub struct TransactionParams<TS> {
40	/// Transactions author.
41	pub signer: TS,
42	/// Transactions mortality.
43	pub mortality: Option<u32>,
44}
45
46/// Tagged relay account, which balance may be exposed as metrics by the relay.
47#[derive(Clone, Debug)]
48pub enum TaggedAccount<AccountId> {
49	/// Account, used to sign message (also headers and parachains) relay transactions from given
50	/// bridged chain.
51	Messages {
52		/// Account id.
53		id: AccountId,
54		/// Name of the bridged chain, which sends us messages or delivery confirmations.
55		bridged_chain: String,
56	},
57}
58
59impl<AccountId> TaggedAccount<AccountId> {
60	/// Returns reference to the account id.
61	pub fn id(&self) -> &AccountId {
62		match *self {
63			TaggedAccount::Messages { ref id, .. } => id,
64		}
65	}
66
67	/// Returns stringified account tag.
68	pub fn tag(&self) -> String {
69		match *self {
70			TaggedAccount::Messages { ref bridged_chain, .. } => {
71				format!("{bridged_chain}Messages")
72			},
73		}
74	}
75}
76
77/// Batch call builder.
78pub trait BatchCallBuilder<Call>: Clone + Send + Sync {
79	/// Create batch call from given calls vector.
80	fn build_batch_call(&self, _calls: Vec<Call>) -> Call;
81}
82
83/// Batch call builder constructor.
84pub trait BatchCallBuilderConstructor<Call>: Clone {
85	/// Call builder, used by this constructor.
86	type CallBuilder: BatchCallBuilder<Call>;
87	/// Create a new instance of a batch call builder.
88	fn new_builder() -> Option<Self::CallBuilder>;
89}
90
91/// Batch call builder based on `pallet-utility`.
92#[derive(Clone)]
93pub struct UtilityPalletBatchCallBuilder<C: Chain>(PhantomData<C>);
94
95impl<C: Chain> BatchCallBuilder<C::Call> for UtilityPalletBatchCallBuilder<C>
96where
97	C: ChainWithUtilityPallet,
98{
99	fn build_batch_call(&self, calls: Vec<C::Call>) -> C::Call {
100		C::UtilityPallet::build_batch_call(calls)
101	}
102}
103
104impl<C: Chain> BatchCallBuilderConstructor<C::Call> for UtilityPalletBatchCallBuilder<C>
105where
106	C: ChainWithUtilityPallet,
107{
108	type CallBuilder = Self;
109
110	fn new_builder() -> Option<Self::CallBuilder> {
111		Some(Self(Default::default()))
112	}
113}
114
115// A `BatchCallBuilderConstructor` that always returns `None`.
116impl<Call> BatchCallBuilderConstructor<Call> for () {
117	type CallBuilder = ();
118	fn new_builder() -> Option<Self::CallBuilder> {
119		None
120	}
121}
122
123// Dummy `BatchCallBuilder` implementation that must never be used outside
124// of the `impl BatchCallBuilderConstructor for ()` code.
125impl<Call> BatchCallBuilder<Call> for () {
126	fn build_batch_call(&self, _calls: Vec<Call>) -> Call {
127		unreachable!("never called, because ()::new_builder() returns None; qed")
128	}
129}
130
131/// Module for handling storage proofs compatibility.
132pub mod proofs {
133	use bp_runtime::{HashOf, RawStorageProof};
134	use relay_substrate_client::Chain;
135	use sp_trie::StorageProof;
136
137	/// Converts proof to `RawStorageProof` type.
138	pub fn to_raw_storage_proof<SourceChain: Chain>(
139		proof: (StorageProof, HashOf<SourceChain>),
140	) -> RawStorageProof {
141		proof.0.into_iter_nodes().collect()
142	}
143}