Skip to content

Commit 6cc8f7f

Browse files
committed
tests: fix CheckErrors::NoSuchContract error in tests
1 parent a475f76 commit 6cc8f7f

File tree

6 files changed

+119
-16
lines changed

6 files changed

+119
-16
lines changed

Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clarity/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ rstest_reuse = { version = "0.5.0", optional = true }
3535
wasmtime = { version = "15.0.0", optional = true }
3636
hashbrown = { workspace = true, features = ["serde"] }
3737
getrandom = { version = "0.2", features = ["js"], optional = true}
38+
ouroboros = "0.18.5"
3839

3940
[dependencies.serde_json]
4041
version = "1.0"
@@ -45,7 +46,6 @@ workspace = true
4546
features = ["blob", "serde_json", "i128_blob", "bundled", "trace"]
4647
optional = true
4748

48-
4949
[dev-dependencies]
5050
assert-json-diff = "1.0.0"
5151
rstest = { version = "0.17.0" }

clarity/src/vm/contexts.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ use crate::vm::errors::{
4545
};
4646
use crate::vm::events::*;
4747
use crate::vm::representations::{ClarityName, SymbolicExpression};
48+
#[cfg(all(feature = "clarity-wasm", feature = "testing"))]
49+
use crate::vm::tests::clarity_wasm::MemoryAnalysisDatabase;
4850
use crate::vm::types::signatures::FunctionSignature;
4951
use crate::vm::types::{
5052
AssetIdentifier, BuffData, CallableData, PrincipalData, QualifiedContractIdentifier,
@@ -219,6 +221,8 @@ pub struct GlobalContext<'a> {
219221
pub execution_time_tracker: ExecutionTimeTracker,
220222
#[cfg(feature = "clarity-wasm")]
221223
pub engine: Engine,
224+
#[cfg(all(feature = "clarity-wasm", feature = "testing"))]
225+
pub analysis_db: MemoryAnalysisDatabase,
222226
}
223227

224228
#[derive(Serialize, Deserialize, Clone)]
@@ -1341,8 +1345,6 @@ impl<'a, 'b> Environment<'a, 'b> {
13411345
contract_content: &str,
13421346
ast_rules: ASTRules,
13431347
) -> Result<()> {
1344-
use super::database::MemoryBackingStore;
1345-
13461348
let clarity_version = self.contract_context.clarity_version;
13471349

13481350
let mut contract_ast = ast::build_ast_with_rules(
@@ -1354,18 +1356,22 @@ impl<'a, 'b> Environment<'a, 'b> {
13541356
ast_rules,
13551357
)?;
13561358

1357-
let mut store = MemoryBackingStore::new();
1358-
let contract_analysis = analysis::run_analysis(
1359-
&contract_identifier,
1360-
&contract_ast.expressions,
1361-
&mut store.as_analysis_db(),
1362-
false,
1363-
LimitedCostTracker::Free,
1364-
self.global_context.epoch_id,
1365-
clarity_version,
1366-
true,
1367-
)
1368-
.map_err(|(check_error, _)| check_error.err)?;
1359+
let contract_analysis =
1360+
self.global_context
1361+
.analysis_db
1362+
.with_analysis_db_mut(|analysis_db| {
1363+
analysis::run_analysis(
1364+
&contract_identifier,
1365+
&contract_ast.expressions,
1366+
analysis_db,
1367+
true,
1368+
LimitedCostTracker::Free,
1369+
self.global_context.epoch_id,
1370+
clarity_version,
1371+
true,
1372+
)
1373+
.map_err(|(check_error, _)| check_error.err)
1374+
})?;
13691375

13701376
self.initialize_contract_from_ast(
13711377
contract_identifier,
@@ -1683,6 +1689,8 @@ impl<'a> GlobalContext<'a> {
16831689
execution_time_tracker: ExecutionTimeTracker::NoTracking,
16841690
#[cfg(feature = "clarity-wasm")]
16851691
engine,
1692+
#[cfg(all(feature = "clarity-wasm", feature = "testing"))]
1693+
analysis_db: MemoryAnalysisDatabase::build(),
16861694
}
16871695
}
16881696

@@ -1804,6 +1812,7 @@ impl<'a> GlobalContext<'a> {
18041812
self.database.begin();
18051813
let read_only = self.is_read_only();
18061814
self.read_only.push(read_only);
1815+
self.analysis_db.begin();
18071816
}
18081817

18091818
pub fn begin_read_only(&mut self) {

clarity/src/vm/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl fmt::Display for WasmError {
193193
#[cfg(feature = "clarity-wasm")]
194194
impl std::error::Error for WasmError {}
195195

196-
pub type InterpreterResult<R> = Result<R, Error>;
196+
pub type InterpreterResult<R, E = Error> = Result<R, E>;
197197

198198
impl<T> PartialEq<IncomparableError<T>> for IncomparableError<T> {
199199
fn eq(&self, _other: &IncomparableError<T>) -> bool {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::collections::{BTreeMap, BTreeSet};
2+
3+
use ouroboros::self_referencing;
4+
use stacks_common::types::StacksEpochId;
5+
6+
use crate::vm::analysis::{AnalysisDatabase, CheckResult, ContractAnalysis};
7+
use crate::vm::database::MemoryBackingStore;
8+
use crate::vm::errors::CheckErrors;
9+
use crate::vm::types::{
10+
FunctionSignature, FunctionType, QualifiedContractIdentifier, TraitIdentifier,
11+
};
12+
use crate::vm::{ClarityName, ClarityVersion};
13+
14+
#[self_referencing]
15+
pub struct MemoryAnalysisDatabase {
16+
mem_store: MemoryBackingStore,
17+
#[not_covariant]
18+
#[borrows(mut mem_store)]
19+
pub analysis_db: AnalysisDatabase<'this>,
20+
}
21+
22+
impl MemoryAnalysisDatabase {
23+
pub fn build() -> Self {
24+
let mem_store = MemoryBackingStore::new();
25+
26+
MemoryAnalysisDatabaseBuilder {
27+
mem_store,
28+
analysis_db_builder: |mem_store| AnalysisDatabase::new(mem_store),
29+
}
30+
.build()
31+
}
32+
33+
pub fn begin(&mut self) {
34+
self.with_analysis_db_mut(|db| db.begin())
35+
}
36+
}

clarity/src/vm/tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ mod sequences;
3737
mod simple_apply_eval;
3838
mod traits;
3939
mod variables;
40+
// Module to wrap code related to clarity-wasm tests
41+
pub mod clarity_wasm;
4042

4143
#[cfg(any(test, feature = "testing"))]
4244
impl<'a> OwnedEnvironment<'a> {

0 commit comments

Comments
 (0)