Skip to content

Commit cf01028

Browse files
authored
Merge pull request #4791 from stacks-network/feat/sqlite-feature-flag
Make rusqlite optional
2 parents 578a24f + 951c479 commit cf01028

File tree

22 files changed

+503
-174
lines changed

22 files changed

+503
-174
lines changed

clarity/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ regex = "1"
2727
lazy_static = "1.4.0"
2828
integer-sqrt = "0.1.3"
2929
slog = { version = "2.5.2", features = [ "max_level_trace" ] }
30-
stacks_common = { package = "stacks-common", path = "../stacks-common" }
30+
stacks_common = { package = "stacks-common", path = "../stacks-common", optional = true, default-features = false }
3131
rstest = "0.17.0"
3232
rstest_reuse = "0.5.0"
3333
hashbrown = { workspace = true }
@@ -39,6 +39,7 @@ features = ["arbitrary_precision", "unbounded_depth"]
3939

4040
[dependencies.rusqlite]
4141
version = "=0.24.2"
42+
optional = true
4243
features = ["blob", "serde_json", "i128_blob", "bundled", "trace"]
4344

4445
[dependencies.time]
@@ -52,8 +53,9 @@ assert-json-diff = "1.0.0"
5253
# criterion = "0.3"
5354

5455
[features]
55-
default = []
56-
developer-mode = []
56+
default = ["canonical"]
57+
canonical = ["rusqlite", "stacks_common/canonical"]
58+
developer-mode = ["stacks_common/developer-mode"]
5759
slog_json = ["stacks_common/slog_json"]
58-
testing = []
60+
testing = ["canonical"]
5961
devtools = []

clarity/src/vm/analysis/arithmetic_checker/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ use stacks_common::types::StacksEpochId;
2222

2323
use crate::vm::analysis::arithmetic_checker::Error::*;
2424
use crate::vm::analysis::arithmetic_checker::{ArithmeticOnlyChecker, Error};
25-
use crate::vm::analysis::{mem_type_check, ContractAnalysis};
25+
use crate::vm::analysis::ContractAnalysis;
2626
use crate::vm::ast::parse;
2727
use crate::vm::costs::LimitedCostTracker;
2828
use crate::vm::functions::define::DefineFunctions;
2929
use crate::vm::functions::NativeFunctions;
3030
use crate::vm::tests::test_clarity_versions;
31+
use crate::vm::tooling::mem_type_check;
3132
use crate::vm::types::QualifiedContractIdentifier;
3233
use crate::vm::variables::NativeVariables;
3334
use crate::vm::ClarityVersion;

clarity/src/vm/analysis/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ use self::type_checker::v2_1::TypeChecker as TypeChecker2_1;
3737
pub use self::types::{AnalysisPass, ContractAnalysis};
3838
use crate::vm::ast::{build_ast_with_rules, ASTRules};
3939
use crate::vm::costs::LimitedCostTracker;
40-
use crate::vm::database::{MemoryBackingStore, STORE_CONTRACT_SRC_INTERFACE};
40+
#[cfg(feature = "canonical")]
41+
use crate::vm::database::MemoryBackingStore;
42+
use crate::vm::database::STORE_CONTRACT_SRC_INTERFACE;
4143
use crate::vm::representations::SymbolicExpression;
4244
use crate::vm::types::{QualifiedContractIdentifier, TypeSignature};
4345
use crate::vm::ClarityVersion;
4446

