Skip to content

Commit

Permalink
add basic logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dknopik committed Oct 10, 2024
1 parent b3f5035 commit c3d4328
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 76 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ path = "ethshadow.rs"
[dependencies]
ethshadow = { path = "lib" }
clap = { version = "4.5", features = ["cargo"] }
color-eyre = "0.6"
color-eyre = "0.6"
env_logger = "0.11"
3 changes: 3 additions & 0 deletions ethshadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use clap::{arg, command, value_parser};
use color_eyre::eyre::WrapErr;
use color_eyre::Result;
use env_logger::Env;
use ethshadow::generate;
use std::env;
use std::fs::File;
Expand All @@ -28,6 +29,8 @@ fn main() -> Result<()> {
)
.get_matches();

env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

let dir = matches
.remove_one::<PathBuf>("dir")
.expect("there is a default in place");
Expand Down
3 changes: 2 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ libsecp256k1 = "0.7.1"
hex = "0.4.3"
typetag = "0.2"
itertools = "0.13"
users = "0.11"
users = "0.11"
log = "0.4"
24 changes: 14 additions & 10 deletions lib/src/clients/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use crate::clients::{Client, JSON_RPC_PORT};
use crate::config::shadow::Process;
use crate::error::Error;
use crate::node::{NodeInfo, SimulationContext};
use crate::utils::log_and_wait;
use crate::validators::Validator;
use crate::CowStr;
use log::debug;
use serde::Deserialize;
use std::collections::HashMap;
use std::process::{Command, Stdio};
use std::process::Command;

const PORT: &str = "21000";

Expand Down Expand Up @@ -39,14 +41,14 @@ impl Client for Geth {
let dir = node.dir().join("geth");
let dir = dir.to_str().ok_or(Error::NonUTF8Path)?;

let status = Command::new(self.executable.as_ref())
.arg("init")
.arg("--datadir")
.arg(dir)
.arg(genesis_file)
.stdout(Stdio::null())
.spawn()?
.wait()?;
debug!("Calling geth init");
let status = log_and_wait(
Command::new(self.executable.as_ref())
.arg("init")
.arg("--datadir")
.arg(dir)
.arg(genesis_file),
)?;
if !status.success() {
return Err(Error::ChildProcessFailure("geth init".to_string()));
}
Expand Down Expand Up @@ -79,5 +81,7 @@ impl Client for Geth {
})
}

fn is_el_client(&self) -> bool { true }
fn is_el_client(&self) -> bool {
true
}
}
4 changes: 3 additions & 1 deletion lib/src/clients/lighthouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,7 @@ impl Client for Lighthouse {
})
}

fn is_cl_client(&self) -> bool { true }
fn is_cl_client(&self) -> bool {
true
}
}
41 changes: 21 additions & 20 deletions lib/src/clients/lighthouse_bootnode.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use log::debug;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::read_to_string;
use std::process::{Command, Stdio};

use serde::Deserialize;
use std::process::Command;

use crate::clients::{Client, Validator};
use crate::config::shadow::Process;
use crate::error::Error;
use crate::node::{NodeInfo, SimulationContext};
use crate::utils::log_and_wait;
use crate::{genesis, CowStr};

const PORT: &str = "4011";
Expand Down Expand Up @@ -37,23 +38,23 @@ impl Client for LighthouseBootnode {
_validators: &[Validator],
) -> Result<Process, Error> {
let dir = node.dir().join("lighthouse_bootnode");
let status = Command::new(self.lcli_executable.as_ref())
.arg("generate-bootnode-enr")
.arg("--testnet-dir")
.arg(ctx.metadata_path())
.arg("--ip")
.arg(node.ip().to_string())
.arg("--udp-port")
.arg(PORT)
.arg("--tcp-port")
.arg(PORT)
.arg("--genesis-fork-version")
.arg(genesis::GENESIS_FORK_VERSION)
.arg("--output-dir")
.arg(&dir)
.stdout(Stdio::null())
.spawn()?
.wait()?;
debug!("Calling lcli generate-bootnode-enr");
let status = log_and_wait(
Command::new(self.lcli_executable.as_ref())
.arg("generate-bootnode-enr")
.arg("--testnet-dir")
.arg(ctx.metadata_path())
.arg("--ip")
.arg(node.ip().to_string())
.arg("--udp-port")
.arg(PORT)
.arg("--tcp-port")
.arg(PORT)
.arg("--genesis-fork-version")
.arg(genesis::GENESIS_FORK_VERSION)
.arg("--output-dir")
.arg(&dir),
)?;
if !status.success() {
return Err(Error::ChildProcessFailure(
"lcli generate-bootnode-enr".to_string(),
Expand Down
8 changes: 6 additions & 2 deletions lib/src/clients/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub trait Client: Debug {
ValidatorDemand::None
}

fn is_cl_client(&self) -> bool { false }
fn is_el_client(&self) -> bool { false }
fn is_cl_client(&self) -> bool {
false
}
fn is_el_client(&self) -> bool {
false
}
}
4 changes: 3 additions & 1 deletion lib/src/clients/reth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@ impl Client for Reth {
})
}

fn is_el_client(&self) -> bool { true }
fn is_el_client(&self) -> bool {
true
}
}
26 changes: 13 additions & 13 deletions lib/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::config::ethshadow::DEFAULT_MNEMONIC;
use crate::config::EthShadowConfig;
use crate::error::Error;
use crate::utils::log_and_wait;
use std::ffi::OsString;
use std::fmt::Display;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::process::Command;
use users::get_current_uid;

