diff --git a/substrate/bin/node-template/pallets/template/src/lib.rs b/substrate/bin/node-template/pallets/template/src/lib.rs index 34e055bf546b0b57a247a8cb083a41de7d064508..892778adeb46b431cb46e87dd4e7ac4c1cea129b 100644 --- a/substrate/bin/node-template/pallets/template/src/lib.rs +++ b/substrate/bin/node-template/pallets/template/src/lib.rs @@ -28,6 +28,9 @@ pub trait Trait: system::Trait { // This pallet's storage items. decl_storage! { + // It is important to update your storage name so that your pallet's + // storage items are isolated from other pallets. + // ---------------------------------vvvvvvvvvvvvvv trait Store for Module<T: Trait> as TemplateModule { // Just a dummy storage item. // Here we are declaring a StorageValue, `Something` as a Option<u32> diff --git a/substrate/frame/contracts/src/lib.rs b/substrate/frame/contracts/src/lib.rs index 93e470a51bb83fbccb28dba4709985c22d7362e8..e1022859a1956273b222182ddf2f3356fb30cd47 100644 --- a/substrate/frame/contracts/src/lib.rs +++ b/substrate/frame/contracts/src/lib.rs @@ -98,6 +98,7 @@ mod rent; #[cfg(test)] mod tests; +mod migration; use crate::exec::ExecutionContext; use crate::account_db::{AccountDb, DirectAccountDb}; @@ -666,6 +667,10 @@ decl_module! { fn on_finalize() { GasSpent::kill(); } + + fn on_runtime_upgrade() { + migration::on_runtime_upgrade::<T>() + } } } @@ -923,7 +928,7 @@ decl_event! { } decl_storage! { - trait Store for Module<T: Trait> as Contract { + trait Store for Module<T: Trait> as Contracts { /// Gas spent so far in this block. GasSpent get(fn gas_spent): Gas; /// Current cost schedule for contracts. diff --git a/substrate/frame/contracts/src/migration.rs b/substrate/frame/contracts/src/migration.rs new file mode 100644 index 0000000000000000000000000000000000000000..83d3771c83dbb067d00e378571369b739608c4c4 --- /dev/null +++ b/substrate/frame/contracts/src/migration.rs @@ -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); + } +} diff --git a/substrate/frame/elections/src/lib.rs b/substrate/frame/elections/src/lib.rs index 95d1858476287aa2c809e3d569c1e56b36638c61..d93d18fe23f64c049bc1337acb3f538cbf208ec7 100644 --- a/substrate/frame/elections/src/lib.rs +++ b/substrate/frame/elections/src/lib.rs @@ -209,7 +209,7 @@ pub trait Trait: frame_system::Trait { } decl_storage! { - trait Store for Module<T: Trait> as Council { + trait Store for Module<T: Trait> as Elections { // ---- parameters /// How long to give each top candidate to present themselves after the vote ends. diff --git a/substrate/frame/evm/src/lib.rs b/substrate/frame/evm/src/lib.rs index abf20114f2c6a7f2fb70760ca0de6ee3c45d26c7..df0ddaafbbe164cb1ddaee8d647961f90b996c7e 100644 --- a/substrate/frame/evm/src/lib.rs +++ b/substrate/frame/evm/src/lib.rs @@ -131,7 +131,7 @@ pub trait Trait: frame_system::Trait + pallet_timestamp::Trait { } decl_storage! { - trait Store for Module<T: Trait> as Example { + trait Store for Module<T: Trait> as EVM { Accounts get(fn accounts) config(): map hasher(blake2_256) H160 => Account; AccountCodes: map hasher(blake2_256) H160 => Vec<u8>; AccountStorages: double_map hasher(blake2_256) H160, hasher(blake2_256) H256 => H256; diff --git a/substrate/frame/example-offchain-worker/src/lib.rs b/substrate/frame/example-offchain-worker/src/lib.rs index 1c72a8be68653974803ee7702ae3aa53accdd1b5..d4905d26b00d0866de2bd2544612b5bc458c6965 100644 --- a/substrate/frame/example-offchain-worker/src/lib.rs +++ b/substrate/frame/example-offchain-worker/src/lib.rs @@ -106,7 +106,7 @@ pub trait Trait: frame_system::Trait { } decl_storage! { - trait Store for Module<T: Trait> as Example { + trait Store for Module<T: Trait> as ExampleOffchainWorker { /// A vector of recently submitted prices. /// /// This is used to calculate average price, should have bounded size. diff --git a/substrate/frame/example/src/lib.rs b/substrate/frame/example/src/lib.rs index 826538ae582fe7e7b3073408cd1fd4042eccdf9c..e0d89fc7f39d9d5b9db303984a5dafb13db0cdc6 100644 --- a/substrate/frame/example/src/lib.rs +++ b/substrate/frame/example/src/lib.rs @@ -326,6 +326,10 @@ decl_storage! { // A macro for the Storage trait, and its implementation, for this pallet. // This allows for type-safe usage of the Substrate storage database, so you can // keep things around between blocks. + // + // It is important to update your storage name so that your pallet's + // storage items are isolated from other pallets. + // ---------------------------------vvvvvvv trait Store for Module<T: Trait> as Example { // Any storage declarations of the form: // `pub? Name get(fn getter_name)? [config()|config(myname)] [build(|_| {...})] : <type> (= <new_default_value>)?;` diff --git a/substrate/frame/finality-tracker/src/lib.rs b/substrate/frame/finality-tracker/src/lib.rs index 08056a34ab028f759ccae0705065c80d3283d689..3b6de38de8e79ea025c6b6ecc0e89a70189c159a 100644 --- a/substrate/frame/finality-tracker/src/lib.rs +++ b/substrate/frame/finality-tracker/src/lib.rs @@ -26,6 +26,8 @@ use frame_support::traits::Get; 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; @@ -40,7 +42,7 @@ pub trait Trait: SystemTrait { } decl_storage! { - trait Store for Module<T: Trait> as Timestamp { + trait Store for Module<T: Trait> as FinalityTracker { /// Recent hints. RecentHints get(fn recent_hints) build(|_| vec![T::BlockNumber::zero()]): Vec<T::BlockNumber>; /// Ordered recent hints. @@ -89,6 +91,10 @@ decl_module! { fn on_finalize() { Self::update_hint(<Self as Store>::Update::take()) } + + fn on_runtime_upgrade() { + migration::on_runtime_upgrade::<T>() + } } } diff --git a/substrate/frame/finality-tracker/src/migration.rs b/substrate/frame/finality-tracker/src/migration.rs new file mode 100644 index 0000000000000000000000000000000000000000..1eff123db370e9ca8aac909074b5f9cb5859f032 --- /dev/null +++ b/substrate/frame/finality-tracker/src/migration.rs @@ -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); + } +} diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index cb2071d5d9ea872876d1a66e8c432341333de0b5..e993a0bebcdbdb449c0387b0c8a2558c524188a8 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -80,6 +80,7 @@ use frame_system::{self as system, ensure_signed, ensure_root}; #[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; @@ -382,7 +383,7 @@ pub struct RegistrarInfo< } decl_storage! { - trait Store for Module<T: Trait> as Sudo { + trait Store for Module<T: Trait> as Identity { /// Information that is pertinent to identify the entity behind an account. pub IdentityOf get(fn identity): map hasher(blake2_256) T::AccountId => Option<Registration<BalanceOf<T>>>; @@ -873,6 +874,10 @@ decl_module! { Self::deposit_event(RawEvent::IdentityKilled(target, deposit)); } + + fn on_runtime_upgrade() { + migration::on_runtime_upgrade::<T>() + } } } diff --git a/substrate/frame/identity/src/migration.rs b/substrate/frame/identity/src/migration.rs new file mode 100644 index 0000000000000000000000000000000000000000..e312d9e04f239ab33e034f3ff03b0939d1a479d0 --- /dev/null +++ b/substrate/frame/identity/src/migration.rs @@ -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."); +} diff --git a/substrate/frame/nicks/src/lib.rs b/substrate/frame/nicks/src/lib.rs index caed6e40accb0c4909f64ad2a4e3317a27e8aa95..4fa94b575fac18aeb629f9b1af69b4383cabe62e 100644 --- a/substrate/frame/nicks/src/lib.rs +++ b/substrate/frame/nicks/src/lib.rs @@ -76,7 +76,7 @@ pub trait Trait: frame_system::Trait { } decl_storage! { - trait Store for Module<T: Trait> as Sudo { + trait Store for Module<T: Trait> as Nicks { /// The lookup table for names. NameOf: map hasher(blake2_256) T::AccountId => Option<(Vec<u8>, BalanceOf<T>)>; } diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index 531f2557140e118c4b781d00b277682deaa905e2..00b229750db47435ae51c16142336f02b62e6b79 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -251,7 +251,7 @@ mod tests { use self::module::Module; decl_storage! { - trait Store for Module<T: Trait> as Example { + trait Store for Module<T: Trait> as Test { pub Data get(fn data) build(|_| vec![(15u32, 42u64)]): linked_map hasher(twox_64_concat) u32 => u64; pub OptionLinkedMap: linked_map hasher(blake2_256) u32 => Option<u32>; @@ -488,7 +488,7 @@ mod tests { } const EXPECTED_METADATA: StorageMetadata = StorageMetadata { - prefix: DecodeDifferent::Encode("Example"), + prefix: DecodeDifferent::Encode("Test"), entries: DecodeDifferent::Encode( &[ StorageEntryMetadata { diff --git a/substrate/frame/support/test/tests/genesisconfig.rs b/substrate/frame/support/test/tests/genesisconfig.rs index 12af18aac90fada32f8126e4c3c8d59afa09ecaf..5a5ab9d1dbcdd9fd4db78f7871e42a8fe2ca7f4d 100644 --- a/substrate/frame/support/test/tests/genesisconfig.rs +++ b/substrate/frame/support/test/tests/genesisconfig.rs @@ -24,7 +24,7 @@ frame_support::decl_module! { } frame_support::decl_storage! { - trait Store for Module<T: Trait> as Example { + trait Store for Module<T: Trait> as Test { pub AppendableDM config(t): double_map hasher(blake2_256) u32, hasher(blake2_256) T::BlockNumber => Vec<u32>; } } diff --git a/substrate/frame/transaction-payment/src/lib.rs b/substrate/frame/transaction-payment/src/lib.rs index 2367d5ee5abe30b1b1b8e661d446c1ae9202330a..3a1239630f9efdc29bfcb8ec6f42dbf251542637 100644 --- a/substrate/frame/transaction-payment/src/lib.rs +++ b/substrate/frame/transaction-payment/src/lib.rs @@ -48,6 +48,8 @@ use sp_runtime::{ }; use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo; +mod migration; + type Multiplier = Fixed64; type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance; @@ -77,7 +79,7 @@ pub trait Trait: frame_system::Trait { } decl_storage! { - trait Store for Module<T: Trait> as Balances { + trait Store for Module<T: Trait> as TransactionPayment { pub NextFeeMultiplier get(fn next_fee_multiplier): Multiplier = Multiplier::from_parts(0); } } @@ -95,6 +97,10 @@ decl_module! { *fm = T::FeeMultiplierUpdate::convert(*fm) }); } + + fn on_runtime_upgrade() { + migration::on_runtime_upgrade() + } } } diff --git a/substrate/frame/transaction-payment/src/migration.rs b/substrate/frame/transaction-payment/src/migration.rs new file mode 100644 index 0000000000000000000000000000000000000000..6db3cfd0f9b6003a3a1d4722c19c02ef057ae465 --- /dev/null +++ b/substrate/frame/transaction-payment/src/migration.rs @@ -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); + } +}