4547
/// Used by CLI tools like the docs generator. Not used in production
48+
#[cfg(feature = "canonical")]
4649
pub fn mem_type_check(
4750
snippet: &str,
4851
version: ClarityVersion,

clarity/src/vm/analysis/type_checker/v2_05/tests/assets.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
use stacks_common::types::StacksEpochId;
1818

1919
use crate::vm::analysis::errors::CheckErrors;
20-
use crate::vm::analysis::{mem_type_check, AnalysisDatabase};
20+
use crate::vm::analysis::AnalysisDatabase;
2121
use crate::vm::ast::parse;
2222
use crate::vm::database::MemoryBackingStore;
23+
use crate::vm::tooling::mem_type_check;
2324
use crate::vm::types::{
2425
QualifiedContractIdentifier, SequenceSubtype, StringSubtype, TypeSignature,
2526
};

clarity/src/vm/analysis/type_checker/v2_1/tests/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::vm::analysis::{mem_type_check as mem_run_analysis, AnalysisDatabase};
2929
use crate::vm::ast::errors::ParseErrors;
3030
use crate::vm::ast::{build_ast, parse};
3131
use crate::vm::contexts::OwnedEnvironment;
32-
use crate::vm::database::MemoryBackingStore;
3332
use crate::vm::representations::SymbolicExpression;
3433
use crate::vm::tests::test_clarity_versions;
3534
use crate::vm::types::signatures::TypeSignature::OptionalType;

clarity/src/vm/ast/definition_sorter/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::vm::ast::errors::{ParseErrors, ParseResult};
2525
use crate::vm::ast::expression_identifier::ExpressionIdentifier;
2626
use crate::vm::ast::parser;
2727
use crate::vm::ast::types::{BuildASTPass, ContractAST};
28-
use crate::vm::database::MemoryBackingStore;
2928
use crate::vm::types::QualifiedContractIdentifier;
3029
use crate::vm::ClarityVersion;
3130

clarity/src/vm/costs/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::{cmp, fmt};
1919

2020
use hashbrown::HashMap;
2121
use lazy_static::lazy_static;
22-
use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
2322
use serde::{Deserialize, Serialize};
2423
use stacks_common::types::StacksEpochId;
2524

@@ -1166,23 +1165,6 @@ impl fmt::Display for ExecutionCost {
11661165
}
11671166
}
11681167

