|
| 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 | +} |
0 commit comments