From 51a61414c69e317218326d965d4ef076cc7bc3e3 Mon Sep 17 00:00:00 2001 From: Dmitry Novikov Date: Tue, 16 Jul 2024 16:15:41 +0400 Subject: [PATCH] fix --- Cargo.lock | 1 + pallets/gear-program/Cargo.toml | 1 + pallets/gear-program/src/migrations/mod.rs | 82 ++++++++++++++++++++++ runtime/vara/src/migrations.rs | 1 + 4 files changed, 85 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 47947c00aa3..92dbbe2d2b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8458,6 +8458,7 @@ dependencies = [ "log", "pallet-authorship", "pallet-balances", + "pallet-gear-bank", "pallet-gear-gas", "pallet-gear-scheduler", "pallet-timestamp", diff --git a/pallets/gear-program/Cargo.toml b/pallets/gear-program/Cargo.toml index 5b026b8cbe7..0a10e65889a 100644 --- a/pallets/gear-program/Cargo.toml +++ b/pallets/gear-program/Cargo.toml @@ -34,6 +34,7 @@ sp-runtime.workspace = true # Temporary dependencies required for migration to v7. To be removed upon migration. pallet-balances.workspace = true pallet-treasury.workspace = true +pallet-gear-bank.workspace = true [dev-dependencies] common = { workspace = true, features = ["std"] } diff --git a/pallets/gear-program/src/migrations/mod.rs b/pallets/gear-program/src/migrations/mod.rs index 40283419031..48648835ee5 100644 --- a/pallets/gear-program/src/migrations/mod.rs +++ b/pallets/gear-program/src/migrations/mod.rs @@ -19,3 +19,85 @@ pub mod allocations; pub mod paused_storage; pub mod v8; + +pub mod v8_fix { + use crate::{migrations::v8::AccountIdOf, Config, Pallet, ProgramStorage}; + use common::{Origin, Program}; + use core::marker::PhantomData; + use frame_support::{ + pallet_prelude::Weight, + traits::{ + tokens::Pay, Currency, GetStorageVersion, LockableCurrency, OnRuntimeUpgrade, + StorageVersion, WithdrawReasons, + }, + }; + + const EXISTENTIAL_DEPOSIT_LOCK_ID: [u8; 8] = *b"glock/ed"; + + type CurrencyOf = ::Currency; + type BalanceOf = as Currency>>::Balance; + + pub struct AsapFix(PhantomData); + + impl OnRuntimeUpgrade for AsapFix + where + T: pallet_treasury::Config + pallet_gear_bank::Config, + T::AccountId: Origin, + T::Paymaster: Pay>, + { + fn on_runtime_upgrade() -> Weight { + let mut weight = 0; + + if Pallet::::on_chain_storage_version() == StorageVersion::new(8) { + log::info!("Running asap fix migrations"); + + let ed = CurrencyOf::::minimum_balance(); + + ProgramStorage::::iter().for_each(|(program_id, program)| { + let program_id: AccountIdOf = program_id.cast(); + + if let Program::Active(_) = program { + if CurrencyOf::::total_balance(&program_id) - CurrencyOf::::free_balance(&program_id) != ed + { + let mut to_set_lock = true; + + if CurrencyOf::::free_balance(&program_id) < ed { + match ::Paymaster::pay( + &program_id, + (), + ed, + ) { + Ok(_) => {} + Err(e) => { + to_set_lock = false; + + log::error!( + "❌ Failed to transfer ED and set lock to {program_id:?}: {e:?}" + ); + } + }; + } + + if to_set_lock { + CurrencyOf::::set_lock( + EXISTENTIAL_DEPOSIT_LOCK_ID, + &program_id, + ed, + WithdrawReasons::all(), + ) + } + } + }; + }); + + weight = 200_000_000; + + log::info!("✅ Successfully fixed migrated storage"); + } else { + log::info!("🟠 Migration requires onchain version 8, so was skipped"); + } + + weight.into() + } + } +} diff --git a/runtime/vara/src/migrations.rs b/runtime/vara/src/migrations.rs index 78dca8e64ac..d59eadbb7e8 100644 --- a/runtime/vara/src/migrations.rs +++ b/runtime/vara/src/migrations.rs @@ -25,4 +25,5 @@ pub type Migrations = ( // migration for removed paused program storage pallet_gear_program::migrations::paused_storage::RemovePausedProgramStorageMigration, pallet_gear_program::migrations::v8::MigrateToV8, + pallet_gear_program::migrations::v8_fix::AsapFix, );