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