Skip to content

Commit

Permalink
Rebuild accounts' reservation (#242)
Browse files Browse the repository at this point in the history
* Set reservation to zero

* Configure genesis collator

* Rebuild accounts' reservation and update tests

* Update tests
  • Loading branch information
aurexav authored Feb 1, 2023
1 parent 0626861 commit 3399b66
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 260 deletions.
5 changes: 5 additions & 0 deletions tool/state-processor/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tool/state-processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ tar = { version = "0.4" }
ureq = { version = "2.6" }
zstd = { version = "0.12" }

# darwinia
dc-types = { path = "../../core/types" }

# hack-ink
subhasher = { git = "https://github.com/hack-ink/subalfred" }
subspector = { git = "https://github.com/hack-ink/subalfred" }
Expand Down
52 changes: 51 additions & 1 deletion tool/state-processor/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub const GWEI: u128 = 1_000_000_000;
// darwinia
use crate::*;

pub const KTON_ID: u64 = 1026;
// https://github.dev/darwinia-network/darwinia-2.0/blob/c9fdfa170501648102bd0137c0437e367e743770/runtime/common/src/gov_origin.rs#L46
pub const ROOT: [u8; 20] = [0x72, 0x6f, 0x6f, 0x74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Expand All @@ -8,29 +10,77 @@ pub trait Configurable {
// This account's balance will be burned.
// Please make sure no one transfer balance to this account.
const PARACHAIN_BACKING: &'static str;

// Make sure these account doesn't exist in the old chains.
// To prevent their data get overridden.
fn genesis_collator() -> Vec<AccountId20> {
vec![
array_bytes::hex2array_unchecked("0x0eef9fabb6eb6fed2ab24a842931f8950426070a"),
array_bytes::hex2array_unchecked("0xa858cde8f6cf178786578a3b0becf5c27d18300c"),
array_bytes::hex2array_unchecked("0x986b41d07776aa48f6d7a80caad49485f9a71714"),
]
}

// https://github.com/paritytech/substrate/blob/129fee774a6d185d117a57fd1e81b3d0d05ad747/frame/identity/src/lib.rs#L113
fn basic_deposit() -> Balance;

// https://github.com/paritytech/substrate/blob/129fee774a6d185d117a57fd1e81b3d0d05ad747/frame/identity/src/lib.rs#L117
fn field_deposit() -> Balance;
}
impl Configurable for () {
const NAME: &'static str = "";
const PARACHAIN_BACKING: &'static str = "";

fn basic_deposit() -> Balance {
0
}

fn field_deposit() -> Balance {
0
}
}

pub struct Darwinia;
impl Configurable for Darwinia {
const NAME: &'static str = "darwinia";
const PARACHAIN_BACKING: &'static str =
"0x1000000000000000000000000000000000000000000000000000000000000000";

fn basic_deposit() -> Balance {
100 * UNIT + 258 * 100 * MICROUNIT
}

fn field_deposit() -> Balance {
66 * 100 * MICROUNIT
}
}

pub struct Crab;
impl Configurable for Crab {
const NAME: &'static str = "crab";
const PARACHAIN_BACKING: &'static str =
"0x64766d3a0000000000000035a314e53e2fddfeca7b743042aacfb1abaf0adea3";

fn basic_deposit() -> Balance {
100 * UNIT + 258 * 100 * MICROUNIT
}

fn field_deposit() -> Balance {
66 * 100 * MICROUNIT
}
}

pub struct Pangolin;
impl Configurable for Pangolin {
const NAME: &'static str = "pangolin";
const PARACHAIN_BACKING: &'static str =
"0x64766d3a000000000000008c585f9791ee5b4b23fe82888ce576dbb69607ebe9";

fn basic_deposit() -> Balance {
100 * UNIT + 258 * 100 * MICROUNIT
}

fn field_deposit() -> Balance {
66 * 100 * MICROUNIT
}
}
7 changes: 3 additions & 4 deletions tool/state-processor/src/identity/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
### Process steps
1. take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SubsOf`
2. free super_id's reservation
3. adjust identities' deposit and judgement decimal
4. set `AccountMigration::Identities`
5. truncate registrar account id and adjust registrars fee decimal
6. set `Identity::Registrars
3. adjust registrations and set `AccountMigration::Identities`
4. truncate registrar account id and adjust registrars fee decimal
5. set `Identity::Registrars
46 changes: 28 additions & 18 deletions tool/state-processor/src/identity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
// darwinia
use crate::*;

impl<S> Processor<S> {
impl<S> Processor<S>
where
S: Configurable,
{
/// 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<AccountId32>>>::default();
let mut subs_of = Map::<(Balance, Vec<AccountId32>)>::default();

log::info!("take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SubsOf`");
self.solo_state
.take_map(b"Identity", b"IdentityOf", &mut identities, get_hashed_key)
.take_value(b"Identity", b"Registrars", "", &mut registrars)
.take_map(b"Identity", b"SubsOf", &mut subs_of, get_last_64_key);
.take_value(b"Identity", b"Registrars", "", &mut registrars);

log::info!("free super_id's reservation");
subs_of.into_iter().for_each(|(super_id, (mut subs_deposit, _))| {
subs_deposit.adjust();
log::info!("adjust registrations and set `AccountMigration::Identities`");
identities.into_iter().for_each(|(k, mut v)| {
v.adjust();

self.shell_state
.unreserve(array_bytes::hex2array_unchecked::<_, 32>(super_id), subs_deposit);
});

log::info!("adjust identities' deposit and judgement decimal");
identities.iter_mut().for_each(|(_, v)| v.adjust());
let a = get_last_64(&k);
// Calculate the identity reservation.
//
// https://github.com/paritytech/substrate/blob/129fee774a6d185d117a57fd1e81b3d0d05ad747/frame/identity/src/lib.rs#L364
let r = S::basic_deposit() + v.info.additional.len() as Balance * S::field_deposit();
// Calculate the judgement reservation.
//
// https://github.com/paritytech/substrate/blob/129fee774a6d185d117a57fd1e81b3d0d05ad747/frame/identity/src/lib.rs#L564
let rj = v.judgements.iter().fold(0, |acc, (i, _)| {
registrars
.get(*i as usize)
.and_then(|r| r.as_ref().map(|r| acc + r.fee))
.unwrap_or_else(|| {
log::error!("failed to find a registrar for `Account({a})`");

log::info!("set `AccountMigration::Identities`");
{
let ik = item_key(b"AccountMigration", b"Identities");
acc
})
});

self.shell_state.insert_map(identities, |h| format!("{ik}{h}"));
}
self.shell_state.reserve(a, r + rj);
self.shell_state.insert_value(b"AccountMigration", b"Identities", &k, v);
});

log::info!("truncate registrar account id and adjust registrars fee decimal");
let registrars = registrars
Expand Down
3 changes: 0 additions & 3 deletions tool/state-processor/src/indices/README.md

This file was deleted.

22 changes: 0 additions & 22 deletions tool/state-processor/src/indices/mod.rs

This file was deleted.

2 changes: 0 additions & 2 deletions tool/state-processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use type_registry::*;
mod balances;
mod evm;
mod identity;
mod indices;
mod proxy;
mod session;
mod staking;
mod system;
Expand Down
25 changes: 4 additions & 21 deletions tool/state-processor/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ where

assert!(*_guard != 0);

self.process_system()
.process_indices()
.process_identity()
.process_vesting()
.process_proxy()
.process_staking()
.process_evm();
self.process_system().process_identity().process_vesting().process_staking().process_evm();

self
}
Expand Down Expand Up @@ -199,8 +193,7 @@ impl<R> State<R> {
self.map.keys().into_iter().any(|k| k.starts_with(&item_key(pallet, item)))
}

// Remove this after: https://github.com/darwinia-network/darwinia-2.0/issues/240
pub fn unreserve<A>(&mut self, account_id_32: A, amount: u128)
pub fn reserve<A>(&mut self, account_id_32: A, amount: u128)
where
A: AsRef<[u8]>,
{
Expand All @@ -212,18 +205,8 @@ impl<R> State<R> {
};

self.mutate_value(p, i, &blake2_128_concat_to_string(h), |a: &mut AccountInfo| {
a.data.free += amount;

if let Some(r) = a.data.reserved.checked_sub(amount) {
a.data.reserved = r;
} else {
log::error!(
"insufficient reservation of account({})",
array_bytes::bytes2hex("0x", account_id_32)
);

a.data.reserved = 0;
}
a.data.free -= amount;
a.data.reserved += amount;
});
}

Expand Down
3 changes: 0 additions & 3 deletions tool/state-processor/src/proxy/README.md

This file was deleted.

35 changes: 0 additions & 35 deletions tool/state-processor/src/proxy/mod.rs

This file was deleted.

Loading

0 comments on commit 3399b66

Please sign in to comment.