#![cfg_attr(not(feature = "std"), no_std)]
pub mod endpoint;
pub mod messages;
#[cfg(not(feature = "std"))]
extern crate alloc;
use crate::messages::MessageKey;
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeSet;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use frame_support::inherent::InherentData;
use frame_support::inherent::{InherentIdentifier, IsFatalError};
use messages::{BlockMessagesWithStorageKey, ChannelId, CrossDomainMessage, MessageId};
use sp_domains::{ChainId, DomainAllowlistUpdates, DomainId};
use sp_subspace_mmr::ConsensusChainMmrLeafProof;
#[cfg(feature = "std")]
use std::collections::BTreeSet;
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"messengr";
pub trait OnXDMRewards<Balance> {
fn on_xdm_rewards(rewards: Balance);
fn on_chain_protocol_fees(chain_id: ChainId, fees: Balance);
}
impl<Balance> OnXDMRewards<Balance> for () {
fn on_xdm_rewards(_: Balance) {}
fn on_chain_protocol_fees(_chain_id: ChainId, _fees: Balance) {}
}
pub trait DomainRegistration {
fn is_domain_registered(domain_id: DomainId) -> bool;
}
impl DomainRegistration for () {
fn is_domain_registered(_domain_id: DomainId) -> bool {
false
}
}
pub trait StorageKeys {
fn confirmed_domain_block_storage_key(domain_id: DomainId) -> Option<Vec<u8>>;
fn outbox_storage_key(chain_id: ChainId, message_key: MessageKey) -> Option<Vec<u8>>;
fn inbox_responses_storage_key(chain_id: ChainId, message_key: MessageKey) -> Option<Vec<u8>>;
}
impl StorageKeys for () {
fn confirmed_domain_block_storage_key(_domain_id: DomainId) -> Option<Vec<u8>> {
None
}
fn outbox_storage_key(_chain_id: ChainId, _message_key: MessageKey) -> Option<Vec<u8>> {
None
}
fn inbox_responses_storage_key(
_chain_id: ChainId,
_message_key: MessageKey,
) -> Option<Vec<u8>> {
None
}
}
#[derive(Debug, Encode, Decode)]
pub struct InherentType {
pub maybe_updates: Option<DomainAllowlistUpdates>,
}
#[derive(Debug, Encode)]
#[cfg_attr(feature = "std", derive(Decode))]
pub enum InherentError {
MissingAllowlistUpdates,
IncorrectAllowlistUpdates,
}
impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
true
}
}
#[cfg(feature = "std")]
pub struct InherentDataProvider {
data: InherentType,
}
#[cfg(feature = "std")]
impl InherentDataProvider {
pub fn new(data: InherentType) -> Self {
Self { data }
}
pub fn data(&self) -> &InherentType {
&self.data
}
}
#[cfg(feature = "std")]
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for InherentDataProvider {
async fn provide_inherent_data(
&self,
inherent_data: &mut InherentData,
) -> Result<(), sp_inherents::Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &self.data)
}
async fn try_handle_error(
&self,
identifier: &InherentIdentifier,
error: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
if *identifier != INHERENT_IDENTIFIER {
return None;
}
let error = InherentError::decode(&mut &*error).ok()?;
Some(Err(sp_inherents::Error::Application(Box::from(format!(
"{error:?}"
)))))
}
}
sp_api::decl_runtime_apis! {
pub trait RelayerApi<BlockNumber, CNumber, CHash>
where
BlockNumber: Encode + Decode,
CNumber: Encode + Decode,
CHash: Encode + Decode,
{
fn block_messages() -> BlockMessagesWithStorageKey;
fn outbox_message_unsigned(
msg: CrossDomainMessage<CNumber, CHash, sp_core::H256>,
) -> Option<Block::Extrinsic>;
fn inbox_response_message_unsigned(
msg: CrossDomainMessage<CNumber, CHash, sp_core::H256>,
) -> Option<Block::Extrinsic>;
fn should_relay_outbox_message(dst_chain_id: ChainId, msg_id: MessageId) -> bool;
fn should_relay_inbox_message_response(dst_chain_id: ChainId, msg_id: MessageId) -> bool;
fn updated_channels() -> BTreeSet<(ChainId, ChannelId)>;
fn channel_storage_key(chain_id: ChainId, channel_id: ChannelId) -> Vec<u8>;
}
pub trait MessengerApi<CNumber, CHash>
where
CNumber: Encode + Decode,
CHash: Encode + Decode,
{
fn is_xdm_mmr_proof_valid(
ext: &Block::Extrinsic
) -> Option<bool>;
fn extract_xdm_mmr_proof(ext: &Block::Extrinsic) -> Option<ConsensusChainMmrLeafProof<CNumber, CHash, sp_core::H256>>;
fn confirmed_domain_block_storage_key(domain_id: DomainId) -> Vec<u8>;
fn outbox_storage_key(message_key: MessageKey) -> Vec<u8>;
fn inbox_response_storage_key(message_key: MessageKey) -> Vec<u8>;
fn domain_chains_allowlist_update(domain_id: DomainId) -> Option<DomainAllowlistUpdates>;
}
}