use polkadot_sdk::*;
use alloc::collections::BTreeMap;
use codec::{Decode, DecodeWithMemTracking, Encode};
use frame_support::PalletId;
use ismp::{
consensus::{ConsensusClient, ConsensusStateId},
host::StateMachine,
};
use sp_core::{
crypto::{AccountId32, ByteArray},
H160, H256,
};
use sp_std::prelude::*;
#[derive(
Debug, Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq,
)]
pub struct UpdateConsensusState {
pub consensus_state_id: ConsensusStateId,
pub unbonding_period: Option<u64>,
pub challenge_periods: BTreeMap<StateMachine, u64>,
}
#[derive(
Debug, Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq,
)]
pub enum MessageCommitment {
Request(H256),
Response(H256),
}
#[derive(
Debug, Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq,
)]
pub struct FundMessageParams<Balance> {
pub commitment: MessageCommitment,
pub amount: Balance,
}
#[derive(Debug, Clone, Encode, Decode, scale_info::TypeInfo, PartialEq, Eq)]
pub struct ResponseReceipt {
pub response: H256,
pub relayer: Vec<u8>,
}
pub trait ConsensusClientProvider {
fn consensus_clients() -> Vec<Box<dyn ConsensusClient>>;
}
fortuples::fortuples! {
#[tuples::max_size(30)]
impl ConsensusClientProvider for #Tuple
where
#(#Member: ConsensusClient + Default + 'static),*
{
fn consensus_clients() -> Vec<Box<dyn ConsensusClient>> {
vec![
#( Box::new(#Member::default()) as Box<dyn ConsensusClient> ),*
]
}
}
}
#[derive(PartialEq, Eq, scale_info::TypeInfo)]
pub enum ModuleId {
Pallet(PalletId),
Contract(AccountId32),
Evm(H160),
}
impl ModuleId {
pub fn to_bytes(&self) -> Vec<u8> {
match self {
ModuleId::Pallet(pallet_id) => pallet_id.0.to_vec(),
ModuleId::Contract(account_id) => account_id.as_slice().to_vec(),
ModuleId::Evm(account_id) => account_id.0.to_vec(),
}
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, &'static str> {
if bytes.len() == 8 {
let mut inner = [0u8; 8];
inner.copy_from_slice(bytes);
Ok(Self::Pallet(PalletId(inner)))
} else if bytes.len() == 32 {
Ok(Self::Contract(AccountId32::from_slice(bytes).expect("Infallible")))
} else if bytes.len() == 20 {
Ok(Self::Evm(H160::from_slice(bytes)))
} else {
Err("Unknown Module ID format")
}
}
}
pub const ISMP_ID: sp_runtime::ConsensusEngineId = *b"ISMP";
#[derive(Encode, Decode, Clone, scale_info::TypeInfo, Default)]
pub struct ConsensusDigest {
pub mmr_root: H256,
pub child_trie_root: H256,
}
pub const ISMP_TIMESTAMP_ID: sp_runtime::ConsensusEngineId = *b"ISTM";
#[derive(Encode, Decode, Clone, scale_info::TypeInfo, Default)]
pub struct TimestampDigest {
pub timestamp: u64,
}