Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pallet-nis to support Block Number Provider #6764

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7aab0eb
include BlockNumberProvider in config
Doordashcon Dec 4, 2024
f2bfa0c
BlockNumberProvider: Default
Doordashcon Dec 5, 2024
f947458
add prdoc & benchmark helper function
Doordashcon Dec 8, 2024
edc68f2
remove BlockNumberFor import benchmarks
Doordashcon Dec 8, 2024
f73ae91
Merge branch 'master' into BNP-pallet-nis
Doordashcon Dec 8, 2024
7e38fde
add migration file
Doordashcon Dec 15, 2024
64d0bb8
WIP migration
Doordashcon Jan 7, 2025
fafd840
remove convert_block_length_to_relay_length
Doordashcon Jan 7, 2025
ef38d00
BNP config doc
Doordashcon Jan 7, 2025
c2fbdfa
migrate last_period for Summary storage item also
Doordashcon Feb 4, 2025
7fe4577
adds documentation for BlockNumberProvider
Doordashcon Feb 4, 2025
148c4ab
Merge branch 'master' into BNP-pallet-nis
Doordashcon Feb 4, 2025
f7c02fc
add generics
Doordashcon Feb 4, 2025
601b043
configure weight cal
Doordashcon Feb 4, 2025
3ab5a0a
use taplo
Doordashcon Feb 5, 2025
fcf5395
nit
Doordashcon Feb 5, 2025
48d3af5
log/std feature
Doordashcon Feb 6, 2025
2a41a85
adjusting migration file..., introduce LastProcessedBlock
Doordashcon Feb 9, 2025
b075dec
fix BlockNumberProvider Doc
Doordashcon Feb 9, 2025
3fa3627
decode to current type
Doordashcon Feb 9, 2025
542c7ca
Merge branch 'master' into BNP-pallet-nis
Doordashcon Feb 14, 2025
80f7aff
nit
Doordashcon Feb 16, 2025
f170935
fmt
Doordashcon Feb 16, 2025
8205841
Merge branch 'master' into BNP-pallet-nis
Doordashcon Feb 16, 2025
70a7e5d
add try-runtime to tests
Doordashcon Feb 17, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,7 @@ impl pallet_nis::Config for Runtime {
type MaxIntakeWeight = MaxIntakeWeight;
type ThawThrottle = ThawThrottle;
type RuntimeHoldReason = RuntimeHoldReason;
type BlockNumberProvider = System;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkSetup = ();
}
Expand Down
15 changes: 15 additions & 0 deletions prdoc/pr_6764.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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: Update pallet-nis to support Block Number Provider

doc:
- audience: Runtime Dev
description: |
This PR makes the block number provider used in the nis pallet configurable.
Before this PR, pallet-nis always used the system block number,
with this PR some runtime can opt to use the relay chain block number instead.

