Skip to content

Commit

Permalink
Process indices & more utility fns (#151)
Browse files Browse the repository at this point in the history
* More tools

* Process indices

* More error logs

Signed-off-by: Xavier Lau <[email protected]>

Signed-off-by: Xavier Lau <[email protected]>
  • Loading branch information
AurevoirXavier authored Dec 21, 2022
1 parent 22e95f7 commit 08b703a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
3 changes: 3 additions & 0 deletions tool/state-processor/src/indices/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Process steps
- take solo `Indices::Accounts`
- adjust the reserved's decimals then free solo `Indices` reservations
22 changes: 22 additions & 0 deletions tool/state-processor/src/indices/mod.rs
Original file line number Diff line number Diff line change
@@ -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 = <Map<([u8; 32], u128, bool)>>::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
}
}
61 changes: 56 additions & 5 deletions tool/state-processor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod balances;
mod evm;
mod indices;
mod staking;
mod system;
mod vesting;
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -188,6 +201,22 @@ impl State {
self
}

fn mutate_value<D, F>(&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<D, F>(
&mut self,
pallet: &[u8],
Expand All @@ -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
Expand Down Expand Up @@ -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<A>(&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<D>(path: &str) -> Result<D>
Expand Down Expand Up @@ -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<D>(data: D) -> String
where
D: AsRef<[u8]>,
{
array_bytes::bytes2hex("", subhasher::blake2_128_concat(data))
}
2 changes: 1 addition & 1 deletion tool/state-processor/src/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tool/state-processor/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 15 additions & 10 deletions tool/state-processor/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ 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,
pub providers: u32,
pub sufficients: u32,
pub data: AccountData,
}
#[derive(Debug, Encode, Decode)]
#[derive(Default, Debug, Encode, Decode)]
pub struct AccountData {
pub free: u128,
pub reserved: u128,
pub free_kton_or_misc_frozen: u128,
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,
Expand All @@ -32,23 +32,28 @@ 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,
pub expired_time: u128,
pub in_use: bool,
}

#[derive(Debug, Encode, Decode)]
#[derive(Default, Debug, Encode, Decode)]
pub struct StakingLedger {
pub stash: [u8; 32],
#[codec(compact)]
Expand All @@ -62,7 +67,7 @@ pub struct StakingLedger {
pub kton_staking_lock: StakingLock,
pub claimed_rewards: Vec<u32>,
}
#[derive(Debug, Encode, Decode)]
#[derive(Default, Debug, Encode, Decode)]
pub struct TimeDepositItem {
#[codec(compact)]
pub value: u128,
Expand All @@ -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<Unbonding>,
}
#[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,
Expand Down

0 comments on commit 08b703a

Please sign in to comment.