Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
fix: subnet genesis file error (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko authored Nov 9, 2023
1 parent 4db1a63 commit 4799cce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
22 changes: 17 additions & 5 deletions crates/topos/src/components/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,23 @@ pub(crate) async fn handle_command(
);

// Load genesis pointed by the local config
let genesis = Genesis::new(
home.join("subnet")
.join(config.base.subnet.clone())
.join("genesis.json"),
);
let genesis_file_path = home
.join("subnet")
.join(config.base.subnet.clone())
.join("genesis.json");
let genesis = match Genesis::new(genesis_file_path.clone()) {
Ok(genesis) => genesis,
Err(_) => {
error!(
"Could not load genesis.json file on path {} \n Please make sure to have \
a valid genesis.json file for your subnet in the {}/subnet/{} folder.",
genesis_file_path.display(),
home.display(),
&config.base.subnet
);
std::process::exit(1);
}
};

// Get secrets
let keys = match &config.base.secrets_config {
Expand Down
13 changes: 8 additions & 5 deletions crates/topos/src/config/genesis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rlp::Rlp;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::str::FromStr;
use std::{fs, path::PathBuf};
Expand All @@ -18,19 +17,23 @@ pub struct Genesis {
pub json: Value,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Failed to parse validators")]
ParseValidators,
#[error("Invalid genesis file on path {0}: {1}")]
InvalidGenesisFile(String, String),
}

impl Genesis {
pub fn new(path: PathBuf) -> Self {
pub fn new(path: PathBuf) -> Result<Self, Error> {
info!("Reading subnet genesis file {}", path.display());
let genesis_file = fs::File::open(&path).expect("opened file");
let genesis_file = fs::File::open(&path)
.map_err(|e| Error::InvalidGenesisFile(path.display().to_string(), e.to_string()))?;

let json: Value = serde_json::from_reader(genesis_file).expect("genesis json parsed");

Self { path, json }
Ok(Self { path, json })
}

// Considered as being the set of premined addresses for now
Expand Down
1 change: 1 addition & 0 deletions crates/topos/src/config/genesis/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ macro_rules! test_case {
#[once]
pub fn genesis() -> Genesis {
Genesis::new(test_case!("genesis-example.json").into())
.expect("Expected valid test genesis file")
}

#[rstest]
Expand Down

0 comments on commit 4799cce

Please sign in to comment.