Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Increase config.rs coverage #329

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ tokio = "1.32.0"
toml = "0.8.0"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
url = "2.4.1"
url = { version = "2.4.1", features = ["serde"] }
wsts = "1.2"
87 changes: 45 additions & 42 deletions romeo/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use bdk::bitcoin::Network as BitcoinNetwork;
use blockstack_lib::vm::ContractName;
use clap::Parser;
use stacks_core::{
wallet::{BitcoinCredentials, Credentials, Wallet},
Network as StacksNetwork,
wallet::{BitcoinCredentials, Credentials as StacksCredentials, Wallet},
Network as StacksNetwork, StacksError,
};
use url::Url;

Expand All @@ -37,8 +37,7 @@ pub struct Config {
pub bitcoin_network: BitcoinNetwork,

/// Credentials used to interact with the Stacks network
pub stacks_credentials: Credentials,

pub stacks_credentials: StacksCredentials,
/// Credentials used to interact with the Bitcoin network
pub bitcoin_credentials: BitcoinCredentials,

Expand All @@ -63,20 +62,21 @@ pub struct Config {

impl Config {
/// Read the config file in the path
pub fn from_path(path: impl AsRef<Path>) -> anyhow::Result<Self> {
let config_root = normalize(
std::env::current_dir().unwrap(),
path.as_ref().parent().unwrap(),
);
pub fn try_from_path(path: impl AsRef<Path>) -> Result<Self, StacksError> {
ConfigFile::try_from(path.as_ref()).unwrap().try_into()
}

let config_file = ConfigFile::from_path(&path)?;
let state_directory =
normalize(config_root.clone(), config_file.state_directory);
/// The sbtc wallet address is the taproot address
/// of the bitcoin credentials
pub fn sbtc_wallet_address(&self) -> bdk::bitcoin::Address {
self.bitcoin_credentials.address_p2tr()
}
}

let stacks_node_url = Url::parse(&config_file.stacks_node_url)?;
let bitcoin_node_url = Url::parse(&config_file.bitcoin_node_url)?;
let electrum_node_url = Url::parse(&config_file.electrum_node_url)?;
impl TryFrom<ConfigFile> for Config {
type Error = stacks_core::StacksError;

fn try_from(config_file: ConfigFile) -> Result<Self, Self::Error> {
let wallet = Wallet::new(&config_file.mnemonic)?;

let stacks_credentials =
Expand All @@ -86,35 +86,21 @@ impl Config {
let hiro_api_key = config_file.hiro_api_key;

Ok(Self {
state_directory,
state_directory: config_file.state_directory,
stacks_network: config_file.stacks_network,
bitcoin_network: config_file.bitcoin_network,
stacks_credentials,
bitcoin_credentials,
stacks_node_url,
bitcoin_node_url,
electrum_node_url,
stacks_node_url: config_file.stacks_node_url,
bitcoin_node_url: config_file.bitcoin_node_url,
electrum_node_url: config_file.electrum_node_url,
contract_name: ContractName::from(
config_file.contract_name.as_str(),
),
hiro_api_key,
strict: config_file.strict.unwrap_or_default(),
strict: config_file.strict,
})
}

/// The sbtc wallet address is the taproot address
/// of the bitcoin credentials
pub fn sbtc_wallet_address(&self) -> bdk::bitcoin::Address {
self.bitcoin_credentials.address_p2tr()
}
}

fn normalize(root_dir: PathBuf, path: impl AsRef<Path>) -> PathBuf {
if path.as_ref().is_relative() {
root_dir.join(path)
} else {
path.as_ref().into()
}
}

#[derive(Debug, Clone, serde::Deserialize)]
Expand All @@ -132,28 +118,45 @@ struct ConfigFile {
pub bitcoin_network: BitcoinNetwork,

/// Address of a stacks node
pub stacks_node_url: String,
pub stacks_node_url: Url,

/// Address of a bitcoin node
pub bitcoin_node_url: String,
pub bitcoin_node_url: Url,

/// Address of the Electrum node
pub electrum_node_url: String,
pub electrum_node_url: Url,

/// sBTC asset contract name
pub contract_name: String,
pub contract_name: ContractName,

/// optional api key used for the stacks node
pub hiro_api_key: Option<String>,

/// Strict mode
pub strict: Option<bool>,
#[serde(default)]
pub strict: bool,
}

impl ConfigFile {
pub fn from_path(path: impl AsRef<Path>) -> anyhow::Result<Self> {
let config_file = File::open(&path)?;
impl TryFrom<&Path> for ConfigFile {
type Error = std::io::Error;

fn try_from(value: &Path) -> Result<Self, Self::Error> {
let config_file = File::open(value)?;
Ok(serde_json::from_reader(config_file)?)
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn deserialize() {
let path_str = "../devenv/sbtc/docker/config.json";
// this file is now the ground truth.
let path = Path::new(path_str);
Config::try_from_path(path).unwrap();

let path = path_str;
Config::try_from_path(path).unwrap();
}
}
2 changes: 1 addition & 1 deletion romeo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.init();

let args = romeo::config::Cli::parse();
let config = romeo::config::Config::from_path(args.config_file)?;
let config = romeo::config::Config::try_from_path(args.config_file)?;

Check warning on line 18 in romeo/src/main.rs

View check run for this annotation

Codecov / codecov/patch

romeo/src/main.rs#L18

Added line #L18 was not covered by tests

romeo::system::run(config).await;

Expand Down
4 changes: 2 additions & 2 deletions romeo/src/stacks_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore]
async fn get_nonce_info() {
let config = Config::from_path("./testing/config.json")
let config = Config::try_from_path("./testing/config.json")

Check warning on line 524 in romeo/src/stacks_client.rs

View check run for this annotation

Codecov / codecov/patch

romeo/src/stacks_client.rs#L524

Added line #L524 was not covered by tests
.expect("Failed to find config file");
let http_client = reqwest::Client::new();

Expand All @@ -534,7 +534,7 @@
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore]
async fn get_fee_rate() {
let config = Config::from_path("./testing/config.json")
let config = Config::try_from_path("./testing/config.json")

Check warning on line 537 in romeo/src/stacks_client.rs

View check run for this annotation

Codecov / codecov/patch

romeo/src/stacks_client.rs#L537

Added line #L537 was not covered by tests
.expect("Failed to find config file");
let http_client = reqwest::Client::new();

Expand Down
Loading