From 351f328d4745f67aa539d0f6e2184ea576e9b886 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Wed, 8 May 2024 01:02:48 +0800 Subject: [PATCH] Fix system account frozen field (#1485) * Fix system account frozen field Signed-off-by: Xavier Lau * TODO * Add migration * Fix compile * Fix compile * Add migration back --------- Signed-off-by: Xavier Lau --- runtime/crab/src/lib.rs | 2 +- runtime/crab/src/migration.rs | 128 ++++++++++++------------------ runtime/darwinia/src/lib.rs | 2 +- runtime/darwinia/src/migration.rs | 113 ++++++++------------------ 4 files changed, 86 insertions(+), 159 deletions(-) diff --git a/runtime/crab/src/lib.rs b/runtime/crab/src/lib.rs index a60cc56d0..5fb5d48d9 100644 --- a/runtime/crab/src/lib.rs +++ b/runtime/crab/src/lib.rs @@ -65,7 +65,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - (migration::CustomOnRuntimeUpgrade, darwinia_staking::migration::v2::MigrateToV2), + migration::CustomOnRuntimeUpgrade, >; /// Runtime version. diff --git a/runtime/crab/src/migration.rs b/runtime/crab/src/migration.rs index d30cb86e1..0c9a3c67d 100644 --- a/runtime/crab/src/migration.rs +++ b/runtime/crab/src/migration.rs @@ -29,13 +29,6 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { fn pre_upgrade() -> Result, sp_runtime::DispatchError> { log::info!("pre"); - assert!(Balances::free_balance(ROOT) != 0); - - >::iter().for_each(|(k, v)| { - log::info!("{k:?}"); - log::info!("{v:?}"); - }); - Ok(Vec::new()) } @@ -43,15 +36,6 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { log::info!("post"); - assert!(Balances::free_balance(ROOT) == 0); - - >::iter().for_each(|(k, v)| { - log::info!("{k:?}"); - log::info!("{v:?}"); - - assert!(!v.is_empty()); - }); - Ok(()) } @@ -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", - ]; - - >::iter().for_each(|(k, mut v)| { - if v.is_empty() { - // Clear the storage entry if the vector is empty. - - >::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. - - >::remove(k); - } else { - >::insert(k, v); - } - - w += 1; + if let Ok(a) = + array_bytes::hex_n_into::<_, AccountId, 20>("0xacfa39b864e42d1bd3792783a571d2958af0bf1f") + { + let mut l = >::get(a); + + if let Some(i) = l.iter().position(|l| l.id == *b"phrelect") { + l.remove(i); + + if l.is_empty() { + >::remove(a); + } else { + >::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 = >::get(a) + .into_iter() + .map(|f| f.amount) + .max() + .unwrap_or(0); + let frozen = >::get(a) + .into_iter() + .map(|l| l.amount) + .max() + .unwrap_or(0); + let frozen = freeze.max(frozen); + let _ = >::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() - ::DbWeight::get().reads_writes(r as _, w as _) + ::DbWeight::get().reads_writes(1, 23) } diff --git a/runtime/darwinia/src/lib.rs b/runtime/darwinia/src/lib.rs index 14d978a58..3aa155440 100644 --- a/runtime/darwinia/src/lib.rs +++ b/runtime/darwinia/src/lib.rs @@ -65,7 +65,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - (migration::CustomOnRuntimeUpgrade, darwinia_staking::migration::v2::MigrateToV2), + migration::CustomOnRuntimeUpgrade, >; /// Runtime version. diff --git a/runtime/darwinia/src/migration.rs b/runtime/darwinia/src/migration.rs index a1d82a14d..a4bd31f58 100644 --- a/runtime/darwinia/src/migration.rs +++ b/runtime/darwinia/src/migration.rs @@ -29,13 +29,6 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { fn pre_upgrade() -> Result, sp_runtime::DispatchError> { log::info!("pre"); - assert!(migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[])); - - >::iter().for_each(|(k, v)| { - log::info!("{k:?}"); - log::info!("{v:?}"); - }); - Ok(Vec::new()) } @@ -43,15 +36,6 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { log::info!("post"); - assert!(!migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[])); - - >::iter().for_each(|(k, v)| { - log::info!("{k:?}"); - log::info!("{v:?}"); - - assert!(!v.is_empty()); - }); - Ok(()) } @@ -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", - ]; - - >::iter().for_each(|(k, mut v)| { - if v.is_empty() { - // Clear the storage entry if the vector is empty. - - >::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. - - >::remove(k); - } else { - >::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 = >::get(a) + .into_iter() + .map(|f| f.amount) + .max() + .unwrap_or(0); + let frozen = >::get(a) + .into_iter() + .map(|l| l.amount) + .max() + .unwrap_or(0); + let frozen = freeze.max(frozen); + let _ = >::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", @@ -135,5 +90,5 @@ fn migrate() -> frame_support::weights::Weight { ); // frame_support::weights::Weight::zero() - ::DbWeight::get().reads_writes(r as _, w as _) + ::DbWeight::get().reads_writes(0, 118) }