pub const GENESIS_FORK_VERSION: &str = "0x10000000";
Expand Down Expand Up @@ -151,18 +152,17 @@ pub fn generate(image_name: &str, output_path: OsString) -> Result<(), Error> {
data_mount.push(":/data");
let mut config_mount = output_path;
config_mount.push("/values.env:/config/values.env");
let status = Command::new("docker")
.args(["run", "--rm", "-i", "-u"])
.arg(get_current_uid().to_string())
.arg("-v")
.arg(data_mount)
.arg("-v")
.arg(config_mount)
.arg(image_name)
.arg("all")
.stdout(Stdio::null())
.spawn()?
.wait()?;
let status = log_and_wait(
Command::new("docker")
.args(["run", "--rm", "-i", "-u"])
.arg(get_current_uid().to_string())
.arg("-v")
.arg(data_mount)
.arg("-v")
.arg(config_mount)
.arg(image_name)
.arg("all"),
)?;
if status.success() {
Ok(())
} else {
Expand Down
13 changes: 9 additions & 4 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::network_graph::{generate_network_graph, GeneratedNetworkGraph};
use crate::node::NodeManager;
use crate::validators::ValidatorManager;
use config::ethshadow;
use log::{debug, info};
use serde_yaml::Value;
use std::borrow::Cow;
use std::ffi::{OsStr, OsString};
Expand All @@ -19,6 +20,7 @@ mod genesis;
mod gml;
mod network_graph;
mod node;
mod utils;
mod validators;

type CowStr = Cow<'static, str>;
Expand Down Expand Up @@ -54,6 +56,7 @@ pub fn generate<T: TryInto<FullConfig, Error = Error>>(
config: T,
mut output_path: PathBuf,
) -> Result<ShadowInvocation, Error> {
debug!("Reading config file");
// get the config and extend it with our supported builtins
let FullConfig {
mut ethshadow_config,
Expand All @@ -62,14 +65,15 @@ pub fn generate<T: TryInto<FullConfig, Error = Error>>(
ethshadow_config.add_default_builtins();
shadow_config.apply_defaults(ethshadow_config.minimum_latency())?;

debug!("Creating output directory");
create_dir(&output_path).map_err(|e| match e.kind() {
ErrorKind::AlreadyExists => Error::OutputFolderExists,
_ => Error::Io(e),
})?;
output_path = output_path.canonicalize()?;
let dir_path = output_path.clone().into_os_string();

// generate genesis
info!("Generating genesis information");
genesis::write_config(&ethshadow_config, output_path.clone())?;
genesis::generate(
ethshadow_config
Expand All @@ -80,7 +84,7 @@ pub fn generate<T: TryInto<FullConfig, Error = Error>>(
dir_path,
)?;

// generate network graph
debug!("Generating network graph");
let GeneratedNetworkGraph {
gml,
mut network_graph,
Expand All @@ -102,9 +106,10 @@ pub fn generate<T: TryInto<FullConfig, Error = Error>>(

let nodes = ethshadow_config.desugar_nodes()?;

// generate nodes
debug!("Computing validators");
let validators = ValidatorManager::new(&ethshadow_config, &nodes, &output_path)?;

info!("Generating nodes");
let mut node_manager = NodeManager::new(
output_path.clone(),
&nodes,
Expand All @@ -114,7 +119,7 @@ pub fn generate<T: TryInto<FullConfig, Error = Error>>(
);
node_manager.generate_nodes()?;

// write modified shadow.yaml to disk
info!("Writing finished configuration");
output_path.push("shadow.yaml");
serde_yaml::to_writer(File::create_new(&output_path)?, &shadow_config.0)?;
let config_path = output_path.as_os_str().to_owned();
Expand Down
Loading

0 comments on commit c3d4328

Please sign in to comment.