Skip to content

Commit 11dbff3

Browse files
authored
Merge pull request #2216 from opentensor/refund-evm-failed-preocompile
refund for both success and fail
2 parents a1369c7 + a57c732 commit 11dbff3

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

precompiles/src/extensions.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate alloc;
22

33
use alloc::format;
44

5-
use frame_support::dispatch::{GetDispatchInfo, Pays, PostDispatchInfo};
5+
use frame_support::dispatch::{DispatchInfo, GetDispatchInfo, Pays, PostDispatchInfo};
66
use frame_system::RawOrigin;
77
use pallet_admin_utils::{PrecompileEnable, PrecompileEnum};
88
use pallet_evm::{
@@ -71,33 +71,16 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle {
7171

7272
match call.dispatch(R::RuntimeOrigin::from(origin)) {
7373
Ok(post_info) => {
74-
if post_info.pays_fee(&info) == Pays::Yes {
75-
let actual_weight = post_info.actual_weight.unwrap_or(info.call_weight);
76-
let cost =
77-
<R as pallet_evm::Config>::GasWeightMapping::weight_to_gas(actual_weight);
78-
self.record_cost(cost)?;
79-
80-
self.refund_external_cost(
81-
Some(
82-
info.call_weight
83-
.ref_time()
84-
.saturating_sub(actual_weight.ref_time()),
85-
),
86-
Some(
87-
info.call_weight
88-
.proof_size()
89-
.saturating_sub(actual_weight.proof_size()),
90-
),
91-
);
92-
}
93-
9474
log::debug!("Dispatch succeeded. Post info: {post_info:?}");
75+
self.charge_and_refund_after_dispatch::<R, Call>(&info, &post_info)?;
9576

9677
Ok(())
9778
}
9879
Err(e) => {
9980
log::error!("Dispatch failed. Error: {e:?}");
10081
log::warn!("Returning error PrecompileFailure::Error");
82+
self.charge_and_refund_after_dispatch::<R, Call>(&info, &e.post_info)?;
83+
10184
Err(PrecompileFailure::Error {
10285
exit_status: ExitError::Other(
10386
format!("dispatch execution failed: {}", <&'static str>::from(e)).into(),
@@ -106,6 +89,36 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle {
10689
}
10790
}
10891
}
92+
93+
fn charge_and_refund_after_dispatch<R, Call>(
94+
&mut self,
95+
info: &DispatchInfo,
96+
post_info: &PostDispatchInfo,
97+
) -> EvmResult<()>
98+
where
99+
R: frame_system::Config + pallet_evm::Config,
100+
{
101+
if post_info.pays_fee(info) == Pays::Yes {
102+
let actual_weight = post_info.actual_weight.unwrap_or(info.call_weight);
103+
let cost = <R as pallet_evm::Config>::GasWeightMapping::weight_to_gas(actual_weight);
104+
self.record_cost(cost)?;
105+
106+
self.refund_external_cost(
107+
Some(
108+
info.call_weight
109+
.ref_time()
110+
.saturating_sub(actual_weight.ref_time()),
111+
),
112+
Some(
113+
info.call_weight
114+
.proof_size()
115+
.saturating_sub(actual_weight.proof_size()),
116+
),
117+
);
118+
}
119+
120+
Ok(())
121+
}
109122
}
110123

111124
impl<T> PrecompileHandleExt for T where T: PrecompileHandle {}

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
220220
// `spec_version`, and `authoring_version` are the same between Wasm and native.
221221
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
222222
// the compatible custom types.
223-
spec_version: 347,
223+
spec_version: 348,
224224
impl_version: 1,
225225
apis: RUNTIME_API_VERSIONS,
226226
transaction_version: 1,

0 commit comments

Comments
 (0)