@@ -31,11 +31,13 @@ use super::analysis::{self, ContractAnalysis};
31
31
#[ cfg( feature = "clarity-wasm" ) ]
32
32
use super :: clarity_wasm:: call_function;
33
33
use super :: EvalHook ;
34
+ use crate :: vm:: analysis:: AnalysisDatabase ;
34
35
use crate :: vm:: ast:: { ASTRules , ContractAST } ;
35
36
use crate :: vm:: callables:: { DefinedFunction , FunctionIdentifier } ;
36
37
use crate :: vm:: contracts:: Contract ;
37
38
use crate :: vm:: costs:: cost_functions:: ClarityCostFunction ;
38
39
use crate :: vm:: costs:: { runtime_cost, CostErrors , CostTracker , ExecutionCost , LimitedCostTracker } ;
40
+ use crate :: vm:: database:: MemoryBackingStore ;
39
41
use crate :: vm:: database:: {
40
42
ClarityDatabase , DataMapMetadata , DataVariableMetadata , FungibleTokenMetadata ,
41
43
NonFungibleTokenMetadata ,
@@ -658,13 +660,51 @@ impl<'a> OwnedEnvironment<'a> {
658
660
contract_content : & str ,
659
661
sponsor : Option < PrincipalData > ,
660
662
ast_rules : ASTRules ,
663
+ ) -> Result < ( ( ) , AssetMap , Vec < StacksTransactionEvent > ) > {
664
+ let mut store = MemoryBackingStore :: new ( ) ;
665
+ self . initialize_contract_with_db (
666
+ contract_identifier,
667
+ contract_content,
668
+ sponsor,
669
+ ast_rules,
670
+ & mut store. as_analysis_db ( ) ,
671
+ )
672
+ }
673
+
674
+ /// Initializes a Clarity smart contract with a custom analysis database within a transaction context.
675
+ ///
676
+ /// This function creates a complete transaction environment to initialize a Clarity smart contract
677
+ /// using a provided memory-backed database for analysis data. It executes the contract initialization
678
+ /// within a proper execution context.
679
+ ///
680
+ /// # Arguments
681
+ ///
682
+ /// * `contract_identifier` - Unique identifier for the contract (principal + contract name)
683
+ /// * `contract_content` - The raw Clarity source code as a string
684
+ /// * `sponsor` - Optional sponsor principal for transaction fees (if `None`, sender pays)
685
+ /// * `ast_rules` - Parsing rules to apply during AST construction (e.g., `ASTRules::PrecheckSize`)
686
+ /// * `analysis_db` - Mutable reference to a database for analysis data
687
+ ///
688
+ #[ cfg( any( test, feature = "testing" ) ) ]
689
+ pub fn initialize_contract_with_db (
690
+ & mut self ,
691
+ contract_identifier : QualifiedContractIdentifier ,
692
+ contract_content : & str ,
693
+ sponsor : Option < PrincipalData > ,
694
+ ast_rules : ASTRules ,
695
+ analysis_db : & mut AnalysisDatabase ,
661
696
) -> Result < ( ( ) , AssetMap , Vec < StacksTransactionEvent > ) > {
662
697
self . execute_in_env (
663
698
contract_identifier. issuer . clone ( ) . into ( ) ,
664
699
sponsor,
665
700
None ,
666
701
|exec_env| {
667
- exec_env. initialize_contract ( contract_identifier, contract_content, ast_rules)
702
+ exec_env. initialize_contract_with_db (
703
+ contract_identifier,
704
+ contract_content,
705
+ ast_rules,
706
+ analysis_db,
707
+ )
668
708
} ,
669
709
)
670
710
}
@@ -1341,8 +1381,46 @@ impl<'a, 'b> Environment<'a, 'b> {
1341
1381
contract_content : & str ,
1342
1382
ast_rules : ASTRules ,
1343
1383
) -> Result < ( ) > {
1344
- use super :: database:: MemoryBackingStore ;
1384
+ let mut store = MemoryBackingStore :: new ( ) ;
1385
+ let mut analysis_db = store. as_analysis_db ( ) ;
1386
+ analysis_db. begin ( ) ;
1387
+
1388
+ self . initialize_contract_with_db (
1389
+ contract_identifier,
1390
+ contract_content,
1391
+ ast_rules,
1392
+ & mut analysis_db,
1393
+ )
1394
+ }
1345
1395
1396
+ /// Initializes a Clarity smart contract with a custom analysis database.
1397
+ ///
1398
+ /// This function should only be used for testing.
1399
+ ///
1400
+ /// This function parses and analyzes a Clarity smart contract using
1401
+ /// a provided database for analysis data. It's primarily used for testing
1402
+ /// scenarios where you need control over the backing storage.
1403
+ ///
1404
+ /// # Arguments
1405
+ ///
1406
+ /// * `contract_identifier` - Unique identifier for the contract (principal + name)
1407
+ /// * `contract_content` - The raw Clarity source code as a string
1408
+ /// * `ast_rules` - Parsing rules to apply during AST construction
1409
+ /// * `analysis_db` - Mutable reference to a database for analysis data
1410
+ ///
1411
+ /// # Returns
1412
+ ///
1413
+ /// * `Ok(())` - Contract successfully initialized
1414
+ /// * `Err(Error)` - Initialization failed due to parsing or analysis
1415
+ ///
1416
+ #[ cfg( feature = "rusqlite" ) ]
1417
+ pub fn initialize_contract_with_db (
1418
+ & mut self ,
1419
+ contract_identifier : QualifiedContractIdentifier ,
1420
+ contract_content : & str ,
1421
+ ast_rules : ASTRules ,
1422
+ analysis_db : & mut AnalysisDatabase ,
1423
+ ) -> Result < ( ) > {
1346
1424
let clarity_version = self . contract_context . clarity_version ;
1347
1425
1348
1426
let mut contract_ast = ast:: build_ast_with_rules (
@@ -1354,12 +1432,11 @@ impl<'a, 'b> Environment<'a, 'b> {
1354
1432
ast_rules,
1355
1433
) ?;
1356
1434
1357
- let mut store = MemoryBackingStore :: new ( ) ;
1358
1435
let contract_analysis = analysis:: run_analysis (
1359
1436
& contract_identifier,
1360
1437
& contract_ast. expressions ,
1361
- & mut store . as_analysis_db ( ) ,
1362
- false ,
1438
+ analysis_db ,
1439
+ true ,
1363
1440
LimitedCostTracker :: Free ,
1364
1441
self . global_context . epoch_id ,
1365
1442
clarity_version,
0 commit comments