From 73c12b3f4f44e52e39610ccaef77826709c2fd0d Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Tue, 21 Jan 2025 16:33:11 +0530 Subject: [PATCH 1/3] ensure max outgoing messages per channel is atleast one and ensure to return open channel that can still accept messages --- domains/pallets/messenger/src/benchmarking.rs | 3 ++ domains/pallets/messenger/src/lib.rs | 44 ++++++++++------ domains/pallets/messenger/src/messages.rs | 23 ++++++++- domains/pallets/messenger/src/tests.rs | 51 +++++++++++++++---- 4 files changed, 95 insertions(+), 26 deletions(-) diff --git a/domains/pallets/messenger/src/benchmarking.rs b/domains/pallets/messenger/src/benchmarking.rs index 8616d2b3c4..9422459e19 100644 --- a/domains/pallets/messenger/src/benchmarking.rs +++ b/domains/pallets/messenger/src/benchmarking.rs @@ -203,6 +203,9 @@ mod benchmarks { last_delivered_message_response_nonce: None, }; Outbox::::insert((dst_chain_id, channel_id, next_outbox_nonce), req_msg); + OutboxMessageCount::::mutate((dst_chain_id, channel_id), |count| { + *count = count.saturating_add(1u32); + }); // Insert a dummy response message which will be handled during the `relay_message_response` call let resp_msg: Message> = Message { src_chain_id: dst_chain_id, diff --git a/domains/pallets/messenger/src/lib.rs b/domains/pallets/messenger/src/lib.rs index 4fa397b3c1..ebc1492c6f 100644 --- a/domains/pallets/messenger/src/lib.rs +++ b/domains/pallets/messenger/src/lib.rs @@ -243,25 +243,21 @@ mod pallet { /// Used by the dst_chains to verify the message response. #[pallet::storage] #[pallet::getter(fn inbox_responses)] - pub(super) type InboxResponses = CountedStorageMap< - _, - Identity, - (ChainId, ChannelId, Nonce), - Message>, - OptionQuery, - >; + pub(super) type InboxResponses = + StorageMap<_, Identity, (ChainId, ChannelId, Nonce), Message>, OptionQuery>; /// Stores the outgoing messages that are awaiting message responses from the dst_chain. /// Messages are processed in the outbox nonce order of chain's channel. #[pallet::storage] #[pallet::getter(fn outbox)] - pub(super) type Outbox = CountedStorageMap< - _, - Identity, - (ChainId, ChannelId, Nonce), - Message>, - OptionQuery, - >; + pub(super) type Outbox = + StorageMap<_, Identity, (ChainId, ChannelId, Nonce), Message>, OptionQuery>; + + /// Stores the outgoing messages count that are awaiting message responses from the dst_chain. + #[pallet::storage] + #[pallet::getter(fn outbox_message_count)] + pub(super) type OutboxMessageCount = + StorageMap<_, Identity, (ChainId, ChannelId), u32, ValueQuery>; /// A temporary storage for storing decoded outbox response message between `pre_dispatch_relay_message_response` /// and `relay_message_response`. @@ -545,6 +541,15 @@ mod pallet { /// Invalid channel reserve fee InvalidChannelReserveFee, + + /// Invalid max outgoing messages + InvalidMaxOutgoingMessages, + + /// Message count overflow + MessageCountOverflow, + + /// Message count underflow + MessageCountUnderflow, } #[pallet::call] @@ -907,8 +912,11 @@ mod pallet { // loop through channels in descending order until open channel is found. // we always prefer latest opened channel. while let Some(channel_id) = next_channel_id.checked_sub(ChannelId::one()) { + let message_count = OutboxMessageCount::::get((dst_chain_id, channel_id)); if let Some(channel) = Channels::::get(dst_chain_id, channel_id) { - if channel.state == ChannelState::Open { + if channel.state == ChannelState::Open + && message_count < channel.max_outgoing_messages + { return Some((channel_id, channel.fee)); } } @@ -1006,6 +1014,12 @@ mod pallet { Error::::InvalidChain, ); + // ensure max outgoing messages is atleast 1 + ensure!( + init_params.max_outgoing_messages >= 1u32, + Error::::InvalidMaxOutgoingMessages + ); + // If the channel owner is in this chain then the channel reserve fee // must not be empty ensure!( diff --git a/domains/pallets/messenger/src/messages.rs b/domains/pallets/messenger/src/messages.rs index 26e9172068..65782096dc 100644 --- a/domains/pallets/messenger/src/messages.rs +++ b/domains/pallets/messenger/src/messages.rs @@ -1,7 +1,7 @@ #[cfg(not(feature = "std"))] extern crate alloc; -use crate::pallet::{ChainAllowlist, UpdatedChannels}; +use crate::pallet::{ChainAllowlist, OutboxMessageCount, UpdatedChannels}; use crate::{ BalanceOf, ChannelId, ChannelState, Channels, CloseChannelBy, Config, Error, Event, InboxResponses, MessageWeightTags as MessageWeightTagStore, Nonce, Outbox, OutboxMessageResult, @@ -52,7 +52,7 @@ impl Pallet { |maybe_channel| -> Result { let channel = maybe_channel.as_mut().ok_or(Error::::MissingChannel)?; // check if the outbox is full - let count = Outbox::::count(); + let count = OutboxMessageCount::::get((dst_chain_id, channel_id)); ensure!( count < channel.max_outgoing_messages, Error::::OutboxFull @@ -72,6 +72,15 @@ impl Pallet { .latest_response_received_message_nonce, }; Outbox::::insert((dst_chain_id, channel_id, next_outbox_nonce), msg); + OutboxMessageCount::::try_mutate( + (dst_chain_id, channel_id), + |count| -> Result<(), DispatchError> { + *count = count + .checked_add(1u32) + .ok_or(Error::::MessageCountOverflow)?; + Ok(()) + }, + )?; // update channel state channel.next_outbox_nonce = next_outbox_nonce @@ -340,6 +349,16 @@ impl Pallet { let req_msg = Outbox::::take((dst_chain_id, channel_id, nonce)) .ok_or(Error::::MissingMessage)?; + OutboxMessageCount::::try_mutate( + (dst_chain_id, channel_id), + |count| -> Result<(), DispatchError> { + *count = count + .checked_sub(1u32) + .ok_or(Error::::MessageCountUnderflow)?; + Ok(()) + }, + )?; + // clear out box message weight tag MessageWeightTagStore::::mutate(|maybe_messages| { let mut messages = maybe_messages.as_mut().cloned().unwrap_or_default(); diff --git a/domains/pallets/messenger/src/tests.rs b/domains/pallets/messenger/src/tests.rs index 8559c4db3a..f58ae7a9f0 100644 --- a/domains/pallets/messenger/src/tests.rs +++ b/domains/pallets/messenger/src/tests.rs @@ -6,6 +6,7 @@ use crate::mock::{ chain_a, chain_b, consensus_chain, storage_proof_of_inbox_message_responses, storage_proof_of_outbox_messages, AccountId, Balance, TestExternalities, }; +use crate::pallet::OutboxMessageCount; use crate::{ ChainAllowlist, ChainAllowlistUpdate, Channel, ChannelId, ChannelState, Channels, CloseChannelBy, Error, FeeModel, Inbox, InboxResponses, InitiateChannelParams, Nonce, Outbox, @@ -59,7 +60,10 @@ fn create_channel(chain_id: ChainId, channel_id: ChannelId) { assert_eq!(channel.next_inbox_nonce, Nonce::zero()); assert_eq!(channel.next_outbox_nonce, Nonce::one()); assert_eq!(channel.latest_response_received_message_nonce, None); - assert_eq!(Outbox::::count(), 1); + assert_eq!( + OutboxMessageCount::::get((chain_id, channel_id)), + 1 + ); let msg = Outbox::::get((chain_id, channel_id, Nonce::zero())).unwrap(); assert_eq!(msg.dst_chain_id, chain_id); assert_eq!(msg.channel_id, channel_id); @@ -338,6 +342,7 @@ fn send_message_between_chains( msg: EndpointPayload, channel_id: ChannelId, ) { + let chain_a_id = chain_a::SelfChainId::get(); let chain_b_id = chain_b::SelfChainId::get(); // send message form outbox @@ -368,21 +373,35 @@ fn send_message_between_chains( // check state on chain_b chain_b_test_ext.execute_with(|| { // Outbox, Outbox responses, Inbox, InboxResponses must be empty - assert_eq!(Outbox::::count(), 0); + assert_eq!( + OutboxMessageCount::::get((chain_a_id, channel_id)), + 0 + ); assert!(OutboxResponses::::get().is_none()); assert!(Inbox::::get().is_none()); // latest inbox message response is cleared on next message - assert_eq!(InboxResponses::::count(), 1); + assert!(InboxResponses::::contains_key(( + chain_a_id, + channel_id, + Nonce::one() + )),); }); // check state on chain_a chain_a_test_ext.execute_with(|| { // Outbox, Outbox responses, Inbox, InboxResponses must be empty - assert_eq!(Outbox::::count(), 0); + assert_eq!( + OutboxMessageCount::::get((chain_b_id, channel_id)), + 0 + ); assert!(OutboxResponses::::get().is_none()); assert!(Inbox::::get().is_none()); - assert_eq!(InboxResponses::::count(), 0); + assert!(!InboxResponses::::contains_key(( + chain_b_id, + channel_id, + Nonce::one() + ))); let channel = chain_a::Messenger::channels(chain_b_id, channel_id).unwrap(); assert_eq!( @@ -435,12 +454,19 @@ fn close_channel_between_chains( assert_eq!(channel.next_outbox_nonce, Nonce::zero()); // Outbox, Outbox responses, Inbox, InboxResponses must be empty - assert_eq!(Outbox::::count(), 0); + assert_eq!( + OutboxMessageCount::::get((chain_a_id, channel_id)), + 0 + ); assert!(OutboxResponses::::get().is_none()); assert!(Inbox::::get().is_none()); // latest inbox message response is cleared on next message - assert_eq!(InboxResponses::::count(), 1); + assert!(InboxResponses::::contains_key(( + chain_a_id, + channel_id, + Nonce::one() + ))); }); // check channel state be closed on chain_a @@ -464,10 +490,17 @@ fn close_channel_between_chains( })); // Outbox, Outbox responses, Inbox, InboxResponses must be empty - assert_eq!(Outbox::::count(), 0); + assert_eq!( + OutboxMessageCount::::get((chain_b_id, channel_id)), + 0 + ); assert!(OutboxResponses::::get().is_none()); assert!(Inbox::::get().is_none()); - assert_eq!(InboxResponses::::count(), 0); + assert!(!InboxResponses::::contains_key(( + chain_b_id, + channel_id, + Nonce::one() + ))); }) } From 73c7574bf7919ba8022306bc4779c5c163be19f3 Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Thu, 23 Jan 2025 12:43:20 +0530 Subject: [PATCH 2/3] set default max outgoing messages to 25 for all channels --- Cargo.lock | 4 ++++ crates/subspace-runtime/src/lib.rs | 5 +++++ domains/client/domain-operator/src/tests.rs | 18 --------------- domains/pallets/messenger/src/benchmarking.rs | 9 +------- domains/pallets/messenger/src/lib.rs | 22 ++++++------------- domains/pallets/messenger/src/mock.rs | 2 ++ domains/pallets/messenger/src/tests.rs | 11 +++------- domains/pallets/transporter/src/mock.rs | 2 ++ domains/runtime/auto-id/Cargo.toml | 1 + domains/runtime/auto-id/src/lib.rs | 6 +++++ domains/runtime/evm/Cargo.toml | 1 + domains/runtime/evm/src/lib.rs | 6 +++++ domains/test/runtime/auto-id/Cargo.toml | 1 + domains/test/runtime/auto-id/src/lib.rs | 6 +++++ domains/test/runtime/evm/Cargo.toml | 1 + domains/test/runtime/evm/src/lib.rs | 6 +++++ test/subspace-test-runtime/src/lib.rs | 5 +++++ 17 files changed, 57 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 24779abeb8..4f608bd685 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1013,6 +1013,7 @@ dependencies = [ "sp-subspace-mmr", "sp-transaction-pool", "sp-version", + "static_assertions", "subspace-core-primitives", "subspace-runtime-primitives", "substrate-wasm-builder", @@ -1061,6 +1062,7 @@ dependencies = [ "sp-subspace-mmr", "sp-transaction-pool", "sp-version", + "static_assertions", "subspace-core-primitives", "subspace-runtime-primitives", "substrate-wasm-builder", @@ -3285,6 +3287,7 @@ dependencies = [ "sp-subspace-mmr", "sp-transaction-pool", "sp-version", + "static_assertions", "subspace-core-primitives", "subspace-runtime-primitives", "substrate-wasm-builder", @@ -3340,6 +3343,7 @@ dependencies = [ "sp-subspace-mmr", "sp-transaction-pool", "sp-version", + "static_assertions", "subspace-core-primitives", "subspace-runtime-primitives", "substrate-wasm-builder", diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index f0ac27b258..0de246f52f 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -650,8 +650,12 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; + pub const MaxOutgoingMessages: u32 = 25; } +// ensure the max outgoing messages is not 0. +const_assert!(MaxOutgoingMessages::get() >= 1); + pub struct DomainRegistration; impl sp_messenger::DomainRegistration for DomainRegistration { fn is_domain_registered(domain_id: DomainId) -> bool { @@ -684,6 +688,7 @@ impl pallet_messenger::Config for Runtime { type ChannelInitReservePortion = ChannelInitReservePortion; type DomainRegistration = DomainRegistration; type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; } impl frame_system::offchain::SendTransactionTypes for Runtime diff --git a/domains/client/domain-operator/src/tests.rs b/domains/client/domain-operator/src/tests.rs index d201ac9d8e..7ddc81f156 100644 --- a/domains/client/domain-operator/src/tests.rs +++ b/domains/client/domain-operator/src/tests.rs @@ -3784,9 +3784,6 @@ async fn test_domain_sudo_calls() { .construct_and_send_extrinsic(evm_domain_test_runtime::RuntimeCall::Messenger( pallet_messenger::Call::initiate_channel { dst_chain_id: ChainId::Consensus, - params: pallet_messenger::InitiateChannelParams { - max_outgoing_messages: 100, - }, }, )) .await @@ -3927,9 +3924,6 @@ async fn test_xdm_between_consensus_and_domain_should_work() { .construct_and_send_extrinsic(evm_domain_test_runtime::RuntimeCall::Messenger( pallet_messenger::Call::initiate_channel { dst_chain_id: ChainId::Consensus, - params: pallet_messenger::InitiateChannelParams { - max_outgoing_messages: 100, - }, }, )) .await @@ -4140,9 +4134,6 @@ async fn test_xdm_between_domains_should_work() { bob.construct_and_send_extrinsic(auto_id_domain_test_runtime::RuntimeCall::Messenger( pallet_messenger::Call::initiate_channel { dst_chain_id: ChainId::Domain(EVM_DOMAIN_ID), - params: pallet_messenger::InitiateChannelParams { - max_outgoing_messages: 100, - }, }, )) .await @@ -4294,9 +4285,6 @@ async fn test_unordered_cross_domains_message_should_work() { .construct_and_send_extrinsic(evm_domain_test_runtime::RuntimeCall::Messenger( pallet_messenger::Call::initiate_channel { dst_chain_id: ChainId::Consensus, - params: pallet_messenger::InitiateChannelParams { - max_outgoing_messages: 100, - }, }, )) .await @@ -5430,9 +5418,6 @@ async fn test_xdm_false_invalid_fraud_proof() { .construct_and_send_extrinsic(evm_domain_test_runtime::RuntimeCall::Messenger( pallet_messenger::Call::initiate_channel { dst_chain_id: ChainId::Consensus, - params: pallet_messenger::InitiateChannelParams { - max_outgoing_messages: 100, - }, }, )) .await @@ -5628,9 +5613,6 @@ async fn test_stale_fork_xdm_true_invalid_fraud_proof() { .construct_and_send_extrinsic(evm_domain_test_runtime::RuntimeCall::Messenger( pallet_messenger::Call::initiate_channel { dst_chain_id: ChainId::Consensus, - params: pallet_messenger::InitiateChannelParams { - max_outgoing_messages: 100, - }, }, )) .await diff --git a/domains/pallets/messenger/src/benchmarking.rs b/domains/pallets/messenger/src/benchmarking.rs index 9422459e19..53399a4384 100644 --- a/domains/pallets/messenger/src/benchmarking.rs +++ b/domains/pallets/messenger/src/benchmarking.rs @@ -33,9 +33,6 @@ mod benchmarks { fn initiate_channel() { let dst_chain_id: ChainId = u32::MAX.into(); assert_ne!(T::SelfChainId::get(), dst_chain_id); - let channel_params = InitiateChannelParams { - max_outgoing_messages: 100, - }; let channel_id = NextChannelId::::get(dst_chain_id); let account = account("account", 0, 0); T::Currency::set_balance( @@ -47,11 +44,7 @@ mod benchmarks { ChainAllowlist::::put(list); #[extrinsic_call] - _( - RawOrigin::Signed(account.clone()), - dst_chain_id, - channel_params, - ); + _(RawOrigin::Signed(account.clone()), dst_chain_id); let channel = Channels::::get(dst_chain_id, channel_id).expect("channel should exist"); assert_eq!(channel.state, ChannelState::Initiated); diff --git a/domains/pallets/messenger/src/lib.rs b/domains/pallets/messenger/src/lib.rs index ebc1492c6f..ab36d46b3b 100644 --- a/domains/pallets/messenger/src/lib.rs +++ b/domains/pallets/messenger/src/lib.rs @@ -98,12 +98,6 @@ pub(crate) enum CloseChannelBy { Sudo, } -/// Parameters for a new channel between two chains. -#[derive(Default, Debug, Encode, Decode, Clone, Eq, PartialEq, TypeInfo, Copy)] -pub struct InitiateChannelParams { - pub max_outgoing_messages: u32, -} - /// Hold identifier trait for messenger specific balance holds pub trait HoldIdentifier { fn messenger_channel() -> FungibleHoldId; @@ -114,8 +108,8 @@ mod pallet { use crate::weights::WeightInfo; use crate::{ BalanceOf, ChainAllowlistUpdate, Channel, ChannelId, ChannelState, CloseChannelBy, - FeeModel, HoldIdentifier, InitiateChannelParams, Nonce, OutboxMessageResult, StateRootOf, - ValidatedRelayMessage, U256, + FeeModel, HoldIdentifier, Nonce, OutboxMessageResult, StateRootOf, ValidatedRelayMessage, + U256, }; #[cfg(not(feature = "std"))] use alloc::boxed::Box; @@ -191,6 +185,8 @@ mod pallet { type DomainRegistration: DomainRegistration; /// Channels fee model type ChannelFeeModel: Get>>; + /// Maximum outgoing messages from a given channel + type MaxOutgoingMessages: Get; } /// Pallet messenger used to communicate between chains and other blockchains. @@ -559,11 +555,7 @@ mod pallet { /// Channel is set to initiated and do not accept or receive any messages. #[pallet::call_index(0)] #[pallet::weight(T::WeightInfo::initiate_channel())] - pub fn initiate_channel( - origin: OriginFor, - dst_chain_id: ChainId, - params: InitiateChannelParams, - ) -> DispatchResult { + pub fn initiate_channel(origin: OriginFor, dst_chain_id: ChainId) -> DispatchResult { let owner = ensure_signed(origin)?; // reserve channel open fees @@ -580,7 +572,7 @@ mod pallet { // initiate the channel config let channel_open_params = ChannelOpenParams { - max_outgoing_messages: params.max_outgoing_messages, + max_outgoing_messages: T::MaxOutgoingMessages::get(), fee_model: T::ChannelFeeModel::get(), }; let channel_id = Self::do_init_channel( @@ -1014,7 +1006,7 @@ mod pallet { Error::::InvalidChain, ); - // ensure max outgoing messages is atleast 1 + // ensure max outgoing messages is at least 1 ensure!( init_params.max_outgoing_messages >= 1u32, Error::::InvalidMaxOutgoingMessages diff --git a/domains/pallets/messenger/src/mock.rs b/domains/pallets/messenger/src/mock.rs index d6cd143682..0f82ecb686 100644 --- a/domains/pallets/messenger/src/mock.rs +++ b/domains/pallets/messenger/src/mock.rs @@ -60,6 +60,7 @@ macro_rules! impl_runtime { pub const ChannelReserveFee: Balance = 10; pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: 1}; + pub const MaxOutgoingMessages: u32 = 25; } #[derive( @@ -102,6 +103,7 @@ macro_rules! impl_runtime { type HoldIdentifier = MockHoldIdentifer; type DomainRegistration = DomainRegistration; type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; /// function to fetch endpoint response handler by Endpoint. fn get_endpoint_handler( #[allow(unused_variables)] endpoint: &Endpoint, diff --git a/domains/pallets/messenger/src/tests.rs b/domains/pallets/messenger/src/tests.rs index f58ae7a9f0..a60fe44f4f 100644 --- a/domains/pallets/messenger/src/tests.rs +++ b/domains/pallets/messenger/src/tests.rs @@ -9,8 +9,8 @@ use crate::mock::{ use crate::pallet::OutboxMessageCount; use crate::{ ChainAllowlist, ChainAllowlistUpdate, Channel, ChannelId, ChannelState, Channels, - CloseChannelBy, Error, FeeModel, Inbox, InboxResponses, InitiateChannelParams, Nonce, Outbox, - OutboxMessageResult, OutboxResponses, Pallet, U256, + CloseChannelBy, Error, FeeModel, Inbox, InboxResponses, Nonce, Outbox, OutboxMessageResult, + OutboxResponses, Pallet, U256, }; use frame_support::traits::fungible::Inspect; use frame_support::traits::tokens::{Fortitude, Preservation}; @@ -32,16 +32,11 @@ use sp_trie::StorageProof; use std::collections::BTreeSet; fn create_channel(chain_id: ChainId, channel_id: ChannelId) { - let params = InitiateChannelParams { - max_outgoing_messages: 100, - }; - let list = BTreeSet::from([chain_id]); ChainAllowlist::::put(list); assert_ok!(Messenger::initiate_channel( RuntimeOrigin::signed(USER_ACCOUNT), chain_id, - params, )); System::assert_has_event(RuntimeEvent::Messenger( @@ -71,7 +66,7 @@ fn create_channel(chain_id: ChainId, channel_id: ChannelId) { msg.payload, VersionedPayload::V0(Payload::Protocol(RequestResponse::Request( ProtocolMessageRequest::ChannelOpen(ChannelOpenParams { - max_outgoing_messages: params.max_outgoing_messages, + max_outgoing_messages: 25, fee_model: ::ChannelFeeModel::get() }) ))) diff --git a/domains/pallets/transporter/src/mock.rs b/domains/pallets/transporter/src/mock.rs index cf99ce9426..f62e9ba793 100644 --- a/domains/pallets/transporter/src/mock.rs +++ b/domains/pallets/transporter/src/mock.rs @@ -54,6 +54,7 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: 1}; pub TransactionWeightFee: Balance = 100_000; + pub const MaxOutgoingMessages: u32 = 25; } #[derive( @@ -97,6 +98,7 @@ impl pallet_messenger::Config for MockRuntime { type HoldIdentifier = MockHoldIdentifer; type DomainRegistration = DomainRegistration; type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; /// function to fetch endpoint response handler by Endpoint. fn get_endpoint_handler(_endpoint: &Endpoint) -> Option>> { #[cfg(feature = "runtime-benchmarks")] diff --git a/domains/runtime/auto-id/Cargo.toml b/domains/runtime/auto-id/Cargo.toml index fb46a63690..737692c6e9 100644 --- a/domains/runtime/auto-id/Cargo.toml +++ b/domains/runtime/auto-id/Cargo.toml @@ -56,6 +56,7 @@ sp-storage = { default-features = false, git = "https://github.com/subspace/polk sp-subspace-mmr = { version = "0.1.0", default-features = false, path = "../../../crates/sp-subspace-mmr" } sp-transaction-pool = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } sp-version = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } +static_assertions = "1.1.0" subspace-core-primitives = { version = "0.1.0", path = "../../../crates/subspace-core-primitives", default-features = false } subspace-runtime-primitives = { version = "0.1.0", path = "../../../crates/subspace-runtime-primitives", default-features = false } diff --git a/domains/runtime/auto-id/src/lib.rs b/domains/runtime/auto-id/src/lib.rs index ade1200f0c..7b390f9be6 100644 --- a/domains/runtime/auto-id/src/lib.rs +++ b/domains/runtime/auto-id/src/lib.rs @@ -66,6 +66,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{ }; use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf}; use sp_version::RuntimeVersion; +use static_assertions::const_assert; use subspace_runtime_primitives::{ BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment, SlowAdjustingFeeUpdate, SHANNON, SSC, @@ -389,8 +390,12 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; + pub const MaxOutgoingMessages: u32 = 25; } +// ensure the max outgoing messages is not 0. +const_assert!(MaxOutgoingMessages::get() >= 1); + impl pallet_messenger::Config for Runtime { type RuntimeEvent = RuntimeEvent; type SelfChainId = SelfChainId; @@ -416,6 +421,7 @@ impl pallet_messenger::Config for Runtime { type ChannelInitReservePortion = ChannelInitReservePortion; type DomainRegistration = (); type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; } impl frame_system::offchain::SendTransactionTypes for Runtime diff --git a/domains/runtime/evm/Cargo.toml b/domains/runtime/evm/Cargo.toml index 6fecb2f63f..b1826a280a 100644 --- a/domains/runtime/evm/Cargo.toml +++ b/domains/runtime/evm/Cargo.toml @@ -66,6 +66,7 @@ sp-storage = { default-features = false, git = "https://github.com/subspace/polk sp-subspace-mmr = { version = "0.1.0", default-features = false, path = "../../../crates/sp-subspace-mmr" } sp-transaction-pool = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } sp-version = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } +static_assertions = "1.1.0" subspace-core-primitives = { version = "0.1.0", path = "../../../crates/subspace-core-primitives", default-features = false } subspace-runtime-primitives = { version = "0.1.0", path = "../../../crates/subspace-runtime-primitives", default-features = false } diff --git a/domains/runtime/evm/src/lib.rs b/domains/runtime/evm/src/lib.rs index 965573b918..ec5dc5ea81 100644 --- a/domains/runtime/evm/src/lib.rs +++ b/domains/runtime/evm/src/lib.rs @@ -84,6 +84,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{ }; use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf}; use sp_version::RuntimeVersion; +use static_assertions::const_assert; use subspace_runtime_primitives::{ BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment, SlowAdjustingFeeUpdate, SHANNON, SSC, @@ -534,8 +535,12 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; + pub const MaxOutgoingMessages: u32 = 25; } +// ensure the max outgoing messages is not 0. +const_assert!(MaxOutgoingMessages::get() >= 1); + impl pallet_messenger::Config for Runtime { type RuntimeEvent = RuntimeEvent; type SelfChainId = SelfChainId; @@ -561,6 +566,7 @@ impl pallet_messenger::Config for Runtime { type ChannelInitReservePortion = ChannelInitReservePortion; type DomainRegistration = (); type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; } impl frame_system::offchain::SendTransactionTypes for Runtime diff --git a/domains/test/runtime/auto-id/Cargo.toml b/domains/test/runtime/auto-id/Cargo.toml index a69eed2e75..ec75b680f4 100644 --- a/domains/test/runtime/auto-id/Cargo.toml +++ b/domains/test/runtime/auto-id/Cargo.toml @@ -56,6 +56,7 @@ sp-storage = { default-features = false, git = "https://github.com/subspace/polk sp-subspace-mmr = { version = "0.1.0", default-features = false, path = "../../../../crates/sp-subspace-mmr" } sp-transaction-pool = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } sp-version = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } +static_assertions = "1.1.0" subspace-core-primitives = { version = "0.1.0", path = "../../../../crates/subspace-core-primitives", default-features = false } subspace-runtime-primitives = { version = "0.1.0", path = "../../../../crates/subspace-runtime-primitives", default-features = false } diff --git a/domains/test/runtime/auto-id/src/lib.rs b/domains/test/runtime/auto-id/src/lib.rs index ca1d80a13d..369e1e18ec 100644 --- a/domains/test/runtime/auto-id/src/lib.rs +++ b/domains/test/runtime/auto-id/src/lib.rs @@ -66,6 +66,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{ }; use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf}; use sp_version::RuntimeVersion; +use static_assertions::const_assert; use subspace_runtime_primitives::{ BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment, SlowAdjustingFeeUpdate, SSC, @@ -388,8 +389,12 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; + pub const MaxOutgoingMessages: u32 = 25; } +// ensure the max outgoing messages is not 0. +const_assert!(MaxOutgoingMessages::get() >= 1); + impl pallet_messenger::Config for Runtime { type RuntimeEvent = RuntimeEvent; type SelfChainId = SelfChainId; @@ -415,6 +420,7 @@ impl pallet_messenger::Config for Runtime { type ChannelInitReservePortion = ChannelInitReservePortion; type DomainRegistration = (); type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; } impl frame_system::offchain::SendTransactionTypes for Runtime diff --git a/domains/test/runtime/evm/Cargo.toml b/domains/test/runtime/evm/Cargo.toml index 2f26410460..485cda416f 100644 --- a/domains/test/runtime/evm/Cargo.toml +++ b/domains/test/runtime/evm/Cargo.toml @@ -63,6 +63,7 @@ sp-std = { default-features = false, git = "https://github.com/subspace/polkadot sp-subspace-mmr = { version = "0.1.0", default-features = false, path = "../../../../crates/sp-subspace-mmr" } sp-transaction-pool = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } sp-version = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "94a1a8143a89bbe9f938c1939ff68abc1519a305" } +static_assertions = "1.1.0" subspace-core-primitives = { version = "0.1.0", path = "../../../../crates/subspace-core-primitives", default-features = false } subspace-runtime-primitives = { version = "0.1.0", path = "../../../../crates/subspace-runtime-primitives", default-features = false } diff --git a/domains/test/runtime/evm/src/lib.rs b/domains/test/runtime/evm/src/lib.rs index a46ad36a43..020a5fc8d3 100644 --- a/domains/test/runtime/evm/src/lib.rs +++ b/domains/test/runtime/evm/src/lib.rs @@ -84,6 +84,7 @@ use sp_subspace_mmr::domain_mmr_runtime_interface::{ }; use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrLeaf}; use sp_version::RuntimeVersion; +use static_assertions::const_assert; use subspace_runtime_primitives::{ BlockNumber as ConsensusBlockNumber, Hash as ConsensusBlockHash, Moment, SHANNON, SSC, }; @@ -498,8 +499,12 @@ parameter_types! { pub const ChannelReserveFee: Balance = SSC; pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; + pub const MaxOutgoingMessages: u32 = 25; } +// ensure the max outgoing messages is not 0. +const_assert!(MaxOutgoingMessages::get() >= 1); + pub struct StorageKeys; impl sp_messenger::StorageKeys for StorageKeys { @@ -547,6 +552,7 @@ impl pallet_messenger::Config for Runtime { type ChannelInitReservePortion = ChannelInitReservePortion; type DomainRegistration = (); type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; } impl frame_system::offchain::SendTransactionTypes for Runtime diff --git a/test/subspace-test-runtime/src/lib.rs b/test/subspace-test-runtime/src/lib.rs index db14548917..2cad894248 100644 --- a/test/subspace-test-runtime/src/lib.rs +++ b/test/subspace-test-runtime/src/lib.rs @@ -595,8 +595,12 @@ parameter_types! { pub const ChannelReserveFee: Balance = SSC; pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; + pub const MaxOutgoingMessages: u32 = 25; } +// ensure the max outgoing messages is not 0. +const_assert!(MaxOutgoingMessages::get() >= 1); + pub struct OnXDMRewards; impl sp_messenger::OnXDMRewards for OnXDMRewards { @@ -640,6 +644,7 @@ impl pallet_messenger::Config for Runtime { type ChannelInitReservePortion = ChannelInitReservePortion; type DomainRegistration = DomainRegistration; type ChannelFeeModel = ChannelFeeModel; + type MaxOutgoingMessages = MaxOutgoingMessages; } impl frame_system::offchain::SendTransactionTypes for Runtime From 96e9911f5bdd95e64e52366305663c756f1a70cc Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Fri, 24 Jan 2025 13:49:25 +0530 Subject: [PATCH 3/3] use 10_000 and a constant --- crates/subspace-runtime/src/lib.rs | 4 ++-- domains/primitives/runtime/src/lib.rs | 3 +++ domains/runtime/auto-id/src/lib.rs | 4 ++-- domains/runtime/evm/src/lib.rs | 4 ++-- domains/test/runtime/auto-id/src/lib.rs | 4 ++-- domains/test/runtime/evm/src/lib.rs | 4 ++-- test/subspace-test-runtime/src/lib.rs | 4 ++-- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index 0de246f52f..dd655e6c10 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -34,7 +34,7 @@ use core::num::NonZeroU64; use domain_runtime_primitives::opaque::Header as DomainHeader; use domain_runtime_primitives::{ maximum_domain_block_weight, AccountIdConverter, BlockNumber as DomainNumber, - Hash as DomainHash, + Hash as DomainHash, MAX_OUTGOING_MESSAGES, }; use frame_support::genesis_builder_helper::{build_state, get_preset}; use frame_support::inherent::ProvideInherent; @@ -650,7 +650,7 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; - pub const MaxOutgoingMessages: u32 = 25; + pub const MaxOutgoingMessages: u32 = MAX_OUTGOING_MESSAGES; } // ensure the max outgoing messages is not 0. diff --git a/domains/primitives/runtime/src/lib.rs b/domains/primitives/runtime/src/lib.rs index e8989b499f..5918367db8 100644 --- a/domains/primitives/runtime/src/lib.rs +++ b/domains/primitives/runtime/src/lib.rs @@ -71,6 +71,9 @@ pub type EVMChainId = u64; /// Dispatch ratio for domains pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(65); +// Max outgoing messages for XDM +pub const MAX_OUTGOING_MESSAGES: u32 = 10_000; + /// The maximum domain block weight with 3.25 MiB as proof size /// Consensus allows 3.75 MiB but Fraud proof can carry extra size along with proof size /// So we set the proof size to 3.25 MiB diff --git a/domains/runtime/auto-id/src/lib.rs b/domains/runtime/auto-id/src/lib.rs index 7b390f9be6..5e0d73450c 100644 --- a/domains/runtime/auto-id/src/lib.rs +++ b/domains/runtime/auto-id/src/lib.rs @@ -17,7 +17,7 @@ use core::mem; use domain_runtime_primitives::opaque::Header; pub use domain_runtime_primitives::{ block_weights, maximum_block_length, opaque, Balance, BlockNumber, Hash, Nonce, - EXISTENTIAL_DEPOSIT, + EXISTENTIAL_DEPOSIT, MAX_OUTGOING_MESSAGES, }; use domain_runtime_primitives::{ AccountId, Address, CheckExtrinsicsValidityError, DecodeExtrinsicError, HoldIdentifier, @@ -390,7 +390,7 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; - pub const MaxOutgoingMessages: u32 = 25; + pub const MaxOutgoingMessages: u32 = MAX_OUTGOING_MESSAGES; } // ensure the max outgoing messages is not 0. diff --git a/domains/runtime/evm/src/lib.rs b/domains/runtime/evm/src/lib.rs index ec5dc5ea81..68e5b6960a 100644 --- a/domains/runtime/evm/src/lib.rs +++ b/domains/runtime/evm/src/lib.rs @@ -23,7 +23,7 @@ pub use domain_runtime_primitives::{ }; use domain_runtime_primitives::{ CheckExtrinsicsValidityError, DecodeExtrinsicError, HoldIdentifier, ERR_BALANCE_OVERFLOW, - ERR_NONCE_OVERFLOW, SLOT_DURATION, + ERR_NONCE_OVERFLOW, MAX_OUTGOING_MESSAGES, SLOT_DURATION, }; use fp_account::EthereumSignature; use fp_self_contained::{CheckedSignature, SelfContainedCall}; @@ -535,7 +535,7 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; - pub const MaxOutgoingMessages: u32 = 25; + pub const MaxOutgoingMessages: u32 = MAX_OUTGOING_MESSAGES; } // ensure the max outgoing messages is not 0. diff --git a/domains/test/runtime/auto-id/src/lib.rs b/domains/test/runtime/auto-id/src/lib.rs index 369e1e18ec..df9f990554 100644 --- a/domains/test/runtime/auto-id/src/lib.rs +++ b/domains/test/runtime/auto-id/src/lib.rs @@ -17,7 +17,7 @@ use core::mem; use domain_runtime_primitives::opaque::Header; pub use domain_runtime_primitives::{ block_weights, maximum_block_length, opaque, AccountId, Address, Balance, BlockNumber, Hash, - Nonce, Signature, EXISTENTIAL_DEPOSIT, + Nonce, Signature, EXISTENTIAL_DEPOSIT, MAX_OUTGOING_MESSAGES, }; use domain_runtime_primitives::{ CheckExtrinsicsValidityError, DecodeExtrinsicError, HoldIdentifier, ERR_BALANCE_OVERFLOW, @@ -389,7 +389,7 @@ parameter_types! { pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); // TODO update the fee model pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; - pub const MaxOutgoingMessages: u32 = 25; + pub const MaxOutgoingMessages: u32 = MAX_OUTGOING_MESSAGES; } // ensure the max outgoing messages is not 0. diff --git a/domains/test/runtime/evm/src/lib.rs b/domains/test/runtime/evm/src/lib.rs index 020a5fc8d3..299bfcf0a7 100644 --- a/domains/test/runtime/evm/src/lib.rs +++ b/domains/test/runtime/evm/src/lib.rs @@ -19,7 +19,7 @@ use core::mem; pub use domain_runtime_primitives::opaque::Header; use domain_runtime_primitives::{ block_weights, maximum_block_length, maximum_domain_block_weight, ERR_BALANCE_OVERFLOW, - ERR_NONCE_OVERFLOW, EXISTENTIAL_DEPOSIT, SLOT_DURATION, + ERR_NONCE_OVERFLOW, EXISTENTIAL_DEPOSIT, MAX_OUTGOING_MESSAGES, SLOT_DURATION, }; pub use domain_runtime_primitives::{ opaque, Balance, BlockNumber, CheckExtrinsicsValidityError, DecodeExtrinsicError, Hash, @@ -499,7 +499,7 @@ parameter_types! { pub const ChannelReserveFee: Balance = SSC; pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; - pub const MaxOutgoingMessages: u32 = 25; + pub const MaxOutgoingMessages: u32 = MAX_OUTGOING_MESSAGES; } // ensure the max outgoing messages is not 0. diff --git a/test/subspace-test-runtime/src/lib.rs b/test/subspace-test-runtime/src/lib.rs index 2cad894248..7f74f3be88 100644 --- a/test/subspace-test-runtime/src/lib.rs +++ b/test/subspace-test-runtime/src/lib.rs @@ -41,7 +41,7 @@ use core::mem; use core::num::NonZeroU64; use domain_runtime_primitives::opaque::Header as DomainHeader; use domain_runtime_primitives::{ - AccountIdConverter, BlockNumber as DomainNumber, Hash as DomainHash, + AccountIdConverter, BlockNumber as DomainNumber, Hash as DomainHash, MAX_OUTGOING_MESSAGES, }; use frame_support::genesis_builder_helper::{build_state, get_preset}; use frame_support::inherent::ProvideInherent; @@ -595,7 +595,7 @@ parameter_types! { pub const ChannelReserveFee: Balance = SSC; pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20); pub const ChannelFeeModel: FeeModel = FeeModel{relay_fee: SSC}; - pub const MaxOutgoingMessages: u32 = 25; + pub const MaxOutgoingMessages: u32 = MAX_OUTGOING_MESSAGES; } // ensure the max outgoing messages is not 0.