Skip to content

Commit

Permalink
HRMP - set DefaultChannelSizeAndCapacityWithSystem with dynamic val…
Browse files Browse the repository at this point in the history
…ues according to the `ActiveConfig` (paritytech#4332)

## Summary
This PR enhances the capability to set
`DefaultChannelSizeAndCapacityWithSystem` for HRMP. Currently, all
testnets (Rococo, Westend) have a hard-coded value set as 'half of the
maximum' determined by the live `ActiveConfig`. While this approach
appears satisfactory, potential issues could arise if the live
`ActiveConfig` are adjusted below these hard-coded values, necessitating
a new runtime release with updated values. Additionally, hard-coded
values have consequences, such as Rococo's benchmarks not functioning:
https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/6082656.

## Solution
The proposed solution here is to utilize
`ActiveConfigHrmpChannelSizeAndCapacityRatio`, which reads the current
`ActiveConfig` and calculates `DefaultChannelSizeAndCapacityWithSystem`,
for example, "half of the maximum" based on live data. This way,
whenever `ActiveConfig` is modified,
`ActiveConfigHrmpChannelSizeAndCapacityRatio` automatically returns
adjusted values with the appropriate ratio. Thus, manual adjustments and
new runtime releases become unnecessary.


Relates to a comment/discussion:
https://github.com/paritytech/polkadot-sdk/pull/3721/files#r1541001420
Relates to a comment/discussion:
https://github.com/paritytech/polkadot-sdk/pull/3721/files#r1549291588

---------

Co-authored-by: command-bot <>
  • Loading branch information
bkontur authored and TarekkMA committed Aug 2, 2024
1 parent 709f8ef commit da592d3
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 414 deletions.
15 changes: 14 additions & 1 deletion polkadot/runtime/parachains/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use primitives::{
NodeFeatures, SessionIndex, LEGACY_MIN_BACKING_VOTES, MAX_CODE_SIZE, MAX_HEAD_DATA_SIZE,
MAX_POV_SIZE, ON_DEMAND_MAX_QUEUE_MAX_SIZE,
};
use sp_runtime::{traits::Zero, Perbill};
use sp_runtime::{traits::Zero, Perbill, Percent};
use sp_std::prelude::*;

#[cfg(test)]
Expand Down Expand Up @@ -1460,3 +1460,16 @@ impl<T: Config> Pallet<T> {
Ok(())
}
}

/// The implementation of `Get<(u32, u32)>` which reads `ActiveConfig` and returns `P` percent of
/// `hrmp_channel_max_message_size` / `hrmp_channel_max_capacity`.
pub struct ActiveConfigHrmpChannelSizeAndCapacityRatio<T, P>(sp_std::marker::PhantomData<(T, P)>);
impl<T: crate::hrmp::pallet::Config, P: Get<Percent>> Get<(u32, u32)>
for ActiveConfigHrmpChannelSizeAndCapacityRatio<T, P>
{
fn get() -> (u32, u32) {
let config = ActiveConfig::<T>::get();
let percent = P::get();
(percent * config.hrmp_channel_max_message_size, percent * config.hrmp_channel_max_capacity)
}
}
50 changes: 49 additions & 1 deletion polkadot/runtime/parachains/src/configuration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use super::*;
use crate::{
configuration,
mock::{new_test_ext, Configuration, ParasShared, RuntimeOrigin, Test},
mock::{new_test_ext, Configuration, MockGenesisConfig, ParasShared, RuntimeOrigin, Test},
};
use bitvec::{bitvec, prelude::Lsb0};
use frame_support::{assert_err, assert_noop, assert_ok};
Expand Down Expand Up @@ -547,3 +547,51 @@ fn verify_externally_accessible() {
);
});
}

#[test]
fn active_config_hrmp_channel_size_and_capacity_ratio_works() {
frame_support::parameter_types! {
pub Ratio100: Percent = Percent::from_percent(100);
pub Ratio50: Percent = Percent::from_percent(50);
}

let mut genesis: MockGenesisConfig = Default::default();
genesis.configuration.config.hrmp_channel_max_message_size = 1024;
genesis.configuration.config.hrmp_channel_max_capacity = 100;

new_test_ext(genesis).execute_with(|| {
let active_config = configuration::ActiveConfig::<Test>::get();
assert_eq!(active_config.hrmp_channel_max_message_size, 1024);
assert_eq!(active_config.hrmp_channel_max_capacity, 100);

assert_eq!(
ActiveConfigHrmpChannelSizeAndCapacityRatio::<Test, Ratio100>::get(),
(1024, 100)
);
assert_eq!(ActiveConfigHrmpChannelSizeAndCapacityRatio::<Test, Ratio50>::get(), (512, 50));

// change ActiveConfig
assert_ok!(Configuration::set_hrmp_channel_max_message_size(
RuntimeOrigin::root(),
active_config.hrmp_channel_max_message_size * 4
));
assert_ok!(Configuration::set_hrmp_channel_max_capacity(
RuntimeOrigin::root(),
active_config.hrmp_channel_max_capacity * 4
));
on_new_session(1);
on_new_session(2);
let active_config = configuration::ActiveConfig::<Test>::get();
assert_eq!(active_config.hrmp_channel_max_message_size, 4096);
assert_eq!(active_config.hrmp_channel_max_capacity, 400);

assert_eq!(
ActiveConfigHrmpChannelSizeAndCapacityRatio::<Test, Ratio100>::get(),
(4096, 400)
);
assert_eq!(
ActiveConfigHrmpChannelSizeAndCapacityRatio::<Test, Ratio50>::get(),
(2048, 200)
);
})
}
8 changes: 6 additions & 2 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use runtime_common::{
use runtime_parachains::{
assigner_coretime as parachains_assigner_coretime,
assigner_on_demand as parachains_assigner_on_demand, configuration as parachains_configuration,
configuration::ActiveConfigHrmpChannelSizeAndCapacityRatio,
coretime, disputes as parachains_disputes,
disputes::slashing as parachains_slashing,
dmp as parachains_dmp, hrmp as parachains_hrmp, inclusion as parachains_inclusion,
Expand Down Expand Up @@ -1035,15 +1036,18 @@ impl pallet_message_queue::Config for Runtime {
impl parachains_dmp::Config for Runtime {}

parameter_types! {
pub const DefaultChannelSizeAndCapacityWithSystem: (u32, u32) = (51200, 500);
pub const HrmpChannelSizeAndCapacityWithSystemRatio: Percent = Percent::from_percent(100);
}

impl parachains_hrmp::Config for Runtime {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeEvent = RuntimeEvent;
type ChannelManager = EnsureRoot<AccountId>;
type Currency = Balances;
type DefaultChannelSizeAndCapacityWithSystem = DefaultChannelSizeAndCapacityWithSystem;
type DefaultChannelSizeAndCapacityWithSystem = ActiveConfigHrmpChannelSizeAndCapacityRatio<
Runtime,
HrmpChannelSizeAndCapacityWithSystemRatio,
>;
type WeightInfo = weights::runtime_parachains_hrmp::WeightInfo<Runtime>;
}

Expand Down
Loading

0 comments on commit da592d3

Please sign in to comment.