setting up genesis upgrade-info.json file

This commit is contained in:
Jędrzej Stuczyński
2023-11-07 09:45:23 +00:00
parent 95e9a96ae1
commit 822dac8ee3
3 changed files with 28 additions and 30 deletions
+13 -4
View File
@@ -252,7 +252,7 @@ fn setup_genesis_binary(
if target.exists() {
// if there already exists a binary at the genesis location, see if it's the same one
let existing_bin_info = Daemon::new(target).get_build_information()?;
return if &existing_bin_info != daemon_info {
return if existing_bin_info != daemon_info {
Err(NymvisorError::DuplicateDaemonGenesisBinary {
daemon_name: config.daemon.name.clone(),
existing_info: Box::new(existing_bin_info),
@@ -264,9 +264,8 @@ fn setup_genesis_binary(
};
}
generate_and_save_genesis_upgrade_info(&config, daemon_info)?;
generate_and_save_genesis_upgrade_info(config, daemon_info)?;
// TODO: setup initial upgrade-info.json file
fs::copy(source_dir, &target).map_err(|source| NymvisorError::DaemonBinaryCopyFailure {
source_path: source_dir.to_path_buf(),
target_path: target,
@@ -312,6 +311,8 @@ fn generate_and_save_genesis_upgrade_info(
config: &Config,
genesis_info: BinaryBuildInformationOwned,
) -> Result<(), NymvisorError> {
info!("setting up the genesis upgrade-info.json");
let info = UpgradeInfo {
manual: true,
name: GENESIS_DIR.to_string(),
@@ -324,7 +325,15 @@ fn generate_and_save_genesis_upgrade_info(
};
let save_path = config.upgrade_info_filepath(&info.name);
info.save(save_path)
// if the upgrade info file already exists return an error since there is no associated binary
if save_path.exists() {
Err(NymvisorError::UpgradeInfoWithNoBinary {
name: info.name,
path: save_path,
})
} else {
info.save(save_path)
}
}
fn save_config(config: Config, env: &Env) -> Result<(), NymvisorError> {
+4 -2
View File
@@ -34,10 +34,9 @@ pub(crate) enum NymvisorError {
},
#[error(
"failed to load upgrade info for upgrade '{name}' using path '{}'. detailed message: {source}", path.display()
"failed to load upgrade info using path '{}'. detailed message: {source}", path.display()
)]
UpgradeInfoLoadFailure {
name: String,
path: PathBuf,
#[source]
source: io::Error,
@@ -53,6 +52,9 @@ pub(crate) enum NymvisorError {
source: io::Error,
},
#[error("there seem to be a upgrade-info.json file present without the associated binary for upgrade '{name}' at path {}", path.display())]
UpgradeInfoWithNoBinary { name: String, path: PathBuf },
#[error("failed to initialise the path '{}': {source}", path.display())]
PathInitFailure {
path: PathBuf,
+11 -24
View File
@@ -6,6 +6,7 @@ use nym_bin_common::build_information::BinaryBuildInformationOwned;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io;
use std::path::Path;
use time::OffsetDateTime;
@@ -127,8 +128,16 @@ impl UpgradeInfo {
}
pub(crate) fn try_load<P: AsRef<Path>>(path: P) -> Result<Self, NymvisorError> {
let file = std::fs::File::open(path)?;
serde_json::from_reader(file).map_err(Into::into)
let path = path.as_ref();
std::fs::File::open(path)
.and_then(|file| {
serde_json::from_reader(file)
.map_err(|serde_json_err| io::Error::new(io::ErrorKind::Other, serde_json_err))
})
.map_err(|source| NymvisorError::UpgradeInfoLoadFailure {
path: path.to_path_buf(),
source,
})
}
}
@@ -143,25 +152,3 @@ pub struct UpgradeHistoryEntry {
performed_at: OffsetDateTime,
info: UpgradeInfo,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn foo() {
let a = UpgradeInfo {
manual: false,
name: "".to_string(),
notes: "".to_string(),
publish_date: None,
version: "".to_string(),
platforms: Default::default(),
upgrade_time: OffsetDateTime::now_utc(),
binary_details: None,
};
let aa = serde_json::to_string_pretty(&a).unwrap();
println!("{aa}")
}
}