git.delta.rocks / unique-network / refs/commits / 4a3d8395a0e4

difftreelog

feat sponsor evm transactions coming from substrate

Yaroslav Bolyukin2021-11-18parent: #82bedef.patch.diff
in: master

2 files changed

modifiedpallets/evm-transaction-payment/src/lib.rsdiffbeforeafterboth
1#![cfg_attr(not(feature = "std"), no_std)]1#![cfg_attr(not(feature = "std"), no_std)]
22
3pub use pallet::*;
4use up_evm_mapping::EvmBackwardsAddressMapping;
5
6#[frame_support::pallet]
7pub mod pallet {
8 use core::marker::PhantomData;3use core::marker::PhantomData;
4use fp_evm::WithdrawReason;
9 use frame_support::traits::Currency;5use frame_support::traits::{Currency, IsSubType};
10 use pallet_evm::EVMCurrencyAdapter;6pub use pallet::*;
11 use fp_evm::WithdrawReason;7use pallet_evm::{EVMCurrencyAdapter, EnsureAddressOrigin};
12 use sp_core::{H160, U256};8use sp_core::{H160, U256};
13 use sp_runtime::TransactionOutcome;9use sp_runtime::TransactionOutcome;
14 use up_sponsorship::SponsorshipHandler;10use up_sponsorship::SponsorshipHandler;
11use up_evm_mapping::EvmBackwardsAddressMapping;
12use pallet_evm::AddressMapping;
13
14#[frame_support::pallet]
15pub mod pallet {
16 use super::*;
17
15 use sp_std::vec::Vec;18 use frame_support::traits::Currency;
16
17 type NegativeImbalanceOf<C, T> =
18 <C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;19 use sp_std::vec::Vec;
1920
20 #[pallet::config]21 #[pallet::config]
21 pub trait Config: frame_system::Config {22 pub trait Config: frame_system::Config {
22 type SponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;23 type EvmSponsorshipHandler: SponsorshipHandler<H160, (H160, Vec<u8>)>;
23 type Currency: Currency<Self::AccountId>;24 type Currency: Currency<Self::AccountId>;
24 type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;25 type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;
26 type EvmAddressMapping: AddressMapping<Self::AccountId>;
25 }27 }
2628
27 #[pallet::pallet]29 #[pallet::pallet]
28 #[pallet::generate_store(pub(super) trait Store)]30 #[pallet::generate_store(pub(super) trait Store)]
29 pub struct Pallet<T>(_);31 pub struct Pallet<T>(_);
32}
33
34type NegativeImbalanceOf<C, T> =
35 <C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;
3036
31 pub struct ChargeEvmLiquidityInfo<T>37pub struct ChargeEvmLiquidityInfo<T>
32 where38where
44 WithdrawReason::Call { target, input } => {50 WithdrawReason::Call { target, input } => {
45 // This method is only used for checking, we shouldn't touch storage in it51 // This method is only used for checking, we shouldn't touch storage in it
46 frame_support::storage::with_transaction(|| {52 frame_support::storage::with_transaction(|| {
47 TransactionOutcome::Rollback(T::SponsorshipHandler::get_sponsor(53 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(
48 &origin,54 &origin,
49 &(*target, input.clone()),55 &(*target, input.clone()),
50 ))56 ))
70 ) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {75 ) -> core::result::Result<Self::LiquidityInfo, pallet_evm::Error<T>> {
71 let mut who_pays_fee = *who;76 let mut who_pays_fee = *who;
72 if let WithdrawReason::Call { target, input } = &reason {77 if let WithdrawReason::Call { target, input } = &reason {
73 who_pays_fee = T::SponsorshipHandler::get_sponsor(who, &(*target, input.clone()))78 who_pays_fee = T::EvmSponsorshipHandler::get_sponsor(who, &(*target, input.clone()))
74 .unwrap_or(who_pays_fee);79 .unwrap_or(who_pays_fee);
75 }80 }
76 let negative_imbalance =81 let negative_imbalance = EVMCurrencyAdapter::<<T as Config>::Currency, ()>::withdraw_fee(
97 )101 )
98 }102 }
99 }103}
100}104
105/// Implements sponsoring for evm calls performed from pallet-evm (via api.tx.ethereum.transact/api.tx.evm.call)
106pub struct BridgeSponsorshipHandler<T>(PhantomData<T>);
107impl<T, C> SponsorshipHandler<T::AccountId, C> for BridgeSponsorshipHandler<T>
108where
109 T: Config + pallet_evm::Config,
110 C: IsSubType<pallet_evm::Call<T>>,
111{
112 fn get_sponsor(who: &T::AccountId, call: &C) -> Option<T::AccountId> {
113 match call.is_sub_type()? {
114 pallet_evm::Call::call {
115 source,
116 target,
117 input,
118 ..
119 } => {
120 let _ = T::CallOrigin::ensure_address_origin(
121 source,
122 <frame_system::RawOrigin<T::AccountId>>::Signed(who.clone()).into(),
123 )
124 .ok()?;
125 let who = T::EvmBackwardsAddressMapping::from_account_id(who.clone());
126 // Effects from EvmSponsorshipHandler are applied in OnChargeEvmTransaction by pallet_evm::runner
127 // TODO: Should we implement simulation mode (test, but do not apply effects) in `up-sponsorship`?
128 let sponsor = frame_support::storage::with_transaction(|| {
129 TransactionOutcome::Rollback(T::EvmSponsorshipHandler::get_sponsor(
130 &who,
131 &(target.clone(), input.clone()),
132 ))
133 })?;
134 let sponsor = T::EvmAddressMapping::into_account_id(sponsor);
135 Some(sponsor)
136 }
137 _ => None,
138 }
139 }
140}
101141
modifiedruntime/src/lib.rsdiffbeforeafterboth
--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -777,20 +777,14 @@
 	pub const MaxScheduledPerBlock: u32 = 50;
 }
 
