diff --git a/precompiles/src/extensions.rs b/precompiles/src/extensions.rs index 62c2cef6e6..fab35a8b79 100644 --- a/precompiles/src/extensions.rs +++ b/precompiles/src/extensions.rs @@ -2,7 +2,7 @@ extern crate alloc; use alloc::format; -use frame_support::dispatch::{GetDispatchInfo, Pays, PostDispatchInfo}; +use frame_support::dispatch::{DispatchInfo, GetDispatchInfo, Pays, PostDispatchInfo}; use frame_system::RawOrigin; use pallet_admin_utils::{PrecompileEnable, PrecompileEnum}; use pallet_evm::{ @@ -71,33 +71,16 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { match call.dispatch(R::RuntimeOrigin::from(origin)) { Ok(post_info) => { - if post_info.pays_fee(&info) == Pays::Yes { - let actual_weight = post_info.actual_weight.unwrap_or(info.call_weight); - let cost = - ::GasWeightMapping::weight_to_gas(actual_weight); - self.record_cost(cost)?; - - self.refund_external_cost( - Some( - info.call_weight - .ref_time() - .saturating_sub(actual_weight.ref_time()), - ), - Some( - info.call_weight - .proof_size() - .saturating_sub(actual_weight.proof_size()), - ), - ); - } - log::debug!("Dispatch succeeded. Post info: {post_info:?}"); + self.charge_and_refund_after_dispatch::(&info, &post_info)?; Ok(()) } Err(e) => { log::error!("Dispatch failed. Error: {e:?}"); log::warn!("Returning error PrecompileFailure::Error"); + self.charge_and_refund_after_dispatch::(&info, &e.post_info)?; + Err(PrecompileFailure::Error { exit_status: ExitError::Other( format!("dispatch execution failed: {}", <&'static str>::from(e)).into(), @@ -106,6 +89,36 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { } } } + + fn charge_and_refund_after_dispatch( + &mut self, + info: &DispatchInfo, + post_info: &PostDispatchInfo, + ) -> EvmResult<()> + where + R: frame_system::Config + pallet_evm::Config, + { + if post_info.pays_fee(info) == Pays::Yes { + let actual_weight = post_info.actual_weight.unwrap_or(info.call_weight); + let cost = ::GasWeightMapping::weight_to_gas(actual_weight); + self.record_cost(cost)?; + + self.refund_external_cost( + Some( + info.call_weight + .ref_time() + .saturating_sub(actual_weight.ref_time()), + ), + Some( + info.call_weight + .proof_size() + .saturating_sub(actual_weight.proof_size()), + ), + ); + } + + Ok(()) + } } impl PrecompileHandleExt for T where T: PrecompileHandle {} diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 9ece1dd025..2171708685 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 347, + spec_version: 348, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,