// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see .
//! Storage migrations for srml-staking.
/// Indicator of a version of a storage layout.
pub type VersionNumber = u32;
// the current expected version of the storage
pub const CURRENT_VERSION: VersionNumber = 1;
#[cfg(any(test, feature = "migrate"))]
mod inner {
use crate::{Store, Module, Trait};
use support::{StorageLinkedMap, StorageValue};
use rstd::vec::Vec;
use super::{CURRENT_VERSION, VersionNumber};
// the minimum supported version of the migration logic.
const MIN_SUPPORTED_VERSION: VersionNumber = 0;
// migrate storage from v0 to v1.
//
// this upgrades the `Nominators` linked_map value type from `Vec` to
// `Option>`
pub fn to_v1(version: &mut VersionNumber) {
if *version != 0 { return }
*version += 1;
let now = >::current_era();
let res = as Store>::Nominators::translate::, _, _>(
|key| key,
|targets| crate::Nominations {
targets,
submitted_in: now,
suppressed: false,
},
);
if let Err(e) = res {
support::print("Encountered error in migration of Staking::Nominators map.");
if e.is_none() {
support::print("Staking::Nominators map reinitialized");
}
}
support::print("Finished migrating Staking storage to v1.");
}
pub(super) fn perform_migrations() {
as Store>::StorageVersion::mutate(|version| {
if *version < MIN_SUPPORTED_VERSION {
support::print("Cannot migrate staking storage because version is less than\
minimum.");
support::print(*version);
return
}
if *version == CURRENT_VERSION { return }
to_v1::(version);
});
}
}
#[cfg(not(any(test, feature = "migrate")))]
mod inner {
pub(super) fn perform_migrations() { }
}
/// Perform all necessary storage migrations to get storage into the expected stsate for current
/// logic. No-op if fully upgraded.
pub(crate) fn perform_migrations() {
inner::perform_migrations::();
}