Skip to content

Commit

Permalink
add runtime api to extract xdm message id
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Dec 2, 2024
1 parent bbb0d4d commit 3984e67
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/subspace-fake-runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use sp_domains_fraud_proof::storage_proof::FraudProofStorageKeyRequest;
use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, ChannelId, CrossDomainMessage, MessageId, MessageKey,
};
use sp_messenger::XdmId;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_runtime::transaction_validity::{TransactionSource, TransactionValidity};
use sp_runtime::{ApplyExtrinsicResult, ExtrinsicInclusionMode};
Expand Down Expand Up @@ -361,6 +362,10 @@ sp_api::impl_runtime_apis! {
fn domain_chains_allowlist_update(_domain_id: DomainId) -> Option<DomainAllowlistUpdates>{
unreachable!()
}

fn xdm_id(_ext: &<Block as BlockT>::Extrinsic) -> Option<XdmId> {
unreachable!()
}
}

impl sp_messenger::RelayerApi<Block, BlockNumber, BlockNumber, <Block as BlockT>::Hash> for Runtime {
Expand Down
13 changes: 13 additions & 0 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, Endp
use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, FeeModel, MessageId, MessageKey,
};
use sp_messenger::XdmId;
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
use sp_mmr_primitives::EncodableOpaqueLeaf;
use sp_runtime::traits::{
Expand Down Expand Up @@ -1442,6 +1443,18 @@ impl_runtime_apis! {
fn domain_chains_allowlist_update(domain_id: DomainId) -> Option<DomainAllowlistUpdates>{
Messenger::domain_chains_allowlist_update(domain_id)
}

fn xdm_id(ext: &<Block as BlockT>::Extrinsic) -> Option<XdmId> {
match &ext.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })=> {
Some(XdmId::RelayMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(XdmId::RelayResponseMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
_ => None,
}
}
}

impl sp_messenger::RelayerApi<Block, BlockNumber, BlockNumber, <Block as BlockT>::Hash> for Runtime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,12 @@ async fn handle_xdm_message<TxPool, Client>(
Client: HeaderBackend<BlockOf<TxPool>>,
{
let at = client.info().best_hash;
let tx_hash = tx_pool.hash_of(&ext);
tracing::debug!(
target: LOG_TARGET,
"Submitting extrinsic to tx pool at block: {:?}",
"Submitting extrinsic {:?} to tx pool for chain {:?} at block: {:?}",
tx_hash,
chain_id,
at
);

Expand All @@ -487,5 +490,13 @@ async fn handle_xdm_message<TxPool, Client>(
chain_id,
err
);
} else {
tracing::debug!(
target: LOG_TARGET,
"Submitted extrinsic {:?} to tx pool for chain {:?} at {:?}",
tx_hash,
chain_id,
at
)
}
}
11 changes: 11 additions & 0 deletions domains/primitives/messenger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use codec::{Decode, Encode};
use frame_support::inherent::InherentData;
use frame_support::inherent::{InherentIdentifier, IsFatalError};
use messages::{BlockMessagesWithStorageKey, ChannelId, CrossDomainMessage, MessageId};
use scale_info::TypeInfo;
use sp_domains::{ChainId, DomainAllowlistUpdates, DomainId};
use sp_subspace_mmr::ConsensusChainMmrLeafProof;
#[cfg(feature = "std")]
Expand Down Expand Up @@ -159,6 +160,13 @@ impl sp_inherents::InherentDataProvider for InherentDataProvider {
}
}

/// Represent a union of XDM types with their message ID
#[derive(Debug, Encode, Decode, TypeInfo)]
pub enum XdmId {
RelayMessage(MessageKey),
RelayResponseMessage(MessageKey),
}

sp_api::decl_runtime_apis! {
/// Api useful for relayers to fetch messages and submit transactions.
pub trait RelayerApi<BlockNumber, CNumber, CHash>
Expand Down Expand Up @@ -220,5 +228,8 @@ sp_api::decl_runtime_apis! {

/// Returns any domain's chains allowlist updates on consensus chain.
fn domain_chains_allowlist_update(domain_id: DomainId) -> Option<DomainAllowlistUpdates>;

/// Returns XDM message ID
fn xdm_id(ext: &Block::Extrinsic) -> Option<XdmId>;
}
}
13 changes: 13 additions & 0 deletions domains/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, Endp
use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, FeeModel, MessageId, MessageKey,
};
use sp_messenger::XdmId;
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
use sp_mmr_primitives::EncodableOpaqueLeaf;
use sp_runtime::generic::Era;
Expand Down Expand Up @@ -909,6 +910,18 @@ impl_runtime_apis! {
// not valid call on domains
None
}

