From 0760426ecf76f6780cbb2827c49c832d13b47b4e Mon Sep 17 00:00:00 2001 From: Javier Viola <363911+pepoviola@users.noreply.github.com> Date: Mon, 15 Apr 2024 23:00:46 +0200 Subject: [PATCH] fix(orchestrator): Allow to use custom types with pjs and small fixes (#200) --- crates/examples/examples/pjs.rs | 3 +- .../examples/small_network_with_default.rs | 20 ++++++---- .../examples/small_network_with_para.rs | 13 ++----- crates/orchestrator/src/lib.rs | 7 ++-- crates/orchestrator/src/network/node.rs | 39 +++++++++++++++---- crates/provider/src/kubernetes/client.rs | 8 +++- crates/sdk/tests/smoke.rs | 4 +- 7 files changed, 61 insertions(+), 33 deletions(-) diff --git a/crates/examples/examples/pjs.rs b/crates/examples/examples/pjs.rs index a1681e6..8200e15 100644 --- a/crates/examples/examples/pjs.rs +++ b/crates/examples/examples/pjs.rs @@ -40,7 +40,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { return parachains.toJSON() "#; - let paras = alice.pjs(query_paras, vec![]).await??; + let paras = alice.pjs(query_paras, vec![], None).await??; println!("parachains registered: {:?}", paras); @@ -50,6 +50,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { .pjs_file( format!("{}/{}", manifest_dir, "examples/pjs_transfer.js"), vec![json!("//Alice")], + None, ) .await?; diff --git a/crates/examples/examples/small_network_with_default.rs b/crates/examples/examples/small_network_with_default.rs index 4a6fd00..f7f9592 100644 --- a/crates/examples/examples/small_network_with_default.rs +++ b/crates/examples/examples/small_network_with_default.rs @@ -1,9 +1,11 @@ +use std::time::Duration; + use zombienet_sdk::{NetworkConfigBuilder, NetworkConfigExt}; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { tracing_subscriber::fmt::init(); - let mut _network = NetworkConfigBuilder::new() + let network = NetworkConfigBuilder::new() .with_relaychain(|r| { r.with_chain("rococo-local") .with_default_command("polkadot") @@ -17,18 +19,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { .with_collator(|n| n.with_name("collator") // TODO: check how we can clean - // .with_command("polkadot-parachain") - .with_command("test-parachain") + .with_command("polkadot-parachain") + // .with_command("test-parachain") .with_image("docker.io/paritypr/test-parachain:c90f9713b5bc73a9620b2e72b226b4d11e018190") ) }) .build() .unwrap() - // .spawn_native() - .spawn_k8s() + .spawn_native() + // .spawn_k8s() .await?; println!("🚀🚀🚀🚀 network deployed"); + // give some time to node's bootstraping + tokio::time::sleep(Duration::from_secs(12)).await; // // Add a new node to the running network. // let opts = AddNodeOptions { @@ -43,10 +47,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { // with `nodes` (e.g pause, resume, restart) // Get a ref to the node - // let node = network.get_node("alice")?; + let node = network.get_node("alice")?; - // let is_10 = node.assert("block_height{status=\"best\"}", 10).await?; - // println!("is_10: {is_10}"); + let is_10 = node.assert("block_height{status=\"best\"}", 10).await?; + println!("is_10: {is_10}"); // let role = node.reports("node_roles").await?; // println!("Role is {role}"); diff --git a/crates/examples/examples/small_network_with_para.rs b/crates/examples/examples/small_network_with_para.rs index 5c33272..12b99bf 100644 --- a/crates/examples/examples/small_network_with_para.rs +++ b/crates/examples/examples/small_network_with_para.rs @@ -1,9 +1,6 @@ use std::time::Duration; -use zombienet_sdk::{ - AddCollatorOptions, AddNodeOptions, NetworkConfigBuilder, NetworkConfigExt, - RegistrationStrategy, -}; +use zombienet_sdk::{AddCollatorOptions, AddNodeOptions, NetworkConfigBuilder, NetworkConfigExt}; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { @@ -18,7 +15,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { .with_parachain(|p| { p.with_id(100) .cumulus_based(true) - .with_registration_strategy(RegistrationStrategy::UsingExtrinsic) + //.with_registration_strategy(RegistrationStrategy::UsingExtrinsic) .with_collator(|n| n.with_name("collator").with_command("polkadot-parachain")) }) .build() @@ -37,12 +34,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { // TODO: add check to ensure if unique network.add_node("new1", opts).await?; - tokio::time::sleep(Duration::from_secs(2)).await; - // Example of some operations that you can do // with `nodes` (e.g pause, resume, restart) - tokio::time::sleep(Duration::from_secs(10)).await; + tokio::time::sleep(Duration::from_secs(12)).await; // Get a ref to the node let node = network.get_node("alice")?; @@ -57,7 +52,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { // node.pause().await?; // println!("node new1 paused!"); - tokio::time::sleep(Duration::from_secs(2)).await; + // tokio::time::sleep(Duration::from_secs(2)).await; // node.resume().await?; // println!("node new1 resumed!"); diff --git a/crates/orchestrator/src/lib.rs b/crates/orchestrator/src/lib.rs index 633257d..c7cab90 100644 --- a/crates/orchestrator/src/lib.rs +++ b/crates/orchestrator/src/lib.rs @@ -71,7 +71,7 @@ where mut network_spec: NetworkSpec, ) -> Result<Network<T>, OrchestratorError> { // main driver for spawn the network - debug!("Network spec to spawn, {:#?}", network_spec); + debug!(network_spec = ?network_spec,"Network spec to spawn"); // TODO: move to Provider trait validate_spec_with_provider_capabilities(&network_spec, self.provider.capabilities()) @@ -84,14 +84,13 @@ where // create namespace let ns = self.provider.create_namespace().await?; + info!("🧰 ns: {}", ns.name()); + info!("🧰 base_dir: {:?}", ns.base_dir()); network_spec .populate_nodes_available_args(ns.clone()) .await?; - info!("🧰 ns: {}", ns.name()); - info!("🧰 base_dir: {:?}", ns.base_dir()); - let base_dir = ns.base_dir().to_string_lossy(); let scoped_fs = ScopedFilesystem::new(&self.filesystem, &base_dir); // Create chain-spec for relaychain diff --git a/crates/orchestrator/src/network/node.rs b/crates/orchestrator/src/network/node.rs index ca84186..19acb8b 100644 --- a/crates/orchestrator/src/network/node.rs +++ b/crates/orchestrator/src/network/node.rs @@ -78,8 +78,9 @@ impl NetworkNode { &self, code: impl AsRef<str>, args: Vec<serde_json::Value>, + user_types: Option<serde_json::Value>, ) -> Result<PjsResult, anyhow::Error> { - let code = pjs_build_template(self.ws_uri(), code.as_ref(), args); + let code = pjs_build_template(self.ws_uri(), code.as_ref(), args, user_types); trace!("Code to execute: {code}"); let value = match pjs_inner(code)? { ReturnValue::Deserialized(val) => Ok(val), @@ -98,9 +99,10 @@ impl NetworkNode { &self, file: impl AsRef<Path>, args: Vec<serde_json::Value>, + user_types: Option<serde_json::Value>, ) -> Result<PjsResult, anyhow::Error> { let content = std::fs::read_to_string(file)?; - let code = pjs_build_template(self.ws_uri(), content.as_ref(), args); + let code = pjs_build_template(self.ws_uri(), content.as_ref(), args, user_types); trace!("Code to execute: {code}"); let value = match pjs_inner(code)? { @@ -210,12 +212,32 @@ impl std::fmt::Debug for NetworkNode { // Helper methods -fn pjs_build_template(ws_uri: &str, content: &str, args: Vec<serde_json::Value>) -> String { - format!( +fn pjs_build_template( + ws_uri: &str, + content: &str, + args: Vec<serde_json::Value>, + user_types: Option<serde_json::Value>, +) -> String { + let types = if let Some(user_types) = user_types { + if let Some(types) = user_types.pointer("/types") { + // if the user_types includes the `types` key use the inner value + types.clone() + } else { + user_types.clone() + } + } else { + // No custom types, just an emtpy json + json!({}) + }; + + let tmpl = format!( r#" const {{ util, utilCrypto, keyring, types }} = pjs; ( async () => {{ - const api = await pjs.api.ApiPromise.create({{ provider: new pjs.api.WsProvider('{}') }}); + const api = await pjs.api.ApiPromise.create({{ + provider: new pjs.api.WsProvider('{}'), + types: {} + }}); const _run = async (api, hashing, keyring, types, util, arguments) => {{ {} }}; @@ -223,9 +245,12 @@ fn pjs_build_template(ws_uri: &str, content: &str, args: Vec<serde_json::Value>) }})() "#, ws_uri, + types, content, - json!(args) - ) + json!(args), + ); + trace!(tmpl = tmpl, "code to execute"); + tmpl } // Since pjs-rs run a custom javascript runtime (using deno_core) we need to diff --git a/crates/provider/src/kubernetes/client.rs b/crates/provider/src/kubernetes/client.rs index e9d5306..8aac49a 100644 --- a/crates/provider/src/kubernetes/client.rs +++ b/crates/provider/src/kubernetes/client.rs @@ -1,5 +1,6 @@ use std::{ collections::BTreeMap, fmt::Debug, os::unix::process::ExitStatusExt, process::ExitStatus, + time::Duration, }; use anyhow::anyhow; @@ -18,7 +19,7 @@ use kube::{ use serde::de::DeserializeOwned; use tokio::{io::AsyncRead, net::TcpListener, task::JoinHandle}; use tokio_util::compat::FuturesAsyncReadCompatExt; -use tracing::debug; +use tracing::{debug, trace}; use crate::{constants::LOCALHOST, types::ExecutionResult}; @@ -161,7 +162,10 @@ impl KubernetesClient { .await .map_err(|err| Error::from(anyhow!("error while creating pod {name}: {err}")))?; - await_condition(pods, name, helpers::is_pod_ready()) + trace!("Pod {name} checking for ready state!"); + let wait_ready = await_condition(pods, name, helpers::is_pod_ready()); + // TODO: we may want to allow to set this timeout for pod spawning. + let _ = tokio::time::timeout(Duration::from_secs(30), wait_ready) .await .map_err(|err| { Error::from(anyhow!("error while awaiting pod {name} running: {err}")) diff --git a/crates/sdk/tests/smoke.rs b/crates/sdk/tests/smoke.rs index 70056fc..802dafd 100644 --- a/crates/sdk/tests/smoke.rs +++ b/crates/sdk/tests/smoke.rs @@ -103,7 +103,7 @@ async fn ci_k8s_basic_functionalities_should_works() { "#; let is_registered = alice - .pjs(para_is_registered, vec![json!(2000)]) + .pjs(para_is_registered, vec![json!(2000)], None) .await .unwrap() .unwrap(); @@ -115,7 +115,7 @@ async fn ci_k8s_basic_functionalities_should_works() { return parachains.toJSON() "#; - let paras = alice.pjs(query_paras, vec![]).await.unwrap(); + let paras = alice.pjs(query_paras, vec![], None).await.unwrap(); println!("parachains registered: {:?}", paras); -- GitLab