Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx committed Jul 16, 2024
1 parent 13d95cf commit 51a6141
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 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 pallets/gear-program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
82 changes: 82 additions & 0 deletions pallets/gear-program/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = <T as pallet_gear_bank::Config>::Currency;
type BalanceOf<T> = <CurrencyOf<T> as Currency<AccountIdOf<T>>>::Balance;

pub struct AsapFix<T: Config>(PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for AsapFix<T>
where
T: pallet_treasury::Config + pallet_gear_bank::Config,
T::AccountId: Origin,
T::Paymaster: Pay<Beneficiary = T::AccountId, AssetKind = (), Balance = BalanceOf<T>>,
{
fn on_runtime_upgrade() -> Weight {
let mut weight = 0;

if Pallet::<T>::on_chain_storage_version() == StorageVersion::new(8) {
log::info!("Running asap fix migrations");

let ed = CurrencyOf::<T>::minimum_balance();

ProgramStorage::<T>::iter().for_each(|(program_id, program)| {
let program_id: AccountIdOf<T> = program_id.cast();

if let Program::Active(_) = program {
if CurrencyOf::<T>::total_balance(&program_id) - CurrencyOf::<T>::free_balance(&program_id) != ed
{
let mut to_set_lock = true;

if CurrencyOf::<T>::free_balance(&program_id) < ed {
match <T as pallet_treasury::Config>::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::<T>::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()
}
}
}
1 change: 1 addition & 0 deletions runtime/vara/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ pub type Migrations = (
// migration for removed paused program storage
pallet_gear_program::migrations::paused_storage::RemovePausedProgramStorageMigration<Runtime>,
pallet_gear_program::migrations::v8::MigrateToV8<Runtime>,
pallet_gear_program::migrations::v8_fix::AsapFix<Runtime>,
);

0 comments on commit 51a6141

Please sign in to comment.