Skip to content

Commit

Permalink
genesis: add ability to pass in rng to SwarmConfig::build
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill authored and bors-libra committed Aug 12, 2021
1 parent 10a1f59 commit f4a3a5f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions config/management/genesis/src/config_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ impl FullnodeBuilder {
}

impl BuildSwarm for FullnodeBuilder {
fn build_swarm(&self) -> anyhow::Result<(Vec<NodeConfig>, Ed25519PrivateKey)> {
fn build_swarm<R>(&self, _rng: R) -> anyhow::Result<(Vec<NodeConfig>, Ed25519PrivateKey)>
where
R: ::rand::RngCore + ::rand::CryptoRng,
{
let configs = match self.build_type {
FullnodeType::ValidatorFullnode => self.build_vfn(),
FullnodeType::PublicFullnode(num_nodes) => self.build_public_fn(num_nodes),
Expand All @@ -126,7 +129,7 @@ pub fn test_config() -> (NodeConfig, Ed25519PrivateKey) {
diem_framework_releases::current_module_blobs().to_vec(),
)
.template(NodeConfig::default_for_validator());
let (mut configs, key) = builder.build_swarm().unwrap();
let (mut configs, key) = builder.build_swarm(rand::rngs::OsRng).unwrap();

let mut config = configs.swap_remove(0);
config.set_data_dir(path.path().to_path_buf());
Expand Down
23 changes: 18 additions & 5 deletions config/management/genesis/src/swarm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use std::{

pub trait BuildSwarm {
/// Generate the configs for a swarm
fn build_swarm(&self) -> Result<(Vec<NodeConfig>, Ed25519PrivateKey)>;
fn build_swarm<R>(&self, rng: R) -> Result<(Vec<NodeConfig>, Ed25519PrivateKey)>
where
R: ::rand::RngCore + ::rand::CryptoRng;
}

pub struct SwarmConfig {
Expand All @@ -27,8 +29,12 @@ pub struct SwarmConfig {
}

impl SwarmConfig {
pub fn build<T: BuildSwarm>(config_builder: &T, output_dir: &Path) -> Result<Self> {
let (mut configs, diem_root_key) = config_builder.build_swarm()?;
pub fn build_with_rng<T, R>(config_builder: &T, output_dir: &Path, rng: R) -> Result<Self>
where
T: BuildSwarm,
R: ::rand::RngCore + ::rand::CryptoRng,
{
let (mut configs, diem_root_key) = config_builder.build_swarm(rng)?;
let mut config_files = vec![];

for (index, config) in configs.iter_mut().enumerate() {
Expand Down Expand Up @@ -60,11 +66,18 @@ impl SwarmConfig {
waypoint: configs[0].base.waypoint.waypoint(),
})
}

pub fn build<T: BuildSwarm>(config_builder: &T, output_dir: &Path) -> Result<Self> {
Self::build_with_rng(config_builder, output_dir, rand::rngs::OsRng)
}
}

impl BuildSwarm for ValidatorBuilder {
fn build_swarm(&self) -> Result<(Vec<NodeConfig>, Ed25519PrivateKey)> {
let (root_keys, validators) = self.clone().build(rand::rngs::OsRng)?;
fn build_swarm<R>(&self, rng: R) -> Result<(Vec<NodeConfig>, Ed25519PrivateKey)>
where
R: ::rand::RngCore + ::rand::CryptoRng,
{
let (root_keys, validators) = self.clone().build(rng)?;

Ok((
validators.into_iter().map(|v| v.config).collect(),
Expand Down
1 change: 1 addition & 0 deletions diem-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = "2018"
fail = "0.4.0"
futures = "0.3.12"
jemallocator = { version = "0.3.2", features = ["profiling", "unprefixed_malloc_on_supported_platforms"] }
rand = "0.8.3"
structopt = "0.3.21"
tokio = { version = "1.8.1", features = ["full"] }
tokio-stream = "0.1.4"
Expand Down
8 changes: 6 additions & 2 deletions diem-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ pub fn start(config: &NodeConfig, log_file: Option<PathBuf>) {
}
}

pub fn load_test_environment(config_path: Option<PathBuf>, random_ports: bool) {
pub fn load_test_environment<R>(config_path: Option<PathBuf>, random_ports: bool, rng: R)
where
R: ::rand::RngCore + ::rand::CryptoRng,
{
// Either allocate a temppath or reuse the passed in path and make sure the directory exists
let config_temp_path = diem_temppath::TempPath::new();
let config_path = config_path.unwrap_or_else(|| config_temp_path.as_ref().to_path_buf());
Expand All @@ -114,7 +117,8 @@ pub fn load_test_environment(config_path: Option<PathBuf>, random_ports: bool) {
.template(template)
.randomize_first_validator_ports(random_ports);
let test_config =
diem_genesis_tool::swarm_config::SwarmConfig::build(&builder, &config_path).unwrap();
diem_genesis_tool::swarm_config::SwarmConfig::build_with_rng(&builder, &config_path, rng)
.unwrap();

// Prepare log file since we cannot automatically route logs to stderr
let mut log_file = config_path.clone();
Expand Down
2 changes: 1 addition & 1 deletion diem-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {

if args.test {
println!("Entering test mode, this should never be used in production!");
diem_node::load_test_environment(args.config, args.random_ports);
diem_node::load_test_environment(args.config, args.random_ports, rand::rngs::OsRng);
} else {
let config = NodeConfig::load(args.config.unwrap()).expect("Failed to load node config");
println!("Using node config {:?}", &config);
Expand Down

0 comments on commit f4a3a5f

Please sign in to comment.