diem_config/
generator.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8//! Convenience structs and functions for generating a random set of Diem ndoes
9//! without the genesis.blob.
10
11use crate::config::{
12    NetworkConfig, NodeConfig, Peer, PeerRole, PeerSet, TestConfig,
13    HANDSHAKE_VERSION,
14};
15use rand::{rngs::StdRng, SeedableRng};
16use std::collections::{HashMap, HashSet};
17
18pub struct ValidatorSwarm {
19    pub nodes: Vec<NodeConfig>,
20}
21
22pub fn validator_swarm(
23    template: &NodeConfig, count: usize, seed: [u8; 32], randomize_ports: bool,
24) -> ValidatorSwarm {
25    let mut rng = StdRng::from_seed(seed);
26    let mut nodes = Vec::new();
27
28    for index in 0..count {
29        let node =
30            NodeConfig::random_with_template(index as u32, template, &mut rng);
31        if randomize_ports {
32            //node.randomize_ports();
33        }
34
35        // For a validator node, any of its validator peers are considered an
36        // upstream peer
37        /*let network = node.validator_network.as_mut().unwrap();
38        network.discovery_method = DiscoveryMethod::Onchain;
39        network.mutual_authentication = true;
40        network.network_id = NetworkId::Validator;*/
41
42        nodes.push(node);
43    }
44
45    // set the first validator as every validators' initial configured seed
46    // peer.
47    /*let seed_config = &nodes[0].validator_network.as_ref().unwrap();
48    let seeds = build_seed_for_network(&seed_config, PeerRole::Validator);
49    for node in &mut nodes {
50        let network = node.validator_network.as_mut().unwrap();
51        network.seeds = seeds.clone();
52    }*/
53
54    ValidatorSwarm { nodes }
55}
56
57pub fn validator_swarm_for_testing(nodes: usize) -> ValidatorSwarm {
58    let config = NodeConfig {
59        test: Some(TestConfig::open_module()),
60        ..Default::default()
61    };
62    validator_swarm(&config, nodes, [1u8; 32], true)
63}
64
65/// Convenience function that builds a `PeerSet` containing a single peer for
66/// testing with a fully formatted `NetworkAddress` containing its network
67/// identity pubkey and handshake protocol version.
68pub fn build_seed_for_network(
69    seed_config: &NetworkConfig, seed_role: PeerRole,
70) -> PeerSet {
71    let seed_pubkey =
72        diem_crypto::PrivateKey::public_key(&seed_config.identity_key());
73    let seed_addr = seed_config
74        .listen_address
75        .clone()
76        .append_prod_protos(seed_pubkey, HANDSHAKE_VERSION);
77
78    let mut keys = HashSet::new();
79    keys.insert(seed_pubkey);
80    let mut seeds = HashMap::default();
81    seeds.insert(
82        seed_config.peer_id(),
83        Peer::new(vec![seed_addr], keys, seed_role),
84    );
85    seeds
86}