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 10 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
11 changes: 3 additions & 8 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions substrate/frame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ frame-system = { workspace = true }

# primitive types used for developing FRAME runtimes.
sp-api = { optional = true, workspace = true }
sp-application-crypto = { optional = true, workspace = true }
sp-block-builder = { optional = true, workspace = true }
sp-consensus-aura = { optional = true, workspace = true }
sp-consensus-grandpa = { optional = true, workspace = true }
Expand All @@ -45,6 +46,7 @@ sp-inherents = { optional = true, workspace = true }
sp-keyring = { optional = true, workspace = true }
sp-offchain = { optional = true, workspace = true }
sp-session = { optional = true, workspace = true }
sp-staking = { optional = true, workspace = true }
sp-storage = { optional = true, workspace = true }
sp-transaction-pool = { optional = true, workspace = true }
sp-version = { optional = true, workspace = true }
Expand Down Expand Up @@ -72,6 +74,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 @@ -80,6 +83,7 @@ runtime = [
"sp-keyring",
"sp-offchain",
"sp-session",
"sp-staking",
"sp-storage",
"sp-transaction-pool",
"sp-version",
Expand All @@ -96,6 +100,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 All @@ -107,6 +112,7 @@ std = [
"sp-keyring?/std",
"sp-offchain?/std",
"sp-runtime/std",
"sp-staking?/std",
"sp-session?/std",
"sp-storage/std",
"sp-transaction-pool?/std",
Expand All @@ -118,6 +124,7 @@ runtime-benchmarks = [
"frame-system-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
]
try-runtime = [
"frame-executive/try-runtime",
Expand Down
30 changes: 5 additions & 25 deletions substrate/frame/im-online/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
pallet-authorship = { workspace = true }
scale-info = { features = ["derive", "serde"], 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 }
frame = { workspace = true, features = ["experimental", "runtime"] }
pallet-authorship = { workspace = true }

[dev-dependencies]
pallet-session = { workspace = true, default-features = true }
Expand All @@ -36,30 +29,17 @@ 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",
"sp-staking/runtime-benchmarks",
"frame/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",
]
16 changes: 3 additions & 13 deletions substrate/frame/im-online/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,15 @@

#![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;

pub fn create_heartbeat<T: Config>(
k: u32,
) -> Result<
(
crate::Heartbeat<frame_system::pallet_prelude::BlockNumberFor<T>>,
<T::AuthorityId as RuntimeAppPublic>::Signature,
),
(crate::Heartbeat<BlockNumberFor<T>>, <T::AuthorityId as RuntimeAppPublic>::Signature),
&'static str,
> {
let mut keys = Vec::new();
Expand All @@ -49,7 +39,7 @@ pub fn create_heartbeat<T: Config>(
Keys::<T>::put(bounded_keys);

let input_heartbeat = Heartbeat {
block_number: frame_system::pallet_prelude::BlockNumberFor::<T>::zero(),
block_number: BlockNumberFor::<T>::zero(),
session_index: 0,
authority_index: k - 1,
validators_len: keys.len() as u32,
Expand Down
49 changes: 14 additions & 35 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,39 +85,19 @@ 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::*;
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,
};

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 +111,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 +228,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 +345,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 +400,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 +543,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 +714,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 +784,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
16 changes: 5 additions & 11 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::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 All @@ -42,7 +36,7 @@ mod v0 {
pub external_addresses: Vec<Vec<u8>>,
}

#[storage_alias]
#[frame::storage_alias]
pub(super) type ReceivedHeartbeats<T: Config> = StorageDoubleMap<
Pallet<T>,
Twox64Concat,
Expand All @@ -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,15 @@ 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;
use frame::testing_prelude::*;

#[test]
fn migration_works() {
Expand Down
Loading
Loading