Skip to content

Commit 348c9cf

Browse files
committed
chore: cost proptest unit tests from @kantai
1 parent a072fe9 commit 348c9cf

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

clarity/src/vm/contexts.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,11 @@ impl<'a, 'hooks> OwnedEnvironment<'a, 'hooks> {
799799
self.context.cost_track.get_total()
800800
}
801801

802+
#[cfg(any(test, feature = "testing"))]
803+
pub fn mut_cost_tracker(&mut self) -> &mut LimitedCostTracker {
804+
&mut self.context.cost_track
805+
}
806+
802807
/// Destroys this environment, returning ownership of its database reference.
803808
/// If the context wasn't top-level (i.e., it had uncommitted data), return None,
804809
/// because the database is not guaranteed to be in a sane state.

clarity/src/vm/costs/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ pub struct TrackerData {
331331
/// if the cost tracker is non-free, this holds the StacksEpochId that should be used to evaluate
332332
/// the Clarity cost functions. If the tracker *is* free, then those functions do not need to be
333333
/// evaluated, so no epoch identifier is necessary.
334-
epoch: StacksEpochId,
334+
pub epoch: StacksEpochId,
335335
mainnet: bool,
336336
chain_id: u32,
337337
}
@@ -1053,7 +1053,7 @@ pub fn parse_cost(
10531053

10541054
// TODO: add tests from mutation testing results #4832
10551055
#[cfg_attr(test, mutants::skip)]
1056-
fn compute_cost(
1056+
pub fn compute_cost(
10571057
cost_tracker: &TrackerData,
10581058
cost_function_reference: ClarityCostFunctionReference,
10591059
input_sizes: &[u64],

stackslib/src/clarity_vm/tests/costs.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ use clarity::vm::contexts::{
2424
use clarity::vm::contracts::Contract;
2525
use clarity::vm::costs::cost_functions::ClarityCostFunction;
2626
use clarity::vm::costs::{
27-
parse_cost, ClarityCostFunctionEvaluator, ClarityCostFunctionReference, CostErrors,
28-
DefaultVersion, ExecutionCost, LimitedCostTracker, COSTS_1_NAME, COSTS_2_NAME, COSTS_3_NAME,
27+
compute_cost, parse_cost, ClarityCostFunctionEvaluator, ClarityCostFunctionReference,
28+
CostErrors, DefaultVersion, ExecutionCost, LimitedCostTracker, COSTS_1_NAME, COSTS_2_NAME,
29+
COSTS_3_NAME,
2930
};
3031
use clarity::vm::database::{ClarityDatabase, MemoryBackingStore};
3132
use clarity::vm::errors::{CheckErrors, Error, RuntimeErrorType};
@@ -885,19 +886,16 @@ fn eval_cost_fn(
885886
let mainnet = owned_env.is_mainnet();
886887
let boot_costs_id = boot_code_id(cost_contract_name, mainnet);
887888
let cost_fn_name = cost_fn.get_name_str();
888-
889-
let exec = format!("({cost_fn_name} u{argument})");
890-
891-
let exec_result = owned_env
892-
.eval_read_only(&boot_costs_id, &exec)
893-
.map(|(value, _, _)| Some(value));
894-
889+
let cost_tracker = owned_env.mut_cost_tracker();
890+
let data = match cost_tracker {
891+
LimitedCostTracker::Free => panic!(),
892+
LimitedCostTracker::Limited(data) => data,
893+
};
895894
let clarity_cost_fn_ref = ClarityCostFunctionReference {
896895
contract_id: boot_costs_id,
897896
function_name: cost_fn_name.to_string(),
898897
};
899-
900-
parse_cost(&clarity_cost_fn_ref.to_string(), exec_result)
898+
compute_cost(data, clarity_cost_fn_ref, &[argument], data.epoch)
901899
}
902900

903901
fn eval_replaced_cost_fn(
@@ -926,7 +924,13 @@ fn proptest_cost_fn(cost_fn: &ClarityCostFunction, cost_contract_name: &str) {
926924
inputs.push(2u64.pow(i) + 1);
927925
});
928926
for use_mainnet in [true, false] {
929-
with_owned_env(StacksEpochId::latest(), use_mainnet, |mut owned_env| {
927+
let epoch = match cost_contract_name {
928+
COSTS_1_NAME => StacksEpochId::Epoch20,
929+
COSTS_2_NAME => StacksEpochId::Epoch2_05,
930+
COSTS_3_NAME => StacksEpochId::latest(),
931+
_ => panic!(),
932+
};
933+
with_owned_env(epoch, use_mainnet, |mut owned_env| {
930934
for i in inputs.iter() {
931935
eprintln!("Evaluating {cost_contract_name}.{cost_fn}({i})");
932936
let clar_evaled = eval_cost_fn(&mut owned_env, cost_contract_name, cost_fn, *i);

0 commit comments

Comments
 (0)