Skip to content

Commit

Permalink
genesis: require passing in an rng when using ValidatorBuilder
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 844c4a7 commit 10a1f59
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion config/management/genesis/src/swarm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl SwarmConfig {

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

Ok((
validators.into_iter().map(|v| v.config).collect(),
Expand Down
28 changes: 14 additions & 14 deletions config/management/genesis/src/validator_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ pub struct RootKeys {
}

impl RootKeys {
pub fn generate<R>(rng: &mut R) -> Self
pub fn generate<R>(mut rng: R) -> Self
where
R: ::rand::RngCore + ::rand::CryptoRng,
{
// TODO use distinct keys for diem root and treasury
// let root_key = Ed25519PrivateKey::generate(rng);
// let treasury_compliance_key = Ed25519PrivateKey::generate(rng);
let key = Ed25519PrivateKey::generate(rng).to_bytes();
let key = Ed25519PrivateKey::generate(&mut rng).to_bytes();
let root_key = Ed25519PrivateKey::try_from(key.as_ref()).unwrap();
let treasury_compliance_key = Ed25519PrivateKey::try_from(key.as_ref()).unwrap();

Expand Down Expand Up @@ -189,10 +189,10 @@ impl ValidatorBuilder {
self
}

pub fn build(mut self) -> Result<(RootKeys, Vec<ValidatorConfig>)> {
// TODO Pass this in
let mut rng = rand::rngs::OsRng;

pub fn build<R>(mut self, mut rng: R) -> Result<(RootKeys, Vec<ValidatorConfig>)>
where
R: ::rand::RngCore + ::rand::CryptoRng,
{
// Canonicalize the config directory path
self.config_directory = self.config_directory.canonicalize()?;

Expand Down Expand Up @@ -254,7 +254,7 @@ impl ValidatorBuilder {
fn initialize_validator_config<R>(
&self,
index: usize,
rng: &mut R,
rng: R,
validator_network_address_encryption_key: NetworkAddressEncryptionKey,
validator_network_address_encryption_key_version: NetworkAddressEncryptionKeyVersion,
) -> Result<ValidatorConfig>
Expand Down Expand Up @@ -321,7 +321,7 @@ impl ValidatorBuilder {

fn initialize_validator_storage<R>(
validator: &ValidatorConfig,
rng: &mut R,
mut rng: R,
validator_network_address_encryption_key: NetworkAddressEncryptionKey,
validator_network_address_encryption_key_version: NetworkAddressEncryptionKeyVersion,
) -> Result<()>
Expand All @@ -331,22 +331,22 @@ impl ValidatorBuilder {
let mut storage = validator.storage();

// Set owner key and account address
storage.import_private_key(OWNER_KEY, Ed25519PrivateKey::generate(rng))?;
storage.import_private_key(OWNER_KEY, Ed25519PrivateKey::generate(&mut rng))?;
let owner_address =
diem_config::utils::validator_owner_account_from_name(validator.owner().as_bytes());
storage.set(OWNER_ACCOUNT, owner_address)?;

// Set operator key and account address
let operator_key = Ed25519PrivateKey::generate(rng);
let operator_key = Ed25519PrivateKey::generate(&mut rng);
let operator_address =
AuthenticationKey::ed25519(&Ed25519PublicKey::from(&operator_key)).derived_address();
storage.set(OPERATOR_ACCOUNT, operator_address)?;
storage.import_private_key(OPERATOR_KEY, operator_key)?;

storage.import_private_key(CONSENSUS_KEY, Ed25519PrivateKey::generate(rng))?;
storage.import_private_key(EXECUTION_KEY, Ed25519PrivateKey::generate(rng))?;
storage.import_private_key(FULLNODE_NETWORK_KEY, Ed25519PrivateKey::generate(rng))?;
storage.import_private_key(VALIDATOR_NETWORK_KEY, Ed25519PrivateKey::generate(rng))?;
storage.import_private_key(CONSENSUS_KEY, Ed25519PrivateKey::generate(&mut rng))?;
storage.import_private_key(EXECUTION_KEY, Ed25519PrivateKey::generate(&mut rng))?;
storage.import_private_key(FULLNODE_NETWORK_KEY, Ed25519PrivateKey::generate(&mut rng))?;
storage.import_private_key(VALIDATOR_NETWORK_KEY, Ed25519PrivateKey::generate(&mut rng))?;

// Initialize all other data in storage
storage.set(SAFETY_DATA, SafetyData::new(0, 0, 0, 0, None))?;
Expand Down
8 changes: 7 additions & 1 deletion testsuite/forge/src/backend/k8s/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::{Factory, Result, Swarm, Version};
use anyhow::format_err;
use rand::rngs::StdRng;
use std::{env, fs::File, io::Read, num::NonZeroUsize, path::PathBuf};
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -73,7 +74,12 @@ impl Factory for K8sFactory {
)))
}

fn launch_swarm(&self, _node_num: NonZeroUsize, _version: &Version) -> Result<Box<dyn Swarm>> {
fn launch_swarm(
&self,
_rng: &mut StdRng,
_node_num: NonZeroUsize,
_version: &Version,
) -> Result<Box<dyn Swarm>> {
let rt = Runtime::new().unwrap();
let swarm = rt
.block_on(K8sSwarm::new(
Expand Down
10 changes: 8 additions & 2 deletions testsuite/forge/src/backend/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{Factory, Result, Swarm, Version};
use rand::rngs::StdRng;
use std::{
collections::HashMap,
num::NonZeroUsize,
Expand Down Expand Up @@ -120,11 +121,16 @@ impl Factory for LocalFactory {
Box::new(self.versions.keys().cloned())
}

fn launch_swarm(&self, node_num: NonZeroUsize, version: &Version) -> Result<Box<dyn Swarm>> {
fn launch_swarm(
&self,
rng: &mut StdRng,
node_num: NonZeroUsize,
version: &Version,
) -> Result<Box<dyn Swarm>> {
let mut swarm = LocalSwarm::builder(self.versions.clone())
.number_of_validators(node_num)
.initial_version(version.clone())
.build()?;
.build(rng)?;
swarm.launch()?;

Ok(Box::new(swarm))
Expand Down
7 changes: 5 additions & 2 deletions testsuite/forge/src/backend/local/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ impl LocalSwarmBuilder {
self
}

pub fn build(self) -> Result<LocalSwarm> {
pub fn build<R>(self, rng: R) -> Result<LocalSwarm>
where
R: ::rand::RngCore + ::rand::CryptoRng,
{
let dir = if let Some(dir) = self.dir {
if dir.exists() {
fs::remove_dir_all(&dir)?;
Expand All @@ -132,7 +135,7 @@ impl LocalSwarmBuilder {
)
.num_validators(self.number_of_validators)
.template(self.template)
.build()?;
.build(rng)?;

// Get the initial version to start the nodes with, either the one provided or fallback to
// using the the latest version
Expand Down
8 changes: 7 additions & 1 deletion testsuite/forge/src/interface/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@

use super::{Swarm, Version};
use crate::Result;
use rand::rngs::StdRng;
use std::num::NonZeroUsize;

/// Trait used to represent a interface for constructing a launching new networks
pub trait Factory {
fn versions<'a>(&'a self) -> Box<dyn Iterator<Item = Version> + 'a>;

fn launch_swarm(&self, node_num: NonZeroUsize, version: &Version) -> Result<Box<dyn Swarm>>;
fn launch_swarm(
&self,
rng: &mut StdRng,
node_num: NonZeroUsize,
version: &Version,
) -> Result<Box<dyn Swarm>>;
}
8 changes: 5 additions & 3 deletions testsuite/forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ impl<'cfg, F: Factory> Forge<'cfg, F> {
if test_count > 0 {
let initial_version = self.initial_version();
let mut rng = ::rand::rngs::StdRng::from_seed(OsRng.gen());
let mut swarm = self
.factory
.launch_swarm(self.tests.initial_validator_count, &initial_version)?;
let mut swarm = self.factory.launch_swarm(
&mut rng,
self.tests.initial_validator_count,
&initial_version,
)?;

// Run PublicUsageTests
for test in self.filter_tests(self.tests.public_usage_tests.iter()) {
Expand Down

0 comments on commit 10a1f59

Please sign in to comment.