fn xdm_id(ext: &<Block as BlockT>::Extrinsic) -> Option<XdmId> {
match &ext.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })=> {
Some(XdmId::RelayMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(XdmId::RelayResponseMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
_ => None,
}
}
}

impl sp_messenger::RelayerApi<Block, BlockNumber, ConsensusBlockNumber, ConsensusBlockHash> for Runtime {
Expand Down
13 changes: 13 additions & 0 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, Endp
use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, FeeModel, MessageId, MessageKey,
};
use sp_messenger::XdmId;
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
use sp_mmr_primitives::EncodableOpaqueLeaf;
use sp_runtime::generic::Era;
Expand Down Expand Up @@ -1320,6 +1321,18 @@ impl_runtime_apis! {
// not valid call on domains
None
}

fn xdm_id(ext: &<Block as BlockT>::Extrinsic) -> Option<XdmId> {
match &ext.0.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })=> {
Some(XdmId::RelayMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(XdmId::RelayResponseMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
_ => None,
}
}
}

impl sp_messenger::RelayerApi<Block, BlockNumber, ConsensusBlockNumber, ConsensusBlockHash> for Runtime {
Expand Down
13 changes: 13 additions & 0 deletions domains/test/runtime/auto-id/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, Endp
use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, FeeModel, MessageId, MessageKey,
};
use sp_messenger::XdmId;
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
use sp_mmr_primitives::EncodableOpaqueLeaf;
use sp_runtime::generic::Era;
Expand Down Expand Up @@ -900,6 +901,18 @@ impl_runtime_apis! {
// not valid call on domains
None
}

fn xdm_id(ext: &<Block as BlockT>::Extrinsic) -> Option<XdmId> {
match &ext.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })=> {
Some(XdmId::RelayMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(XdmId::RelayResponseMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
_ => None,
}
}
}

impl sp_messenger::RelayerApi<Block, BlockNumber, ConsensusBlockNumber, ConsensusBlockHash> for Runtime {
Expand Down
13 changes: 13 additions & 0 deletions domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, ChannelId, CrossDomainMessage, FeeModel, MessageId,
MessageKey,
};
use sp_messenger::XdmId;
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
use sp_mmr_primitives::EncodableOpaqueLeaf;
use sp_runtime::generic::Era;
Expand Down Expand Up @@ -1276,6 +1277,18 @@ impl_runtime_apis! {
// not valid call on domains
None
}

fn xdm_id(ext: &<Block as BlockT>::Extrinsic) -> Option<XdmId> {
match &ext.0.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })=> {
Some(XdmId::RelayMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(XdmId::RelayResponseMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
_ => None,
}
}
}

impl sp_messenger::RelayerApi<Block, BlockNumber, ConsensusBlockNumber, ConsensusBlockHash> for Runtime {
Expand Down
13 changes: 13 additions & 0 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, ChannelId, CrossDomainMessage, FeeModel, MessageId,
MessageKey,
};
use sp_messenger::XdmId;
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
use sp_mmr_primitives::EncodableOpaqueLeaf;
use sp_runtime::traits::{
Expand Down Expand Up @@ -1503,6 +1504,18 @@ impl_runtime_apis! {
fn domain_chains_allowlist_update(domain_id: DomainId) -> Option<DomainAllowlistUpdates>{
Messenger::domain_chains_allowlist_update(domain_id)
}

fn xdm_id(ext: &<Block as BlockT>::Extrinsic) -> Option<XdmId> {
match &ext.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg })=> {
Some(XdmId::RelayMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(XdmId::RelayResponseMessage((msg.src_chain_id, msg.channel_id, msg.nonce)))
}
_ => None,
}
}
}

impl sp_messenger::RelayerApi<Block, BlockNumber, BlockNumber, <Block as BlockT>::Hash> for Runtime {
Expand Down

0 comments on commit 3984e67

Please sign in to comment.