Skip to content

Commit

Permalink
Process proxy and support generic runtime (#172)
Browse files Browse the repository at this point in the history
* Format

* Process proxy and support generic runtime

* Format

* Format

* Fixes
  • Loading branch information
AurevoirXavier authored Jan 4, 2023
1 parent 114f3fe commit 431c1b1
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 70 deletions.
4 changes: 2 additions & 2 deletions pallet/account-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ version = "6.0.0"

[dependencies]
# crates.io
codec = { default-features = false, package = "parity-scale-codec", version = "3.2", features = ["derive"] }
scale-info = { default-features = false, version = "2.3", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.2", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"] }

# darwinia
darwinia-deposit = { default-features = false, path = "../deposit" }
Expand Down
6 changes: 2 additions & 4 deletions pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(missing_docs)]

// TODO: update weight

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -127,10 +125,10 @@ pub mod pallet {
/// [`pallet_asset::AssetAccount`] data.
///
/// https://github.dev/paritytech/substrate/blob/polkadot-v0.9.30/frame/assets/src/types.rs#L115
// The size of `pallet_asset::AssetAccount` is 64 bytes.
// The size of encoded `pallet_asset::AssetAccount` is 18 bytes.
#[pallet::storage]
#[pallet::getter(fn kton_account_of)]
pub type KtonAccounts<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, [u8; 64]>;
pub type KtonAccounts<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, [u8; 18]>;

/// [`pallet_vesting::Vesting`] data.
///
Expand Down
12 changes: 6 additions & 6 deletions pallet/message-transact/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ version = "6.0.0"

[dependencies]
# crates.io
codec = { default-features = false, package = "parity-scale-codec", version = "3.2.1", features = ["derive"] }
ethereum = { default-features = false, version = "0.12.0", features = ["with-codec"] }
scale-info = { default-features = false, version = "2.3.0", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.2.1", default-features = false, features = ["derive"] }
ethereum = { version = "0.12.0", default-features = false, features = ["with-codec"] }
scale-info = { version = "2.3.0", default-features = false, features = ["derive"] }

# frontier
fp-ethereum = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
Expand All @@ -31,22 +31,22 @@ sp-std = { default-features = false, git = "https://github.com/paritytech
array-bytes = { version = "4.1" }
libsecp256k1 = { version = "0.5", features = ["static-context", "hmac"] }
rlp = { version = "0.5" }
sha3 = { version = "0.9" }
sha3 = { version = "0.9" }

# darwinia
pallet-bridge-dispatch = { git = "https://github.com/darwinia-network/darwinia-messages-substrate", branch = "polkadot-v0.9.30" }
bp-message-dispatch = { git = "https://github.com/darwinia-network/darwinia-messages-substrate", branch = "polkadot-v0.9.30" }
bp-runtime = { git = "https://github.com/darwinia-network/darwinia-messages-substrate", branch = "polkadot-v0.9.30" }
pallet-bridge-dispatch = { git = "https://github.com/darwinia-network/darwinia-messages-substrate", branch = "polkadot-v0.9.30" }

# frontier
fp-self-contained = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
pallet-ethereum = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }

# moonbeam
precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = ["testing"] }

# substrate
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-ethereum = { git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

Expand Down
5 changes: 1 addition & 4 deletions runtime/crab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ use frame_support::{
};
use frame_system::EnsureRoot;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, H256, U256};
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
use sp_runtime::{
generic,
traits::Block as BlockT,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult,
ApplyExtrinsicResult, Perbill, Permill,
};
pub use sp_runtime::{MultiAddress, Perbill, Permill};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down
5 changes: 1 addition & 4 deletions runtime/darwinia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ use frame_support::{
};
use frame_system::EnsureRoot;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, H256, U256};
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
use sp_runtime::{
generic,
traits::Block as BlockT,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult,
ApplyExtrinsicResult, MultiAddress, Perbill, Permill,
};
pub use sp_runtime::{MultiAddress, Perbill, Permill};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down
5 changes: 1 addition & 4 deletions runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ use frame_support::{
};
use frame_system::EnsureRoot;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, H256, U256};
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
use sp_runtime::{
generic,
traits::Block as BlockT,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult,
ApplyExtrinsicResult, Perbill, Permill,
};
pub use sp_runtime::{MultiAddress, Perbill, Permill};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down
2 changes: 1 addition & 1 deletion tool/state-processor/src/balances/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::*;

