Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate pallet-aura to umbrella crate #6622

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions prdoc/pr_6622.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: migrate pallet-aura to umbrella crate

doc:
- audience: Runtime Dev
description: |
Imports frame umbrella crate systems into pallet-aura and
includes BoundedSlice as a runtime::prelude export from the
polkadot-sdk-frame crate.

crates:
- name: pallet-aura
bump: minor
- name: polkadot-sdk-frame
bump: minor
18 changes: 3 additions & 15 deletions substrate/frame/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,23 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { features = ["derive", "max-encoded-len"], workspace = true }
log = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }
pallet-timestamp = { workspace = true }
sp-application-crypto = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-runtime = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }
sp-io = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"frame/std",
"log/std",
"pallet-timestamp/std",
"scale-info/std",
"sp-application-crypto/std",
"sp-consensus-aura/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame/try-runtime",
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime",
]
40 changes: 22 additions & 18 deletions substrate/frame/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@ extern crate alloc;

use alloc::vec::Vec;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
traits::{DisabledValidators, FindAuthor, Get, OnTimestampSet, OneSessionHandler},
BoundedSlice, BoundedVec, ConsensusEngineId, Parameter,
use frame::{
deps::{
frame_support::{ConsensusEngineId, Parameter},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read the new checklist named in the issue, and update the PR to adhere to it. Thank you!

sp_runtime::{generic::DigestItem, BoundToRuntimeAppPublic, RuntimeAppPublic},
},
prelude::*,
runtime::prelude::{BoundedSlice, BoundedVec},
traits::{
DisabledValidators, FindAuthor, Get, IsMember, OnTimestampSet, OneSessionHandler,
SaturatedConversion, Saturating, Zero,
},
};
use log;
use sp_consensus_aura::{AuthorityIndex, ConsensusLog, Slot, AURA_ENGINE_ID};
use sp_runtime::{
generic::DigestItem,
traits::{IsMember, Member, SaturatedConversion, Saturating, Zero},
RuntimeAppPublic,
};

