Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Top level API through zombienet-sdk crate #126

Merged
merged 5 commits into from
Nov 7, 2023
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = [
"crates/sdk",
"crates/examples",
"crates/support",
"crates/configuration",
Expand Down
7 changes: 1 addition & 6 deletions crates/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
configuration = { path = "../configuration" }
orchestrator = { path = "../orchestrator" }
provider = { path = "../provider" }
# TODO: we shouldn't need to pull from support, we need
# to review the exports for neeeded types
support = { path = "../support" }
zombienet-sdk = { path = "../sdk" }
tokio = { workspace = true }
futures = { workspace = true }
subxt = { workspace = true }
18 changes: 5 additions & 13 deletions crates/examples/examples/simple_network_example.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
// use std::time::Duration;

use configuration::NetworkConfig;
use futures::stream::StreamExt;
use orchestrator::Orchestrator;
use provider::NativeProvider;
use support::{fs::local::LocalFileSystem, process::os::OsProcessManager};
use zombienet_sdk::{NetworkConfig, NetworkConfigExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = NetworkConfig::load_from_toml("./crates/examples/examples/0001-simple.toml")
.expect("errored?");
let network = NetworkConfig::load_from_toml("./crates/examples/examples/0001-simple.toml")
.expect("errored?")
.spawn_native()
.await?;

let fs = LocalFileSystem;
let pm = OsProcessManager;
let provider = NativeProvider::new(fs.clone(), pm);
let orchestrator = Orchestrator::new(fs, provider);
let network = orchestrator.spawn(config).await?;
println!("🚀🚀🚀🚀 network deployed");

let client = network
Expand Down
2 changes: 1 addition & 1 deletion crates/examples/examples/small_network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use configuration::NetworkConfigBuilder;
use zombienet_sdk::NetworkConfigBuilder;

fn main() {
let config = NetworkConfigBuilder::new()
Expand Down
18 changes: 6 additions & 12 deletions crates/examples/examples/small_network_with_default.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use configuration::NetworkConfigBuilder;
use orchestrator::{AddNodeOpts, Orchestrator};
use provider::NativeProvider;
use support::{fs::local::LocalFileSystem, process::os::OsProcessManager};
use zombienet_sdk::{AddNodeOpts, NetworkConfigBuilder, NetworkConfigExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = NetworkConfigBuilder::new()
let mut network = NetworkConfigBuilder::new()
.with_relaychain(|r| {
r.with_chain("rococo-local")
.with_default_command("polkadot")
Expand All @@ -18,13 +15,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_collator(|n| n.with_name("collator").with_command("polkadot-parachain"))
})
.build()
.unwrap();
.unwrap()
.spawn_native()
.await?;

let fs = LocalFileSystem;
let pm = OsProcessManager;
let provider = NativeProvider::new(fs.clone(), pm);
let orchestrator = Orchestrator::new(fs, provider);
let mut network = orchestrator.spawn(config).await?;
println!("🚀🚀🚀🚀 network deployed");
// add a new node
let opts = AddNodeOpts {
Expand All @@ -36,7 +30,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// TODO: add check to ensure if unique
network.add_node("new1", opts, None).await?;

// Example of some opertions that you can do
// Example of some operations that you can do
// with `nodes` (e.g pause, resume, restart)

// Get a ref to the node
Expand Down
18 changes: 6 additions & 12 deletions crates/examples/examples/small_network_with_para.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::time::Duration;

use configuration::{NetworkConfigBuilder, RegistrationStrategy};
use orchestrator::{AddNodeOpts, Orchestrator};
use provider::NativeProvider;
use support::{fs::local::LocalFileSystem, process::os::OsProcessManager};
use zombienet_sdk::{AddNodeOpts, NetworkConfigBuilder, NetworkConfigExt, RegistrationStrategy};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = NetworkConfigBuilder::new()
let mut network = NetworkConfigBuilder::new()
.with_relaychain(|r| {
r.with_chain("rococo-local")
.with_default_command("polkadot")
Expand All @@ -21,13 +18,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_collator(|n| n.with_name("collator").with_command("polkadot-parachain"))
})
.build()
.unwrap();
.unwrap()
.spawn_native()
.await?;

let fs = LocalFileSystem;
let pm = OsProcessManager;
let provider = NativeProvider::new(fs.clone(), pm);
let orchestrator = Orchestrator::new(fs, provider);
let mut network = orchestrator.spawn(config).await?;
println!("🚀🚀🚀🚀 network deployed");
// add a new node
let opts = AddNodeOpts {
Expand All @@ -41,7 +35,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

tokio::time::sleep(Duration::from_secs(2)).await;

// Example of some opertions that you can do
// Example of some operations that you can do
// with `nodes` (e.g pause, resume, restart)

tokio::time::sleep(Duration::from_secs(10)).await;
Expand Down
4 changes: 2 additions & 2 deletions crates/orchestrator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// TODO(Javier): Remove when we implement the logic in the orchestrator to spawn with the provider.
#![allow(dead_code)]

mod errors;
pub mod errors;
mod generators;
mod network;
pub mod network;
mod network_helper;
mod network_spec;
mod shared;
Expand Down
21 changes: 15 additions & 6 deletions crates/orchestrator/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,22 @@ impl<T: FileSystem> Network<T> {
// deregister and stop the collator?
// remove_parachain()

pub fn get_node(&self, node_name: impl Into<String>) -> Result<&NetworkNode, anyhow::Error> {
let node_name = node_name.into();
if let Some(node) = self.nodes_by_name.get(&node_name) {
Ok(node)
} else {
Err(anyhow::anyhow!("can't find the node!"))
pub fn get_node(&self, name: impl Into<String>) -> Result<&NetworkNode, anyhow::Error> {
let name = &name.into();
if let Some(node) = self.nodes_by_name.get(name) {
return Ok(node);
}

let list = self
.nodes_by_name
.keys()
.cloned()
.collect::<Vec<_>>()
.join(", ");

Err(anyhow::anyhow!(
"can't find node with name: {name:?}, should be one of {list}"
))
}

pub fn nodes(&self) -> Vec<&NetworkNode> {
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub trait ProviderNamespace {
async fn static_setup(&self) -> Result<(), ProviderError>;
}

pub type DynNamespace = Arc<dyn ProviderNamespace>;
pub type DynNamespace = Arc<dyn ProviderNamespace + Send + Sync>;

type ExecutionResult = Result<String, (ExitStatus, String)>;

Expand Down
16 changes: 16 additions & 0 deletions crates/sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "zombienet-sdk"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
configuration = { path = "../configuration" }
orchestrator = { path = "../orchestrator" }
provider = { path = "../provider" }
support = { path = "../support" }
async-trait = { workspace = true }
tokio = { workspace = true }
futures = { workspace = true }
subxt = { workspace = true }
31 changes: 31 additions & 0 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use async_trait::async_trait;
pub use configuration::{NetworkConfig, NetworkConfigBuilder, RegistrationStrategy};
pub use orchestrator::{errors::OrchestratorError, network::Network, AddNodeOpts, Orchestrator};
use provider::NativeProvider;
use support::{fs::local::LocalFileSystem, process::os::OsProcessManager};

#[async_trait]
pub trait NetworkConfigExt {
/// Spawns a network using the native provider.
///
/// # Example:
/// ```rust
/// # use zombienet_sdk::{NetworkConfig, NetworkConfigExt};
/// # async fn example() -> Result<(), zombienet_sdk::OrchestratorError> {
/// let network = NetworkConfig::load_from_toml("config.toml")?
/// .spawn_native()
/// .await?;
/// # Ok(())
/// # }
/// ```
async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
}

#[async_trait]
impl NetworkConfigExt for NetworkConfig {
async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
let provider = NativeProvider::new(LocalFileSystem {}, OsProcessManager {});
let orchestrator = Orchestrator::new(LocalFileSystem {}, provider);
orchestrator.spawn(self).await
}
}