Skip to content

Commit

Permalink
chore: Add in place of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstone committed Dec 10, 2024
1 parent c5d3bc5 commit 9472de9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 35 deletions.
62 changes: 37 additions & 25 deletions blueprint-test-utils/src/tangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::path::PathBuf;
pub mod node;
pub mod transactions;

pub use node::NodeConfig;

const TANGLE_RELEASE_MAC: &str = "https://github.com/tangle-network/tangle/releases/download/83f587f/tangle-testnet-manual-seal-darwin-amd64";
const TANGLE_RELEASE_LINUX: &str = "https://github.com/tangle-network/tangle/releases/download/83f587f/tangle-testnet-manual-seal-linux-amd64";

Expand Down Expand Up @@ -63,34 +65,44 @@ pub async fn download_tangle_binary() -> Result<PathBuf, Box<dyn std::error::Err
Ok(binary_path)
}

/// Run a Tangle node with the default settings.
/// Run a Tangle node with the given configuration.
/// The node will shut down when the returned handle is dropped.
pub async fn run(use_local_tangle: bool) -> Result<SubstrateNode, Error> {
let builder = SubstrateNode::builder()
.binary_paths(if use_local_tangle {
let tangle_from_env =
std::env::var(TANGLE_NODE_ENV).unwrap_or_else(|_| "tangle".to_string());
vec![
tangle_from_env,
"../tangle/target/release/tangle".to_string(),
"../../tangle/target/release/tangle".to_string(),
"../../../tangle/target/release/tangle".to_string(),
]
} else {
let binary_path = download_tangle_binary().await.map_err(|e| {
Error::Io(std::io::Error::new(
std::io::ErrorKind::Other,
e.to_string(),
))
})?;
vec![binary_path.to_string_lossy().into_owned()]
})
pub async fn run(config: NodeConfig) -> Result<SubstrateNode, Error> {
let mut builder = SubstrateNode::builder();

// Add binary paths
if config.use_local_tangle {
let tangle_from_env =
std::env::var(TANGLE_NODE_ENV).unwrap_or_else(|_| "tangle".to_string());
builder
.add_binary_path(tangle_from_env)
.add_binary_path("../tangle/target/release/tangle")
.add_binary_path("../../tangle/target/release/tangle")
.add_binary_path("../../../tangle/target/release/tangle");
} else {
let binary_path = download_tangle_binary().await.map_err(|e| {
Error::Io(std::io::Error::new(
std::io::ErrorKind::Other,
e.to_string(),
))
})?;
builder.add_binary_path(binary_path.to_string_lossy().to_string());
}

// Add standard arguments
builder
.arg("validator")
.arg_val("rpc-cors", "all")
.arg_val("rpc-methods", "unsafe")
.arg("rpc-external")
.arg_val("sealing", "manual")
.clone();
.arg_val("sealing", "manual");

// Add log configuration
let log_string = config.to_log_string();
if !log_string.is_empty() {
builder.arg_val("log", log_string);
}

builder.spawn()
}

Expand Down Expand Up @@ -132,7 +144,7 @@ macro_rules! test_tangle_blueprint {
[$($inputs:expr),*],
[$($expected_output:expr),*],
$call_id:expr,
$use_local_tangle:expr,
$node_config:expr,
) => {
::blueprint_test_utils::tangle_blueprint_test_template!(
$N,
Expand Down Expand Up @@ -183,7 +195,7 @@ macro_rules! test_tangle_blueprint {
assert_eq!(result, expected);
}
},
$use_local_tangle,
$node_config,
);
};
(
Expand Down
59 changes: 54 additions & 5 deletions blueprint-test-utils/src/tangle/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type CowStr = Cow<'static, str>;

#[derive(Debug, Clone)]
pub struct SubstrateNodeBuilder {
binary_paths: Vec<OsString>,
binary_paths: Vec<String>,
custom_flags: HashMap<CowStr, Option<CowStr>>,
}

Expand All @@ -73,12 +73,18 @@ impl SubstrateNodeBuilder {

/// Set the path to the `substrate` binary; defaults to "substrate-node"
/// or "substrate".
pub fn binary_paths<Paths, S>(&mut self, paths: Paths) -> &mut Self
pub fn binary_paths<I, S>(&mut self, paths: I) -> &mut Self
where
Paths: IntoIterator<Item = S>,
S: Into<OsString>,
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.binary_paths = paths.into_iter().map(|p| p.into()).collect();
self.binary_paths = paths.into_iter().map(Into::into).collect();
self
}

/// Add a single binary path.
pub fn add_binary_path<S: Into<String>>(&mut self, path: S) -> &mut Self {
self.binary_paths.push(path.into());
self
}

Expand Down Expand Up @@ -499,3 +505,46 @@ impl SubstrateNodeInfo {
.ok_or_else(|| Error::CouldNotExtractP2pPort(self.log.clone()))
}
}

