Skip to content

Commit

Permalink
Fix system account frozen field (#1485)
Browse files Browse the repository at this point in the history
* Fix system account frozen field

Signed-off-by: Xavier Lau <xavier@inv.cafe>

* TODO

* Add migration

* Fix compile

* Fix compile

* Add migration back

---------

Signed-off-by: Xavier Lau <xavier@inv.cafe>
  • Loading branch information
aurexav authored May 7, 2024
1 parent bf7a8b3 commit 351f328
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 159 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
128 changes: 50 additions & 78 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,60 @@ 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;
if let Ok(a) =
array_bytes::hex_n_into::<_, AccountId, 20>("0xacfa39b864e42d1bd3792783a571d2958af0bf1f")
{
let mut l = <pallet_balances::Locks<Runtime>>::get(a);

if let Some(i) = l.iter().position(|l| l.id == *b"phrelect") {
l.remove(i);

if l.is_empty() {
<pallet_balances::Locks<Runtime>>::remove(a);
} else {
<pallet_balances::Locks<Runtime>>::insert(a, l);
}
}
}

r += 1;
[
"0xd891ce6a97b4f01a8b9b36d0298aa3631fe2eef5",
"0x88a39b052d477cfde47600a7c9950a441ce61cb4",
"0x0a1287977578f888bdc1c7627781af1cc000e6ab",
"0x0b001c95e86d64c1ad6e43944c568a6c31b53887",
"0x7ae2a0914db8bfbdad538b0eac3fa473a0e07843",
"0xacfa39b864e42d1bd3792783a571d2958af0bf1f",
"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(())
}
});
});

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(1, 23)
}
2 changes: 1 addition & 1 deletion runtime/darwinia/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
113 changes: 34 additions & 79 deletions runtime/darwinia/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!(migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[]));

<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!(!migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[]));

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

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

Ok(())
}

Expand All @@ -61,71 +45,42 @@ 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 {
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;
[
"0x71571c42067900bfb7ca8b51fccc07ef77074aea",
"0x0a1287977578f888bdc1c7627781af1cc000e6ab",
"0x0b001c95e86d64c1ad6e43944c568a6c31b53887",
"0xfa5727be643dba6599fc7f812fe60da3264a8205",
"0x5af9a1be7bc22f9a6b2ce90acd69c23dceeb23c2",
"0x1678a973ae9750d25c126cdbce891bb8cfacd520",
"0x5dd68958e07cec3f65489db8983ad737c37e0646",
"0xf11d8d9412fc6b90242e17af259cf7bd1eaa416b",
"0xdca962b899641d60ccf7268a2260f20b6c01c06d",
]
.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();

let _ = migration::clear_storage_prefix(
b"BridgeKusamaGrandpa",
b"ImportedHeaders",
Expand All @@ -135,5 +90,5 @@ fn migrate() -> frame_support::weights::Weight {
);

// 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, 118)
}

0 comments on commit 351f328

Please sign in to comment.