Skip to main content

tezos_smart_rollup_installer/
config.rs

1// SPDX-FileCopyrightText: 2023 TriliTech <contact@trili.tech>
2//
3// SPDX-License-Identifier: MIT
4
5use std::{ffi::OsString, path::Path};
6
7use std::fs::File;
8use tezos_smart_rollup_encoding::dac::PreimageHash;
9use tezos_smart_rollup_host::path::{OwnedPath, RefPath};
10use tezos_smart_rollup_installer_config::binary::owned::{
11    OwnedConfigInstruction, OwnedConfigProgram,
12};
13use tezos_smart_rollup_installer_config::yaml::{ConfigConversionError, YamlConfig};
14use thiserror::Error;
15
16#[derive(Debug, Error)]
17pub enum ConfigurationError {
18    #[error("Unable to read config file: {0}.")]
19    FileNotFound(std::io::Error),
20    #[error("Unable to parse config file: {0}.")]
21    ParseError(serde_yaml::Error),
22    #[error("Unable to convert config to a valid program: {0}.")]
23    YamlConfigInvalid(#[from] ConfigConversionError),
24}
25
26// Path that we write the kernel to, before upgrading.
27const PREPARE_KERNEL_PATH: RefPath = RefPath::assert_from(b"/installer/kernel/boot.wasm");
28
29// Path of currently running kernel.
30const KERNEL_BOOT_PATH: RefPath = RefPath::assert_from(b"/kernel/boot.wasm");
31
32pub fn create_installer_config(
33    root_hash: PreimageHash,
34    setup_file: Option<OsString>,
35) -> Result<OwnedConfigProgram, ConfigurationError> {
36    let mut reveal_instructions = vec![
37        OwnedConfigInstruction::reveal_instr(
38            root_hash,
39            OwnedPath::from(PREPARE_KERNEL_PATH),
40        ),
41        OwnedConfigInstruction::move_instr(
42            OwnedPath::from(PREPARE_KERNEL_PATH),
43            OwnedPath::from(KERNEL_BOOT_PATH),
44        ),
45    ];
46
47    let setup_program: OwnedConfigProgram = match setup_file {
48        None => OwnedConfigProgram(vec![]),
49        Some(setup_file) => {
50            let setup_file = File::open(Path::new(&setup_file))
51                .map_err(ConfigurationError::FileNotFound)?;
52            let yaml_config: YamlConfig = YamlConfig::from_reader(setup_file)
53                .map_err(ConfigurationError::ParseError)?;
54            yaml_config.try_into()?
55        }
56    };
57
58    reveal_instructions.extend(setup_program.0);
59
60    Ok(OwnedConfigProgram(reveal_instructions))
61}