-
Notifications
You must be signed in to change notification settings - Fork 823
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?
Conversation
Hello @gui1117, please review |
@@ -693,7 +702,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 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.
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.
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?
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.
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 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.
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.
Gone ahead writing a migration
substrate/frame/nis/src/mock.rs
Outdated
); | ||
Nis::on_initialize( | ||
<Test as pallet_nis::Config>::BlockNumberProvider::current_block_number(), | ||
); |
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.
on_finalize/on_initialize is taking system block number. Here is works because both pallet_nis block number and system block number are the same.
I think the code is cleaner if we use system block number same as before in this test no?
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.
Thanks for the review
substrate/frame/nis/src/lib.rs
Outdated
pub type BlockNumberFor<T> = | ||
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber; | ||
pub type BalanceOf<T> = |
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.
I don't mind reexporting types, but those names are so common I am afraid it will crate unecessary conflicts and bad IDE suggestions.
pub type BlockNumberFor<T> = | |
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber; | |
pub type BalanceOf<T> = | |
pub(crate) type BlockNumberFor<T> = | |
<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber; | |
pub(crate) type BalanceOf<T> = |
substrate/frame/nis/src/lib.rs
Outdated
<<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> = |
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.
pub type ReceiptRecordOf<T> = | |
pub(crate) type ReceiptRecordOf<T> = |
<T as frame_system::Config>::AccountId, | ||
SystemBlockNumberFor<T>, | ||
BalanceOf<T>, | ||
>; |
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.
we would also need to migrate Summary
storage.
substrate/frame/nis/src/migration.rs
Outdated
// 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| { |
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.
Doesn't look good, Here we are decoding the value with the old type. Note that system block number and relay block number can be different types like u32
and u64
.
We should only write the new value.
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.
...
if StorageVersion::get::<Pallet<T>>() == 0 {
Receipts::<T>::translate(|index, old_receipt: v0::OldReceiptRecordOf<T>| {
call_count.saturating_inc();
log::info!(target: TARGET, "migrating reciept record expiry #{:?}", &index);
let new_expiry =
BlockConversion::convert_block_number_to_relay_height(old_receipt.expiry);
Some(ReceiptRecord {
proportion: old_receipt.proportion,
owner: old_receipt.owner,
expiry: new_expiry,
})
});
....
}
...
@Doordashcon please check this comment - #6297 (comment) |
substrate/frame/nis/src/lib.rs
Outdated
/// # Example: Using Relay Chain Block Numbers | ||
/// ```rust,ignore | ||
/// impl Config for Runtime { | ||
/// type BlockNumberProvider = RelayChainBlockNumber; |
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.
/// type BlockNumberProvider = RelayChainBlockNumber; | |
/// type BlockNumberProvider = frame_system::Pallet<Runtime>; |
substrate/frame/nis/src/lib.rs
Outdated
/// | ||
/// # Warning | ||
/// Only implement custom providers if you need alternative timekeeping - most | ||
/// pallets should use the system block number through `frame_system::Pallet<T>`. |
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.
/// pallets should use the system block number through `frame_system::Pallet<T>`. | |
/// pallets should use the system block number through `frame_system::Pallet<Runtime>`. |
substrate/frame/nis/src/lib.rs
Outdated
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() |
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.
This wont work. I mentioned in the issue
Ensure pallet hooks like on_initialize(n) are compatible with the BlockNumberProvider and can handle cases when the hook isn’t called on every tick (e.g., adapt conditions that rely on exact matches like compilation_time == BlockNumberProvider::current_block_number()).
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.
proposed fix @muharem
If IntakePeriod
is not at every block then process_queues
will execute not long after the IntakePeriod
.
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() };
let last_processed = LastProcessedBlock::<T>::get();
let intake_period = T::IntakePeriod::get();
if intake_period.is_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(),
u32::max_value(),
&mut weight_counter,
)
}
}
weight_counter.used
}
substrate/frame/nis/src/migration.rs
Outdated
} | ||
|
||
/// Migrate the pallet storage from `0` to `1`. | ||
pub type MigrateV0ToV1<T, BlockConversion> = frame_support::migrations::VersionedMigration< |
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.
so everyone who uses this pallet should migrate to RC block numbers? why? this PR switches storage version to 1
, from 0
. this means this migration should be applied by all setups? what if my setup stays on Relay Chain? what if my setup stays on Parachain with system block provider?
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.
Working on a fix
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.
please review @muharem
@Doordashcon please let me know if you feel that we cannot get this merged by 17 Feb |
/// - 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 |
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.
/// # Example: Using Relay Chain Block Numbers | |
/// # Example: Using the local chain block numbers |
@@ -0,0 +1,261 @@ | |||
use crate::*; |
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 license header
/// | ||
/// # Warning | ||
/// Only implement custom providers if you need alternative timekeeping - most | ||
/// pallets should use the system block number through `frame_system::Pallet<Runtime>`. |
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.
/// | |
/// # 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.
@@ -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>; |
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
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() || |
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.
if intake_period.is_zero() || | |
if |
saturating_sub will be always at least zero and thus, we don't need this check for zero.
"running migration with onchain storage version {:?}", on_chain_version | ||
); | ||
|
||
if on_chain_version == 0 { |
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.
Does not needs to be bound to the storage version. Also later you could do a change of the block number source.
/// The log target. | ||
const TARGET: &'static str = "runtime::nis::migration::change_block_number_provider"; | ||
|
||
pub trait BlockNumberConversion<Old, New> { |
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.
Could be just the generic convert trait.
Part of #6297
Review Notes
ReceiptRecord
&SummaryRecord
types used in theReceipts
andSummary
storage items respectively, now adoptsT::BlockNumberProvider
with the condition that concrete types implementDefault