Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
212712d
initial commit
Feb 3, 2025
9314582
effort to add block number provider
Feb 3, 2025
fbb1eb7
fixing imports in the wrong pallet
Feb 4, 2025
8ea5046
fixing imports
Feb 4, 2025
a2c3c97
fixing import of blocknumberprovider under the correct pallet
Feb 4, 2025
88cf158
importing blocknumberprovider in the pallet
Feb 4, 2025
1ea6c26
adding blocknumber to mock
Feb 4, 2025
b29cf54
Use pallet's BlockNumberFor
ggwpez Feb 5, 2025
0670883
Merge branch 'master' into seemant/scheduler-bnp
seemantaggarwal Feb 5, 2025
ce52df4
cargo fmt + prdoc added
Feb 5, 2025
16585b9
cargo fmt
Feb 5, 2025
df2b959
Merge branch 'master' into seemant/scheduler-bnp
seemantaggarwal Feb 5, 2025
279ed58
adding testing
Feb 5, 2025
55b0acd
trial and error
Feb 6, 2025
d20b051
reverting 55b0acd158d1db765dd76598da716cdd57e62ec0
Feb 6, 2025
39a6e6c
trial and error more
Feb 8, 2025
0feaccc
reverting 39a6e6c24a1a2a799769cbf850385e774c02e044
Feb 8, 2025
62a4592
using block number in benchmarking
Feb 9, 2025
2fdf53d
cargo fmt
Feb 9, 2025
ae9e068
updating prdoc
Feb 10, 2025
c96dd35
adding a suggested block number value as a document
Feb 10, 2025
03fc0da
cargo fmt
Feb 10, 2025
9041d6c
Merge branch 'master' into seemant/scheduler-bnp
seemantaggarwal Feb 10, 2025
d7a8db1
addressing comment
Feb 10, 2025
b745976
improving the rust doc, addressing comment
Feb 11, 2025
b887564
improving the pr doc
Feb 11, 2025
2ba273a
fixing the soon to be deprecated max_value()
Feb 11, 2025
4c54432
updating tests
Feb 11, 2025
39ded63
fixup
ggwpez Feb 12, 2025
e09547a
docs
ggwpez Feb 12, 2025
17b7aec
Merge branch 'master' into seemant/scheduler-bnp
ggwpez Feb 12, 2025
441f5d3
prdoc
ggwpez Feb 12, 2025
21683a4
patch
ggwpez Feb 12, 2025
4069c9e
more docs
ggwpez Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl frame_system::Config for Runtime {
type SS58Prefix = SS58Prefix;
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type BlockNumberProvider = frame_system::Pallet<Runtime>;
}

