Add lcli mock-el (#4587)

* Track time to early attester cache

* Add debug log for early attester cache

* Add mock-el command to lcli

* Revert beacon chain changes

* Tidy, fix compilation errors

* Add required flag

* Default to SYNCING response

* Hide flag
This commit is contained in:
Paul Hauner 2023-11-29 10:04:29 +11:00 committed by GitHub
parent 8a599ec7dc
commit 86163d94f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 124 additions and 0 deletions

2
Cargo.lock generated
View File

@ -3875,7 +3875,9 @@ dependencies = [
"eth2_wallet",
"ethereum_hashing",
"ethereum_ssz",
"execution_layer",
"genesis",
"hex",
"int_to_bytes",
"lighthouse_network",
"lighthouse_version",

View File

@ -43,6 +43,8 @@ beacon_chain = { workspace = true }
store = { workspace = true }
malloc_utils = { workspace = true }
rayon = { workspace = true }
execution_layer = { workspace = true }
hex = { workspace = true }
[package.metadata.cargo-udeps.ignore]
normal = ["malloc_utils"]

View File

@ -11,6 +11,7 @@ mod indexed_attestations;
mod insecure_validators;
mod interop_genesis;
mod mnemonic_validators;
mod mock_el;
mod new_testnet;
mod parse_ssz;
mod replace_state_pubkeys;
@ -891,6 +892,61 @@ 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. It cannot perform validator duties.")
.arg(
Arg::with_name("jwt-output-path")
.long("jwt-output-path")
.value_name("PATH")
.takes_value(true)
.required(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("Controls the response to newPayload and forkchoiceUpdated. \
Set to 'true' to return VALID. Set to 'false' to return SYNCING.")
.default_value("false")
.hidden(true)
)
.arg(
Arg::with_name("shanghai-time")
.long("shanghai-time")
.value_name("UNIX_TIMESTAMP")
.takes_value(true)
.help("The payload timestamp that enables Shanghai. Defaults to the mainnet value.")
.default_value("1681338479")
)
.arg(
Arg::with_name("cancun-time")
.long("cancun-time")
.value_name("UNIX_TIMESTAMP")
.takes_value(true)
.help("The payload timestamp that enables Cancun. No default is provided \
until Cancun is triggered on mainnet.")
)
)
.get_matches();
let result = matches
@ -1032,6 +1088,8 @@ fn run<T: EthSpec>(
state_root::run::<T>(env, network_config, matches)
.map_err(|e| format!("Failed to run state-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)),
}
}

62
lcli/src/mock_el.rs Normal file
View File

@ -0,0 +1,62 @@
use clap::ArgMatches;
use clap_utils::{parse_optional, 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 shanghai_time = parse_required(matches, "shanghai-time")?;
let cancun_time = parse_optional(matches, "cancun-time")?;
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,
shanghai_time: Some(shanghai_time),
cancun_time,
};
let kzg = None;
let server: MockServer<T> = MockServer::new_with_config(&handle, config, kzg);
if all_payloads_valid {
eprintln!(
"Using --all-payloads-valid=true can be dangerous. \
Never use this flag when operating validators."
);
// 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. \
It cannot perform validator duties. It may cause nodes to follow an invalid chain."
);
eprintln!("Server listening on {}:{}", listen_addr, listen_port);
let shutdown_reason = env.block_until_shutdown_requested()?;
eprintln!("Shutting down: {:?}", shutdown_reason);
Ok(())
}