Skip to content

Commit

Permalink
refactor(mainnet): scheduler & preimage config and tests into utility
Browse files Browse the repository at this point in the history
  • Loading branch information
al3mart committed Jan 20, 2025
1 parent 4e697c1 commit d68af12
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 23 deletions.
161 changes: 158 additions & 3 deletions runtime/mainnet/src/config/utility.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use monetary::deposit;
use frame_support::{pallet_prelude::ConstU32, traits::EqualPrivilegeOnly};

use crate::{
config::monetary, parameter_types, Balance, Balances, OriginCaller, Runtime, RuntimeCall,
RuntimeEvent,
config::{monetary::deposit, system::RuntimeBlockWeights},
parameter_types, AccountId, Balance, Balances, EnsureRoot, HoldConsideration,
LinearStoragePrice, OriginCaller, Perbill, Preimage, Runtime, RuntimeCall, RuntimeEvent,
RuntimeHoldReason, RuntimeOrigin, Weight,
};

parameter_types! {
Expand All @@ -23,6 +25,46 @@ impl pallet_multisig::Config for Runtime {
type WeightInfo = pallet_multisig::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub const PreimageHoldReason: RuntimeHoldReason = RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
pub const PreimageBaseDeposit: Balance = deposit(2, 64);
pub const PreimageByteDeposit: Balance = deposit(0, 1);
}

impl pallet_preimage::Config for Runtime {
type Consideration = HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_preimage::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(60) *
RuntimeBlockWeights::get().max_block;
}

impl pallet_scheduler::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type MaxScheduledPerBlock = ConstU32<512>;
#[cfg(not(feature = "runtime-benchmarks"))]
type MaxScheduledPerBlock = ConstU32<50>;
type MaximumWeight = MaximumSchedulerWeight;
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type PalletsOrigin = OriginCaller;
type Preimages = Preimage;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type ScheduleOrigin = EnsureRoot<AccountId>;
type WeightInfo = pallet_scheduler::weights::SubstrateWeight<Runtime>;
}

impl pallet_utility::Config for Runtime {
type PalletsOrigin = OriginCaller;
type RuntimeCall = RuntimeCall;
Expand Down Expand Up @@ -90,4 +132,117 @@ mod tests {
TypeId::of::<()>(),
);
}

#[test]
#[cfg(not(feature = "runtime-benchmarks"))]
fn scheduler_call_queue_per_block_is_limited() {
assert_eq!(
TypeId::of::<<Runtime as pallet_scheduler::Config>::MaxScheduledPerBlock>(),
TypeId::of::<ConstU32<50>>(),
);
}

#[test]
#[cfg(feature = "runtime-benchmarks")]
fn scheduler_call_queue_per_block_is_limited() {
assert_eq!(
TypeId::of::<<Runtime as pallet_scheduler::Config>::MaxScheduledPerBlock>(),
TypeId::of::<ConstU32<512>>(),
);
}

#[test]
fn scheduler_has_max_weight_per_dispatchable() {
assert_eq!(
TypeId::of::<<Runtime as pallet_scheduler::Config>::MaximumWeight>(),
TypeId::of::<MaximumSchedulerWeight>(),
);

assert_eq!(
<<Runtime as pallet_scheduler::Config>::MaximumWeight as Get<Weight>>::get(),
Perbill::from_percent(60) * RuntimeBlockWeights::get().max_block,
);
}

#[test]
fn scheduler_privilege_cmp_is_equal_privilege_only() {
// EqualPrvilegeOnly can be used while ScheduleOrigin is reserve to Root.
assert_eq!(
TypeId::of::<<Runtime as pallet_scheduler::Config>::OriginPrivilegeCmp>(),
TypeId::of::<EqualPrivilegeOnly>(),
);
}

#[test]
fn only_root_can_schedule() {
assert_eq!(
TypeId::of::<<Runtime as pallet_scheduler::Config>::ScheduleOrigin>(),
TypeId::of::<EnsureRoot<AccountId>>(),
);
}

#[test]
fn scheduler_does_not_use_default_weights() {
assert_ne!(
TypeId::of::<<Runtime as pallet_scheduler::Config>::WeightInfo>(),
TypeId::of::<()>()
);
}

#[test]
fn scheduler_uses_preimage_to_look_up_calls() {
assert_eq!(
TypeId::of::<<Runtime as pallet_scheduler::Config>::Preimages>(),
TypeId::of::<Preimage>(),
);
}

#[test]
fn preimage_uses_balances_as_currency() {
assert_eq!(
TypeId::of::<<Runtime as pallet_preimage::Config>::Currency>(),
TypeId::of::<Balances>(),
);
}

#[test]
fn preimage_manage_origin_is_root() {
assert_eq!(
TypeId::of::<<Runtime as pallet_preimage::Config>::ManagerOrigin>(),
TypeId::of::<EnsureRoot<AccountId>>(),
);
}

#[test]
fn preimage_does_not_use_default_weights() {
assert_ne!(
TypeId::of::<<Runtime as pallet_preimage::Config>::WeightInfo>(),
TypeId::of::<()>()
);
}

#[test]
fn preimage_hold_reason_uses_linear_price() {
assert_eq!(
TypeId::of::<<Runtime as pallet_preimage::Config>::Consideration>(),
TypeId::of::<
HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>,
>()
);
}

#[test]
fn preimage_base_deposit() {
assert_eq!(PreimageBaseDeposit::get(), deposit(2, 64));
}

#[test]
fn preimage_byte_deposit() {
assert_eq!(PreimageByteDeposit::get(), deposit(0, 1));
}
}
21 changes: 1 addition & 20 deletions runtime/mainnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern crate alloc;

use alloc::{borrow::Cow, vec::Vec};

use config::{monetary::deposit, xcm::XcmOriginToTransactDispatchOrigin};
use config::xcm::XcmOriginToTransactDispatchOrigin;
use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
use cumulus_primitives_core::{AggregateMessageOrigin, ParaId};
use frame_support::{
Expand Down Expand Up @@ -243,25 +243,6 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime {
type XcmpQueue = TransformOrigin<MessageQueue, AggregateMessageOrigin, ParaId, ParaIdToSibling>;
}

parameter_types! {
pub const PreimageHoldReason: RuntimeHoldReason = RuntimeHoldReason::Preimage(pallet_preimage::HoldReason::Preimage);
pub const PreimageBaseDeposit: Balance = deposit(2, 64);
pub const PreimageByteDeposit: Balance = deposit(0, 1);
}

impl pallet_preimage::Config for Runtime {
type Consideration = HoldConsideration<
AccountId,
Balances,
PreimageHoldReason,
LinearStoragePrice<PreimageBaseDeposit, PreimageByteDeposit, Balance>,
>;
type Currency = Balances;
type ManagerOrigin = EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_preimage::weights::SubstrateWeight<Runtime>;
}

#[frame_support::runtime]
mod runtime {
// Create the runtime by composing the FRAME pallets that were previously configured.
Expand Down

0 comments on commit d68af12

Please sign in to comment.