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-im-online to umbrella crate #6736

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 2 additions & 7 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions substrate/frame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ sp-genesis-builder = { optional = true, workspace = true }
sp-inherents = { optional = true, workspace = true }
sp-storage = { optional = true, workspace = true }
sp-keyring = { optional = true, workspace = true }
sp-application-crypto = { optional = true, workspace = true }

frame-executive = { optional = true, workspace = true }
frame-system-rpc-runtime-api = { optional = true, workspace = true }
Expand All @@ -72,6 +73,7 @@ runtime = [
"frame-executive",
"frame-system-rpc-runtime-api",
"sp-api",
"sp-application-crypto",
"sp-block-builder",
"sp-consensus-aura",
"sp-consensus-grandpa",
Expand All @@ -96,6 +98,7 @@ std = [
"log/std",
"scale-info/std",
"sp-api?/std",
"sp-application-crypto?/std",
"sp-arithmetic/std",
"sp-block-builder?/std",
"sp-consensus-aura?/std",
Expand Down
25 changes: 4 additions & 21 deletions substrate/frame/im-online/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { features = ["derive"], workspace = true }
log = { workspace = true }
scale-info = { features = ["derive", "serde"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame = { workspace = true, features = ["experimental", "runtime"] }
pallet-authorship = { workspace = true }
sp-application-crypto = { features = ["serde"], workspace = true }
sp-core = { features = ["serde"], workspace = true }
sp-io = { workspace = true }
sp-runtime = { features = ["serde"], workspace = true }
sp-staking = { features = ["serde"], workspace = true }

[dev-dependencies]
Expand All @@ -36,30 +30,19 @@ pallet-session = { workspace = true, default-features = true }
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"frame/std",
"log/std",
"pallet-authorship/std",
"pallet-session/std",
"scale-info/std",
"sp-application-crypto/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-staking/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"frame/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame/try-runtime",
"pallet-authorship/try-runtime",
"pallet-session/try-runtime",
"sp-runtime/try-runtime",
]
9 changes: 1 addition & 8 deletions substrate/frame/im-online/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@

#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::v2::*;
use frame_support::{traits::UnfilteredDispatchable, WeakBoundedVec};
use frame_system::RawOrigin;
use sp_runtime::{
traits::{ValidateUnsigned, Zero},
transaction_validity::TransactionSource,
};

use crate::*;
use frame::benchmarking::prelude::*;

const MAX_KEYS: u32 = 1000;

Expand Down
45 changes: 15 additions & 30 deletions substrate/frame/im-online/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@
//!
//! ```
//! use pallet_im_online::{self as im_online};
//! use frame::prelude::*;
//!
//! #[frame_support::pallet]
//! #[frame::pallet]
//! pub mod pallet {
//! use super::*;
//! use frame_support::pallet_prelude::*;
//! use frame_system::pallet_prelude::*;
//!
//! #[pallet::pallet]
//! pub struct Pallet<T>(_);
Expand Down Expand Up @@ -86,26 +85,12 @@ extern crate alloc;

use alloc::{vec, vec::Vec};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
pallet_prelude::*,
traits::{
EstimateNextSessionRotation, Get, OneSessionHandler, ValidatorSet,
ValidatorSetWithIdentification,
},
BoundedSlice, WeakBoundedVec,
};
use frame_system::{
offchain::{CreateInherent, SubmitTransaction},
pallet_prelude::*,
use frame::{
prelude::*,
traits::{ValidatorSet, ValidatorSetWithIdentification},
};
pub use pallet::*;
use scale_info::TypeInfo;
use sp_application_crypto::RuntimeAppPublic;
use sp_runtime::{
offchain::storage::{MutateStorageError, StorageRetrievalError, StorageValueRef},
traits::{AtLeast32BitUnsigned, Convert, Saturating, TrailingZeroInput},
PerThing, Perbill, Permill, RuntimeDebug, SaturatedConversion,
};
use sp_staking::{
offence::{Kind, Offence, ReportOffence},
SessionIndex,
Expand All @@ -114,11 +99,11 @@ pub use weights::WeightInfo;

pub mod sr25519 {
mod app_sr25519 {
use sp_application_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
use frame::deps::sp_application_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
app_crypto!(sr25519, IM_ONLINE);
}

sp_application_crypto::with_pair! {
frame::deps::sp_application_crypto::with_pair! {
/// An i'm online keypair using sr25519 as its crypto.
pub type AuthorityPair = app_sr25519::Pair;
}
Expand All @@ -132,11 +117,11 @@ pub mod sr25519 {

pub mod ed25519 {
mod app_ed25519 {
use sp_application_crypto::{app_crypto, ed25519, key_types::IM_ONLINE};
use frame::deps::sp_application_crypto::{app_crypto, ed25519, key_types::IM_ONLINE};
app_crypto!(ed25519, IM_ONLINE);
}

sp_application_crypto::with_pair! {
frame::deps::sp_application_crypto::with_pair! {
/// An i'm online keypair using ed25519 as its crypto.
pub type AuthorityPair = app_ed25519::Pair;
}
Expand Down Expand Up @@ -249,7 +234,7 @@ pub type IdentificationTuple<T> = (

type OffchainResult<T, A> = Result<A, OffchainErr<BlockNumberFor<T>>>;

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

Expand Down Expand Up @@ -366,7 +351,7 @@ pub mod pallet {
>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
#[derive(DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub keys: Vec<T::AuthorityId>,
}
Expand Down Expand Up @@ -421,7 +406,7 @@ pub mod pallet {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn offchain_worker(now: BlockNumberFor<T>) {
// Only send messages if we are a potential validator.
if sp_io::offchain::is_validator() {
if offchain::is_validator() {
for res in Self::send_heartbeats(now).into_iter().flatten() {
if let Err(e) = res {
log::debug!(
Expand Down Expand Up @@ -564,7 +549,7 @@ impl<T: Config> Pallet<T> {
let residual = Permill::from_rational(1u32, session_length.saturated_into());
let threshold: Permill = progress.saturating_pow(6).saturating_add(residual);

let seed = sp_io::offchain::random_seed();
let seed = offchain::random_seed();
let random = <u32>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
.expect("input is padded with zeroes; qed");
let random = Permill::from_parts(random % Permill::ACCURACY);
Expand Down Expand Up @@ -735,7 +720,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 Expand Up @@ -805,7 +790,7 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
let validator_set_count = keys.len() as u32;
let offence = UnresponsivenessOffence { session_index, validator_set_count, offenders };
if let Err(e) = T::ReportUnresponsiveness::report_offence(vec![], offence) {
sp_runtime::print(e);
frame::deps::sp_runtime::print(e);
}
}
}
Expand Down
13 changes: 3 additions & 10 deletions substrate/frame/im-online/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@

use super::*;
use alloc::vec::Vec;
use frame_support::{storage_alias, traits::OnRuntimeUpgrade};

#[cfg(feature = "try-runtime")]
use frame_support::ensure;
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use frame::{testing_prelude::*, traits::WrapperOpaque};

/// The log target.
const TARGET: &str = "runtime::im-online::migration::v1";

/// The original data layout of the im-online pallet (`ReceivedHeartbeats` storage item).
mod v0 {
use super::*;
use frame_support::traits::WrapperOpaque;

#[derive(Encode, Decode, Default)]
pub(super) struct BoundedOpaqueNetworkState {
Expand Down Expand Up @@ -61,7 +55,7 @@ pub mod v1 {

impl<T: Config> OnRuntimeUpgrade for Migration<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
fn pre_upgrade() -> Result<Vec<u8>, frame::try_runtime::TryRuntimeError> {
let count = v0::ReceivedHeartbeats::<T>::iter().count();
log::info!(target: TARGET, "Migrating {} received heartbeats", count);

Expand Down Expand Up @@ -128,15 +122,14 @@ pub fn clear_offchain_storage(validator_set_size: u32) {
key.extend(idx.encode());
key
};
sp_runtime::offchain::storage::StorageValueRef::persistent(&key).clear();
StorageValueRef::persistent(&key).clear();
});
}

#[cfg(all(feature = "try-runtime", test))]
mod test {
use super::*;
use crate::mock::{new_test_ext, Runtime as T};
use frame_support::traits::WrapperOpaque;

#[test]
fn migration_works() {
Expand Down
17 changes: 6 additions & 11 deletions substrate/frame/im-online/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@

#![cfg(test)]

use frame_support::{
derive_impl, parameter_types,
traits::{ConstU32, ConstU64},
weights::Weight,
};
use frame::testing_prelude::*;
use pallet_session::historical as pallet_session_historical;
use sp_runtime::{testing::UintAuthorityId, traits::ConvertInto, BuildStorage, Permill};
use sp_staking::{
offence::{OffenceError, ReportOffence},
SessionIndex,
Expand All @@ -36,7 +31,7 @@ use crate::Config;

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

frame_support::construct_runtime!(
construct_runtime!(
pub enum Runtime {
System: frame_system,
Session: pallet_session,
Expand Down Expand Up @@ -73,7 +68,7 @@ impl pallet_session::historical::SessionManager<u64, u64> for TestSessionManager
}

/// An extrinsic type used for tests.
pub type Extrinsic = sp_runtime::testing::TestXt<RuntimeCall, ()>;
pub type Extrinsic = TestXt<RuntimeCall, ()>;
type IdentificationTuple = (u64, u64);
type Offence = crate::UnresponsivenessOffence<IdentificationTuple>;

Expand All @@ -94,9 +89,9 @@ impl ReportOffence<u64, IdentificationTuple, Offence> for OffenceHandler {
}
}

pub fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> TestExternalities {
let t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
let mut result: sp_io::TestExternalities = t.into();
let mut result: TestExternalities = t.into();
// Set the default keys, otherwise session will discard the validator.
result.execute_with(|| {
for i in 1..=6 {
Expand Down Expand Up @@ -150,7 +145,7 @@ parameter_types! {

pub struct TestNextSessionRotation;

impl frame_support::traits::EstimateNextSessionRotation<u64> for TestNextSessionRotation {
impl EstimateNextSessionRotation<u64> for TestNextSessionRotation {
fn average_session_length() -> u64 {
// take the mock result if any and return it
let mock = MockAverageSessionLength::mutate(|p| p.take());
Expand Down
9 changes: 2 additions & 7 deletions substrate/frame/im-online/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@

use super::*;
use crate::mock::*;
use frame_support::{assert_noop, dispatch};
use sp_core::offchain::{
testing::{TestOffchainExt, TestTransactionPoolExt},
OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
};
use sp_runtime::testing::UintAuthorityId;
use frame::testing_prelude::*;

#[test]
fn test_unresponsiveness_slash_fraction() {
Expand Down Expand Up @@ -111,7 +106,7 @@ fn heartbeat(
authority_index: u32,
id: UintAuthorityId,
validators: Vec<u64>,
) -> dispatch::DispatchResult {
) -> DispatchResult {
let heartbeat = Heartbeat {
block_number,
session_index,
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/im-online/src/weights.rs

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

Loading
Loading