diff --git a/frame/merkle-mountain-range/src/lib.rs b/frame/merkle-mountain-range/src/lib.rs index 46af84d218247..6b129472b0e64 100644 --- a/frame/merkle-mountain-range/src/lib.rs +++ b/frame/merkle-mountain-range/src/lib.rs @@ -69,6 +69,8 @@ pub use sp_mmr_primitives::{ self as primitives, utils::NodesUtils, Error, LeafDataProvider, LeafIndex, NodeIndex, }; +pub mod migrations; + #[cfg(feature = "runtime-benchmarks")] mod benchmarking; mod default_weights; @@ -120,8 +122,12 @@ pub mod pallet { use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; + /// The current storage version. + const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + #[pallet::pallet] #[pallet::generate_store(pub(super) trait Store)] + #[pallet::storage_version(STORAGE_VERSION)] pub struct Pallet(PhantomData<(T, I)>); /// This pallet's configuration trait diff --git a/frame/merkle-mountain-range/src/migrations.rs b/frame/merkle-mountain-range/src/migrations.rs new file mode 100644 index 0000000000000..ebb63eefcc02e --- /dev/null +++ b/frame/merkle-mountain-range/src/migrations.rs @@ -0,0 +1,66 @@ +// This file is part of Substrate. + +// Copyright (C) 2022 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub mod v1 { + use frame_support::{ + pallet_prelude::*, + traits::{GetStorageVersion, OnRuntimeUpgrade}, + }; + + use crate::*; + pub struct MigrateToV1(sp_std::marker::PhantomData); + impl OnRuntimeUpgrade for MigrateToV1 { + fn on_runtime_upgrade() -> Weight { + let current = Pallet::::current_storage_version(); + let onchain = Pallet::::on_chain_storage_version(); + + log::info!( + "Running migration with current storage version {:?} / onchain {:?}", + current, + onchain + ); + + if current == 1 && onchain == 0 { + // update on-chain storage version + current.put::>(); + + // reset all storage values + NumberOfLeaves::::set(0); + let res = Nodes::::clear(u32::max_value(), None); + + // (storage version + Nodes::clear, + // storage version write + NumberOfLeaves write + node key removals) + T::DbWeight::get().reads_writes(1 + res.loops as u64, 2 + res.unique as u64) + } else { + log::info!("Migration did not execute. This probably should be removed"); + T::DbWeight::get().reads(1) + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + assert_eq!(Pallet::::on_chain_storage_version(), 0); + Ok(Default::default()) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_: Vec) -> Result<(), &'static str> { + assert_eq!(Pallet::::on_chain_storage_version(), 1); + Ok(()) + } + } +}