1169-
impl ToSql for ExecutionCost {
1170-
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> {
1171-
let val = serde_json::to_string(self)
1172-
.map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;
1173-
Ok(ToSqlOutput::from(val))
1174-
}
1175-
}
1176-
1177-
impl FromSql for ExecutionCost {
1178-
fn column_result(value: ValueRef) -> FromSqlResult<ExecutionCost> {
1179-
let str_val = String::column_result(value)?;
1180-
let parsed = serde_json::from_str(&str_val)
1181-
.map_err(|e| rusqlite::types::FromSqlError::Other(Box::new(e)))?;
1182-
Ok(parsed)
1183-
}
1184-
}
1185-
11861168
pub trait CostOverflowingMath<T> {
11871169
fn cost_overflow_mul(self, other: T) -> Result<T>;
11881170
fn cost_overflow_add(self, other: T) -> Result<T>;

clarity/src/vm/database/clarity_store.rs

Lines changed: 36 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616

1717
use std::path::PathBuf;
1818

19+
#[cfg(feature = "canonical")]
1920
use rusqlite::Connection;
2021
use stacks_common::types::chainstate::{BlockHeaderHash, StacksBlockId, VRFSeed};
2122
use stacks_common::util::hash::{hex_bytes, to_hex, Hash160, Sha512Trunc256Sum};
2223

2324
use crate::vm::analysis::AnalysisDatabase;
2425
use crate::vm::contexts::GlobalContext;
26+
#[cfg(feature = "canonical")]
27+
use crate::vm::database::SqliteConnection;
2528
use crate::vm::database::{
2629
BurnStateDB, ClarityDatabase, ClarityDeserializable, ClaritySerializable, HeadersDB,
27-
SqliteConnection, NULL_BURN_STATE_DB, NULL_HEADER_DB,
30+
NULL_BURN_STATE_DB, NULL_HEADER_DB,
2831
};
2932
use crate::vm::errors::{
3033
CheckErrors, IncomparableError, InterpreterError, InterpreterResult as Result,
@@ -83,6 +86,8 @@ pub trait ClarityBackingStore {
8386

8487
fn get_open_chain_tip_height(&mut self) -> u32;
8588
fn get_open_chain_tip(&mut self) -> StacksBlockId;
89+
90+
#[cfg(feature = "canonical")]
8691
fn get_side_store(&mut self) -> &Connection;
8792

8893
fn get_cc_special_cases_handler(&self) -> Option<SpecialCaseHandler> {
@@ -106,59 +111,27 @@ pub trait ClarityBackingStore {
106111
fn get_contract_hash(
107112
&mut self,
108113
contract: &QualifiedContractIdentifier,
109-
) -> Result<(StacksBlockId, Sha512Trunc256Sum)> {
110-
let key = make_contract_hash_key(contract);
111-
let contract_commitment = self
112-
.get_data(&key)?
113-
.map(|x| ContractCommitment::deserialize(&x))
114-
.ok_or_else(|| CheckErrors::NoSuchContract(contract.to_string()))?;
115-
let ContractCommitment {
116-
block_height,
117-
hash: contract_hash,
118-
} = contract_commitment?;
119-
let bhh = self.get_block_at_height(block_height)
120-
.ok_or_else(|| InterpreterError::Expect("Should always be able to map from height to block hash when looking up contract information.".into()))?;
121-
Ok((bhh, contract_hash))
122-
}
114+
) -> Result<(StacksBlockId, Sha512Trunc256Sum)>;
123115

124116
fn insert_metadata(
125117
&mut self,
126118
contract: &QualifiedContractIdentifier,
127119
key: &str,
128120
value: &str,
129-
) -> Result<()> {
130-
let bhh = self.get_open_chain_tip();
131-
SqliteConnection::insert_metadata(
132-
self.get_side_store(),
133-
&bhh,
134-
&contract.to_string(),
135-
key,
136-
value,
137-
)
138-
}
121+
) -> Result<()>;
139122

140123
fn get_metadata(
141124
&mut self,
142125
contract: &QualifiedContractIdentifier,
143126
key: &str,
144-
) -> Result<Option<String>> {
145-
let (bhh, _) = self.get_contract_hash(contract)?;
146-
SqliteConnection::get_metadata(self.get_side_store(), &bhh, &contract.to_string(), key)
147-
}
127+
) -> Result<Option<String>>;
148128

149129
fn get_metadata_manual(
150130
&mut self,
151131
at_height: u32,
152132
contract: &QualifiedContractIdentifier,
153133
key: &str,
154-
) -> Result<Option<String>> {
155-
let bhh = self.get_block_at_height(at_height)
156-
.ok_or_else(|| {
157-
warn!("Unknown block height when manually querying metadata"; "block_height" => at_height);
158-
RuntimeErrorType::BadBlockHeight(at_height.to_string())
159-
})?;
160-
SqliteConnection::get_metadata(self.get_side_store(), &bhh, &contract.to_string(), key)
161-
}
134+
) -> Result<Option<String>>;
162135

163136
fn put_all_metadata(
164137
&mut self,
@@ -240,6 +213,7 @@ impl ClarityBackingStore for NullBackingStore {
240213
panic!("NullBackingStore can't retrieve data")
241214
}
242215

216+
#[cfg(feature = "canonical")]
243217
fn get_side_store(&mut self) -> &Connection {
244218
panic!("NullBackingStore has no side store")
245219
}
@@ -263,84 +237,37 @@ impl ClarityBackingStore for NullBackingStore {
263237
fn put_all_data(&mut self, mut _items: Vec<(String, String)>) -> Result<()> {
264238
panic!("NullBackingStore cannot put")
265239
}
266-
}
267-
268-
pub struct MemoryBackingStore {
269-
side_store: Connection,
270-
}
271240

272-
impl Default for MemoryBackingStore {
273-
fn default() -> Self {
274-
MemoryBackingStore::new()
275-
}
276-
}
277-
278-
impl MemoryBackingStore {
279-
#[allow(clippy::unwrap_used)]
280-
pub fn new() -> MemoryBackingStore {
281-
let side_store = SqliteConnection::memory().unwrap();
282-
283-
let mut memory_marf = MemoryBackingStore { side_store };
284-
285-
memory_marf.as_clarity_db().initialize();
286-
287-
memory_marf
288-
}
289-
290-
pub fn as_clarity_db(&mut self) -> ClarityDatabase {
291-
ClarityDatabase::new(self, &NULL_HEADER_DB, &NULL_BURN_STATE_DB)
292-
}
293-
294-
pub fn as_analysis_db(&mut self) -> AnalysisDatabase {
295-
AnalysisDatabase::new(self)
296-
}
297-
}
298-
299-
impl ClarityBackingStore for MemoryBackingStore {
300-
fn set_block_hash(&mut self, bhh: StacksBlockId) -> InterpreterResult<StacksBlockId> {
301-
Err(RuntimeErrorType::UnknownBlockHeaderHash(BlockHeaderHash(bhh.0)).into())
302-
}
303-
304-
fn get_data(&mut self, key: &str) -> Result<Option<String>> {
305-
SqliteConnection::get(self.get_side_store(), key)
306-
}
307-
308-
fn get_data_with_proof(&mut self, key: &str) -> Result<Option<(String, Vec<u8>)>> {
309-
Ok(SqliteConnection::get(self.get_side_store(), key)?.map(|x| (x, vec![])))
310-
}
311-
312-
fn get_side_store(&mut self) -> &Connection {
313-
&self.side_store
314-
}
315-
316-
fn get_block_at_height(&mut self, height: u32) -> Option<StacksBlockId> {
317-
if height == 0 {
318-
Some(StacksBlockId([255; 32]))
319-
} else {
320-
None
321-
}
322-
}
323-
324-
fn get_open_chain_tip(&mut self) -> StacksBlockId {
325-
StacksBlockId([255; 32])
326-
}
327-
328-
fn get_open_chain_tip_height(&mut self) -> u32 {
329-
0
241+
fn get_contract_hash(
242+
&mut self,
243+
_contract: &QualifiedContractIdentifier,
244+
) -> Result<(StacksBlockId, Sha512Trunc256Sum)> {
245+
panic!("NullBackingStore cannot get_contract_hash")
330246
}
331247

332-
fn get_current_block_height(&mut self) -> u32 {
333-
1
248+
fn insert_metadata(
249+
&mut self,
250+
_contract: &QualifiedContractIdentifier,
251+
_key: &str,
252+
_value: &str,
253+
) -> Result<()> {
254+
panic!("NullBackingStore cannot insert_metadata")
334255
}
335256

336-
fn get_cc_special_cases_handler(&self) -> Option<SpecialCaseHandler> {
337-
None
257+
fn get_metadata(
258+
&mut self,
259+
_contract: &QualifiedContractIdentifier,
260+
_key: &str,
261+
) -> Result<Option<String>> {
262+
panic!("NullBackingStore cannot get_metadata")
338263
}
339264

340-
fn put_all_data(&mut self, items: Vec<(String, String)>) -> Result<()> {
341-
for (key, value) in items.into_iter() {
342-
SqliteConnection::put(self.get_side_store(), &key, &value)?;
343-
}
344-
Ok(())
265+
fn get_metadata_manual(
266+
&mut self,
267+
_at_height: u32,
268+
_contract: &QualifiedContractIdentifier,
269+
_key: &str,
270+
) -> Result<Option<String>> {
271+
panic!("NullBackingStore cannot get_metadata_manual")
345272
}
346273
}

clarity/src/vm/database/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use hashbrown::HashMap;
18+
#[cfg(feature = "canonical")]
19+
pub use sqlite::MemoryBackingStore;
1820

1921
pub use self::clarity_db::{
2022
BurnStateDB, ClarityDatabase, HeadersDB, StoreType, NULL_BURN_STATE_DB, NULL_HEADER_DB,
2123
STORE_CONTRACT_SRC_INTERFACE,
2224
};
23-
pub use self::clarity_store::{ClarityBackingStore, MemoryBackingStore, SpecialCaseHandler};
25+
pub use self::clarity_store::{ClarityBackingStore, SpecialCaseHandler};
2426
pub use self::key_value_wrapper::{RollbackWrapper, RollbackWrapperPersistedLog};
27+
#[cfg(feature = "canonical")]
2528
pub use self::sqlite::SqliteConnection;
2629
pub use self::structures::{
2730
ClarityDeserializable, ClaritySerializable, DataMapMetadata, DataVariableMetadata,
@@ -31,5 +34,6 @@ pub use self::structures::{
3134
pub mod clarity_db;
3235
pub mod clarity_store;
3336
mod key_value_wrapper;
34-
mod sqlite;
37+
#[cfg(feature = "canonical")]
38+
pub mod sqlite;
3539
mod structures;

0 commit comments

Comments
 (0)