Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions pallets/msa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ codec = {package = "parity-scale-codec", version = "3.1.5", default-features = f
frame-benchmarking = {default-features = false, git = "https://github.com/paritytech/substrate.git", optional = true, branch = "polkadot-v0.9.27" }
frame-support = {default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
frame-system = {default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
log = { version = "0.4.17", default-features = false }
scale-info = {version = "2.1.2", default-features = false, features = ["derive"]}
sp-core = {default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
sp-runtime = {default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
Expand Down
17 changes: 16 additions & 1 deletion pallets/msa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ mod mock;
#[cfg(test)]
mod tests;

/// Import migrations module which executes migrations when the runtime is upgraded.
pub mod migrations;

pub mod weights;

pub use weights::*;
Expand All @@ -97,6 +100,10 @@ use sp_std::prelude::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::traits::StorageVersion;

// Upgrade storage version from 0 to 1
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[pallet::config]
pub trait Config: frame_system::Config {
Expand All @@ -123,6 +130,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

/// Storage type for MSA identifier
Expand Down Expand Up @@ -163,10 +171,17 @@ pub mod pallet {
/// - Key: AccountId
/// - Value: [`MessageSourceId`]
#[pallet::storage]
#[pallet::getter(fn get_msa_by_account_id)]
pub type MessageSourceIdOf<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, MessageSourceId, OptionQuery>;

/// Migrated Storage type for key to MSA information
/// - Key: AccountId
/// - Value: [`MessageSourceId`]
#[pallet::storage]
#[pallet::getter(fn get_msa_by_account_id)]
pub type MessageSourceMigratedIdOf<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, MessageSourceId, OptionQuery>;

/// Storage for MSA keys
/// - Key: MSA Id
/// - Value: List of Keys
Expand Down
2 changes: 2 additions & 0 deletions pallets/msa/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Version 1.
pub mod v1;
69 changes: 69 additions & 0 deletions pallets/msa/src/migrations/v1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// Trivial migration which moves MessageSourceIdOf -> MessageSourceMigratedIdOf
///
/// Note: The depositor is not optional since he can never change.
use crate::*;

use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
use log;
/// Struct on which to implement OnRuntimeUpgrade trait
pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
/// Perform a module upgrade.
///
/// # Warning
///
/// This function will be called before we initialized any runtime state, aka `on_initialize`
/// wasn't called yet. So, information like the block number and any other
/// block local data are not accessible.
///
/// Return the non-negotiable weight consumed for runtime upgrade.
fn on_runtime_upgrade() -> Weight {
let current = Pallet::<T>::current_storage_version();
let onchain = Pallet::<T>::on_chain_storage_version();
log::info!(
"Running MSA migration with current storage version {:?} / onchain {:?}",
current,
onchain
);

// Execute the migration when upgrading MSA storage version from version 0 to version 1
if current == 1 && onchain == 0 {
// Iterate through AccountId in MessageSourceIdOf and
// copy the key/value pair to MessageSourceMigratedIdOf
let mut count = 0;
for (account_id, msa_id) in MessageSourceIdOf::<T>::iter() {
MessageSourceMigratedIdOf::<T>::insert(account_id, msa_id);
count += 1;
}

// put the current storage version into storage
current.put::<Pallet<T>>();
log::info!("Migrated MessageSourceIdOf storage to MessageSourceMigratedIdOf");

// Return the weight to reflect the cost of iterating through MessageSourceIdOf
// writing to MessageSourceMigratedIdOf, read the on chain storage version, and update the on chain storage version
T::DbWeight::get().reads_writes(count + 1, count + 1)
} else {
log::info!("MigrateToV1 has already been completed and can be removed.");

// The weight cost to read the on chain storage version
T::DbWeight::get().reads(1)
}
}

/// Execute some pre-checks prior to a runtime upgrade.
///
/// This hook is never meant to be executed on-chain but is meant to be used by testing tools.
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
Ok(())
}

/// Execute some post-checks after a runtime upgrade.
///
/// This hook is never meant to be executed on-chain but is meant to be used by testing tools.
#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
Ok(())
}
}
7 changes: 6 additions & 1 deletion runtime/frequency-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
Migrations,
>;

// All migrations executed on runtime upgrade as a nested tuple of types implementing
// `OnRuntimeUpgrade`.
type Migrations = (pallet_msa::migrations::v1::MigrateToV1<Runtime>,);

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
Expand Down Expand Up @@ -190,7 +195,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("frequency-rococo"),
impl_name: create_runtime_str!("frequency-rococo"),
authoring_version: 1,
spec_version: 1,
spec_version: 2,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
2 changes: 1 addition & 1 deletion runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("frequency"),
impl_name: create_runtime_str!("frequency"),
authoring_version: 1,
spec_version: 1,
spec_version: 2,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down