Skip to main content

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/// Generator that could be handy from outside
17pub mod generators {
18    pub use orchestrator::generators::{
19        core_assignment, errors, generate_node_bootnode_addr, generate_node_identity,
20        generate_node_keys,
21    };
22}
23
24/// Helpers used for interact with the network
25pub mod tx_helper {
26    pub use orchestrator::{
27        network::chain_upgrade::ChainUpgrade,
28        shared::types::RuntimeUpgradeOptions,
29        tx_helper::{parachain, runtime_upgrade},
30    };
31}
32
33use provider::{DockerProvider, KubernetesProvider, NativeProvider};
34pub use support::fs::local::LocalFileSystem;
35
36pub mod environment;
37pub mod snapshot;
38pub use snapshot::{Bundle, BundleBuilder, SnapshotManifest};
39pub const PROVIDERS: [&str; 3] = ["k8s", "native", "docker"];
40
41// re-export subxt
42pub use subxt;
43pub use subxt_signer;
44
45#[async_trait]
46pub trait NetworkConfigExt {
47    /// Spawns a network using the native or k8s provider.
48    ///
49    /// # Example:
50    /// ```rust
51    /// # use zombienet_sdk::{NetworkConfig, NetworkConfigExt};
52    /// # async fn example() -> Result<(), zombienet_sdk::OrchestratorError> {
53    /// let network = NetworkConfig::load_from_toml("config.toml")?
54    ///     .spawn_native()
55    ///     .await?;
56    /// # Ok(())
57    /// # }
58    /// ```
59    async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
60    async fn spawn_k8s(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
61    async fn spawn_docker(self) -> Result<Network<LocalFileSystem>, OrchestratorError>;
62}
63
64#[async_trait]
65pub trait AttachToLive {
66    /// Attaches to a running live network using the native, docker or k8s provider.
67    ///
68    /// # Example:
69    /// ```rust
70    /// # use zombienet_sdk::{AttachToLive, AttachToLiveNetwork};
71    /// # use std::path::PathBuf;
72    /// # async fn example() -> Result<(), zombienet_sdk::OrchestratorError> {
73    /// let zombie_json_path = PathBuf::from("some/path/zombie.json");
74    /// let network = AttachToLiveNetwork::attach_native(zombie_json_path).await?;
75    /// # Ok(())
76    /// # }
77    /// ```
78    async fn attach_native(
79        zombie_json_path: PathBuf,
80    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
81    async fn attach_k8s(
82        zombie_json_path: PathBuf,
83    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
84    async fn attach_docker(
85        zombie_json_path: PathBuf,
86    ) -> Result<Network<LocalFileSystem>, OrchestratorError>;
87}
88
89#[async_trait]
90impl NetworkConfigExt for NetworkConfig {
91    async fn spawn_native(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
92        let filesystem = LocalFileSystem;
93        let provider = NativeProvider::new(filesystem.clone());
94        let orchestrator = Orchestrator::new(filesystem, provider);
95        orchestrator.spawn(self).await
96    }
97
98    async fn spawn_k8s(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
99        let filesystem = LocalFileSystem;
100        let provider = KubernetesProvider::new(filesystem.clone()).await;
101        let orchestrator = Orchestrator::new(filesystem, provider);
102        orchestrator.spawn(self).await
103    }
104
105    async fn spawn_docker(self) -> Result<Network<LocalFileSystem>, OrchestratorError> {
106        let filesystem = LocalFileSystem;
107        let provider = DockerProvider::new(filesystem.clone()).await;
108        let orchestrator = Orchestrator::new(filesystem, provider);
109        orchestrator.spawn(self).await
110    }
111}
112
113pub struct AttachToLiveNetwork;
114
115#[async_trait]
116impl AttachToLive for AttachToLiveNetwork {
117    async fn attach_native(
118        zombie_json_path: PathBuf,
119    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
120        let filesystem = LocalFileSystem;
121        let provider = NativeProvider::new(filesystem.clone());
122        let orchestrator = Orchestrator::new(filesystem, provider);
123        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
124    }
125
126    async fn attach_k8s(
127        zombie_json_path: PathBuf,
128    ) -> 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.attach_to_live(zombie_json_path.as_ref()).await
133    }
134
135    async fn attach_docker(
136        zombie_json_path: PathBuf,
137    ) -> Result<Network<LocalFileSystem>, OrchestratorError> {
138        let filesystem = LocalFileSystem;
139        let provider = DockerProvider::new(filesystem.clone()).await;
140        let orchestrator = Orchestrator::new(filesystem, provider);
141        orchestrator.attach_to_live(zombie_json_path.as_ref()).await
142    }
143}