Skip to content

Commit c0fd126

Browse files
committed
tests: fix clarity_vm::tests::costs issues
1 parent 91969e3 commit c0fd126

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

stackslib/src/clarity_vm/tests/costs.rs

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use std::collections::HashMap;
1818

19+
use clarity::vm::analysis::AnalysisDatabase;
1920
use clarity::vm::ast::ASTRules;
2021
use clarity::vm::clarity::TransactionConnection;
2122
use clarity::vm::contexts::{
@@ -836,10 +837,17 @@ fn epoch205_nfts_testnet() {
836837
epoch205_nfts(false)
837838
}
838839

839-
fn setup_cost_tracked_test(
840+
/// Sets up a cost-tracked test environment with a custom analysis database.
841+
///
842+
/// This function initializes a Clarity execution environment configured for cost tracking
843+
/// during testing scenarios. It provides control over network configuration (mainnet vs testnet),
844+
/// Clarity language version, and uses a custom analysis database for contract metadata storage.
845+
///
846+
fn setup_cost_tracked_test_with_db(
840847
use_mainnet: bool,
841848
version: ClarityVersion,
842849
owned_env: &mut OwnedEnvironment,
850+
analysis_db: &mut AnalysisDatabase,
843851
) {
844852
let contract_trait = "(define-trait trait-1 (
845853
(foo-exec (int) (response int int))
@@ -864,21 +872,23 @@ fn setup_cost_tracked_test(
864872
let trait_contract_id = QualifiedContractIdentifier::new(p1_principal, "contract-trait".into());
865873

866874
owned_env
867-
.initialize_versioned_contract(
875+
.initialize_versioned_contract_with_db(
868876
trait_contract_id,
869877
version,
870878
contract_trait,
871879
None,
872880
ASTRules::PrecheckSize,
881+
analysis_db,
873882
)
874883
.unwrap();
875884
owned_env
876-
.initialize_versioned_contract(
885+
.initialize_versioned_contract_with_db(
877886
other_contract_id,
878887
version,
879888
contract_other,
880889
None,
881890
ASTRules::PrecheckSize,
891+
analysis_db,
882892
)
883893
.unwrap();
884894
}
@@ -969,11 +979,18 @@ fn proptest_replacements_costs_3() {
969979
proptest_cost_contract(COSTS_3_NAME);
970980
}
971981

972-
fn test_program_cost(
982+
/// Tests and measures the execution cost of a Clarity program with a custom analysis database.
983+
///
984+
/// This function executes a Clarity program in a controlled test environment and returns
985+
/// the execution cost. It uses a custom analysis database to store and retrieve contract metadata
986+
/// during execution.
987+
///
988+
fn test_program_cost_with_db(
973989
prog: &str,
974990
version: ClarityVersion,
975991
owned_env: &mut OwnedEnvironment,
976992
prog_id: usize,
993+
analysis_db: &mut AnalysisDatabase,
977994
) -> ExecutionCost {
978995
let contract_self = format!(
979996
"(define-map map-foo {{ a: int }} {{ b: int }})
@@ -1007,12 +1024,13 @@ fn test_program_cost(
10071024
let other_contract_id = QualifiedContractIdentifier::new(p1_principal, "contract-other".into());
10081025

10091026
owned_env
1010-
.initialize_versioned_contract(
1027+
.initialize_versioned_contract_with_db(
10111028
self_contract_id.clone(),
10121029
version,
10131030
&contract_self,
10141031
None,
10151032
ASTRules::PrecheckSize,
1033+
analysis_db,
10161034
)
10171035
.unwrap();
10181036

@@ -1038,16 +1056,36 @@ fn test_program_cost(
10381056
// Clarity code executes in Epoch 2.00
10391057
fn epoch_20_205_test_all(use_mainnet: bool, epoch: StacksEpochId) {
10401058
with_owned_env(epoch, use_mainnet, |mut owned_env| {
1041-
setup_cost_tracked_test(use_mainnet, ClarityVersion::Clarity1, &mut owned_env);
1059+
let mut store = MemoryBackingStore::new();
1060+
let mut analysis_db = store.as_analysis_db();
1061+
analysis_db.begin();
10421062

1043-
let baseline = test_program_cost("1", ClarityVersion::Clarity1, &mut owned_env, 0);
1063+
setup_cost_tracked_test_with_db(
1064+
use_mainnet,
1065+
ClarityVersion::Clarity1,
1066+
&mut owned_env,
1067+
&mut analysis_db,
1068+
);
1069+
1070+
let baseline = test_program_cost_with_db(
1071+
"1",
1072+
ClarityVersion::Clarity1,
1073+
&mut owned_env,
1074+
0,
1075+
&mut analysis_db,
1076+
);
10441077

10451078
for (ix, f) in NativeFunctions::ALL.iter().enumerate() {
10461079
// Note: The 2.0 and 2.05 test assumes Clarity1.
10471080
if f.get_min_version() == ClarityVersion::Clarity1 {
10481081
let test = get_simple_test(f);
1049-
let cost =
1050-
test_program_cost(test, ClarityVersion::Clarity1, &mut owned_env, ix + 1);
1082+
let cost = test_program_cost_with_db(
1083+
test,
1084+
ClarityVersion::Clarity1,
1085+
&mut owned_env,
1086+
ix + 1,
1087+
&mut analysis_db,
1088+
);
10511089
assert!(cost.exceeds(&baseline));
10521090
}
10531091
}
@@ -1078,14 +1116,35 @@ fn epoch_205_test_all_testnet() {
10781116
// Clarity code executes in Epoch 2.1
10791117
fn epoch_21_test_all(use_mainnet: bool) {
10801118
with_owned_env(StacksEpochId::Epoch21, use_mainnet, |mut owned_env| {
1081-
setup_cost_tracked_test(use_mainnet, ClarityVersion::Clarity2, &mut owned_env);
1119+
let mut store = MemoryBackingStore::new();
1120+
let mut analysis_db = store.as_analysis_db();
1121+
analysis_db.begin();
10821122

1083-
let baseline = test_program_cost("1", ClarityVersion::Clarity2, &mut owned_env, 0);
1123+
setup_cost_tracked_test_with_db(
1124+
use_mainnet,
1125+
ClarityVersion::Clarity2,
1126+
&mut owned_env,
1127+
&mut analysis_db,
1128+
);
1129+
1130+
let baseline = test_program_cost_with_db(
1131+
"1",
1132+
ClarityVersion::Clarity2,
1133+
&mut owned_env,
1134+
0,
1135+
&mut analysis_db,
1136+
);
10841137

10851138
for (ix, f) in NativeFunctions::ALL.iter().enumerate() {
10861139
// Note: Include Clarity2 functions for Epoch21.
10871140
let test = get_simple_test(f);
1088-
let cost = test_program_cost(test, ClarityVersion::Clarity2, &mut owned_env, ix + 1);
1141+
let cost = test_program_cost_with_db(
1142+
test,
1143+
ClarityVersion::Clarity2,
1144+
&mut owned_env,
1145+
ix + 1,
1146+
&mut analysis_db,
1147+
);
10891148
assert!(cost.exceeds(&baseline));
10901149
}
10911150
})

0 commit comments

Comments
 (0)