From 466d0ab85c432153498bea97d32a2355c6d0d67d Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Sun, 17 Mar 2024 22:43:59 +1300 Subject: [PATCH 01/23] establish_channel_with_system --- polkadot/runtime/parachains/src/hrmp.rs | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index 42592d9d9f14..ed99d6653465 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -836,6 +836,37 @@ pub mod pallet { Ok(()) } + + /// TODO write docs + #[pallet::call_index(10)] + #[pallet::weight(::WeightInfo::establish_system_channel())] // TODO benchmarks + pub fn establish_channel_with_system( + origin: OriginFor, + recipient: ParaId, + ) -> DispatchResultWithPostInfo { + let sender = ensure_parachain(::RuntimeOrigin::from(origin))?; + + ensure!( + recipient.is_system(), + Error::::ChannelCreationNotAuthorized + ); + + let config = >::config(); + let max_message_size = config.hrmp_channel_max_message_size; + let max_capacity = config.hrmp_channel_max_capacity; + + Self::init_open_channel(sender, recipient, max_capacity, max_message_size)?; + Self::accept_open_channel(recipient, sender)?; + + Self::deposit_event(Event::HrmpSystemChannelOpened { + sender, + recipient, + proposed_max_capacity: max_capacity, + proposed_max_message_size: max_message_size, + }); + + Ok(Pays::No.into()) + } } } From 544e7871e01f632c4c708b37b1db12957b1cce1d Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Sun, 17 Mar 2024 23:24:05 +1300 Subject: [PATCH 02/23] fmt --- polkadot/runtime/parachains/src/hrmp.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index ed99d6653465..bf7d141d458b 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -846,10 +846,7 @@ pub mod pallet { ) -> DispatchResultWithPostInfo { let sender = ensure_parachain(::RuntimeOrigin::from(origin))?; - ensure!( - recipient.is_system(), - Error::::ChannelCreationNotAuthorized - ); + ensure!(recipient.is_system(), Error::::ChannelCreationNotAuthorized); let config = >::config(); let max_message_size = config.hrmp_channel_max_message_size; From 1f803422b83e57b21f218f4eb93ca4be5043d1bc Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Mon, 25 Mar 2024 14:25:06 +1300 Subject: [PATCH 03/23] make default channel para to be configuable --- polkadot/runtime/parachains/src/hrmp.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index bf7d141d458b..b67919a08abb 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -270,6 +270,9 @@ pub mod pallet { /// implementation should be the same as `Balance` as used in the `Configuration`. type Currency: ReservableCurrency; + /// The default channel size and capacity to use when opening a channel to a system parachain. + type DefaultChannelSizeAndCapacityToSystem: Get<(u32, u32)>; + /// Something that provides the weight of this pallet. type WeightInfo: WeightInfo; } @@ -848,13 +851,15 @@ pub mod pallet { ensure!(recipient.is_system(), Error::::ChannelCreationNotAuthorized); - let config = >::config(); - let max_message_size = config.hrmp_channel_max_message_size; - let max_capacity = config.hrmp_channel_max_capacity; + let (max_message_size, max_capacity) = T::DefaultChannelSizeAndCapacityToSystem::get(); + // create bidirectional channell Self::init_open_channel(sender, recipient, max_capacity, max_message_size)?; Self::accept_open_channel(recipient, sender)?; + Self::init_open_channel(recipient, sender, max_capacity, max_message_size)?; + Self::accept_open_channel(sender, recipient)?; + Self::deposit_event(Event::HrmpSystemChannelOpened { sender, recipient, From 30560d2f2e8aa5b1097817e569a3e1ff0491747e Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Mon, 25 Mar 2024 14:36:37 +1300 Subject: [PATCH 04/23] fmt --- polkadot/runtime/parachains/src/hrmp.rs | 66 +++++++++++++------------ 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index b67919a08abb..c1aab8d338b0 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -270,7 +270,8 @@ pub mod pallet { /// implementation should be the same as `Balance` as used in the `Configuration`. type Currency: ReservableCurrency; - /// The default channel size and capacity to use when opening a channel to a system parachain. + /// The default channel size and capacity to use when opening a channel to a system + /// parachain. type DefaultChannelSizeAndCapacityToSystem: Get<(u32, u32)>; /// Something that provides the weight of this pallet. @@ -567,13 +568,13 @@ pub mod pallet { T::ChannelManager::ensure_origin(origin)?; ensure!( - HrmpIngressChannelsIndex::::decode_len(para).unwrap_or_default() <= - num_inbound as usize, + HrmpIngressChannelsIndex::::decode_len(para).unwrap_or_default() + <= num_inbound as usize, Error::::WrongWitness ); ensure!( - HrmpEgressChannelsIndex::::decode_len(para).unwrap_or_default() <= - num_outbound as usize, + HrmpEgressChannelsIndex::::decode_len(para).unwrap_or_default() + <= num_outbound as usize, Error::::WrongWitness ); @@ -595,8 +596,8 @@ pub mod pallet { T::ChannelManager::ensure_origin(origin)?; ensure!( - HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 <= - channels, + HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 + <= channels, Error::::WrongWitness ); @@ -619,8 +620,8 @@ pub mod pallet { T::ChannelManager::ensure_origin(origin)?; ensure!( - HrmpCloseChannelRequestsList::::decode_len().unwrap_or_default() as u32 <= - channels, + HrmpCloseChannelRequestsList::::decode_len().unwrap_or_default() as u32 + <= channels, Error::::WrongWitness ); @@ -645,8 +646,8 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_parachain(::RuntimeOrigin::from(origin))?; ensure!( - HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 <= - open_requests, + HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 + <= open_requests, Error::::WrongWitness ); Self::cancel_open_request(origin, channel_id.clone())?; @@ -779,10 +780,10 @@ pub mod pallet { let current_recipient_deposit = channel.recipient_deposit; // nothing to update - if current_sender_deposit == new_sender_deposit && - current_recipient_deposit == new_recipient_deposit + if current_sender_deposit == new_sender_deposit + && current_recipient_deposit == new_recipient_deposit { - return Ok(()) + return Ok(()); } // sender @@ -830,7 +831,7 @@ pub mod pallet { channel.sender_deposit = new_sender_deposit; channel.recipient_deposit = new_recipient_deposit; } else { - return Err(Error::::OpenHrmpChannelDoesntExist.into()) + return Err(Error::::OpenHrmpChannelDoesntExist.into()); } Ok(()) })?; @@ -970,7 +971,7 @@ impl Pallet { Some(req_data) => req_data, None => { // Can't normally happen but no need to panic. - continue + continue; }, }; @@ -1028,7 +1029,7 @@ impl Pallet { fn process_hrmp_open_channel_requests(config: &HostConfiguration>) { let mut open_req_channels = HrmpOpenChannelRequestsList::::get(); if open_req_channels.is_empty() { - return + return; } // iterate the vector starting from the end making our way to the beginning. This way we @@ -1037,7 +1038,7 @@ impl Pallet { loop { // bail if we've iterated over all items. if idx == 0 { - break + break; } idx -= 1; @@ -1051,8 +1052,8 @@ impl Pallet { let recipient_deposit = if system_channel { 0 } else { config.hrmp_recipient_deposit }; if request.confirmed { - if >::is_valid_para(channel_id.sender) && - >::is_valid_para(channel_id.recipient) + if >::is_valid_para(channel_id.sender) + && >::is_valid_para(channel_id.recipient) { HrmpChannels::::insert( &channel_id, @@ -1149,14 +1150,14 @@ impl Pallet { // (b) However, a parachain cannot read into "the future", therefore the watermark should // not be greater than the relay-chain context block which the parablock refers to. if new_hrmp_watermark == relay_chain_parent_number { - return Ok(()) + return Ok(()); } if new_hrmp_watermark > relay_chain_parent_number { return Err(HrmpWatermarkAcceptanceErr::AheadRelayParent { new_watermark: new_hrmp_watermark, relay_chain_parent_number, - }) + }); } if let Some(last_watermark) = HrmpWatermarks::::get(&recipient) { @@ -1164,7 +1165,7 @@ impl Pallet { return Err(HrmpWatermarkAcceptanceErr::AdvancementRule { new_watermark: new_hrmp_watermark, last_watermark, - }) + }); } } @@ -1179,7 +1180,7 @@ impl Pallet { { return Err(HrmpWatermarkAcceptanceErr::LandsOnBlockWithNoMessages { new_watermark: new_hrmp_watermark, - }) + }); } Ok(()) } @@ -1201,7 +1202,7 @@ impl Pallet { return Err(OutboundHrmpAcceptanceErr::MoreMessagesThanPermitted { sent: out_hrmp_msgs.len() as u32, permitted: config.hrmp_max_message_num_per_candidate, - }) + }); } let mut last_recipient = None::; @@ -1213,8 +1214,9 @@ impl Pallet { // the messages must be sorted in ascending order and there must be no two messages // sent to the same recipient. Thus we can check that every recipient is strictly // greater than the previous one. - Some(last_recipient) if out_msg.recipient <= last_recipient => - return Err(OutboundHrmpAcceptanceErr::NotSorted { idx }), + Some(last_recipient) if out_msg.recipient <= last_recipient => { + return Err(OutboundHrmpAcceptanceErr::NotSorted { idx }) + }, _ => last_recipient = Some(out_msg.recipient), } @@ -1231,7 +1233,7 @@ impl Pallet { idx, msg_size, max_size: channel.max_message_size, - }) + }); } let new_total_size = channel.total_size + out_msg.data.len() as u32; @@ -1240,7 +1242,7 @@ impl Pallet { idx, total_size: new_total_size, limit: channel.max_total_size, - }) + }); } let new_msg_count = channel.msg_count + 1; @@ -1249,7 +1251,7 @@ impl Pallet { idx, count: new_msg_count, limit: channel.max_capacity, - }) + }); } } @@ -1263,7 +1265,7 @@ impl Pallet { for recipient in recipients { let Some(channel) = HrmpChannels::::get(&HrmpChannelId { sender, recipient }) else { - continue + continue; }; remaining.push(( recipient, @@ -1353,7 +1355,7 @@ impl Pallet { None => { // apparently, that since acceptance of this candidate the recipient was // offboarded and the channel no longer exists. - continue + continue; }, }; From 1e3b210906836534ae50e78b78f4e08e2d5dcf74 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Mon, 25 Mar 2024 15:03:11 +1300 Subject: [PATCH 05/23] fix --- polkadot/runtime/parachains/src/mock.rs | 2 ++ polkadot/runtime/rococo/src/lib.rs | 5 +++++ polkadot/runtime/test-runtime/src/lib.rs | 2 ++ polkadot/runtime/westend/src/lib.rs | 5 +++++ 4 files changed, 14 insertions(+) diff --git a/polkadot/runtime/parachains/src/mock.rs b/polkadot/runtime/parachains/src/mock.rs index 7ed62a392e4e..8ccfd3604d47 100644 --- a/polkadot/runtime/parachains/src/mock.rs +++ b/polkadot/runtime/parachains/src/mock.rs @@ -248,6 +248,7 @@ impl crate::dmp::Config for Test {} parameter_types! { pub const FirstMessageFactorPercent: u64 = 100; + pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); } impl crate::hrmp::Config for Test { @@ -255,6 +256,7 @@ impl crate::hrmp::Config for Test { type RuntimeEvent = RuntimeEvent; type ChannelManager = frame_system::EnsureRoot; type Currency = pallet_balances::Pallet; + type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; type WeightInfo = crate::hrmp::TestWeightInfo; } diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 1f51258df089..a4fec430154d 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -995,11 +995,16 @@ impl pallet_message_queue::Config for Runtime { impl parachains_dmp::Config for Runtime {} +parameter_types! { + pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); +} + impl parachains_hrmp::Config for Runtime { type RuntimeOrigin = RuntimeOrigin; type RuntimeEvent = RuntimeEvent; type ChannelManager = EnsureRoot; type Currency = Balances; + type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; type WeightInfo = weights::runtime_parachains_hrmp::WeightInfo; } diff --git a/polkadot/runtime/test-runtime/src/lib.rs b/polkadot/runtime/test-runtime/src/lib.rs index 8a3cd9309dbd..3e16940ae7d7 100644 --- a/polkadot/runtime/test-runtime/src/lib.rs +++ b/polkadot/runtime/test-runtime/src/lib.rs @@ -554,6 +554,7 @@ impl parachains_dmp::Config for Runtime {} parameter_types! { pub const FirstMessageFactorPercent: u64 = 100; + pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); } impl parachains_hrmp::Config for Runtime { @@ -561,6 +562,7 @@ impl parachains_hrmp::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ChannelManager = frame_system::EnsureRoot; type Currency = Balances; + type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; type WeightInfo = parachains_hrmp::TestWeightInfo; } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 15d78706d3a4..e711af8f4862 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1200,11 +1200,16 @@ impl pallet_message_queue::Config for Runtime { impl parachains_dmp::Config for Runtime {} +parameter_types! { + pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); +} + impl parachains_hrmp::Config for Runtime { type RuntimeOrigin = RuntimeOrigin; type RuntimeEvent = RuntimeEvent; type ChannelManager = EnsureRoot; type Currency = Balances; + type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; type WeightInfo = weights::runtime_parachains_hrmp::WeightInfo; } From cf94655872862e665904a00039c8339be2e7948a Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Tue, 26 Mar 2024 18:08:57 +1300 Subject: [PATCH 06/23] Update polkadot/runtime/parachains/src/hrmp.rs Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> --- polkadot/runtime/parachains/src/hrmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index c1aab8d338b0..a237b2803b36 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -272,7 +272,7 @@ pub mod pallet { /// The default channel size and capacity to use when opening a channel to a system /// parachain. - type DefaultChannelSizeAndCapacityToSystem: Get<(u32, u32)>; + type DefaultChannelSizeAndCapacityWithSystem: Get<(u32, u32)>; /// Something that provides the weight of this pallet. type WeightInfo: WeightInfo; From b6bc275ced92c3d35a6cb532f6151ba2515f61b6 Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Tue, 26 Mar 2024 18:09:04 +1300 Subject: [PATCH 07/23] Update polkadot/runtime/parachains/src/hrmp.rs Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> --- polkadot/runtime/parachains/src/hrmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index a237b2803b36..46bf0de55905 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -854,7 +854,7 @@ pub mod pallet { let (max_message_size, max_capacity) = T::DefaultChannelSizeAndCapacityToSystem::get(); - // create bidirectional channell + // create bidirectional channel Self::init_open_channel(sender, recipient, max_capacity, max_message_size)?; Self::accept_open_channel(recipient, sender)?; From 79da6621fc9ef4a37791771a0264a2d04bb30875 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 26 Mar 2024 19:31:02 +1300 Subject: [PATCH 08/23] rename and event --- polkadot/runtime/parachains/src/hrmp.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index 46bf0de55905..3a73bfad736b 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -846,24 +846,31 @@ pub mod pallet { #[pallet::weight(::WeightInfo::establish_system_channel())] // TODO benchmarks pub fn establish_channel_with_system( origin: OriginFor, - recipient: ParaId, + target_system_chain: ParaId, ) -> DispatchResultWithPostInfo { let sender = ensure_parachain(::RuntimeOrigin::from(origin))?; - ensure!(recipient.is_system(), Error::::ChannelCreationNotAuthorized); + ensure!(target_system_chain.is_system(), Error::::ChannelCreationNotAuthorized); - let (max_message_size, max_capacity) = T::DefaultChannelSizeAndCapacityToSystem::get(); + let (max_message_size, max_capacity) = T::DefaultChannelSizeAndCapacityWithSystem::get(); // create bidirectional channel - Self::init_open_channel(sender, recipient, max_capacity, max_message_size)?; - Self::accept_open_channel(recipient, sender)?; + Self::init_open_channel(sender, target_system_chain, max_capacity, max_message_size)?; + Self::accept_open_channel(target_system_chain, sender)?; - Self::init_open_channel(recipient, sender, max_capacity, max_message_size)?; - Self::accept_open_channel(sender, recipient)?; + Self::init_open_channel(target_system_chain, sender, max_capacity, max_message_size)?; + Self::accept_open_channel(sender, target_system_chain)?; Self::deposit_event(Event::HrmpSystemChannelOpened { sender, - recipient, + recipient: target_system_chain, + proposed_max_capacity: max_capacity, + proposed_max_message_size: max_message_size, + }); + + Self::deposit_event(Event::HrmpSystemChannelOpened { + sender: target_system_chain, + recipient: sender, proposed_max_capacity: max_capacity, proposed_max_message_size: max_message_size, }); From 1a1df499e1d1b01326eab92961509566e95a1af9 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 26 Mar 2024 19:33:29 +1300 Subject: [PATCH 09/23] fmt --- polkadot/runtime/parachains/src/hrmp.rs | 36 ++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index 3a73bfad736b..c95234fe77d1 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -568,13 +568,13 @@ pub mod pallet { T::ChannelManager::ensure_origin(origin)?; ensure!( - HrmpIngressChannelsIndex::::decode_len(para).unwrap_or_default() - <= num_inbound as usize, + HrmpIngressChannelsIndex::::decode_len(para).unwrap_or_default() <= + num_inbound as usize, Error::::WrongWitness ); ensure!( - HrmpEgressChannelsIndex::::decode_len(para).unwrap_or_default() - <= num_outbound as usize, + HrmpEgressChannelsIndex::::decode_len(para).unwrap_or_default() <= + num_outbound as usize, Error::::WrongWitness ); @@ -596,8 +596,8 @@ pub mod pallet { T::ChannelManager::ensure_origin(origin)?; ensure!( - HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 - <= channels, + HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 <= + channels, Error::::WrongWitness ); @@ -620,8 +620,8 @@ pub mod pallet { T::ChannelManager::ensure_origin(origin)?; ensure!( - HrmpCloseChannelRequestsList::::decode_len().unwrap_or_default() as u32 - <= channels, + HrmpCloseChannelRequestsList::::decode_len().unwrap_or_default() as u32 <= + channels, Error::::WrongWitness ); @@ -646,8 +646,8 @@ pub mod pallet { ) -> DispatchResult { let origin = ensure_parachain(::RuntimeOrigin::from(origin))?; ensure!( - HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 - <= open_requests, + HrmpOpenChannelRequestsList::::decode_len().unwrap_or_default() as u32 <= + open_requests, Error::::WrongWitness ); Self::cancel_open_request(origin, channel_id.clone())?; @@ -780,8 +780,8 @@ pub mod pallet { let current_recipient_deposit = channel.recipient_deposit; // nothing to update - if current_sender_deposit == new_sender_deposit - && current_recipient_deposit == new_recipient_deposit + if current_sender_deposit == new_sender_deposit && + current_recipient_deposit == new_recipient_deposit { return Ok(()); } @@ -852,7 +852,8 @@ pub mod pallet { ensure!(target_system_chain.is_system(), Error::::ChannelCreationNotAuthorized); - let (max_message_size, max_capacity) = T::DefaultChannelSizeAndCapacityWithSystem::get(); + let (max_message_size, max_capacity) = + T::DefaultChannelSizeAndCapacityWithSystem::get(); // create bidirectional channel Self::init_open_channel(sender, target_system_chain, max_capacity, max_message_size)?; @@ -1059,8 +1060,8 @@ impl Pallet { let recipient_deposit = if system_channel { 0 } else { config.hrmp_recipient_deposit }; if request.confirmed { - if >::is_valid_para(channel_id.sender) - && >::is_valid_para(channel_id.recipient) + if >::is_valid_para(channel_id.sender) && + >::is_valid_para(channel_id.recipient) { HrmpChannels::::insert( &channel_id, @@ -1221,9 +1222,8 @@ impl Pallet { // the messages must be sorted in ascending order and there must be no two messages // sent to the same recipient. Thus we can check that every recipient is strictly // greater than the previous one. - Some(last_recipient) if out_msg.recipient <= last_recipient => { - return Err(OutboundHrmpAcceptanceErr::NotSorted { idx }) - }, + Some(last_recipient) if out_msg.recipient <= last_recipient => + return Err(OutboundHrmpAcceptanceErr::NotSorted { idx }), _ => last_recipient = Some(out_msg.recipient), } From 7ed9f54a212dbdf8597b2e6ea35458cd401692bc Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 26 Mar 2024 20:00:45 +1300 Subject: [PATCH 10/23] semi --- polkadot/runtime/parachains/src/hrmp.rs | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index c95234fe77d1..cdc8bc113502 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -783,7 +783,7 @@ pub mod pallet { if current_sender_deposit == new_sender_deposit && current_recipient_deposit == new_recipient_deposit { - return Ok(()); + return Ok(()) } // sender @@ -831,7 +831,7 @@ pub mod pallet { channel.sender_deposit = new_sender_deposit; channel.recipient_deposit = new_recipient_deposit; } else { - return Err(Error::::OpenHrmpChannelDoesntExist.into()); + return Err(Error::::OpenHrmpChannelDoesntExist.into()) } Ok(()) })?; @@ -979,7 +979,7 @@ impl Pallet { Some(req_data) => req_data, None => { // Can't normally happen but no need to panic. - continue; + continue }, }; @@ -1046,7 +1046,7 @@ impl Pallet { loop { // bail if we've iterated over all items. if idx == 0 { - break; + break } idx -= 1; @@ -1158,14 +1158,14 @@ impl Pallet { // (b) However, a parachain cannot read into "the future", therefore the watermark should // not be greater than the relay-chain context block which the parablock refers to. if new_hrmp_watermark == relay_chain_parent_number { - return Ok(()); + return Ok(()) } if new_hrmp_watermark > relay_chain_parent_number { return Err(HrmpWatermarkAcceptanceErr::AheadRelayParent { new_watermark: new_hrmp_watermark, relay_chain_parent_number, - }); + }) } if let Some(last_watermark) = HrmpWatermarks::::get(&recipient) { @@ -1173,7 +1173,7 @@ impl Pallet { return Err(HrmpWatermarkAcceptanceErr::AdvancementRule { new_watermark: new_hrmp_watermark, last_watermark, - }); + }) } } @@ -1188,7 +1188,7 @@ impl Pallet { { return Err(HrmpWatermarkAcceptanceErr::LandsOnBlockWithNoMessages { new_watermark: new_hrmp_watermark, - }); + }) } Ok(()) } @@ -1210,7 +1210,7 @@ impl Pallet { return Err(OutboundHrmpAcceptanceErr::MoreMessagesThanPermitted { sent: out_hrmp_msgs.len() as u32, permitted: config.hrmp_max_message_num_per_candidate, - }); + }) } let mut last_recipient = None::; @@ -1240,7 +1240,7 @@ impl Pallet { idx, msg_size, max_size: channel.max_message_size, - }); + }) } let new_total_size = channel.total_size + out_msg.data.len() as u32; @@ -1249,7 +1249,7 @@ impl Pallet { idx, total_size: new_total_size, limit: channel.max_total_size, - }); + }) } let new_msg_count = channel.msg_count + 1; @@ -1258,7 +1258,7 @@ impl Pallet { idx, count: new_msg_count, limit: channel.max_capacity, - }); + }) } } @@ -1272,7 +1272,7 @@ impl Pallet { for recipient in recipients { let Some(channel) = HrmpChannels::::get(&HrmpChannelId { sender, recipient }) else { - continue; + continue }; remaining.push(( recipient, @@ -1362,7 +1362,7 @@ impl Pallet { None => { // apparently, that since acceptance of this candidate the recipient was // offboarded and the channel no longer exists. - continue; + continue }, }; From e163fa8cc6d4463a7f1050e9f8853d25c4029ee6 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 26 Mar 2024 20:01:59 +1300 Subject: [PATCH 11/23] fix --- polkadot/runtime/parachains/src/hrmp.rs | 2 +- polkadot/runtime/parachains/src/mock.rs | 4 ++-- polkadot/runtime/rococo/src/lib.rs | 4 ++-- polkadot/runtime/test-runtime/src/lib.rs | 4 ++-- polkadot/runtime/westend/src/lib.rs | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index cdc8bc113502..c6ceb08d1e89 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -1037,7 +1037,7 @@ impl Pallet { fn process_hrmp_open_channel_requests(config: &HostConfiguration>) { let mut open_req_channels = HrmpOpenChannelRequestsList::::get(); if open_req_channels.is_empty() { - return; + return } // iterate the vector starting from the end making our way to the beginning. This way we diff --git a/polkadot/runtime/parachains/src/mock.rs b/polkadot/runtime/parachains/src/mock.rs index 8ccfd3604d47..cc13feb698dc 100644 --- a/polkadot/runtime/parachains/src/mock.rs +++ b/polkadot/runtime/parachains/src/mock.rs @@ -248,7 +248,7 @@ impl crate::dmp::Config for Test {} parameter_types! { pub const FirstMessageFactorPercent: u64 = 100; - pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); + pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (51200, 500); } impl crate::hrmp::Config for Test { @@ -256,7 +256,7 @@ impl crate::hrmp::Config for Test { type RuntimeEvent = RuntimeEvent; type ChannelManager = frame_system::EnsureRoot; type Currency = pallet_balances::Pallet; - type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; + type DefaultChannelSizeAndCapacityWithSystem = DefaultChannelSizeAndCapacityWithSystem; type WeightInfo = crate::hrmp::TestWeightInfo; } diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 2b24008ea67c..02a2127afa33 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -996,7 +996,7 @@ impl pallet_message_queue::Config for Runtime { impl parachains_dmp::Config for Runtime {} parameter_types! { - pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); + pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (51200, 500); } impl parachains_hrmp::Config for Runtime { @@ -1004,7 +1004,7 @@ impl parachains_hrmp::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ChannelManager = EnsureRoot; type Currency = Balances; - type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; + type DefaultChannelSizeAndCapacityWithSystem = DefaultChannelSizeAndCapacityWithSystem; type WeightInfo = weights::runtime_parachains_hrmp::WeightInfo; } diff --git a/polkadot/runtime/test-runtime/src/lib.rs b/polkadot/runtime/test-runtime/src/lib.rs index 3e16940ae7d7..f3ebe9296ae2 100644 --- a/polkadot/runtime/test-runtime/src/lib.rs +++ b/polkadot/runtime/test-runtime/src/lib.rs @@ -554,7 +554,7 @@ impl parachains_dmp::Config for Runtime {} parameter_types! { pub const FirstMessageFactorPercent: u64 = 100; - pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); + pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (51200, 500); } impl parachains_hrmp::Config for Runtime { @@ -562,7 +562,7 @@ impl parachains_hrmp::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ChannelManager = frame_system::EnsureRoot; type Currency = Balances; - type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; + type DefaultChannelSizeAndCapacityWithSystem = DefaultChannelSizeAndCapacityWithSystem; type WeightInfo = parachains_hrmp::TestWeightInfo; } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 691aed201d87..149ae88ac275 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1201,7 +1201,7 @@ impl pallet_message_queue::Config for Runtime { impl parachains_dmp::Config for Runtime {} parameter_types! { - pub const DefaultChannelSizeAndCapacityToSystem: (u32, u32) = (51200, 500); + pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (51200, 500); } impl parachains_hrmp::Config for Runtime { @@ -1209,7 +1209,7 @@ impl parachains_hrmp::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ChannelManager = EnsureRoot; type Currency = Balances; - type DefaultChannelSizeAndCapacityToSystem = DefaultChannelSizeAndCapacityToSystem; + type DefaultChannelSizeAndCapacityWithSystem = DefaultChannelSizeAndCapacityWithSystem; type WeightInfo = weights::runtime_parachains_hrmp::WeightInfo; } From 807d088fec0e393bbc77900ceadc776f1ffa183d Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 27 Mar 2024 17:27:53 +1300 Subject: [PATCH 12/23] add benchmarks and docs --- polkadot/runtime/parachains/src/hrmp.rs | 14 ++++++++++++-- .../runtime/parachains/src/hrmp/benchmarking.rs | 15 +++++++++++++++ .../rococo/src/weights/runtime_parachains_hrmp.rs | 10 ++++++++++ .../src/weights/runtime_parachains_hrmp.rs | 10 ++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index c6ceb08d1e89..15a711c6ceb5 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -65,6 +65,7 @@ pub trait WeightInfo { fn force_open_hrmp_channel(c: u32) -> Weight; fn establish_system_channel() -> Weight; fn poke_channel_deposits() -> Weight; + fn establish_channel_with_system() -> Weight; } /// A weight info that is only suitable for testing. @@ -104,6 +105,9 @@ impl WeightInfo for TestWeightInfo { fn poke_channel_deposits() -> Weight { Weight::MAX } + fn establish_channel_with_system() -> Weight { + Weight::MAX + } } /// A description of a request to open an HRMP channel. @@ -841,9 +845,15 @@ pub mod pallet { Ok(()) } - /// TODO write docs + /// Establish a bidirectional HRMP channel between a parachain and a system chain. + /// + /// Arguments: + /// + /// - `target_system_chain`: A system chain, `ParaId`. + /// + /// The origin needs to be the parachain origin. #[pallet::call_index(10)] - #[pallet::weight(::WeightInfo::establish_system_channel())] // TODO benchmarks + #[pallet::weight(::WeightInfo::establish_channel_with_system())] pub fn establish_channel_with_system( origin: OriginFor, target_system_chain: ParaId, diff --git a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs index c6baf2f30cf5..0aef25ae6205 100644 --- a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs +++ b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs @@ -513,6 +513,21 @@ mod benchmarks { ); } + #[benchmark] + fn establish_channel_with_system() { + let sender_id = 1u32; + let recipient_id: ParaId = 2u32.into(); + + let sender_origin: crate::Origin = sender_id.into(); + + // make sure para is registered, and has zero balance. + register_parachain_with_balance::(sender_id.into(), Zero::zero()); + register_parachain_with_balance::(recipient_id, Zero::zero()); + + #[extrinsic_call] + _(sender_origin, recipient_id); + } + impl_benchmark_test_suite!( Hrmp, crate::mock::new_test_ext(crate::hrmp::tests::GenesisConfigBuilder::default().build()), diff --git a/polkadot/runtime/rococo/src/weights/runtime_parachains_hrmp.rs b/polkadot/runtime/rococo/src/weights/runtime_parachains_hrmp.rs index 417820e6627f..97b84155b36a 100644 --- a/polkadot/runtime/rococo/src/weights/runtime_parachains_hrmp.rs +++ b/polkadot/runtime/rococo/src/weights/runtime_parachains_hrmp.rs @@ -331,4 +331,14 @@ impl runtime_parachains::hrmp::WeightInfo for WeightInf .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } + fn establish_channel_with_system() -> Weight { + // Proof Size summary in bytes: + // Measured: `417` + // Estimated: `6357` + // Minimum execution time: 629_674_000 picoseconds. + Weight::from_parts(640_174_000, 0) + .saturating_add(Weight::from_parts(0, 6357)) + .saturating_add(T::DbWeight::get().reads(12)) + .saturating_add(T::DbWeight::get().writes(8)) + } } diff --git a/polkadot/runtime/westend/src/weights/runtime_parachains_hrmp.rs b/polkadot/runtime/westend/src/weights/runtime_parachains_hrmp.rs index 9beb15303d87..3d2ab827b8fd 100644 --- a/polkadot/runtime/westend/src/weights/runtime_parachains_hrmp.rs +++ b/polkadot/runtime/westend/src/weights/runtime_parachains_hrmp.rs @@ -324,4 +324,14 @@ impl runtime_parachains::hrmp::WeightInfo for WeightInf .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } + fn establish_channel_with_system() -> Weight { + // Proof Size summary in bytes: + // Measured: `417` + // Estimated: `6357` + // Minimum execution time: 629_674_000 picoseconds. + Weight::from_parts(640_174_000, 0) + .saturating_add(Weight::from_parts(0, 6357)) + .saturating_add(T::DbWeight::get().reads(12)) + .saturating_add(T::DbWeight::get().writes(8)) + } } From af1f3372fa258f1cc46d28cc57faf4a606ee5d13 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 27 Mar 2024 20:05:41 +1300 Subject: [PATCH 13/23] fix --- polkadot/runtime/parachains/src/hrmp/tests.rs | 71 +++++++++++++++++++ polkadot/runtime/parachains/src/mock.rs | 2 +- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/polkadot/runtime/parachains/src/hrmp/tests.rs b/polkadot/runtime/parachains/src/hrmp/tests.rs index 162c14121601..9e0565b5b0de 100644 --- a/polkadot/runtime/parachains/src/hrmp/tests.rs +++ b/polkadot/runtime/parachains/src/hrmp/tests.rs @@ -932,3 +932,74 @@ fn watermark_maxed_out_at_relay_parent() { Hrmp::assert_storage_consistency_exhaustive(); }); } + +// #[test] +// fn open_system_channel_works() { +// let para_a = 1.into(); +// let para_b = 3.into(); + +// new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { +// // We need both A & B to be registered and live parachains. +// register_parachain(para_a); +// register_parachain(para_b); + +// run_to_block(5, Some(vec![4, 5])); +// Hrmp::establish_system_channel(RuntimeOrigin::signed(1), para_a, para_b).unwrap(); +// Hrmp::assert_storage_consistency_exhaustive(); +// assert!(System::events().iter().any(|record| record.event == +// MockEvent::Hrmp(Event::HrmpSystemChannelOpened { +// sender: para_a, +// recipient: para_b, +// proposed_max_capacity: 2, +// proposed_max_message_size: 8 +// }))); + +// // Advance to a block 6, but without session change. That means that the channel has +// // not been created yet. +// run_to_block(6, None); +// assert!(!channel_exists(para_a, para_b)); +// Hrmp::assert_storage_consistency_exhaustive(); + +// // Now let the session change happen and thus open the channel. +// run_to_block(8, Some(vec![8])); +// assert!(channel_exists(para_a, para_b)); +// }); +// } + +// #[test] +// fn open_system_channel_does_not_work_for_non_system_chains() { +// let para_a = 2001.into(); +// let para_b = 2003.into(); + +// new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { +// // We need both A & B to be registered and live parachains. +// register_parachain(para_a); +// register_parachain(para_b); + +// run_to_block(5, Some(vec![4, 5])); +// assert_noop!( +// Hrmp::establish_system_channel(RuntimeOrigin::signed(1), para_a, para_b), +// Error::::ChannelCreationNotAuthorized +// ); +// Hrmp::assert_storage_consistency_exhaustive(); +// }); +// } + +// #[test] +// fn open_system_channel_does_not_work_with_one_non_system_chain() { +// let para_a = 1.into(); +// let para_b = 2003.into(); + +// new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { +// // We need both A & B to be registered and live parachains. +// register_parachain(para_a); +// register_parachain(para_b); + +// run_to_block(5, Some(vec![4, 5])); +// assert_noop!( +// Hrmp::establish_system_channel(RuntimeOrigin::signed(1), para_a, para_b), +// Error::::ChannelCreationNotAuthorized +// ); +// Hrmp::assert_storage_consistency_exhaustive(); +// }); +// } diff --git a/polkadot/runtime/parachains/src/mock.rs b/polkadot/runtime/parachains/src/mock.rs index cc13feb698dc..b225c893314a 100644 --- a/polkadot/runtime/parachains/src/mock.rs +++ b/polkadot/runtime/parachains/src/mock.rs @@ -248,7 +248,7 @@ impl crate::dmp::Config for Test {} parameter_types! { pub const FirstMessageFactorPercent: u64 = 100; - pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (51200, 500); + pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (1, 4); } impl crate::hrmp::Config for Test { From debdef65d519b8b86aba9231795d416737f18a6c Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 27 Mar 2024 20:59:11 +1300 Subject: [PATCH 14/23] fix --- polkadot/runtime/parachains/src/mock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/runtime/parachains/src/mock.rs b/polkadot/runtime/parachains/src/mock.rs index b225c893314a..9ae73b87b58b 100644 --- a/polkadot/runtime/parachains/src/mock.rs +++ b/polkadot/runtime/parachains/src/mock.rs @@ -248,7 +248,7 @@ impl crate::dmp::Config for Test {} parameter_types! { pub const FirstMessageFactorPercent: u64 = 100; - pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (1, 4); + pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (4, 1); } impl crate::hrmp::Config for Test { From 2439a77b57c9da4267ef913fa744fea0b4e48e84 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 27 Mar 2024 21:27:25 +1300 Subject: [PATCH 15/23] tests --- polkadot/runtime/parachains/src/hrmp/tests.rs | 140 +++++++++--------- 1 file changed, 69 insertions(+), 71 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp/tests.rs b/polkadot/runtime/parachains/src/hrmp/tests.rs index 9e0565b5b0de..a20fafb9029c 100644 --- a/polkadot/runtime/parachains/src/hrmp/tests.rs +++ b/polkadot/runtime/parachains/src/hrmp/tests.rs @@ -24,7 +24,7 @@ use crate::mock::{ Configuration, Hrmp, MockGenesisConfig, Paras, ParasShared, RuntimeEvent as MockEvent, RuntimeOrigin, System, Test, }; -use frame_support::{assert_noop, assert_ok}; +use frame_support::{assert_noop, assert_ok, error::BadOrigin}; use primitives::BlockNumber; use std::collections::BTreeMap; @@ -933,73 +933,71 @@ fn watermark_maxed_out_at_relay_parent() { }); } -// #[test] -// fn open_system_channel_works() { -// let para_a = 1.into(); -// let para_b = 3.into(); - -// new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { -// // We need both A & B to be registered and live parachains. -// register_parachain(para_a); -// register_parachain(para_b); - -// run_to_block(5, Some(vec![4, 5])); -// Hrmp::establish_system_channel(RuntimeOrigin::signed(1), para_a, para_b).unwrap(); -// Hrmp::assert_storage_consistency_exhaustive(); -// assert!(System::events().iter().any(|record| record.event == -// MockEvent::Hrmp(Event::HrmpSystemChannelOpened { -// sender: para_a, -// recipient: para_b, -// proposed_max_capacity: 2, -// proposed_max_message_size: 8 -// }))); - -// // Advance to a block 6, but without session change. That means that the channel has -// // not been created yet. -// run_to_block(6, None); -// assert!(!channel_exists(para_a, para_b)); -// Hrmp::assert_storage_consistency_exhaustive(); - -// // Now let the session change happen and thus open the channel. -// run_to_block(8, Some(vec![8])); -// assert!(channel_exists(para_a, para_b)); -// }); -// } - -// #[test] -// fn open_system_channel_does_not_work_for_non_system_chains() { -// let para_a = 2001.into(); -// let para_b = 2003.into(); - -// new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { -// // We need both A & B to be registered and live parachains. -// register_parachain(para_a); -// register_parachain(para_b); - -// run_to_block(5, Some(vec![4, 5])); -// assert_noop!( -// Hrmp::establish_system_channel(RuntimeOrigin::signed(1), para_a, para_b), -// Error::::ChannelCreationNotAuthorized -// ); -// Hrmp::assert_storage_consistency_exhaustive(); -// }); -// } - -// #[test] -// fn open_system_channel_does_not_work_with_one_non_system_chain() { -// let para_a = 1.into(); -// let para_b = 2003.into(); - -// new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { -// // We need both A & B to be registered and live parachains. -// register_parachain(para_a); -// register_parachain(para_b); - -// run_to_block(5, Some(vec![4, 5])); -// assert_noop!( -// Hrmp::establish_system_channel(RuntimeOrigin::signed(1), para_a, para_b), -// Error::::ChannelCreationNotAuthorized -// ); -// Hrmp::assert_storage_consistency_exhaustive(); -// }); -// } +#[test] +fn establish_channel_with_system_works() { + let para_a = 2000.into(); + let para_a_origin: crate::Origin = 2000.into(); + let para_b = 3.into(); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + // We need both A & B to be registered and live parachains. + register_parachain(para_a); + register_parachain(para_b); + + run_to_block(5, Some(vec![4, 5])); + Hrmp::establish_channel_with_system(para_a_origin.into(), para_b).unwrap(); + Hrmp::assert_storage_consistency_exhaustive(); + assert!(System::events().iter().any(|record| record.event == + MockEvent::Hrmp(Event::HrmpSystemChannelOpened { + sender: para_a, + recipient: para_b, + proposed_max_capacity: 1, + proposed_max_message_size: 4 + }))); + + assert!(System::events().iter().any(|record| record.event == + MockEvent::Hrmp(Event::HrmpSystemChannelOpened { + sender: para_b, + recipient: para_a, + proposed_max_capacity: 1, + proposed_max_message_size: 4 + }))); + + // Advance to a block 6, but without session change. That means that the channel has + // not been created yet. + run_to_block(6, None); + assert!(!channel_exists(para_a, para_b)); + assert!(!channel_exists(para_b, para_a)); + Hrmp::assert_storage_consistency_exhaustive(); + + // Now let the session change happen and thus open the channel. + run_to_block(8, Some(vec![8])); + assert!(channel_exists(para_a, para_b)); + assert!(channel_exists(para_b, para_a)); + Hrmp::assert_storage_consistency_exhaustive(); + }); +} + +#[test] +fn establish_channel_with_system_with_invalid_args() { + let para_a = 2001.into(); + let para_a_origin: crate::Origin = 2000.into(); + let para_b = 2003.into(); + + new_test_ext(GenesisConfigBuilder::default().build()).execute_with(|| { + // We need both A & B to be registered and live parachains. + register_parachain(para_a); + register_parachain(para_b); + + run_to_block(5, Some(vec![4, 5])); + assert_noop!( + Hrmp::establish_channel_with_system(RuntimeOrigin::signed(1), para_b), + BadOrigin + ); + assert_noop!( + Hrmp::establish_channel_with_system(para_a_origin.into(), para_b), + Error::::ChannelCreationNotAuthorized + ); + Hrmp::assert_storage_consistency_exhaustive(); + }); +} From f68454ab57282843f5ec00a2de9f78d06556d6c3 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 27 Mar 2024 21:37:09 +1300 Subject: [PATCH 16/23] prdoc --- prdoc/pr_3721.prdoc | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 prdoc/pr_3721.prdoc diff --git a/prdoc/pr_3721.prdoc b/prdoc/pr_3721.prdoc new file mode 100644 index 000000000000..42444beb7bc1 --- /dev/null +++ b/prdoc/pr_3721.prdoc @@ -0,0 +1,12 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: New call `hrmp.establish_channel_with_system` to allow parachains to establish a channel with the system parachain + +doc: + - audience: Runtime Dev + description: | + This PR adds a new call `hrmp.establish_channel_with_system` that allows a parachain origin to open a bidirectional channel with a system parachain. + +crates: + - name: polkadot-runtime-parachains From 6807729e68b523a69f2acfabed316d78324ca2ed Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Thu, 28 Mar 2024 14:31:03 +1300 Subject: [PATCH 17/23] fix westend bench --- polkadot/runtime/westend/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 7184f72fefc1..fb15ec2f7bcb 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1203,7 +1203,7 @@ impl pallet_message_queue::Config for Runtime { impl parachains_dmp::Config for Runtime {} parameter_types! { - pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (51200, 500); + pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (4096, 4); } impl parachains_hrmp::Config for Runtime { From 48bbe0877a3585bba7f0f242b9fef0a02075fd23 Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Wed, 3 Apr 2024 14:00:40 +1300 Subject: [PATCH 18/23] Update prdoc/pr_3721.prdoc Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> --- prdoc/pr_3721.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_3721.prdoc b/prdoc/pr_3721.prdoc index 42444beb7bc1..771f316a2b35 100644 --- a/prdoc/pr_3721.prdoc +++ b/prdoc/pr_3721.prdoc @@ -1,7 +1,7 @@ # Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 # See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json -title: New call `hrmp.establish_channel_with_system` to allow parachains to establish a channel with the system parachain +title: New call `hrmp.establish_channel_with_system` to allow parachains to establish a channel with a system parachain doc: - audience: Runtime Dev From 2ccb92692e88b66aaf84b7ba37c507f6df17952e Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 3 Apr 2024 15:25:02 +1300 Subject: [PATCH 19/23] test --- polkadot/runtime/parachains/src/hrmp.rs | 2 +- .../parachains/src/hrmp/benchmarking.rs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/polkadot/runtime/parachains/src/hrmp.rs b/polkadot/runtime/parachains/src/hrmp.rs index 39dc73a3a8de..2ed86b968b2e 100644 --- a/polkadot/runtime/parachains/src/hrmp.rs +++ b/polkadot/runtime/parachains/src/hrmp.rs @@ -305,7 +305,7 @@ pub mod pallet { proposed_max_capacity: u32, proposed_max_message_size: u32, }, - /// An HRMP channel was opened between two system chains. + /// An HRMP channel was opened with a system chain. HrmpSystemChannelOpened { sender: ParaId, recipient: ParaId, diff --git a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs index 0aef25ae6205..857f95d83381 100644 --- a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs +++ b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs @@ -50,6 +50,15 @@ fn assert_last_event(generic_event: ::RuntimeEvent) { assert_eq!(event, &system_event); } +fn assert_has_event(generic_event: ::RuntimeEvent) { + let events = frame_system::Pallet::::events(); + let system_event: ::RuntimeEvent = generic_event.into(); + + println!("events: {:?}", events); + + assert!(events.iter().any(|record| record.event == system_event)); +} + /// Enumerates the phase in the setup process of a channel between two parachains. enum ParachainSetupStep { /// A channel open has been requested @@ -526,6 +535,29 @@ mod benchmarks { #[extrinsic_call] _(sender_origin, recipient_id); + + let (max_message_size, max_capacity) = + T::DefaultChannelSizeAndCapacityWithSystem::get(); + + assert_has_event::( + Event::::HrmpSystemChannelOpened { + sender: sender_id.into(), + recipient: recipient_id, + proposed_max_capacity: max_capacity, + proposed_max_message_size: max_message_size, + } + .into(), + ); + + assert_has_event::( + Event::::HrmpSystemChannelOpened { + sender: recipient_id, + recipient: sender_id.into(), + proposed_max_capacity: max_capacity, + proposed_max_message_size: max_message_size, + } + .into(), + ); } impl_benchmark_test_suite!( From 212179cb990d3155463324ca59e8ed5d8ed82fc1 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 3 Apr 2024 15:30:28 +1300 Subject: [PATCH 20/23] fmt --- .../runtime/parachains/src/hrmp/benchmarking.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs index 857f95d83381..5af8e5ae9b77 100644 --- a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs +++ b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs @@ -110,24 +110,24 @@ where )); if matches!(until, ParachainSetupStep::Requested) { - return output + return output; } assert_ok!(Hrmp::::hrmp_accept_open_channel(recipient_origin.into(), sender)); if matches!(until, ParachainSetupStep::Accepted) { - return output + return output; } Hrmp::::process_hrmp_open_channel_requests(&Configuration::::config()); if matches!(until, ParachainSetupStep::Established) { - return output + return output; } let channel_id = HrmpChannelId { sender, recipient }; assert_ok!(Hrmp::::hrmp_close_channel(sender_origin.clone().into(), channel_id)); if matches!(until, ParachainSetupStep::CloseRequested) { // NOTE: this is just for expressiveness, otherwise the if-statement is 100% useless. - return output + return output; } output @@ -536,13 +536,12 @@ mod benchmarks { #[extrinsic_call] _(sender_origin, recipient_id); - let (max_message_size, max_capacity) = - T::DefaultChannelSizeAndCapacityWithSystem::get(); + let (max_message_size, max_capacity) = T::DefaultChannelSizeAndCapacityWithSystem::get(); assert_has_event::( Event::::HrmpSystemChannelOpened { sender: sender_id.into(), - recipient: recipient_id, + recipient: recipient_id, proposed_max_capacity: max_capacity, proposed_max_message_size: max_message_size, } From 35db15ab2a8f50f9c0f73b0916a3db705099d701 Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Wed, 3 Apr 2024 22:00:52 +1300 Subject: [PATCH 21/23] Update prdoc/pr_3721.prdoc Co-authored-by: Adrian Catangiu --- prdoc/pr_3721.prdoc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/prdoc/pr_3721.prdoc b/prdoc/pr_3721.prdoc index 771f316a2b35..be36103c4742 100644 --- a/prdoc/pr_3721.prdoc +++ b/prdoc/pr_3721.prdoc @@ -9,4 +9,5 @@ doc: This PR adds a new call `hrmp.establish_channel_with_system` that allows a parachain origin to open a bidirectional channel with a system parachain. crates: - - name: polkadot-runtime-parachains +- name: polkadot-runtime-parachains + bump: minor From 59f8cc24ff5cd753d11a0d159a9d000fd0320619 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Fri, 12 Apr 2024 17:21:48 +1200 Subject: [PATCH 22/23] remove debug print --- polkadot/runtime/parachains/src/hrmp/benchmarking.rs | 2 -- substrate/frame/support/src/traits/randomness.rs | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs index 820be9b01a50..3b32a8b95195 100644 --- a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs +++ b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs @@ -54,8 +54,6 @@ fn assert_has_event(generic_event: ::RuntimeEvent) { let events = frame_system::Pallet::::events(); let system_event: ::RuntimeEvent = generic_event.into(); - println!("events: {:?}", events); - assert!(events.iter().any(|record| record.event == system_event)); } diff --git a/substrate/frame/support/src/traits/randomness.rs b/substrate/frame/support/src/traits/randomness.rs index 3666a486465f..178c0f70ad1e 100644 --- a/substrate/frame/support/src/traits/randomness.rs +++ b/substrate/frame/support/src/traits/randomness.rs @@ -40,7 +40,7 @@ pub trait Randomness { /// Get the basic random seed. /// /// In general you won't want to use this, but rather `Self::random` which allows - /// you to give a subject for the random result and whose value will be + /// you to give a subject for the random result and whose value will eb /// independently low-influence random from any other such seeds. /// /// NOTE: The returned seed should only be used to distinguish commitments made before From c92d726e965bb2dab4972470765b1f5da52403f0 Mon Sep 17 00:00:00 2001 From: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Date: Fri, 12 Apr 2024 07:39:36 +0200 Subject: [PATCH 23/23] Apply suggestions from code review --- polkadot/runtime/parachains/src/hrmp/benchmarking.rs | 8 ++++---- substrate/frame/support/src/traits/randomness.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs index 3b32a8b95195..8bf444e647e2 100644 --- a/polkadot/runtime/parachains/src/hrmp/benchmarking.rs +++ b/polkadot/runtime/parachains/src/hrmp/benchmarking.rs @@ -110,24 +110,24 @@ where )); if matches!(until, ParachainSetupStep::Requested) { - return output; + return output } assert_ok!(Hrmp::::hrmp_accept_open_channel(recipient_origin.into(), sender)); if matches!(until, ParachainSetupStep::Accepted) { - return output; + return output } Hrmp::::process_hrmp_open_channel_requests(&configuration::ActiveConfig::::get()); if matches!(until, ParachainSetupStep::Established) { - return output; + return output } let channel_id = HrmpChannelId { sender, recipient }; assert_ok!(Hrmp::::hrmp_close_channel(sender_origin.clone().into(), channel_id)); if matches!(until, ParachainSetupStep::CloseRequested) { // NOTE: this is just for expressiveness, otherwise the if-statement is 100% useless. - return output; + return output } output diff --git a/substrate/frame/support/src/traits/randomness.rs b/substrate/frame/support/src/traits/randomness.rs index 178c0f70ad1e..3666a486465f 100644 --- a/substrate/frame/support/src/traits/randomness.rs +++ b/substrate/frame/support/src/traits/randomness.rs @@ -40,7 +40,7 @@ pub trait Randomness { /// Get the basic random seed. /// /// In general you won't want to use this, but rather `Self::random` which allows - /// you to give a subject for the random result and whose value will eb + /// you to give a subject for the random result and whose value will be /// independently low-influence random from any other such seeds. /// /// NOTE: The returned seed should only be used to distinguish commitments made before