@@ -18,7 +18,6 @@ use crate::{
1818 evm:: block_hash:: { AccumulateReceipt , EthereumBlockBuilder , LogsBloom } ,
1919 limits,
2020 sp_runtime:: traits:: One ,
21- weights:: WeightInfo ,
2221 BlockHash , Config , EthBlockBuilderIR , EthereumBlock , Event , Pallet , ReceiptInfoData ,
2322 UniqueSaturatedInto , H160 , H256 ,
2423} ;
@@ -27,7 +26,6 @@ use environmental::environmental;
2726use frame_support:: {
2827 pallet_prelude:: { DispatchError , DispatchResultWithPostInfo } ,
2928 storage:: with_transaction,
30- weights:: Weight ,
3129} ;
3230use sp_core:: U256 ;
3331use sp_runtime:: TransactionOutcome ;
@@ -41,6 +39,27 @@ pub const BLOCK_HASH_COUNT: u32 = 256;
4139// that are needed to construct the final transaction receipt.
4240environmental ! ( receipt: AccumulateReceipt ) ;
4341
42+ /// Result of an Ethereum context call execution.
43+ pub ( crate ) struct EthereumCallResult {
44+ /// The amount of gas used by the call.
45+ pub gas_used : U256 ,
46+ /// The dispatch result with post-dispatch information.
47+ pub result : DispatchResultWithPostInfo ,
48+ }
49+
50+ impl EthereumCallResult {
51+ /// Create a new `EthereumCallResult` from a native fee and effective gas price.
52+ pub ( crate ) fn new < T : Config > (
53+ native_fee : crate :: BalanceOf < T > ,
54+ effective_gas_price : U256 ,
55+ result : DispatchResultWithPostInfo ,
56+ ) -> Self {
57+ let eth_fee = Pallet :: < T > :: convert_native_to_evm ( native_fee) ;
58+ let gas_used = eth_fee / effective_gas_price;
59+ Self { gas_used, result }
60+ }
61+ }
62+
4463/// Capture the Ethereum log for the current transaction.
4564///
4665/// This method does nothing if called from outside of the ethereum context.
@@ -72,23 +91,19 @@ pub fn bench_with_ethereum_context<R>(f: impl FnOnce() -> R) -> R {
7291///
7392/// # Parameters
7493/// - transaction_encoded: The RLP encoded transaction bytes.
75- /// - call: A closure that executes the transaction logic and returns the gas consumed and result .
94+ /// - call: A closure that executes the transaction logic and returns an `EthereumCallResult` .
7695pub fn with_ethereum_context < T : Config > (
7796 transaction_encoded : Vec < u8 > ,
78- call : impl FnOnce ( ) -> ( Weight , DispatchResultWithPostInfo ) ,
97+ call : impl FnOnce ( ) -> EthereumCallResult ,
7998) -> DispatchResultWithPostInfo {
8099 receipt:: using ( & mut AccumulateReceipt :: new ( ) , || {
81- let ( err, gas_consumed, mut post_info) =
100+ let ( err, gas_consumed, post_info) =
82101 with_transaction ( || -> TransactionOutcome < Result < _ , DispatchError > > {
83- let ( gas_consumed , result) = call ( ) ;
102+ let EthereumCallResult { gas_used , result } = call ( ) ;
84103 match result {
85- Ok ( post_info) =>
86- TransactionOutcome :: Commit ( Ok ( ( None , gas_consumed, post_info) ) ) ,
87- Err ( err) => TransactionOutcome :: Rollback ( Ok ( (
88- Some ( err. error ) ,
89- gas_consumed,
90- err. post_info ,
91- ) ) ) ,
104+ Ok ( post_info) => TransactionOutcome :: Commit ( Ok ( ( None , gas_used, post_info) ) ) ,
105+ Err ( err) =>
106+ TransactionOutcome :: Rollback ( Ok ( ( Some ( err. error ) , gas_used, err. post_info ) ) ) ,
92107 }
93108 } ) ?;
94109
@@ -106,10 +121,6 @@ pub fn with_ethereum_context<T: Config>(
106121 deposit_eth_extrinsic_revert_event :: < T > ( crate :: Error :: < T > :: BenchmarkingError . into ( ) ) ;
107122
108123 crate :: block_storage:: process_transaction :: < T > ( transaction_encoded, true , gas_consumed) ;
109- post_info
110- . actual_weight
111- . as_mut ( )
112- . map ( |w| w. saturating_reduce ( T :: WeightInfo :: deposit_eth_extrinsic_revert_event ( ) ) ) ;
113124 Ok ( post_info)
114125 }
115126 } )
@@ -174,11 +185,7 @@ pub fn on_finalize_build_eth_block<T: Config>(
174185/// This stores the RLP encoded transaction and receipt details into storage.
175186///
176187/// The data is used during the `on_finalize` hook to reconstruct the ETH block.
177- pub fn process_transaction < T : Config > (
178- transaction_encoded : Vec < u8 > ,
179- success : bool ,
180- gas_used : Weight ,
181- ) {
188+ pub fn process_transaction < T : Config > ( transaction_encoded : Vec < u8 > , success : bool , gas_used : U256 ) {
182189 // Method returns `None` only when called from outside of the ethereum context.
183190 // This is not the case here, since this is called from within the
184191 // ethereum context.
0 commit comments