Skip to main content

bp_runtime/
lib.rs

1// Copyright (C) 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//! Primitives that may be used at (bridges) runtime level.
18
19#![warn(missing_docs)]
20#![cfg_attr(not(feature = "std"), no_std)]
21
22use codec::{Decode, DecodeWithMemTracking, Encode, FullCodec, MaxEncodedLen};
23use frame_support::{
24	pallet_prelude::DispatchResult, weights::Weight, PalletError, StorageHasher, StorageValue,
25};
26use frame_system::RawOrigin;
27use scale_info::TypeInfo;
28use serde::{Deserialize, Serialize};
29use sp_core::storage::StorageKey;
30use sp_runtime::{
31	traits::{BadOrigin, Header as HeaderT, UniqueSaturatedInto},
32	Debug,
33};
34use sp_std::{ops::RangeInclusive, vec, vec::Vec};
35
36pub use chain::{
37	AccountIdOf, AccountPublicOf, BalanceOf, BlockNumberOf, Chain, EncodedOrDecodedCall, HashOf,
38	HasherOf, HeaderOf, NonceOf, Parachain, ParachainIdOf, SignatureOf, TransactionEraOf,
39	UnderlyingChainOf, UnderlyingChainProvider, __private,
40};
41pub use frame_support::storage::storage_prefix as storage_value_final_key;
42use num_traits::{CheckedAdd, CheckedSub, One, SaturatingAdd, Zero};
43#[cfg(feature = "std")]
44pub use storage_proof::craft_valid_storage_proof;
45#[cfg(feature = "test-helpers")]
46pub use storage_proof::{
47	grow_storage_proof, grow_storage_value, record_all_keys as record_all_trie_keys,
48	UnverifiedStorageProofParams,
49};
50pub use storage_proof::{
51	raw_storage_proof_size, RawStorageProof, StorageProofChecker, StorageProofError,
52};
53pub use storage_types::BoundedStorageValue;
54
55extern crate alloc;
56
57pub mod extensions;
58pub mod messages;
59
60mod chain;
61mod storage_proof;
62mod storage_types;
63
64// Re-export macro to avoid include paste dependency everywhere
65pub use sp_runtime::paste;
66
67// Re-export for usage in macro.
68#[doc(hidden)]
69pub mod private {
70	#[doc(hidden)]
71	pub use alloc::vec::Vec;
72}
73
74/// Use this when something must be shared among all instances.
75pub const NO_INSTANCE_ID: ChainId = [0, 0, 0, 0];
76
77/// Generic header Id.
78#[derive(
79	Debug,
80	Default,
81	Clone,
82	Encode,
83	Decode,
84	Copy,
85	Eq,
86	Hash,
87	MaxEncodedLen,
88	PartialEq,
89	PartialOrd,
90	Ord,
91	TypeInfo,
92)]
93pub struct HeaderId<Hash, Number>(pub Number, pub Hash);
94
95impl<Hash: Copy, Number: Copy> HeaderId<Hash, Number> {
96	/// Return header number.
97	pub fn number(&self) -> Number {
98		self.0
99	}
100
101	/// Return header hash.
102	pub fn hash(&self) -> Hash {
103		self.1
104	}
105}
106
107/// Header id used by the chain.
108pub type HeaderIdOf<C> = HeaderId<HashOf<C>, BlockNumberOf<C>>;
109
110/// Generic header id provider.
111pub trait HeaderIdProvider<Header: HeaderT> {
112	/// Get the header id.
113	fn id(&self) -> HeaderId<Header::Hash, Header::Number>;
114
115	/// Get the header id for the parent block.
116	fn parent_id(&self) -> Option<HeaderId<Header::Hash, Header::Number>>;
117}
118
119impl<Header: HeaderT> HeaderIdProvider<Header> for Header {
120	fn id(&self) -> HeaderId<Header::Hash, Header::Number> {
121		HeaderId(*self.number(), self.hash())
122	}
123
124	fn parent_id(&self) -> Option<HeaderId<Header::Hash, Header::Number>> {
125		self.number()
126			.checked_sub(&One::one())
127			.map(|parent_number| HeaderId(parent_number, *self.parent_hash()))
128	}
129}
130
131/// Unique identifier of the chain.
132///
133/// In addition to its main function (identifying the chain), this type may also be used to
134/// identify module instance. We have a bunch of pallets that may be used in different bridges. E.g.
135/// messages pallet may be deployed twice in the same runtime to bridge ThisChain with Chain1 and
136/// Chain2. Sometimes we need to be able to identify deployed instance dynamically. This type may be
137/// used for that.
138pub type ChainId = [u8; 4];
139
140/// Anything that has size.
141pub trait Size {
142	/// Return size of this object (in bytes).
143	fn size(&self) -> u32;
144}
145
146impl Size for () {
147	fn size(&self) -> u32 {
148		0
149	}
150}
151
152impl Size for Vec<u8> {
153	fn size(&self) -> u32 {
154		self.len() as _
155	}
156}
157
158/// Pre-computed size.
159pub struct PreComputedSize(pub usize);
160
161impl Size for PreComputedSize {
162	fn size(&self) -> u32 {
163		u32::try_from(self.0).unwrap_or(u32::MAX)
164	}
165}
166
167/// Era of specific transaction.
168#[derive(Debug, Clone, Copy, PartialEq)]
169pub enum TransactionEra<BlockNumber, BlockHash> {
170	/// Transaction is immortal.
171	Immortal,
172	/// Transaction is valid for a given number of blocks, starting from given block.
173	Mortal(HeaderId<BlockHash, BlockNumber>, u32),
174}
175
176impl<BlockNumber: Copy + UniqueSaturatedInto<u64>, BlockHash: Copy>
177	TransactionEra<BlockNumber, BlockHash>
178{
179	/// Prepare transaction era, based on mortality period and current best block number.
180	pub fn new(
181		best_block_id: HeaderId<BlockHash, BlockNumber>,
182		mortality_period: Option<u32>,
183	) -> Self {
184		mortality_period
185			.map(|mortality_period| TransactionEra::Mortal(best_block_id, mortality_period))
186			.unwrap_or(TransactionEra::Immortal)
187	}
188
189	/// Create new immortal transaction era.
190	pub fn immortal() -> Self {
191		TransactionEra::Immortal
192	}
193
194	/// Returns mortality period if transaction is mortal.
195	pub fn mortality_period(&self) -> Option<u32> {
196		match *self {
197			TransactionEra::Immortal => None,
198			TransactionEra::Mortal(_, period) => Some(period),
199		}
200	}
201
202	/// Returns era that is used by FRAME-based runtimes.
203	pub fn frame_era(&self) -> sp_runtime::generic::Era {
204		match *self {
205			TransactionEra::Immortal => sp_runtime::generic::Era::immortal(),
206			// `unique_saturated_into` is fine here - mortality `u64::MAX` is not something we
207			// expect to see on any chain
208			TransactionEra::Mortal(header_id, period) => {
209				sp_runtime::generic::Era::mortal(period as _, header_id.0.unique_saturated_into())
210			},
211		}
212	}
213
214	/// Returns header hash that needs to be included in the signature payload.
215	pub fn signed_payload(&self, genesis_hash: BlockHash) -> BlockHash {
216		match *self {
217			TransactionEra::Immortal => genesis_hash,
218			TransactionEra::Mortal(header_id, _) => header_id.1,
219		}
220	}
221}
222
223/// This is a copy of the
224/// `frame_support::storage::generator::StorageMap::storage_map_final_key` for maps based
225/// on selected hasher.
226///
227/// We're using it because to call `storage_map_final_key` directly, we need access to the runtime
228/// and pallet instance, which (sometimes) is impossible.
229pub fn storage_map_final_key<H: StorageHasher>(
230	pallet_prefix: &str,
231	map_name: &str,
232	key: &[u8],
233) -> StorageKey {
234	let key_hashed = H::hash(key);
235	let pallet_prefix_hashed = frame_support::Twox128::hash(pallet_prefix.as_bytes());
236	let storage_prefix_hashed = frame_support::Twox128::hash(map_name.as_bytes());
237
238	let mut final_key = Vec::with_capacity(
239		pallet_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.as_ref().len(),
240	);
241
242	final_key.extend_from_slice(&pallet_prefix_hashed[..]);
243	final_key.extend_from_slice(&storage_prefix_hashed[..]);
244	final_key.extend_from_slice(key_hashed.as_ref());
245
246	StorageKey(final_key)
247}
248
249/// This is how a storage key of storage value is computed.
250///
251/// Copied from `frame_support::storage::storage_prefix`.
252pub fn storage_value_key(pallet_prefix: &str, value_name: &str) -> StorageKey {
253	let pallet_hash = sp_io::hashing::twox_128(pallet_prefix.as_bytes());
254	let storage_hash = sp_io::hashing::twox_128(value_name.as_bytes());
255
256	let mut final_key = vec![0u8; 32];
257	final_key[..16].copy_from_slice(&pallet_hash);
258	final_key[16..].copy_from_slice(&storage_hash);
259
260	StorageKey(final_key)
261}
262
263/// Can be use to access the runtime storage key of a `StorageMap`.
264pub trait StorageMapKeyProvider {
265	/// The name of the variable that holds the `StorageMap`.
266	const MAP_NAME: &'static str;
267
268	/// The same as `StorageMap::Hasher1`.
269	type Hasher: StorageHasher;
270	/// The same as `StorageMap::Key1`.
271	type Key: FullCodec + Send + Sync;
272	/// The same as `StorageMap::Value`.
273	type Value: 'static + FullCodec;
274
275	/// This is a copy of the
276	/// `frame_support::storage::generator::StorageMap::storage_map_final_key`.
277	///
278	/// We're using it because to call `storage_map_final_key` directly, we need access
279	/// to the runtime and pallet instance, which (sometimes) is impossible.
280	fn final_key(pallet_prefix: &str, key: &Self::Key) -> StorageKey {
281		storage_map_final_key::<Self::Hasher>(pallet_prefix, Self::MAP_NAME, &key.encode())
282	}
283}
284
285/// Can be used to access the runtime storage key of a `StorageDoubleMap`.
286pub trait StorageDoubleMapKeyProvider {
287	/// The name of the variable that holds the `StorageDoubleMap`.
288	const MAP_NAME: &'static str;
289
290	/// The same as `StorageDoubleMap::Hasher1`.
291	type Hasher1: StorageHasher;
292	/// The same as `StorageDoubleMap::Key1`.
293	type Key1: FullCodec + Send + Sync;
294	/// The same as `StorageDoubleMap::Hasher2`.
295	type Hasher2: StorageHasher;
296	/// The same as `StorageDoubleMap::Key2`.
297	type Key2: FullCodec + Send + Sync;
298	/// The same as `StorageDoubleMap::Value`.
299	type Value: 'static + FullCodec;
300
301	/// This is a copy of the
302	/// `frame_support::storage::generator::StorageDoubleMap::storage_double_map_final_key`.
303	///
304	/// We're using it because to call `storage_double_map_final_key` directly, we need access
305	/// to the runtime and pallet instance, which (sometimes) is impossible.
306	fn final_key(pallet_prefix: &str, key1: &Self::Key1, key2: &Self::Key2) -> StorageKey {
307		let key1_hashed = Self::Hasher1::hash(&key1.encode());
308		let key2_hashed = Self::Hasher2::hash(&key2.encode());
309		let pallet_prefix_hashed = frame_support::Twox128::hash(pallet_prefix.as_bytes());
310		let storage_prefix_hashed = frame_support::Twox128::hash(Self::MAP_NAME.as_bytes());
311
312		let mut final_key = Vec::with_capacity(
313			pallet_prefix_hashed.len() +
314				storage_prefix_hashed.len() +
315				key1_hashed.as_ref().len() +
316				key2_hashed.as_ref().len(),
317		);
318
319		final_key.extend_from_slice(&pallet_prefix_hashed[..]);
320		final_key.extend_from_slice(&storage_prefix_hashed[..]);
321		final_key.extend_from_slice(key1_hashed.as_ref());
322		final_key.extend_from_slice(key2_hashed.as_ref());
323
324		StorageKey(final_key)
325	}
326}
327
328/// Error generated by the `OwnedBridgeModule` trait.
329#[derive(Encode, Decode, DecodeWithMemTracking, PartialEq, Eq, TypeInfo, PalletError)]
330pub enum OwnedBridgeModuleError {
331	/// All pallet operations are halted.
332	Halted,
333}
334
335/// Operating mode for a bridge module.
336pub trait OperatingMode: Send + Copy + Debug + FullCodec {
337	/// Returns true if the bridge module is halted.
338	fn is_halted(&self) -> bool;
339}
340
341/// Basic operating modes for a bridges module (Normal/Halted).
342#[derive(
343	Encode,
344	Decode,
345	DecodeWithMemTracking,
346	Clone,
347	Copy,
348	PartialEq,
349	Eq,
350	Debug,
351	TypeInfo,
352	MaxEncodedLen,
353	Serialize,
354	Deserialize,
355)]
356pub enum BasicOperatingMode {
357	/// Normal mode, when all operations are allowed.
358	Normal,
359	/// The pallet is halted. All operations (except operating mode change) are prohibited.
360	Halted,
361}
362
363impl Default for BasicOperatingMode {
364	fn default() -> Self {
365		Self::Normal
366	}
367}
368
369impl OperatingMode for BasicOperatingMode {
370	fn is_halted(&self) -> bool {
371		*self == BasicOperatingMode::Halted
372	}
373}
374
375const COMMON_LOG_TARGET: &'static str = "runtime::bridge-module";
376
377/// Bridge module that has owner and operating mode
378pub trait OwnedBridgeModule<T: frame_system::Config> {
379	/// The target that will be used when publishing logs related to this module.
380	const LOG_TARGET: &'static str;
381
382	/// A storage entry that holds the module `Owner` account.
383	type OwnerStorage: StorageValue<T::AccountId, Query = Option<T::AccountId>>;
384	/// Operating mode type of the pallet.
385	type OperatingMode: OperatingMode;
386	/// A storage value that holds the pallet operating mode.
387	type OperatingModeStorage: StorageValue<Self::OperatingMode, Query = Self::OperatingMode>;
388
389	/// Check if the module is halted.
390	fn is_halted() -> bool {
391		Self::OperatingModeStorage::get().is_halted()
392	}
393
394	/// Ensure that the origin is either root, or `PalletOwner`.
395	fn ensure_owner_or_root(origin: T::RuntimeOrigin) -> Result<(), BadOrigin> {
396		match origin.into() {
397			Ok(RawOrigin::Root) => Ok(()),
398			Ok(RawOrigin::Signed(ref signer))
399				if Self::OwnerStorage::get().as_ref() == Some(signer) =>
400			{
401				Ok(())
402			},
403			_ => Err(BadOrigin),
404		}
405	}
406
407	/// Ensure that the module is not halted.
408	fn ensure_not_halted() -> Result<(), OwnedBridgeModuleError> {
409		match Self::is_halted() {
410			true => Err(OwnedBridgeModuleError::Halted),
411			false => Ok(()),
412		}
413	}
414
415	/// Change the owner of the module.
416	fn set_owner(origin: T::RuntimeOrigin, maybe_owner: Option<T::AccountId>) -> DispatchResult {
417		Self::ensure_owner_or_root(origin)?;
418		match maybe_owner {
419			Some(owner) => {
420				Self::OwnerStorage::put(&owner);
421				tracing::info!(target: COMMON_LOG_TARGET, module=%Self::LOG_TARGET, ?owner, "Setting pallet.");
422			},
423			None => {
424				Self::OwnerStorage::kill();
425				tracing::info!(target: COMMON_LOG_TARGET, module=%Self::LOG_TARGET, "Removed Owner of pallet.");
426			},
427		}
428
429		Ok(())
430	}
431
432	/// Halt or resume all/some module operations.
433	fn set_operating_mode(
434		origin: T::RuntimeOrigin,
435		operating_mode: Self::OperatingMode,
436	) -> DispatchResult {
437		Self::ensure_owner_or_root(origin)?;
438		Self::OperatingModeStorage::put(operating_mode);
439		tracing::info!(target: COMMON_LOG_TARGET, module=%Self::LOG_TARGET, ?operating_mode, "Setting operating mode.");
440		Ok(())
441	}
442
443	/// Pallet owner has a right to halt all module operations and then resume it. If it is `None`,
444	/// then there are no direct ways to halt/resume module operations, but other runtime methods
445	/// may still be used to do that (i.e. democracy::referendum to update halt flag directly
446	/// or call the `set_operating_mode`).
447	fn module_owner() -> Option<T::AccountId> {
448		Self::OwnerStorage::get()
449	}
450
451	/// The current operating mode of the module.
452	/// Depending on the mode either all, some, or no transactions will be allowed.
453	fn operating_mode() -> Self::OperatingMode {
454		Self::OperatingModeStorage::get()
455	}
456}
457
458/// All extra operations with weights that we need in bridges.
459pub trait WeightExtraOps {
460	/// Checked division of individual components of two weights.
461	///
462	/// Divides components and returns minimal division result. Returns `None` if one
463	/// of `other` weight components is zero.
464	fn min_components_checked_div(&self, other: Weight) -> Option<u64>;
465}
466
467impl WeightExtraOps for Weight {
468	fn min_components_checked_div(&self, other: Weight) -> Option<u64> {
469		Some(sp_std::cmp::min(
470			self.ref_time().checked_div(other.ref_time())?,
471			self.proof_size().checked_div(other.proof_size())?,
472		))
473	}
474}
475
476/// Trait that provides a static `str`.
477pub trait StaticStrProvider {
478	/// Static string.
479	const STR: &'static str;
480}
481
482/// A macro that generates `StaticStrProvider` with the string set to its stringified argument.
483#[macro_export]
484macro_rules! generate_static_str_provider {
485	($str:expr) => {
486		$crate::paste::item! {
487			pub struct [<Str $str>];
488
489			impl $crate::StaticStrProvider for [<Str $str>] {
490				const STR: &'static str = stringify!($str);
491			}
492		}
493	};
494}
495
496/// A trait defining helper methods for `RangeInclusive` (start..=end)
497pub trait RangeInclusiveExt<Idx> {
498	/// Computes the length of the `RangeInclusive`, checking for underflow and overflow.
499	fn checked_len(&self) -> Option<Idx>;
500	/// Computes the length of the `RangeInclusive`, saturating in case of underflow or overflow.
501	fn saturating_len(&self) -> Idx;
502}
503
504impl<Idx> RangeInclusiveExt<Idx> for RangeInclusive<Idx>
505where
506	Idx: CheckedSub + CheckedAdd + SaturatingAdd + One + Zero,
507{
508	fn checked_len(&self) -> Option<Idx> {
509		self.end()
510			.checked_sub(self.start())
511			.and_then(|len| len.checked_add(&Idx::one()))
512	}
513
514	fn saturating_len(&self) -> Idx {
515		let len = match self.end().checked_sub(self.start()) {
516			Some(len) => len,
517			None => return Idx::zero(),
518		};
519		len.saturating_add(&Idx::one())
520	}
521}
522
523#[cfg(test)]
524mod tests {
525	use super::*;
526
527	#[test]
528	fn storage_value_key_works() {
529		assert_eq!(
530			storage_value_key("PalletTransactionPayment", "NextFeeMultiplier"),
531			StorageKey(
532				hex_literal::hex!(
533					"f0e954dfcca51a255ab12c60c789256a3f2edf3bdf381debe331ab7446addfdc"
534				)
535				.to_vec()
536			),
537		);
538	}
539
540	#[test]
541	fn generate_static_str_provider_works() {
542		generate_static_str_provider!(Test);
543		assert_eq!(StrTest::STR, "Test");
544	}
545}