impl cumulus_pallet_weight_reclaim::Config for Runtime {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ impl pallet_scheduler::Config for Runtime {
type WeightInfo = weights::pallet_scheduler::WeightInfo<Runtime>;
type OriginPrivilegeCmp = OriginPrivilegeCmp;
type Preimages = Preimage;
type BlockNumberProvider = frame_system::Pallet<Runtime>;

}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ impl pallet_scheduler::Config for Runtime {
type WeightInfo = weights::pallet_scheduler::WeightInfo<Runtime>;
type OriginPrivilegeCmp = frame_support::traits::EqualPrivilegeOnly;
type Preimages = Preimage;
type BlockNumberProvider = frame_system::Pallet<Runtime>;

}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ impl pallet_scheduler::Config for Runtime {
type WeightInfo = pallet_scheduler::weights::SubstrateWeight<Runtime>;
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type Preimages = Preimage;
type BlockNumberProvider = frame_system::Pallet<Runtime>;

}

impl pallet_glutton::Config for Runtime {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl pallet_scheduler::Config for Test {
type WeightInfo = ();
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type Preimages = ();
type BlockNumberProvider = frame_system::Pallet<Test>;
}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/referenda/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl pallet_scheduler::Config for Test {
type WeightInfo = ();
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type Preimages = Preimage;
type BlockNumberProvider = frame_system::Pallet<Test>;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
Expand Down
19 changes: 12 additions & 7 deletions substrate/frame/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ use frame_system::{
use scale_info::TypeInfo;
use sp_io::hashing::blake2_256;
use sp_runtime::{
traits::{BadOrigin, Dispatchable, One, Saturating, Zero},
traits::{BadOrigin, Dispatchable, One, Saturating, Zero, BlockNumberProvider},
BoundedVec, DispatchError, RuntimeDebug,
};

Expand All @@ -125,6 +125,9 @@ pub type CallOrHashOf<T> =
pub type BoundedCallOf<T> =
Bounded<<T as Config>::RuntimeCall, <T as frame_system::Config>::Hashing>;

pub type BlockNumberFor<T> =
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;

/// The configuration of the retry mechanism for a given task along with its current state.
#[derive(Clone, Copy, RuntimeDebug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct RetryConfig<Period> {
Expand Down Expand Up @@ -230,7 +233,7 @@ impl<T: WeightInfo> MarginalWeightInfo for T {}
pub mod pallet {
use super::*;
use frame_support::{dispatch::PostDispatchInfo, pallet_prelude::*};
use frame_system::pallet_prelude::*;
use frame_system::pallet_prelude::{BlockNumberFor as SystemBlockNumberFor, OriginFor};

/// The in-code storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);
Expand Down Expand Up @@ -297,6 +300,8 @@ pub mod pallet {
#[pallet::storage]
pub type IncompleteSince<T: Config> = StorageValue<_, BlockNumberFor<T>>;

type BlockNumberProvider: BlockNumberProvider;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need a doc comment that explains this type, and warns that it must not be too much out of sync with the local block number. More precisely between every block the increment of this number should be reasonably small otherwise performance degrades will degrade a lot as it reads one storage per block number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will add it towards the end of the pr for sure, I will look into it. At this moment, I am not entirely sure of the value that needs to be advised, i will get more insights into it, thanks for the reminder :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can suggest people to use the system block number or the relay chain block number (this second option, only if they expect their parachain to execute very regularly).


/// Items to be executed, indexed by the block number that they should be executed on.
#[pallet::storage]
pub type Agenda<T: Config> = StorageMap<
Expand Down Expand Up @@ -374,9 +379,10 @@ pub mod pallet {
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
impl<T: Config> Hooks<SystemBlockNumberFor<T>> for Pallet<T> {
/// Execute the scheduled calls
fn on_initialize(now: BlockNumberFor<T>) -> Weight {
let now = T::BlockNumberProvider::current_block_number();
let mut weight_counter = WeightMeter::with_limit(T::MaximumWeight::get());
Self::service_agendas(&mut weight_counter, now, u32::max_value());
weight_counter.consumed()
Expand Down Expand Up @@ -889,8 +895,7 @@ impl<T: Config> Pallet<T> {
fn resolve_time(
when: DispatchTime<BlockNumberFor<T>>,
) -> Result<BlockNumberFor<T>, DispatchError> {
let now = frame_system::Pallet::<T>::block_number();

let now = T::BlockNumberProvider::current_block_number();
let when = match when {
DispatchTime::At(x) => x,
// The current block has already completed it's scheduled tasks, so
Expand Down Expand Up @@ -926,11 +931,11 @@ impl<T: Config> Pallet<T> {
let mut agenda = Agenda::<T>::get(when);
let index = if (agenda.len() as u32) < T::MaxScheduledPerBlock::get() {
// will always succeed due to the above check.
let _ = agenda.try_push(Some(what));
let _ = agenda.try_push(Some(what.clone()));
agenda.len() as u32 - 1
} else {
if let Some(hole_index) = agenda.iter().position(|i| i.is_none()) {
agenda[hole_index] = Some(what);
agenda[hole_index] = Some(what.clone());
hole_index as u32
} else {
return Err((DispatchError::Exhausted, what))
Expand Down
Loading