Skip to content

Commit

Permalink
Migration: Issuance and Dust fix (#197)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lrazovic authored Mar 19, 2024
1 parent 92c3d45 commit b910296
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ serde = { version = "1.0.188", default-features = false }
smallvec = "1.11.0"
log = { version = "0.4.17", default-features = false }
itertools = { version = "0.11.0", default-features = false, features = ["use_alloc"] }
array-bytes = { version = "*", default-features = false }

# Emulations
xcm-emulator = { git = "https://github.com/paritytech/cumulus", default-features = false, branch = "release-v1.0.0" }
Expand Down
8 changes: 6 additions & 2 deletions runtimes/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ version.workspace = true
substrate-wasm-builder.workspace = true

[dependencies]
parity-scale-codec = { workspace= true, default-features = false, features = [
parity-scale-codec = { workspace = true, default-features = false, features = [
"derive",
] }
log.workspace = true
scale-info = { workspace= true, default-features = false, features = [
scale-info = { workspace = true, default-features = false, features = [
"derive",
] }

Expand Down Expand Up @@ -93,6 +93,10 @@ parachains-common.workspace = true
# ORML
orml-oracle.workspace = true

# Migration utilities
hex-literal = { workspace = true }
array-bytes = { workspace = true, default-features = false }

[features]
default = [ "std" ]
fast-mode = [ "shared-configuration/fast-mode" ]
Expand Down
61 changes: 61 additions & 0 deletions runtimes/base/src/custom_migrations/deposit_dust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Polimec Blockchain – https://www.polimec.org/
// Copyright (C) Polimec 2022. All rights reserved.

// The Polimec Blockchain 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.

// The Polimec Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.

#[allow(unused_imports)]
use crate::*;

// Substrate
use frame_support::{log, traits::tokens::Precision::Exact};

pub struct DepositDust;
impl frame_support::traits::OnRuntimeUpgrade for DepositDust {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
log::info!("Pre-upgrade");
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
log::info!("Post-upgrade");
let total_issuance = Balances::total_issuance();
assert_eq!(total_issuance, 100_000_000 * PLMC);
Ok(())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
// +1 R
let total_issuance = Balances::total_issuance();

// Idempotent check.
if total_issuance != 100_000_000 * PLMC {
log::info!("⚠️ Correcting total issuance from {} to {}", total_issuance, 100_000_000 * PLMC);
// +1 R
let treasury_account = PayMaster::get();
// +1 W
// The values are coming from these `DustLost` events:
// - https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polimec.org#/explorer/query/0x6fec4ce782f42afae1437f53e3382d9e6804692de868a28908ed6b9104bdd536
// - https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polimec.org#/explorer/query/0x390d04247334df9d9eb02e1dc7c6d01910c950d99a5d8d17441eb202cd751f42
let _ = <Balances as tokens::fungible::Balanced<AccountId>>::deposit(
&treasury_account,
39988334 + 70094167,
Exact,
);
}

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(2, 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
use crate::*;

// Substrate
#[allow(unused_imports)]
use frame_support::{
dispatch::DispatchError,
log, migration,
storage::unhashed,
traits::{GetStorageVersion, PalletInfoAccess, StorageVersion},
};
use frame_support::traits::{GetStorageVersion, PalletInfoAccess, StorageVersion};

#[cfg(feature = "try-runtime")]
use frame_support::dispatch::DispatchError;

pub struct InitializePallet<Pallet: GetStorageVersion<CurrentStorageVersion = StorageVersion> + PalletInfoAccess>(
sp_std::marker::PhantomData<Pallet>,
Expand Down
22 changes: 22 additions & 0 deletions runtimes/base/src/custom_migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Polimec Blockchain – https://www.polimec.org/
// Copyright (C) Polimec 2022. All rights reserved.

// The Polimec Blockchain 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.

// The Polimec Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.

// the generated files do not pass clippy
#![allow(clippy::all)]

pub mod deposit_dust;
pub mod init_pallet;
pub mod unhashed_migration;
101 changes: 101 additions & 0 deletions runtimes/base/src/custom_migrations/unhashed_migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Polimec Blockchain – https://www.polimec.org/
// Copyright (C) Polimec 2022. All rights reserved.

// The Polimec Blockchain 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.

// The Polimec Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.

#[allow(unused_imports)]
use crate::*;

// Substrate
use frame_support::{log, storage::unhashed};
use sp_runtime::AccountId32;

#[cfg(feature = "try-runtime")]
use sp_core::crypto::Ss58Codec;

#[cfg(feature = "try-runtime")]
use pallet_vesting::Vesting;

// The `VestingInfo` fields from `pallet_vesting` are private, so we need to define them here.
#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, sp_runtime::RuntimeDebug, Eq, PartialEq)]
struct VestingInfo {
locked: Balance,
per_block: Balance,
starting_block: BlockNumber,
}

pub struct UnhashedMigration;
impl frame_support::traits::OnRuntimeUpgrade for UnhashedMigration {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
log::info!("Pre-upgrade");
check_balances();
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
log::info!("Post-upgrade");
check_balances();
Ok(())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
// This account received a wrong vesting schedule.
// Hex encoded representation of 5Ag8zhuoZjKzc3YzmkWFrrmU5GvxdHLtpAN425RW9ZgWS5V7.
let acct: AccountId32 =
hex_literal::hex!["c28dbf096b5acf3c0d87dd8ef8cabea0794cc72200a2368751a0fe470d5f9f69"].into();

// The vesting.Vesting(5Ag8zhuoZjKzc3YzmkWFrrmU5GvxdHLtpAN425RW9ZgWS5V7) encoded storage key.
const ENCODED_STORAGE_KEY: &str =
"0x5f27b51b5ec208ee9cb25b55d87282435f27b51b5ec208ee9cb25b55d872824334f5503ce555ea3ee18396f4bde1b40bc28dbf096b5acf3c0d87dd8ef8cabea0794cc72200a2368751a0fe470d5f9f69";

if let Ok(k) = array_bytes::hex2bytes(ENCODED_STORAGE_KEY) {
// If `is_some` which means it has a vesting schedule, that we could potentially have to correct.
// +1 R
if let Some(value) = unhashed::get::<Vec<VestingInfo>>(&k) {
let v = vec![
VestingInfo { locked: 119574300000000, per_block: 182000456, starting_block: 249000 },
VestingInfo { locked: 6485400000000, per_block: 9870000, starting_block: 249000 },
];
// Idempotent check.
if value != v {
log::info!("⚠️ Correcting storage for {:?}", acct.encode());
// +1 W
unhashed::put::<Vec<VestingInfo>>(&k, &v);
} else {
log::info!("✅ Storage for {:?} is already correct", acct.encode());
}
}
}

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
}
}

#[cfg(feature = "try-runtime")]
fn check_balances() {
let acct: AccountId32 =
hex_literal::hex!["c28dbf096b5acf3c0d87dd8ef8cabea0794cc72200a2368751a0fe470d5f9f69"].into();
let balance = Balances::balance(&acct);
log::info!("Account: {} | Balance: {}", acct.to_ss58check(), balance);
let vesting_stored = <Vesting<Runtime>>::get(acct.clone());
if let Some(vesting) = vesting_stored {
log::info!("Vesting: {:?}", vesting);
} else {
log::info!("Vesting: None");
}
let total_issuance = Balances::total_issuance();
log::info!("Total Issuance: {}", total_issuance);
}
11 changes: 3 additions & 8 deletions runtimes/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,11 @@ pub type Migrations = migrations::Unreleased;
#[allow(missing_docs)]
pub mod migrations {
// Not warn for unused imports in this module.
#![allow(unused_imports)]
use frame_support::migrations::RemovePallet;

parameter_types! {
pub const Sudo: &'static str = "Sudo";
}
use crate::custom_migrations::{deposit_dust::DepositDust, unhashed_migration::UnhashedMigration};

use super::*;
/// Unreleased migrations. Add new ones here:
pub type Unreleased = (RemovePallet<Sudo, ParityDbWeight>,);
pub type Unreleased = (UnhashedMigration, DepositDust);
}

/// Executive: handles dispatch to the various modules.
Expand Down Expand Up @@ -195,7 +190,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("polimec-mainnet"),
impl_name: create_runtime_str!("polimec-mainnet"),
authoring_version: 1,
spec_version: 0_005_006,
spec_version: 0_005_007,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
2 changes: 1 addition & 1 deletion runtimes/testnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ std = [
"sp-block-builder/std",
"sp-consensus-aura/std",
"sp-core/std",
"sp-debug-derive/std",
"sp-inherents/std",
"sp-io/std",
"sp-offchain/std",
Expand All @@ -179,7 +180,6 @@ std = [
"xcm-builder/std",
"xcm-executor/std",
"xcm/std",
"sp-debug-derive/std"
]

runtime-benchmarks = [
Expand Down

0 comments on commit b910296

Please sign in to comment.