zombienet_sdk/
lib.rs

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