Skip to content

Commit

Permalink
Clean up residual data from migration (#1460)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Lau <[email protected]>
Co-authored-by: Bear Wang <[email protected]>
  • Loading branch information
AurevoirXavier and boundless-forest authored Apr 1, 2024
1 parent 2ddad44 commit bac2766
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 4 deletions.
55 changes: 53 additions & 2 deletions runtime/crab/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ pub struct CustomOnRuntimeUpgrade;
impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
assert!(Balances::free_balance(ROOT) != 0);

Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
assert!(Balances::free_balance(ROOT) == 0);

<pallet_balances::Locks<Runtime>>::iter_values().for_each(|v| {
assert!(!v.is_empty());
});

Ok(())
}

Expand All @@ -41,9 +49,52 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
let mut w = 1;
let mut r = 0;
let mut w = 4;
let _ =
migration::clear_storage_prefix(b"MessageGadget", b"CommitmentContract", &[], None, None);
let _ = Balances::transfer_all(RuntimeOrigin::signed(ROOT), Treasury::account_id(), true);
let lock_ids = [
// Democracy lock.
*b"democrac",
// Fee market lock.
*b"da/feecr",
];

<pallet_balances::Locks<Runtime>>::iter().for_each(|(k, mut v)| {
if v.is_empty() {
// Clear the storage entry if the vector is empty.

<pallet_balances::Locks<Runtime>>::remove(k);

w += 1;
} else {
// Find matching lock ids and remove them.

let mut changed = false;

v.retain(|l| {
if lock_ids.contains(&l.id) {
// Mark as changed, the storage entry needs to be updated.
changed = true;

// To remove.
false
} else {
// To keep.
true
}
});

if changed {
<pallet_balances::Locks<Runtime>>::insert(k, v);

w += 1;
}
}

r += 1;
});

w += migration_helper::PalletCleaner {
name: b"EcdsaAuthority",
Expand All @@ -59,5 +110,5 @@ fn migrate() -> frame_support::weights::Weight {
.remove_all();

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(0, w as _)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(r as _, w as _)
}
52 changes: 51 additions & 1 deletion runtime/darwinia/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ pub struct CustomOnRuntimeUpgrade;
impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
assert!(migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[]));

Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
assert!(!migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[]));

<pallet_balances::Locks<Runtime>>::iter_values().for_each(|v| {
assert!(!v.is_empty());
});

Ok(())
}

Expand All @@ -41,9 +49,51 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
let mut r = 0;
let mut w = 101;
let _ =
migration::clear_storage_prefix(b"MessageGadget", b"CommitmentContract", &[], None, None);
let lock_ids = [
// Democracy lock.
*b"democrac",
// Fee market lock.
*b"da/feecr",
];

<pallet_balances::Locks<Runtime>>::iter().for_each(|(k, mut v)| {
if v.is_empty() {
// Clear the storage entry if the vector is empty.

<pallet_balances::Locks<Runtime>>::remove(k);

w += 1;
} else {
// Find matching lock ids and remove them.

let mut changed = false;

v.retain(|l| {
if lock_ids.contains(&l.id) {
// Mark as changed, the storage entry needs to be updated.
changed = true;

// To remove.
false
} else {
// To keep.
true
}
});

if changed {
<pallet_balances::Locks<Runtime>>::insert(k, v);

w += 1;
}
}

r += 1;
});

w += migration_helper::PalletCleaner {
name: b"EcdsaAuthority",
Expand All @@ -67,5 +117,5 @@ fn migrate() -> frame_support::weights::Weight {
);

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(0, w as _)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(r as _, w as _)
}
48 changes: 47 additions & 1 deletion runtime/pangolin/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
<pallet_balances::Locks<Runtime>>::iter_values().for_each(|v| {
assert!(!v.is_empty());
});

Ok(())
}

Expand All @@ -41,9 +45,51 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
let mut r = 0;
let mut w = 1;
let _ =
migration::clear_storage_prefix(b"MessageGadget", b"CommitmentContract", &[], None, None);
let lock_ids = [
// Democracy lock.
*b"democrac",
// Fee market lock.
*b"da/feecr",
];

<pallet_balances::Locks<Runtime>>::iter().for_each(|(k, mut v)| {
if v.is_empty() {
// Clear the storage entry if the vector is empty.

<pallet_balances::Locks<Runtime>>::remove(k);

w += 1;
} else {
// Find matching lock ids and remove them.

let mut changed = false;

v.retain(|l| {
if lock_ids.contains(&l.id) {
// Mark as changed, the storage entry needs to be updated.
changed = true;

// To remove.
false
} else {
// To keep.
true
}
});

if changed {
<pallet_balances::Locks<Runtime>>::insert(k, v);

w += 1;
}
}

r += 1;
});

w += migration_helper::PalletCleaner {
name: b"EcdsaAuthority",
Expand All @@ -59,5 +105,5 @@ fn migrate() -> frame_support::weights::Weight {
.remove_all();

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(0, w as _)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(r as _, w as _)
}

0 comments on commit bac2766

Please sign in to comment.