Skip to content

Commit f832bda

Browse files
committed
fix
1 parent 1c29484 commit f832bda

File tree

4 files changed

+73
-36
lines changed

4 files changed

+73
-36
lines changed

substrate/frame/revive/src/evm/block_hash/block_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131
use alloc::{vec, vec::Vec};
3232

3333
use codec::{Decode, Encode};
34-
use frame_support::{weights::Weight, DefaultNoBound};
34+
use frame_support::DefaultNoBound;
3535
use scale_info::TypeInfo;
3636
use sp_core::{keccak_256, H160, H256, U256};
3737

@@ -97,7 +97,7 @@ impl<T: crate::Config> EthereumBlockBuilder<T> {
9797
&mut self,
9898
transaction_encoded: Vec<u8>,
9999
success: bool,
100-
gas_used: Weight,
100+
gas_used: U256,
101101
encoded_logs: Vec<u8>,
102102
receipt_bloom: LogsBloom,
103103
) {
@@ -108,7 +108,7 @@ impl<T: crate::Config> EthereumBlockBuilder<T> {
108108
let transaction_type = Self::extract_transaction_type(transaction_encoded.as_slice());
109109

110110
// Update gas and logs bloom.
111-
self.gas_used = self.gas_used.saturating_add(gas_used.ref_time().into());
111+
self.gas_used = self.gas_used.saturating_add(gas_used);
112112
self.logs_bloom.accrue_bloom(&receipt_bloom);
113113

114114
// Update the receipt trie.
@@ -120,7 +120,7 @@ impl<T: crate::Config> EthereumBlockBuilder<T> {
120120
transaction_type,
121121
);
122122

123-
self.gas_info.push(ReceiptGasInfo { gas_used: gas_used.ref_time().into() });
123+
self.gas_info.push(ReceiptGasInfo { gas_used });
124124

125125
// The first transaction and receipt are returned to be stored in the pallet storage.
126126
// The index of the incremental hash builders already expects the next items.

substrate/frame/revive/src/evm/block_storage.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
2726
use frame_support::{
2827
pallet_prelude::{DispatchError, DispatchResultWithPostInfo},
2928
storage::with_transaction,
30-
weights::Weight,
3129
};
3230
use sp_core::U256;
3331
use sp_runtime::TransactionOutcome;
@@ -41,6 +39,27 @@ pub const BLOCK_HASH_COUNT: u32 = 256;
4139
// that are needed to construct the final transaction receipt.
4240
environmental!(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`.
7695
pub 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.

substrate/frame/revive/src/evm/fees.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,18 @@ pub trait InfoT<T: Config>: seal::Sealed {
105105
Zero::zero()
106106
}
107107

108-
/// Makes sure that not too much storage deposit was withdrawn.
109-
fn ensure_not_overdrawn(
108+
/// Compute the actual post_dispatch fee
109+
fn compute_actual_fee(
110110
_encoded_len: u32,
111111
_info: &DispatchInfo,
112+
_result: &DispatchResultWithPostInfo,
113+
) -> BalanceOf<T> {
114+
Default::default()
115+
}
116+
117+
/// Makes sure that not too much storage deposit was withdrawn.
118+
fn ensure_not_overdrawn(
119+
_fee: BalanceOf<T>,
112120
result: DispatchResultWithPostInfo,
113121
) -> DispatchResultWithPostInfo {
114122
result
@@ -259,9 +267,29 @@ where
259267
.saturating_add(Self::length_to_fee(encoded_len))
260268
}
261269

262-
fn ensure_not_overdrawn(
270+
fn compute_actual_fee(
263271
encoded_len: u32,
264272
info: &DispatchInfo,
273+
result: &DispatchResultWithPostInfo,
274+
) -> BalanceOf<E::Config> {
275+
let mut post_info = match result {
276+
Ok(post_info) => post_info,
277+
Err(err) => &err.post_info,
278+
}
279+
.clone();
280+
281+
post_info
282+
.actual_weight
283+
.as_mut()
284+
.map(|w| w.saturating_accrue(info.extension_weight));
285+
286+
log::debug!(target: LOG_TARGET, "revive: post_info:{post_info:?}");
287+
<TxPallet<E::Config>>::compute_actual_fee(encoded_len, info, &post_info, Zero::zero())
288+
.into()
289+
}
290+
291+
fn ensure_not_overdrawn(
292+
fee: BalanceOf<E::Config>,
265293
result: DispatchResultWithPostInfo,
266294
) -> DispatchResultWithPostInfo {
267295
// if tx is already failing we can ignore
@@ -270,9 +298,6 @@ where
270298
return result;
271299
};
272300

273-
let fee: BalanceOf<E::Config> =
274-
<TxPallet<E::Config>>::compute_actual_fee(encoded_len, info, &post_info, Zero::zero())
275-
.into();
276301
let available = Self::remaining_txfee();
277302
if fee > available {
278303
log::debug!(target: LOG_TARGET, "Drew too much from the txhold. \

substrate/frame/revive/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,10 @@ pub mod pallet {
12671267
output.gas_consumed,
12681268
base_info.call_weight,
12691269
);
1270-
let result = T::FeeInfo::ensure_not_overdrawn(encoded_len, &info, result);
1271-
(output.gas_consumed, result)
1270+
1271+
let native_fee = T::FeeInfo::compute_actual_fee(encoded_len, &info, &result);
1272+
let result = T::FeeInfo::ensure_not_overdrawn(native_fee, result);
1273+
block_storage::EthereumCallResult::new::<T>(native_fee, effective_gas_price, result)
12721274
})
12731275
}
12741276

@@ -1329,8 +1331,11 @@ pub mod pallet {
13291331

13301332
let result =
13311333
dispatch_result(output.result, output.gas_consumed, base_info.call_weight);
1332-
let result = T::FeeInfo::ensure_not_overdrawn(encoded_len, &info, result);
1333-
(output.gas_consumed, result)
1334+
1335+
let native_fee: BalanceOf<T> =
1336+
T::FeeInfo::compute_actual_fee(encoded_len, &info, &result);
1337+
let result = T::FeeInfo::ensure_not_overdrawn(native_fee, result);
1338+
block_storage::EthereumCallResult::new::<T>(native_fee, effective_gas_price, result)
13341339
})
13351340
}
13361341

0 commit comments

Comments
 (0)