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

Reset mmr storage #12915

Closed
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
6 changes: 6 additions & 0 deletions frame/merkle-mountain-range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T, I = ()>(PhantomData<(T, I)>);

/// This pallet's configuration trait
Expand Down
66 changes: 66 additions & 0 deletions frame/merkle-mountain-range/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -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<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
fn on_runtime_upgrade() -> Weight {
let current = Pallet::<T>::current_storage_version();
let onchain = Pallet::<T>::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::<Pallet<T>>();

// reset all storage values
NumberOfLeaves::<T>::set(0);
let res = Nodes::<T>::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<Vec<u8>, &'static str> {
assert_eq!(Pallet::<T>::on_chain_storage_version(), 0);
Ok(Default::default())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
assert_eq!(Pallet::<T>::on_chain_storage_version(), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

maybe also check NumberOfLeaves is 0 and Nodes is empty

Ok(())
}
}
}