-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor NetworkBuilder and expose through CLI (#3623)
- Loading branch information
1 parent
c335739
commit 7b1ee3e
Showing
203 changed files
with
1,166 additions
and
1,094 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.