Skip to content

Commit f25d401

Browse files
shawntabrizigavofyork
authored andcommitted
Assign unique storage names to pallets. (paritytech#5010)
* 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 <[email protected]>
1 parent aec1f45 commit f25d401

File tree

8 files changed

+230
-1
lines changed

8 files changed

+230
-1
lines changed

frame/contracts/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ mod rent;
8989

9090
#[cfg(test)]
9191
mod tests;
92+
mod migration;
9293

9394
use crate::exec::ExecutionContext;
9495
use crate::account_db::{AccountDb, DirectAccountDb};
@@ -578,6 +579,10 @@ decl_module! {
578579
T::Currency::deposit_into_existing(&rewarded, T::SurchargeReward::get())?;
579580
}
580581
}
582+
583+
fn on_runtime_upgrade() {
584+
migration::on_runtime_upgrade::<T>()
585+
}
581586
}
582587
}
583588

frame/contracts/src/migration.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Migration code to update storage.
18+
19+
use super::*;
20+
use frame_support::storage::migration::{put_storage_value, take_storage_value, StorageIterator};
21+
22+
pub fn on_runtime_upgrade<T: Trait>() {
23+
change_name_contract_to_contracts::<T>()
24+
}
25+
26+
// Change the storage name used by this pallet from `Contract` to `Contracts`.
27+
//
28+
// Since the format of the storage items themselves have not changed, we do not
29+
// need to keep track of a storage version. If the runtime does not need to be
30+
// upgraded, nothing here will happen anyway.
31+
32+
fn change_name_contract_to_contracts<T: Trait>() {
33+
sp_runtime::print("Migrating Contracts.");
34+
35+
if let Some(gas_spent) = take_storage_value::<Gas>(b"Contract", b"GasSpent", &[]) {
36+
put_storage_value(b"Contracts", b"GasSpent", &[], gas_spent);
37+
}
38+
39+
if let Some(current_schedule) = take_storage_value::<Schedule>(b"Contract", b"CurrentSchedule", &[]) {
40+
put_storage_value(b"Contracts", b"CurrentSchedule", &[], current_schedule);
41+
}
42+
43+
for (hash, pristine_code) in StorageIterator::<Vec<u8>>::new(b"Contract", b"PristineCode").drain() {
44+
put_storage_value(b"Contracts", b"PristineCode", &hash, pristine_code);
45+
}
46+
47+
for (hash, code_storage) in StorageIterator::<wasm::PrefabWasmModule>::new(b"Contract", b"CodeStorage").drain() {
48+
put_storage_value(b"Contracts", b"CodeStorage", &hash, code_storage);
49+
}
50+
51+
if let Some(current_schedule) = take_storage_value::<u64>(b"Contract", b"AccountCounter", &[]) {
52+
put_storage_value(b"Contracts", b"AccountCounter", &[], current_schedule);
53+
}
54+
55+
for (hash, contract_info_of) in StorageIterator::<ContractInfo<T>>::new(b"Contract", b"ContractInfoOf").drain() {
56+
put_storage_value(b"Contracts", b"ContractInfoOf", &hash, contract_info_of);
57+
}
58+
59+
if let Some(get_price) = take_storage_value::<BalanceOf<T>>(b"Contract", b"GetPrice", &[]) {
60+
put_storage_value(b"Contracts", b"GetPrice", &[], get_price);
61+
}
62+
}

frame/finality-tracker/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use frame_support::weights::{DispatchClass};
2828
use frame_system::{ensure_none, Trait as SystemTrait};
2929
use sp_finality_tracker::{INHERENT_IDENTIFIER, FinalizedInherentData};
3030

31+
mod migration;
32+
3133
pub const DEFAULT_WINDOW_SIZE: u32 = 101;
3234
pub const DEFAULT_REPORT_LATENCY: u32 = 1000;
3335

@@ -92,6 +94,10 @@ decl_module! {
9294
fn on_finalize() {
9395
Self::update_hint(<Self as Store>::Update::take())
9496
}
97+
98+
fn on_runtime_upgrade() {
99+
migration::on_runtime_upgrade::<T>()
100+
}
95101
}
96102
}
97103

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Migration code to update storage.
18+
19+
use super::*;
20+
use frame_support::storage::migration::{put_storage_value, take_storage_value};
21+
22+
pub fn on_runtime_upgrade<T: Trait>() {
23+
change_name_timestamp_to_finality_tracker::<T>()
24+
}
25+
26+
// Change the storage name used by this pallet from `Timestamp` to `FinalityTracker`.
27+
//
28+
// Since the format of the storage items themselves have not changed, we do not
29+
// need to keep track of a storage version. If the runtime does not need to be
30+
// upgraded, nothing here will happen anyway.
31+
32+
fn change_name_timestamp_to_finality_tracker<T:Trait>() {
33+
sp_runtime::print("Migrating Finality Tracker.");
34+
35+
if let Some(recent_hints) = take_storage_value::<Vec<T::BlockNumber>>(b"Timestamp", b"RecentHints", &[]) {
36+
put_storage_value(b"FinalityTracker", b"RecentHints", &[], recent_hints);
37+
}
38+
39+
if let Some(ordered_hints) = take_storage_value::<Vec<T::BlockNumber>>(b"Timestamp", b"OrderedHints", &[]) {
40+
put_storage_value(b"FinalityTracker", b"OrderedHints", &[], ordered_hints);
41+
}
42+
43+
if let Some(median) = take_storage_value::<T::BlockNumber>(b"Timestamp", b"Median", &[]) {
44+
put_storage_value(b"FinalityTracker", b"Median", &[], median);
45+
}
46+
47+
if let Some(update) = take_storage_value::<T::BlockNumber>(b"Timestamp", b"Update", &[]) {
48+
put_storage_value(b"FinalityTracker", b"Update", &[], update);
49+
}
50+
51+
if let Some(initialized) = take_storage_value::<bool>(b"Timestamp", b"Initialized", &[]) {
52+
put_storage_value(b"FinalityTracker", b"Initialized", &[], initialized);
53+
}
54+
}

