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
+5 -4
View File
@@ -34,6 +34,7 @@
//! definition.
use frame_support::traits::{Get, KeyOwnerProofSystem};
use frame_system::pallet_prelude::HeaderFor;
use log::{error, info};
use sp_consensus_babe::{AuthorityId, EquivocationProof, Slot, KEY_TYPE};
@@ -106,7 +107,7 @@ impl<Offender: Clone> Offence<Offender> for EquivocationOffence<Offender> {
pub struct EquivocationReportSystem<T, R, P, L>(sp_std::marker::PhantomData<(T, R, P, L)>);
impl<T, R, P, L>
OffenceReportSystem<Option<T::AccountId>, (EquivocationProof<T::Header>, T::KeyOwnerProof)>
OffenceReportSystem<Option<T::AccountId>, (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof)>
for EquivocationReportSystem<T, R, P, L>
where
T: Config + pallet_authorship::Config + frame_system::offchain::SendTransactionTypes<Call<T>>,
@@ -122,7 +123,7 @@ where
type Longevity = L;
fn publish_evidence(
evidence: (EquivocationProof<T::Header>, T::KeyOwnerProof),
evidence: (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof),
) -> Result<(), ()> {
use frame_system::offchain::SubmitTransaction;
let (equivocation_proof, key_owner_proof) = evidence;
@@ -140,7 +141,7 @@ where
}
fn check_evidence(
evidence: (EquivocationProof<T::Header>, T::KeyOwnerProof),
evidence: (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof),
) -> Result<(), TransactionValidityError> {
let (equivocation_proof, key_owner_proof) = evidence;
@@ -159,7 +160,7 @@ where
fn process_evidence(
reporter: Option<T::AccountId>,
evidence: (EquivocationProof<T::Header>, T::KeyOwnerProof),
evidence: (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof),
) -> Result<(), DispatchError> {
let (equivocation_proof, key_owner_proof) = evidence;
let reporter = reporter.or_else(|| <pallet_authorship::Pallet<T>>::author());
+27 -22
View File
@@ -29,6 +29,7 @@ use frame_support::{
weights::Weight,
BoundedVec, WeakBoundedVec,
};
use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor};
use sp_consensus_babe::{
digests::{NextConfigDescriptor, NextEpochDescriptor, PreDigest},
AllowedSlots, BabeAuthorityWeight, BabeEpochConfiguration, ConsensusLog, Epoch,
@@ -78,7 +79,7 @@ pub trait WeightInfo {
pub trait EpochChangeTrigger {
/// Trigger an epoch change, if any should take place. This should be called
/// during every block, after initialization is done.
fn trigger<T: Config>(now: T::BlockNumber);
fn trigger<T: Config>(now: BlockNumberFor<T>);
}
/// A type signifying to BABE that an external trigger
@@ -86,7 +87,7 @@ pub trait EpochChangeTrigger {
pub struct ExternalTrigger;
impl EpochChangeTrigger for ExternalTrigger {
fn trigger<T: Config>(_: T::BlockNumber) {} // nothing - trigger is external.
fn trigger<T: Config>(_: BlockNumberFor<T>) {} // nothing - trigger is external.
}
/// A type signifying to BABE that it should perform epoch changes
@@ -94,7 +95,7 @@ impl EpochChangeTrigger for ExternalTrigger {
pub struct SameAuthoritiesForever;
impl EpochChangeTrigger for SameAuthoritiesForever {
fn trigger<T: Config>(now: T::BlockNumber) {
fn trigger<T: Config>(now: BlockNumberFor<T>) {
if <Pallet<T>>::should_epoch_change(now) {
let authorities = <Pallet<T>>::authorities();
let next_authorities = authorities.clone();
@@ -162,7 +163,7 @@ pub mod pallet {
/// (from an offchain context).
type EquivocationReportSystem: OffenceReportSystem<
Option<Self::AccountId>,
(EquivocationProof<Self::Header>, Self::KeyOwnerProof),
(EquivocationProof<HeaderFor<Self>>, Self::KeyOwnerProof),
>;
}
@@ -279,7 +280,7 @@ pub mod pallet {
/// slots, which may be skipped, the block numbers may not line up with the slot numbers.
#[pallet::storage]
pub(super) type EpochStart<T: Config> =
StorageValue<_, (T::BlockNumber, T::BlockNumber), ValueQuery>;
StorageValue<_, (BlockNumberFor<T>, BlockNumberFor<T>), ValueQuery>;
/// How late the current block is compared to its parent.
///
@@ -288,7 +289,7 @@ pub mod pallet {
/// execution context should always yield zero.
#[pallet::storage]
#[pallet::getter(fn lateness)]
pub(super) type Lateness<T: Config> = StorageValue<_, T::BlockNumber, ValueQuery>;
pub(super) type Lateness<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
/// The configuration for the current epoch. Should never be `None` as it is initialized in
/// genesis.
@@ -409,7 +410,7 @@ pub mod pallet {
))]
pub fn report_equivocation(
origin: OriginFor<T>,
equivocation_proof: Box<EquivocationProof<T::Header>>,
equivocation_proof: Box<EquivocationProof<HeaderFor<T>>>,
key_owner_proof: T::KeyOwnerProof,
) -> DispatchResultWithPostInfo {
let reporter = ensure_signed(origin)?;
@@ -435,7 +436,7 @@ pub mod pallet {
))]
pub fn report_equivocation_unsigned(
origin: OriginFor<T>,
equivocation_proof: Box<EquivocationProof<T::Header>>,
equivocation_proof: Box<EquivocationProof<HeaderFor<T>>>,
key_owner_proof: T::KeyOwnerProof,
) -> DispatchResultWithPostInfo {
ensure_none(origin)?;
@@ -505,8 +506,8 @@ impl<T: Config> IsMember<AuthorityId> for Pallet<T> {
}
}
impl<T: Config> pallet_session::ShouldEndSession<T::BlockNumber> for Pallet<T> {
fn should_end_session(now: T::BlockNumber) -> bool {
impl<T: Config> pallet_session::ShouldEndSession<BlockNumberFor<T>> for Pallet<T> {
fn should_end_session(now: BlockNumberFor<T>) -> bool {
// it might be (and it is in current implementation) that session module is calling
// `should_end_session` from it's own `on_initialize` handler, in which case it's
// possible that babe's own `on_initialize` has not run yet, so let's ensure that we
@@ -526,7 +527,7 @@ impl<T: Config> Pallet<T> {
/// Determine whether an epoch change should take place at this block.
/// Assumes that initialization has already taken place.
pub fn should_epoch_change(now: T::BlockNumber) -> bool {
pub fn should_epoch_change(now: BlockNumberFor<T>) -> bool {
// The epoch has technically ended during the passage of time
// between this block and the last, but we have to "end" the epoch now,
// since there is no earlier possible block we could have done it.
@@ -556,11 +557,11 @@ impl<T: Config> Pallet<T> {
//
// WEIGHT NOTE: This function is tied to the weight of `EstimateNextSessionRotation`. If you
// update this function, you must also update the corresponding weight.
pub fn next_expected_epoch_change(now: T::BlockNumber) -> Option<T::BlockNumber> {
pub fn next_expected_epoch_change(now: BlockNumberFor<T>) -> Option<BlockNumberFor<T>> {
let next_slot = Self::current_epoch_start().saturating_add(T::EpochDuration::get());
next_slot.checked_sub(*CurrentSlot::<T>::get()).map(|slots_remaining| {
// This is a best effort guess. Drifts in the slot/block ratio will cause errors here.
let blocks_remaining: T::BlockNumber = slots_remaining.saturated_into();
let blocks_remaining: BlockNumberFor<T> = slots_remaining.saturated_into();
now.saturating_add(blocks_remaining)
})
}
@@ -778,7 +779,7 @@ impl<T: Config> Pallet<T> {
Self::deposit_consensus(ConsensusLog::NextEpochData(next));
}
fn initialize(now: T::BlockNumber) {
fn initialize(now: BlockNumberFor<T>) {
// since `initialize` can be called twice (e.g. if session module is present)
// let's ensure that we only do the initialization once per block
let initialized = Self::initialized().is_some();
@@ -813,7 +814,7 @@ impl<T: Config> Pallet<T> {
// how many slots were skipped between current and last block
let lateness = current_slot.saturating_sub(CurrentSlot::<T>::get() + 1);
let lateness = T::BlockNumber::from(*lateness as u32);
let lateness = BlockNumberFor::<T>::from(*lateness as u32);
Lateness::<T>::put(lateness);
CurrentSlot::<T>::put(current_slot);
@@ -879,7 +880,7 @@ impl<T: Config> Pallet<T> {
/// will push the transaction to the pool. Only useful in an offchain
/// context.
pub fn submit_unsigned_equivocation_report(
equivocation_proof: EquivocationProof<T::Header>,
equivocation_proof: EquivocationProof<HeaderFor<T>>,
key_owner_proof: T::KeyOwnerProof,
) -> Option<()> {
T::EquivocationReportSystem::publish_evidence((equivocation_proof, key_owner_proof)).ok()
@@ -901,12 +902,14 @@ impl<T: Config> OnTimestampSet<T::Moment> for Pallet<T> {
}
}
impl<T: Config> frame_support::traits::EstimateNextSessionRotation<T::BlockNumber> for Pallet<T> {
fn average_session_length() -> T::BlockNumber {
impl<T: Config> frame_support::traits::EstimateNextSessionRotation<BlockNumberFor<T>>
for Pallet<T>
{
fn average_session_length() -> BlockNumberFor<T> {
T::EpochDuration::get().saturated_into()
}
fn estimate_current_session_progress(_now: T::BlockNumber) -> (Option<Permill>, Weight) {
fn estimate_current_session_progress(_now: BlockNumberFor<T>) -> (Option<Permill>, Weight) {
let elapsed = CurrentSlot::<T>::get().saturating_sub(Self::current_epoch_start()) + 1;
(
@@ -916,7 +919,9 @@ impl<T: Config> frame_support::traits::EstimateNextSessionRotation<T::BlockNumbe
)
}
fn estimate_next_session_rotation(now: T::BlockNumber) -> (Option<T::BlockNumber>, Weight) {
fn estimate_next_session_rotation(
now: BlockNumberFor<T>,
) -> (Option<BlockNumberFor<T>>, Weight) {
(
Self::next_expected_epoch_change(now),
// Read: Current Slot, Epoch Index, Genesis Slot
@@ -925,8 +930,8 @@ impl<T: Config> frame_support::traits::EstimateNextSessionRotation<T::BlockNumbe
}
}
impl<T: Config> frame_support::traits::Lateness<T::BlockNumber> for Pallet<T> {
fn lateness(&self) -> T::BlockNumber {
impl<T: Config> frame_support::traits::Lateness<BlockNumberFor<T>> for Pallet<T> {
fn lateness(&self) -> BlockNumberFor<T> {
Self::lateness()
}
}
+2 -7
View File
@@ -42,14 +42,10 @@ use sp_staking::{EraIndex, SessionIndex};
type DummyValidatorId = u64;
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,
Authorship: pallet_authorship,
@@ -70,14 +66,13 @@ impl frame_system::Config for Test {
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Index = u64;
type BlockNumber = u64;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Version = ();
type Hashing = sp_runtime::traits::BlakeTwo256;
type AccountId = DummyValidatorId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type PalletInfo = PalletInfo;
+9 -8
View File
@@ -22,6 +22,7 @@ use super::{
AuthorVrfRandomness, Config, EpochStart, NextRandomness, Randomness, RANDOMNESS_LENGTH,
};
use frame_support::traits::Randomness as RandomnessT;
use frame_system::pallet_prelude::BlockNumberFor;
use sp_runtime::traits::{Hash, One, Saturating};
/// Randomness usable by consensus protocols that **depend** upon finality and take action
@@ -129,8 +130,8 @@ pub struct ParentBlockRandomness<T>(sp_std::marker::PhantomData<T>);
Please use `ParentBlockRandomness` instead.")]
pub struct CurrentBlockRandomness<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> RandomnessT<T::Hash, T::BlockNumber> for RandomnessFromTwoEpochsAgo<T> {
fn random(subject: &[u8]) -> (T::Hash, T::BlockNumber) {
impl<T: Config> RandomnessT<T::Hash, BlockNumberFor<T>> for RandomnessFromTwoEpochsAgo<T> {
fn random(subject: &[u8]) -> (T::Hash, BlockNumberFor<T>) {
let mut subject = subject.to_vec();
subject.reserve(RANDOMNESS_LENGTH);
subject.extend_from_slice(&Randomness::<T>::get()[..]);
@@ -139,8 +140,8 @@ impl<T: Config> RandomnessT<T::Hash, T::BlockNumber> for RandomnessFromTwoEpochs
}
}
impl<T: Config> RandomnessT<T::Hash, T::BlockNumber> for RandomnessFromOneEpochAgo<T> {
fn random(subject: &[u8]) -> (T::Hash, T::BlockNumber) {
impl<T: Config> RandomnessT<T::Hash, BlockNumberFor<T>> for RandomnessFromOneEpochAgo<T> {
fn random(subject: &[u8]) -> (T::Hash, BlockNumberFor<T>) {
let mut subject = subject.to_vec();
subject.reserve(RANDOMNESS_LENGTH);
subject.extend_from_slice(&NextRandomness::<T>::get()[..]);
@@ -149,8 +150,8 @@ impl<T: Config> RandomnessT<T::Hash, T::BlockNumber> for RandomnessFromOneEpochA
}
}
impl<T: Config> RandomnessT<Option<T::Hash>, T::BlockNumber> for ParentBlockRandomness<T> {
fn random(subject: &[u8]) -> (Option<T::Hash>, T::BlockNumber) {
impl<T: Config> RandomnessT<Option<T::Hash>, BlockNumberFor<T>> for ParentBlockRandomness<T> {
fn random(subject: &[u8]) -> (Option<T::Hash>, BlockNumberFor<T>) {
let random = AuthorVrfRandomness::<T>::get().map(|random| {
let mut subject = subject.to_vec();
subject.reserve(RANDOMNESS_LENGTH);
@@ -164,8 +165,8 @@ impl<T: Config> RandomnessT<Option<T::Hash>, T::BlockNumber> for ParentBlockRand
}
#[allow(deprecated)]
impl<T: Config> RandomnessT<Option<T::Hash>, T::BlockNumber> for CurrentBlockRandomness<T> {
fn random(subject: &[u8]) -> (Option<T::Hash>, T::BlockNumber) {
impl<T: Config> RandomnessT<Option<T::Hash>, BlockNumberFor<T>> for CurrentBlockRandomness<T> {
fn random(subject: &[u8]) -> (Option<T::Hash>, BlockNumberFor<T>) {
let (random, _) = ParentBlockRandomness::<T>::random(subject);
(random, <frame_system::Pallet<T>>::block_number())
}