Skip to content

Commit

Permalink
Assign unique storage names to pallets. (paritytech#5010)
Browse files Browse the repository at this point in the history
* Assign unique storage names to pallets.

* Bump spec

* Upgrade logic for finality tracker (untested)

* Logic for migrating Identity (untested)

* Logic for migrating transaction-payment

* Fix tests

* Fix `decl_storage` build

* Contract -> Contracts

* Update Cargo.lock

* bump spec

* update migration

* Fix merge error

* Migration for contracts

* Remove serde

* Remove some illegal spaces and Options

* Fix types in identity.

* Minor variable rename

Co-authored-by: Gavin Wood <gavin@parity.io>
  • Loading branch information
2 people authored and apopiak committed Jun 11, 2020
1 parent aec1f45 commit f25d401
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 1 deletion.
5 changes: 5 additions & 0 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ mod rent;

#[cfg(test)]
mod tests;
mod migration;

use crate::exec::ExecutionContext;
use crate::account_db::{AccountDb, DirectAccountDb};
Expand Down Expand Up @@ -578,6 +579,10 @@ decl_module! {
T::Currency::deposit_into_existing(&rewarded, T::SurchargeReward::get())?;
}
}

fn on_runtime_upgrade() {
migration::on_runtime_upgrade::<T>()
}
}
}

Expand Down
62 changes: 62 additions & 0 deletions frame/contracts/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2020 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 <http://www.gnu.org/licenses/>.

//! Migration code to update storage.

use super::*;
use frame_support::storage::migration::{put_storage_value, take_storage_value, StorageIterator};

pub fn on_runtime_upgrade<T: Trait>() {
change_name_contract_to_contracts::<T>()
}

// Change the storage name used by this pallet from `Contract` to `Contracts`.
//
// Since the format of the storage items themselves have not changed, we do not
// need to keep track of a storage version. If the runtime does not need to be
// upgraded, nothing here will happen anyway.

fn change_name_contract_to_contracts<T: Trait>() {
sp_runtime::print("Migrating Contracts.");

if let Some(gas_spent) = take_storage_value::<Gas>(b"Contract", b"GasSpent", &[]) {
put_storage_value(b"Contracts", b"GasSpent", &[], gas_spent);
}

if let Some(current_schedule) = take_storage_value::<Schedule>(b"Contract", b"CurrentSchedule", &[]) {
put_storage_value(b"Contracts", b"CurrentSchedule", &[], current_schedule);
}

for (hash, pristine_code) in StorageIterator::<Vec<u8>>::new(b"Contract", b"PristineCode").drain() {
put_storage_value(b"Contracts", b"PristineCode", &hash, pristine_code);
}

for (hash, code_storage) in StorageIterator::<wasm::PrefabWasmModule>::new(b"Contract", b"CodeStorage").drain() {
put_storage_value(b"Contracts", b"CodeStorage", &hash, code_storage);
}

if let Some(current_schedule) = take_storage_value::<u64>(b"Contract", b"AccountCounter", &[]) {
put_storage_value(b"Contracts", b"AccountCounter", &[], current_schedule);
}

for (hash, contract_info_of) in StorageIterator::<ContractInfo<T>>::new(b"Contract", b"ContractInfoOf").drain() {
put_storage_value(b"Contracts", b"ContractInfoOf", &hash, contract_info_of);
}

if let Some(get_price) = take_storage_value::<BalanceOf<T>>(b"Contract", b"GetPrice", &[]) {
put_storage_value(b"Contracts", b"GetPrice", &[], get_price);
}
}
6 changes: 6 additions & 0 deletions frame/finality-tracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use frame_support::weights::{DispatchClass};
use frame_system::{ensure_none, Trait as SystemTrait};
use sp_finality_tracker::{INHERENT_IDENTIFIER, FinalizedInherentData};

mod migration;

pub const DEFAULT_WINDOW_SIZE: u32 = 101;
pub const DEFAULT_REPORT_LATENCY: u32 = 1000;

Expand Down Expand Up @@ -92,6 +94,10 @@ decl_module! {
fn on_finalize() {
Self::update_hint(<Self as Store>::Update::take())
}

fn on_runtime_upgrade() {
migration::on_runtime_upgrade::<T>()
}
}
}

Expand Down
54 changes: 54 additions & 0 deletions frame/finality-tracker/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 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 <http://www.gnu.org/licenses/>.

// Migration code to update storage.

use super::*;
use frame_support::storage::migration::{put_storage_value, take_storage_value};

pub fn on_runtime_upgrade<T: Trait>() {
change_name_timestamp_to_finality_tracker::<T>()
}

// Change the storage name used by this pallet from `Timestamp` to `FinalityTracker`.
//
// Since the format of the storage items themselves have not changed, we do not
// need to keep track of a storage version. If the runtime does not need to be
// upgraded, nothing here will happen anyway.

