mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 08:11:03 +00:00
Do not use rely on the block initialization when calling runtime APIs (#2123)
* Don't initialize block when calling runtime APIs * Adapt check_validation_outputs We split the code path for the inclusion and for the commitments checking. * Slap #[skip_initialize_block] on safe runtime APIs That is, those that should not be affected by this attribute * Make `Scheduled` not ephemeral So that it is persisted in the storage and ready to be inspected by the runtime APIs. This is in contrast to what was before, where we would remove the storage entry and then rely on the scheduling performed by `on_initialize` again. * Add a big fat comment * Typos Co-authored-by: Robert Habermeier <rphmeier@gmail.com> * Move session change to the end of the current block Previously, it was the beginning of the next block. This allows us to put #[skip_initialize_block] * Update tests * Fix a test in paras registrar Also refactor it a bit so the next time there are more chances this kind of issue is diagnosed quicker. * Add for_runtime_api to inclusion's check_validation_outputs Co-authored-by: Robert Habermeier <rphmeier@gmail.com>
This commit is contained in:
@@ -399,7 +399,12 @@ impl<T: Config> Module<T> {
|
||||
|
||||
let validators = Validators::get();
|
||||
let parent_hash = <frame_system::Module<T>>::parent_hash();
|
||||
let check_cx = CandidateCheckContext::<T>::new();
|
||||
|
||||
// At the moment we assume (and in fact enforce, below) that the relay-parent is always one
|
||||
// before of the block where we include a candidate (i.e. this code path).
|
||||
let now = <frame_system::Module<T>>::block_number();
|
||||
let relay_parent_number = now - One::one();
|
||||
let check_cx = CandidateCheckContext::<T>::new(now, relay_parent_number);
|
||||
|
||||
// do all checks before writing storage.
|
||||
let core_indices_and_backers = {
|
||||
@@ -483,7 +488,10 @@ impl<T: Config> Module<T> {
|
||||
{
|
||||
// this should never fail because the para is registered
|
||||
let persisted_validation_data =
|
||||
match crate::util::make_persisted_validation_data::<T>(para_id) {
|
||||
match crate::util::make_persisted_validation_data::<T>(
|
||||
para_id,
|
||||
relay_parent_number,
|
||||
) {
|
||||
Some(l) => l,
|
||||
None => {
|
||||
// We don't want to error out here because it will
|
||||
@@ -592,7 +600,7 @@ impl<T: Config> Module<T> {
|
||||
hash: candidate_hash,
|
||||
descriptor,
|
||||
availability_votes,
|
||||
relay_parent_number: check_cx.relay_parent_number,
|
||||
relay_parent_number,
|
||||
backers,
|
||||
backed_in_number: check_cx.now,
|
||||
});
|
||||
@@ -603,11 +611,17 @@ impl<T: Config> Module<T> {
|
||||
}
|
||||
|
||||
/// Run the acceptance criteria checks on the given candidate commitments.
|
||||
pub(crate) fn check_validation_outputs(
|
||||
pub(crate) fn check_validation_outputs_for_runtime_api(
|
||||
para_id: ParaId,
|
||||
validation_outputs: primitives::v1::CandidateCommitments,
|
||||
) -> bool {
|
||||
if let Err(err) = CandidateCheckContext::<T>::new().check_validation_outputs(
|
||||
// This function is meant to be called from the runtime APIs against the relay-parent, hence
|
||||
// `relay_parent_number` is equal to `now`.
|
||||
let now = <frame_system::Module<T>>::block_number();
|
||||
let relay_parent_number = now;
|
||||
let check_cx = CandidateCheckContext::<T>::new(now, relay_parent_number);
|
||||
|
||||
if let Err(err) = check_cx.check_validation_outputs(
|
||||
para_id,
|
||||
&validation_outputs.head_data,
|
||||
&validation_outputs.new_validation_code,
|
||||
@@ -812,12 +826,11 @@ struct CandidateCheckContext<T: Config> {
|
||||
}
|
||||
|
||||
impl<T: Config> CandidateCheckContext<T> {
|
||||
fn new() -> Self {
|
||||
let now = <frame_system::Module<T>>::block_number();
|
||||
fn new(now: T::BlockNumber, relay_parent_number: T::BlockNumber) -> Self {
|
||||
Self {
|
||||
config: <configuration::Module<T>>::config(),
|
||||
now,
|
||||
relay_parent_number: now - One::one(),
|
||||
relay_parent_number,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1111,8 +1124,12 @@ mod tests {
|
||||
}
|
||||
|
||||
fn make_vdata_hash(para_id: ParaId) -> Option<Hash> {
|
||||
let relay_parent_number = <frame_system::Module<Test>>::block_number() - 1;
|
||||
let persisted_validation_data
|
||||
= crate::util::make_persisted_validation_data::<Test>(para_id)?;
|
||||
= crate::util::make_persisted_validation_data::<Test>(
|
||||
para_id,
|
||||
relay_parent_number,
|
||||
)?;
|
||||
Some(persisted_validation_data.hash())
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ use primitives::v1::ValidatorId;
|
||||
use frame_support::{
|
||||
decl_storage, decl_module, decl_error, traits::Randomness,
|
||||
};
|
||||
use sp_runtime::traits::One;
|
||||
use parity_scale_codec::{Encode, Decode};
|
||||
use crate::{
|
||||
configuration::{self, HostConfiguration},
|
||||
@@ -63,8 +62,7 @@ impl<BlockNumber: Default + From<u32>> Default for SessionChangeNotification<Blo
|
||||
}
|
||||
|
||||
#[derive(Encode, Decode)]
|
||||
struct BufferedSessionChange<N> {
|
||||
apply_at: N,
|
||||
struct BufferedSessionChange {
|
||||
validators: Vec<ValidatorId>,
|
||||
queued: Vec<ValidatorId>,
|
||||
session_index: sp_staking::SessionIndex,
|
||||
@@ -98,12 +96,12 @@ decl_storage! {
|
||||
HasInitialized: Option<()>;
|
||||
/// Buffered session changes along with the block number at which they should be applied.
|
||||
///
|
||||
/// Typically this will be empty or one element long, with the single element having a block
|
||||
/// number of the next block.
|
||||
/// Typically this will be empty or one element long. Apart from that this item never hits
|
||||
/// the storage.
|
||||
///
|
||||
/// However this is a `Vec` regardless to handle various edge cases that may occur at runtime
|
||||
/// upgrade boundaries or if governance intervenes.
|
||||
BufferedSessionChanges: Vec<BufferedSessionChange<T::BlockNumber>>;
|
||||
BufferedSessionChanges: Vec<BufferedSessionChange>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,21 +115,6 @@ decl_module! {
|
||||
type Error = Error<T>;
|
||||
|
||||
fn on_initialize(now: T::BlockNumber) -> Weight {
|
||||
// Apply buffered session changes before initializing modules, so they
|
||||
// can be initialized with respect to the current validator set.
|
||||
<BufferedSessionChanges<T>>::mutate(|v| {
|
||||
let drain_up_to = v.iter().take_while(|b| b.apply_at <= now).count();
|
||||
|
||||
// apply only the last session as all others lasted less than a block (weirdly).
|
||||
if let Some(buffered) = v.drain(..drain_up_to).last() {
|
||||
Self::apply_new_session(
|
||||
buffered.session_index,
|
||||
buffered.validators,
|
||||
buffered.queued,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// The other modules are initialized in this order:
|
||||
// - Configuration
|
||||
// - Paras
|
||||
@@ -158,7 +141,6 @@ decl_module! {
|
||||
|
||||
fn on_finalize() {
|
||||
// reverse initialization order.
|
||||
|
||||
hrmp::Module::<T>::initializer_finalize();
|
||||
ump::Module::<T>::initializer_finalize();
|
||||
dmp::Module::<T>::initializer_finalize();
|
||||
@@ -167,6 +149,20 @@ decl_module! {
|
||||
scheduler::Module::<T>::initializer_finalize();
|
||||
paras::Module::<T>::initializer_finalize();
|
||||
configuration::Module::<T>::initializer_finalize();
|
||||
|
||||
// Apply buffered session changes as the last thing. This way the runtime APIs and the
|
||||
// next block will observe the next session.
|
||||
//
|
||||
// Note that we only apply the last session as all others lasted less than a block (weirdly).
|
||||
if let Some(BufferedSessionChange {
|
||||
session_index,
|
||||
validators,
|
||||
queued,
|
||||
}) = BufferedSessionChanges::take().pop()
|
||||
{
|
||||
Self::apply_new_session(session_index, validators, queued);
|
||||
}
|
||||
|
||||
HasInitialized::take();
|
||||
}
|
||||
}
|
||||
@@ -213,7 +209,7 @@ impl<T: Config> Module<T> {
|
||||
}
|
||||
|
||||
/// Should be called when a new session occurs. Buffers the session notification to be applied
|
||||
/// at the next block. If `queued` is `None`, the `validators` are considered queued.
|
||||
/// at the end of the block. If `queued` is `None`, the `validators` are considered queued.
|
||||
fn on_new_session<'a, I: 'a>(
|
||||
_changed: bool,
|
||||
session_index: sp_staking::SessionIndex,
|
||||
@@ -229,8 +225,7 @@ impl<T: Config> Module<T> {
|
||||
validators.clone()
|
||||
};
|
||||
|
||||
<BufferedSessionChanges<T>>::mutate(|v| v.push(BufferedSessionChange {
|
||||
apply_at: <frame_system::Module<T>>::block_number() + One::one(),
|
||||
BufferedSessionChanges::mutate(|v| v.push(BufferedSessionChange {
|
||||
validators,
|
||||
queued,
|
||||
session_index,
|
||||
@@ -264,7 +259,7 @@ impl<T: pallet_session::Config + Config> pallet_session::OneSessionHandler<T::Ac
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::mock::{new_test_ext, Initializer, Test, System};
|
||||
use crate::mock::{new_test_ext, Initializer, System};
|
||||
|
||||
use frame_support::traits::{OnFinalize, OnInitialize};
|
||||
|
||||
@@ -281,20 +276,15 @@ mod tests {
|
||||
let now = System::block_number();
|
||||
Initializer::on_initialize(now);
|
||||
|
||||
let v = <BufferedSessionChanges<Test>>::get();
|
||||
let v = <BufferedSessionChanges>::get();
|
||||
assert_eq!(v.len(), 1);
|
||||
|
||||
let apply_at = now + 1;
|
||||
assert_eq!(v[0].apply_at, apply_at);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn session_change_applied_on_initialize() {
|
||||
fn session_change_applied_on_finalize() {
|
||||
new_test_ext(Default::default()).execute_with(|| {
|
||||
Initializer::on_initialize(1);
|
||||
|
||||
let now = System::block_number();
|
||||
Initializer::on_new_session(
|
||||
false,
|
||||
1,
|
||||
@@ -302,9 +292,9 @@ mod tests {
|
||||
Some(Vec::new().into_iter()),
|
||||
);
|
||||
|
||||
Initializer::on_initialize(now + 1);
|
||||
Initializer::on_finalize(1);
|
||||
|
||||
assert!(<BufferedSessionChanges<Test>>::get().is_empty());
|
||||
assert!(<BufferedSessionChanges>::get().is_empty());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -193,12 +193,13 @@ pub fn full_validation_data<T: initializer::Config>(
|
||||
)
|
||||
-> Option<ValidationData<T::BlockNumber>>
|
||||
{
|
||||
let relay_parent_number = <frame_system::Module<T>>::block_number();
|
||||
with_assumption::<T, _, _>(
|
||||
para_id,
|
||||
assumption,
|
||||
|| Some(ValidationData {
|
||||
persisted: crate::util::make_persisted_validation_data::<T>(para_id)?,
|
||||
transient: crate::util::make_transient_validation_data::<T>(para_id)?,
|
||||
persisted: crate::util::make_persisted_validation_data::<T>(para_id, relay_parent_number)?,
|
||||
transient: crate::util::make_transient_validation_data::<T>(para_id, relay_parent_number)?,
|
||||
}),
|
||||
)
|
||||
}
|
||||
@@ -208,10 +209,11 @@ pub fn persisted_validation_data<T: initializer::Config>(
|
||||
para_id: ParaId,
|
||||
assumption: OccupiedCoreAssumption,
|
||||
) -> Option<PersistedValidationData<T::BlockNumber>> {
|
||||
let relay_parent_number = <frame_system::Module<T>>::block_number();
|
||||
with_assumption::<T, _, _>(
|
||||
para_id,
|
||||
assumption,
|
||||
|| crate::util::make_persisted_validation_data::<T>(para_id),
|
||||
|| crate::util::make_persisted_validation_data::<T>(para_id, relay_parent_number),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -220,7 +222,7 @@ pub fn check_validation_outputs<T: initializer::Config>(
|
||||
para_id: ParaId,
|
||||
outputs: primitives::v1::CandidateCommitments,
|
||||
) -> bool {
|
||||
<inclusion::Module<T>>::check_validation_outputs(para_id, outputs)
|
||||
<inclusion::Module<T>>::check_validation_outputs_for_runtime_api(para_id, outputs)
|
||||
}
|
||||
|
||||
/// Implementation for the `session_index_for_child` function of the runtime API.
|
||||
|
||||
@@ -182,7 +182,7 @@ decl_storage! {
|
||||
ParathreadClaimIndex: Vec<ParaId>;
|
||||
/// The block number where the session start occurred. Used to track how many group rotations have occurred.
|
||||
SessionStartBlock get(fn session_start_block): T::BlockNumber;
|
||||
/// Currently scheduled cores - free but up to be occupied. Ephemeral storage item that's wiped on finalization.
|
||||
/// Currently scheduled cores - free but up to be occupied.
|
||||
///
|
||||
/// Bounded by the number of cores: one for each parachain and parathread multiplexer.
|
||||
Scheduled get(fn scheduled): Vec<CoreAssignment>; // sorted ascending by CoreIndex.
|
||||
@@ -203,13 +203,6 @@ decl_module! {
|
||||
impl<T: Config> Module<T> {
|
||||
/// Called by the initializer to initialize the scheduler module.
|
||||
pub(crate) fn initializer_initialize(_now: T::BlockNumber) -> Weight {
|
||||
Self::schedule(Vec::new());
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
/// Called by the initializer to finalize the scheduler module.
|
||||
pub(crate) fn initializer_finalize() {
|
||||
// Free all scheduled cores and return parathread claims to queue, with retries incremented.
|
||||
let config = <configuration::Module<T>>::config();
|
||||
ParathreadQueue::mutate(|queue| {
|
||||
@@ -225,10 +218,16 @@ impl<T: Config> Module<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Self::schedule(Vec::new());
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
/// Called by the initializer to finalize the scheduler module.
|
||||
pub(crate) fn initializer_finalize() {}
|
||||
|
||||
/// Called by the initializer to note that a new session has started.
|
||||
pub(crate) fn initializer_on_new_session(notification: &SessionChangeNotification<T::BlockNumber>) {
|
||||
let &SessionChangeNotification {
|
||||
|
||||
@@ -17,19 +17,19 @@
|
||||
//! Utilities that don't belong to any particular module but may draw
|
||||
//! on all modules.
|
||||
|
||||
use sp_runtime::traits::{One, Saturating};
|
||||
use sp_runtime::traits::Saturating;
|
||||
use primitives::v1::{Id as ParaId, PersistedValidationData, TransientValidationData};
|
||||
|
||||
use crate::{configuration, paras, dmp, hrmp};
|
||||
|
||||
/// Make the persisted validation data for a particular parachain.
|
||||
/// Make the persisted validation data for a particular parachain and a specified relay-parent.
|
||||
///
|
||||
/// This ties together the storage of several modules.
|
||||
pub fn make_persisted_validation_data<T: paras::Config + hrmp::Config>(
|
||||
para_id: ParaId,
|
||||
relay_parent_number: T::BlockNumber,
|
||||
) -> Option<PersistedValidationData<T::BlockNumber>> {
|
||||
let config = <configuration::Module<T>>::config();
|
||||
let relay_parent_number = <frame_system::Module<T>>::block_number() - One::one();
|
||||
|
||||
Some(PersistedValidationData {
|
||||
parent_head: <paras::Module<T>>::para_head(¶_id)?,
|
||||
@@ -40,14 +40,14 @@ pub fn make_persisted_validation_data<T: paras::Config + hrmp::Config>(
|
||||
})
|
||||
}
|
||||
|
||||
/// Make the transient validation data for a particular parachain.
|
||||
/// Make the transient validation data for a particular parachain and a specified relay-parent.
|
||||
///
|
||||
/// This ties together the storage of several modules.
|
||||
pub fn make_transient_validation_data<T: paras::Config + dmp::Config>(
|
||||
para_id: ParaId,
|
||||
relay_parent_number: T::BlockNumber,
|
||||
) -> Option<TransientValidationData<T::BlockNumber>> {
|
||||
let config = <configuration::Module<T>>::config();
|
||||
let relay_parent_number = <frame_system::Module<T>>::block_number() - One::one();
|
||||
|
||||
let freq = config.validation_upgrade_frequency;
|
||||
let delay = config.validation_upgrade_delay;
|
||||
|
||||
Reference in New Issue
Block a user