Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
add RelaychainBlockNumberProvider (#515)
Browse files Browse the repository at this point in the history
* add RelaychainBlockNumberProvider

* add docs
  • Loading branch information
xlc authored and slumber committed Sep 1, 2021
1 parent 3219b42 commit 0823dcc
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pallets/parachain-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use sp_runtime::{
InvalidTransaction, TransactionLongevity, TransactionSource, TransactionValidity,
ValidTransaction,
},
offchain::storage_lock::BlockNumberProvider,
};
use sp_std::{cmp, collections::btree_map::BTreeMap, prelude::*};

Expand Down Expand Up @@ -1023,3 +1024,18 @@ pub trait CheckInherents<Block: BlockT> {
validation_data: &RelayChainStateProof,
) -> frame_support::inherent::CheckInherentsResult;
}

/// Implements [`BlockNumberProvider`] that returns relaychain block number fetched from
/// validation data.
/// NTOE: When validation data is not available (e.g. within on_initialize), 0 will be returned.
pub struct RelaychainBlockNumberProvider<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> BlockNumberProvider for RelaychainBlockNumberProvider<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()
}
}

0 comments on commit 0823dcc

Please sign in to comment.