crates:
- name: pallet-nis
bump: major
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,7 @@ impl pallet_nis::Config for Runtime {
type MaxIntakeWeight = MaxIntakeWeight;
type ThawThrottle = ThawThrottle;
type RuntimeHoldReason = RuntimeHoldReason;
type BlockNumberProvider = System;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkSetup = SetupAsset;
}
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/nis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ codec = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
sp-arithmetic = { workspace = true }
sp-core = { workspace = true }
Expand All @@ -36,6 +37,7 @@ std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-balances/std",
"scale-info/std",
"sp-arithmetic/std",
Expand Down
12 changes: 9 additions & 3 deletions substrate/frame/nis/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_support::traits::{
use frame_system::RawOrigin;
use sp_arithmetic::Perquintill;
use sp_runtime::{
traits::{Bounded, One, Zero},
traits::{BlockNumberProvider, Bounded, One, Zero},
DispatchError, PerThing,
};

Expand Down Expand Up @@ -57,6 +57,12 @@ fn fill_queues<T: Config>() -> Result<(), DispatchError> {
Ok(())
}

fn set_block_number<T: Config>(
n: <<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber,
) {
<T as crate::Config>::BlockNumberProvider::set_block_number(n);
}

benchmarks! {
place_bid {
let l in 0..(T::MaxQueueLen::get() - 1);
Expand Down Expand Up @@ -166,7 +172,7 @@ benchmarks! {
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
Nis::<T>::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited());
frame_system::Pallet::<T>::set_block_number(Receipts::<T>::get(0).unwrap().expiry);
set_block_number::<T>(Receipts::<T>::get(0).unwrap().expiry);
}: _(RawOrigin::Signed(caller.clone()), 0, None)
verify {
assert!(Receipts::<T>::get(0).is_none());
Expand All @@ -184,7 +190,7 @@ benchmarks! {
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
Nis::<T>::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited());
frame_system::Pallet::<T>::set_block_number(Receipts::<T>::get(0).unwrap().expiry);
set_block_number::<T>(Receipts::<T>::get(0).unwrap().expiry);
Nis::<T>::communify(RawOrigin::Signed(caller.clone()).into(), 0)?;
}: _(RawOrigin::Signed(caller.clone()), 0)
verify {
Expand Down
60 changes: 49 additions & 11 deletions substrate/frame/nis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ use sp_runtime::{
};

mod benchmarking;
pub mod migration;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;

/// The log target of this pallet.
pub const LOG_TARGET: &'static str = "runtime::nis";

pub struct WithMaximumOf<A: TypedGet>(core::marker::PhantomData<A>);
impl<A: TypedGet> Convert<Perquintill, A::Type> for WithMaximumOf<A>
where
Expand Down Expand Up @@ -190,21 +194,26 @@ pub mod pallet {
},
PalletId,
};
use frame_system::pallet_prelude::*;
use frame_system::pallet_prelude::{BlockNumberFor as SystemBlockNumberFor, *};
use sp_arithmetic::{PerThing, Perquintill};
use sp_runtime::{
traits::{AccountIdConversion, Bounded, Convert, ConvertBack, Saturating, Zero},
traits::{
AccountIdConversion, BlockNumberProvider, Bounded, Convert, ConvertBack, Saturating,
Zero,
},
Rounding, TokenError,
};

type BalanceOf<T> =
pub(crate) type BlockNumberFor<T> =
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
pub(crate) type BalanceOf<T> =
<<T as Config>::Currency as FunInspect<<T as frame_system::Config>::AccountId>>::Balance;
type DebtOf<T> =
fungible::Debt<<T as frame_system::Config>::AccountId, <T as Config>::Currency>;
type ReceiptRecordOf<T> =
pub(crate) type ReceiptRecordOf<T> =
ReceiptRecord<<T as frame_system::Config>::AccountId, BlockNumberFor<T>, BalanceOf<T>>;
type IssuanceInfoOf<T> = IssuanceInfo<BalanceOf<T>>;
type SummaryRecordOf<T> = SummaryRecord<BlockNumberFor<T>, BalanceOf<T>>;
pub(crate) type SummaryRecordOf<T> = SummaryRecord<BlockNumberFor<T>, BalanceOf<T>>;
type BidOf<T> = Bid<BalanceOf<T>, <T as frame_system::Config>::AccountId>;
type QueueTotalsTypeOf<T> = BoundedVec<(u32, BalanceOf<T>), <T as Config>::QueueCount>;

Expand Down Expand Up @@ -312,6 +321,25 @@ pub mod pallet {
#[pallet::constant]
type ThawThrottle: Get<(Perquintill, BlockNumberFor<Self>)>;

/// Abstracted source of block numbers for this pallet.
///
/// This provider decouples the pallet from direct use of the system block number, allowing:
/// - Integration with external block number sources.
/// - Custom timekeeping mechanisms (e.g. mock timelines for testing)
/// - Composite block numbers (e.g. parachain blocks vs relay chain blocks)
///
/// # Example: Using Relay Chain Block Numbers
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// # Example: Using Relay Chain Block Numbers
/// # Example: Using the local chain block numbers

/// ```rust,ignore
/// impl Config for Runtime {
/// type BlockNumberProvider = frame_system::Pallet<Runtime>;
/// }
/// ```
///
/// # Warning
/// Only implement custom providers if you need alternative timekeeping - most
/// pallets should use the system block number through `frame_system::Pallet<Runtime>`.
Comment on lines +337 to +340
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
///
/// # Warning
/// Only implement custom providers if you need alternative timekeeping - most
/// pallets should use the system block number through `frame_system::Pallet<Runtime>`.

Not sure this is that useful.

type BlockNumberProvider: BlockNumberProvider<BlockNumber: Default>;

/// Setup the state for benchmarking.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkSetup: crate::BenchmarkSetup;
Expand Down Expand Up @@ -407,6 +435,9 @@ pub mod pallet {
pub type Receipts<T> =
StorageMap<_, Blake2_128Concat, ReceiptIndex, ReceiptRecordOf<T>, OptionQuery>;

#[pallet::storage]
pub type LastProcessedBlock<T> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
Copy link
Member

Choose a reason for hiding this comment

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

Missing docs


#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -515,12 +546,19 @@ pub mod pallet {
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
impl<T: Config> Hooks<SystemBlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_n: SystemBlockNumberFor<T>) -> Weight {
let block_number = T::BlockNumberProvider::current_block_number();
let mut weight_counter =
WeightCounter { used: Weight::zero(), limit: T::MaxIntakeWeight::get() };
if T::IntakePeriod::get().is_zero() || (n % T::IntakePeriod::get()).is_zero() {
let last_processed = LastProcessedBlock::<T>::get();
let intake_period = T::IntakePeriod::get();
if intake_period.is_zero() ||
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if intake_period.is_zero() ||
if

saturating_sub will be always at least zero and thus, we don't need this check for zero.

block_number.saturating_sub(last_processed) >= intake_period
{
if weight_counter.check_accrue(T::WeightInfo::process_queues()) {
LastProcessedBlock::<T>::put(block_number);
weight_counter.check_accrue(T::DbWeight::get().writes(1));
Self::process_queues(
T::Target::get(),
T::QueueCount::get(),
Expand Down Expand Up @@ -693,7 +731,7 @@ pub mod pallet {
let (owner, mut on_hold) = receipt.owner.ok_or(Error::<T>::AlreadyCommunal)?;
ensure!(owner == who, Error::<T>::NotOwner);

let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
Copy link
Member

Choose a reason for hiding this comment

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

Misses a migration for the Receipts to change the expiry to the "new clock". (Not required when switched to System as block number provider.

Copy link
Contributor

@gui1117 gui1117 Dec 9, 2024

Choose a reason for hiding this comment

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

Most of those PR is for pallet in the relay chain to go to a parachain without requiring migration.

But in the wider scope I can see parachain could also make use of the relay chain block number provider when they are going more agile and skip some blocks.
In this case a migration would help indeed. We can just provide a function helper to do this migration, no need for storage versioning. Because parachain could change the configuration of the block number whenever they want, so unrelated to the version of the pallet.

@kianenigma do you think we should provide the block number migration function for all such pallets? I think some approved one don't have it.
Or should it be done on-demand when people ask for it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would not include a migration for the time being, assuming the PR and code-doc are super clear under which circumstances you do, and do not need a migration.

Copy link
Member

@bkchr bkchr Dec 9, 2024

Choose a reason for hiding this comment

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

But in the wider scope I can see parachain could also make use of the relay chain block number provider when they are going more agile and skip some blocks.

This is the entire point of this exercise. Otherwise it is quite useless. Not sure why we approve prs without a migration. Also requiring everyone to write their own migration is a little bit stupid. Especially as by just "reading the docs" it will not be that easy. Block numbers are stored internally in some structures and may not be that obvious for others to find them etc. This will just end up in a mess, if people are forced to write their own migrations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gone ahead writing a migration

ensure!(now >= receipt.expiry, Error::<T>::NotExpired);

let mut summary: SummaryRecordOf<T> = Summary::<T>::get();
Expand Down Expand Up @@ -800,7 +838,7 @@ pub mod pallet {
Receipts::<T>::get(index).ok_or(Error::<T>::UnknownReceipt)?;
// If found, check it is actually communal.
ensure!(receipt.owner.is_none(), Error::<T>::NotOwner);
let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
ensure!(now >= receipt.expiry, Error::<T>::NotExpired);

let mut summary: SummaryRecordOf<T> = Summary::<T>::get();
Expand Down Expand Up @@ -1032,7 +1070,7 @@ pub mod pallet {
return
}

let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let our_account = Self::account_id();
let issuance: IssuanceInfoOf<T> = Self::issuance_with(&our_account, &summary);
let mut remaining = target.saturating_sub(summary.proportion_owed) * issuance.effective;
Expand Down
Loading
Loading