From dd1406bcaf7f5a1d465527623e0dc989fead3ebc Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 18 Nov 2021 13:43:51 +0000 Subject: [PATCH] refactor: extract EvmAddressMapping primitive --- --- a/pallets/common/Cargo.toml +++ b/pallets/common/Cargo.toml @@ -15,10 +15,10 @@ sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' } sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' } sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' } +up-evm-mapping = { default-features = false, path = '../../primitives/evm-mapping' } nft-data-structs = { default-features = false, path = '../../primitives/nft' } pallet-evm-coder-substrate = { default-features = false, path = '../../pallets/evm-coder-substrate' } evm-coder = { default-features = false, path = '../../crates/evm-coder' } - pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" } serde = { version = "1.0.130", default-features = false } scale-info = { version = "1.0.0", default-features = false, features = [ @@ -32,6 +32,7 @@ "frame-system/std", "sp-runtime/std", "sp-std/std", + "up-evm-mapping/std", "nft-data-structs/std", "pallet-evm/std", ] --- a/pallets/common/src/account.rs +++ b/pallets/common/src/account.rs @@ -2,12 +2,12 @@ use codec::{Encode, EncodeLike, Decode}; use sp_core::H160; use scale_info::{Type, TypeInfo}; -use sp_core::crypto::AccountId32; use core::cmp::Ordering; use serde::{Serialize, Deserialize}; use pallet_evm::AddressMapping; use sp_std::vec::Vec; use sp_std::clone::Clone; +use up_evm_mapping::EvmBackwardsAddressMapping; pub trait CrossAccountId: Encode + EncodeLike + Decode + TypeInfo + Clone + PartialEq + Ord + core::fmt::Debug + Default @@ -174,19 +174,5 @@ } else { BasicCrossAccountIdRepr::Substrate(v.as_sub().clone()) } - } -} - -pub trait EvmBackwardsAddressMapping { - fn from_account_id(account_id: AccountId) -> H160; -} - -/// Should have same mapping as EnsureAddressTruncated -pub struct MapBackwardsAddressTruncated; -impl EvmBackwardsAddressMapping for MapBackwardsAddressTruncated { - fn from_account_id(account_id: AccountId32) -> H160 { - let mut out = [0; 20]; - out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]); - H160(out) } } --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -141,7 +141,7 @@ pub mod pallet { use super::*; use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key}; - use account::{EvmBackwardsAddressMapping, CrossAccountId}; + use account::CrossAccountId; use frame_support::traits::Currency; use nft_data_structs::TokenId; use scale_info::TypeInfo; @@ -153,7 +153,7 @@ type CrossAccountId: CrossAccountId; type EvmAddressMapping: pallet_evm::AddressMapping; - type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping; + type EvmBackwardsAddressMapping: up_evm_mapping::EvmBackwardsAddressMapping; type Currency: Currency; type CollectionCreationPrice: Get< --- a/pallets/evm-transaction-payment/Cargo.toml +++ b/pallets/evm-transaction-payment/Cargo.toml @@ -15,6 +15,7 @@ fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" } pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" } up-sponsorship = { version = "0.1.0", default-features = false, git = "https://github.com/UniqueNetwork/pallet-sponsoring" } +up-evm-mapping = { default-features = false, path = "../../primitives/evm-mapping" } [dependencies.codec] default-features = false @@ -35,4 +36,5 @@ "pallet-ethereum/std", "fp-evm/std", "up-sponsorship/std", + "up-evm-mapping/std", ] --- a/pallets/evm-transaction-payment/src/lib.rs +++ b/pallets/evm-transaction-payment/src/lib.rs @@ -1,6 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] pub use pallet::*; +use up_evm_mapping::EvmBackwardsAddressMapping; #[frame_support::pallet] pub mod pallet { @@ -20,6 +21,7 @@ pub trait Config: frame_system::Config { type SponsorshipHandler: SponsorshipHandler)>; type Currency: Currency; + type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping; } #[pallet::pallet] --- a/pallets/nft/Cargo.toml +++ b/pallets/nft/Cargo.toml @@ -34,6 +34,7 @@ 'fp-evm/std', 'nft-data-structs/std', 'up-sponsorship/std', + 'up-evm-mapping/std', 'sp-std/std', 'sp-api/std', 'sp-runtime/std', @@ -135,7 +136,7 @@ sp-api = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.12" } up-sponsorship = { version = "0.1.0", default-features = false, git = "https://github.com/UniqueNetwork/pallet-sponsoring" } - +up-evm-mapping = { default-features = false, path = "../../primitives/evm-mapping" } evm-coder = { default-features = false, path = "../../crates/evm-coder" } pallet-evm-coder-substrate = { default-features = false, path = "../../pallets/evm-coder-substrate" } primitive-types = { version = "0.10.1", default-features = false, features = [ --- /dev/null +++ b/primitives/evm-mapping/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "up-evm-mapping" +version = "0.1.0" +edition = "2018" + +[dependencies] +sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' } +frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' } + +[features] +default = ["std"] +std = [ + "sp-core/std", + "frame-support/std", +] --- /dev/null +++ b/primitives/evm-mapping/src/lib.rs @@ -0,0 +1,23 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +use frame_support::sp_runtime::AccountId32; +use sp_core::H160; + +/// Transforms substrate addresses to ethereum (Reverse of `EvmAddressMapping`) +/// pallet_evm doesn't have this, as it only checks if eth address +/// is owned by substrate via `EnsureAddressOrigin` trait +/// +/// This trait implementations shouldn't conflict with used `EnsureAddressOrigin` +pub trait EvmBackwardsAddressMapping { + fn from_account_id(account_id: AccountId) -> H160; +} + +/// Should have same mapping as EnsureAddressTruncated +pub struct MapBackwardsAddressTruncated; +impl EvmBackwardsAddressMapping for MapBackwardsAddressTruncated { + fn from_account_id(account_id: AccountId32) -> H160 { + let mut out = [0; 20]; + out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]); + H160(out) + } +} --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -69,6 +69,7 @@ 'pallet-ethereum/std', 'fp-rpc/std', 'up-rpc/std', + 'up-evm-mapping/std', 'fp-self-contained/std', 'parachain-info/std', 'serde', @@ -362,7 +363,7 @@ [dependencies.orml-vesting] git = "https://github.com/open-web3-stack/open-runtime-module-library" -version = "0.4.1-dev" +version = "0.4.1-dev" default-features = false ################################################################################ @@ -375,6 +376,7 @@ derivative = "2.2.0" pallet-nft = { path = '../pallets/nft', default-features = false, version = '3.0.0' } up-rpc = { path = "../primitives/rpc", default-features = false } +up-evm-mapping = { path = "../primitives/evm-mapping", default-features = false } pallet-inflation = { path = '../pallets/inflation', default-features = false, version = '3.0.0' } nft-data-structs = { path = '../primitives/nft', default-features = false, version = '0.9.0' } pallet-common = { default-features = false, path = "../pallets/common" } --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -736,7 +736,7 @@ impl pallet_common::Config for Runtime { type Event = Event; - type EvmBackwardsAddressMapping = pallet_common::account::MapBackwardsAddressTruncated; + type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated; type EvmAddressMapping = HashedAddressMapping; type CrossAccountId = pallet_common::account::BasicCrossAccountId; @@ -811,6 +811,7 @@ pallet_evm_contract_helpers::HelpersContractSponsoring, ); type Currency = Balances; + type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated; } impl pallet_nft_charge_transaction::Config for Runtime { -- gitstuff