Skip to content

Commit

Permalink
chore: cargo fmt-stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
cylewitruk committed Mar 4, 2024
1 parent d1be1f7 commit 0dcae6d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 49 deletions.
3 changes: 1 addition & 2 deletions clarity/src/vm/contexts.rs
Expand Up @@ -23,8 +23,7 @@ use serde::Serialize;
use serde_json::json;
use stacks_common::consts::CHAIN_ID_TESTNET;
use stacks_common::types::chainstate::StacksBlockId;
use stacks_common::types::StacksEpochId;
use stacks_common::types::{StacksHashMap as HashMap, StacksHashSet as HashSet};
use stacks_common::types::{StacksEpochId, StacksHashMap as HashMap, StacksHashSet as HashSet};

use super::EvalHook;
use crate::vm::ast::{ASTRules, ContractAST};
Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/database/mod.rs
Expand Up @@ -34,4 +34,4 @@ mod key_value_wrapper;
mod sqlite;
mod structures;
#[cfg(feature = "testing")]
mod tests;
mod tests;
96 changes: 58 additions & 38 deletions clarity/src/vm/database/tests/clarity_db_tests.rs
@@ -1,8 +1,15 @@
use fake::{Faker, Fake};
use fake::{Fake, Faker};
use rusqlite::NO_PARAMS;
use stacks_common::util::hash::Sha512Trunc256Sum;

use crate::vm::{contracts::Contract, database::{clarity_store::ContractCommitment, ClarityBackingStore, ClarityDatabase, ClaritySerializable, MemoryBackingStore, NULL_BURN_STATE_DB, NULL_HEADER_DB}, fakes::raw::EnglishWord, Value};
use crate::vm::contracts::Contract;
use crate::vm::database::clarity_store::ContractCommitment;
use crate::vm::database::{
ClarityBackingStore, ClarityDatabase, ClaritySerializable, MemoryBackingStore,
NULL_BURN_STATE_DB, NULL_HEADER_DB,
};
use crate::vm::fakes::raw::EnglishWord;
use crate::vm::Value;

#[test]
fn insert_contract() {
Expand All @@ -18,12 +25,12 @@ fn insert_contract() {
db.insert_contract(&contract_id, contract)
.expect("failed to insert contract into backing store");

let exists = sql_exists(
let exists = sql_exists(
&mut store,
&format!(
"SELECT * FROM metadata_table WHERE key LIKE '%{}%'",
"SELECT * FROM metadata_table WHERE key LIKE '%{}%'",
contract_id.to_string()
)
),
);
assert!(!exists);
}
Expand All @@ -43,7 +50,8 @@ fn get_contract() {
db.insert_contract(&contract_id, contract.clone())
.expect("failed to insert contract into backing store");

let retrieved_contract = db.get_contract(&contract_id)
let retrieved_contract = db
.get_contract(&contract_id)
.expect("failed to retrieve contract from backing store");

assert_eq!(contract, retrieved_contract);
Expand Down Expand Up @@ -81,10 +89,14 @@ fn insert_contract_with_commit_should_exist_in_backing_store() {
db.commit().expect("failed to commit to backing store");

let count = sql_query_u32(
&mut store,
&format!("SELECT COUNT(*) FROM metadata_table WHERE key LIKE '{}'",
format!("clr-meta::{}::vm-metadata::9::contract", contract_id.to_string())
)
&mut store,
&format!(
"SELECT COUNT(*) FROM metadata_table WHERE key LIKE '{}'",
format!(
"clr-meta::{}::vm-metadata::9::contract",
contract_id.to_string()
)
),
);

assert_eq!(1, count);
Expand All @@ -99,10 +111,14 @@ fn put_data_no_commit() {

db.begin();

db.put("hello", &ContractCommitment {
block_height: 1,
hash: Sha512Trunc256Sum::from_data(&[1, 2, 3, 4])
}).expect("failed to put data");
db.put(
"hello",
&ContractCommitment {
block_height: 1,
hash: Sha512Trunc256Sum::from_data(&[1, 2, 3, 4]),
},
)
.expect("failed to put data");

let count = sql_query_u32(&mut store, "SELECT COUNT(*) FROM data_table");
assert_eq!(0, count);
Expand All @@ -118,16 +134,20 @@ fn put_data_with_commit_should_exist_in_backing_store() {
db.begin();

let key = Faker.fake::<String>();
db.put(&key, &ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>())
}).expect("failed to put data");
db.put(
&key,
&ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>()),
},
)
.expect("failed to put data");

db.commit().expect("failed to commit to backing store");

let count = sql_query_u32(
&mut store,
&format!("SELECT COUNT(*) FROM data_table where key = '{}'", key)
&mut store,
&format!("SELECT COUNT(*) FROM data_table where key = '{}'", key),
);
assert_eq!(1, count);
}
Expand All @@ -140,38 +160,38 @@ fn put_data_without_begin_fails() {
let mut db = ClarityDatabase::new(&mut store, &NULL_HEADER_DB, &NULL_BURN_STATE_DB);

let key = Faker.fake::<String>();
db.put(&key, &ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>())
}).expect_err("expected not-nested error");
db.put(
&key,
&ContractCommitment {
block_height: Faker.fake(),
hash: Sha512Trunc256Sum::from_data(&Faker.fake::<Vec<u8>>()),
},
)
.expect_err("expected not-nested error");
}
}

