-
Notifications
You must be signed in to change notification settings - Fork 768
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
base: master
Are you sure you want to change the base?
Changes from all commits
7aab0eb
f2bfa0c
f947458
edc68f2
f73ae91
7e38fde
64d0bb8
fafd840
ef38d00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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: minor |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -190,18 +194,23 @@ 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 type BlockNumberFor<T> = | ||
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber; | ||
pub 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 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>>; | ||
|
@@ -312,12 +321,20 @@ pub mod pallet { | |
#[pallet::constant] | ||
type ThawThrottle: Get<(Perquintill, BlockNumberFor<Self>)>; | ||
|
||
/// Provider for the block number. | ||
type BlockNumberProvider: BlockNumberProvider<BlockNumber: Default>; | ||
|
||
/// Setup the state for benchmarking. | ||
#[cfg(feature = "runtime-benchmarks")] | ||
type BenchmarkSetup: crate::BenchmarkSetup; | ||
} | ||
|
||
/// The in-code storage version. | ||
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); | ||
|
||
#[pallet::pallet] | ||
#[pallet::storage_version(STORAGE_VERSION)] | ||
|
||
pub struct Pallet<T>(_); | ||
|
||
/// A single bid, an item of a *queue* in `Queues`. | ||
|
@@ -515,11 +532,13 @@ 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() { | ||
if T::IntakePeriod::get().is_zero() || (block_number % T::IntakePeriod::get()).is_zero() | ||
{ | ||
if weight_counter.check_accrue(T::WeightInfo::process_queues()) { | ||
Self::process_queues( | ||
T::Target::get(), | ||
|
@@ -693,7 +712,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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misses a migration for the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. @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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -800,7 +819,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(); | ||
|
@@ -1032,7 +1051,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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use super::*; | ||
use codec::{Decode, Encode, MaxEncodedLen}; | ||
use core::marker::PhantomData; | ||
use frame_support::traits::UncheckedOnRuntimeUpgrade; | ||
use sp_runtime::Weight; | ||
|
||
#[cfg(feature = "try-runtime")] | ||
use sp_runtime::TryRuntimeError; | ||
|
||
type SystemBlockNumberFor<T> = frame_system::pallet_prelude::BlockNumberFor<T>; | ||
|
||
pub mod v0 { | ||
use super::*; | ||
use frame_support::{pallet_prelude::OptionQuery, storage_alias, Blake2_128Concat}; | ||
|
||
#[storage_alias] | ||
pub type Receipts<T: Config> = | ||
StorageMap<Pallet<T>, Blake2_128Concat, ReceiptIndex, ReceiptRecordOf<T>, OptionQuery>; | ||
pub type ReceiptRecordOf<T> = ReceiptRecord< | ||
<T as frame_system::Config>::AccountId, | ||
SystemBlockNumberFor<T>, | ||
BalanceOf<T>, | ||
>; | ||
} | ||
|
||
pub mod v1 { | ||
use super::*; | ||
pub fn get_all_receipt_expiries<T: Config>() -> Vec<(ReceiptIndex, SystemBlockNumberFor<T>)> { | ||
let mut receipt_expiry = Vec::new(); | ||
|
||
// Using for_each to collect the index and expiry values | ||
v0::Receipts::<T>::iter().for_each(|(index, receipt)| { | ||
// Collecting both the index and the expiry block number as a tuple | ||
receipt_expiry.push((index, receipt.expiry)); | ||
}); | ||
|
||
receipt_expiry | ||
} | ||
|
||
pub trait BlockToRelayHeightConversion<T: Config> { | ||
fn convert_block_number_to_relay_height( | ||
block_number: SystemBlockNumberFor<T>, | ||
) -> BlockNumberFor<T>; | ||
} | ||
|
||
pub struct MigrateToRBN<T, BlockConversion>(PhantomData<T>, PhantomData<BlockConversion>); | ||
impl<T: Config, BlockConversion: BlockToRelayHeightConversion<T>> UncheckedOnRuntimeUpgrade | ||
for MigrateToRBN<T, BlockConversion> | ||
{ | ||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> { | ||
// Collect the receipt expiry data, convert it, and prepare for encoding | ||
let updated_expiry_records: Vec<(ReceiptIndex, BlockNumberFor<T>)> = | ||
get_all_receipt_expiries::<T>() | ||
.into_iter() | ||
.map(|(index, expiry)| { | ||
let updated_expiry = | ||
BlockConversion::convert_block_number_to_relay_height(expiry); | ||
(index, updated_expiry) | ||
}) | ||
.collect(); | ||
|
||
// Encode the updated records and return them | ||
Ok(updated_expiry_records.encode()) | ||
} | ||
|
||
fn on_runtime_upgrade() -> Weight { | ||
log::info!( | ||
target: LOG_TARGET, | ||
"Running migration to change reciept expiry to new clock.", | ||
); | ||
|
||
let mut call_count = 0u64; | ||
|
||
// Iterate over v0::Receipts and update the expiry in Receipts | ||
v0::Receipts::<T>::iter().for_each(|(index, v0_receipt)| { | ||
// Use mutate to modify the value directly in the storage | ||
Receipts::<T>::mutate(index, |maybe_receipt| { | ||
if let Some(receipt) = maybe_receipt { | ||
// Update the expiry field | ||
receipt.expiry = BlockConversion::convert_block_number_to_relay_height( | ||
v0_receipt.expiry, | ||
); | ||
|
||
// Increment the call count | ||
call_count = call_count.saturating_add(2); | ||
} | ||
}); | ||
}); | ||
|
||
call_count.into() | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docs.