type Locks = Vec<BalanceLock>;

impl Processor {
impl<S> Processor<S> {
pub fn process_balances(&mut self) -> (u128, u128) {
// Balances storage items.
// https://github.dev/darwinia-network/substrate/blob/darwinia-v0.12.5/frame/balances/src/lib.rs#L486
Expand Down
37 changes: 37 additions & 0 deletions tool/state-processor/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub const GWEI: u128 = 1_000_000_000;
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];

pub trait Configurable {
const NAME: &'static str;

const KTON_NAME: &'static [u8];
const KTON_SYMBOL: &'static [u8];
}
impl Configurable for () {
const KTON_NAME: &'static [u8] = b"";
const KTON_SYMBOL: &'static [u8] = b"";
const NAME: &'static str = "";
}

pub struct Darwinia;
impl Configurable for Darwinia {
const KTON_NAME: &'static [u8] = b"Darwinia Commitment Token";
const KTON_SYMBOL: &'static [u8] = b"KTON";
const NAME: &'static str = "darwinia";
}

pub struct Crab;
impl Configurable for Crab {
const KTON_NAME: &'static [u8] = b"Crab Commitment Token";
const KTON_SYMBOL: &'static [u8] = b"CKTON";
const NAME: &'static str = "crab";
}

pub struct Pangolin;
impl Configurable for Pangolin {
const KTON_NAME: &'static [u8] = b"Pangolin Commitment Token";
const KTON_SYMBOL: &'static [u8] = b"PKTON";
const NAME: &'static str = "pangolin";
}
2 changes: 1 addition & 1 deletion tool/state-processor/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::*;
// https://github.dev/paritytech/frontier/blob/polkadot-v0.9.30/primitives/storage/src/lib.rs#L23
const PALLET_ETHEREUM_SCHEMA: &str = "0x3a657468657265756d5f736368656d61";

impl Processor {
impl<S> Processor<S> {
fn process_ethereum_schema(&mut self) -> &mut Self {
log::info!("set `PALLET_ETHEREUM_SCHEMA`");
self.shell_state.insert_raw_key_raw_value(PALLET_ETHEREUM_SCHEMA.into(), "0x3".into());
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
@@ -1,7 +1,7 @@
// darwinia
use crate::*;

impl Processor {
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
Expand Down
76 changes: 49 additions & 27 deletions tool/state-processor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
mod balances;
mod evm;
mod indices;
mod proxy;
mod staking;
mod system;
mod vesting;

mod adjust;
use adjust::*;
mod configuration;
use configuration::*;

mod type_registry;
use type_registry::*;
Expand All @@ -19,6 +22,7 @@ use std::{
env,
fs::File,
io::{Read, Write},
marker::PhantomData,
mem,
sync::RwLock,
};
Expand All @@ -39,25 +43,32 @@ fn main() -> Result<()> {
env::set_var("RUST_LOG", "state_processor");
pretty_env_logger::init();

Processor::new()?.process()?;
<Processor<Darwinia>>::new()?.process()?;

Ok(())
}

struct Processor {
solo_state: State,
para_state: State,
shell_state: State,
struct Processor<S> {
solo_state: State<S>,
para_state: State<()>,
shell_state: State<()>,
shell_chain_spec: ChainSpec,
}
impl Processor {
impl<S> Processor<S>
where
S: Configurable,
{
fn new() -> Result<Self> {
let mut shell_chain_spec = from_file::<ChainSpec>("test-data/shell.json")?;
let mut shell_chain_spec =
from_file::<ChainSpec>(&format!("test-data/{}-shell.json", S::NAME))?;

Ok(Self {
solo_state: State::from_file("test-data/solo.json")?,
para_state: State::from_file("test-data/para.json")?,
shell_state: State(mem::take(&mut shell_chain_spec.genesis.raw.top)),
solo_state: State::from_file(&format!("test-data/{}-solo.json", S::NAME))?,
para_state: State::from_file(&format!("test-data/{}-para.json", S::NAME))?,
shell_state: State {
map: mem::take(&mut shell_chain_spec.genesis.raw.top),
_runtime: Default::default(),
},
shell_chain_spec,
})
}
Expand All @@ -69,17 +80,22 @@ impl Processor {

assert!(*_guard != 0);

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

self.save()
}

fn save(mut self) -> Result<()> {
log::info!("saving processed chain spec");

mem::swap(&mut self.shell_state.0, &mut self.shell_chain_spec.genesis.raw.top);
mem::swap(&mut self.shell_state.map, &mut self.shell_chain_spec.genesis.raw.top);

let mut f = File::create("test-data/processed.json")?;
let mut f = File::create(format!("test-data/{}-processed.json", S::NAME))?;
let v = serde_json::to_vec(&self.shell_chain_spec)?;

f.write_all(&v)?;
Expand All @@ -88,14 +104,20 @@ impl Processor {
}
}

pub struct State(Map<String>);
impl State {
pub struct State<R> {
map: Map<String>,
_runtime: PhantomData<R>,
}
impl<R> State<R> {
fn from_file(path: &str) -> Result<Self> {
Ok(Self(from_file::<ChainSpec>(path)?.genesis.raw.top))
Ok(Self {
map: from_file::<ChainSpec>(path)?.genesis.raw.top,
_runtime: <PhantomData<R>>::default(),
})
}

fn insert_raw_key_raw_value(&mut self, key: String, value: String) -> &mut Self {
self.0.insert(key, value);
self.map.insert(key, value);

self
}
Expand All @@ -104,7 +126,7 @@ impl State {
where
E: Encode,
{
self.0.insert(key, encode_value(value));
self.map.insert(key, encode_value(value));

self
}
Expand All @@ -118,7 +140,7 @@ impl State {
where
F: Fn(&str, &str) -> String,
{
self.0.retain(|k, v| {
self.map.retain(|k, v| {
if k.starts_with(prefix) {
buffer.insert(process_key(k, prefix), v.to_owned());

Expand All @@ -133,11 +155,11 @@ impl State {

fn insert_raw_key_map(&mut self, pairs: Map<String>) -> &mut Self {
pairs.into_iter().for_each(|(k, v)| {
if self.0.contains_key(&k) {
if self.map.contains_key(&k) {
log::error!("key({k}) has already existed, overriding");
}

self.0.insert(k, v);
self.map.insert(k, v);
});

self
Expand All @@ -149,7 +171,7 @@ impl State {
{
let key = full_key(pallet, item, hash);

if let Some(v) = self.0.get(&key) {
if let Some(v) = self.map.get(&key) {
match decode(v) {
Ok(v) => *value = v,
Err(e) => log::error!(
Expand All @@ -175,7 +197,7 @@ impl State {
{
let key = full_key(pallet, item, hash);

if let Some(v) = self.0.remove(&key) {
if let Some(v) = self.map.remove(&key) {
match decode(&v) {
Ok(v) => *value = v,
Err(e) => log::error!(
Expand All @@ -199,7 +221,7 @@ impl State {
where
E: Encode,
{
self.0.insert(full_key(pallet, item, hash), encode_value(value));
self.map.insert(full_key(pallet, item, hash), encode_value(value));

self
}
Expand Down Expand Up @@ -234,7 +256,7 @@ impl State {
let len = buffer.len();
let prefix = item_key(pallet, item);

self.0.retain(|full_key, v| {
self.map.retain(|full_key, v| {
if full_key.starts_with(&prefix) {
match decode(v) {
Ok(v) => {
Expand Down Expand Up @@ -266,14 +288,14 @@ impl State {
F: Fn(&str) -> String,
{
pairs.into_iter().for_each(|(k, v)| {
self.0.insert(process_key(&k), encode_value(v));
self.map.insert(process_key(&k), encode_value(v));
});

self
}

fn contains_key(&self, key: &str) -> bool {
self.0.contains_key(key)
self.map.contains_key(key)
}

// fn inc_consumers(&mut self, who: &str) {}
Expand Down
3 changes: 3 additions & 0 deletions tool/state-processor/src/proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Process steps
- take solo `Proxy::Proxies`
- adjust the reserved's decimals then free solo `Proxies` reservations
Loading

0 comments on commit 431c1b1

Please sign in to comment.