Skip to content

Commit ce3129b

Browse files
seemantaggarwalggwpez
authored andcommitted
Update Scheduler to have a configurable block provider paritytech#7434 (paritytech#7441)
Follow up from paritytech#6362 (comment) The goal of this PR is to have the scheduler pallet work on a parachain which does not produce blocks on a regular schedule, thus can use the relay chain as a block provider. Because blocks are not produced regularly, we cannot make the assumption that block number increases monotonically, and thus have new logic to handle multiple spend periods passing between blocks. Requirement: instead of using the hard coded system block number. We add an associated type BlockNumberProvider --------- Signed-off-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Oliver Tale-Yazdi <[email protected]>
1 parent 0342175 commit ce3129b

File tree

12 files changed

+95
-34
lines changed

12 files changed

+95
-34
lines changed

cumulus/parachains/runtimes/collectives/collectives-westend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ impl pallet_scheduler::Config for Runtime {
628628
type WeightInfo = weights::pallet_scheduler::WeightInfo<Runtime>;
629629
type OriginPrivilegeCmp = EqualOrGreatestRootCmp;
630630
type Preimages = Preimage;
631+
type BlockNumberProvider = frame_system::Pallet<Runtime>;
631632
}
632633

633634
parameter_types! {

polkadot/runtime/rococo/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ impl pallet_scheduler::Config for Runtime {
344344
type WeightInfo = weights::pallet_scheduler::WeightInfo<Runtime>;
345345
type OriginPrivilegeCmp = OriginPrivilegeCmp;
346346
type Preimages = Preimage;
347+
type BlockNumberProvider = frame_system::Pallet<Runtime>;
347348
}
348349

349350
parameter_types! {

polkadot/runtime/westend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl pallet_scheduler::Config for Runtime {
250250
type WeightInfo = weights::pallet_scheduler::WeightInfo<Runtime>;
251251
type OriginPrivilegeCmp = frame_support::traits::EqualPrivilegeOnly;
252252
type Preimages = Preimage;
253+
type BlockNumberProvider = frame_system::Pallet<Runtime>;
253254
}
254255

255256
parameter_types! {

prdoc/pr_7441.prdoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
title: 'Update Scheduler to have a configurable block number provider'
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
This PR makes `pallet_scheduler` configurable by introducing `BlockNumberProvider` in
6+
`pallet_scheduler::Config`. Instead of relying solely on
7+
`frame_system::Pallet::<T>::block_number()`, the scheduler can now use any block number source,
8+
including external providers like the relay chain.
9+
10+
Parachains can continue using `frame_system::Pallet::<Runtime>` without issue. To retain the
11+
previous behavior, set `BlockNumberProvider` to `frame_system::Pallet::<Runtime>`.
12+
13+
crates:
14+
- name: collectives-westend-runtime
15+
bump: patch
16+
- name: rococo-runtime
17+
bump: patch
18+
- name: westend-runtime
19+
bump: patch
20+
- name: pallet-democracy
21+
bump: patch
22+
- name: pallet-referenda
23+
bump: patch
24+
- name: pallet-scheduler
25+
bump: major

substrate/bin/node/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ impl pallet_scheduler::Config for Runtime {
504504
type WeightInfo = pallet_scheduler::weights::SubstrateWeight<Runtime>;
505505
type OriginPrivilegeCmp = EqualPrivilegeOnly;
506506
type Preimages = Preimage;
507+
type BlockNumberProvider = frame_system::Pallet<Runtime>;
507508
}
508509

509510
impl pallet_glutton::Config for Runtime {

substrate/frame/democracy/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl pallet_scheduler::Config for Test {
106106
type WeightInfo = ();
107107
type OriginPrivilegeCmp = EqualPrivilegeOnly;
108108
type Preimages = ();
109+
type BlockNumberProvider = frame_system::Pallet<Test>;
109110
}
110111

111112
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]

substrate/frame/referenda/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl pallet_scheduler::Config for Test {
8181
type WeightInfo = ();
8282
type OriginPrivilegeCmp = EqualPrivilegeOnly;
8383
type Preimages = Preimage;
84+
type BlockNumberProvider = frame_system::Pallet<Test>;
8485
}
8586
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
8687
impl pallet_balances::Config for Test {

substrate/frame/scheduler/src/benchmarking.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
4949
/// - `None`: aborted (hash without preimage)
5050
/// - `Some(true)`: hash resolves into call if possible, plain call otherwise
5151
/// - `Some(false)`: plain call
52-
fn fill_schedule<T: Config>(
53-
when: frame_system::pallet_prelude::BlockNumberFor<T>,
54-
n: u32,
55-
) -> Result<(), &'static str> {
52+
fn fill_schedule<T: Config>(when: BlockNumberFor<T>, n: u32) -> Result<(), &'static str> {
5653
let t = DispatchTime::At(when);
5754
let origin: <T as Config>::PalletsOrigin = frame_system::RawOrigin::Root.into();
5855
for i in 0..n {

substrate/frame/scheduler/src/lib.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,11 @@ use frame_support::{
100100
},
101101
weights::{Weight, WeightMeter},
102102
};
103-
use frame_system::{
104-
pallet_prelude::BlockNumberFor,
105-
{self as system},
106-
};
103+
use frame_system::{self as system};
107104
use scale_info::TypeInfo;
108105
use sp_io::hashing::blake2_256;
109106
use sp_runtime::{
110-
traits::{BadOrigin, Dispatchable, One, Saturating, Zero},
107+
traits::{BadOrigin, BlockNumberProvider, Dispatchable, One, Saturating, Zero},
111108
BoundedVec, DispatchError, RuntimeDebug,
112109
};
113110

@@ -125,6 +122,9 @@ pub type CallOrHashOf<T> =
125122
pub type BoundedCallOf<T> =
126123
Bounded<<T as Config>::RuntimeCall, <T as frame_system::Config>::Hashing>;
127124

125+
pub type BlockNumberFor<T> =
126+
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
127+
128128
/// The configuration of the retry mechanism for a given task along with its current state.
129129
#[derive(Clone, Copy, RuntimeDebug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
130130
pub struct RetryConfig<Period> {
@@ -230,7 +230,7 @@ impl<T: WeightInfo> MarginalWeightInfo for T {}
230230
pub mod pallet {
231231
use super::*;
232232
use frame_support::{dispatch::PostDispatchInfo, pallet_prelude::*};
233-
use frame_system::pallet_prelude::*;
233+
use frame_system::pallet_prelude::{BlockNumberFor as SystemBlockNumberFor, OriginFor};
234234

235235
/// The in-code storage version.
236236
const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);
@@ -292,6 +292,35 @@ pub mod pallet {
292292

293293
/// The preimage provider with which we look up call hashes to get the call.
294294
type Preimages: QueryPreimage<H = Self::Hashing> + StorePreimage;
295+
296+
/// Query the current block number.
297+
///
298+
/// Must return monotonically increasing values when called from consecutive blocks. It is
299+
/// generally expected that the values also do not differ "too much" between consecutive
300+
/// blocks. A future addition to this pallet will allow bigger difference between
301+
/// consecutive blocks to make it possible to be utilized by parachains with *Agile
302+
/// Coretime*. *Agile Coretime* parachains are currently not supported and must continue to
303+
/// use their local block number provider.
304+
///
305+
/// Can be configured to return either:
306+
/// - the local block number of the runtime via `frame_system::Pallet`
307+
/// - a remote block number, eg from the relay chain through `RelaychainDataProvider`
308+
/// - an arbitrary value through a custom implementation of the trait
309+
///
310+
/// Suggested values:
311+
/// - Solo- and Relay-chains should use `frame_system::Pallet`. There are no concerns with
312+
/// this configuration.
313+
/// - Parachains should also use `frame_system::Pallet` for the time being. The scheduler
314+
/// pallet is not yet ready for the case that big numbers of blocks are skipped. In an
315+
/// *Agile Coretime* chain with relay chain number provider configured, it could otherwise
316+
/// happen that the scheduler will not be able to catch up to its agendas, since too many
317+
/// relay blocks are missing if the parachain only produces blocks rarely.
318+
///
319+
/// There is currently no migration provided to "hot-swap" block number providers and it is
320+
/// therefore highly advised to stay with the default (local) values. If you still want to
321+
/// swap block number providers on the fly, then please at least ensure that you do not run
322+
/// any pallet migration in the same runtime upgrade.
323+
type BlockNumberProvider: BlockNumberProvider;
295324
}
296325

297326
#[pallet::storage]
@@ -374,11 +403,12 @@ pub mod pallet {
374403
}
375404

376405
#[pallet::hooks]
377-
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
406+
impl<T: Config> Hooks<SystemBlockNumberFor<T>> for Pallet<T> {
378407
/// Execute the scheduled calls
379-
fn on_initialize(now: BlockNumberFor<T>) -> Weight {
408+
fn on_initialize(_now: SystemBlockNumberFor<T>) -> Weight {
409+
let now = T::BlockNumberProvider::current_block_number();
380410
let mut weight_counter = WeightMeter::with_limit(T::MaximumWeight::get());
381-
Self::service_agendas(&mut weight_counter, now, u32::max_value());
411+
Self::service_agendas(&mut weight_counter, now, u32::MAX);
382412
weight_counter.consumed()
383413
}
384414
}
@@ -889,8 +919,7 @@ impl<T: Config> Pallet<T> {
889919
fn resolve_time(
890920
when: DispatchTime<BlockNumberFor<T>>,
891921
) -> Result<BlockNumberFor<T>, DispatchError> {
892-
let now = frame_system::Pallet::<T>::block_number();
893-
922+
let now = T::BlockNumberProvider::current_block_number();
894923
let when = match when {
895924
DispatchTime::At(x) => x,
896925
// The current block has already completed it's scheduled tasks, so
@@ -1165,7 +1194,7 @@ impl<T: Config> Pallet<T> {
11651194
let mut count_down = max;
11661195
let service_agenda_base_weight = T::WeightInfo::service_agenda_base(max_items);
11671196
while count_down > 0 && when <= now && weight.can_consume(service_agenda_base_weight) {
1168-
if !Self::service_agenda(weight, &mut executed, now, when, u32::max_value()) {
1197+
if !Self::service_agenda(weight, &mut executed, now, when, u32::MAX) {
11691198
incomplete_since = incomplete_since.min(when);
11701199
}
11711200
when.saturating_inc();

substrate/frame/scheduler/src/migration.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
2020
use super::*;
2121
use frame_support::traits::OnRuntimeUpgrade;
22-
use frame_system::pallet_prelude::BlockNumberFor;
2322

2423
#[cfg(feature = "try-runtime")]
2524
use sp_runtime::TryRuntimeError;

0 commit comments

Comments
 (0)