Skip to content

Commit

Permalink
State types check (#230)
Browse files Browse the repository at this point in the history
* Check

* Use type

* Update processor files

* Find others

* Format

* Default pangolin

* Fix review
  • Loading branch information
boundless-forest authored Jan 28, 2023
1 parent 8ba1ff7 commit ff23cac
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 72 deletions.
8 changes: 4 additions & 4 deletions tool/state-processor/src/balances/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use crate::*;
type Locks = Vec<BalanceLock>;

impl<S> Processor<S> {
pub fn process_balances(&mut self) -> (u128, u128) {
pub fn process_balances(&mut self) -> (Balance, Balance) {
// Balances storage items.
// https://github.dev/darwinia-network/substrate/blob/darwinia-v0.12.5/frame/balances/src/lib.rs#L486

let mut solo_ring_total_issuance = u128::default();
let mut solo_kton_total_issuance = u128::default();
let mut solo_ring_total_issuance = Balance::default();
let mut solo_kton_total_issuance = Balance::default();
let mut solo_ring_locks = <Map<Locks>>::default();
let mut solo_kton_locks = <Map<Locks>>::default();
let mut para_ring_total_issuance = u128::default();
let mut para_ring_total_issuance = Balance::default();

log::info!("take solo `Balances::TotalIssuance`, `Kton::TotalIssuance`, `Balances::Locks` and `Kton::Locks`");
self.solo_state
Expand Down
4 changes: 2 additions & 2 deletions tool/state-processor/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ impl<S> Processor<S> {
/// Only care about the solo chain, since parachains don't have identity now.
pub fn process_identity(&mut self) -> &mut Self {
let mut identities = <Map<Registration>>::default();
let mut registrars = Vec::<Option<RegistrarInfo<[u8; 32]>>>::default();
let mut subs_of = Map::<(u128, Vec<[u8; 32]>)>::default();
let mut registrars = Vec::<Option<RegistrarInfo<AccountId32>>>::default();
let mut subs_of = Map::<(Balance, Vec<AccountId32>)>::default();

log::info!("take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SuperOf` and `Identity::SuperOf`");
self.solo_state
Expand Down
2 changes: 1 addition & 1 deletion tool/state-processor/src/indices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl<S> Processor<S> {
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();
let mut accounts = <Map<(AccountId32, Balance, bool)>>::default();

log::info!("take solo `Indices::Accounts`");
self.solo_state.take_map(b"Indices", b"Accounts", &mut accounts, get_identity_key);
Expand Down
2 changes: 1 addition & 1 deletion tool/state-processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() -> Result<()> {
std::env::set_var("RUST_LOG", "state_processor");
pretty_env_logger::init();

// <Processor<Pangolin>>::new()?.process()?;
// <Processor<Pangoro>>::new()?.process()?;
<Processor<Pangolin>>::new()?.test().process()?;
// <Processor<Darwinia>>::new()?.process()?;
// <Processor<Crab>>::new()?.process()?;
Expand Down
2 changes: 1 addition & 1 deletion tool/state-processor/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<S> Processor<S> {
}

// The size of encoded `pallet_proxy::ProxyDefinition` is 37 bytes.
let mut proxies = <Map<(Vec<[u8; 37]>, u128)>>::default();
let mut proxies = <Map<(Vec<[u8; 37]>, Balance)>>::default();

log::info!("take solo `Proxy::Proxies`");
self.solo_state.take_map(b"Proxy", b"Proxies", &mut proxies, get_identity_key);
Expand Down
12 changes: 6 additions & 6 deletions tool/state-processor/src/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ impl<S> Processor<S> {
// Storage items.
// https://github.dev/darwinia-network/darwinia-common/blob/darwinia-v0.12.5/frame/staking/src/lib.rs#L560
let mut ledgers = <Map<StakingLedger>>::default();
let mut ring_pool_storage = u128::default();
let mut kton_pool_storage = u128::default();
let mut ring_pool = u128::default();
let mut kton_pool = u128::default();
let mut ring_pool_storage = Balance::default();
let mut kton_pool_storage = Balance::default();
let mut ring_pool = Balance::default();
let mut kton_pool = Balance::default();
let mut elapsed_time = u64::default();

log::info!("take solo `Staking::Ledger`, `Staking::RingPool`, `Staking::KtonPool` and `Staking::LivingTime`");
Expand All @@ -33,7 +33,7 @@ impl<S> Processor<S> {
let mut staked_deposits = Vec::default();

if !v.deposit_items.is_empty() {
let mut deposit_ring = u128::default();
let mut deposit_ring = Balance::default();

self.shell_state.insert_raw_key_value(
deposit_k,
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<S> Processor<S> {
);

log::info!("set `Staking::ElapsedTime`");
self.shell_state.insert_value(b"Staking", b"ElapsedTime", "", elapsed_time as u128);
self.shell_state.insert_value(b"Staking", b"ElapsedTime", "", elapsed_time as Moment);

self
}
Expand Down
22 changes: 11 additions & 11 deletions tool/state-processor/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::*;
#[derive(Debug)]
pub struct AccountAll {
pub nonce: u32,
pub consumers: u32,
pub providers: u32,
pub sufficients: u32,
pub ring: u128,
pub ring_reserved: u128,
pub consumers: RefCount,
pub providers: RefCount,
pub sufficients: RefCount,
pub ring: Balance,
pub ring_reserved: Balance,
pub ring_locks: Vec<BalanceLock>,
pub kton: u128,
pub kton_reserved: u128,
pub kton: Balance,
pub kton_reserved: Balance,
pub kton_locks: Vec<BalanceLock>,
}

Expand All @@ -29,8 +29,8 @@ where
let (ring_total_issuance_storage, kton_total_issuance_storage) = self.process_balances();
let (solo_validators, para_collators) = self.process_session();
let mut accounts = Map::default();
let mut ring_total_issuance = u128::default();
let mut kton_total_issuance = u128::default();
let mut ring_total_issuance = Balance::default();
let mut kton_total_issuance = Balance::default();

// Skip for testnets, due to https://github.com/paritytech/substrate/issues/13172.
// log::info!("decrease solo pallet-session account references");
Expand Down Expand Up @@ -269,7 +269,7 @@ where
}
}

fn try_get_evm_address(key: &str) -> Option<[u8; 20]> {
fn try_get_evm_address(key: &str) -> Option<AccountId20> {
let k = array_bytes::hex2bytes_unchecked(key);

if is_evm_address(&k) {
Expand All @@ -282,7 +282,7 @@ fn try_get_evm_address(key: &str) -> Option<[u8; 20]> {
fn new_kton_account(
account_info: &mut AccountInfo,
asset_details: &mut AssetDetails,
balance: u128,
balance: Balance,
) -> AssetAccount {
account_info.sufficients += 1;
asset_details.accounts += 1;
Expand Down
5 changes: 3 additions & 2 deletions tool/state-processor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::*;
struct Tester {
// solo chain
solo_accounts: Map<AccountInfo>,
solo_remaining_ring: Map<u128>,
solo_remaining_kton: Map<u128>,
solo_remaining_ring: Map<Balance>,
solo_remaining_kton: Map<Balance>,
solo_evm_codes: Map<Vec<u8>>,
// para chain
para_accounts: Map<AccountInfo>,
Expand Down Expand Up @@ -467,6 +467,7 @@ fn deposit_items_migrate() {
assert_eq!(migrated_deposits.len(), ledger.deposit_items.len());
ledger.deposit_items.iter().zip(migrated_deposits.iter()).for_each(|(old, new)| {
assert_eq!(new.value, old.value * GWEI);
assert_eq!(new.start_time, old.start_time as u128);
assert_eq!(new.expired_time, old.expire_time as u128);
assert!(new.in_use);
});
Expand Down
96 changes: 52 additions & 44 deletions tool/state-processor/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@ use std::iter::once;
use enumflags2::{bitflags, BitFlags};
use parity_scale_codec::{Decode, Encode, EncodeLike, Error, Input};

pub type Balance = u128;
pub type AccountId20 = [u8; 20];
pub type AccountId32 = [u8; 32];
pub type BlockNumber = u32;
pub type RefCount = u32;
pub type Moment = u128;
pub type DepositId = u16;

#[derive(Default, Debug, PartialEq, Eq, Encode, Decode)]
pub struct AccountInfo {
pub nonce: u32,
pub consumers: u32,
pub providers: u32,
pub sufficients: u32,
pub consumers: RefCount,
pub providers: RefCount,
pub sufficients: RefCount,
pub data: AccountData,
}
#[derive(Default, Debug, PartialEq, Eq, 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,
pub free: Balance,
pub reserved: Balance,
pub free_kton_or_misc_frozen: Balance,
pub reserved_kton_or_fee_frozen: Balance,
}

#[derive(Default, Debug, Encode, Decode)]
pub struct BalanceLock {
pub id: [u8; 8],
pub amount: u128,
pub amount: Balance,
pub reasons: Reasons,
}

Expand All @@ -43,13 +51,13 @@ impl Default for Reasons {
// https://github.dev/paritytech/substrate/blob/polkadot-v0.9.30/frame/assets/src/types.rs#L33
#[derive(Default, Debug, Encode, Decode)]
pub struct AssetDetails {
pub owner: [u8; 20],
pub issuer: [u8; 20],
pub admin: [u8; 20],
pub freezer: [u8; 20],
pub supply: u128,
pub deposit: u128,
pub min_balance: u128,
pub owner: AccountId20,
pub issuer: AccountId20,
pub admin: AccountId20,
pub freezer: AccountId20,
pub supply: Balance,
pub deposit: Balance,
pub min_balance: Balance,
pub is_sufficient: bool,
pub accounts: u32,
pub sufficients: u32,
Expand All @@ -60,7 +68,7 @@ pub struct AssetDetails {
// https://github.dev/paritytech/substrate/blob/polkadot-v0.9.30/frame/assets/src/types.rs#L115
#[derive(Default, Debug, Encode, Decode)]
pub struct AssetAccount {
pub balance: u128,
pub balance: Balance,
pub is_frozen: bool,
pub reason: ExistenceReason,
pub extra: (),
Expand All @@ -74,7 +82,7 @@ pub enum ExistenceReason {
#[codec(index = 1)]
Sufficient,
#[codec(index = 2)]
DepositHeld(u128),
DepositHeld(Balance),
#[codec(index = 3)]
DepositRefunded,
}
Expand All @@ -87,14 +95,14 @@ impl Default for ExistenceReason {
// https://github.dev/paritytech/substrate/blob/polkadot-v0.9.30/frame/assets/src/types.rs#L73
#[derive(Debug, Encode, Decode)]
pub struct Approval {
pub amount: u128,
pub deposit: u128,
pub amount: Balance,
pub deposit: Balance,
}

// https://github.dev/paritytech/substrate/blob/polkadot-v0.9.30/frame/assets/src/types.rs#L127
#[derive(Clone, Default, Encode, Decode)]
pub struct AssetMetadata {
pub deposit: u128,
pub deposit: Balance,
pub name: Vec<u8>,
pub symbol: Vec<u8>,
pub decimals: u8,
Expand All @@ -103,29 +111,29 @@ pub struct AssetMetadata {

#[derive(Default, Debug, Encode, Decode)]
pub struct VestingInfo {
pub locked: u128,
pub per_block: u128,
pub starting_block: u32,
pub locked: Balance,
pub per_block: Balance,
pub starting_block: BlockNumber,
}

#[derive(Default, Debug, Encode, Decode)]
pub struct Deposit {
pub id: u16,
pub value: u128,
pub start_time: u128,
pub expired_time: u128,
pub id: DepositId,
pub value: Balance,
pub start_time: Moment,
pub expired_time: Moment,
pub in_use: bool,
}

#[derive(Default, Debug, Encode, Decode)]
pub struct StakingLedger {
pub stash: [u8; 32],
pub stash: AccountId32,
#[codec(compact)]
pub active: u128,
pub active: Balance,
#[codec(compact)]
pub active_deposit_ring: u128,
pub active_deposit_ring: Balance,
#[codec(compact)]
pub active_kton: u128,
pub active_kton: Balance,
pub deposit_items: Vec<TimeDepositItem>,
pub ring_staking_lock: StakingLock,
pub kton_staking_lock: StakingLock,
Expand All @@ -134,37 +142,37 @@ pub struct StakingLedger {
#[derive(Default, Debug, Encode, Decode)]
pub struct TimeDepositItem {
#[codec(compact)]
pub value: u128,
pub value: Balance,
#[codec(compact)]
pub start_time: u64,
#[codec(compact)]
pub expire_time: u64,
}
#[derive(Default, Debug, Encode, Decode)]
pub struct StakingLock {
pub staking_amount: u128,
pub staking_amount: Balance,
pub unbondings: Vec<Unbonding>,
}
#[derive(Default, Debug, Encode, Decode)]
pub struct Unbonding {
pub amount: u128,
pub until: u32,
pub amount: Balance,
pub until: BlockNumber,
}

#[derive(Default, Debug, Encode, Decode)]
pub struct Ledger {
pub staked_ring: u128,
pub staked_kton: u128,
pub staked_deposits: Vec<u16>,
pub unstaking_ring: Vec<(u128, u32)>,
pub unstaking_kton: Vec<(u128, u32)>,
pub unstaking_deposits: Vec<(u16, u32)>,
pub staked_ring: Balance,
pub staked_kton: Balance,
pub staked_deposits: Vec<DepositId>,
pub unstaking_ring: Vec<(Balance, BlockNumber)>,
pub unstaking_kton: Vec<(Balance, BlockNumber)>,
pub unstaking_deposits: Vec<(DepositId, BlockNumber)>,
}

#[derive(Default, Debug, Encode)]
pub struct Registration {
pub judgements: Vec<(u32, Judgement)>,
pub deposit: u128,
pub deposit: Balance,
pub info: IdentityInfo,
}
impl Decode for Registration {
Expand All @@ -176,7 +184,7 @@ impl Decode for Registration {
#[derive(Debug, PartialEq, Eq, Encode, Decode)]
pub enum Judgement {
Unknown,
FeePaid(u128),
FeePaid(Balance),
Reasonable,
KnownGood,
OutOfDate,
Expand Down Expand Up @@ -288,7 +296,7 @@ impl<'a, T: Input> Input for AppendZerosInput<'a, T> {
#[derive(Debug, Encode, Decode, PartialEq, Eq)]
pub struct RegistrarInfo<A> {
pub account: A,
pub fee: u128,
pub fee: Balance,
pub fields: IdentityFields,
}
#[derive(Debug, PartialEq, Eq)]
Expand Down

0 comments on commit ff23cac

Please sign in to comment.