From c8416318763e98e684490c465a4384dc829a5848 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 22 Nov 2022 14:43:01 +0800 Subject: [PATCH] Use `H160` as `AccountId` (#55) * Configure `H160` for runtime * Configure `H160` genesis Signed-off-by: Xavier Lau * Docs * Improve code * Add missing features * Format and enable missing features for #50 * Format * Fix evm config * Revert the rename Signed-off-by: Xavier Lau Co-authored-by: bear --- Cargo.lock | 166 +++++++++++++++++-- Cargo.toml | 2 +- core/primitives/Cargo.toml | 9 +- core/primitives/src/lib.rs | 7 +- node/Cargo.toml | 13 +- node/src/chain_spec.rs | 159 +++++++++--------- precompiles/bls12-381/Cargo.toml | 22 ++- precompiles/state-storage/Cargo.toml | 19 ++- precompiles/state-storage/src/lib.rs | 17 +- precompiles/state-storage/src/mock.rs | 34 +--- runtime/darwinia/Cargo.toml | 9 + runtime/darwinia/src/lib.rs | 38 ++++- runtime/darwinia/src/pallets/evm.rs | 31 ++-- runtime/darwinia/src/pallets/polkadot_xcm.rs | 15 +- runtime/darwinia/src/pallets/session.rs | 6 + runtime/darwinia/src/pallets/system.rs | 2 +- 16 files changed, 352 insertions(+), 197 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 58f41e74b..e599396fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,6 +12,27 @@ dependencies = [ "regex", ] +[[package]] +name = "account" +version = "0.1.1" +source = "git+https://github.com/darwinia-network/moonbeam?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" +dependencies = [ + "blake2-rfc", + "hex", + "impl-serde", + "libsecp256k1 0.6.0", + "log", + "parity-scale-codec", + "scale-info", + "serde", + "sha3 0.9.1", + "sp-core", + "sp-io", + "sp-runtime", + "sp-runtime-interface", + "sp-std", +] + [[package]] name = "addr2line" version = "0.17.0" @@ -433,6 +454,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6107fe1be6682a68940da878d9e9f5e90ca5745b3dec9fd1bb393c8777d4f581" +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + [[package]] name = "base64" version = "0.13.1" @@ -2068,6 +2095,7 @@ dependencies = [ name = "darwinia" version = "6.0.0" dependencies = [ + "array-bytes", "clap", "cumulus-client-cli", "cumulus-client-collator", @@ -2265,6 +2293,7 @@ dependencies = [ "xcm", "xcm-builder", "xcm-executor", + "xcm-primitives", ] [[package]] @@ -2297,6 +2326,7 @@ dependencies = [ name = "dc-primitives" version = "6.0.0" dependencies = [ + "account", "sp-core", "sp-runtime", ] @@ -2899,7 +2929,7 @@ dependencies = [ "futures", "hex", "jsonrpsee", - "libsecp256k1", + "libsecp256k1 0.7.1", "log", "lru 0.8.1", "parity-scale-codec", @@ -4550,7 +4580,7 @@ dependencies = [ "futures-timer", "instant", "lazy_static", - "libsecp256k1", + "libsecp256k1 0.7.1", "log", "multiaddr", "multihash", @@ -4621,7 +4651,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74b4b888cfbeb1f5551acd3aa1366e01bf88ede26cc3c4645d0d2d004d5ca7b0" dependencies = [ "asynchronous-codec", - "base64", + "base64 0.13.1", "byteorder", "bytes", "fnv", @@ -5002,6 +5032,25 @@ dependencies = [ "tikv-jemalloc-sys", ] +[[package]] +name = "libsecp256k1" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9d220bc1feda2ac231cb78c3d26f27676b8cf82c96971f7aeef3d0cf2797c73" +dependencies = [ + "arrayref", + "base64 0.12.3", + "digest 0.9.0", + "hmac-drbg", + "libsecp256k1-core 0.2.2", + "libsecp256k1-gen-ecmult 0.2.1", + "libsecp256k1-gen-genmult 0.2.1", + "rand 0.7.3", + "serde", + "sha2 0.9.9", + "typenum", +] + [[package]] name = "libsecp256k1" version = "0.7.1" @@ -5009,18 +5058,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95b09eff1b35ed3b33b877ced3a691fc7a481919c7e29c53c906226fcf55e2a1" dependencies = [ "arrayref", - "base64", + "base64 0.13.1", "digest 0.9.0", "hmac-drbg", - "libsecp256k1-core", - "libsecp256k1-gen-ecmult", - "libsecp256k1-gen-genmult", + "libsecp256k1-core 0.3.0", + "libsecp256k1-gen-ecmult 0.3.0", + "libsecp256k1-gen-genmult 0.3.0", "rand 0.8.5", "serde", "sha2 0.9.9", "typenum", ] +[[package]] +name = "libsecp256k1-core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0f6ab710cec28cef759c5f18671a27dae2a5f952cdaaee1d8e2908cb2478a80" +dependencies = [ + "crunchy", + "digest 0.9.0", + "subtle", +] + [[package]] name = "libsecp256k1-core" version = "0.3.0" @@ -5032,13 +5092,31 @@ dependencies = [ "subtle", ] +[[package]] +name = "libsecp256k1-gen-ecmult" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccab96b584d38fac86a83f07e659f0deafd0253dc096dab5a36d53efe653c5c3" +dependencies = [ + "libsecp256k1-core 0.2.2", +] + [[package]] name = "libsecp256k1-gen-ecmult" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3038c808c55c87e8a172643a7d87187fc6c4174468159cb3090659d55bcb4809" dependencies = [ - "libsecp256k1-core", + "libsecp256k1-core 0.3.0", +] + +[[package]] +name = "libsecp256k1-gen-genmult" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67abfe149395e3aa1c48a2beb32b068e2334402df8181f818d3aee2b304c4f5d" +dependencies = [ + "libsecp256k1-core 0.2.2", ] [[package]] @@ -5047,7 +5125,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3db8d6ba2cec9eacc40e6e8ccc98931840301f1006e95647ceb2dd5c3aa06f7c" dependencies = [ - "libsecp256k1-core", + "libsecp256k1-core 0.3.0", ] [[package]] @@ -5771,6 +5849,36 @@ dependencies = [ "num-traits", ] +[[package]] +name = "orml-traits" +version = "0.4.1-dev" +source = "git+https://github.com/open-web3-stack/open-runtime-module-library?branch=polkadot-v0.9.30#ae13a54dbc1a654df0e2d5e9dc18582b62716365" +dependencies = [ + "frame-support", + "impl-trait-for-tuples", + "num-traits", + "orml-utilities", + "parity-scale-codec", + "scale-info", + "sp-io", + "sp-runtime", + "sp-std", + "xcm", +] + +[[package]] +name = "orml-utilities" +version = "0.4.1-dev" +source = "git+https://github.com/open-web3-stack/open-runtime-module-library?branch=polkadot-v0.9.30#ae13a54dbc1a654df0e2d5e9dc18582b62716365" +dependencies = [ + "frame-support", + "parity-scale-codec", + "scale-info", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "os_str_bytes" version = "6.4.0" @@ -8089,7 +8197,7 @@ dependencies = [ "frame-support", "frame-system", "impl-trait-for-tuples", - "libsecp256k1", + "libsecp256k1 0.7.1", "log", "pallet-authorship", "pallet-babe", @@ -8374,7 +8482,7 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "precompile-utils" version = "0.1.0" -source = "git+https://github.com/darwinia-network/moonbeam.git?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" +source = "git+https://github.com/darwinia-network/moonbeam?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" dependencies = [ "affix", "evm", @@ -8400,7 +8508,7 @@ dependencies = [ [[package]] name = "precompile-utils-macro" version = "0.1.0" -source = "git+https://github.com/darwinia-network/moonbeam.git?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" +source = "git+https://github.com/darwinia-network/moonbeam?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" dependencies = [ "case", "num_enum", @@ -9224,7 +9332,7 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55" dependencies = [ - "base64", + "base64 0.13.1", ] [[package]] @@ -10778,7 +10886,7 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d1c5305e39e09653383c2c7244f2f78b3bcae37cf50c64cb4789c9f5096ec2" dependencies = [ - "base64", + "base64 0.13.1", "bytes", "flate2", "futures", @@ -11005,7 +11113,7 @@ dependencies = [ "hash256-std-hasher", "impl-serde", "lazy_static", - "libsecp256k1", + "libsecp256k1 0.7.1", "log", "merlin", "num-traits", @@ -11129,7 +11237,7 @@ dependencies = [ "bytes", "futures", "hash-db", - "libsecp256k1", + "libsecp256k1 0.7.1", "log", "parity-scale-codec", "parking_lot 0.12.1", @@ -12682,7 +12790,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcd849399d17d2270141cfe47fa0d91ee52d5f8ea9b98cf7ddde0d53e5f79882" dependencies = [ "anyhow", - "base64", + "base64 0.13.1", "bincode", "directories-next", "file-per-thread-logger", @@ -13220,6 +13328,30 @@ dependencies = [ "xcm", ] +[[package]] +name = "xcm-primitives" +version = "0.1.0" +source = "git+https://github.com/darwinia-network/moonbeam?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" +dependencies = [ + "ethereum", + "ethereum-types", + "frame-support", + "frame-system", + "hex", + "log", + "orml-traits", + "parity-scale-codec", + "scale-info", + "serde", + "sha3 0.8.2", + "sp-io", + "sp-runtime", + "sp-std", + "xcm", + "xcm-builder", + "xcm-executor", +] + [[package]] name = "xcm-procedural" version = "0.9.30" diff --git a/Cargo.toml b/Cargo.toml index 52ec33ac4..17c5b7891 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,6 @@ panic = "unwind" members = [ "core/*", "node", + "precompiles/*", "runtime/*", - "precompiles/*" ] diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index e080680fd..542d0b0d9 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -10,12 +10,19 @@ repository = "https://github.com/darwinia-network/darwinia" version = "6.0.0" [dependencies] -sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } +# moonbeam +account = { default-features = false, git = "https://github.com/darwinia-network/moonbeam", branch = "polkadot-v0.9.30" } +# substrate +sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } + [features] default = ["std"] std = [ + # moonbeam + "account/std", + # substrate "sp-core/std", "sp-runtime/std", ] diff --git a/core/primitives/src/lib.rs b/core/primitives/src/lib.rs index 8670ad098..411a8f719 100644 --- a/core/primitives/src/lib.rs +++ b/core/primitives/src/lib.rs @@ -28,7 +28,7 @@ #![deny(missing_docs)] /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. -pub type Signature = sp_runtime::MultiSignature; +pub type Signature = account::EthereumSignature; /// Some way of identifying an account on the chain. /// We intentionally make it equivalent to the public key of our transaction signing scheme. @@ -50,7 +50,7 @@ pub type Hash = sp_core::H256; pub type BlockNumber = u32; /// The address format for describing accounts. -pub type Address = sp_runtime::MultiAddress; +pub type Address = AccountId; /// Hashing type pub type Hashing = sp_runtime::traits::BlakeTwo256; @@ -97,6 +97,3 @@ pub const MWEI: Balance = 1_000 * KWEI; pub const KWEI: Balance = 1_000 * WEI; /// 1 wei — 1 pub const WEI: Balance = 1; - -/// Darwinia EVM addresses specific prefix. -pub const EVM_ADDR_PREFIX: &[u8] = b"dvm:"; diff --git a/node/Cargo.toml b/node/Cargo.toml index b30e2a29c..97b1a66bf 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -14,12 +14,13 @@ substrate-build-script-utils = { git = "https://github.com/paritytech/substrate" [dependencies] # crates.io -clap = { version = "3.2", features = ["derive"] } -codec = { package = "parity-scale-codec", version = "3.2" } -futures = { version = "0.3" } -jsonrpsee = { version = "0.15", features = ["server"] } -log = { version = "0.4" } -serde = { version = "1.0", features = ["derive"] } +array-bytes = { version = "4.2" } +clap = { version = "3.2", features = ["derive"] } +codec = { package = "parity-scale-codec", version = "3.2" } +futures = { version = "0.3" } +jsonrpsee = { version = "0.15", features = ["server"] } +log = { version = "0.4" } +serde = { version = "1.0", features = ["derive"] } # cumulus cumulus-client-cli = { git = "https://github.com/paritytech/cumulus.git", branch = "polkadot-v0.9.30" } diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index c4195e3ad..3c4384aed 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -32,12 +32,28 @@ use fp_evm::GenesisAccount; // substrate use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup}; use sc_service::ChainType; -use sp_core::{sr25519, Pair, Public, H160, U256}; -use sp_runtime::traits::{IdentifyAccount, Verify}; +use sp_core::{Pair, Public, H160}; /// Specialized `ChainSpec` for the normal parachain runtime. pub type ChainSpec = sc_service::GenericChainSpec; +// These are are testnet-only keys. +// address = "0x75a1807b6aff253070b96ed9e43c0c5c17c7e1d4" +// public-key = "0x036ae37e38766cd9be397dfd42486d8aeb46c30d4b0526d12dc9f5eb6a8e4c09f5" +// secret-seed = "0x63c24046f3b744c8cd8f74e91d9e3603577035f3119ac1389db0f461e591375d" +#[allow(unused)] +const COLLATOR_A: &str = "0x75a1807b6aff253070b96ed9e43c0c5c17c7e1d4"; +// address = "0x5f69def84585715b92d397b1e92d4bf26d48d6b7" +// public-key = "0x03f6230f7fd8bd24a3814753c5bdd0417d5e00149e15b4bac130887e925c6a53a0" +// secret-seed = "0xee92a5c93339cb59bdad8c088c1b3adbae7ec94110681d871ab3beb8ac6530b2" +#[allow(unused)] +const COLLATOR_B: &str = "0x5f69def84585715b92d397b1e92d4bf26d48d6b7"; +// address = "0xa4e3cf11462254ed4b7ce00079eb11ca2a8b5393" +// public-key = "0x0218893313cc713836d57c60daedd28ee0b0823a167469af37e16f970fdb5305ef" +// secret-seed = "0x68ade89c684eb715ad047d9a54f8a07457840194091622736d742503d148966a" +#[allow(unused)] +const COLLATOR_C: &str = "0xa4e3cf11462254ed4b7ce00079eb11ca2a8b5393"; + /// The default XCM version to set in genesis config. const SAFE_XCM_VERSION: u32 = xcm::prelude::XCM_VERSION; @@ -69,8 +85,6 @@ impl Extensions { } } -type AccountPublic = ::Signer; - /// Generate collator keys from seed. /// /// This function's return type must always match the session keys of the chain in tuple format. @@ -78,14 +92,6 @@ pub fn get_collator_keys_from_seed(seed: &str) -> AuraId { get_from_seed::(seed) } -/// Helper function to generate an account ID from seed -pub fn get_account_id_from_seed(seed: &str) -> AccountId -where - AccountPublic: From<::Public>, -{ - AccountPublic::from(get_from_seed::(seed)).into_account() -} - /// Generate the session keys from individual elements. /// /// The input must be a tuple of individual keys (a single arg for now since we have just one key). @@ -111,27 +117,19 @@ pub fn development_config() -> ChainSpec { // initial collators. vec![ ( - get_account_id_from_seed::("Alice"), + array_bytes::hex_n_into_unchecked(COLLATOR_A), + // Make `--alice` available for testnet. get_collator_keys_from_seed("Alice"), ), ( - get_account_id_from_seed::("Bob"), + array_bytes::hex_n_into_unchecked(COLLATOR_B), + // Make `--bob` available for testnet. get_collator_keys_from_seed("Bob"), ), ], vec![ - get_account_id_from_seed::("Alice"), - get_account_id_from_seed::("Bob"), - get_account_id_from_seed::("Charlie"), - get_account_id_from_seed::("Dave"), - get_account_id_from_seed::("Eve"), - get_account_id_from_seed::("Ferdie"), - get_account_id_from_seed::("Alice//stash"), - get_account_id_from_seed::("Bob//stash"), - get_account_id_from_seed::("Charlie//stash"), - get_account_id_from_seed::("Dave//stash"), - get_account_id_from_seed::("Eve//stash"), - get_account_id_from_seed::("Ferdie//stash"), + array_bytes::hex_n_into_unchecked(COLLATOR_A), + array_bytes::hex_n_into_unchecked(COLLATOR_B), ], 1000.into(), ) @@ -166,27 +164,19 @@ pub fn local_testnet_config() -> ChainSpec { // initial collators. vec![ ( - get_account_id_from_seed::("Alice"), + array_bytes::hex_n_into_unchecked(COLLATOR_A), + // Make `--alice` available for testnet. get_collator_keys_from_seed("Alice"), ), ( - get_account_id_from_seed::("Bob"), + array_bytes::hex_n_into_unchecked(COLLATOR_B), + // Make `--bob` available for testnet. get_collator_keys_from_seed("Bob"), ), ], vec![ - get_account_id_from_seed::("Alice"), - get_account_id_from_seed::("Bob"), - get_account_id_from_seed::("Charlie"), - get_account_id_from_seed::("Dave"), - get_account_id_from_seed::("Eve"), - get_account_id_from_seed::("Ferdie"), - get_account_id_from_seed::("Alice//stash"), - get_account_id_from_seed::("Bob//stash"), - get_account_id_from_seed::("Charlie//stash"), - get_account_id_from_seed::("Dave//stash"), - get_account_id_from_seed::("Eve//stash"), - get_account_id_from_seed::("Ferdie//stash"), + array_bytes::hex_n_into_unchecked(COLLATOR_A), + array_bytes::hex_n_into_unchecked(COLLATOR_B), ], 1000.into(), ) @@ -231,14 +221,16 @@ pub fn shell_config() -> ChainSpec { }, balances: Default::default(), parachain_info: darwinia_runtime::ParachainInfoConfig { parachain_id: 2046.into() }, + // TODO: update this before final release collator_selection: darwinia_runtime::CollatorSelectionConfig { - invulnerables: vec![get_account_id_from_seed::("Alice")], + invulnerables: vec![array_bytes::hex_n_into_unchecked(COLLATOR_A)], ..Default::default() }, + // TODO: update this before final release session: darwinia_runtime::SessionConfig { keys: vec![( - get_account_id_from_seed::("Alice"), - get_account_id_from_seed::("Alice"), + array_bytes::hex_n_into_unchecked(COLLATOR_A), + array_bytes::hex_n_into_unchecked(COLLATOR_A), session_keys(get_collator_keys_from_seed("Alice")), )], }, @@ -280,9 +272,7 @@ fn testnet_genesis( ) -> darwinia_runtime::GenesisConfig { darwinia_runtime::GenesisConfig { system: darwinia_runtime::SystemConfig { - code: darwinia_runtime::WASM_BINARY - .expect("WASM binary was not build, please build it!") - .to_vec(), + code: darwinia_runtime::WASM_BINARY.unwrap().to_vec(), }, balances: darwinia_runtime::BalancesConfig { balances: endowed_accounts.iter().cloned().map(|k| (k, 1 << 60)).collect(), @@ -298,7 +288,7 @@ fn testnet_genesis( .into_iter() .map(|(acc, aura)| { ( - acc.clone(), // account id + acc, // account id acc, // validator id session_keys(aura), // session keys ) @@ -316,43 +306,44 @@ fn testnet_genesis( ethereum: Default::default(), evm: EvmConfig { accounts: { - let mut map = BTreeMap::new(); - map.insert( - // Testing account. - H160::from_str("0x6be02d1d3665660d22ff9624b7be0551ee1ac91b") - .expect("internal `H160` is valid; qed"), - GenesisAccount { - balance: U256::from_str("0xffffffffffffffffffffffffffffffff") - .expect("internal `U256` is valid; qed"), - code: Default::default(), - nonce: Default::default(), - storage: Default::default(), - }, - ); - map.insert( - // Benchmarking account. - H160::from_str("1000000000000000000000000000000000000001") - .expect("internal `H160` is valid; qed"), - GenesisAccount { - nonce: U256::from(1), - balance: U256::from(1_000_000_000_000_000_000_000_000_u128), - storage: Default::default(), - code: vec![0x00], - }, - ); - - for precompile in DarwiniaPrecompiles::::used_addresses() { - map.insert( - precompile, - GenesisAccount { - nonce: Default::default(), - balance: Default::default(), - storage: Default::default(), - code: REVERT_BYTECODE.to_vec(), - }, - ); - } - map + BTreeMap::from_iter( + DarwiniaPrecompiles::::used_addresses() + .iter() + .map(|p| { + ( + p.to_owned(), + GenesisAccount { + nonce: Default::default(), + balance: Default::default(), + storage: Default::default(), + code: REVERT_BYTECODE.to_vec(), + }, + ) + }) + .chain([ + // Testing account. + ( + H160::from_str("0x6be02d1d3665660d22ff9624b7be0551ee1ac91b") + .unwrap(), + GenesisAccount { + balance: (10_000_000 * UNIT).into(), + code: Default::default(), + nonce: Default::default(), + storage: Default::default(), + }, + ), + // Benchmarking account. + ( + H160::from_str("1000000000000000000000000000000000000001").unwrap(), + GenesisAccount { + nonce: 1.into(), + balance: (10_000_000 * UNIT).into(), + storage: Default::default(), + code: vec![0x00], + }, + ), + ]), + ) }, }, base_fee: Default::default(), diff --git a/precompiles/bls12-381/Cargo.toml b/precompiles/bls12-381/Cargo.toml index 9c0ad018d..396855bfb 100644 --- a/precompiles/bls12-381/Cargo.toml +++ b/precompiles/bls12-381/Cargo.toml @@ -12,21 +12,26 @@ version = "6.0.0" [dependencies] # crates.io milagro_bls = { default-features = false, git = "https://github.com/darwinia-network/milagro_bls" } + # frontier fp-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" } pallet-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" } + # moonbeam -precompile-utils = {default-features = false, git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" } +precompile-utils = { default-features = false, git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" } + # paritytech frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } [dev-dependencies] # crates.io -codec = { package = "parity-scale-codec", version = "3.2" } -scale-info = { version = "2.3", features = ["derive"] } +codec = { package = "parity-scale-codec", version = "3.2" } +scale-info = { version = "2.3", features = ["derive"] } + # moonbeam -precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = [ "testing" ] } +precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = ["testing"] } + # paritytech frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } @@ -37,14 +42,15 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = " [features] default = ["std"] - std = [ # frontier "fp-evm/std", "pallet-evm/std", + # moonbeam "precompile-utils/std", + # paritytech - # "frame-support/std", - "sp-std/std" -] \ No newline at end of file + "frame-support/std", + "sp-std/std", +] diff --git a/precompiles/state-storage/Cargo.toml b/precompiles/state-storage/Cargo.toml index 9552d8018..edd52a2b8 100644 --- a/precompiles/state-storage/Cargo.toml +++ b/precompiles/state-storage/Cargo.toml @@ -13,18 +13,22 @@ version = "6.0.0" # frontier fp-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" } pallet-evm = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" } + # moonbeam -precompile-utils = {default-features = false, git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" } +precompile-utils = { default-features = false, git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" } + # paritytech frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } [dev-dependencies] # crates.io -codec = { package = "parity-scale-codec", version = "3.2" } -scale-info = { version = "2.3", features = ["derive"] } +codec = { package = "parity-scale-codec", version = "3.2" } +scale-info = { version = "2.3", features = ["derive"] } + # moonbeam -precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = [ "testing" ] } +precompile-utils = { git = "https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = ["testing"] } + # paritytech frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" } @@ -35,14 +39,15 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = " [features] default = ["std"] - std = [ # frontier "fp-evm/std", "pallet-evm/std", + # moonbeam "precompile-utils/std", + # paritytech - # "frame-support/std", - "sp-std/std" + "frame-support/std", + "sp-std/std", ] diff --git a/precompiles/state-storage/src/lib.rs b/precompiles/state-storage/src/lib.rs index 1e0ccf7e1..22c564b5e 100644 --- a/precompiles/state-storage/src/lib.rs +++ b/precompiles/state-storage/src/lib.rs @@ -39,14 +39,6 @@ pub trait StorageFilterT { pub struct StateStorage { _marker: PhantomData<(Runtime, Filter)>, } - -pub struct EthereumStorageFilter; -impl StorageFilterT for EthereumStorageFilter { - fn allow(prefix: &[u8]) -> bool { - prefix != Twox128::hash(b"EVM") && prefix != Twox128::hash(b"Ethereum") - } -} - #[precompile_utils::precompile] impl StateStorage where @@ -66,7 +58,14 @@ where return Err(revert("Read restriction")); } - let output = frame_support::storage::unhashed::get_raw(&bytes); + let output = frame_support::storage::unhashed::get_raw(bytes); Ok(output.unwrap_or_default().as_slice().into()) } } + +pub struct EthereumStorageFilter; +impl StorageFilterT for EthereumStorageFilter { + fn allow(prefix: &[u8]) -> bool { + prefix != Twox128::hash(b"EVM") && prefix != Twox128::hash(b"Ethereum") + } +} diff --git a/precompiles/state-storage/src/mock.rs b/precompiles/state-storage/src/mock.rs index df8fd8995..cc8f2ad0f 100644 --- a/precompiles/state-storage/src/mock.rs +++ b/precompiles/state-storage/src/mock.rs @@ -22,7 +22,7 @@ use codec::{Decode, Encode, MaxEncodedLen}; // frontier use fp_evm::{Precompile, PrecompileSet}; -use pallet_evm::AddressMapping; +use pallet_evm::IdentityAddressMapping; // parity use frame_support::{ pallet_prelude::Weight, @@ -33,7 +33,6 @@ use sp_core::{H160, H256, U256}; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, - AccountId32, }; use sp_std::{marker::PhantomData, prelude::*}; // darwinia @@ -41,6 +40,7 @@ use crate::*; pub type Block = frame_system::mocking::MockBlock; pub type Balance = u64; +pub type AccountId = H160; pub type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; #[derive( @@ -86,7 +86,7 @@ frame_support::parameter_types! { } impl frame_system::Config for TestRuntime { type AccountData = pallet_balances::AccountData; - type AccountId = AccountId32; + type AccountId = AccountId; type BaseCallFilter = Everything; type BlockHashCount = (); type BlockLength = (); @@ -177,15 +177,6 @@ fn addr(a: u64) -> H160 { H160::from_low_u64_be(a) } -pub struct HashedAddressMapping; -impl AddressMapping for HashedAddressMapping { - fn into_account_id(address: H160) -> AccountId32 { - let mut data = [0u8; 32]; - data[0..20].copy_from_slice(&address[..]); - AccountId32::from(Into::<[u8; 32]>::into(data)) - } -} - frame_support::parameter_types! { pub const TransactionByteFee: u64 = 1; pub const ChainId: u64 = 42; @@ -197,10 +188,10 @@ frame_support::parameter_types! { pub type PCall = StateStorageCall; impl pallet_evm::Config for TestRuntime { - type AddressMapping = HashedAddressMapping; + type AddressMapping = IdentityAddressMapping; type BlockGasLimit = BlockGasLimit; type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping; - type CallOrigin = pallet_evm::EnsureAddressRoot; + type CallOrigin = pallet_evm::EnsureAddressRoot; type ChainId = ChainId; type Currency = Balances; type FeeCalculator = (); @@ -212,7 +203,7 @@ impl pallet_evm::Config for TestRuntime { type Runner = pallet_evm::runner::stack::Runner; type RuntimeEvent = RuntimeEvent; type WeightPerGas = WeightPerGas; - type WithdrawOrigin = pallet_evm::EnsureAddressNever; + type WithdrawOrigin = pallet_evm::EnsureAddressNever; } frame_support::construct_runtime! { @@ -231,20 +222,11 @@ frame_support::construct_runtime! { #[derive(Default)] pub(crate) struct ExtBuilder { // endowed accounts with balances - balances: Vec<(AccountId32, Balance)>, + balances: Vec<(AccountId, Balance)>, } impl ExtBuilder { - pub(crate) fn with_balances(mut self, balances: Vec<(H160, Balance)>) -> Self { - let balances = balances - .iter() - .map(|(account, amount)| { - ( - ::AddressMapping::into_account_id(*account), - *amount, - ) - }) - .collect(); + pub(crate) fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self { self.balances = balances; self } diff --git a/runtime/darwinia/Cargo.toml b/runtime/darwinia/Cargo.toml index da5cf00d8..f5bd2feac 100644 --- a/runtime/darwinia/Cargo.toml +++ b/runtime/darwinia/Cargo.toml @@ -65,6 +65,9 @@ pallet-evm-precompile-modexp = { default-features = false, git = "https://gith pallet-evm-precompile-simple = { default-features = false, git = "https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" } +# moonbeam +xcm-primitives = { default-features = false, git = "https://github.com/darwinia-network/moonbeam", branch = "polkadot-v0.9.30" } + # polkadot pallet-xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.30" } polkadot-parachain = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.30" } @@ -154,6 +157,9 @@ std = [ "pallet-evm-precompile-modexp/std", "pallet-evm-precompile-simple/std", + # moonbeam + "xcm-primitives/std", + # polkadot "pallet-xcm/std", "polkadot-parachain/std", @@ -215,6 +221,9 @@ runtime-benchmarks = [ "pallet-ethereum/runtime-benchmarks", "pallet-evm/runtime-benchmarks", + # moonbeam + "xcm-primitives/runtime-benchmarks", + # polkadot "pallet-xcm/runtime-benchmarks", "polkadot-parachain/runtime-benchmarks", diff --git a/runtime/darwinia/src/lib.rs b/runtime/darwinia/src/lib.rs index 5ae9ae430..3ae10d639 100644 --- a/runtime/darwinia/src/lib.rs +++ b/runtime/darwinia/src/lib.rs @@ -100,6 +100,38 @@ pub type Executive = frame_executive::Executive< AllPalletsWithSystem, >; +// TODO: move to impl.rs +pub struct DealWithFees(sp_std::marker::PhantomData); +impl frame_support::traits::OnUnbalanced> + for DealWithFees +where + R: pallet_balances::Config, /* R: pallet_balances::Config + pallet_treasury::Config, + * pallet_treasury::Pallet: + * OnUnbalanced>, */ +{ + // this seems to be called for substrate-based transactions + fn on_unbalanceds( + mut fees_then_tips: impl Iterator>, + ) { + if let Some(fees) = fees_then_tips.next() { + // for fees, 80% are burned, 20% to the treasury + // let (_, to_treasury) = fees.ration(80, 20); + // Balances pallet automatically burns dropped Negative Imbalances by decreasing + // total_supply accordingly + // as OnUnbalanced<_>>::on_unbalanced(to_treasury); + } + } + + // this is called from pallet_evm for Ethereum-based transactions + // (technically, it calls on_unbalanced, which calls this when non-zero) + fn on_nonzero_unbalanced(amount: pallet_balances::NegativeImbalance) { + // Balances pallet automatically burns dropped Negative Imbalances by decreasing + // total_supply accordingly + // let (_, to_treasury) = amount.ration(80, 20); + // as OnUnbalanced<_>>::on_unbalanced(to_treasury); + } +} + impl fp_self_contained::SelfContainedCall for RuntimeCall { type SignedInfo = H160; @@ -190,12 +222,6 @@ impl WeightToFeePolynomial for WeightToFee { } } -sp_runtime::impl_opaque_keys! { - pub struct SessionKeys { - pub aura: Aura, - } -} - /// Runtime version. #[sp_version::runtime_version] pub const VERSION: RuntimeVersion = RuntimeVersion { diff --git a/runtime/darwinia/src/pallets/evm.rs b/runtime/darwinia/src/pallets/evm.rs index b1d73060b..f90c3b082 100644 --- a/runtime/darwinia/src/pallets/evm.rs +++ b/runtime/darwinia/src/pallets/evm.rs @@ -20,12 +20,11 @@ use crate::*; use darwinia_precompile_bls12_381::BLS12381; use darwinia_precompile_state_storage::{EthereumStorageFilter, StateStorage}; -use dc_primitives::EVM_ADDR_PREFIX; // frontier use pallet_ethereum::EthereumBlockHashMapping; use pallet_evm::{ - AddressMapping, EnsureAddressTruncated, FeeCalculator, FixedGasWeightMapping, Precompile, - PrecompileHandle, PrecompileResult, PrecompileSet, + AddressMapping, EnsureAddressNever, EnsureAddressRoot, FeeCalculator, FixedGasWeightMapping, + Precompile, PrecompileHandle, PrecompileResult, PrecompileSet, }; use pallet_evm_precompile_blake2::Blake2F; use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing}; @@ -72,21 +71,15 @@ impl FeeCalculator for FixedGasPrice { (U256::from(GWEI), Weight::zero()) } } -pub struct ConcatAddressMapping; -impl AddressMapping for ConcatAddressMapping + +// TODO: Integrate to the upstream repo +pub struct FromH160; +impl AddressMapping for FromH160 where - AccountId: From<[u8; 32]>, + T: From, { - fn into_account_id(address: H160) -> AccountId { - let check_sum = |account_id: &[u8; 32]| -> u8 { - account_id[1..31].iter().fold(account_id[0], |sum, &byte| sum ^ byte) - }; - - let mut raw_account = [0u8; 32]; - raw_account[0..4].copy_from_slice(EVM_ADDR_PREFIX); - raw_account[11..31].copy_from_slice(&address[..]); - raw_account[31] = check_sum(&raw_account); - raw_account.into() + fn into_account_id(address: H160) -> T { + address.into() } } @@ -149,10 +142,10 @@ where } impl pallet_evm::Config for Runtime { - type AddressMapping = ConcatAddressMapping; + type AddressMapping = FromH160; type BlockGasLimit = BlockGasLimit; type BlockHashMapping = EthereumBlockHashMapping; - type CallOrigin = EnsureAddressTruncated; + type CallOrigin = EnsureAddressRoot; type ChainId = ChainId; type Currency = Balances; type FeeCalculator = FixedGasPrice; @@ -164,7 +157,7 @@ impl pallet_evm::Config for Runtime { type Runner = pallet_evm::runner::stack::Runner; type RuntimeEvent = RuntimeEvent; type WeightPerGas = WeightPerGas; - type WithdrawOrigin = EnsureAddressTruncated; + type WithdrawOrigin = EnsureAddressNever; } fn addr(a: u64) -> H160 { diff --git a/runtime/darwinia/src/pallets/polkadot_xcm.rs b/runtime/darwinia/src/pallets/polkadot_xcm.rs index 93cccefb5..8fdb7b611 100644 --- a/runtime/darwinia/src/pallets/polkadot_xcm.rs +++ b/runtime/darwinia/src/pallets/polkadot_xcm.rs @@ -19,10 +19,11 @@ // darwinia use crate::*; use darwinia_common_runtime::xcm_barrier::*; +// moonbeam +use xcm_primitives::*; // polkadot use pallet_xcm::XcmPassthrough; use polkadot_parachain::primitives::Sibling; -use polkadot_runtime_common::impls::ToAuthor; use xcm::latest::prelude::*; use xcm_builder::*; use xcm_executor::XcmExecutor; @@ -55,8 +56,8 @@ pub type LocationToAccountId = ( ParentIsPreset, // Sibling parachain origins convert to AccountId via the `ParaId::into`. SiblingParachainConvertsVia, - // Straight up local `AccountId32` origins just alias directly to `AccountId`. - AccountId32Aliases, + // Straight up local `AccountId20` origins just alias directly to `AccountId`. + AccountKey20Aliases, ); /// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance, /// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can @@ -74,7 +75,7 @@ pub type XcmOriginToTransactDispatchOrigin = ( SiblingParachainAsNative, // Native signed account converter; this just converts an `AccountId32` origin into a normal // `RuntimeOrigin::Signed` origin of the same 32-byte value. - SignedAccountId32AsNative, + SignedAccountKey20AsNative, // Xcm origins can be represented natively under the Xcm pallet's Xcm origin. XcmPassthrough, ); @@ -117,18 +118,18 @@ impl xcm_executor::Config for XcmConfig { type RuntimeCall = RuntimeCall; type SubscriptionService = PolkadotXcm; type Trader = - UsingComponents>; + UsingComponents>; type Weigher = FixedWeightBounds; type XcmSender = XcmRouter; } /// No local origins on this chain are allowed to dispatch XCM sends/executions. -pub type LocalOriginToLocation = SignedToAccountId32; +pub type LocalOriginToLocation = SignedToAccountId20; /// The means for routing XCM messages which are not for local execution into the right message /// queues. pub type XcmRouter = ( // Two routers - use UMP to communicate with the relay chain: - cumulus_primitives_utility::ParentAsUmp, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, ); diff --git a/runtime/darwinia/src/pallets/session.rs b/runtime/darwinia/src/pallets/session.rs index 2ec09faa7..81f96d7a8 100644 --- a/runtime/darwinia/src/pallets/session.rs +++ b/runtime/darwinia/src/pallets/session.rs @@ -19,6 +19,12 @@ // darwinia use crate::*; +sp_runtime::impl_opaque_keys! { + pub struct SessionKeys { + pub aura: Aura, + } +} + frame_support::parameter_types! { pub const Period: u32 = 6 * HOURS; pub const Offset: u32 = 0; diff --git a/runtime/darwinia/src/pallets/system.rs b/runtime/darwinia/src/pallets/system.rs index 66958fff3..ccdfd2c16 100644 --- a/runtime/darwinia/src/pallets/system.rs +++ b/runtime/darwinia/src/pallets/system.rs @@ -83,7 +83,7 @@ impl frame_system::Config for Runtime { /// The index type for storing how many extrinsics an account has signed. type Index = Index; /// The lookup mechanism to get account ID from whatever is passed in dispatchers. - type Lookup = sp_runtime::traits::AccountIdLookup; + type Lookup = sp_runtime::traits::IdentityLookup; type MaxConsumers = frame_support::traits::ConstU32<16>; /// What to do if an account is fully reaped from the system. type OnKilledAccount = ();