Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clarity/src/vm/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum AssetMapEntry {
The AssetMap is used to track which assets have been transfered from whom
during the execution of a transaction.
*/
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssetMap {
/// Sum of all STX transfers by principal
stx_map: HashMap<PrincipalData, u128>,
Expand Down
20 changes: 18 additions & 2 deletions clarity/src/vm/functions/post_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ pub fn special_restrict_assets(
Ok(last_result)
})();

// If there was a runtime error, pass it up immediately. We will roll back
// any way, so no need to check allowances.
if let Err(runtime_err) = eval_result {
env.global_context.roll_back()?;
return Err(runtime_err);
}

let asset_maps = env.global_context.get_readonly_asset_map()?;

// If the allowances are violated:
Expand Down Expand Up @@ -274,7 +281,8 @@ pub fn special_restrict_assets(
Err(InterpreterError::Expect("Failed to get body result".into()).into())
}
Err(e) => {
// Runtime error inside body, pass it up
// Runtime error inside body, pass it up (but this should have been
// caught already above)
Err(e)
}
}
Expand Down Expand Up @@ -338,6 +346,13 @@ pub fn special_as_contract(
Ok(last_result)
})();

// If there was a runtime error, pass it up immediately. We will roll back
// any way, so no need to check allowances.
if let Err(runtime_err) = eval_result {
nested_env.global_context.roll_back()?;
return Err(runtime_err);
}

let asset_maps = nested_env.global_context.get_readonly_asset_map()?;

// If the allowances are violated:
Expand Down Expand Up @@ -368,7 +383,8 @@ pub fn special_as_contract(
Err(InterpreterError::Expect("Failed to get body result".into()).into())
}
Err(e) => {
// Runtime error inside body, pass it up
// Runtime error inside body, pass it up (but this should have been
// caught already above)
Err(e)
}
}
Expand Down
38 changes: 38 additions & 0 deletions clarity/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,44 @@ where
})
}

/// Runs `program` in a test environment, first calling `global_context_function`.
/// Returns the final evaluated result along with the asset map.
#[cfg(any(test, feature = "testing"))]
pub fn execute_call_in_global_context_and_return_asset_map<F>(
program: &str,
clarity_version: ClarityVersion,
epoch: StacksEpochId,
use_mainnet: bool,
mut global_context_function: F,
) -> Result<(Option<Value>, crate::vm::contexts::AssetMap)>
where
F: FnMut(&mut GlobalContext) -> Result<()>,
{
use crate::vm::database::MemoryBackingStore;
use crate::vm::tests::test_only_mainnet_to_chain_id;
use crate::vm::types::QualifiedContractIdentifier;

let contract_id = QualifiedContractIdentifier::transient();
let mut contract_context = ContractContext::new(contract_id.clone(), clarity_version);
let mut marf = MemoryBackingStore::new();
let conn = marf.as_clarity_db();
let chain_id = test_only_mainnet_to_chain_id(use_mainnet);
let mut global_context = GlobalContext::new(
use_mainnet,
chain_id,
conn,
LimitedCostTracker::new_free(),
epoch,
);
global_context.execute(|g| {
global_context_function(g)?;
let parsed =
ast::build_ast(&contract_id, program, &mut (), clarity_version, epoch)?.expressions;
eval_all(&parsed, &mut contract_context, g, None)
.map(|r| (r, g.get_readonly_asset_map().cloned().unwrap_or_default()))
})
}

#[cfg(any(test, feature = "testing"))]
pub fn execute_with_parameters(
program: &str,
Expand Down
2 changes: 2 additions & 0 deletions clarity/src/vm/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ mod defines;
mod post_conditions;
mod principals;
#[cfg(test)]
pub mod proptest_utils;
#[cfg(test)]
mod representations;
mod sequences;
#[cfg(test)]
Expand Down
Loading
Loading