Skip to content

Commit 6b46466

Browse files
feat: add migrations from 2.0.0 (#428)
1 parent eba6868 commit 6b46466

File tree

5 files changed

+216
-12
lines changed

5 files changed

+216
-12
lines changed

β€Žsubstrate-node/pallets/pallet-smart-contract/src/contract_migration.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ pub fn migrate_to_version_4<T: Config>() -> frame_support::weights::Weight {
120120
let mut migrated_count = 0;
121121
// We transform the storage values from the old into the new format.
122122
Contracts::<T>::translate::<deprecated::ContractV3, _>(|k, ctr| {
123+
// Don't map deleted contracts
124+
if matches!(ctr.state, types::ContractState::Deleted(_)) {
125+
info!(
126+
"contract with id: {:?} in state delete, skipping...",
127+
ctr.contract_id
128+
);
129+
return None;
130+
}
131+
123132
// dummy default
124133
let rc = types::RentContract { node_id: 0 };
125134

@@ -175,8 +184,8 @@ pub fn migrate_to_version_4<T: Config>() -> frame_support::weights::Weight {
175184

176185
match public_ips_list.try_push(new_ip) {
177186
Ok(()) => (),
178-
Err(err) => {
179-
error!("error while pushing ip to contract ip list: {:?}", err);
187+
Err(_) => {
188+
error!("error while pushing ip to contract ip list");
180189
should_free_ip = true;
181190
continue;
182191
}

β€Žsubstrate-node/pallets/pallet-tfgrid/src/farm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ impl<T: Config> Clone for FarmName<T> {
6060
}
6161
}
6262

63-
pub fn replace_farm_name_spaces_with_underscores(input: &[u8]) -> Vec<u8> {
63+
pub fn replace_farm_name_invalid_characters(input: &[u8]) -> Vec<u8> {
6464
input
6565
.iter()
6666
.map(|c| match c {
6767
b' ' => b'_',
68+
b'\'' => b'-',
69+
b';' => b'_',
6870
_ => *c,
6971
})
7072
.collect()

β€Žsubstrate-node/pallets/pallet-tfgrid/src/grid_migration.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use super::Config;
22
use super::PubConfigOf;
33
use super::*;
44
use super::{InterfaceIp, InterfaceOf, PublicIpOf};
5-
use frame_support::BoundedVec;
65
use frame_support::{
76
traits::{ConstU32, Get},
87
weights::Weight,
8+
BoundedVec,
99
};
1010
use log::info;
1111
use sp_std::collections::btree_map::BTreeMap;
@@ -230,6 +230,7 @@ pub fn migrate_farms<T: Config>() -> frame_support::weights::Weight {
230230
info!(" >>> Migrating farms storage...");
231231

232232
let mut migrated_count = 0;
233+
let mut generated_farm_names: u32 = 0;
233234
// We transform the storage values from the old into the new format.
234235
Farms::<T>::translate::<deprecated::FarmV3, _>(|k, farm| {
235236
info!(" Migrated farm for {:?}...", k);
@@ -248,14 +249,30 @@ pub fn migrate_farms<T: Config>() -> frame_support::weights::Weight {
248249
}
249250
}
250251

251-
let replaced_farm_name = farm::replace_farm_name_spaces_with_underscores(&farm.name);
252-
let name = match <T as Config>::FarmName::try_from(replaced_farm_name) {
253-
Ok(n) => n,
254-
Err(_) => {
255-
info!("invalid farm name, skipping updating farm {:?} ...", k);
256-
return None;
257-
}
258-
};
252+
let mut farm_name = farm.name;
253+
let truncated = farm_name.len() > <T as Config>::MaxFarmNameLength::get() as usize;
254+
if truncated {
255+
info!("farm name length exceeded, truncating farm name");
256+
farm_name.truncate(<T as Config>::MaxFarmNameLength::get() as usize);
257+
}
258+
259+
let mut replaced_farm_name = farm::replace_farm_name_invalid_characters(&farm_name);
260+
let name =
261+
<T as Config>::FarmName::try_from(replaced_farm_name.clone()).unwrap_or_else(|_| {
262+
info!("generating farm name for farm: {:?}", farm.id);
263+
replaced_farm_name = b"change_me_".to_vec();
264+
let first = ((generated_farm_names / 26) + b'a' as u32) as u8;
265+
let second = ((generated_farm_names % 26) + b'a' as u32) as u8;
266+
replaced_farm_name.extend_from_slice(&[first, second]);
267+
generated_farm_names += 1;
268+
<T as Config>::FarmName::try_from(replaced_farm_name.clone()).unwrap()
269+
});
270+
271+
if replaced_farm_name != farm_name || truncated {
272+
info!("farm name changed, reworking storage");
273+
FarmIdByName::<T>::remove(&farm_name);
274+
FarmIdByName::<T>::insert(replaced_farm_name, farm.id);
275+
}
259276

260277
let new_farm = Farm {
261278
version: 4,

β€Žsubstrate-node/runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub use sp_runtime::{Perbill, Permill};
5252
use pallet_transaction_payment::CurrencyAdapter;
5353

5454
pub mod impls;
55+
mod migrations;
5556

5657
/// Import the template pallet.
5758
pub use pallet_tfgrid;
@@ -756,6 +757,7 @@ pub type Executive = frame_executive::Executive<
756757
Runtime,
757758
AllPalletsWithSystem,
758759
(
760+
migrations::CustomOnRuntimeUpgrades,
759761
pallet_smart_contract::contract_migration::v5::ContractMigrationV5<Runtime>,
760762
pallet_tfgrid::grid_migration::v7::GridMigration<Runtime>,
761763
pallet_tfgrid::nodes_migration::v7patch::NodesMigration<Runtime>,
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
use super::*;
2+
3+
pub struct RemoveCollectiveFlip;
4+
impl frame_support::traits::OnRuntimeUpgrade for RemoveCollectiveFlip {
5+
fn on_runtime_upgrade() -> Weight {
6+
use frame_support::storage::migration;
7+
// Remove the storage value `RandomMaterial` from removed pallet `RandomnessCollectiveFlip`
8+
migration::remove_storage_prefix(b"RandomnessCollectiveFlip", b"RandomMaterial", b"");
9+
<Runtime as frame_system::Config>::DbWeight::get().writes(1)
10+
}
11+
}
12+
13+
/// Migrate from `PalletVersion` to the new `StorageVersion`
14+
pub struct MigratePalletVersionToStorageVersion;
15+
impl frame_support::traits::OnRuntimeUpgrade for MigratePalletVersionToStorageVersion {
16+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
17+
frame_support::migrations::migrate_from_pallet_version_to_storage_version::<
18+
AllPalletsWithSystem,
19+
>(&RocksDbWeight::get())
20+
}
21+
}
22+
23+
impl frame_system::migrations::V2ToV3 for Runtime {
24+
type Pallet = System;
25+
type AccountId = AccountId;
26+
type Index = Index;
27+
type AccountData = pallet_balances::AccountData<Balance>;
28+
}
29+
30+
pub struct SystemToTripleRefCount;
31+
impl frame_support::traits::OnRuntimeUpgrade for SystemToTripleRefCount {
32+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
33+
frame_system::migrations::migrate_from_dual_to_triple_ref_count::<Runtime, Runtime>()
34+
}
35+
}
36+
37+
pub struct GrandpaStoragePrefixMigration;
38+
impl frame_support::traits::OnRuntimeUpgrade for GrandpaStoragePrefixMigration {
39+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
40+
use frame_support::traits::PalletInfo;
41+
let name = <Runtime as frame_system::Config>::PalletInfo::name::<Grandpa>()
42+
.expect("grandpa is part of pallets in construct_runtime, so it has a name; qed");
43+
pallet_grandpa::migrations::v4::migrate::<Runtime, &str>(name)
44+
}
45+
}
46+
47+
const COUNCIL_OLD_PREFIX: &str = "Instance1Collective";
48+
/// Migrate from `Instance1Collective` to the new pallet prefix `Council`
49+
pub struct CouncilStoragePrefixMigration;
50+
impl frame_support::traits::OnRuntimeUpgrade for CouncilStoragePrefixMigration {
51+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
52+
pallet_collective::migrations::v4::migrate::<Runtime, Council, _>(COUNCIL_OLD_PREFIX)
53+
}
54+
55+
#[cfg(feature = "try-runtime")]
56+
fn pre_upgrade() -> Result<(), &'static str> {
57+
pallet_collective::migrations::v4::pre_migrate::<Council, _>(COUNCIL_OLD_PREFIX);
58+
Ok(())
59+
}
60+
61+
#[cfg(feature = "try-runtime")]
62+
fn post_upgrade() -> Result<(), &'static str> {
63+
pallet_collective::migrations::v4::post_migrate::<Council, _>(COUNCIL_OLD_PREFIX);
64+
Ok(())
65+
}
66+
}
67+
68+
const COUNCIL_MEMBERSHIP_OLD_PREFIX: &str = "Instance1Membership";
69+
/// Migrate from `Instance1Membership` to the new pallet prefix `TechnicalMembership`
70+
pub struct CouncilMembershipStoragePrefixMigration;
71+
impl frame_support::traits::OnRuntimeUpgrade for CouncilMembershipStoragePrefixMigration {
72+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
73+
use frame_support::traits::PalletInfo;
74+
let name = <Runtime as frame_system::Config>::PalletInfo::name::<CouncilMembership>()
75+
.expect("CouncilMembership is part of runtime, so it has a name; qed");
76+
pallet_membership::migrations::v4::migrate::<Runtime, CouncilMembership, _>(
77+
COUNCIL_MEMBERSHIP_OLD_PREFIX,
78+
name,
79+
)
80+
}
81+
82+
#[cfg(feature = "try-runtime")]
83+
fn pre_upgrade() -> Result<(), &'static str> {
84+
use frame_support::traits::PalletInfo;
85+
let name = <Runtime as frame_system::Config>::PalletInfo::name::<CouncilMembership>()
86+
.expect("CouncilMembership is part of runtime, so it has a name; qed");
87+
pallet_membership::migrations::v4::pre_migrate::<CouncilMembership, _>(
88+
COUNCIL_MEMBERSHIP_OLD_PREFIX,
89+
name,
90+
);
91+
Ok(())
92+
}
93+
94+
#[cfg(feature = "try-runtime")]
95+
fn post_upgrade() -> Result<(), &'static str> {
96+
use frame_support::traits::PalletInfo;
97+
let name = <Runtime as frame_system::Config>::PalletInfo::name::<CouncilMembership>()
98+
.expect("CouncilMembership is part of runtime, so it has a name; qed");
99+
pallet_membership::migrations::v4::post_migrate::<CouncilMembership, _>(
100+
COUNCIL_MEMBERSHIP_OLD_PREFIX,
101+
name,
102+
);
103+
Ok(())
104+
}
105+
}
106+
107+
pub struct PalletTftPriceStoragePrefixMigration;
108+
impl frame_support::traits::OnRuntimeUpgrade for PalletTftPriceStoragePrefixMigration {
109+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
110+
use frame_support::storage::migration;
111+
112+
// Remove storage prefixes and all related items
113+
// The storage for pallet tft price has changed from U64F64 to u32
114+
migration::remove_storage_prefix(b"TftPriceModule", b"TftPrice", b"");
115+
migration::remove_storage_prefix(b"TftPriceModule", b"AverageTftPrice", b"");
116+
pallet_tft_price::TftPriceHistory::<Runtime>::remove_all(None);
117+
pallet_tft_price::BufferRange::<Runtime>::put((0, 0));
118+
119+
// Reinsert some default values
120+
pallet_tft_price::TftPrice::<Runtime>::put(45);
121+
pallet_tft_price::AverageTftPrice::<Runtime>::put(45);
122+
123+
<Runtime as frame_system::Config>::DbWeight::get().writes(2)
124+
}
125+
}
126+
127+
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
128+
pub struct CustomOnRuntimeUpgrades;
129+
impl OnRuntimeUpgrade for CustomOnRuntimeUpgrades {
130+
fn on_runtime_upgrade() -> Weight {
131+
let mut weight = 0;
132+
133+
// 1. RemoveCollectiveFlip
134+
frame_support::log::info!("πŸ”οΈ RemoveCollectiveFlip start");
135+
weight += <RemoveCollectiveFlip as OnRuntimeUpgrade>::on_runtime_upgrade();
136+
frame_support::log::info!("πŸš€ RemoveCollectiveFlip end");
137+
138+
// 2. MigratePalletVersionToStorageVersion
139+
frame_support::log::info!("πŸ”οΈ MigratePalletVersionToStorageVersion start");
140+
weight += <MigratePalletVersionToStorageVersion as OnRuntimeUpgrade>::on_runtime_upgrade();
141+
frame_support::log::info!("πŸš€ MigratePalletVersionToStorageVersion end");
142+
143+
// 3. GrandpaStoragePrefixMigration
144+
frame_support::log::info!("πŸ”οΈ GrandpaStoragePrefixMigration start");
145+
frame_support::traits::StorageVersion::new(0).put::<Grandpa>();
146+
weight += <GrandpaStoragePrefixMigration as OnRuntimeUpgrade>::on_runtime_upgrade();
147+
frame_support::log::info!("πŸš€ GrandpaStoragePrefixMigration end");
148+
149+
// 4. SystemToTripleRefCount
150+
frame_support::log::info!("πŸ”οΈ SystemToTripleRefCount start");
151+
weight += <SystemToTripleRefCount as OnRuntimeUpgrade>::on_runtime_upgrade();
152+
frame_support::log::info!("πŸš€ SystemToTripleRefCount end");
153+
154+
// 5. CouncilStoragePrefixMigration
155+
frame_support::log::info!("πŸ”οΈ CouncilStoragePrefixMigration start");
156+
frame_support::traits::StorageVersion::new(0).put::<Council>();
157+
weight += <CouncilStoragePrefixMigration as OnRuntimeUpgrade>::on_runtime_upgrade();
158+
frame_support::log::info!("πŸš€ CouncilStoragePrefixMigration end");
159+
160+
// 6. CouncilMembershipStoragePrefixMigration
161+
frame_support::log::info!("πŸ”οΈ CouncilMembershipStoragePrefixMigration start");
162+
frame_support::traits::StorageVersion::new(0).put::<CouncilMembership>();
163+
weight +=
164+
<CouncilMembershipStoragePrefixMigration as OnRuntimeUpgrade>::on_runtime_upgrade();
165+
frame_support::log::info!("πŸš€ CouncilMembershipStoragePrefixMigration end");
166+
167+
// 7. PalletTftPriceStoragePrefixMigration
168+
frame_support::log::info!("πŸ”οΈ PalletTftPriceStoragePrefixMigration start");
169+
weight += <PalletTftPriceStoragePrefixMigration as OnRuntimeUpgrade>::on_runtime_upgrade();
170+
frame_support::log::info!("πŸš€ PalletTftPriceStoragePrefixMigration end");
171+
172+
weight
173+
}
174+
}

0 commit comments

Comments
Β (0)