/// Executes the provided SQL query, which is expected to return a positive
/// integer, and returns it as a u32. Panics upon SQL failure.
fn sql_query_u32<S: ClarityBackingStore>(store: &mut S, sql: &str) -> u32 {
let sqlite = store.get_side_store();
sqlite.query_row(
sql,
NO_PARAMS,
|row| {
sqlite
.query_row(sql, NO_PARAMS, |row| {
let i: u32 = row.get(0)?;
Ok(i)
}
).expect("failed to verify results in sqlite")
})
.expect("failed to verify results in sqlite")
}

/// Executes the provided SQL query as a subquery within a `SELECT EXISTS(...)`
/// Executes the provided SQL query as a subquery within a `SELECT EXISTS(...)`
/// statement. Returns true if the statement returns any rows, false otherwise.
/// Panics upon SQL failure.
fn sql_exists<S: ClarityBackingStore>(store: &mut S, sql: &str) -> bool {
let sqlite = store.get_side_store();
sqlite.query_row(
&format!("SELECT EXISTS({});", sql),
NO_PARAMS,
|row| {
sqlite
.query_row(&format!("SELECT EXISTS({});", sql), NO_PARAMS, |row| {
let exists: bool = row.get(0)?;
Ok(exists)
}
).expect("failed to verify results in sqlite")
}
})
.expect("failed to verify results in sqlite")
}
2 changes: 1 addition & 1 deletion clarity/src/vm/database/tests/mod.rs
@@ -1 +1 @@
mod clarity_db_tests;
mod clarity_db_tests;
24 changes: 18 additions & 6 deletions clarity/src/vm/fakes.rs
Expand Up @@ -103,7 +103,6 @@ pub mod raw {

impl Dummy<Faker> for DefinedFunction {
fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {

let arg_types: Vec<TypeSignature> = (0..rng.gen_range(1..3))
.into_iter()
.map(|_| random_type_signature(None, rng))
Expand All @@ -116,12 +115,11 @@ pub mod raw {

DefinedFunction {
define_type: Faker.fake_with_rng(rng),
name: Faker.fake_with_rng(rng),
name: Faker.fake_with_rng(rng),
arg_types,
arguments,
body: Faker.fake_with_rng(rng),
identifier: Faker.fake_with_rng(rng),

}
}
}
Expand Down Expand Up @@ -177,7 +175,12 @@ pub mod raw {
))),
TypeSignature::TupleType(TupleTypeSignature {
type_map: (0..rng.gen_range(1..3))
.map(|_| (Faker.fake_with_rng(rng), random_type_signature(next_level, rng)))
.map(|_| {
(
Faker.fake_with_rng(rng),
random_type_signature(next_level, rng),
)
})
.collect(),
}),
];
Expand Down Expand Up @@ -251,7 +254,12 @@ pub mod raw {
.collect(),
type_signature: TupleTypeSignature {
type_map: (0..rng.gen_range(1..3))
.map(|_| (Faker.fake_with_rng(rng), random_type_signature(next_level, rng)))
.map(|_| {
(
Faker.fake_with_rng(rng),
random_type_signature(next_level, rng),
)
})
.collect(),
},
}),
Expand Down Expand Up @@ -366,7 +374,11 @@ pub mod raw {
FunctionType::ArithmeticUnary,
FunctionType::ArithmeticComparison,
FunctionType::ArithmeticVariadic,
FunctionType::Binary(Faker.fake_with_rng(rng), Faker.fake_with_rng(rng), Faker.fake_with_rng(rng)),
FunctionType::Binary(
Faker.fake_with_rng(rng),
Faker.fake_with_rng(rng),
Faker.fake_with_rng(rng),
),
];

fn_types[rng.gen_range(0..fn_types.len())].clone()
Expand Down
2 changes: 1 addition & 1 deletion stacks-common/src/types/mod.rs
Expand Up @@ -13,9 +13,9 @@ use crate::util::hash::Hash160;
use crate::util::secp256k1::{MessageSignature, Secp256k1PublicKey};

pub mod chainstate;
pub mod net;
pub mod hashmap;
pub mod hashset;
pub mod net;

pub use hashmap::StacksHashMap;
pub use hashset::StacksHashSet;
Expand Down

0 comments on commit 0dcae6d

Please sign in to comment.