Moves Block to frame_system instead of construct_runtime and removes Header and BlockNumber (#14437)

* Initial setup

* Adds node block

* Uses UncheckedExtrinsic and removes Where section

* Updates frame_system to use Block

* Adds deprecation warning

* Fixes pallet-timestamp

* Removes Header and BlockNumber

* Addresses review comments

* Addresses review comments

* Adds comment about compiler bug

* Removes where clause

* Refactors code

* Fixes errors in cargo check

* Fixes errors in cargo check

* Fixes warnings in cargo check

* Formatting

* Fixes construct_runtime tests

* Uses import instead of full path for BlockNumber

* Uses import instead of full path for Header

* Formatting

* Fixes construct_runtime tests

* Fixes imports in benchmarks

* Formatting

* Fixes construct_runtime tests

* Formatting

* Minor updates

* Fixes construct_runtime ui tests

* Fixes construct_runtime ui tests with 1.70

* Fixes docs

* Fixes docs

* Adds u128 mock block type

* Fixes split example

* fixes for cumulus

* ".git/.scripts/commands/fmt/fmt.sh"

* Updates new tests

* Fixes fully-qualified path in few places

* Formatting

* Update frame/examples/default-config/src/lib.rs

Co-authored-by: Juan <juangirini@gmail.com>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Juan <juangirini@gmail.com>

* ".git/.scripts/commands/fmt/fmt.sh"

* Addresses some review comments

* Fixes build

* ".git/.scripts/commands/fmt/fmt.sh"

* Update frame/democracy/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/democracy/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/construct_runtime/mod.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Addresses review comments

* Updates trait bounds

* Minor fix

* ".git/.scripts/commands/fmt/fmt.sh"

* Removes unnecessary bound

* ".git/.scripts/commands/fmt/fmt.sh"

* Updates test

* Fixes build

* Adds a bound for header

* ".git/.scripts/commands/fmt/fmt.sh"

* Removes where block

* Minor fix

* Minor fix

* Fixes tests

* ".git/.scripts/commands/update-ui/update-ui.sh" 1.70

* Updates test

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* Updates doc

* Updates doc

---------

Co-authored-by: command-bot <>
Co-authored-by: Juan <juangirini@gmail.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
gupnik
2023-07-13 17:31:34 +05:30
committed by GitHub
parent e42a669c50
commit 5e7b27e98c
277 changed files with 2017 additions and 2450 deletions
+3 -3
View File
@@ -388,21 +388,21 @@ pub mod pallet {
// dispatched.
//
// This function must return the weight consumed by `on_initialize` and `on_finalize`.
fn on_initialize(_n: T::BlockNumber) -> Weight {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
// Anything that needs to be done at the start of the block.
// We don't do anything here.
Weight::zero()
}
// `on_finalize` is executed at the end of block after all extrinsic are dispatched.
fn on_finalize(_n: T::BlockNumber) {
fn on_finalize(_n: BlockNumberFor<T>) {
// Perform necessary data/state clean up here.
}
// A runtime code run after every block and have access to extended set of APIs.
//
// For instance you can generate extrinsics for the upcoming produced block.
fn offchain_worker(_n: T::BlockNumber) {
fn offchain_worker(_n: BlockNumberFor<T>) {
// We don't do anything here.
// but we could dispatch extrinsic (transaction/unsigned/inherent) using
// sp_io::submit_extrinsic.
+2 -8
View File
@@ -27,22 +27,17 @@ use sp_core::H256;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
// Reexport crate as its pallet name for construct_runtime.
use crate as pallet_example_basic;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
// For testing the pallet, we construct a mock runtime.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
pub enum Test
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
@@ -57,13 +52,12 @@ impl frame_system::Config for Test {
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
@@ -107,17 +107,14 @@ pub mod pallet {
pub mod tests {
use super::*;
use frame_support::derive_impl;
use sp_runtime::traits::ConstU64;
use super::pallet as pallet_default_config_example;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
pub enum Test
{
System: frame_system,
DefaultPallet: pallet_default_config_example,
@@ -129,6 +126,8 @@ pub mod tests {
// these items are defined by frame-system as `no_default`, so we must specify them here.
// Note that these are types that actually rely on the outer runtime, and can't sensibly
// have an _independent_ default.
type Block = Block;
type BlockHashCount = ConstU64<10>;
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
@@ -140,7 +139,9 @@ pub mod tests {
// type Index = u32;
// type BlockNumber = u32;
// type Header = sp_runtime::generic::Header<Self::BlockNumber, Self::Hashing>;
// type Header =
// sp_runtime::generic::Header<frame_system::pallet_prelude::BlockNumberFor<Self>,
// Self::Hashing>;
// type Hash = sp_core::hash::H256;
// type Hashing = sp_runtime::traits::BlakeTwo256;
// type AccountId = u64;
@@ -21,22 +21,17 @@ use crate::*;
use frame_support::{assert_ok, traits::ConstU64};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
// Reexport crate as its pallet name for construct_runtime.
use crate as pallet_dev_mode;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
// For testing the pallet, we construct a mock runtime.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
pub enum Test
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
@@ -51,13 +46,12 @@ impl frame_system::Config for Test {
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
@@ -183,7 +183,7 @@ pub mod pallet {
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub foo: u32,
pub bar: T::BlockNumber,
pub bar: BlockNumberFor<T>,
}
impl<T: Config> Default for GenesisConfig<T> {
@@ -249,22 +249,22 @@ pub mod pallet {
/// All the possible hooks that a pallet can have. See [`frame_support::traits::Hooks`] for more
/// info.
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn integrity_test() {}
fn offchain_worker(_n: T::BlockNumber) {
fn offchain_worker(_n: BlockNumberFor<T>) {
unimplemented!()
}
fn on_initialize(_n: T::BlockNumber) -> Weight {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
unimplemented!()
}
fn on_finalize(_n: T::BlockNumber) {
fn on_finalize(_n: BlockNumberFor<T>) {
unimplemented!()
}
fn on_idle(_n: T::BlockNumber, _remaining_weight: Weight) -> Weight {
fn on_idle(_n: BlockNumberFor<T>, _remaining_weight: Weight) -> Weight {
unimplemented!()
}
@@ -283,7 +283,7 @@ pub mod pallet {
}
#[cfg(feature = "try-runtime")]
fn try_state(_n: T::BlockNumber) -> Result<(), TryRuntimeError> {
fn try_state(_n: BlockNumberFor<T>) -> Result<(), TryRuntimeError> {
unimplemented!()
}
}
@@ -23,15 +23,11 @@ use sp_runtime::BuildStorage;
// Reexport crate as its pallet name for construct_runtime.
use crate as pallet_example_kitchensink;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
// For testing the pallet, we construct a mock runtime.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
pub enum Test
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
@@ -44,6 +40,8 @@ frame_support::construct_runtime!(
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type Block = Block;
type BlockHashCount = ConstU64<10>;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
@@ -53,6 +53,7 @@ use frame_system::{
AppCrypto, CreateSignedTransaction, SendSignedTransaction, SendUnsignedTransaction,
SignedPayload, Signer, SigningTypes, SubmitTransaction,
},
pallet_prelude::BlockNumberFor,
};
use lite_json::json::JsonValue;
use sp_core::crypto::KeyTypeId;
@@ -136,14 +137,14 @@ pub mod pallet {
/// every `GRACE_PERIOD` blocks. We use Local Storage to coordinate
/// sending between distinct runs of this offchain worker.
#[pallet::constant]
type GracePeriod: Get<Self::BlockNumber>;
type GracePeriod: Get<BlockNumberFor<Self>>;
/// Number of blocks of cooldown after unsigned transaction is included.
///
/// This ensures that we only accept unsigned transactions once, every `UnsignedInterval`
/// blocks.
#[pallet::constant]
type UnsignedInterval: Get<Self::BlockNumber>;
type UnsignedInterval: Get<BlockNumberFor<Self>>;
/// A configuration for base priority of unsigned transactions.
///
@@ -171,7 +172,7 @@ pub mod pallet {
/// be cases where some blocks are skipped, or for some the worker runs twice (re-orgs),
/// so the code should be able to handle that.
/// You can use `Local Storage` API to coordinate runs of the worker.
fn offchain_worker(block_number: T::BlockNumber) {
fn offchain_worker(block_number: BlockNumberFor<T>) {
// Note that having logs compiled to WASM may cause the size of the blob to increase
// significantly. You can use `RuntimeDebug` custom derive to hide details of the types
// in WASM. The `sp-api` crate also provides a feature `disable-logging` to disable
@@ -258,7 +259,7 @@ pub mod pallet {
#[pallet::weight({0})]
pub fn submit_price_unsigned(
origin: OriginFor<T>,
_block_number: T::BlockNumber,
_block_number: BlockNumberFor<T>,
price: u32,
) -> DispatchResultWithPostInfo {
// This ensures that the function can only be called via unsigned transaction.
@@ -275,7 +276,7 @@ pub mod pallet {
#[pallet::weight({0})]
pub fn submit_price_unsigned_with_signed_payload(
origin: OriginFor<T>,
price_payload: PricePayload<T::Public, T::BlockNumber>,
price_payload: PricePayload<T::Public, BlockNumberFor<T>>,
_signature: T::Signature,
) -> DispatchResultWithPostInfo {
// This ensures that the function can only be called via unsigned transaction.
@@ -341,7 +342,7 @@ pub mod pallet {
/// This storage entry defines when new transaction is going to be accepted.
#[pallet::storage]
#[pallet::getter(fn next_unsigned_at)]
pub(super) type NextUnsignedAt<T: Config> = StorageValue<_, T::BlockNumber, ValueQuery>;
pub(super) type NextUnsignedAt<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
}
/// Payload used by this example crate to hold price
@@ -353,7 +354,7 @@ pub struct PricePayload<Public, BlockNumber> {
public: Public,
}
impl<T: SigningTypes> SignedPayload<T> for PricePayload<T::Public, T::BlockNumber> {
impl<T: SigningTypes> SignedPayload<T> for PricePayload<T::Public, BlockNumberFor<T>> {
fn public(&self) -> T::Public {
self.public.clone()
}
@@ -374,7 +375,7 @@ impl<T: Config> Pallet<T> {
/// and local storage usage.
///
/// Returns a type of transaction that should be produced in current run.
fn choose_transaction_type(block_number: T::BlockNumber) -> TransactionType {
fn choose_transaction_type(block_number: BlockNumberFor<T>) -> TransactionType {
/// A friendlier name for the error that is going to be returned in case we are in the grace
/// period.
const RECENTLY_SENT: () = ();
@@ -389,16 +390,17 @@ impl<T: Config> Pallet<T> {
// low-level method of local storage API, which means that only one worker
// will be able to "acquire a lock" and send a transaction if multiple workers
// happen to be executed concurrently.
let res = val.mutate(|last_send: Result<Option<T::BlockNumber>, StorageRetrievalError>| {
match last_send {
// If we already have a value in storage and the block number is recent enough
// we avoid sending another transaction at this time.
Ok(Some(block)) if block_number < block + T::GracePeriod::get() =>
Err(RECENTLY_SENT),
// In every other case we attempt to acquire the lock and send a transaction.
_ => Ok(block_number),
}
});
let res =
val.mutate(|last_send: Result<Option<BlockNumberFor<T>>, StorageRetrievalError>| {
match last_send {
// If we already have a value in storage and the block number is recent enough
// we avoid sending another transaction at this time.
Ok(Some(block)) if block_number < block + T::GracePeriod::get() =>
Err(RECENTLY_SENT),
// In every other case we attempt to acquire the lock and send a transaction.
_ => Ok(block_number),
}
});
// The result of `mutate` call will give us a nested `Result` type.
// The first one matches the return of the closure passed to `mutate`, i.e.
@@ -419,9 +421,9 @@ impl<T: Config> Pallet<T> {
let transaction_type = block_number % 4u32.into();
if transaction_type == Zero::zero() {
TransactionType::Signed
} else if transaction_type == T::BlockNumber::from(1u32) {
} else if transaction_type == BlockNumberFor::<T>::from(1u32) {
TransactionType::UnsignedForAny
} else if transaction_type == T::BlockNumber::from(2u32) {
} else if transaction_type == BlockNumberFor::<T>::from(2u32) {
TransactionType::UnsignedForAll
} else {
TransactionType::Raw
@@ -472,7 +474,9 @@ impl<T: Config> Pallet<T> {
}
/// A helper function to fetch the price and send a raw unsigned transaction.
fn fetch_price_and_send_raw_unsigned(block_number: T::BlockNumber) -> Result<(), &'static str> {
fn fetch_price_and_send_raw_unsigned(
block_number: BlockNumberFor<T>,
) -> Result<(), &'static str> {
// Make sure we don't fetch the price if unsigned transaction is going to be rejected
// anyway.
let next_unsigned_at = <NextUnsignedAt<T>>::get();
@@ -505,7 +509,7 @@ impl<T: Config> Pallet<T> {
/// A helper function to fetch the price, sign payload and send an unsigned transaction
fn fetch_price_and_send_unsigned_for_any_account(
block_number: T::BlockNumber,
block_number: BlockNumberFor<T>,
) -> Result<(), &'static str> {
// Make sure we don't fetch the price if unsigned transaction is going to be rejected
// anyway.
@@ -535,7 +539,7 @@ impl<T: Config> Pallet<T> {
/// A helper function to fetch the price, sign payload and send an unsigned transaction
fn fetch_price_and_send_unsigned_for_all_accounts(
block_number: T::BlockNumber,
block_number: BlockNumberFor<T>,
) -> Result<(), &'static str> {
// Make sure we don't fetch the price if unsigned transaction is going to be rejected
// anyway.
@@ -669,7 +673,7 @@ impl<T: Config> Pallet<T> {
}
fn validate_transaction_parameters(
block_number: &T::BlockNumber,
block_number: &BlockNumberFor<T>,
new_price: &u32,
) -> TransactionValidity {
// Now let's check if the transaction has any chance to succeed.
@@ -30,20 +30,16 @@ use sp_core::{
use sp_keystore::{testing::MemoryKeystore, Keystore, KeystoreExt};
use sp_runtime::{
testing::{Header, TestXt},
testing::TestXt,
traits::{BlakeTwo256, Extrinsic as ExtrinsicT, IdentifyAccount, IdentityLookup, Verify},
RuntimeAppPublic,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
// For testing the module, we construct a mock runtime.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
pub enum Test
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Example: example_offchain_worker::{Pallet, Call, Storage, Event<T>, ValidateUnsigned},
@@ -58,12 +54,11 @@ impl frame_system::Config for Test {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = sp_core::sr25519::Public;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
@@ -274,7 +269,7 @@ fn should_submit_unsigned_transaction_on_chain_for_any_account() {
let signature_valid =
<PricePayload<
<Test as SigningTypes>::Public,
<Test as frame_system::Config>::BlockNumber,
frame_system::pallet_prelude::BlockNumberFor<Test>,
> as SignedPayload<Test>>::verify::<crypto::TestAuthId>(&price_payload, signature);
assert!(signature_valid);
@@ -328,7 +323,7 @@ fn should_submit_unsigned_transaction_on_chain_for_all_accounts() {
let signature_valid =
<PricePayload<
<Test as SigningTypes>::Public,
<Test as frame_system::Config>::BlockNumber,
frame_system::pallet_prelude::BlockNumberFor<Test>,
> as SignedPayload<Test>>::verify::<crypto::TestAuthId>(&price_payload, signature);
assert!(signature_valid);
+4 -5
View File
@@ -17,16 +17,13 @@
use crate as pallet_template;
use frame_support::{derive_impl, sp_runtime::BuildStorage};
use sp_core::ConstU64;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
pub enum Test
{
System: frame_system,
TemplatePallet: pallet_template,
@@ -37,6 +34,8 @@ frame_support::construct_runtime!(
/// details.
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
type Block = Block;
type BlockHashCount = ConstU64<10>;
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;