pub mod migrations;
mod mock;
Expand All @@ -76,11 +79,9 @@ impl<T: pallet_timestamp::Config> Get<T::Moment> for MinimumPeriodTimesTwo<T> {
}
}

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::config]
pub trait Config: pallet_timestamp::Config + frame_system::Config {
Expand Down Expand Up @@ -157,7 +158,7 @@ pub mod pallet {
}

#[cfg(feature = "try-runtime")]
fn try_state(_: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
fn try_state(_: BlockNumberFor<T>) -> Result<(), frame::deps::sp_runtime::TryRuntimeError> {
Self::do_try_state()
}
}
Expand All @@ -174,7 +175,7 @@ pub mod pallet {
pub type CurrentSlot<T: Config> = StorageValue<_, Slot, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
#[derive(frame::derive::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub authorities: Vec<T::AuthorityId>,
}
Expand Down Expand Up @@ -265,7 +266,7 @@ impl<T: Config> Pallet<T> {
/// * The number of authorities must be less than or equal to `T::MaxAuthorities`. This however,
/// is guarded by the type system.
#[cfg(any(test, feature = "try-runtime"))]
pub fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
pub fn do_try_state() -> Result<(), frame::deps::sp_runtime::TryRuntimeError> {
// We don't have any guarantee that we are already after `on_initialize` and thus we have to
// check the current slot from the digest or take the last known slot.
let current_slot =
Expand All @@ -274,7 +275,7 @@ impl<T: Config> Pallet<T> {
// Check that the current slot is less than the maximal slot number, unless we allow for
// multiple blocks per slot.
if !T::AllowMultipleBlocksPerSlot::get() {
frame_support::ensure!(
frame::deps::frame_support::ensure!(
current_slot < u64::MAX,
"Current slot has reached maximum value and cannot be incremented further.",
);
Expand All @@ -284,11 +285,14 @@ impl<T: Config> Pallet<T> {
<Authorities<T>>::decode_len().ok_or("Failed to decode authorities length")?;

// Check that the authorities are non-empty.
frame_support::ensure!(!authorities_len.is_zero(), "Authorities must be non-empty.");
frame::deps::frame_support::ensure!(
!authorities_len.is_zero(),
"Authorities must be non-empty."
);

// Check that the current authority is not disabled.
let authority_index = *current_slot % authorities_len as u64;
frame_support::ensure!(
frame::deps::frame_support::ensure!(
!T::DisabledValidators::is_disabled(authority_index as u32),
"Current validator is disabled and should not be attempting to author blocks.",
);
Expand All @@ -297,7 +301,7 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
impl<T: Config> BoundToRuntimeAppPublic for Pallet<T> {
type Public = T::AuthorityId;
}

Expand Down
8 changes: 6 additions & 2 deletions substrate/frame/aura/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

//! Migrations for the AURA pallet.

use frame_support::{pallet_prelude::*, traits::Get, weights::Weight};
use frame::deps::frame_support::{
pallet_prelude::*,
traits::{Get, StorageInstance},
weights::Weight,
};

struct __LastTimestamp<T>(core::marker::PhantomData<T>);
impl<T: RemoveLastTimestamp> frame_support::traits::StorageInstance for __LastTimestamp<T> {
impl<T: RemoveLastTimestamp> StorageInstance for __LastTimestamp<T> {
fn pallet_prefix() -> &'static str {
T::PalletPrefix::get()
}
Expand Down
11 changes: 6 additions & 5 deletions substrate/frame/aura/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
#![cfg(test)]

use crate as pallet_aura;
use frame_support::{
derive_impl, parameter_types,
use frame::{
deps::sp_runtime::{testing::UintAuthorityId, BuildStorage},
prelude::*,
runtime::prelude::{construct_runtime, derive_impl, parameter_types},
traits::{ConstU32, ConstU64, DisabledValidators},
};
use sp_consensus_aura::{ed25519::AuthorityId, AuthorityIndex};
use sp_runtime::{testing::UintAuthorityId, BuildStorage};

type Block = frame_system::mocking::MockBlock<Test>;

const SLOT_DURATION: u64 = 2;

frame_support::construct_runtime!(
construct_runtime!(
pub enum Test
{
System: frame_system,
Expand Down Expand Up @@ -87,7 +88,7 @@ impl pallet_aura::Config for Test {
type SlotDuration = ConstU64<SLOT_DURATION>;
}

fn build_ext(authorities: Vec<u64>) -> sp_io::TestExternalities {
fn build_ext(authorities: Vec<u64>) -> frame::testing_prelude::TestExternalities {
let mut storage = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_aura::GenesisConfig::<Test> {
authorities: authorities.into_iter().map(|a| UintAuthorityId(a).to_public_key()).collect(),
Expand Down
6 changes: 4 additions & 2 deletions substrate/frame/aura/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
use super::pallet;
use crate::mock::{build_ext_and_execute_test, Aura, MockDisabledValidators, System, Test};
use codec::Encode;
use frame_support::traits::OnInitialize;
use frame::deps::{
frame_support::traits::OnInitialize,
sp_runtime::{Digest, DigestItem},
};
use sp_consensus_aura::{Slot, AURA_ENGINE_ID};
use sp_runtime::{Digest, DigestItem};

#[test]
fn initial_values() {
Expand Down
3 changes: 3 additions & 0 deletions substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ pub mod runtime {
/// Consider using the new version of this [`frame_construct_runtime`].
pub use frame_support::construct_runtime;

/// Related to runtime construction.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not relevant for me

pub use frame_support::{BoundedSlice, BoundedVec};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you added them just to use the runtime prelude in the aura pallet, I think it's better to change it to the main prelude (see comment) and remove this from here


/// Macro to amalgamate the runtime into `struct Runtime`.
///
/// This is the newer version of [`construct_runtime`].
Expand Down