fn change_name_timestamp_to_finality_tracker<T:Trait>() {
sp_runtime::print("Migrating Finality Tracker.");

if let Some(recent_hints) = take_storage_value::<Vec<T::BlockNumber>>(b"Timestamp", b"RecentHints", &[]) {
put_storage_value(b"FinalityTracker", b"RecentHints", &[], recent_hints);
}

if let Some(ordered_hints) = take_storage_value::<Vec<T::BlockNumber>>(b"Timestamp", b"OrderedHints", &[]) {
put_storage_value(b"FinalityTracker", b"OrderedHints", &[], ordered_hints);
}

if let Some(median) = take_storage_value::<T::BlockNumber>(b"Timestamp", b"Median", &[]) {
put_storage_value(b"FinalityTracker", b"Median", &[], median);
}

if let Some(update) = take_storage_value::<T::BlockNumber>(b"Timestamp", b"Update", &[]) {
put_storage_value(b"FinalityTracker", b"Update", &[], update);
}

if let Some(initialized) = take_storage_value::<bool>(b"Timestamp", b"Initialized", &[]) {
put_storage_value(b"FinalityTracker", b"Initialized", &[], initialized);
}
}
8 changes: 7 additions & 1 deletion frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ use frame_support::{
};
use frame_system::{self as system, ensure_signed, ensure_root};

mod benchmarking;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
mod migration;

type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
Expand Down Expand Up @@ -1132,6 +1134,10 @@ decl_module! {
id.info.additional.len() as Weight // X
)).into())
}

fn on_runtime_upgrade() {
migration::on_runtime_upgrade::<T>()
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions frame/identity/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2020 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 <http://www.gnu.org/licenses/>.

//! Migration code to update storage.

use super::*;
use frame_support::storage::migration::{put_storage_value, take_storage_value, StorageIterator};

pub fn on_runtime_upgrade<T: Trait>() {
change_name_sudo_to_identity::<T>()
}

// Change the storage name used by this pallet from `Sudo` to `Identity`.
//
// Since the format of the storage items themselves have not changed, we do not
// need to keep track of a storage version. If the runtime does not need to be
// upgraded, nothing here will happen anyway.

fn change_name_sudo_to_identity<T: Trait>() {
sp_runtime::print("Migrating Identity.");

for (hash, identity_of) in StorageIterator::<Registration<BalanceOf<T>>>::new(b"Sudo", b"IdentityOf").drain() {
put_storage_value(b"Identity", b"IdentityOf", &hash, identity_of);
}

for (hash, super_of) in StorageIterator::<(T::AccountId, Data)>::new(b"Sudo", b"SuperOf").drain() {
put_storage_value(b"Identity", b"SuperOf", &hash, super_of);
}

for (hash, subs_of) in StorageIterator::<(BalanceOf<T>, Vec<T::AccountId>)>::new(b"Sudo", b"SubsOf").drain() {
put_storage_value(b"Identity", b"SubsOf", &hash, subs_of);
}

if let Some(registrars) = take_storage_value::<Vec<Option<RegistrarInfo<BalanceOf<T>, T::AccountId>>>>(b"Sudo", b"Registrars", &[]) {
put_storage_value(b"Identity", b"Registrars", &[], registrars);
}

sp_runtime::print("Done Identity.");
}
6 changes: 6 additions & 0 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ use sp_runtime::{
};
use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;

mod migration;

type Multiplier = Fixed128;
type BalanceOf<T> =
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
Expand Down Expand Up @@ -101,6 +103,10 @@ decl_module! {
*fm = T::FeeMultiplierUpdate::convert(*fm);
});
}

fn on_runtime_upgrade() {
migration::on_runtime_upgrade()
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions frame/transaction-payment/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2020 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 <http://www.gnu.org/licenses/>.

//! Migration code to update storage.

use super::*;
use frame_support::storage::migration::{put_storage_value, take_storage_value};

pub fn on_runtime_upgrade() {
change_name_balances_to_transaction_payment()
}

// Change the storage name used by this pallet from `Balances` to `TransactionPayment`.
//
// Since the format of the storage items themselves have not changed, we do not
// need to keep track of a storage version. If the runtime does not need to be
// upgraded, nothing here will happen anyway.

fn change_name_balances_to_transaction_payment() {
sp_runtime::print("Migrating Transaction Payment.");

if let Some(next_fee_multiplier) = take_storage_value::<Multiplier>(b"Balances", b"NextFeeMultiplier", &[]) {
put_storage_value(b"TransactionPayment", b"NextFeeMultiplier", &[], next_fee_multiplier);
}
}

0 comments on commit f25d401

Please sign in to comment.