1use polkadot_sdk::*;
18
19use alloc::collections::BTreeMap;
20
21use codec::{Decode, DecodeWithMemTracking, Encode};
22use frame_support::PalletId;
23use ismp::{
24 consensus::{ConsensusClient, ConsensusStateId},
25 host::StateMachine,
26};
27use sp_core::{
28 crypto::{AccountId32, ByteArray},
29 H160, H256,
30};
31use sp_std::prelude::*;
32
33#[derive(
35 Debug, Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq,
36)]
37pub struct UpdateConsensusState {
38 pub consensus_state_id: ConsensusStateId,
40 pub unbonding_period: Option<u64>,
42 pub challenge_periods: BTreeMap<StateMachine, u64>,
44}
45
46#[derive(
48 Debug, Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq,
49)]
50pub enum MessageCommitment {
51 Request(H256),
53 Response(H256),
55}
56
57#[derive(
59 Debug, Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq,
60)]
61pub struct FundMessageParams<Balance> {
62 pub commitment: MessageCommitment,
64 pub amount: Balance,
66}
67
68#[derive(Debug, Clone, Encode, Decode, scale_info::TypeInfo, PartialEq, Eq)]
70pub struct ResponseReceipt {
71 pub response: H256,
73 pub relayer: Vec<u8>,
75}
76
77pub trait ConsensusClientProvider {
80 fn consensus_clients() -> Vec<Box<dyn ConsensusClient>>;
82}
83
84fortuples::fortuples! {
85 #[tuples::max_size(30)]
86 impl ConsensusClientProvider for #Tuple
87 where
88 #(#Member: ConsensusClient + Default + 'static),*
89 {
90
91 fn consensus_clients() -> Vec<Box<dyn ConsensusClient>> {
92 vec![
93 #( Box::new(#Member::default()) as Box<dyn ConsensusClient> ),*
94 ]
95 }
96 }
97}
98
99#[derive(PartialEq, Eq, scale_info::TypeInfo)]
101pub enum ModuleId {
102 Pallet(PalletId),
104 Contract(AccountId32),
106 Evm(H160),
108}
109
110impl ModuleId {
111 pub fn to_bytes(&self) -> Vec<u8> {
113 match self {
114 ModuleId::Pallet(pallet_id) => pallet_id.0.to_vec(),
115 ModuleId::Contract(account_id) => account_id.as_slice().to_vec(),
116 ModuleId::Evm(account_id) => account_id.0.to_vec(),
117 }
118 }
119
120 pub fn from_bytes(bytes: &[u8]) -> Result<Self, &'static str> {
122 if bytes.len() == 8 {
123 let mut inner = [0u8; 8];
124 inner.copy_from_slice(bytes);
125 Ok(Self::Pallet(PalletId(inner)))
126 } else if bytes.len() == 32 {
127 Ok(Self::Contract(AccountId32::from_slice(bytes).expect("Infallible")))
128 } else if bytes.len() == 20 {
129 Ok(Self::Evm(H160::from_slice(bytes)))
130 } else {
131 Err("Unknown Module ID format")
132 }
133 }
134}
135
136pub const ISMP_ID: sp_runtime::ConsensusEngineId = *b"ISMP";
138
139#[derive(Encode, Decode, Clone, scale_info::TypeInfo, Default)]
141pub struct ConsensusDigest {
142 pub mmr_root: H256,
144 pub child_trie_root: H256,
146}
147
148pub const ISMP_TIMESTAMP_ID: sp_runtime::ConsensusEngineId = *b"ISTM";
150
151#[derive(Encode, Decode, Clone, scale_info::TypeInfo, Default)]
153pub struct TimestampDigest {
154 pub timestamp: u64,
156}