zombienet_orchestrator/generators/
para_artifact.rs

1use std::path::{Path, PathBuf};
2
3use configuration::types::CommandWithCustomArgs;
4use provider::{
5    constants::NODE_CONFIG_DIR,
6    types::{GenerateFileCommand, GenerateFilesOptions, TransferedFile},
7    DynNamespace,
8};
9use serde::{Deserialize, Serialize};
10use support::fs::FileSystem;
11use uuid::Uuid;
12
13use super::errors::GeneratorError;
14use crate::ScopedFilesystem;
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub(crate) enum ParaArtifactType {
18    Wasm,
19    State,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub(crate) enum ParaArtifactBuildOption {
24    Path(String),
25    Command(String),
26    CommandWithCustomArgs(CommandWithCustomArgs),
27}
28
29/// Parachain artifact (could be either the genesis state or genesis wasm)
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct ParaArtifact {
32    artifact_type: ParaArtifactType,
33    build_option: ParaArtifactBuildOption,
34    artifact_path: Option<PathBuf>,
35    // image to use for building the para artifact
36    image: Option<String>,
37}
38
39impl ParaArtifact {
40    pub(crate) fn new(
41        artifact_type: ParaArtifactType,
42        build_option: ParaArtifactBuildOption,
43    ) -> Self {
44        Self {
45            artifact_type,
46            build_option,
47            artifact_path: None,
48            image: None,
49        }
50    }
51
52    pub(crate) fn image(mut self, image: Option<String>) -> Self {
53        self.image = image;
54        self
55    }
56
57    pub(crate) fn artifact_path(&self) -> Option<&PathBuf> {
58        self.artifact_path.as_ref()
59    }
60
61    pub(crate) async fn build<'a, T>(
62        &mut self,
63        chain_spec_path: Option<impl AsRef<Path>>,
64        artifact_path: impl AsRef<Path>,
65        ns: &DynNamespace,
66        scoped_fs: &ScopedFilesystem<'a, T>,
67        maybe_output_path: Option<PathBuf>,
68    ) -> Result<(), GeneratorError>
69    where
70        T: FileSystem,
71    {
72        let (cmd, custom_args) = match &self.build_option {
73            ParaArtifactBuildOption::Path(path) => {
74                let t = TransferedFile::new(PathBuf::from(path), artifact_path.as_ref().into());
75                scoped_fs.copy_files(vec![&t]).await?;
76                self.artifact_path = Some(artifact_path.as_ref().into());
77                return Ok(()); // work done!
78            },
79            ParaArtifactBuildOption::Command(cmd) => (cmd, &vec![]),
80            ParaArtifactBuildOption::CommandWithCustomArgs(cmd_with_custom_args) => {
81                (
82                    &cmd_with_custom_args.cmd().as_str().to_string(),
83                    cmd_with_custom_args.args(),
84                )
85                // (cmd.cmd_as_str().to_string(), cmd.1)
86            },
87        };
88
89        let generate_subcmd = match self.artifact_type {
90            ParaArtifactType::Wasm => "export-genesis-wasm",
91            ParaArtifactType::State => "export-genesis-state",
92        };
93
94        // TODO: replace uuid with para_id-random
95        let temp_name = format!("temp-{}-{}", generate_subcmd, Uuid::new_v4());
96        let mut args: Vec<String> = vec![generate_subcmd.into()];
97
98        let files_to_inject = if let Some(chain_spec_path) = chain_spec_path {
99            // TODO: we should get the full path from the scoped filesystem
100            let chain_spec_path_local = format!(
101                "{}/{}",
102                ns.base_dir().to_string_lossy(),
103                chain_spec_path.as_ref().to_string_lossy()
104            );
105            // Remote path to be injected
106            let chain_spec_path_in_pod = format!(
107                "{}/{}",
108                NODE_CONFIG_DIR,
109                chain_spec_path.as_ref().to_string_lossy()
110            );
111            // Path in the context of the node, this can be different in the context of the providers (e.g native)
112            let chain_spec_path_in_args = if ns.capabilities().prefix_with_full_path {
113                // In native
114                format!(
115                    "{}/{}{}",
116                    ns.base_dir().to_string_lossy(),
117                    &temp_name,
118                    &chain_spec_path_in_pod
119                )
120            } else {
121                chain_spec_path_in_pod.clone()
122            };
123
124            args.push("--chain".into());
125            args.push(chain_spec_path_in_args);
126
127            for custom_arg in custom_args {
128                match custom_arg {
129                    configuration::types::Arg::Flag(flag) => {
130                        args.push(flag.into());
131                    },
132                    configuration::types::Arg::Option(flag, flag_value) => {
133                        args.push(flag.into());
134                        args.push(flag_value.into());
135                    },
136                    configuration::types::Arg::Array(flag, values) => {
137                        args.push(flag.into());
138                        values.iter().for_each(|v| args.push(v.into()));
139                    },
140                }
141            }
142
143            vec![TransferedFile::new(
144                chain_spec_path_local,
145                chain_spec_path_in_pod,
146            )]
147        } else {
148            vec![]
149        };
150
151        let artifact_path_ref = artifact_path.as_ref();
152        let generate_command = GenerateFileCommand::new(cmd.as_str(), artifact_path_ref).args(args);
153        let options = GenerateFilesOptions::with_files(
154            vec![generate_command],
155            self.image.clone(),
156            &files_to_inject,
157            maybe_output_path,
158        )
159        .temp_name(temp_name);
160        ns.generate_files(options).await?;
161        self.artifact_path = Some(artifact_path_ref.into());
162
163        Ok(())
164    }
165}