Skip to content

Commit

Permalink
Return latest known relay chain block number in on_initialize etc (#…
Browse files Browse the repository at this point in the history
…2862)

This changes `RelaychainDataProvider` to return the latest known relay
chain block number in `on_initialize` et all, aka when the
`validation_data` wasn't yet set by the inherent.

---------

Co-authored-by: Dónal Murray <donal.murray@parity.io>
  • Loading branch information
bkchr and seadanda authored Jan 8, 2024
1 parent a97a6f2 commit dbff87c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
53 changes: 18 additions & 35 deletions cumulus/pallets/parachain-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1683,20 +1683,33 @@ pub trait RelaychainStateProvider {
}

/// Implements [`BlockNumberProvider`] that returns relay chain block number fetched from validation
/// data. When validation data is not available (e.g. within on_initialize), 0 will be returned.
/// data.
///
/// When validation data is not available (e.g. within `on_initialize`), it will fallback to use
/// [`Pallet::last_relay_block_number()`].
///
/// **NOTE**: This has been deprecated, please use [`RelaychainDataProvider`]
#[deprecated = "Use `RelaychainDataProvider` instead"]
pub struct RelaychainBlockNumberProvider<T>(sp_std::marker::PhantomData<T>);
pub type RelaychainBlockNumberProvider<T> = RelaychainDataProvider<T>;

#[allow(deprecated)]
impl<T: Config> BlockNumberProvider for RelaychainBlockNumberProvider<T> {
/// Implements [`BlockNumberProvider`] and [`RelaychainStateProvider`] that returns relevant relay
/// data fetched from validation data.
///
/// NOTE: When validation data is not available (e.g. within `on_initialize`):
///
/// - [`current_relay_chain_state`](Self::current_relay_chain_state): Will return the default value
/// of [`RelayChainState`].
/// - [`current_block_number`](Self::current_block_number): Will return
/// [`Pallet::last_relay_block_number()`].
pub struct RelaychainDataProvider<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> BlockNumberProvider for RelaychainDataProvider<T> {
type BlockNumber = relay_chain::BlockNumber;

fn current_block_number() -> relay_chain::BlockNumber {
Pallet::<T>::validation_data()
.map(|d| d.relay_parent_number)
.unwrap_or_default()
.unwrap_or_else(|| Pallet::<T>::last_relay_block_number())
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -1739,33 +1752,3 @@ impl<T: Config> RelaychainStateProvider for RelaychainDataProvider<T> {
ValidationData::<T>::put(validation_data)
}
}

/// Implements [`BlockNumberProvider`] and [`RelaychainStateProvider`] that returns relevant relay
/// data fetched from validation data.
/// NOTE: When validation data is not available (e.g. within on_initialize), default values will be
/// returned.
pub struct RelaychainDataProvider<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> BlockNumberProvider for RelaychainDataProvider<T> {
type BlockNumber = relay_chain::BlockNumber;

fn current_block_number() -> relay_chain::BlockNumber {
Pallet::<T>::validation_data()
.map(|d| d.relay_parent_number)
.unwrap_or_default()
}

#[cfg(feature = "runtime-benchmarks")]
fn set_block_number(block: Self::BlockNumber) {
let mut validation_data = Pallet::<T>::validation_data().unwrap_or_else(||
// PersistedValidationData does not impl default in non-std
PersistedValidationData {
parent_head: vec![].into(),
relay_parent_number: Default::default(),
max_pov_size: Default::default(),
relay_parent_storage_root: Default::default(),
});
validation_data.relay_parent_number = block;
ValidationData::<T>::put(validation_data)
}
}
11 changes: 11 additions & 0 deletions prdoc/pr_2862.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: Return latest known relay chain block number in `on_initialize` etc.

doc:
- audience: Runtime Dev
description: |
`RelaychainDataProvider` and `RelaychainBlockNumberProvider` will now return the latest known
relay chain block number in `on_initialize`, aka when `validation_data` wasn't yet set by
the inherent.

crates:
- name: "cumulus-pallet-parachain-system"

0 comments on commit dbff87c

Please sign in to comment.