From f15a0bc992eb84483b15ec432e53e1b001d112fe Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 9 Jan 2023 18:33:39 +0800 Subject: [PATCH] Improve existing check (#191) * Improve existing check * Remove unused variable --- tool/state-processor/src/balances/mod.rs | 25 +++++++++--------------- tool/state-processor/src/processor.rs | 8 ++------ tool/state-processor/src/proxy/mod.rs | 15 +++++++------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/tool/state-processor/src/balances/mod.rs b/tool/state-processor/src/balances/mod.rs index 83328bdcb..996a85aef 100644 --- a/tool/state-processor/src/balances/mod.rs +++ b/tool/state-processor/src/balances/mod.rs @@ -12,7 +12,6 @@ impl Processor { let mut solo_kton_total_issuance = u128::default(); let mut solo_ring_locks = >::default(); let mut solo_kton_locks = >::default(); - let mut para_ring_locks = >::default(); let mut para_ring_total_issuance = u128::default(); log::info!("take solo `Balances::TotalIssuance`, `Kton::TotalIssuance`, `Balances::Locks` and `Kton::Locks`"); @@ -31,16 +30,16 @@ impl Processor { solo_kton_total_issuance.adjust(); log::info!("take para `Balances::TotalIssuance` and `Balances::Locks`"); - self.para_state - .take_value(b"Balances", b"TotalIssuance", "", &mut para_ring_total_issuance) - .take_map(b"Balances", b"Locks", &mut para_ring_locks, get_hashed_key); + self.para_state.take_value( + b"Balances", + b"TotalIssuance", + "", + &mut para_ring_total_issuance, + ); - log::info!("check solo ring locks, there should not be any `solo_ring_locks`"); - check_locks(solo_ring_locks); - log::info!("check solo kton locks, there should not be any `solo_kton_locks`"); - check_locks(solo_kton_locks); - log::info!("check para locks, there should not be any `para_ring_locks`"); - check_locks(para_ring_locks); + if self.para_state.exists(b"Balances", b"Locks") { + log::error!("check para `Balances::Locks`, it isn't empty"); + } (solo_ring_total_issuance + para_ring_total_issuance, solo_kton_total_issuance) } @@ -81,9 +80,3 @@ fn prune(locks: &mut Map) { !v.is_empty() }); } - -fn check_locks(locks: Map) { - locks - .into_iter() - .for_each(|(k, _)| log::error!("found unexpected locks of account({})", get_last_64(&k))); -} diff --git a/tool/state-processor/src/processor.rs b/tool/state-processor/src/processor.rs index eceff5a63..9a2bc782d 100644 --- a/tool/state-processor/src/processor.rs +++ b/tool/state-processor/src/processor.rs @@ -299,14 +299,10 @@ impl State { self.map.contains_key(key) } - pub fn starts_with(&self, key: &str) -> bool { - self.map.keys().into_iter().any(|k| k.starts_with(key)) + pub fn exists(&self, pallet: &[u8], item: &[u8]) -> bool { + self.map.keys().into_iter().any(|k| k.starts_with(&item_key(pallet, item))) } - // pub fn inc_consumers(&mut self, who: &str) {} - - // pub fn transfer(&mut self, from: &str, to: &str, amount: u128) {} - pub fn unreserve(&mut self, account_id_32: A, amount: u128) where A: AsRef<[u8]>, diff --git a/tool/state-processor/src/proxy/mod.rs b/tool/state-processor/src/proxy/mod.rs index aa5a222d6..080e6ed75 100644 --- a/tool/state-processor/src/proxy/mod.rs +++ b/tool/state-processor/src/proxy/mod.rs @@ -5,12 +5,11 @@ impl Processor { pub fn process_proxy(&mut self) -> &mut Self { // Storage items. // https://github.dev/darwinia-network/substrate/blob/darwinia-v0.12.5/frame/proxy/src/lib.rs#L599 - let announcements_key = item_key(b"Proxy", b"Announcements"); - if self.solo_state.starts_with(&announcements_key) { - log::error!("The solo chain `Proxy::Announcements` not empty"); + if self.solo_state.exists(b"Proxy", b"Announcements") { + log::error!("check solo `Proxy::Announcements`, it isn't empty"); } - if self.para_state.starts_with(&announcements_key) { - log::error!("The para chain `Proxy::Announcements` not empty"); + if self.para_state.exists(b"Proxy", b"Announcements") { + log::error!("check para `Proxy::Announcements`, it isn't empty"); } // The size of encoded `pallet_proxy::ProxyDefinition` is 37 bytes. @@ -26,9 +25,9 @@ impl Processor { self.shell_state.unreserve(array_bytes::hex2bytes_unchecked(get_last_64(&a)), v); }); - // Make sure the Proxy::Proxies storage of para chain is empty. - if self.para_state.starts_with(&item_key(b"Proxy", b"Proxies")) { - log::error!("The para chain `Proxy::Proxies` not empty"); + // Make sure the para `Proxy::Proxies` is empty. + if self.para_state.exists(b"Proxy", b"Proxies") { + log::error!("check para `Proxy::Proxies`, it isn't empty"); } self