Skip to content

Commit

Permalink
Add mock-el command to lcli
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 20, 2022
1 parent 02af15e commit d2de931
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions lcli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ eth2 = { path = "../common/eth2" }
snap = "1.0.1"
beacon_chain = { path = "../beacon_node/beacon_chain" }
store = { path = "../beacon_node/store" }
execution_layer = { path = "../beacon_node/execution_layer" }
hex = "0.4.2"
40 changes: 40 additions & 0 deletions lcli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod generate_bootnode_enr;
mod indexed_attestations;
mod insecure_validators;
mod interop_genesis;
mod mock_el;
mod new_testnet;
mod parse_ssz;
mod replace_state_pubkeys;
Expand Down Expand Up @@ -751,6 +752,43 @@ fn main() {
.help("Number of repeat runs, useful for benchmarking."),
)
)
.subcommand(
SubCommand::with_name("mock-el")
.about("Creates a mock execution layer server. This is NOT SAFE and should only \
be used for testing and development on testnets. Do not use in production. Do not \
use on mainnet.")
.arg(
Arg::with_name("jwt-output-path")
.long("jwt-output-path")
.value_name("PATH")
.takes_value(true)
.help("Path to write the JWT secret."),
)
.arg(
Arg::with_name("listen-address")
.long("listen-address")
.value_name("IP_ADDRESS")
.takes_value(true)
.help("The server will listen on this address.")
.default_value("127.0.0.1")
)
.arg(
Arg::with_name("listen-port")
.long("listen-port")
.value_name("PORT")
.takes_value(true)
.help("The server will listen on this port.")
.default_value("8551")
)
.arg(
Arg::with_name("all-payloads-valid")
.long("all-payloads-valid")
.takes_value(true)
.help("When set to true, the server will indicate that all payloads are \
valid.")
.default_value("true")
)
)
.get_matches();

let result = matches
Expand Down Expand Up @@ -838,6 +876,8 @@ fn run<T: EthSpec>(
.map_err(|e| format!("Failed to run indexed-attestations command: {}", e)),
("block-root", Some(matches)) => block_root::run::<T>(env, matches)
.map_err(|e| format!("Failed to run block-root command: {}", e)),
("mock-el", Some(matches)) => mock_el::run::<T>(env, matches)
.map_err(|e| format!("Failed to run mock-el command: {}", e)),
(other, _) => Err(format!("Unknown subcommand {}. See --help.", other)),
}
}
50 changes: 50 additions & 0 deletions lcli/src/mock_el.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use clap::ArgMatches;
use clap_utils::parse_required;
use environment::Environment;
use execution_layer::{
auth::JwtKey,
test_utils::{
Config, MockExecutionConfig, MockServer, DEFAULT_JWT_SECRET, DEFAULT_TERMINAL_BLOCK,
},
};
use std::net::Ipv4Addr;
use std::path::PathBuf;
use types::*;

pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> {
let jwt_path: PathBuf = parse_required(matches, "jwt-output-path")?;
let listen_addr: Ipv4Addr = parse_required(matches, "listen-address")?;
let listen_port: u16 = parse_required(matches, "listen-port")?;
let all_payloads_valid: bool = parse_required(matches, "all-payloads-valid")?;

let handle = env.core_context().executor.handle().unwrap();
let spec = &T::default_spec();
let jwt_key = JwtKey::from_slice(&DEFAULT_JWT_SECRET).unwrap();
std::fs::write(&jwt_path, hex::encode(DEFAULT_JWT_SECRET)).unwrap();

let config = MockExecutionConfig {
server_config: Config {
listen_addr,
listen_port,
},
jwt_key,
terminal_difficulty: spec.terminal_total_difficulty,
terminal_block: DEFAULT_TERMINAL_BLOCK,
terminal_block_hash: spec.terminal_block_hash,
};
let server: MockServer<T> = MockServer::new_with_config(&handle, config);

if all_payloads_valid {
// Indicate that all payloads are valid.
server.all_payloads_valid();
}

eprintln!("This tool is for TESTING PURPOSES ONLY. Do not use in production or on mainnet.");
eprintln!("Server listening on {}:{}", listen_addr, listen_port);

let shutdown_reason = env.block_until_shutdown_requested()?;

eprintln!("Shutting down: {:?}", shutdown_reason);

Ok(())
}

0 comments on commit d2de931

Please sign in to comment.