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

Commit

Permalink
Show something
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr committed Jun 20, 2023
1 parent 242858f commit c33def7
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 64 deletions.
40 changes: 20 additions & 20 deletions frame/support/procedural/src/pallet/expand/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
let frame_system = &def.frame_system;
quote::quote! {
impl<#type_impl_gen>
#frame_support::traits::Hooks<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::Hooks<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause {}
}
} else {
Expand Down Expand Up @@ -137,50 +137,50 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
#hooks_impl

impl<#type_impl_gen>
#frame_support::traits::OnFinalize<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::OnFinalize<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause
{
fn on_finalize(n: <T as #frame_system::Config>::BlockNumber) {
fn on_finalize(n: #frame_system::pallet_prelude::BlockNumberFor::<T>) {
#frame_support::sp_tracing::enter_span!(
#frame_support::sp_tracing::trace_span!("on_finalize")
);
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::on_finalize(n)
}
}

impl<#type_impl_gen>
#frame_support::traits::OnIdle<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::OnIdle<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause
{
fn on_idle(
n: <T as #frame_system::Config>::BlockNumber,
n: #frame_system::pallet_prelude::BlockNumberFor::<T>,
remaining_weight: #frame_support::weights::Weight
) -> #frame_support::weights::Weight {
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::on_idle(n, remaining_weight)
}
}

impl<#type_impl_gen>
#frame_support::traits::OnInitialize<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::OnInitialize<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause
{
fn on_initialize(
n: <T as #frame_system::Config>::BlockNumber
n: #frame_system::pallet_prelude::BlockNumberFor::<T>
) -> #frame_support::weights::Weight {
#frame_support::sp_tracing::enter_span!(
#frame_support::sp_tracing::trace_span!("on_initialize")
);
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::on_initialize(n)
}
Expand All @@ -205,7 +205,7 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {

<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::on_runtime_upgrade()
}
Expand All @@ -215,7 +215,7 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
<
Self
as
#frame_support::traits::Hooks<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::Hooks<#frame_system::pallet_prelude::BlockNumberFor::<T>>
>::pre_upgrade()
}

Expand All @@ -226,19 +226,19 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
<
Self
as
#frame_support::traits::Hooks<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::Hooks<#frame_system::pallet_prelude::BlockNumberFor::<T>>
>::post_upgrade(state)
}
}

