Skip to content

Commit

Permalink
config: introduce ability to load genesis from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed May 31, 2022
1 parent ad19e6a commit af867e8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/sui-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ base64ct = { version = "1.5.0", features = ["alloc"] }
rand = "0.7.3"
dirs = "4.0.0"
multiaddr = "0.14.0"
once_cell = "1.11.0"
debug-ignore = { version = "1.0.2", features = ["serde"] }
tracing = "0.1.34"

Expand All @@ -28,3 +29,6 @@ sui-framework = { path = "../sui-framework" }
sui-adapter = { path = "../sui-adapter" }
sui-types = { path = "../sui-types" }
workspace-hack = { path = "../workspace-hack"}

[dev-dependencies]
tempfile = "3.3.0"
2 changes: 1 addition & 1 deletion crates/sui-config/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
json_rpc_address: utils::available_local_socket_address(),
consensus_config: Some(consensus_config),
committee_config: committee_config.clone(),
genesis: genesis.clone(),
genesis: crate::node::Genesis::new(genesis.clone()),
}
})
.collect();
Expand Down
99 changes: 96 additions & 3 deletions crates/sui-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::genesis;
use crate::Config;
use anyhow::Result;
use debug_ignore::DebugIgnore;
use multiaddr::Multiaddr;
use narwhal_config::Committee as ConsensusCommittee;
Expand All @@ -27,7 +28,7 @@ pub struct NodeConfig {
pub consensus_config: Option<ConsensusConfig>,
pub committee_config: CommitteeConfig,

pub genesis: genesis::Genesis,
pub genesis: Genesis,
}

impl Config for NodeConfig {}
Expand Down Expand Up @@ -61,8 +62,8 @@ impl NodeConfig {
&self.committee_config
}

pub fn genesis(&self) -> &genesis::Genesis {
&self.genesis
pub fn genesis(&self) -> Result<&genesis::Genesis> {
self.genesis.genesis()
}
}

Expand Down Expand Up @@ -149,3 +150,95 @@ impl ValidatorInfo {
&self.network_address
}
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct Genesis {
#[serde(flatten)]
location: GenesisLocation,

#[serde(skip)]
genesis: once_cell::sync::OnceCell<genesis::Genesis>,
}

impl Genesis {
pub fn new(genesis: genesis::Genesis) -> Self {
Self {
location: GenesisLocation::InPlace { genesis },
genesis: Default::default(),
}
}

pub fn new_from_file<P: Into<PathBuf>>(path: P) -> Self {
Self {
location: GenesisLocation::File {
genesis_file_location: path.into(),
},
genesis: Default::default(),
}
}

fn genesis(&self) -> Result<&genesis::Genesis> {
match &self.location {
GenesisLocation::InPlace { genesis } => Ok(genesis),
GenesisLocation::File {
genesis_file_location,
} => self
.genesis
.get_or_try_init(|| genesis::Genesis::load(&genesis_file_location)),
}
}
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
enum GenesisLocation {
InPlace {
genesis: genesis::Genesis,
},
File {
#[serde(rename = "genesis-file-location")]
genesis_file_location: PathBuf,
},
}

#[cfg(test)]
mod tests {
use super::Genesis;
use crate::genesis;

#[test]
fn serialize_genesis_config_from_file() {
let g = Genesis::new_from_file("path/to/file");

let s = serde_yaml::to_string(&g).unwrap();
assert_eq!("---\ngenesis-file-location: path/to/file\n", s);
let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
assert_eq!(g, loaded_genesis);
}

#[test]
fn serialize_genesis_config_in_place() {
let g = Genesis::new(genesis::Genesis::get_default_genesis());

let mut s = serde_yaml::to_string(&g).unwrap();
let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
assert_eq!(g, loaded_genesis);

// If both in-place and file location are provided, prefer the in-place variant
s.push_str("\ngenesis-file-location: path/to/file");
let loaded_genesis: Genesis = serde_yaml::from_str(&s).unwrap();
assert_eq!(g, loaded_genesis);
}

#[test]
fn load_genesis_config_from_file() {
let file = tempfile::NamedTempFile::new().unwrap();
let genesis_config = Genesis::new_from_file(file.path());

let genesis = genesis::Genesis::get_default_genesis();
genesis.save(file.path()).unwrap();

let loaded_genesis = genesis_config.genesis().unwrap();
assert_eq!(&genesis, loaded_genesis);
}
}
2 changes: 1 addition & 1 deletion crates/sui-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl SuiNode {
store,
index_store,
checkpoint_store,
config.genesis(),
config.genesis()?,
)
.await,
);
Expand Down

0 comments on commit af867e8

Please sign in to comment.