git.delta.rocks / unique-network / refs/commits / 71150e7c9b75

difftreelog

feat use new ethereum sponsoring interface

Yaroslav Bolyukin2023-10-02parent: #6ab76d3.patch.diff
in: master

2 files changed

modifiedpallets/evm-transaction-payment/src/lib.rsdiffbeforeafterboth
before · pallets/evm-transaction-payment/src/lib.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![doc = include_str!("../README.md")]18#![cfg_attr(not(feature = "std"), no_std)]19#![deny(missing_docs)]2021use core::marker::PhantomData;2223use fp_evm::{CheckEvmTransaction, FeeCalculator, TransactionValidationError, WithdrawReason};24use frame_support::{25	storage::with_transaction,26	traits::{Currency, Imbalance, IsSubType, OnUnbalanced},27};28pub use pallet::*;29use pallet_evm::{30	account::CrossAccountId, EnsureAddressOrigin, NegativeImbalanceOf, OnChargeEVMTransaction,31	OnCheckEvmTransaction,32};33use sp_core::{H160, U256};34use sp_runtime::{traits::UniqueSaturatedInto, DispatchError, TransactionOutcome};35use up_sponsorship::SponsorshipHandler;3637#[frame_support::pallet]38pub mod pallet {39	use sp_std::vec::Vec;4041	use super::*;4243	/// Contains call data44	pub struct CallContext {45		/// Contract address46		pub contract_address: H160,47		/// Transaction data48		pub input: Vec<u8>,49		/// Max fee for transaction - gasLimit * gasPrice50		pub max_fee: U256,51	}5253	#[pallet::config]54	pub trait Config: frame_system::Config + pallet_evm::Config {55		/// Loosly-coupled handlers for evm call sponsoring56		type EvmSponsorshipHandler: SponsorshipHandler<Self::CrossAccountId, CallContext>;57	}5859	#[pallet::pallet]60	pub struct Pallet<T>(_);61}6263/// Implements [`fp_evm::TransactionValidityHack`], which provides sponsor address to pallet-evm64pub struct TransactionValidityHack<T: Config>(PhantomData<*const T>);65impl<T: Config> fp_evm::TransactionValidityHack<T::CrossAccountId> for TransactionValidityHack<T> {66	fn who_pays_fee(67		origin: H160,68		max_fee: U256,69		reason: &WithdrawReason,70	) -> Option<T::CrossAccountId> {71		match reason {72			WithdrawReason::Call { target, input } => {73				let origin_sub = T::CrossAccountId::from_eth(origin);74				let call_context = CallContext {75					contract_address: *target,76					input: input.clone(),77					max_fee,78				};79				T::EvmSponsorshipHandler::get_sponsor(&origin_sub, &call_context)80			}81			_ => None,82		}83	}84}8586/// Implements sponsoring for evm calls performed from pallet-evm (via api.tx.ethereum.transact/api.tx.evm.call)87pub struct BridgeSponsorshipHandler<T>(PhantomData<T>);88impl<T, C> SponsorshipHandler<T::AccountId, C> for BridgeSponsorshipHandler<T>89where90	T: Config + pallet_evm::Config,91	C: IsSubType<pallet_evm::Call<T>>,92{93	fn get_sponsor(who: &T::AccountId, call: &C) -> Option<T::AccountId> {94		match call.is_sub_type()? {95			pallet_evm::Call::call {96				source,97				target,98				input,99				gas_limit,100				max_fee_per_gas,101				..102			} => {103				let _ = T::CallOrigin::ensure_address_origin(104					source,105					<frame_system::RawOrigin<T::AccountId>>::Signed(who.clone()).into(),106				)107				.ok()?;108				let who = T::CrossAccountId::from_sub(who.clone());109				let max_fee = max_fee_per_gas.saturating_mul((*gas_limit).into());110				let call_context = CallContext {111					contract_address: *target,112					input: input.clone(),113					max_fee,114				};115				// Effects from EvmSponsorshipHandler are applied by pallet_evm::runner116				// TODO: Should we implement simulation mode (test, but do not apply effects) in `up-sponsorship`?117				let sponsor = frame_support::storage::with_transaction(|| {118					TransactionOutcome::Rollback(Ok::<_, DispatchError>(119						T::EvmSponsorshipHandler::get_sponsor(&who, &call_context),120					))121				})122				// FIXME: it may fail with DispatchError in case of depth limit123				.ok()??;124				Some(sponsor.as_sub().clone())125			}126			_ => None,127		}128	}129}
modifiedruntime/common/config/ethereum.rsdiffbeforeafterboth
--- a/runtime/common/config/ethereum.rs
+++ b/runtime/common/config/ethereum.rs
@@ -89,12 +89,13 @@
 	type OnCreate = pallet_evm_contract_helpers::HelpersOnCreate<Self>;
 	type ChainId = ChainId;
 	type Runner = pallet_evm::runner::stack::Runner<Self>;
-	type OnChargeTransaction = pallet_evm::EVMCurrencyAdapter<Balances, DealWithFees>;
-	type TransactionValidityHack = pallet_evm_transaction_payment::TransactionValidityHack<Self>;
+	type OnChargeTransaction =
+		pallet_evm_transaction_payment::WrappedEVMCurrencyAdapter<Balances, DealWithFees>;
 	type FindAuthor = EthereumFindAuthor<Aura>;
 	type Timestamp = crate::Timestamp;
 	type WeightInfo = pallet_evm::weights::SubstrateWeight<Self>;
 	type GasLimitPovSizeRatio = ProofSizePerGas;
+	type OnCheckEvmTransaction = pallet_evm_transaction_payment::TransactionValidity<Self>;
 }
 
 impl pallet_evm_migration::Config for Runtime {