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