Skip to content

Commit b910296

Browse files
authored
Migration: Issuance and Dust fix (#197)
* feat: remove migrations already ran * feat: add vesting migration * docs: add more comments * chore: print the total issuance * chore: split migrations in multiple files * chore: fmt * docs: add more details about the account
1 parent 92c3d45 commit b910296

File tree

9 files changed

+201
-18
lines changed

9 files changed

+201
-18
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ serde = { version = "1.0.188", default-features = false }
8787
smallvec = "1.11.0"
8888
log = { version = "0.4.17", default-features = false }
8989
itertools = { version = "0.11.0", default-features = false, features = ["use_alloc"] }
90+
array-bytes = { version = "*", default-features = false }
9091

9192
# Emulations
9293
xcm-emulator = { git = "https://github.com/paritytech/cumulus", default-features = false, branch = "release-v1.0.0" }

runtimes/base/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ version.workspace = true
1414
substrate-wasm-builder.workspace = true
1515

1616
[dependencies]
17-
parity-scale-codec = { workspace= true, default-features = false, features = [
17+
parity-scale-codec = { workspace = true, default-features = false, features = [
1818
"derive",
1919
] }
2020
log.workspace = true
21-
scale-info = { workspace= true, default-features = false, features = [
21+
scale-info = { workspace = true, default-features = false, features = [
2222
"derive",
2323
] }
2424

@@ -93,6 +93,10 @@ parachains-common.workspace = true
9393
# ORML
9494
orml-oracle.workspace = true
9595

96+
# Migration utilities
97+
hex-literal = { workspace = true }
98+
array-bytes = { workspace = true, default-features = false }
99+
96100
[features]
97101
default = [ "std" ]
98102
fast-mode = [ "shared-configuration/fast-mode" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Polimec Blockchain – https://www.polimec.org/
2+
// Copyright (C) Polimec 2022. All rights reserved.
3+
4+
// The Polimec Blockchain 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+
// The Polimec Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
#[allow(unused_imports)]
18+
use crate::*;
19+
20+
// Substrate
21+
use frame_support::{log, traits::tokens::Precision::Exact};
22+
23+
pub struct DepositDust;
24+
impl frame_support::traits::OnRuntimeUpgrade for DepositDust {
25+
#[cfg(feature = "try-runtime")]
26+
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
27+
log::info!("Pre-upgrade");
28+
Ok(Vec::new())
29+
}
30+
31+
#[cfg(feature = "try-runtime")]
32+
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
33+
log::info!("Post-upgrade");
34+
let total_issuance = Balances::total_issuance();
35+
assert_eq!(total_issuance, 100_000_000 * PLMC);
36+
Ok(())
37+
}
38+
39+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
40+
// +1 R
41+
let total_issuance = Balances::total_issuance();
42+
43+
// Idempotent check.
44+
if total_issuance != 100_000_000 * PLMC {
45+
log::info!("⚠️ Correcting total issuance from {} to {}", total_issuance, 100_000_000 * PLMC);
46+
// +1 R
47+
let treasury_account = PayMaster::get();
48+
// +1 W
49+
// The values are coming from these `DustLost` events:
50+
// - https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polimec.org#/explorer/query/0x6fec4ce782f42afae1437f53e3382d9e6804692de868a28908ed6b9104bdd536
51+
// - https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polimec.org#/explorer/query/0x390d04247334df9d9eb02e1dc7c6d01910c950d99a5d8d17441eb202cd751f42
52+
let _ = <Balances as tokens::fungible::Balanced<AccountId>>::deposit(
53+
&treasury_account,
54+
39988334 + 70094167,
55+
Exact,
56+
);
57+
}
58+
59+
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(2, 1)
60+
}
61+
}

runtimes/base/src/custom_migrations.rs runtimes/base/src/custom_migrations/init_pallet.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
use crate::*;
1919

2020
// Substrate
21-
#[allow(unused_imports)]
22-
use frame_support::{
23-
dispatch::DispatchError,
24-
log, migration,
25-
storage::unhashed,
26-
traits::{GetStorageVersion, PalletInfoAccess, StorageVersion},
27-
};
21+
use frame_support::traits::{GetStorageVersion, PalletInfoAccess, StorageVersion};
22+
23+
#[cfg(feature = "try-runtime")]
24+
use frame_support::dispatch::DispatchError;
2825

2926
pub struct InitializePallet<Pallet: GetStorageVersion<CurrentStorageVersion = StorageVersion> + PalletInfoAccess>(
3027
sp_std::marker::PhantomData<Pallet>,
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Polimec Blockchain – https://www.polimec.org/
2+
// Copyright (C) Polimec 2022. All rights reserved.
3+
4+
// The Polimec Blockchain 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+
// The Polimec Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
// the generated files do not pass clippy
18+
#![allow(clippy::all)]
19+
20+
pub mod deposit_dust;
21+
pub mod init_pallet;
22+
pub mod unhashed_migration;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Polimec Blockchain – https://www.polimec.org/
2+
// Copyright (C) Polimec 2022. All rights reserved.
3+
4+
// The Polimec Blockchain 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+
// The Polimec Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
#[allow(unused_imports)]
18+
use crate::*;
19+
20+
// Substrate
21+
use frame_support::{log, storage::unhashed};
22+
use sp_runtime::AccountId32;
23+
24+
#[cfg(feature = "try-runtime")]
25+
use sp_core::crypto::Ss58Codec;
26+
27+
#[cfg(feature = "try-runtime")]
28+
use pallet_vesting::Vesting;
29+
30+
// The `VestingInfo` fields from `pallet_vesting` are private, so we need to define them here.
31+
#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, sp_runtime::RuntimeDebug, Eq, PartialEq)]
32+
struct VestingInfo {
33+
locked: Balance,
34+
per_block: Balance,
35+
starting_block: BlockNumber,
36+
}
37+
38+
pub struct UnhashedMigration;
39+
impl frame_support::traits::OnRuntimeUpgrade for UnhashedMigration {
40+
#[cfg(feature = "try-runtime")]
41+
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
42+
log::info!("Pre-upgrade");
43+
check_balances();
44+
Ok(Vec::new())
45+
}
46+
47+
#[cfg(feature = "try-runtime")]
48+
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
49+
log::info!("Post-upgrade");
50+
check_balances();
51+
Ok(())
52+
}
53+
54+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
55+
// This account received a wrong vesting schedule.
56+
// Hex encoded representation of 5Ag8zhuoZjKzc3YzmkWFrrmU5GvxdHLtpAN425RW9ZgWS5V7.
57+
let acct: AccountId32 =
58+
hex_literal::hex!["c28dbf096b5acf3c0d87dd8ef8cabea0794cc72200a2368751a0fe470d5f9f69"].into();
59+
60+
// The vesting.Vesting(5Ag8zhuoZjKzc3YzmkWFrrmU5GvxdHLtpAN425RW9ZgWS5V7) encoded storage key.
61+
const ENCODED_STORAGE_KEY: &str =
62+
"0x5f27b51b5ec208ee9cb25b55d87282435f27b51b5ec208ee9cb25b55d872824334f5503ce555ea3ee18396f4bde1b40bc28dbf096b5acf3c0d87dd8ef8cabea0794cc72200a2368751a0fe470d5f9f69";
63+
64+
if let Ok(k) = array_bytes::hex2bytes(ENCODED_STORAGE_KEY) {
65+
// If `is_some` which means it has a vesting schedule, that we could potentially have to correct.
66+
// +1 R
67+
if let Some(value) = unhashed::get::<Vec<VestingInfo>>(&k) {
68+
let v = vec![
69+
VestingInfo { locked: 119574300000000, per_block: 182000456, starting_block: 249000 },
70+
VestingInfo { locked: 6485400000000, per_block: 9870000, starting_block: 249000 },
71+
];
72+
// Idempotent check.
73+
if value != v {
74+
log::info!("⚠️ Correcting storage for {:?}", acct.encode());
75+
// +1 W
76+
unhashed::put::<Vec<VestingInfo>>(&k, &v);
77+
} else {
78+
log::info!("✅ Storage for {:?} is already correct", acct.encode());
79+
}
80+
}
81+
}
82+
83+
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
84+
}
85+
}
86+
87+
#[cfg(feature = "try-runtime")]
88+
fn check_balances() {
89+
let acct: AccountId32 =
90+
hex_literal::hex!["c28dbf096b5acf3c0d87dd8ef8cabea0794cc72200a2368751a0fe470d5f9f69"].into();
91+
let balance = Balances::balance(&acct);
92+
log::info!("Account: {} | Balance: {}", acct.to_ss58check(), balance);
93+
let vesting_stored = <Vesting<Runtime>>::get(acct.clone());
94+
if let Some(vesting) = vesting_stored {
95+
log::info!("Vesting: {:?}", vesting);
96+
} else {
97+
log::info!("Vesting: None");
98+
}
99+
let total_issuance = Balances::total_issuance();
100+
log::info!("Total Issuance: {}", total_issuance);
101+
}

runtimes/base/src/lib.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,11 @@ pub type Migrations = migrations::Unreleased;
136136
#[allow(missing_docs)]
137137
pub mod migrations {
138138
// Not warn for unused imports in this module.
139-
#![allow(unused_imports)]
140-
use frame_support::migrations::RemovePallet;
141139

142-
parameter_types! {
143-
pub const Sudo: &'static str = "Sudo";
144-
}
140+
use crate::custom_migrations::{deposit_dust::DepositDust, unhashed_migration::UnhashedMigration};
145141

146-
use super::*;
147142
/// Unreleased migrations. Add new ones here:
148-
pub type Unreleased = (RemovePallet<Sudo, ParityDbWeight>,);
143+
pub type Unreleased = (UnhashedMigration, DepositDust);
149144
}
150145

151146
/// Executive: handles dispatch to the various modules.
@@ -195,7 +190,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
195190
spec_name: create_runtime_str!("polimec-mainnet"),
196191
impl_name: create_runtime_str!("polimec-mainnet"),
197192
authoring_version: 1,
198-
spec_version: 0_005_006,
193+
spec_version: 0_005_007,
199194
impl_version: 0,
200195
apis: RUNTIME_API_VERSIONS,
201196
transaction_version: 1,

runtimes/testnet/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ std = [
168168
"sp-block-builder/std",
169169
"sp-consensus-aura/std",
170170
"sp-core/std",
171+
"sp-debug-derive/std",
171172
"sp-inherents/std",
172173
"sp-io/std",
173174
"sp-offchain/std",
@@ -179,7 +180,6 @@ std = [
179180
"xcm-builder/std",
180181
"xcm-executor/std",
181182
"xcm/std",
182-
"sp-debug-derive/std"
183183
]
184184

185185
runtime-benchmarks = [

0 commit comments

Comments
 (0)