Skip to content

Commit

Permalink
Fix system account frozen field
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Lau <xavier@inv.cafe>
  • Loading branch information
aurexav committed Apr 25, 2024
1 parent 351a0b3 commit 177523b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 81 deletions.
2 changes: 1 addition & 1 deletion runtime/crab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(migration::CustomOnRuntimeUpgrade, darwinia_staking::migration::v2::MigrateToV2<Runtime>),
migration::CustomOnRuntimeUpgrade,
>;

/// Runtime version.
Expand Down
115 changes: 35 additions & 80 deletions runtime/crab/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,13 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
log::info!("pre");

assert!(Balances::free_balance(ROOT) != 0);

<pallet_balances::Locks<Runtime>>::iter().for_each(|(k, v)| {
log::info!("{k:?}");
log::info!("{v:?}");
});

Ok(Vec::new())
}

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

assert!(Balances::free_balance(ROOT) == 0);

<pallet_balances::Locks<Runtime>>::iter().for_each(|(k, v)| {
log::info!("{k:?}");
log::info!("{v:?}");

assert!(!v.is_empty());
});

Ok(())
}

Expand All @@ -61,72 +45,43 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
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 {
if v.is_empty() {
// Clear the storage entry if the vector is empty.

<pallet_balances::Locks<Runtime>>::remove(k);
} else {
<pallet_balances::Locks<Runtime>>::insert(k, v);
}

w += 1;
[
"0xd891ce6a97b4f01a8b9b36d0298aa3631fe2eef5",
"0x88a39b052d477cfde47600a7c9950a441ce61cb4",
"0x0a1287977578f888bdc1c7627781af1cc000e6ab",
"0x0b001c95e86d64c1ad6e43944c568a6c31b53887",
"0x7ae2a0914db8bfbdad538b0eac3fa473a0e07843",
"0x5af9a1be7bc22f9a6b2ce90acd69c23dceeb23c2",
"0x1678a973ae9750d25c126cdbce891bb8cfacd520",
"0x4ed7ae57608cf4f60753cde4f49cf821c293ed2a",
"0x5b7544b3f6abd9e03fba494796b1ee6f9543e2e4",
"0x44cda595218ddb3810fb66c2e982f50ea00255ee",
]
.iter()
.filter_map(|a| array_bytes::hex_n_into::<_, AccountId, 20>(a).ok())
.for_each(|a| {
let freeze = <pallet_balances::Freezes<Runtime>>::get(a)
.into_iter()
.map(|f| f.amount)
.max()
.unwrap_or(0);
let frozen = <pallet_balances::Locks<Runtime>>::get(a)
.into_iter()
.map(|l| l.amount)
.max()
.unwrap_or(0);
let frozen = freeze.max(frozen);
let _ = <frame_system::Account<Runtime>>::try_mutate(a, |a| {
if a.data.frozen == frozen {
Err(())
} else {
a.data.frozen = frozen;

Ok(())
}
}

r += 1;
});
});

w += migration_helper::PalletCleaner {
name: b"EcdsaAuthority",
values: &[
b"Authorities",
b"NextAuthorities",
b"Nonce",
b"AuthoritiesChangeToSign",
b"MessageRootToSign",
],
maps: &[],
}
.remove_all();

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

0 comments on commit 177523b

Please sign in to comment.