impl<#type_impl_gen>
#frame_support::traits::OffchainWorker<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::OffchainWorker<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause
{
fn offchain_worker(n: <T as #frame_system::Config>::BlockNumber) {
fn offchain_worker(n: #frame_system::pallet_prelude::BlockNumberFor::<T>) {
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::offchain_worker(n)
}
Expand All @@ -253,7 +253,7 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {
fn integrity_test() {
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::integrity_test()
}
Expand All @@ -262,17 +262,17 @@ pub fn expand_hooks(def: &mut Def) -> proc_macro2::TokenStream {

#[cfg(feature = "try-runtime")]
impl<#type_impl_gen>
#frame_support::traits::TryState<<T as #frame_system::Config>::BlockNumber>
#frame_support::traits::TryState<#frame_system::pallet_prelude::BlockNumberFor::<T>>
for #pallet_ident<#type_use_gen> #where_clause
{
fn try_state(
n: <T as #frame_system::Config>::BlockNumber,
n: #frame_system::pallet_prelude::BlockNumberFor::<T>,
_s: #frame_support::traits::TryStateSelect
) -> Result<(), #frame_support::sp_runtime::TryRuntimeError> {
#log_try_state
<
Self as #frame_support::traits::Hooks<
<T as #frame_system::Config>::BlockNumber
#frame_system::pallet_prelude::BlockNumberFor::<T>
>
>::try_state(n)
}
Expand Down
4 changes: 2 additions & 2 deletions frame/system/src/extensions/check_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{Config, Pallet};
use crate::{Config, Pallet, pallet_prelude::BlockNumberFor};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<T: Config + Send + Sync> SignedExtension for CheckGenesis<T> {
const IDENTIFIER: &'static str = "CheckGenesis";

fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(<Pallet<T>>::block_hash(T::BlockNumber::zero()))
Ok(<Pallet<T>>::block_hash(BlockNumberFor::<T>::zero()))
}

fn pre_dispatch(
Expand Down
4 changes: 2 additions & 2 deletions frame/system/src/extensions/check_mortality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{BlockHash, Config, Pallet};
use crate::{BlockHash, Config, Pallet, pallet_prelude::BlockNumberFor};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<T: Config + Send + Sync> SignedExtension for CheckMortality<T> {

fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
let current_u64 = <Pallet<T>>::block_number().saturated_into::<u64>();
let n = self.0.birth(current_u64).saturated_into::<T::BlockNumber>();
let n = self.0.birth(current_u64).saturated_into::<BlockNumberFor<T>>();
if !<BlockHash<T>>::contains_key(n) {
Err(InvalidTransaction::AncientBirthBlock.into())
} else {
Expand Down
49 changes: 19 additions & 30 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use pallet_prelude::BlockNumberFor;
#[cfg(feature = "std")]
use serde::Serialize;
use sp_io::hashing::blake2_256;
Expand Down Expand Up @@ -201,6 +202,7 @@ impl<MaxNormal: Get<u32>, MaxOverflow: Get<u32>> ConsumerLimits for (MaxNormal,
pub mod pallet {
use crate::{self as frame_system, pallet_prelude::*, *};
use frame_support::pallet_prelude::*;
use sp_runtime::traits::NumberFor;

/// Contains default types suitable for various environments
pub mod config_preludes {
Expand All @@ -217,13 +219,10 @@ pub mod pallet {
#[frame_support::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type Index = u32;
type BlockNumber = u32;
type Header = sp_runtime::generic::Header<Self::BlockNumber, Self::Hashing>;
type Hash = sp_core::hash::H256;
type Hashing = sp_runtime::traits::BlakeTwo256;
type AccountId = u64;
type Lookup = sp_runtime::traits::IdentityLookup<u64>;
type BlockHashCount = frame_support::traits::ConstU32<10>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type AccountData = ();
type OnNewAccount = ();
Expand Down Expand Up @@ -287,21 +286,6 @@ pub mod pallet {
+ Copy
+ MaxEncodedLen;

/// The block number type used by the runtime.
type BlockNumber: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Debug
+ MaybeDisplay
+ AtLeast32BitUnsigned
+ Default
+ Bounded
+ Copy
+ sp_std::hash::Hash
+ sp_std::str::FromStr
+ MaxEncodedLen
+ TypeInfo;

/// The output of the `Hashing` function.
type Hash: Parameter
+ Member
Expand Down Expand Up @@ -339,11 +323,13 @@ pub mod pallet {
type Lookup: StaticLookup<Target = Self::AccountId>;

/// The block header.
type Header: Parameter + traits::Header<Number = Self::BlockNumber, Hash = Self::Hash>;
#[pallet::no_default]
type Header: Parameter + traits::Header<Number = BlockNumberFor<Self>, Hash = Self::Hash>;

/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
#[pallet::constant]
type BlockHashCount: Get<Self::BlockNumber>;
#[pallet::no_default]
type BlockHashCount: Get<BlockNumberFor<Self>>;

/// The weight of runtime database operations the runtime can invoke.
#[pallet::constant]
Expand Down Expand Up @@ -396,6 +382,9 @@ pub mod pallet {

/// The maximum number of consumers allowed on a single account.
type MaxConsumers: ConsumerLimits;

#[pallet::no_default]
type Block: traits::Block;
}

#[pallet::pallet]
Expand Down Expand Up @@ -590,7 +579,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn block_hash)]
pub type BlockHash<T: Config> =
StorageMap<_, Twox64Concat, T::BlockNumber, T::Hash, ValueQuery>;
StorageMap<_, Twox64Concat, BlockNumberFor<T>, T::Hash, ValueQuery>;

/// Extrinsics data for the current block (maps an extrinsic's index to its data).
#[pallet::storage]
Expand All @@ -603,7 +592,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::whitelist_storage]
#[pallet::getter(fn block_number)]
pub(super) type Number<T: Config> = StorageValue<_, T::BlockNumber, ValueQuery>;
pub(super) type Number<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;

/// Hash of the previous block.
#[pallet::storage]
Expand Down Expand Up @@ -649,7 +638,7 @@ pub mod pallet {
#[pallet::unbounded]
#[pallet::getter(fn event_topics)]
pub(super) type EventTopics<T: Config> =
StorageMap<_, Blake2_128Concat, T::Hash, Vec<(T::BlockNumber, EventIndex)>, ValueQuery>;
StorageMap<_, Blake2_128Concat, T::Hash, Vec<(BlockNumberFor<T>, EventIndex)>, ValueQuery>;

/// Stores the `spec_version` and `spec_name` of when the last runtime upgrade happened.
#[pallet::storage]
Expand Down Expand Up @@ -680,7 +669,7 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
<BlockHash<T>>::insert::<_, T::Hash>(T::BlockNumber::zero(), hash69());
<BlockHash<T>>::insert::<_, T::Hash>(BlockNumberFor::<T>::zero(), hash69());
<ParentHash<T>>::put::<T::Hash>(hash69());
<LastRuntimeUpgrade<T>>::put(LastRuntimeUpgradeInfo::from(T::Version::get()));
<UpgradedToU32RefCount<T>>::put(true);
Expand Down Expand Up @@ -1388,7 +1377,7 @@ impl<T: Config> Pallet<T> {
}

/// Start the execution of a particular block.
pub fn initialize(number: &T::BlockNumber, parent_hash: &T::Hash, digest: &generic::Digest) {
pub fn initialize(number: &BlockNumberFor<T>, parent_hash: &T::Hash, digest: &generic::Digest) {
// populate environment
ExecutionPhase::<T>::put(Phase::Initialization);
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
Expand Down Expand Up @@ -1496,8 +1485,8 @@ impl<T: Config> Pallet<T> {
pub fn externalities() -> TestExternalities {
TestExternalities::new(sp_core::storage::Storage {
top: map![
<BlockHash<T>>::hashed_key_for(T::BlockNumber::zero()) => [69u8; 32].encode(),
<Number<T>>::hashed_key().to_vec() => T::BlockNumber::one().encode(),
<BlockHash<T>>::hashed_key_for(BlockNumberFor::<T>::zero()) => [69u8; 32].encode(),
<Number<T>>::hashed_key().to_vec() => BlockNumberFor::<T>::one().encode(),
<ParentHash<T>>::hashed_key().to_vec() => [69u8; 32].encode()
],
children_default: map![],
Expand Down Expand Up @@ -1542,7 +1531,7 @@ impl<T: Config> Pallet<T> {
/// Set the block number to something in particular. Can be used as an alternative to
/// `initialize` for tests that don't need to bother with the other environment entries.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn set_block_number(n: T::BlockNumber) {
pub fn set_block_number(n: BlockNumberFor<T>) {
<Number<T>>::put(n);
}

Expand Down Expand Up @@ -1760,7 +1749,7 @@ impl<T: Config> HandleLifetime<T::AccountId> for Consumer<T> {
}

impl<T: Config> BlockNumberProvider for Pallet<T> {
type BlockNumber = <T as Config>::BlockNumber;
type BlockNumber = BlockNumberFor<T>;

fn current_block_number() -> Self::BlockNumber {
Pallet::<T>::block_number()
Expand Down Expand Up @@ -1832,5 +1821,5 @@ pub mod pallet_prelude {
pub type OriginFor<T> = <T as crate::Config>::RuntimeOrigin;

/// Type alias for the `BlockNumber` associated type of system config.
pub type BlockNumberFor<T> = <T as crate::Config>::BlockNumber;
pub type BlockNumberFor<T> = sp_runtime::traits::NumberFor<<T as crate::Config>::Block>;
}
4 changes: 3 additions & 1 deletion frame/system/src/mocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use sp_runtime::generic;

use crate::pallet_prelude::BlockNumberFor;

/// An unchecked extrinsic type to be used in tests.
pub type MockUncheckedExtrinsic<T, Signature = (), Extra = ()> = generic::UncheckedExtrinsic<
<T as crate::Config>::AccountId,
Expand All @@ -29,6 +31,6 @@ pub type MockUncheckedExtrinsic<T, Signature = (), Extra = ()> = generic::Unchec

/// An implementation of `sp_runtime::traits::Block` to be used in tests.
pub type MockBlock<T> = generic::Block<
generic::Header<<T as crate::Config>::BlockNumber, sp_runtime::traits::BlakeTwo256>,
generic::Header<u64, sp_runtime::traits::BlakeTwo256>,
MockUncheckedExtrinsic<T>,
>;
Loading

0 comments on commit c33def7

Please sign in to comment.