Skip to content

Commit

Permalink
Merge branch 'feat/test2' into feat/test-mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorycoppola committed Nov 19, 2021
2 parents 67c3468 + cdcacdf commit 4439b1e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/clarity_vm/clarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl ClarityInstance {

let epoch = GENESIS_EPOCH;

let cost_track = Some(LimitedCostTracker::new_free());
let cost_track = Some(LimitedCostTracker::new_free_on_network(self.mainnet));

let mut conn = ClarityBlockConnection {
datastore: writable,
Expand Down
13 changes: 10 additions & 3 deletions src/vm/costs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,10 @@ impl LimitedCostTracker {
LimitedCostTracker::new(use_mainnet, ExecutionCost::max_value(), clarity_db, epoch)
}

pub fn new_free() -> LimitedCostTracker {
pub fn new_free_on_network(use_mainnet:bool) -> LimitedCostTracker {
if !use_mainnet{
panic!("oh no");
}
LimitedCostTracker {
cost_function_references: HashMap::new(),
cost_contracts: HashMap::new(),
Expand All @@ -641,10 +644,14 @@ impl LimitedCostTracker {
memory: 0,
memory_limit: CLARITY_MEMORY_LIMIT,
non_free: None,
mainnet: false,
mainnet: use_mainnet,
}
}

pub fn new_free() -> LimitedCostTracker {
LimitedCostTracker::new_free_on_network(false)
}

fn default_cost_contract_for_epoch(epoch_id: StacksEpochId) -> String {
match epoch_id {
StacksEpochId::Epoch10 => {
Expand Down Expand Up @@ -795,7 +802,7 @@ fn compute_cost(
let mut null_store = NullBackingStore::new();
let conn = null_store.as_clarity_db();
let mut global_context =
GlobalContext::new(mainnet, conn, LimitedCostTracker::new_free(), eval_in_epoch);
GlobalContext::new(mainnet, conn, LimitedCostTracker::new_free_on_network(mainnet), eval_in_epoch);

let cost_contract = cost_tracker
.cost_contracts
Expand Down
15 changes: 10 additions & 5 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ pub fn eval_all(
/// This method executes the program in Epoch 2.0 *and* Epoch 2.05 and asserts
/// that the result is the same before returning the result
#[cfg(test)]
pub fn execute(program: &str) -> Result<Option<Value>> {
let epoch_200_result = execute_in_epoch(program, StacksEpochId::Epoch20);
let epoch_205_result = execute_in_epoch(program, StacksEpochId::Epoch2_05);
pub fn execute_on_network(program: &str, use_mainnet:bool) -> Result<Option<Value>> {
let epoch_200_result = execute_in_epoch(program, StacksEpochId::Epoch20, use_mainnet);
let epoch_205_result = execute_in_epoch(program, StacksEpochId::Epoch2_05, use_mainnet);
assert_eq!(
epoch_200_result, epoch_205_result,
"Epoch 2.0 and 2.05 should have same execution result, but did not for program `{}`",
Expand All @@ -389,12 +389,17 @@ pub fn execute(program: &str) -> Result<Option<Value>> {
}

#[cfg(test)]
pub fn execute_in_epoch(program: &str, epoch: StacksEpochId) -> Result<Option<Value>> {
pub fn execute(program: &str) -> Result<Option<Value>> {
execute_on_network(program, false)
}

#[cfg(test)]
pub fn execute_in_epoch(program: &str, epoch: StacksEpochId, use_mainnet:bool) -> Result<Option<Value>> {
let contract_id = QualifiedContractIdentifier::transient();
let mut contract_context = ContractContext::new(contract_id.clone());
let mut marf = MemoryBackingStore::new();
let conn = marf.as_clarity_db();
let mut global_context = GlobalContext::new(false, conn, LimitedCostTracker::new_free(), epoch);
let mut global_context = GlobalContext::new(false, conn, LimitedCostTracker::new_free_on_network(use_mainnet), epoch);
global_context.execute(|g| {
let parsed = ast::build_ast(&contract_id, program, &mut ())?.expressions;
eval_all(&parsed, &mut contract_context, g)
Expand Down
7 changes: 3 additions & 4 deletions src/vm/tests/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ use vm::costs::cost_functions::ClarityCostFunction;
use vm::costs::{ClarityCostFunctionReference, ExecutionCost, LimitedCostTracker};
use vm::database::ClarityDatabase;
use vm::errors::{CheckErrors, Error, RuntimeErrorType};
use vm::execute as vm_execute;
use vm::functions::NativeFunctions;
use vm::representations::SymbolicExpression;
use vm::tests::{
execute, is_committed, is_err_code, symbols_from_values, with_marfed_environment,
execute, execute_on_network, is_committed, is_err_code, symbols_from_values, with_marfed_environment,
with_memory_environment, TEST_BURN_STATE_DB, TEST_HEADER_DB,
};
use vm::types::{AssetIdentifier, PrincipalData, QualifiedContractIdentifier, ResponseData, Value};
Expand Down Expand Up @@ -800,8 +799,8 @@ fn test_cost_contract_short_circuits(#[case] use_mainnet: bool) {

let marf_kv = clarity_instance.destroy();

let p1 = execute("'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR");
let p2 = execute("'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G");
let p1 = execute_on_network("'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR", use_mainnet);
let p2 = execute_on_network("'SM2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQVX8X0G", use_mainnet);

let p1_principal = match p1 {
Value::Principal(PrincipalData::Standard(ref data)) => data.clone(),
Expand Down
5 changes: 5 additions & 0 deletions src/vm/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use vm::contracts::Contract;
use vm::database::ClarityDatabase;
use vm::errors::Error;
use vm::execute as vm_execute;
use vm::execute_on_network as vm_execute_on_network;
use vm::representations::SymbolicExpression;
use vm::types::{PrincipalData, ResponseData, Value};

Expand Down Expand Up @@ -216,6 +217,10 @@ pub fn execute(s: &str) -> Value {
vm_execute(s).unwrap().unwrap()
}

pub fn execute_on_network(s: &str, use_mainnet:bool) -> Value {
vm_execute_on_network(s, use_mainnet).unwrap().unwrap()
}

pub fn symbols_from_values(vec: Vec<Value>) -> Vec<SymbolicExpression> {
vec.into_iter()
.map(|value| SymbolicExpression::atom_value(value))
Expand Down

0 comments on commit 4439b1e

Please sign in to comment.