diff --git a/tool/state-processor/src/indices/README.md b/tool/state-processor/src/indices/README.md new file mode 100644 index 000000000..de0265283 --- /dev/null +++ b/tool/state-processor/src/indices/README.md @@ -0,0 +1,3 @@ +### Process steps +- take solo `Indices::Accounts` +- adjust the reserved's decimals then free solo `Indices` reservations diff --git a/tool/state-processor/src/indices/mod.rs b/tool/state-processor/src/indices/mod.rs new file mode 100644 index 000000000..499e401a4 --- /dev/null +++ b/tool/state-processor/src/indices/mod.rs @@ -0,0 +1,22 @@ +// darwinia +use crate::*; + +impl Processor { + pub fn process_indices(&mut self) -> &mut Self { + // Storage items. + // https://github.dev/darwinia-network/substrate/blob/darwinia-v0.12.5/frame/indices/src/lib.rs#L291 + let mut accounts = >::default(); + + log::info!("take solo `Indices::Accounts`"); + self.solo_state.take_map(b"Indices", b"Accounts", &mut accounts, get_identity_key); + + // https://github.dev/darwinia-network/substrate/blob/darwinia-v0.12.5/frame/indices/src/lib.rs#L154 + log::info!("adjust the reserved's decimals then free solo `Indices` reservations"); + accounts.into_iter().for_each(|(_, (a, mut v, _))| { + v.adjust(); + self.shell_state.unreserve(a, v); + }); + + self + } +} diff --git a/tool/state-processor/src/main.rs b/tool/state-processor/src/main.rs index f375bb67b..35a5fc250 100644 --- a/tool/state-processor/src/main.rs +++ b/tool/state-processor/src/main.rs @@ -1,5 +1,6 @@ mod balances; mod evm; +mod indices; mod staking; mod system; mod vesting; @@ -65,7 +66,7 @@ impl Processor { assert!(*_guard != 0); - self.process_system().process_vesting().process_staking().process_evm(); + self.process_system().process_indices().process_vesting().process_staking().process_evm(); self.save() } @@ -148,12 +149,18 @@ impl State { if let Some(v) = self.0.get(&key) { match decode(v) { Ok(v) => *value = v, - Err(e) => log::warn!( + Err(e) => log::error!( "failed to decode `{}::{}::{hash}({v})`, due to `{e}`", String::from_utf8_lossy(pallet), String::from_utf8_lossy(item), ), } + } else { + log::error!( + "key not found `{}::{}::{hash}`", + String::from_utf8_lossy(pallet), + String::from_utf8_lossy(item), + ); } self @@ -168,12 +175,18 @@ impl State { if let Some(v) = self.0.remove(&key) { match decode(&v) { Ok(v) => *value = v, - Err(e) => log::warn!( + Err(e) => log::error!( "failed to decode `{}::{}::{hash}({v})`, due to `{e}`", String::from_utf8_lossy(pallet), String::from_utf8_lossy(item) ), } + } else { + log::error!( + "key not found `{}::{}::{hash}`", + String::from_utf8_lossy(pallet), + String::from_utf8_lossy(item), + ); } self @@ -188,6 +201,22 @@ impl State { self } + fn mutate_value(&mut self, pallet: &[u8], item: &[u8], hash: &str, f: F) -> &mut Self + where + D: Default + Encode + Decode, + F: FnOnce(&mut D), + { + let mut v = D::default(); + + self.get_value(pallet, item, hash, &mut v); + + f(&mut v); + + self.insert_value(pallet, item, hash, v); + + self + } + fn take_map( &mut self, pallet: &[u8], @@ -208,7 +237,7 @@ impl State { Ok(v) => { buffer.insert(process_key(full_key, &prefix), v); }, - Err(e) => log::warn!("failed to decode `{full_key}:{v}`, due to `{e}`"), + Err(e) => log::error!("failed to decode `{full_key}:{v}`, due to `{e}`"), } false @@ -240,9 +269,24 @@ impl State { self } + // fn inc_consumers(&mut self, who: &str) {} + // fn transfer(&mut self, from: &str, to: &str, amount: u128) {} - // fn inc_consumers(&mut self, who: &str) {} + fn unreserve(&mut self, who: A, amount: u128) + where + A: AsRef<[u8]>, + { + self.mutate_value( + b"System", + b"Account", + &blake2_128_concat_to_string(who), + |a: &mut AccountInfo| { + a.data.free += amount; + a.data.reserved -= amount; + }, + ); + } } fn from_file(path: &str) -> Result @@ -303,3 +347,10 @@ fn get_last_64(key: &str) -> String { fn replace_first_match(key: &str, from: &str, to: &str) -> String { key.replacen(from, to, 1) } + +fn blake2_128_concat_to_string(data: D) -> String +where + D: AsRef<[u8]>, +{ + array_bytes::bytes2hex("", subhasher::blake2_128_concat(data)) +} diff --git a/tool/state-processor/src/staking/mod.rs b/tool/state-processor/src/staking/mod.rs index a6b6d5b4c..672e1983f 100644 --- a/tool/state-processor/src/staking/mod.rs +++ b/tool/state-processor/src/staking/mod.rs @@ -32,7 +32,7 @@ impl Processor { ledgers.into_iter().for_each(|(_, mut v)| { v.adjust(); - let hash_k = array_bytes::bytes2hex("", subhasher::blake2_128_concat(v.stash)); + let hash_k = blake2_128_concat_to_string(v.stash); let deposit_k = format!("{deposit_ik}{hash_k}"); let staking_k = format!("{staking_ik}{hash_k}"); let mut staked_deposits = Vec::default(); diff --git a/tool/state-processor/src/system/mod.rs b/tool/state-processor/src/system/mod.rs index 812a4d25c..fef0c78d0 100644 --- a/tool/state-processor/src/system/mod.rs +++ b/tool/state-processor/src/system/mod.rs @@ -114,7 +114,7 @@ impl Processor { self.shell_state.insert_value( b"System", b"Account", - &array_bytes::bytes2hex("", subhasher::blake2_128_concat(k)), + &blake2_128_concat_to_string(k), a, ); // TODO: migrate kton balances. diff --git a/tool/state-processor/src/type_registry.rs b/tool/state-processor/src/type_registry.rs index b5e1c877d..ef2170053 100644 --- a/tool/state-processor/src/type_registry.rs +++ b/tool/state-processor/src/type_registry.rs @@ -3,7 +3,7 @@ use parity_scale_codec::{Decode, Encode}; pub const GWEI: u128 = 1_000_000_000; -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct AccountInfo { pub nonce: u32, pub consumers: u32, @@ -11,7 +11,7 @@ pub struct AccountInfo { pub sufficients: u32, pub data: AccountData, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct AccountData { pub free: u128, pub reserved: u128, @@ -19,7 +19,7 @@ pub struct AccountData { pub reserved_kton_or_fee_frozen: u128, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct BalanceLock { pub id: [u8; 8], pub amount: u128, @@ -32,15 +32,20 @@ pub enum Reasons { Misc = 1, All = 2, } +impl Default for Reasons { + fn default() -> Self { + Self::All + } +} -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct VestingInfo { pub locked: u128, pub per_block: u128, pub starting_block: u32, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct Deposit { pub id: u8, pub value: u128, @@ -48,7 +53,7 @@ pub struct Deposit { pub in_use: bool, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct StakingLedger { pub stash: [u8; 32], #[codec(compact)] @@ -62,7 +67,7 @@ pub struct StakingLedger { pub kton_staking_lock: StakingLock, pub claimed_rewards: Vec, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct TimeDepositItem { #[codec(compact)] pub value: u128, @@ -71,18 +76,18 @@ pub struct TimeDepositItem { #[codec(compact)] pub expire_time: u64, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct StakingLock { pub staking_amount: u128, pub unbondings: Vec, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct Unbonding { pub amount: u128, pub until: u32, } -#[derive(Debug, Encode, Decode)] +#[derive(Default, Debug, Encode, Decode)] pub struct Ledger { pub staked_ring: u128, pub staked_kton: u128,