16
16
17
17
use std:: collections:: HashMap ;
18
18
19
+ use clarity:: vm:: analysis:: AnalysisDatabase ;
19
20
use clarity:: vm:: ast:: ASTRules ;
20
21
use clarity:: vm:: clarity:: TransactionConnection ;
21
22
use clarity:: vm:: contexts:: {
@@ -836,10 +837,17 @@ fn epoch205_nfts_testnet() {
836
837
epoch205_nfts ( false )
837
838
}
838
839
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 (
840
847
use_mainnet : bool ,
841
848
version : ClarityVersion ,
842
849
owned_env : & mut OwnedEnvironment ,
850
+ analysis_db : & mut AnalysisDatabase ,
843
851
) {
844
852
let contract_trait = "(define-trait trait-1 (
845
853
(foo-exec (int) (response int int))
@@ -864,21 +872,23 @@ fn setup_cost_tracked_test(
864
872
let trait_contract_id = QualifiedContractIdentifier :: new ( p1_principal, "contract-trait" . into ( ) ) ;
865
873
866
874
owned_env
867
- . initialize_versioned_contract (
875
+ . initialize_versioned_contract_with_db (
868
876
trait_contract_id,
869
877
version,
870
878
contract_trait,
871
879
None ,
872
880
ASTRules :: PrecheckSize ,
881
+ analysis_db,
873
882
)
874
883
. unwrap ( ) ;
875
884
owned_env
876
- . initialize_versioned_contract (
885
+ . initialize_versioned_contract_with_db (
877
886
other_contract_id,
878
887
version,
879
888
contract_other,
880
889
None ,
881
890
ASTRules :: PrecheckSize ,
891
+ analysis_db,
882
892
)
883
893
. unwrap ( ) ;
884
894
}
@@ -969,11 +979,18 @@ fn proptest_replacements_costs_3() {
969
979
proptest_cost_contract ( COSTS_3_NAME ) ;
970
980
}
971
981
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 (
973
989
prog : & str ,
974
990
version : ClarityVersion ,
975
991
owned_env : & mut OwnedEnvironment ,
976
992
prog_id : usize ,
993
+ analysis_db : & mut AnalysisDatabase ,
977
994
) -> ExecutionCost {
978
995
let contract_self = format ! (
979
996
"(define-map map-foo {{ a: int }} {{ b: int }})
@@ -1007,12 +1024,13 @@ fn test_program_cost(
1007
1024
let other_contract_id = QualifiedContractIdentifier :: new ( p1_principal, "contract-other" . into ( ) ) ;
1008
1025
1009
1026
owned_env
1010
- . initialize_versioned_contract (
1027
+ . initialize_versioned_contract_with_db (
1011
1028
self_contract_id. clone ( ) ,
1012
1029
version,
1013
1030
& contract_self,
1014
1031
None ,
1015
1032
ASTRules :: PrecheckSize ,
1033
+ analysis_db,
1016
1034
)
1017
1035
. unwrap ( ) ;
1018
1036
@@ -1038,16 +1056,36 @@ fn test_program_cost(
1038
1056
// Clarity code executes in Epoch 2.00
1039
1057
fn epoch_20_205_test_all ( use_mainnet : bool , epoch : StacksEpochId ) {
1040
1058
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 ( ) ;
1042
1062
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
+ ) ;
1044
1077
1045
1078
for ( ix, f) in NativeFunctions :: ALL . iter ( ) . enumerate ( ) {
1046
1079
// Note: The 2.0 and 2.05 test assumes Clarity1.
1047
1080
if f. get_min_version ( ) == ClarityVersion :: Clarity1 {
1048
1081
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
+ ) ;
1051
1089
assert ! ( cost. exceeds( & baseline) ) ;
1052
1090
}
1053
1091
}
@@ -1078,14 +1116,35 @@ fn epoch_205_test_all_testnet() {
1078
1116
// Clarity code executes in Epoch 2.1
1079
1117
fn epoch_21_test_all ( use_mainnet : bool ) {
1080
1118
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 ( ) ;
1082
1122
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
+ ) ;
1084
1137
1085
1138
for ( ix, f) in NativeFunctions :: ALL . iter ( ) . enumerate ( ) {
1086
1139
// Note: Include Clarity2 functions for Epoch21.
1087
1140
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
+ ) ;
1089
1148
assert ! ( cost. exceeds( & baseline) ) ;
1090
1149
}
1091
1150
} )
0 commit comments