referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.

// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.

//! The library of substrate relay. contains some public codes to provide to substrate relay.

#![warn(missing_docs)]

use relay_substrate_client::{Chain, ChainWithUtilityPallet, UtilityPallet};

use std::marker::PhantomData;

// to avoid `finality_relay` dependency in other crates
pub use finality_relay::HeadersToRelay;

pub mod cli;
pub mod equivocation;
pub mod error;
pub mod finality;
pub mod finality_base;
pub mod messages;
pub mod on_demand;
pub mod parachains;

/// Transaction creation parameters.
#[derive(Clone, Debug)]
pub struct TransactionParams<TS> {
	/// Transactions author.
	pub signer: TS,
	/// Transactions mortality.
	pub mortality: Option<u32>,
}

/// Tagged relay account, which balance may be exposed as metrics by the relay.
#[derive(Clone, Debug)]
pub enum TaggedAccount<AccountId> {
	/// Account, used to sign message (also headers and parachains) relay transactions from given
	/// bridged chain.
	Messages {
		/// Account id.
		id: AccountId,
		/// Name of the bridged chain, which sends us messages or delivery confirmations.
		bridged_chain: String,
	},
}

impl<AccountId> TaggedAccount<AccountId> {
	/// Returns reference to the account id.
	pub fn id(&self) -> &AccountId {
		match *self {
			TaggedAccount::Messages { ref id, .. } => id,
		}
	}

	/// Returns stringified account tag.
	pub fn tag(&self) -> String {
		match *self {
			TaggedAccount::Messages { ref bridged_chain, .. } => {
				format!("{bridged_chain}Messages")
			},
		}
	}
}

/// Batch call builder.
pub trait BatchCallBuilder<Call>: Clone + Send + Sync {
	/// Create batch call from given calls vector.
	fn build_batch_call(&self, _calls: Vec<Call>) -> Call;
}

/// Batch call builder constructor.
pub trait BatchCallBuilderConstructor<Call>: Clone {
	/// Call builder, used by this constructor.
	type CallBuilder: BatchCallBuilder<Call>;
	/// Create a new instance of a batch call builder.
	fn new_builder() -> Option<Self::CallBuilder>;
}

/// Batch call builder based on `pallet-utility`.
#[derive(Clone)]
pub struct UtilityPalletBatchCallBuilder<C: Chain>(PhantomData<C>);

impl<C: Chain> BatchCallBuilder<C::Call> for UtilityPalletBatchCallBuilder<C>
where
	C: ChainWithUtilityPallet,
{
	fn build_batch_call(&self, calls: Vec<C::Call>) -> C::Call {
		C::UtilityPallet::build_batch_call(calls)
	}
}

impl<C: Chain> BatchCallBuilderConstructor<C::Call> for UtilityPalletBatchCallBuilder<C>
where
	C: ChainWithUtilityPallet,
{
	type CallBuilder = Self;

	fn new_builder() -> Option<Self::CallBuilder> {
		Some(Self(Default::default()))
	}
}

// A `BatchCallBuilderConstructor` that always returns `None`.
impl<Call> BatchCallBuilderConstructor<Call> for () {
	type CallBuilder = ();
	fn new_builder() -> Option<Self::CallBuilder> {
		None
	}
}

// Dummy `BatchCallBuilder` implementation that must never be used outside
// of the `impl BatchCallBuilderConstructor for ()` code.
impl<Call> BatchCallBuilder<Call> for () {
	fn build_batch_call(&self, _calls: Vec<Call>) -> Call {
		unreachable!("never called, because ()::new_builder() returns None; qed")
	}
}

/// Module for handling storage proofs compatibility.
pub mod proofs {
	use bp_runtime::{HashOf, RawStorageProof};
	use relay_substrate_client::Chain;
	use sp_trie::StorageProof;

	/// Converts proof to `RawStorageProof` type.
	pub fn to_raw_storage_proof<SourceChain: Chain>(
		proof: (StorageProof, HashOf<SourceChain>),
	) -> RawStorageProof {
		proof.0.into_iter_nodes().collect()
	}
}