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    ) -> Result<(), GeneratorError>
68    where
69        T: FileSystem,
70    {
71        let (cmd, custom_args) = match &self.build_option {
72            ParaArtifactBuildOption::Path(path) => {
73                let t = TransferedFile::new(PathBuf::from(path), artifact_path.as_ref().into());
74                scoped_fs.copy_files(vec![&t]).await?;
75                self.artifact_path = Some(artifact_path.as_ref().into());
76                return Ok(()); // work done!
77            },
78            ParaArtifactBuildOption::Command(cmd) => (cmd, &vec![]),
79            ParaArtifactBuildOption::CommandWithCustomArgs(cmd_with_custom_args) => {
80                (
81                    &cmd_with_custom_args.cmd().as_str().to_string(),
82                    cmd_with_custom_args.args(),
83                )
84                // (cmd.cmd_as_str().to_string(), cmd.1)
85            },
86        };
87
88        let generate_subcmd = match self.artifact_type {
89            ParaArtifactType::Wasm => "export-genesis-wasm",
90            ParaArtifactType::State => "export-genesis-state",
91        };
92
93        // TODO: replace uuid with para_id-random
94        let temp_name = format!("temp-{}-{}", generate_subcmd, Uuid::new_v4());
95        let mut args: Vec<String> = vec![generate_subcmd.into()];
96
97        let files_to_inject = if let Some(chain_spec_path) = chain_spec_path {
98            // TODO: we should get the full path from the scoped filesystem
99            let chain_spec_path_local = format!(
100                "{}/{}",
101                ns.base_dir().to_string_lossy(),
102                chain_spec_path.as_ref().to_string_lossy()
103            );
104            // Remote path to be injected
105            let chain_spec_path_in_pod = format!(
106                "{}/{}",
107                NODE_CONFIG_DIR,
108                chain_spec_path.as_ref().to_string_lossy()
109            );
110            // Path in the context of the node, this can be different in the context of the providers (e.g native)
111            let chain_spec_path_in_args = if ns.capabilities().prefix_with_full_path {
112                // In native
113                format!(
114                    "{}/{}{}",
115                    ns.base_dir().to_string_lossy(),
116                    &temp_name,
117                    &chain_spec_path_in_pod
118                )
119            } else {
120                chain_spec_path_in_pod.clone()
121            };
122
123            args.push("--chain".into());
124            args.push(chain_spec_path_in_args);
125
126            for custom_arg in custom_args {
127                match custom_arg {
128                    configuration::types::Arg::Flag(flag) => {
129                        args.push(flag.into());
130                    },
131                    configuration::types::Arg::Option(flag, flag_value) => {
132                        args.push(flag.into());
133                        args.push(flag_value.into());
134                    },
135                    configuration::types::Arg::Array(flag, values) => {
136                        args.push(flag.into());
137                        values.iter().for_each(|v| args.push(v.into()));
138                    },
139                }
140            }
141
142            vec![TransferedFile::new(
143                chain_spec_path_local,
144                chain_spec_path_in_pod,
145            )]
146        } else {
147            vec![]
148        };
149
150        let artifact_path_ref = artifact_path.as_ref();
151        let generate_command = GenerateFileCommand::new(cmd.as_str(), artifact_path_ref).args(args);
152        let options = GenerateFilesOptions::with_files(
153            vec![generate_command],
154            self.image.clone(),
155            &files_to_inject,
156        )
157        .temp_name(temp_name);
158        ns.generate_files(options).await?;
159        self.artifact_path = Some(artifact_path_ref.into());
160
161        Ok(())
162    }
163}