zombienet_sdk/
lib.rs

1use std::path::PathBuf;
2
3use async_trait::async_trait;
4pub use configuration::{
5    types::{Arg, AssetLocation},
6    CustomProcess, CustomProcessBuilder, GlobalSettings, GlobalSettingsBuilder, NetworkConfig,
7    NetworkConfigBuilder, RegistrationStrategy, WithRelaychain,
8};
9pub use orchestrator::{
10    errors::OrchestratorError,
11    network::{node::NetworkNode, Network},
12    sc_chain_spec, AddCollatorOptions, AddNodeOptions, Orchestrator,
13};
14pub use provider::types::{ExecutionResult, RunScriptOptions};
15
16// Helpers used for interact with the network
17pub mod tx_helper {
18    pub use orchestrator::{
19        network::chain_upgrade::ChainUpgrade,
20        shared::types::RuntimeUpgradeOptions,
21        tx_helper::{parachain, runtime_upgrade},
22    };
23}
24
25use provider::{DockerProvider, KubernetesProvider, NativeProvider};
26pub use support::fs::local::LocalFileSystem;
27
28pub mod environment;
29pub const PROVIDERS: [&str; 3] = ["k8s", "native", "docker"];
30
31// re-export subxt
32pub use subxt;
33pub use subxt_signer;
34
35#[async_trait]
36pub trait NetworkConfigExt {
37    /// Spawns a network using the native or k8s provider.
38    ///
39    /// # Example:
40    /// ```rust
41    /// # use zombienet_sdk::{NetworkConfig, NetworkConfigExt};
42    /// # async fn example() -> Result<(), zombienet_sdk::OrchestratorError> {
43    /// let network = NetworkConfig::load_from_toml("config.toml")?
44    ///     .spawn_native()
45    ///     .await?;
46    /// # Ok(())
47    /// # }
48    /// ```
49    async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
50    async fn spawn_k8s(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
51    async fn spawn_docker(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
52}
53
54#[async_trait]
55pub trait AttachToLive {
56    /// Attaches to a running live network using the native, docker or k8s provider.
57    ///
58    /// # Example:
59    /// ```rust
60    /// # use zombienet_sdk::{AttachToLive, AttachToLiveNetwork};
61    /// # use std::path::PathBuf;
62    /// # async fn example() -> Result<(), zombienet_sdk::OrchestratorError> {
63    /// let zombie_json_path = PathBuf::from("some/path/zombie.json");
64    /// let network = AttachToLiveNetwork::attach_native(zombie_json_path).await?;
65    /// # Ok(())
66    /// # }
67    /// ```
68    async fn attach_native(
69        zombie_json_path: PathBuf,
70    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
71    async fn attach_k8s(
72        zombie_json_path: PathBuf,
73    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
74    async fn attach_docker(
75        zombie_json_path: PathBuf,
76    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
77}
78
79#[async_trait]
80impl NetworkConfigExt for NetworkConfig {
81    async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
82        let filesystem = LocalFileSystem;
83        let provider = NativeProvider::new(filesystem.clone());
84        let orchestrator = Orchestrator::new(filesystem, provider);
85        orchestrator.spawn(self).await
86    }
87
88    async fn spawn_k8s(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
89        let filesystem = LocalFileSystem;
90        let provider = KubernetesProvider::new(filesystem.clone()).await;
91        let orchestrator = Orchestrator::new(filesystem, provider);
92        orchestrator.spawn(self).await
93    }
94
95    async fn spawn_docker(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
96        let filesystem = LocalFileSystem;
97        let provider = DockerProvider::new(filesystem.clone()).await;
98        let orchestrator = Orchestrator::new(filesystem, provider);
99        orchestrator.spawn(self).await
100    }
101}
102
103pub struct AttachToLiveNetwork;
104
105#[async_trait]
106impl AttachToLive for AttachToLiveNetwork {
107    async fn attach_native(
108        zombie_json_path: PathBuf,
109    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
110        let filesystem = LocalFileSystem;
111        let provider = NativeProvider::new(filesystem.clone());
112        let orchestrator = Orchestrator::new(filesystem, provider);
113        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
114    }
115
116    async fn attach_k8s(
117        zombie_json_path: PathBuf,
118    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
119        let filesystem = LocalFileSystem;
120        let provider = KubernetesProvider::new(filesystem.clone()).await;
121        let orchestrator = Orchestrator::new(filesystem, provider);
122        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
123    }
124
125    async fn attach_docker(
126        zombie_json_path: PathBuf,
127    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
128        let filesystem = LocalFileSystem;
129        let provider = DockerProvider::new(filesystem.clone()).await;
130        let orchestrator = Orchestrator::new(filesystem, provider);
131        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
132    }
133}