zombienet_sdk/
lib.rs

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