Skip to content

Commit

Permalink
feat: add version and epoch options to crosscheck binary
Browse files Browse the repository at this point in the history
See-also: #554
  • Loading branch information
ureeves committed Nov 14, 2024
1 parent 112f4ca commit 2a42ef3
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 27 deletions.
15 changes: 13 additions & 2 deletions clar2wasm/src/bin/crosscheck.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod utils;
use std::fs;

use clap::Parser;
use clar2wasm::tools::crosscheck_compare_only;
use clar2wasm::tools::crosscheck_compare_only_with_epoch_and_version;
use utils::*;

/// crosscheck is a tool to compare the results of the compiled and interpreted
/// versions of a Clarity snippet.
Expand All @@ -10,6 +12,12 @@ use clar2wasm::tools::crosscheck_compare_only;
struct Args {
/// Clarity source file to compile
input: String,
/// Epoch of the stacks chain
#[arg(long)]
stacks_epoch: Option<WrappedEpochId>,
/// The clarity version to use
#[arg(long)]
clarity_version: Option<WrappedClarityVersion>,
}

fn main() {
Expand All @@ -30,5 +38,8 @@ fn main() {
}
};

crosscheck_compare_only(&source);
let epoch = args.stacks_epoch.unwrap_or_default().into();
let version = args.clarity_version.unwrap_or_default().into();

crosscheck_compare_only_with_epoch_and_version(&source, epoch, version);
}
82 changes: 82 additions & 0 deletions clar2wasm/src/bin/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use clap::builder::PossibleValue;
use clap::ValueEnum;
use clarity::types::StacksEpochId;
use clarity::vm::ClarityVersion;

#[derive(Clone)]
pub struct WrappedEpochId(StacksEpochId);

impl Default for WrappedEpochId {
fn default() -> WrappedEpochId {
WrappedEpochId(StacksEpochId::Epoch25)
}
}

impl From<WrappedEpochId> for StacksEpochId {
fn from(epoch: WrappedEpochId) -> Self {
epoch.0
}
}

impl ValueEnum for WrappedEpochId {
fn value_variants<'a>() -> &'a [Self] {
&[
WrappedEpochId(StacksEpochId::Epoch10),
WrappedEpochId(StacksEpochId::Epoch20),
WrappedEpochId(StacksEpochId::Epoch2_05),
WrappedEpochId(StacksEpochId::Epoch21),
WrappedEpochId(StacksEpochId::Epoch22),
WrappedEpochId(StacksEpochId::Epoch23),
WrappedEpochId(StacksEpochId::Epoch24),
WrappedEpochId(StacksEpochId::Epoch25),
WrappedEpochId(StacksEpochId::Epoch30),
]
}

fn to_possible_value(&self) -> Option<PossibleValue> {
match &self.0 {
StacksEpochId::Epoch10 => Some(PossibleValue::new("1.0")),
StacksEpochId::Epoch20 => Some(PossibleValue::new("2.0")),
StacksEpochId::Epoch2_05 => Some(PossibleValue::new("2.05")),
StacksEpochId::Epoch21 => Some(PossibleValue::new("2.1")),
StacksEpochId::Epoch22 => Some(PossibleValue::new("2.2")),
StacksEpochId::Epoch23 => Some(PossibleValue::new("2.3")),
StacksEpochId::Epoch24 => Some(PossibleValue::new("2.4")),
StacksEpochId::Epoch25 => Some(PossibleValue::new("2.5")),
StacksEpochId::Epoch30 => Some(PossibleValue::new("3.0")),
}
}
}

#[derive(Clone)]
pub struct WrappedClarityVersion(ClarityVersion);

impl Default for WrappedClarityVersion {
fn default() -> WrappedClarityVersion {
WrappedClarityVersion(ClarityVersion::Clarity2)
}
}

impl From<WrappedClarityVersion> for ClarityVersion {
fn from(version: WrappedClarityVersion) -> Self {
version.0
}
}

impl ValueEnum for WrappedClarityVersion {
fn value_variants<'a>() -> &'a [Self] {
&[
WrappedClarityVersion(ClarityVersion::Clarity1),
WrappedClarityVersion(ClarityVersion::Clarity2),
WrappedClarityVersion(ClarityVersion::Clarity3),
]
}

fn to_possible_value(&self) -> Option<PossibleValue> {
match &self.0 {
ClarityVersion::Clarity1 => Some(PossibleValue::new("1")),
ClarityVersion::Clarity2 => Some(PossibleValue::new("2")),
ClarityVersion::Clarity3 => Some(PossibleValue::new("3")),
}
}
}
61 changes: 36 additions & 25 deletions clar2wasm/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,37 +526,48 @@ pub fn crosscheck_with_amount(snippet: &str, amount: u128, expected: Result<Opti
}
}

pub fn crosscheck_compare_only(snippet: &str) {
fn crosscheck_compare_only_with_env(snippet: &str, env: TestEnvironment) {
// to avoid false positives when both the compiled and interpreted fail,
// we don't allow failures in these tests
execute_crosscheck(
TestEnvironment::new(TestConfig::latest_epoch(), TestConfig::clarity_version()),
snippet,
|result| {
// If both interpreted and compiled results have errors, panic and
// show both errors.
// If only one fails, panic with the error from the failing one.
match (result.interpreted.as_ref(), result.compiled.as_ref()) {
(Err(interpreted_err), Err(compiled_err)) => {
panic!(
"Interpreted and compiled snippets failed: {:?}, {:?}",
interpreted_err, compiled_err
);
}
(Err(interpreted_err), Ok(_)) => {
panic!("Interpreted snippet failed: {:?}", interpreted_err);
}
(Ok(_), Err(compiled_err)) => {
panic!("Compiled snippet failed: {:?}", compiled_err);
}
_ => {
// Both succeeded; no action needed.
}
execute_crosscheck(env, snippet, |result| {
// If both interpreted and compiled results have errors, panic and
// show both errors.
// If only one fails, panic with the error from the failing one.
match (result.interpreted.as_ref(), result.compiled.as_ref()) {
(Err(interpreted_err), Err(compiled_err)) => {
panic!(
"Interpreted and compiled snippets failed: {:?}, {:?}",
interpreted_err, compiled_err
);
}
},
(Err(interpreted_err), Ok(_)) => {
panic!("Interpreted snippet failed: {:?}", interpreted_err);
}
(Ok(_), Err(compiled_err)) => {
panic!("Compiled snippet failed: {:?}", compiled_err);
}
_ => {
// Both succeeded; no action needed.
}
}
});
}

pub fn crosscheck_compare_only(snippet: &str) {
crosscheck_compare_only_with_env(
snippet,
TestEnvironment::new(TestConfig::latest_epoch(), TestConfig::clarity_version()),
);
}

pub fn crosscheck_compare_only_with_epoch_and_version(
snippet: &str,
epoch: StacksEpochId,
version: ClarityVersion,
) {
crosscheck_compare_only_with_env(snippet, TestEnvironment::new(epoch, version));
}

pub fn crosscheck_compare_only_with_expected_error<E: Fn(&Error) -> bool>(
snippet: &str,
expected: E,
Expand Down

0 comments on commit 2a42ef3

Please sign in to comment.