Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
remove unnecessary types from v0 mod
Browse files Browse the repository at this point in the history
  • Loading branch information
melekes committed Jun 6, 2023
1 parent c822c2d commit ace0f6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
9 changes: 5 additions & 4 deletions frame/im-online/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ pub mod weights;

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
pallet_prelude::*,
traits::{
EstimateNextSessionRotation, Get, OneSessionHandler, ValidatorSet,
ValidatorSetWithIdentification,
},
BoundedSlice, WeakBoundedVec,
};
use frame_system::offchain::{SendTransactionTypes, SubmitTransaction};
use frame_system::{
offchain::{SendTransactionTypes, SubmitTransaction},
pallet_prelude::*,
};
pub use pallet::*;
use scale_info::TypeInfo;
use sp_application_crypto::RuntimeAppPublic;
Expand All @@ -106,9 +110,6 @@ use sp_staking::{
use sp_std::prelude::*;
pub use weights::WeightInfo;

use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

pub mod sr25519 {
mod app_sr25519 {
use sp_application_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
Expand Down
47 changes: 22 additions & 25 deletions frame/im-online/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sp_runtime::TryRuntimeError;
/// The log target.
const TARGET: &'static str = "runtime::im-online::migration::v1";

/// The original data layout of the im-online pallet without a specific version number.
/// The original data layout of the im-online pallet (`ReceivedHeartbeats` storage item).
mod v0 {
use super::*;
use frame_support::traits::WrapperOpaque;
Expand All @@ -55,13 +55,6 @@ mod v0 {
WeakBoundedVec<WeakBoundedVec<u8, MultiAddrEncodingLimit>, AddressesLimit>,
}

#[storage_alias]
pub(crate) type HeartbeatAfter<T: Config> = StorageValue<Pallet<T>, T::BlockNumber, ValueQuery>;

#[storage_alias]
pub(crate) type Keys<T: Config> =
StorageValue<Pallet<T>, WeakBoundedVec<T::AuthorityId, T::MaxKeys>, ValueQuery>;

#[storage_alias]
pub(crate) type ReceivedHeartbeats<T: Config> = StorageDoubleMap<
Pallet<T>,
Expand All @@ -71,23 +64,12 @@ mod v0 {
AuthIndex,
WrapperOpaque<BoundedOpaqueNetworkState<u32, u32, T::MaxPeerInHeartbeats>>,
>;

#[storage_alias]
pub(crate) type AuthoredBlocks<T: pallet::Config> = StorageDoubleMap<
Pallet<T>,
Twox64Concat,
SessionIndex,
Twox64Concat,
ValidatorId<T>,
u32,
ValueQuery,
>;
}

pub mod v1 {
use super::*;

/// Migration for moving im-online from V0 to V1 storage.
/// Simple migration that replaces `ReceivedHeartbeats` values with `()`.
pub struct Migration<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for Migration<T> {
Expand All @@ -105,17 +87,32 @@ pub mod v1 {
}

fn on_runtime_upgrade() -> Weight {
ReceivedHeartbeats::<T>::translate::<_, _>(
let mut weight = T::DbWeight::get().reads(1);
if StorageVersion::get::<Pallet<T>>() != 0 {
log::warn!(
target: TARGET,
"Skipping migration because current storage version is not 0"
);
return weight
}

let count = v0::ReceivedHeartbeats::<T>::iter().count();
weight.saturating_accrue(
T::DbWeight::get().reads(v0::ReceivedHeartbeats::<T>::iter().count() as u64),
);
weight.saturating_accrue(
T::DbWeight::get().writes(v0::ReceivedHeartbeats::<T>::iter().count() as u64),
);

v0::ReceivedHeartbeats::<T>::translate::<_, _>(
|k: T::SessionIndex, T::AccountId, state: _| {
log::info!(target: TARGET, "Migrated received heartbeat for {:?}...", k);
log::trace!(target: TARGET, "Migrated received heartbeat for {:?}...", k);
Some(())
},
);

StorageVersion::new(1).put::<Pallet<T>>();

let count = ReceivedHeartbeats::<T>::iter().count();
T::DbWeight::get().reads_writes(count as Weight + 1, count as Weight + 1)
weight.saturating_add(T::DbWeight::get().writes(1))
}

#[cfg(feature = "try-runtime")]
Expand Down

0 comments on commit ace0f6e

Please sign in to comment.