Skip to content

Commit

Permalink
Refactor NetworkBuilder and expose through CLI (#3623)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzenioszeniou committed Oct 8, 2021
1 parent c335739 commit 7b1ee3e
Show file tree
Hide file tree
Showing 203 changed files with 1,166 additions and 1,094 deletions.
35 changes: 29 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ members = [
"testing/jormungandr-integration-tests",
"testing/jormungandr-scenario-tests",
"testing/mjolnir",
"testing/hersir",
]
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,14 @@ impl BlockchainConfiguration {

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case", remote = "Discrimination")]
enum DiscriminationDef {
pub enum DiscriminationDef {
Test,
Production,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case", remote = "ConsensusVersion")]
enum ConsensusVersionDef {
pub enum ConsensusVersionDef {
Bft,
GenesisPraos,
}
Expand Down
2 changes: 1 addition & 1 deletion jormungandr-lib/src/interfaces/block0_configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use self::block_content_max_size::BlockContentMaxSize;
pub use self::default_values::*;
pub use self::epoch_stability_depth::EpochStabilityDepth;
pub use self::fees_go_to::FeesGoTo;
pub use self::initial_config::BlockchainConfiguration;
pub use self::initial_config::{BlockchainConfiguration, ConsensusVersionDef, DiscriminationDef};
pub use self::initial_fragment::{
try_initials_vec_from_messages, Initial, InitialUTxO, LegacyUTxO,
};
Expand Down
4 changes: 2 additions & 2 deletions jormungandr-lib/src/interfaces/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Serialize for SerdeMemberPublicKey {
}
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq)]
#[derive(Clone, Deserialize, Serialize, Debug, Eq, PartialEq)]
pub struct VotePlan {
payload_type: VotePrivacy,
vote_start: BlockDate,
Expand All @@ -157,7 +157,7 @@ pub struct VotePlan {
#[serde(with = "serde_proposals")]
proposals: Proposals,
#[serde(with = "serde_committee_member_public_keys", default = "Vec::new")]
committee_member_public_keys: Vec<chain_vote::MemberPublicKey>,
pub committee_member_public_keys: Vec<chain_vote::MemberPublicKey>,
}

#[derive(Deserialize, Serialize)]
Expand Down
18 changes: 18 additions & 0 deletions testing/hersir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "hersir"
description = "Hersir is a simple command line tool that lets you deploy a network of Jormungandr nodes"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chain-addr = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master", features = [ "property-test-api" ] }
chain-impl-mockchain = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
jormungandr-testing-utils = { path = "../jormungandr-testing-utils" }
jormungandr-lib = { path = "../../jormungandr-lib" }
serde = "1.0"
serde_derive = "1.0"
serde_yaml = "0.8.21"
structopt = "0.3.23"
thiserror = "1.0"
19 changes: 19 additions & 0 deletions testing/hersir/res/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
nodes:
- spawn_params:
alias: passive
leadership_mode: passive
persistence_mode: inmemory
trusted_peers:
- leader
- spawn_params:
alias: leader
leadership_mode: leader
persistence_mode: inmemory

blockchain:
discrimination: test
consensus: genesis_praos
linear_fee:
constant: 1
coefficient: 1
certificate: 1
11 changes: 11 additions & 0 deletions testing/hersir/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::path::PathBuf;
use structopt::StructOpt;

#[derive(StructOpt)]
pub struct Args {
#[structopt(long, short)]
pub config: PathBuf,

#[structopt(long, short)]
pub verbose: bool,
}
36 changes: 36 additions & 0 deletions testing/hersir/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use jormungandr_testing_utils::testing::network::{
Blockchain, Node, NodeAlias, SpawnParams, Topology,
};
use serde::Deserialize;
use std::collections::HashSet;

#[derive(Debug, Deserialize)]
pub struct Config {
pub blockchain: Blockchain,
pub nodes: Vec<NodeConfig>,
}

impl Config {
pub fn build_topology(&self) -> Topology {
let mut topology = Topology::default();

for node_config in self.nodes.iter() {
let mut node = Node::new(node_config.spawn_params.get_alias());

for trusted_peer in node_config.trusted_peers.iter() {
node = node.with_trusted_peer(trusted_peer);
}

topology = topology.with_node(node);
}

topology
}
}

#[derive(Debug, Deserialize)]
pub struct NodeConfig {
pub spawn_params: SpawnParams,
#[serde(default)]
pub trusted_peers: HashSet<NodeAlias>,
}
16 changes: 16 additions & 0 deletions testing/hersir/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use jormungandr_testing_utils::testing::network::controller::ControllerError;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("IO error: {0}")]
IO(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_yaml::Error),
#[error("Circular dependency in network topology")]
CircularTrust,
#[error("Controller error: {0}")]
Controller(#[from] ControllerError),
#[error("INTERNAL ERROR: {0}")]
Internal(String),
}
30 changes: 30 additions & 0 deletions testing/hersir/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
mod args;
mod config;
mod error;
mod spawn;

use args::Args;
use std::time::Duration;
use structopt::StructOpt;

fn main() {
let args = Args::from_args();

let nodes = match spawn::spawn_network(args) {
Ok(nodes) => nodes,
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
};

loop {
for node in nodes.values() {
if let Err(e) = node.rest().network_stats() {
eprintln!("{}", e);
std::process::exit(1);
}
}
std::thread::sleep(Duration::from_secs(1));
}
}
48 changes: 48 additions & 0 deletions testing/hersir/src/spawn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::{args::Args, config::Config, error::Error};
use jormungandr_testing_utils::testing::{
jormungandr::JormungandrProcess,
network::{builder::NetworkBuilder, NodeAlias},
};
use std::{collections::HashMap, fs::File};

pub fn spawn_network(args: Args) -> Result<HashMap<NodeAlias, JormungandrProcess>, Error> {
let config: Config = serde_yaml::from_reader(File::open(args.config)?)?;

let mut topology = config.build_topology();

let mut controller = NetworkBuilder::default()
.topology(topology.clone())
.blockchain_config(config.blockchain)
.build()?;

let mut processes: HashMap<NodeAlias, JormungandrProcess> = HashMap::new();

while !topology.nodes.is_empty() {
let alias = topology
.nodes
.values()
.find(|n| n.trusted_peers.is_empty())
.map(|n| n.alias.clone())
.ok_or(Error::CircularTrust)?;

let spawn_params = config
.nodes
.iter()
.find(|c| c.spawn_params.get_alias() == &alias)
.map(|c| &c.spawn_params)
.ok_or_else(|| Error::Internal(format!("Node '{}' has no spawn parameters", alias)))?;

processes.insert(alias.clone(), controller.spawn(spawn_params.clone())?);

topology.nodes.remove(&alias);
topology.nodes.values_mut().for_each(|n| {
n.trusted_peers.remove(&alias);
});

if args.verbose {
println!("Node '{}' started", alias);
}
}

Ok(processes)
}
7 changes: 0 additions & 7 deletions testing/jormungandr-integration-tests/build.rs

This file was deleted.

12 changes: 0 additions & 12 deletions testing/jormungandr-integration-tests/src/common/mod.rs

This file was deleted.

0 comments on commit 7b1ee3e

Please sign in to comment.