Skip to content

Commit

Permalink
Fix calibration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Jun 26, 2023
1 parent 422ce41 commit 2004130
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
10 changes: 7 additions & 3 deletions soroban-env-host/benches/common/cost_types/guard_frame.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::common::HostCostMeasurement;
use soroban_env_host::{cost_runner::GuardFrameRun, xdr::Hash, Symbol};
use soroban_env_host::{
cost_runner::GuardFrameRun,
xdr::{ContractExecutable, Hash, ScContractInstance},
Symbol,
};

pub(crate) struct GuardFrameMeasure;

Expand All @@ -9,7 +13,7 @@ impl HostCostMeasurement for GuardFrameMeasure {
fn new_best_case(
_host: &soroban_env_host::Host,
_rng: &mut rand::prelude::StdRng,
) -> (Hash, Symbol) {
) -> (Hash, Symbol, ScContractInstance) {
let id: Hash = [0; 32].into();
let fun: Symbol = Symbol::try_from_small_str("add").unwrap();
let empty_instance = ScContractInstance {
Expand All @@ -23,7 +27,7 @@ impl HostCostMeasurement for GuardFrameMeasure {
host: &soroban_env_host::Host,
rng: &mut rand::prelude::StdRng,
_input: u64,
) -> (Hash, Symbol) {
) -> (Hash, Symbol, ScContractInstance) {
Self::new_best_case(host, rng)
}
}
26 changes: 9 additions & 17 deletions soroban-env-host/benches/common/cost_types/num_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rand::{rngs::StdRng, RngCore};
use soroban_env_common::{Env, EnvBase};
use soroban_env_host::{
cost_runner::{Int256AddSubRun, Int256DivRun, Int256MulRun, Int256PowRun, Int256ShiftRun},
Host, I256Object, U32Val, I256,
Host, I256Val, U32Val, I256,
};

// These are best guesses.
Expand Down Expand Up @@ -33,11 +33,7 @@ macro_rules! impl_int256_measure {
impl HostCostMeasurement for $measure {
type Runner = $runner;

fn new_worst_case(
host: &Host,
_rng: &mut StdRng,
_input: u64,
) -> (I256Object, I256Object) {
fn new_worst_case(host: &Host, _rng: &mut StdRng, _input: u64) -> (I256Val, I256Val) {
let (lhs, rhs) = $worst();
let bytes = host
.bytes_new_from_slice(lhs.to_be_bytes().as_slice())
Expand All @@ -47,22 +43,18 @@ macro_rules! impl_int256_measure {
.bytes_new_from_slice(rhs.to_be_bytes().as_slice())
.unwrap();
let rhs_obj = host.i256_obj_from_be_bytes(bytes).unwrap();
(lhs_obj, rhs_obj)
(lhs_obj.into(), rhs_obj.into())
}

fn new_random_case(
host: &Host,
rng: &mut StdRng,
_input: u64,
) -> (I256Object, I256Object) {
fn new_random_case(host: &Host, rng: &mut StdRng, _input: u64) -> (I256Val, I256Val) {
let mut bytes = [0; 32];
rng.fill_bytes(bytes.as_mut_slice());
let bo = host.bytes_new_from_slice(bytes.as_slice()).unwrap();
let lhs_obj = host.i256_obj_from_be_bytes(bo).unwrap();
rng.fill_bytes(bytes.as_mut_slice());
let bo = host.bytes_new_from_slice(bytes.as_slice()).unwrap();
let rhs_obj = host.i256_obj_from_be_bytes(bo).unwrap();
(lhs_obj, rhs_obj)
(lhs_obj.into(), rhs_obj.into())
}
}
};
Expand All @@ -78,21 +70,21 @@ macro_rules! impl_int256_measure_rhs_u32 {
impl HostCostMeasurement for $measure {
type Runner = $runner;

fn new_worst_case(host: &Host, _rng: &mut StdRng, _input: u64) -> (I256Object, U32Val) {
fn new_worst_case(host: &Host, _rng: &mut StdRng, _input: u64) -> (I256Val, U32Val) {
let (lhs, rhs) = $worst();
let bytes = host
.bytes_new_from_slice(lhs.to_be_bytes().as_slice())
.unwrap();
let lhs_obj = host.i256_obj_from_be_bytes(bytes).unwrap();
(lhs_obj, U32Val::from(rhs))
(lhs_obj.into(), U32Val::from(rhs))
}

fn new_random_case(host: &Host, rng: &mut StdRng, _input: u64) -> (I256Object, U32Val) {
fn new_random_case(host: &Host, rng: &mut StdRng, _input: u64) -> (I256Val, U32Val) {
let mut bytes = [0; 32];
rng.fill_bytes(bytes.as_mut_slice());
let bo = host.bytes_new_from_slice(bytes.as_slice()).unwrap();
let lhs_obj = host.i256_obj_from_be_bytes(bo).unwrap();
(lhs_obj, U32Val::from(rng.next_u32()))
(lhs_obj.into(), U32Val::from(rng.next_u32()))
}
}
};
Expand Down
1 change: 0 additions & 1 deletion soroban-env-host/src/host/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ impl Host {
id.metered_clone(&self.0.budget)?,
code_entry.as_slice(),
)?;
self.charge_budget(ContractCostType::InvokeVmFunction, None)?;
self.with_frame(
Frame::ContractVM(vm.clone(), *func, args.to_vec(), contract_instance),
|| vm.invoke_function_raw(self, func, args),
Expand Down
1 change: 1 addition & 0 deletions soroban-env-host/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl Vm {
func_sym: &Symbol,
args: &[Val],
) -> Result<Val, HostError> {
host.charge_budget(ContractCostType::InvokeVmFunction, None)?;
let wasm_args: Vec<Value> = args
.iter()
.map(|i| Value::I64(i.get_payload() as i64))
Expand Down

0 comments on commit 2004130

Please sign in to comment.