#[derive(Debug, Clone, Default)]
pub struct NodeConfig {
pub use_local_tangle: bool,
pub log_level: Option<String>,
pub log_targets: Vec<(String, String)>,
}

impl NodeConfig {
pub fn new(use_local_tangle: bool) -> Self {
Self {
use_local_tangle,
log_level: None,
log_targets: Vec::new(),
}
}

pub fn with_log_level(mut self, level: impl Into<String>) -> Self {
self.log_level = Some(level.into());
self
}

pub fn with_log_target(mut self, target: impl Into<String>, level: impl Into<String>) -> Self {
self.log_targets.push((target.into(), level.into()));
self
}

pub fn to_log_string(&self) -> String {
let mut parts = Vec::new();

// Add global level if set
if let Some(level) = &self.log_level {
parts.push(level.clone());
}

// Add target-specific levels
for (target, level) in &self.log_targets {
parts.push(format!("{}={}", target, level));
}

parts.join(",")
}
}
6 changes: 3 additions & 3 deletions blueprint-test-utils/src/test_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use gadget_sdk::clients::tangle::services::{RpcServicesWithBlueprint, ServicesCl
use gadget_sdk::subxt_core::config::Header;
use gadget_sdk::utils::test_utils::get_client;
use crate::tangle::node::SubstrateNode;
use crate::tangle::transactions;
use crate::tangle::{transactions, NodeConfig};
use tnt_core_bytecode::bytecode::MASTER_BLUEPRINT_SERVICE_MANAGER;

const LOCAL_BIND_ADDR: &str = "127.0.0.1";
Expand All @@ -64,7 +64,7 @@ pub async fn new_test_ext_blueprint_manager<
>(
additional_params: D,
f: F,
use_local_tangle: bool,
node_config: NodeConfig,
) -> LocalhostTestExt {
assert!(N > 0, "At least one node is required");
assert!(N <= NAME_IDS.len(), "Only up to 5 nodes are supported");
Expand All @@ -85,7 +85,7 @@ pub async fn new_test_ext_blueprint_manager<
let blueprint_name = manifest.package.as_ref().unwrap().name.clone();

tracing::info!("Starting Tangle node...");
let tangle_node = crate::tangle::run(use_local_tangle).await.unwrap();
let tangle_node = crate::tangle::run(node_config).await.unwrap();
tracing::info!("Tangle node running on port: {}", tangle_node.ws_port());

let mut opts = Opts {
Expand Down
4 changes: 2 additions & 2 deletions blueprints/incredible-squaring/tests/e2e.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blueprint_test_utils::{test_tangle_blueprint, InputValue, OutputValue};
use blueprint_test_utils::{tangle::NodeConfig, test_tangle_blueprint, InputValue, OutputValue};

const SQUARING_JOB_ID: usize = 0;
const N: usize = 5;
Expand All @@ -10,5 +10,5 @@ test_tangle_blueprint!(
[InputValue::Uint64(5)], // Inputs
[OutputValue::Uint64(25)], // Expected output: input squared
0,
true,
NodeConfig::new(false),
);

0 comments on commit 9472de9

Please sign in to comment.