Skip to content

Commit

Permalink
Kton state migrate (#137)
Browse files Browse the repository at this point in the history
* Step 1

* Step 2

* Step 3

* Step 4

* Finish processor side

* Add runtime side

* Add metadata insert

* Fix approvals inc

* Fresh new details

* Use Vec

* Adapt new way

* Code clean

* Remove `sp-core`

* Fix todo and review

* Fix link and format
  • Loading branch information
boundless-forest authored Dec 23, 2022
1 parent 56164de commit e775dcd
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions pallet/account-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dc-primitives = { default-features = false, path = "../../core/primitives" }
# substrate
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-assets = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-vesting = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
Expand All @@ -47,6 +48,7 @@ std = [
# paritytech
"frame-support/std",
"frame-system/std",
"pallet-assets/std",
"pallet-balances/std",
"pallet-vesting/std",
"sp-core/std",
Expand Down
60 changes: 59 additions & 1 deletion pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ use darwinia_staking::Ledger;
use dc_primitives::{AccountId as AccountId20, Balance, BlockNumber, Index};
// substrate
use frame_support::{
log,
log, migration,
pallet_prelude::*,
traits::{Currency, ExistenceRequirement::KeepAlive, LockableCurrency, WithdrawReasons},
StorageHasher,
};
use frame_system::{pallet_prelude::*, AccountInfo};
use pallet_assets::AssetAccount;
use pallet_balances::AccountData;
use pallet_vesting::VestingInfo;
use sp_core::sr25519::{Public, Signature};
Expand Down Expand Up @@ -118,6 +120,14 @@ pub mod pallet {
pub type Accounts<T: Config> =
StorageMap<_, Blake2_128Concat, AccountId32, AccountInfo<Index, AccountData<Balance>>>;

/// [`pallet_asset::AssetAccount`] data.
///
/// https://github.dev/paritytech/substrate/blob/polkadot-v0.9.30/frame/assets/src/types.rs#L115
#[pallet::storage]
#[pallet::getter(fn kton_account_of)]
pub type KtonAccounts<T: Config> =
StorageMap<_, Blake2_128Concat, AccountId32, AssetAccount<u128, u128, ()>>;

/// [`pallet_vesting::Vesting`] data.
///
/// <https://github.dev/paritytech/substrate/blob/19162e43be45817b44c7d48e50d03f074f60fbf4/frame/vesting/src/lib.rs#L188>
Expand Down Expand Up @@ -203,6 +213,54 @@ pub mod pallet {
<darwinia_staking::Ledgers<T>>::insert(to, l);
}

if let Some(a) = KtonAccounts::<T>::take(&from) {
frame_system::Pallet::<T>::inc_sufficients(&to);

migration::put_storage_value(
b"Assets",
b"Account",
&[
Blake2_128Concat::hash(&1026u64.encode()),
Blake2_128Concat::hash(&to.encode()),
]
.concat(),
a.encode(),
);
// The upstream structure cannot be accessed due to permission restrictions.
#[derive(Debug, Encode, Decode)]
pub struct AssetDetails {
pub owner: AccountId20,
pub issuer: AccountId20,
pub admin: AccountId20,
pub freezer: AccountId20,
pub supply: u128,
pub deposit: u128,
pub min_balance: u128,
pub is_sufficient: bool,
pub accounts: u32,
pub sufficients: u32,
pub approvals: u32,
pub is_frozen: bool,
}

if let Some(mut asset_details) = migration::get_storage_value::<AssetDetails>(
b"Assets",
b"Asset",
&Blake2_128Concat::hash(&1026u64.encode()),
) {
asset_details.accounts += 1;
asset_details.sufficients += 1;

// Fresh to new details
migration::put_storage_value(
b"Assets",
b"Asset",
&Blake2_128Concat::hash(&1026u64.encode()),
asset_details.encode(),
);
}
}

Self::deposit_event(Event::Migrated { from, to });

Ok(())
Expand Down
57 changes: 57 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.

1 change: 1 addition & 0 deletions tool/state-processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ log = { version = "0.4" }
once_cell = { version = "1.16" }
parity-scale-codec = { version = "3.2", features = ["derive"] }
pretty_env_logger = { version = "0.4" }
primitive-types = { version = "0.12.1", features = ["codec"] }
serde = { version = "1.0" }
serde_json = { version = "1.0" }

Expand Down
13 changes: 8 additions & 5 deletions tool/state-processor/src/system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
- remove para backing account - TODO
- check remaining sum - TODO
- XCM relate - TODO
- set KTON total issuances - TODO
- compare the calculated one with the storage one
- check remaining sum - TODO
- create KTON asset details
- set accounts
- if is EVM address
- insert to `Assets::Account` and `Assets::Approvals`(update kton asset details)
- insert to `System::Account`
- if is Substrate address
- reset the nonce
- insert to `AccountMigration::Accounts`
- calculate misc frozen and fee frozen
- insert to `AccountMigration::KtonAccounts`
- insert to `AccountMigration::Accounts`\
- set KTON total issuances
- compare the calculated one with the storage one
- check remaining sum - TODO
- set `Assets::Metadata`

- some remaining accounts, bridge endpoint accounts - TODO
- special accounts - TODO
Expand Down
99 changes: 93 additions & 6 deletions tool/state-processor/src/system/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// darwinia
use crate::*;
use subhasher::blake2_128_concat;

#[derive(Debug)]
pub struct AccountAll {
Expand Down Expand Up @@ -87,14 +88,24 @@ impl Processor {
log::info!("set `Balances::TotalIssuance`");
self.shell_state.insert_value(b"Balances", b"TotalIssuance", "", ring_total_issuance);

log::info!("`kton_total_issuance({kton_total_issuance})`");
log::info!("`kton_total_issuance_storage({kton_total_issuance_storage})`");
let mut kton_details = AssetDetails {
owner: ROOT,
issuer: ROOT,
admin: ROOT,
freezer: ROOT,
supply: kton_total_issuance,
deposit: 0,
min_balance: 1, // The same as the value in the runtime.
is_sufficient: true, // The same as the value in the runtime.
sufficients: 0,
accounts: 0,
approvals: 0,
is_frozen: false,
};

// TODO: set KTON total issuance

log::info!("update ring misc frozen and fee frozen");
log::info!("set `System::Account`");
log::info!("set `Balances::Locks`");
log::info!("set `Assets::Account` and `Assets::Approvals`");
accounts.into_iter().for_each(|(k, v)| {
let key = get_last_64(&k);
let mut a = AccountInfo {
Expand Down Expand Up @@ -122,20 +133,96 @@ impl Processor {
a.sufficients += 1;
}

if v.kton != 0 || v.kton_reserved != 0 {
let aa = AssetAccount {
balance: v.kton,
is_frozen: false,
reason: ExistenceReason::Sufficient,
extra: (),
};

a.sufficients += 1;
kton_details.accounts += 1;
kton_details.sufficients += 1;
// Note: this is double map structure in the pallet-assets.
self.shell_state.insert_value(
b"Assets",
b"Account",
&format!(
"{}{}",
array_bytes::bytes2hex("", blake2_128_concat(&KTON_ID.encode())),
array_bytes::bytes2hex("", blake2_128_concat(&k.encode())),
),
&aa,
);

// https://github.dev/darwinia-network/darwinia-common/blob/6a9392cfb9fe2c99b1c2b47d0c36125d61991bb7/frame/dvm/evm/precompiles/kton/src/lib.rs#L72
let mut approves = Map::<primitive_types::U256>::default();
self.solo_state.take_map(
b"KtonERC20",
b"Approves",
&mut approves,
get_hashed_key,
);
approves.iter().for_each(|(k, v)| {
kton_details.approvals += 1;
self.shell_state.insert_value(
b"Assets",
b"Approvals",
&k,
Approval { amount: v.as_u128(), deposit: 0 },
);
});
}

self.shell_state.insert_value(
b"System",
b"Account",
&blake2_128_concat_to_string(k),
a,
);
// TODO: migrate kton balances.
} else {
a.nonce = 0;

if v.kton != 0 || v.kton_reserved != 0 {
let aa = AssetAccount {
balance: v.kton,
is_frozen: false,
reason: ExistenceReason::Sufficient,
extra: (),
};

self.shell_state.insert_value(b"AccountMigration", b"KtonAccounts", &k, &aa);
}

self.shell_state.insert_value(b"AccountMigration", b"Accounts", &k, a);
}
});

log::info!("set `Assets::Asset`");
log::info!("kton_total_issuance({kton_total_issuance})");
log::info!("kton_total_issuance_storage({kton_total_issuance_storage})");
self.shell_state.insert_value(
b"Assets",
b"Asset",
&array_bytes::bytes2hex("", blake2_128_concat(&KTON_ID.encode())),
kton_details,
);

log::info!("set `Assets::Metadata`");
self.shell_state.insert_value(
b"Assets",
b"Metadata",
&array_bytes::bytes2hex("", blake2_128_concat(&KTON_ID.encode())),
AssetMetadata {
deposit: 0,
name: b"Darwinia Commitment Token".to_vec(),
symbol: b"KTON".to_vec(),
decimals: 18,
is_frozen: false,
},
);

self
}

Expand Down
Loading

0 comments on commit e775dcd

Please sign in to comment.