frame/identity/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ use frame_support::{
8080
};
8181
use frame_system::{self as system, ensure_signed, ensure_root};
8282

83-
mod benchmarking;
83+
#[cfg(feature = "runtime-benchmarks")]
84+
pub mod benchmarking;
85+
mod migration;
8486

8587
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
8688
type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
@@ -1132,6 +1134,10 @@ decl_module! {
11321134
id.info.additional.len() as Weight // X
11331135
)).into())
11341136
}
1137+
1138+
fn on_runtime_upgrade() {
1139+
migration::on_runtime_upgrade::<T>()
1140+
}
11351141
}
11361142
}
11371143

frame/identity/src/migration.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Migration code to update storage.
18+
19+
use super::*;
20+
use frame_support::storage::migration::{put_storage_value, take_storage_value, StorageIterator};
21+
22+
pub fn on_runtime_upgrade<T: Trait>() {
23+
change_name_sudo_to_identity::<T>()
24+
}
25+
26+
// Change the storage name used by this pallet from `Sudo` to `Identity`.
27+
//
28+
// Since the format of the storage items themselves have not changed, we do not
29+
// need to keep track of a storage version. If the runtime does not need to be
30+
// upgraded, nothing here will happen anyway.
31+
32+
fn change_name_sudo_to_identity<T: Trait>() {
33+
sp_runtime::print("Migrating Identity.");
34+
35+
for (hash, identity_of) in StorageIterator::<Registration<BalanceOf<T>>>::new(b"Sudo", b"IdentityOf").drain() {
36+
put_storage_value(b"Identity", b"IdentityOf", &hash, identity_of);
37+
}
38+
39+
for (hash, super_of) in StorageIterator::<(T::AccountId, Data)>::new(b"Sudo", b"SuperOf").drain() {
40+
put_storage_value(b"Identity", b"SuperOf", &hash, super_of);
41+
}
42+
43+
for (hash, subs_of) in StorageIterator::<(BalanceOf<T>, Vec<T::AccountId>)>::new(b"Sudo", b"SubsOf").drain() {
44+
put_storage_value(b"Identity", b"SubsOf", &hash, subs_of);
45+
}
46+
47+
if let Some(registrars) = take_storage_value::<Vec<Option<RegistrarInfo<BalanceOf<T>, T::AccountId>>>>(b"Sudo", b"Registrars", &[]) {
48+
put_storage_value(b"Identity", b"Registrars", &[], registrars);
49+
}
50+
51+
sp_runtime::print("Done Identity.");
52+
}

frame/transaction-payment/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ use sp_runtime::{
5656
};
5757
use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
5858

59+
mod migration;
60+
5961
type Multiplier = Fixed128;
6062
type BalanceOf<T> =
6163
<<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
@@ -101,6 +103,10 @@ decl_module! {
101103
*fm = T::FeeMultiplierUpdate::convert(*fm);
102104
});
103105
}
106+
107+
fn on_runtime_upgrade() {
108+
migration::on_runtime_upgrade()
109+
}
104110
}
105111
}
106112

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Substrate.
3+
4+
// Substrate is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Substrate is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//! Migration code to update storage.
18+
19+
use super::*;
20+
use frame_support::storage::migration::{put_storage_value, take_storage_value};
21+
22+
pub fn on_runtime_upgrade() {
23+
change_name_balances_to_transaction_payment()
24+
}
25+
26+
// Change the storage name used by this pallet from `Balances` to `TransactionPayment`.
27+
//
28+
// Since the format of the storage items themselves have not changed, we do not
29+
// need to keep track of a storage version. If the runtime does not need to be
30+
// upgraded, nothing here will happen anyway.
31+
32+
fn change_name_balances_to_transaction_payment() {
33+
sp_runtime::print("Migrating Transaction Payment.");
34+
35+
if let Some(next_fee_multiplier) = take_storage_value::<Multiplier>(b"Balances", b"NextFeeMultiplier", &[]) {
36+
put_storage_value(b"TransactionPayment", b"NextFeeMultiplier", &[], next_fee_multiplier);
37+
}
38+
}

0 commit comments

Comments
 (0)