-pub struct Sponsoring;
-impl SponsoringResolve<AccountId, Call> for Sponsoring {
-	fn resolve(who: &AccountId, call: &Call) -> Option<AccountId>
-	where
-		Call: Dispatchable<Info = DispatchInfo>,
-		AccountId: AsRef<[u8]>,
-	{
-		pallet_nft_transaction_payment::Module::<Runtime>::withdraw_type(who, call)
-	}
-}
-
+type EvmSponsorshipHandler = (
+	pallet_nft::NftEthSponsorshipHandler<Runtime>,
+	pallet_evm_contract_helpers::HelpersContractSponsoring<Runtime>,
+);
 type SponsorshipHandler = (
 	pallet_nft::NftSponsorshipHandler<Runtime>,
 	//pallet_contract_helpers::ContractSponsorshipHandler<Runtime>,
+	pallet_evm_transaction_payment::BridgeSponsorshipHandler<Runtime>,
 );
 
 impl pallet_unq_scheduler::Config for Runtime {
@@ -806,16 +800,14 @@
 }
 
 impl pallet_evm_transaction_payment::Config for Runtime {
-	type SponsorshipHandler = (
-		pallet_nft::NftEthSponsorshipHandler<Self>,
-		pallet_evm_contract_helpers::HelpersContractSponsoring<Self>,
-	);
+	type EvmSponsorshipHandler = EvmSponsorshipHandler;
 	type Currency = Balances;
+	type EvmAddressMapping = HashedAddressMapping<Self::Hashing>;
 	type EvmBackwardsAddressMapping = up_evm_mapping::MapBackwardsAddressTruncated;
 }
 
 impl pallet_nft_charge_transaction::Config for Runtime {
-	type SponsorshipHandler = pallet_nft::NftSponsorshipHandler<Runtime>;
+	type SponsorshipHandler = SponsorshipHandler;
 }
 
 // impl pallet_contract_helpers::Config for Runtime {