Skip to content

Commit

Permalink
Refactor XVM weight.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 12, 2023
1 parent cc73fa4 commit ca3dc07
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
4 changes: 1 addition & 3 deletions chain-extensions/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::dispatch::Encode;
use frame_support::weights::Weight;
use pallet_contracts::chain_extension::{ChainExtension, Environment, Ext, InitState, RetVal};
use pallet_xvm::XvmContext;
use sp_runtime::DispatchError;
Expand Down Expand Up @@ -89,8 +88,7 @@ where
pallet_xvm::Pallet::<T>::xvm_bare_call(xvm_context, caller, to, input);

let actual_weight = pallet_xvm::consumed_weight(&call_result);
// TODO: implement proof of size refund.
env.adjust_weight(charged_weight, Weight::from_parts(actual_weight, 0));
env.adjust_weight(charged_weight, actual_weight);

match call_result {
Ok(success) => {
Expand Down
7 changes: 2 additions & 5 deletions pallets/pallet-xvm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
},
)
.map_err(|e| {
let consumed_weight = e.post_info.actual_weight.unwrap_or_default().ref_time();
let consumed_weight = e.post_info.actual_weight.unwrap_or_default();
XvmCallError {
error: XvmError::ExecutionError(Into::<&str>::into(e.error).into()),
consumed_weight,
Expand All @@ -91,10 +91,7 @@ where

Ok(XvmCallOk {
output: call_info.value,
consumed_weight: post_dispatch_info
.actual_weight
.unwrap_or_default()
.ref_time(),
consumed_weight: post_dispatch_info.actual_weight.unwrap_or_default(),
})
}
}
8 changes: 4 additions & 4 deletions pallets/pallet-xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub type VmId = u8;

// TODO: remove later after solution is properly benchmarked
// Just a arbitrary weight constant to avoid having ZERO weight in some parts of execution
pub const PLACEHOLDER_WEIGHT: u64 = 1_000_000;
pub const PLACEHOLDER_WEIGHT: Weight = Weight::from_parts(1_000_000, 0);

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, scale_info::TypeInfo)]
pub enum XvmError {
Expand All @@ -78,7 +78,7 @@ pub struct XvmCallOk {
/// Output of XVM call. E.g. if call was a query, this will contain query response.
output: Vec<u8>,
/// Total consumed weight. This is in context of Substrate (1 unit of weight ~ 1 ps of execution time)
consumed_weight: u64,
consumed_weight: Weight,
}

impl XvmCallOk {
Expand All @@ -94,7 +94,7 @@ pub struct XvmCallError {
// TODO: use XvmError enum from pallet? Perhaps that's a better approach. Or at least provide mapping?
error: XvmError,
/// Total consumed weight. This is in context of Substrate (1 unit of weight ~ 1 ps of execution time)
consumed_weight: u64,
consumed_weight: Weight,
}

impl XvmCallError {
Expand All @@ -106,7 +106,7 @@ impl XvmCallError {
/// Result for executing X-VM calls
pub type XvmResult = Result<XvmCallOk, XvmCallError>;

pub fn consumed_weight(result: &XvmResult) -> u64 {
pub fn consumed_weight(result: &XvmResult) -> Weight {
match result {
Ok(res) => res.consumed_weight,
Err(err) => err.consumed_weight,
Expand Down
2 changes: 1 addition & 1 deletion pallets/pallet-xvm/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
"WASM XVM call result: {:?}", call_result
);

let consumed_weight = call_result.gas_consumed.ref_time();
let consumed_weight = call_result.gas_consumed;

match call_result.result {
Ok(success) => Ok(XvmCallOk {
Expand Down

0 comments on commit ca3dc07

Please sign in to comment.