git.delta.rocks / unique-network / refs/commits / dd1406bcaf7f

difftreelog

refactor extract EvmAddressMapping primitive

Yaroslav Bolyukin2021-11-18parent: #1e3b702.patch.diff
in: master

10 files changed

modifiedpallets/common/Cargo.tomldiffbeforeafterboth
--- 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",
 ]
modifiedpallets/common/src/account.rsdiffbeforeafterboth
--- 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<AccountId>:
 	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<AccountId> {
-	fn from_account_id(account_id: AccountId) -> H160;
-}
-
-/// Should have same mapping as EnsureAddressTruncated
-pub struct MapBackwardsAddressTruncated;
-impl EvmBackwardsAddressMapping<AccountId32> 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)
 	}
 }
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- 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<Self::AccountId>;
 
 		type EvmAddressMapping: pallet_evm::AddressMapping<Self::AccountId>;
-		type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;
+		type EvmBackwardsAddressMapping: up_evm_mapping::EvmBackwardsAddressMapping<Self::AccountId>;
 
 		type Currency: Currency<Self::AccountId>;
 		type CollectionCreationPrice: Get<
modifiedpallets/evm-transaction-payment/Cargo.tomldiffbeforeafterboth
--- 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",
 ]
modifiedpallets/evm-transaction-payment/src/lib.rsdiffbeforeafterboth
before · pallets/evm-transaction-payment/src/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]23pub use pallet::*;45#[frame_support::pallet]6pub mod pallet {7	use core::marker::PhantomData;8	use frame_support::traits::Currency;9	use pallet_evm::EVMCurrencyAdapter;10	use fp_evm::WithdrawReason;11	use sp_core::{H160, U256};12	use sp_runtime::TransactionOutcome;13	use up_sponsorship::SponsorshipHandler;14	use sp_std::vec::Vec;1516	type NegativeImbalanceOf<C, T> =17		<C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;1819	#[pallet::config]20	pub trait Config: frame_system::Config {21		type SponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;22		type Currency: Currency<Self::AccountId>;23	}2425	#[pallet::pallet]26	#[pallet::generate_store(pub(super) trait Store)]27	pub struct Pallet<T>(_);2829	pub struct ChargeEvmLiquidityInfo<T>30	where31		T: Config,32		T: pallet_evm::Config,33	{34		who: H160,35		negative_imbalance: NegativeImbalanceOf<<T as Config>::Currency, T>,36	}3738	pub struct TransactionValidityHack<T: Config>(PhantomData<*const T>);39	impl<T: Config> fp_evm::TransactionValidityHack for TransactionValidityHack<T> {40		fn who_pays_fee(origin: H160, reason: &WithdrawReason) -> Option<H160> {41			match reason {42				WithdrawReason::Call { target, input } => {43					// This method is only used for checking, we shouldn't touch storage in it44					frame_support::storage::with_transaction(|| {45						TransactionOutcome::Rollback(T::SponsorshipHandler::get_sponsor(46							&origin,47							&(*target, input.clone()),48						))49					})50				}51				_ => None,52			}53		}54	}5556	pub struct OnChargeTransaction<T: Config>(PhantomData<*const T>);57	impl<T> pallet_evm::OnChargeEVMTransaction<T> for OnChargeTransaction<T>58	where59		T: Config,60		T: pallet_evm::Config,61	{62		type LiquidityInfo = Option<ChargeEvmLiquidityInfo<T>>;6364		fn withdraw_fee(65			who: &H160,66			reason: WithdrawReason,67			fee: U256,68		) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {69			let mut who_pays_fee = *who;70			if let WithdrawReason::Call { target, input } = &reason {71				who_pays_fee = T::SponsorshipHandler::get_sponsor(who, &(*target, input.clone()))72					.unwrap_or(who_pays_fee);73			}74			let negative_imbalance =75				EVMCurrencyAdapter::<<T as Config>::Currency, ()>::withdraw_fee(76					&who_pays_fee,77					reason,78					fee,79				)?;80			Ok(negative_imbalance.map(|i| ChargeEvmLiquidityInfo {81				who: who_pays_fee,82				negative_imbalance: i,83			}))84		}8586		fn correct_and_deposit_fee(87			who: &H160,88			corrected_fee: U256,89			already_withdrawn: Self::LiquidityInfo,90		) {91			<EVMCurrencyAdapter<<T as Config>::Currency, ()> as pallet_evm::OnChargeEVMTransaction<T>>::correct_and_deposit_fee(92				&already_withdrawn.as_ref().map(|e| e.who).unwrap_or(*who),93				corrected_fee,94				already_withdrawn.map(|e| e.negative_imbalance),95			)96		}97	}98}
after · pallets/evm-transaction-payment/src/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]23pub use pallet::*;4use up_evm_mapping::EvmBackwardsAddressMapping;56#[frame_support::pallet]7pub mod pallet {8	use core::marker::PhantomData;9	use frame_support::traits::Currency;10	use pallet_evm::EVMCurrencyAdapter;11	use fp_evm::WithdrawReason;12	use sp_core::{H160, U256};13	use sp_runtime::TransactionOutcome;14	use up_sponsorship::SponsorshipHandler;15	use sp_std::vec::Vec;1617	type NegativeImbalanceOf<C, T> =18		<C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;1920	#[pallet::config]21	pub trait Config: frame_system::Config {22		type SponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;23		type Currency: Currency<Self::AccountId>;24		type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;25	}2627	#[pallet::pallet]28	#[pallet::generate_store(pub(super) trait Store)]29	pub struct Pallet<T>(_);3031	pub struct ChargeEvmLiquidityInfo<T>32	where33		T: Config,34		T: pallet_evm::Config,35	{36		who: H160,37		negative_imbalance: NegativeImbalanceOf<<T as Config>::Currency, T>,38	}3940	pub struct TransactionValidityHack<T: Config>(PhantomData<*const T>);41	impl<T: Config> fp_evm::TransactionValidityHack for TransactionValidityHack<T> {42		fn who_pays_fee(origin: H160, reason: &WithdrawReason) -> Option<H160> {43			match reason {44				WithdrawReason::Call { target, input } => {45					// This method is only used for checking, we shouldn't touch storage in it46					frame_support::storage::with_transaction(|| {47						TransactionOutcome::Rollback(T::SponsorshipHandler::get_sponsor(48							&origin,49							&(*target, input.clone()),50						))51					})52				}53				_ => None,54			}55		}56	}5758	pub struct OnChargeTransaction<T: Config>(PhantomData<*const T>);59	impl<T> pallet_evm::OnChargeEVMTransaction<T> for OnChargeTransaction<T>60	where61		T: Config,62		T: pallet_evm::Config,63	{64		type LiquidityInfo = Option<ChargeEvmLiquidityInfo<T>>;6566		fn withdraw_fee(67			who: &H160,68			reason: WithdrawReason,69			fee: U256,70		) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {71			let mut who_pays_fee = *who;72			if let WithdrawReason::Call { target, input } = &reason {73				who_pays_fee = T::SponsorshipHandler::get_sponsor(who, &(*target, input.clone()))74					.unwrap_or(who_pays_fee);75			}76			let negative_imbalance =77				EVMCurrencyAdapter::<<T as Config>::Currency, ()>::withdraw_fee(78					&who_pays_fee,79					reason,80					fee,81				)?;82			Ok(negative_imbalance.map(|i| ChargeEvmLiquidityInfo {83				who: who_pays_fee,84				negative_imbalance: i,85			}))86		}8788		fn correct_and_deposit_fee(89			who: &H160,90			corrected_fee: U256,91			already_withdrawn: Self::LiquidityInfo,92		) {93			<EVMCurrencyAdapter<<T as Config>::Currency, ()> as pallet_evm::OnChargeEVMTransaction<T>>::correct_and_deposit_fee(94				&already_withdrawn.as_ref().map(|e| e.who).unwrap_or(*who),95				corrected_fee,96				already_withdrawn.map(|e| e.negative_imbalance),97			)98		}99	}100}
modifiedpallets/nft/Cargo.tomldiffbeforeafterboth
--- 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 = [
addedprimitives/evm-mapping/Cargo.tomldiffbeforeafterboth
--- /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",
+]
addedprimitives/evm-mapping/src/lib.rsdiffbeforeafterboth
--- /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<AccountId> {
+	fn from_account_id(account_id: AccountId) -> H160;
+}
+
+/// Should have same mapping as EnsureAddressTruncated
+pub struct MapBackwardsAddressTruncated;
+impl EvmBackwardsAddressMapping<AccountId32> 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)
+	}
+}
modifiedruntime/Cargo.tomldiffbeforeafterboth
--- 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" }
modifiedruntime/src/lib.rsdiffbeforeafterboth
--- 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<Self::Hashing>;
 	type CrossAccountId = pallet_common::account::BasicCrossAccountId<Self>;
 
@@ -811,6 +811,7 @@
 		pallet_evm_contract_helpers::HelpersContractSponsoring<Self>,
 	);
 	type Currency = Balances;
+	type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated;
 }
 
 impl pallet_nft_charge_transaction::Config for Runtime {