Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XVM CE: Sufficient charged gas check before XVM execution. #1073

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chain-extensions/types/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl From<FailureReason> for XvmExecutionResult {
FailureReason::Error(FailureError::SameVmCallDenied) => 129,
FailureReason::Error(FailureError::ReentranceDenied) => 130,
FailureReason::Error(FailureError::VmError(_)) => 131,
FailureReason::Error(FailureError::OutOfGas) => 132,
};
Self::Err(error_code)
}
Expand Down
28 changes: 22 additions & 6 deletions chain-extensions/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use alloc::format;

use astar_primitives::{
evm::UnifiedAddressMapper,
xvm::{Context, VmId, XvmCall},
xvm::{CallFailure, Context, FailureError, VmId, XvmCall},
};
use frame_support::{dispatch::Encode, weights::Weight};
use frame_system::RawOrigin;
Expand Down Expand Up @@ -78,10 +78,7 @@ where
XvmFuncId::Call => {
// We need to immediately charge for the worst case scenario. Gas equals Weight in pallet-contracts context.
let weight_limit = env.ext().gas_meter().gas_left();
// TODO: track proof size in align fees ticket
// We don't track used proof size, so we can't refund after.
// So we will charge a 32KB dummy value as a temporary replacement.
let charged_weight = env.charge_weight(weight_limit.set_proof_size(32 * 1024))?;
let charged_weight = env.charge_weight(weight_limit)?;

let XvmCallArgs {
vm_id,
Expand All @@ -103,9 +100,16 @@ where
<T as pallet_unified_accounts::Config>::WeightInfo::to_h160(),
);

if actual_weight.any_gt(weight_limit) {
return out_of_gas_err(actual_weight);
}

if UA::to_h160(&source).is_none() {
let weight_of_claim = <T as pallet_unified_accounts::Config>::WeightInfo::claim_default_evm_address();
actual_weight.saturating_accrue(weight_of_claim);
if actual_weight.any_gt(weight_limit) {
return out_of_gas_err(actual_weight);
}

let claim_result =
pallet_unified_accounts::Pallet::<T>::claim_default_evm_address(
Expand All @@ -122,7 +126,8 @@ where

let xvm_context = Context {
source_vm_id: VmId::Wasm,
weight_limit,
// Weight limit left for XVM call.
weight_limit: weight_limit.saturating_sub(actual_weight),
};
let vm_id = {
match TryInto::<VmId>::try_into(vm_id) {
Expand Down Expand Up @@ -174,3 +179,14 @@ where
}
}
}

fn out_of_gas_err(actual_weight: Weight) -> Result<RetVal, DispatchError> {
Ok(RetVal::Diverging {
flags: ReturnFlags::REVERT,
data: format!(
"{:?}",
CallFailure::error(FailureError::OutOfGas, actual_weight)
)
.into(),
})
}
2 changes: 2 additions & 0 deletions primitives/src/xvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub enum FailureError {
ReentranceDenied,
/// The call failed with error on EVM or WASM execution.
VmError(Vec<u8>),
/// Out of gas.
OutOfGas,
}

/// XVM call result.
Expand Down
Loading