Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ impl WasmerProdInstanceState {
}

impl InstanceState for WasmerProdInstanceState {
// TODO: delete
fn get_points_limit(&self) -> Result<u64, ExecutorError> {
// InstanceState::get_points_limit(&self)
// self.instance_rc()?.get_points_limit()
todo!()
Err(anyhow!("get_points_limit not supported, to be deleted").into())
}

fn set_points_used(&mut self, points: u64) -> Result<(), ExecutorError> {
Expand Down
2 changes: 2 additions & 0 deletions chain/vm/src/host/vm_hooks/vh_handler/vh_call_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use super::VMHooksManagedTypes;

pub trait VMHooksCallValue: VMHooksHandlerSource + VMHooksManagedTypes {
fn check_not_payable(&mut self) {
self.use_gas(self.gas_schedule().base_ops_api_cost.get_call_value);

if self.input_ref().egld_value > num_bigint::BigUint::zero() {
self.vm_error(vm_err_msg::NON_PAYABLE_FUNC_EGLD);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
host::context::big_int_to_i64,
host::vm_hooks::{VMHooksError, VMHooksHandlerSource},
host::{
context::big_int_to_i64,
vm_hooks::{VMHooksError, VMHooksHandlerSource},
},
types::RawHandle,
vm_err_msg,
};
Expand Down Expand Up @@ -51,12 +53,16 @@ macro_rules! unary_op_method {

/// Provides VM hook implementations for methods that deal big ints.
pub trait VMHooksBigInt: VMHooksHandlerSource + VMHooksError {
fn bi_new(&self, value: i64) -> RawHandle {
fn bi_new(&mut self, value: i64) -> RawHandle {
self.use_gas(self.gas_schedule().big_int_api_cost.big_int_new);

self.m_types_lock()
.bi_new_from_big_int(num_bigint::BigInt::from(value))
}

fn bi_set_int64(&self, destination: RawHandle, value: i64) {
fn bi_set_int64(&mut self, destination: RawHandle, value: i64) {
self.use_gas(self.gas_schedule().big_int_api_cost.big_int_set_int_64);

self.m_types_lock()
.bi_overwrite(destination, num_bigint::BigInt::from(value))
}
Expand Down
5 changes: 5 additions & 0 deletions chain/vm/src/host/vm_hooks/vh_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
host::context::{
BackTransfers, ManagedTypeContainer, TxFunctionName, TxInput, TxLog, TxResult,
},
schedule::GasSchedule,
types::{VMAddress, VMCodeMetadata, H256},
};

Expand Down Expand Up @@ -35,6 +36,10 @@ pub trait VMHooksHandlerSource: Debug {
self.halt_with_error(ReturnCode::ExecutionFailed, message)
}

fn gas_schedule(&self) -> &GasSchedule;

fn use_gas(&mut self, gas: u64);

fn input_ref(&self) -> &TxInput;

fn current_address(&self) -> &VMAddress {
Expand Down
27 changes: 27 additions & 0 deletions chain/vm/src/host/vm_hooks/vh_tx_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use num_bigint::BigUint;
use num_traits::Zero;

use crate::host::runtime::RuntimeInstanceCallLambdaDefault;
use crate::schedule::GasSchedule;
use crate::{
blockchain::{
reserved::STORAGE_RESERVED_PREFIX,
Expand Down Expand Up @@ -74,6 +75,32 @@ impl<S: InstanceState> VMHooksHandlerSource for TxContextVMHooksHandler<S> {
let _ = self.instance_state_ref.set_breakpoint_value(breakpoint);
}

fn gas_schedule(&self) -> &GasSchedule {
&self.tx_context_ref.0.runtime_ref.vm_ref.gas_schedule
}

fn use_gas(&mut self, gas: u64) {
let gas_limit = self.input_ref().gas_limit;
let state_ref = &mut self.instance_state_ref;
let prev_gas_used = state_ref
.get_points_used()
.expect("error fetching points used from instance state");

let next_gas_used = prev_gas_used + gas;

// println!("use gas {gas}: {prev_gas_used} -> {next_gas_used}");

if next_gas_used > gas_limit {
state_ref
.set_breakpoint_value(BreakpointValue::OutOfGas)
.expect("error setting breakpoint value in instance");
} else {
state_ref
.set_points_used(next_gas_used)
.expect("error setting points used in instance");
}
}

fn input_ref(&self) -> &TxInput {
self.tx_context_ref.input_ref()
}
Expand Down
7 changes: 7 additions & 0 deletions chain/vm/src/schedule/gas_schedule.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem::MaybeUninit;

use multiversx_chain_vm_executor::OpcodeCost;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -41,4 +43,9 @@ impl GasSchedule {
let full_schedule: GasSchedule = toml::from_str(content)?;
Ok(full_schedule)
}

/// TODO: safer to replace with auto-generated zero const initializer
pub const fn zeroed() -> GasSchedule {
unsafe { MaybeUninit::zeroed().assume_init() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ fn serialize_deserialize_test() {

assert_eq!(gas_schedule_v8, deserialized);
}

#[test]
fn zeroed_schedule_test() {
const ZERO_GAS_SCHEDULE: GasSchedule = GasSchedule::zeroed();
assert_eq!(ZERO_GAS_SCHEDULE.wasm_opcode_cost.opcode_unreachable, 0);
assert_eq!(ZERO_GAS_SCHEDULE.wasm_opcode_cost.opcode_nop, 0);
assert_eq!(ZERO_GAS_SCHEDULE.wasm_opcode_cost.opcode_block, 0);
}
4 changes: 2 additions & 2 deletions contracts/feature-tests/gas-tests/tests/factorial_gas_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn factorial_gas_test(mut world: ScenarioWorld) {
let (new_address, gas_used) = world
.tx()
.from(OWNER_ADDRESS)
.gas(100)
.gas(1500)
.typed(factorial_proxy::FactorialProxy)
.init()
.code(CODE_PATH)
Expand All @@ -22,7 +22,7 @@ fn factorial_gas_test(mut world: ScenarioWorld) {
.returns(ReturnsGasUsed)
.run();

assert_eq!(gas_used, 45);
assert_eq!(gas_used, 1045);
assert_eq!(new_address, SC_ADDRESS.to_address());
}

Expand Down
24 changes: 18 additions & 6 deletions framework/scenario/src/api/impl_vh/vh_single_tx_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ use multiversx_chain_vm_executor::{MemLength, MemPtr};
use multiversx_chain_vm::{
blockchain::state::{AccountData, BlockInfo},
chain_core::types::ReturnCode,
host::context::{BackTransfers, ManagedTypeContainer, TxFunctionName, TxInput, TxResult},
host::vm_hooks::{
VMHooksBigFloat, VMHooksBigInt, VMHooksBlockchain, VMHooksCallValue, VMHooksCrypto,
VMHooksEndpointArgument, VMHooksEndpointFinish, VMHooksError, VMHooksErrorManaged,
VMHooksHandler, VMHooksHandlerSource, VMHooksLog, VMHooksManagedBuffer, VMHooksManagedMap,
VMHooksManagedTypes, VMHooksSend, VMHooksStorageRead, VMHooksStorageWrite,
host::{
context::{BackTransfers, ManagedTypeContainer, TxFunctionName, TxInput, TxResult},
vm_hooks::{
VMHooksBigFloat, VMHooksBigInt, VMHooksBlockchain, VMHooksCallValue, VMHooksCrypto,
VMHooksEndpointArgument, VMHooksEndpointFinish, VMHooksError, VMHooksErrorManaged,
VMHooksHandler, VMHooksHandlerSource, VMHooksLog, VMHooksManagedBuffer,
VMHooksManagedMap, VMHooksManagedTypes, VMHooksSend, VMHooksStorageRead,
VMHooksStorageWrite,
},
},
schedule::GasSchedule,
types::{VMAddress, VMCodeMetadata},
};

use crate::executor::debug::ContractDebugInstanceState;

const ZERO_GAS_SCHEDULE: GasSchedule = GasSchedule::zeroed();

#[derive(Default, Debug)]
pub struct SingleTxApiData {
pub tx_input_box: Box<TxInput>,
Expand Down Expand Up @@ -77,6 +83,12 @@ impl VMHooksHandlerSource for SingleTxApiVMHooksHandler {
panic!("VM error occured, status: {status}, message: {message}")
}

fn gas_schedule(&self) -> &GasSchedule {
&ZERO_GAS_SCHEDULE
}

fn use_gas(&mut self, _gas: u64) {}

fn input_ref(&self) -> &TxInput {
&self.0.tx_input_box
}
Expand Down
26 changes: 18 additions & 8 deletions framework/scenario/src/api/impl_vh/vh_static_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ use multiversx_chain_vm_executor::{MemLength, MemPtr};
use multiversx_chain_vm::{
blockchain::state::{AccountData, BlockInfo},
chain_core::types::ReturnCode,
host::context::{
BackTransfers, ManagedTypeContainer, TxFunctionName, TxInput, TxLog, TxResult,
},
host::vm_hooks::{
VMHooksBigFloat, VMHooksBigInt, VMHooksBlockchain, VMHooksCallValue, VMHooksCrypto,
VMHooksEndpointArgument, VMHooksEndpointFinish, VMHooksError, VMHooksErrorManaged,
VMHooksHandler, VMHooksHandlerSource, VMHooksLog, VMHooksManagedBuffer, VMHooksManagedMap,
VMHooksManagedTypes, VMHooksSend, VMHooksStorageRead, VMHooksStorageWrite,
host::{
context::{BackTransfers, ManagedTypeContainer, TxFunctionName, TxInput, TxLog, TxResult},
vm_hooks::{
VMHooksBigFloat, VMHooksBigInt, VMHooksBlockchain, VMHooksCallValue, VMHooksCrypto,
VMHooksEndpointArgument, VMHooksEndpointFinish, VMHooksError, VMHooksErrorManaged,
VMHooksHandler, VMHooksHandlerSource, VMHooksLog, VMHooksManagedBuffer,
VMHooksManagedMap, VMHooksManagedTypes, VMHooksSend, VMHooksStorageRead,
VMHooksStorageWrite,
},
},
schedule::GasSchedule,
types::{VMAddress, VMCodeMetadata},
};

use crate::executor::debug::ContractDebugInstanceState;

const ZERO_GAS_SCHEDULE: GasSchedule = GasSchedule::zeroed();

/// A simple wrapper around a managed type container Mutex.
///
/// Implements `VMHooksManagedTypes` and thus can be used as a basis of a minimal static API.
Expand Down Expand Up @@ -50,6 +54,12 @@ impl VMHooksHandlerSource for StaticApiVMHooksHandler {
panic!("VM error occured, status: {status}, message: {message}")
}

fn gas_schedule(&self) -> &GasSchedule {
&ZERO_GAS_SCHEDULE
}

fn use_gas(&mut self, _gas: u64) {}

fn input_ref(&self) -> &TxInput {
panic!("cannot access tx inputs in the StaticApi")
}
Expand Down
Loading