Skip to main content

zombienet_sdk/
lib.rs

1use std::path::PathBuf;
2
3use async_trait::async_trait;
4pub use configuration::{
5    json_patch,
6    types::{Arg, AssetLocation},
7    Buildable, CustomProcess, CustomProcessBuilder, Dependency, DependencyCondition,
8    GlobalSettings, GlobalSettingsBuilder, NamedPort, NetworkConfig, NetworkConfigBuilder,
9    ReadyCheck, ReadyTarget, RegistrationStrategy,
10};
11pub use orchestrator::{
12    errors::OrchestratorError,
13    network::{
14        jamchain::Jamchain,
15        node::{
16            CustomProcessNode, CustomProcessState, JamNetworkNode, NetworkNode, NodeKind,
17            ProcessPort, SpawnedNode,
18        },
19        Network,
20    },
21    sc_chain_spec, AddCollatorOptions, AddNodeOptions, Orchestrator,
22};
23pub use provider::types::{ExecutionResult, RunScriptOptions};
24
25/// Generator that could be handy from outside
26pub mod generators {
27    pub use orchestrator::generators::{
28        core_assignment, errors, generate_node_bootnode_addr, generate_node_identity,
29        generate_node_keys,
30    };
31}
32
33/// Helpers used for interact with the network
34pub mod tx_helper {
35    pub use orchestrator::{
36        network::chain_upgrade::ChainUpgrade,
37        shared::types::RuntimeUpgradeOptions,
38        tx_helper::{parachain, runtime_upgrade},
39    };
40}
41
42use provider::{DockerProvider, KubernetesProvider, NativeProvider};
43pub use support::fs::local::LocalFileSystem;
44
45pub mod environment;
46pub mod snapshot;
47pub use snapshot::{Bundle, BundleBuilder, SnapshotManifest};
48pub const PROVIDERS: [&str; 3] = ["k8s", "native", "docker"];
49
50// re-export subxt
51pub use subxt;
52pub use subxt_signer;
53
54#[async_trait]
55pub trait NetworkConfigExt {
56    /// Spawns a network using the native or k8s provider.
57    ///
58    /// # Example:
59    /// ```rust
60    /// # use zombienet_sdk::{NetworkConfig, NetworkConfigExt};
61    /// # async fn example() -> Result<(), zombienet_sdk::OrchestratorError> {
62    /// let network = NetworkConfig::load_from_toml("config.toml")?
63    ///     .spawn_native()
64    ///     .await?;
65    /// # Ok(())
66    /// # }
67    /// ```
68    async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
69    async fn spawn_k8s(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
70    /// Spawns a network using the k8s provider into `namespace`, which must already
71    /// exist. It is left in place if spawning fails; `Network::destroy` deletes it.
72    async fn spawn_k8s_in_namespace(
73        self,
74        namespace: &str,
75    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
76    async fn spawn_docker(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
77}
78
79#[async_trait]
80pub trait AttachToLive {
81    /// Attaches to a running live network using the native, docker or k8s provider.
82    ///
83    /// # Example:
84    /// ```rust
85    /// # use zombienet_sdk::{AttachToLive, AttachToLiveNetwork};
86    /// # use std::path::PathBuf;
87    /// # async fn example() -> Result<(), zombienet_sdk::OrchestratorError> {
88    /// let zombie_json_path = PathBuf::from("some/path/zombie.json");
89    /// let network = AttachToLiveNetwork::attach_native(zombie_json_path).await?;
90    /// # Ok(())
91    /// # }
92    /// ```
93    async fn attach_native(
94        zombie_json_path: PathBuf,
95    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
96    async fn attach_k8s(
97        zombie_json_path: PathBuf,
98    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
99    async fn attach_docker(
100        zombie_json_path: PathBuf,
101    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
102
103    /// Attaches to a running live network using the k8s provider, from in-memory
104    /// `zombie.json` content. Its `local_base_dir` is used as-is.
105    ///
106    /// # Example:
107    /// ```rust
108    /// # use zombienet_sdk::{AttachToLive, AttachToLiveNetwork};
109    /// # async fn example(zombie_json: serde_json::Value) -> Result<(), zombienet_sdk::OrchestratorError> {
110    /// let network = AttachToLiveNetwork::attach_k8s_from_json(zombie_json).await?;
111    /// # Ok(())
112    /// # }
113    /// ```
114    async fn attach_k8s_from_json(
115        zombie_json: serde_json::Value,
116    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
117}
118
119#[async_trait]
120impl NetworkConfigExt for NetworkConfig {
121    async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
122        let filesystem = LocalFileSystem;
123        let provider = NativeProvider::new(filesystem.clone());
124        let orchestrator = Orchestrator::new(filesystem, provider);
125        orchestrator.spawn(self).await
126    }
127
128    async fn spawn_k8s(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
129        let filesystem = LocalFileSystem;
130        let provider = KubernetesProvider::new(filesystem.clone()).await;
131        let orchestrator = Orchestrator::new(filesystem, provider);
132        orchestrator.spawn(self).await
133    }
134
135    async fn spawn_k8s_in_namespace(
136        self,
137        namespace: &str,
138    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
139        let filesystem = LocalFileSystem;
140        let provider = KubernetesProvider::new_in_namespace(filesystem.clone(), namespace).await;
141        let orchestrator = Orchestrator::new(filesystem, provider);
142        orchestrator.spawn(self).await
143    }
144
145    async fn spawn_docker(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
146        let filesystem = LocalFileSystem;
147        let provider = DockerProvider::new(filesystem.clone()).await;
148        let orchestrator = Orchestrator::new(filesystem, provider);
149        orchestrator.spawn(self).await
150    }
151}
152
153pub struct AttachToLiveNetwork;
154
155#[async_trait]
156impl AttachToLive for AttachToLiveNetwork {
157    async fn attach_native(
158        zombie_json_path: PathBuf,
159    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
160        let filesystem = LocalFileSystem;
161        let provider = NativeProvider::new(filesystem.clone());
162        let orchestrator = Orchestrator::new(filesystem, provider);
163        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
164    }
165
166    async fn attach_k8s(
167        zombie_json_path: PathBuf,
168    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
169        let filesystem = LocalFileSystem;
170        let provider = KubernetesProvider::new(filesystem.clone()).await;
171        let orchestrator = Orchestrator::new(filesystem, provider);
172        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
173    }
174
175    async fn attach_docker(
176        zombie_json_path: PathBuf,
177    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
178        let filesystem = LocalFileSystem;
179        let provider = DockerProvider::new(filesystem.clone()).await;
180        let orchestrator = Orchestrator::new(filesystem, provider);
181        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
182    }
183
184    async fn attach_k8s_from_json(
185        zombie_json: serde_json::Value,
186    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
187        let filesystem = LocalFileSystem;
188        let provider = KubernetesProvider::new(filesystem.clone()).await;
189        let orchestrator = Orchestrator::new(filesystem, provider);
190        orchestrator.attach_to_live_from_json(&zombie_json).await
191    }
192}