sp_sidechain/
lib.rs

1//! Primitives for the Sidechain pallet
2#![cfg_attr(not(feature = "std"), no_std)]
3#![deny(missing_docs)]
4
5use frame_support::pallet_prelude::Weight;
6use parity_scale_codec::{Decode, Encode};
7use scale_info::TypeInfo;
8use sidechain_domain::{ScEpochNumber, ScSlotNumber, UtxoId};
9
10#[cfg(test)]
11mod tests;
12
13/// Information about current Partner Chain slot and epoch.
14#[deprecated(since = "1.7.0", note = "See deprecation notes for [GetSidechainStatus]")]
15#[derive(TypeInfo, Clone, Encode, Decode)]
16pub struct SidechainStatus {
17	/// current Partner Chain epoch
18	pub epoch: ScEpochNumber,
19	/// current Partner Chain slot
20	pub slot: ScSlotNumber,
21	/// Number of slots per Partner Chain epoch
22	pub slots_per_epoch: u32,
23}
24
25/// Handler to be called when new epoch starts
26///
27/// Instances of [OnNewEpoch] can be added to the Sidechain pallet to be called on new epoch.
28pub trait OnNewEpoch {
29	/// New epoch handler
30	fn on_new_epoch(old_epoch: ScEpochNumber, new_epoch: ScEpochNumber) -> Weight;
31}
32
33impl OnNewEpoch for () {
34	fn on_new_epoch(_old_epoch: ScEpochNumber, _new_epoch: ScEpochNumber) -> Weight {
35		Weight::zero()
36	}
37}
38
39macro_rules! on_new_epoch_tuple_impl {
40	($first:ident, $($rest:ident),+) => {
41		impl<$first, $($rest),+> OnNewEpoch for ($first, $($rest),+)
42			where
43				$first: OnNewEpoch,
44				$($rest: OnNewEpoch),+
45		{
46			fn on_new_epoch(old_epoch: ScEpochNumber, new_epoch: ScEpochNumber) -> Weight {
47				<$first as OnNewEpoch>::on_new_epoch(old_epoch, new_epoch)
48					$(.saturating_add(<$rest as OnNewEpoch>::on_new_epoch(old_epoch, new_epoch)))+
49			}
50		}
51	};
52}
53on_new_epoch_tuple_impl!(A, B);
54on_new_epoch_tuple_impl!(A, B, C);
55on_new_epoch_tuple_impl!(A, B, C, D);
56
57#[allow(deprecated)]
58mod api_declarations {
59	use super::*;
60	sp_api::decl_runtime_apis! {
61		/// Runtime API for retrieving the Partner Chain's genesis UTXO
62		pub trait GetGenesisUtxo {
63			/// Returns the Partner Chain's genesis UTXO
64			fn genesis_utxo() -> UtxoId;
65		}
66
67		/// Runtime API for getting information about current Partner Chain slot and epoch
68		#[deprecated(since = "1.7.0", note = "Code that needs this data should define its own runtime API instead.")]
69		pub trait GetSidechainStatus {
70			/// Returns current Partner Chain slot and epoch
71			fn get_sidechain_status() -> SidechainStatus;
72		}
73	}
74}
75pub use api_declarations::*;
76
77/// Reads the genesis UTXO from the environment variable `GENESIS_UTXO`
78#[cfg(feature = "std")]
79pub fn read_genesis_utxo_from_env_with_defaults() -> Result<UtxoId, envy::Error> {
80	/// This structure is needed to read sidechain params from the environment variables because the main
81	/// type uses `rename_all = "camelCase"` serde option
82	#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
83	struct GenesisUtxoEnvConfiguration {
84		pub genesis_utxo: UtxoId,
85	}
86	let raw = envy::from_env::<GenesisUtxoEnvConfiguration>()?;
87	Ok(raw.genesis_utxo)
88}