Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [Unreleased]

- Rewrote verbosity configuration in both `simplex test` and SDK to check if debug symbols should be included in program compilation.

## [0.0.5]

### Simplex
Expand Down
6 changes: 3 additions & 3 deletions crates/cli/src/commands/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ pub struct TestFlags {
/// Run tests regardless of failure
#[arg(long = "no-fail-fast")]
pub no_fail_fast: bool,
/// Log simplicity pruning stack trace
#[arg(short = 'v', long)]
pub verbose: bool,
/// Verbosity level for test output (-v for debug, -vv for trace)
#[arg(short = 'v', long, action = clap::ArgAction::Count)]
pub verbose: u8,
/// Display one character per test instead of one line
#[arg(short = 'q', long)]
pub quiet: bool,
Expand Down
3 changes: 3 additions & 0 deletions crates/cli/src/commands/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum CommandError {

#[error("IO error: {0}")]
Io(#[from] std::io::Error),

#[error("Verbosity level should be either -v or -vv, got: -v x {0}")]
BadVersbosityMode(u8),
}

#[derive(thiserror::Error, Debug)]
Expand Down
8 changes: 5 additions & 3 deletions crates/cli/src/commands/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;
use std::process::Stdio;

use smplx_test::config::Verbosity;
use smplx_sdk::global::Verbosity;
use smplx_test::{SMPLX_TEST_MARKER, TestConfig};

use super::core::{TestArguments, TestFlags};
Expand All @@ -20,10 +20,12 @@ impl Test {
pub fn run(mut config: TestConfig, args: &TestArguments, flags: &TestFlags) -> Result<(), CommandError> {
let cache_path = Self::get_test_config_cache_name()?;

if flags.verbose {
config.verbosity = Some(Verbosity(4));
if flags.verbose > Verbosity::MAX_VERBOSITY_LEVEL {
return Err(CommandError::BadVersbosityMode(flags.verbose));
}

config.verbosity = std::cmp::max(config.verbosity, Verbosity::new(flags.verbose));

config.to_file(&cache_path)?;

let mut cargo_test_command = Self::build_cargo_test_command(&cache_path, args, flags);
Expand Down
15 changes: 1 addition & 14 deletions crates/cli/src/config/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::path::{Path, PathBuf};

use smplx_build::BuildConfig;
use smplx_regtest::RegtestConfig;
use smplx_sdk::program::TrackerLogLevel;
use smplx_test::{TestConfig, config::Verbosity};
use smplx_test::TestConfig;

use super::error::ConfigError;

Expand Down Expand Up @@ -67,11 +66,6 @@ impl Config {
}

fn validate(config: &Config) -> Result<(), ConfigError> {
match config.test.verbosity {
Comment thread
ivanlele marked this conversation as resolved.
Some(verbosity) => Self::validate_verbosity(verbosity),
None => Ok(()),
}?;

match config.test.esplora.clone() {
Some(esplora_config) => {
Self::validate_network(&esplora_config.network)?;
Expand All @@ -86,13 +80,6 @@ impl Config {
}
}

fn validate_verbosity(verbosity: Verbosity) -> Result<(), ConfigError> {
match TryInto::<TrackerLogLevel>::try_into(verbosity) {
Ok(_) => Ok(()),
Err(val) => Err(ConfigError::BadVersbosityMode(val.0)),
}
}

fn validate_network(network: &String) -> Result<(), ConfigError> {
if network != "Liquid" && network != "LiquidTestnet" && network != "ElementsRegtest" {
return Err(ConfigError::BadNetworkName(network.clone()));
Expand Down
3 changes: 0 additions & 3 deletions crates/cli/src/config/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ pub enum ConfigError {

#[error("Path doesn't exist: '{0}'")]
PathNotExists(PathBuf),

#[error("Verbosity level should be either 1, 2, 3, 4, got: {0}")]
BadVersbosityMode(u64),
}
71 changes: 60 additions & 11 deletions crates/sdk/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
use std::sync::OnceLock;

use serde::{Deserialize, Serialize};

use crate::program::TrackerLogLevel;

static GLOBAL_CONFIG: OnceLock<GlobalConfig> = OnceLock::new();

/// A structure to represent the global configuration settings for the application.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Default)]
pub struct GlobalConfig {
log_level: TrackerLogLevel,
verbosity: Verbosity,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Serialize, Deserialize)]
/// An enumeration to represent the verbosity levels of the Simplicity execution logging.
pub enum Verbosity {
/// No logs will be printed.
#[default]
None = 0,
/// The debug mode
Debug = 1,
/// The trace mode
Trace = 2,
}

impl Default for GlobalConfig {
fn default() -> Self {
Self {
log_level: TrackerLogLevel::Debug,
impl Verbosity {
/// The maximum allowed verbosity level.
pub const MAX_VERBOSITY_LEVEL: u8 = 2;

/// Creates a `Verbosity` instance from the number of verbosity flags provided (e.g., -v, -vv).
#[must_use]
pub fn new(flags: u8) -> Self {
match flags {
0 => Verbosity::None,
1 => Verbosity::Debug,
_ => Verbosity::Trace,
}
}
}

static GLOBAL_CONFIG: OnceLock<GlobalConfig> = OnceLock::new();
/// Converts the `Verbosity` level to a corresponding `TrackerLogLevel`.
#[must_use]
pub fn tracker_log_level(&self) -> TrackerLogLevel {
match self {
Verbosity::None => TrackerLogLevel::None,
Verbosity::Debug => TrackerLogLevel::Debug,
Verbosity::Trace => TrackerLogLevel::Trace,
}
}

/// Determines if the current verbosity level includes debug symbols.
#[must_use]
pub fn include_debug_symbols(&self) -> bool {
matches!(self, Verbosity::Debug | Verbosity::Trace)
}
}

/// Sets the global configuration for the SDK.
///
Expand All @@ -26,13 +63,25 @@ static GLOBAL_CONFIG: OnceLock<GlobalConfig> = OnceLock::new();
///
/// # Errors
/// Returns an error containing the newly created `GlobalConfig` if the global configuration has already been initialized.
pub fn set_global_config(log_level: TrackerLogLevel) -> Result<(), GlobalConfig> {
GLOBAL_CONFIG.set(GlobalConfig { log_level })
pub fn set_global_config(verbosity: Verbosity) -> Result<(), GlobalConfig> {
GLOBAL_CONFIG.set(GlobalConfig { verbosity })
}

/// Returns the default log level if `GLOBAL_CONFIG` is not initialized
pub fn get_log_level() -> TrackerLogLevel {
GLOBAL_CONFIG
.get()
.map_or(GlobalConfig::default().log_level, |config| config.log_level)
.map_or(GlobalConfig::default().verbosity.tracker_log_level(), |config| {
config.verbosity.tracker_log_level()
})
}

/// Returns whether the global configuration includes debug symbols,
/// defaulting to `false` if `GLOBAL_CONFIG` is not initialized.
pub fn get_include_debug_symbols() -> bool {
GLOBAL_CONFIG
.get()
.map_or(GlobalConfig::default().verbosity.include_debug_symbols(), |config| {
config.verbosity.include_debug_symbols()
})
}
11 changes: 8 additions & 3 deletions crates/sdk/src/program/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use simplicityhl::simplicity::{BitMachine, RedeemNode, Value, leaf_version};
use simplicityhl::tracker::DefaultTracker;
use simplicityhl::{Parameters, WitnessTypes, WitnessValues};

use crate::global::get_log_level;
use crate::global::{get_include_debug_symbols, get_log_level};

use super::arguments::ArgumentsTrait;
use super::error::ProgramError;
Expand Down Expand Up @@ -301,8 +301,13 @@ impl Program {
}

fn load(&self) -> Result<CompiledProgram, ProgramError> {
let compiled = CompiledProgram::new(self.source, self.arguments.build_arguments(), true)
.map_err(ProgramError::Compilation)?;
let compiled = CompiledProgram::new(
self.source,
self.arguments.build_arguments(),
get_include_debug_symbols(),
)
.map_err(ProgramError::Compilation)?;

Ok(compiled)
Comment thread
ivanlele marked this conversation as resolved.
}

Expand Down
23 changes: 3 additions & 20 deletions crates/test/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::Path;
use serde::{Deserialize, Serialize};

use smplx_regtest::RegtestConfig;
use smplx_sdk::program::TrackerLogLevel;
use smplx_sdk::global::Verbosity;

use super::error::TestError;

Expand All @@ -22,7 +22,7 @@ pub struct TestConfig {
pub bitcoins: u64,
pub esplora: Option<EsploraConfig>,
pub rpc: Option<RpcConfig>,
pub verbosity: Option<Verbosity>,
pub verbosity: Verbosity,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand All @@ -38,23 +38,6 @@ pub struct RpcConfig {
pub password: String,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Verbosity(pub u64);

impl TryInto<TrackerLogLevel> for Verbosity {
type Error = Self;

fn try_into(self) -> Result<TrackerLogLevel, Self::Error> {
match self {
Verbosity(1) => Ok(TrackerLogLevel::None),
Verbosity(2) => Ok(TrackerLogLevel::Warning),
Verbosity(3) => Ok(TrackerLogLevel::Debug),
Verbosity(4) => Ok(TrackerLogLevel::Trace),
_ => Err(self),
}
}
}

impl TestConfig {
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, TestError> {
let mut content = String::new();
Expand Down Expand Up @@ -97,7 +80,7 @@ impl Default for TestConfig {
bitcoins: DEFAULT_BITCOINS,
esplora: None,
rpc: None,
verbosity: Some(Verbosity(3)),
verbosity: Verbosity::None,
}
}
}
8 changes: 1 addition & 7 deletions crates/test/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ impl TestContext {
let config = TestConfig::from_file(&config_path)?;

// error is ignored because we assume that all tests use the same verbosity
let _ = set_global_config(
config
.verbosity
.expect("This will be set")
.try_into()
.expect("Validated in CLI"),
);
let _ = set_global_config(config.verbosity);

let (signer, provider_info, client) = Self::setup(&config)?;

Expand Down