Merge branch 'master' into master-sf

This commit is contained in:
pawanjay176
2020-04-28 18:19:33 +05:30
164 changed files with 5444 additions and 2104 deletions

View File

@@ -1,12 +1,12 @@
use crate::helpers::{parse_hex_bytes, parse_u64};
use clap::ArgMatches;
use clap_utils::{parse_required, parse_ssz_required};
use deposit_contract::{decode_eth1_tx_data, DEPOSIT_DATA_LEN};
use tree_hash::TreeHash;
use types::EthSpec;
pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let rlp_bytes = parse_hex_bytes(matches, "deposit-data")?;
let amount = parse_u64(matches, "deposit-amount")?;
let rlp_bytes = parse_ssz_required::<Vec<u8>>(matches, "deposit-data")?;
let amount = parse_required(matches, "deposit-amount")?;
if rlp_bytes.len() != DEPOSIT_DATA_LEN {
return Err(format!(

View File

@@ -1,31 +1,35 @@
use clap::ArgMatches;
use eth1_test_rig::DepositContract;
use futures::compat::Future01CompatExt;
use std::fs::File;
use std::io::Read;
use clap_utils;
use deposit_contract::{
testnet::{ABI, BYTECODE},
CONTRACT_DEPLOY_GAS,
};
use environment::Environment;
use futures::{Future, IntoFuture};
use std::path::PathBuf;
use types::EthSpec;
use web3::{transports::Http, Web3};
use web3::{
contract::{Contract, Options},
transports::Ipc,
types::{Address, U256},
Web3,
};
pub async fn run<T: EthSpec>(matches: &ArgMatches<'_>) -> Result<(), String> {
let confirmations = matches
.value_of("confirmations")
.ok_or_else(|| "Confirmations not specified")?
.parse::<usize>()
.map_err(|e| format!("Failed to parse confirmations: {}", e))?;
pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> {
let eth1_ipc_path: PathBuf = clap_utils::parse_required(matches, "eth1-ipc")?;
let from_address: Address = clap_utils::parse_required(matches, "from-address")?;
let confirmations: usize = clap_utils::parse_required(matches, "confirmations")?;
let password = parse_password(matches)?;
let (_event_loop_handle, transport) =
Ipc::new(eth1_ipc_path).map_err(|e| format!("Unable to connect to eth1 IPC: {:?}", e))?;
let web3 = Web3::new(transport);
let endpoint = matches
.value_of("eth1-endpoint")
.ok_or_else(|| "eth1-endpoint not specified")?;
let (_event_loop, transport) = Http::new(&endpoint).map_err(|e| {
let bytecode = String::from_utf8(BYTECODE.to_vec()).map_err(|e| {
format!(
"Failed to start HTTP transport connected to ganache: {:?}",
"Unable to parse deposit contract bytecode as utf-8: {:?}",
e
)
})?;
let web3 = Web3::new(transport);
// It's unlikely that this will be the _actual_ deployment block, however it'll be close
// enough to serve our purposes.
@@ -39,49 +43,26 @@ pub async fn run<T: EthSpec>(matches: &ArgMatches<'_>) -> Result<(), String> {
.await
.map_err(|e| format!("Failed to get block number: {}", e))?;
info!("Present eth1 block number is {}", deploy_block);
let address = env.runtime().block_on(
Contract::deploy(web3.eth(), &ABI)
.map_err(|e| format!("Unable to build contract deployer: {:?}", e))?
.confirmations(confirmations)
.options(Options {
gas: Some(U256::from(CONTRACT_DEPLOY_GAS)),
..Options::default()
})
.execute(bytecode, (), from_address)
.into_future()
.map_err(|e| format!("Unable to execute deployment: {:?}", e))
.and_then(|pending| {
pending.map_err(|e| format!("Unable to await pending contract: {:?}", e))
})
.map(|tx_receipt| tx_receipt.address())
.map_err(|e| format!("Failed to execute deployment: {:?}", e)),
)?;
info!("Deploying the bytecode at https://github.com/sigp/unsafe-eth2-deposit-contract",);
info!(
"Submitting deployment transaction, waiting for {} confirmations",
confirmations
);
let deposit_contract = DepositContract::deploy_testnet(web3, confirmations, password)
.await
.map_err(|e| format!("Failed to deploy contract: {}", e))?;
info!(
"Deposit contract deployed. address: {}, deploy_block: {}",
deposit_contract.address(),
deploy_block
);
println!("deposit_contract_address: {:?}", address);
println!("deposit_contract_deploy_block: {}", deploy_block);
Ok(())
}
pub fn parse_password(matches: &ArgMatches) -> Result<Option<String>, String> {
if let Some(password_path) = matches.value_of("password") {
Ok(Some(
File::open(password_path)
.map_err(|e| format!("Unable to open password file: {:?}", e))
.and_then(|mut file| {
let mut password = String::new();
file.read_to_string(&mut password)
.map_err(|e| format!("Unable to read password file to string: {:?}", e))
.map(|_| password)
})
.map(|password| {
// Trim the linefeed from the end.
if password.ends_with('\n') {
password[0..password.len() - 1].to_string()
} else {
password
}
})?,
))
} else {
Ok(None)
}
}

View File

@@ -0,0 +1,60 @@
use clap::ArgMatches;
use eth2_libp2p::{
discovery::{build_enr, CombinedKey, Keypair, ENR_FILENAME},
NetworkConfig, NETWORK_KEY_FILENAME,
};
use std::convert::TryInto;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::net::IpAddr;
use std::path::PathBuf;
use types::{EnrForkId, EthSpec};
pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let ip: IpAddr = clap_utils::parse_required(matches, "ip")?;
let udp_port: u16 = clap_utils::parse_required(matches, "udp-port")?;
let tcp_port: u16 = clap_utils::parse_required(matches, "tcp-port")?;
let output_dir: PathBuf = clap_utils::parse_required(matches, "output-dir")?;
if output_dir.exists() {
return Err(format!(
"{:?} already exists, will not override",
output_dir
));
}
let mut config = NetworkConfig::default();
config.enr_address = Some(ip);
config.enr_udp_port = Some(udp_port);
config.enr_tcp_port = Some(tcp_port);
let local_keypair = Keypair::generate_secp256k1();
let enr_key: CombinedKey = local_keypair
.clone()
.try_into()
.map_err(|e| format!("Unable to convert keypair: {:?}", e))?;
let enr = build_enr::<T>(&enr_key, &config, EnrForkId::default())
.map_err(|e| format!("Unable to create ENR: {:?}", e))?;
fs::create_dir_all(&output_dir).map_err(|e| format!("Unable to create output-dir: {:?}", e))?;
let mut enr_file = File::create(output_dir.join(ENR_FILENAME))
.map_err(|e| format!("Unable to create {}: {:?}", ENR_FILENAME, e))?;
enr_file
.write_all(&enr.to_base64().as_bytes())
.map_err(|e| format!("Unable to write ENR to {}: {:?}", ENR_FILENAME, e))?;
let secret_bytes = match local_keypair {
Keypair::Secp256k1(key) => key.secret().to_bytes(),
_ => return Err("Key is not a secp256k1 key".into()),
};
let mut key_file = File::create(output_dir.join(NETWORK_KEY_FILENAME))
.map_err(|e| format!("Unable to create {}: {:?}", NETWORK_KEY_FILENAME, e))?;
key_file
.write_all(&secret_bytes)
.map_err(|e| format!("Unable to write key to {}: {:?}", NETWORK_KEY_FILENAME, e))?;
Ok(())
}

View File

@@ -29,6 +29,14 @@ pub fn parse_path_with_default_in_home_dir(
})
}
pub fn parse_path(matches: &ArgMatches, name: &'static str) -> Result<PathBuf, String> {
matches
.value_of(name)
.ok_or_else(|| format!("{} not specified", name))?
.parse::<PathBuf>()
.map_err(|e| format!("Unable to parse {}: {}", name, e))
}
pub fn parse_u64(matches: &ArgMatches, name: &'static str) -> Result<u64, String> {
matches
.value_of(name)

View File

@@ -5,7 +5,7 @@ mod change_genesis_time;
mod check_deposit_data;
mod deploy_deposit_contract;
mod eth1_genesis;
mod helpers;
mod generate_bootnode_enr;
mod interop_genesis;
mod new_testnet;
mod parse_hex;
@@ -18,6 +18,7 @@ use log::Level;
use parse_hex::run_parse_hex;
use std::fs::File;
use std::path::PathBuf;
use std::process;
use std::time::{SystemTime, UNIX_EPOCH};
use transition_blocks::run_transition_blocks;
use types::{test_utils::TestingBeaconStateBuilder, EthSpec, MainnetEthSpec, MinimalEthSpec};
@@ -28,8 +29,7 @@ async fn main() {
let matches = App::new("Lighthouse CLI Tool")
.about(
"Performs various testing-related tasks, modelled after zcli. \
by @protolambda.",
"Performs various testing-related tasks, including defining testnets.",
)
.arg(
Arg::with_name("spec")
@@ -41,6 +41,15 @@ async fn main() {
.possible_values(&["minimal", "mainnet"])
.default_value("mainnet")
)
.arg(
Arg::with_name("testnet-dir")
.short("d")
.long("testnet-dir")
.value_name("PATH")
.takes_value(true)
.global(true)
.help("The testnet dir. Defaults to ~/.lighthouse/testnet"),
)
.subcommand(
SubCommand::with_name("genesis_yaml")
.about("Generates a genesis YAML file")
@@ -120,13 +129,22 @@ async fn main() {
"Deploy a testing eth1 deposit contract.",
)
.arg(
Arg::with_name("eth1-endpoint")
Arg::with_name("eth1-ipc")
.long("eth1-ipc")
.short("e")
.long("eth1-endpoint")
.value_name("HTTP_SERVER")
.value_name("ETH1_IPC_PATH")
.help("Path to an Eth1 JSON-RPC IPC endpoint")
.takes_value(true)
.default_value("http://localhost:8545")
.help("The URL to the eth1 JSON-RPC http API."),
.required(true)
)
.arg(
Arg::with_name("from-address")
.long("from-address")
.short("f")
.value_name("FROM_ETH1_ADDRESS")
.help("The address that will submit the contract creation. Must be unlocked.")
.takes_value(true)
.required(true)
)
.arg(
Arg::with_name("confirmations")
@@ -136,13 +154,6 @@ async fn main() {
.default_value("3")
.help("The number of block confirmations before declaring the contract deployed."),
)
.arg(
Arg::with_name("password")
.long("password")
.value_name("FILE")
.takes_value(true)
.help("The password file to unlock the eth1 account (see --index)"),
)
)
.subcommand(
SubCommand::with_name("refund-deposit-contract")
@@ -150,37 +161,32 @@ async fn main() {
"Calls the steal() function on a testnet eth1 contract.",
)
.arg(
Arg::with_name("testnet-dir")
.short("d")
.long("testnet-dir")
.value_name("PATH")
.takes_value(true)
.help("The testnet dir. Defaults to ~/.lighthouse/testnet"),
)
.arg(
Arg::with_name("eth1-endpoint")
Arg::with_name("eth1-ipc")
.long("eth1-ipc")
.short("e")
.long("eth1-endpoint")
.value_name("HTTP_SERVER")
.value_name("ETH1_IPC_PATH")
.help("Path to an Eth1 JSON-RPC IPC endpoint")
.takes_value(true)
.default_value("http://localhost:8545")
.help("The URL to the eth1 JSON-RPC http API."),
.required(true)
)
.arg(
Arg::with_name("password")
.long("password")
.value_name("FILE")
Arg::with_name("from-address")
.long("from-address")
.short("f")
.value_name("FROM_ETH1_ADDRESS")
.help("The address that will submit the contract creation. Must be unlocked.")
.takes_value(true)
.help("The password file to unlock the eth1 account (see --index)"),
.required(true)
)
.arg(
Arg::with_name("account-index")
.short("i")
.long("account-index")
.value_name("INDEX")
Arg::with_name("contract-address")
.long("contract-address")
.short("c")
.value_name("CONTRACT_ETH1_ADDRESS")
.help("The address of the contract to be refunded. Its owner must match
--from-address.")
.takes_value(true)
.default_value("0")
.help("The eth1 accounts[] index which will send the transaction"),
.required(true)
)
)
.subcommand(
@@ -188,14 +194,6 @@ async fn main() {
.about(
"Listens to the eth1 chain and finds the genesis beacon state",
)
.arg(
Arg::with_name("testnet-dir")
.short("d")
.long("testnet-dir")
.value_name("PATH")
.takes_value(true)
.help("The testnet dir. Defaults to ~/.lighthouse/testnet"),
)
.arg(
Arg::with_name("eth1-endpoint")
.short("e")
@@ -211,14 +209,6 @@ async fn main() {
.about(
"Produces an interop-compatible genesis state using deterministic keypairs",
)
.arg(
Arg::with_name("testnet-dir")
.short("d")
.long("testnet-dir")
.value_name("PATH")
.takes_value(true)
.help("The testnet dir. Defaults to ~/.lighthouse/testnet"),
)
.arg(
Arg::with_name("validator-count")
.long("validator-count")
@@ -262,14 +252,15 @@ async fn main() {
.subcommand(
SubCommand::with_name("new-testnet")
.about(
"Produce a new testnet directory.",
"Produce a new testnet directory. If any of the optional flags are not
supplied the values will remain the default for the --spec flag",
)
.arg(
Arg::with_name("testnet-dir")
.long("testnet-dir")
.value_name("DIRECTORY")
.takes_value(true)
.help("The output path for the new testnet directory. Defaults to ~/.lighthouse/testnet"),
Arg::with_name("force")
.long("force")
.short("f")
.takes_value(false)
.help("Overwrites any previous testnet configurations"),
)
.arg(
Arg::with_name("min-genesis-time")
@@ -284,7 +275,6 @@ async fn main() {
.long("min-genesis-active-validator-count")
.value_name("INTEGER")
.takes_value(true)
.default_value("16384")
.help("The number of validators required to trigger eth2 genesis."),
)
.arg(
@@ -292,7 +282,6 @@ async fn main() {
.long("min-genesis-delay")
.value_name("SECONDS")
.takes_value(true)
.default_value("3600") // 10 minutes
.help("The delay between sufficient eth1 deposits and eth2 genesis."),
)
.arg(
@@ -300,7 +289,6 @@ async fn main() {
.long("min-deposit-amount")
.value_name("GWEI")
.takes_value(true)
.default_value("100000000") // 0.1 Eth
.help("The minimum permitted deposit amount."),
)
.arg(
@@ -308,7 +296,6 @@ async fn main() {
.long("max-effective-balance")
.value_name("GWEI")
.takes_value(true)
.default_value("3200000000") // 3.2 Eth
.help("The amount required to become a validator."),
)
.arg(
@@ -316,7 +303,6 @@ async fn main() {
.long("effective-balance-increment")
.value_name("GWEI")
.takes_value(true)
.default_value("100000000") // 0.1 Eth
.help("The steps in effective balance calculation."),
)
.arg(
@@ -324,7 +310,6 @@ async fn main() {
.long("ejection-balance")
.value_name("GWEI")
.takes_value(true)
.default_value("1600000000") // 1.6 Eth
.help("The balance at which a validator gets ejected."),
)
.arg(
@@ -332,7 +317,6 @@ async fn main() {
.long("eth1-follow-distance")
.value_name("ETH1_BLOCKS")
.takes_value(true)
.default_value("16")
.help("The distance to follow behind the eth1 chain head."),
)
.arg(
@@ -340,7 +324,6 @@ async fn main() {
.long("genesis-fork-version")
.value_name("HEX")
.takes_value(true)
.default_value("0x00000000")
.help("Used to avoid reply attacks between testnets. Recommended to set to
non-default."),
)
@@ -349,7 +332,7 @@ async fn main() {
.long("deposit-contract-address")
.value_name("ETH1_ADDRESS")
.takes_value(true)
.default_value("0x0000000000000000000000000000000000000000")
.required(true)
.help("The address of the deposit contract."),
)
.arg(
@@ -385,11 +368,55 @@ async fn main() {
function signature."),
)
)
.subcommand(
SubCommand::with_name("generate-bootnode-enr")
.about(
"Generates an ENR address to be used as a pre-genesis boot node..",
)
.arg(
Arg::with_name("ip")
.long("ip")
.value_name("IP_ADDRESS")
.takes_value(true)
.required(true)
.help("The IP address to be included in the ENR and used for discovery"),
)
.arg(
Arg::with_name("udp-port")
.long("udp-port")
.value_name("UDP_PORT")
.takes_value(true)
.required(true)
.help("The UDP port to be included in the ENR and used for discovery"),
)
.arg(
Arg::with_name("tcp-port")
.long("tcp-port")
.value_name("TCP_PORT")
.takes_value(true)
.required(true)
.help("The TCP port to be included in the ENR and used for application comms"),
)
.arg(
Arg::with_name("output-dir")
.long("output-dir")
.value_name("OUTPUT_DIRECTORY")
.takes_value(true)
.required(true)
.help("The directory in which to create the network dir"),
)
)
.get_matches();
macro_rules! run_with_spec {
($env_builder: expr) => {
run($env_builder, &matches)
match run($env_builder, &matches) {
Ok(()) => process::exit(0),
Err(e) => {
println!("Failed to run lcli: {}", e);
process::exit(1)
}
}
};
}
@@ -404,14 +431,14 @@ async fn main() {
}
}
async fn run<T: EthSpec>(env_builder: EnvironmentBuilder<T>, matches: &ArgMatches<'_>) {
fn run<T: EthSpec>(env_builder: EnvironmentBuilder<T>, matches: &ArgMatches) -> Result<(), String> {
let env = env_builder
.multi_threaded_tokio_runtime()
.expect("should start tokio runtime")
.map_err(|e| format!("should start tokio runtime: {:?}", e))?
.async_logger("trace", None)
.expect("should start null logger")
.map_err(|e| format!("should start null logger: {:?}", e))?
.build()
.expect("should build env");
.map_err(|e| format!("should build env: {:?}", e))?;
match matches.subcommand() {
("genesis_yaml", Some(matches)) => {
@@ -450,31 +477,34 @@ async fn run<T: EthSpec>(env_builder: EnvironmentBuilder<T>, matches: &ArgMatche
_ => unreachable!("guarded by slog possible_values"),
};
info!("Genesis state YAML file created. Exiting successfully.");
Ok(())
}
("transition-blocks", Some(matches)) => run_transition_blocks::<T>(matches)
.unwrap_or_else(|e| error!("Failed to transition blocks: {}", e)),
("pretty-hex", Some(matches)) => run_parse_hex::<T>(matches)
.unwrap_or_else(|e| error!("Failed to pretty print hex: {}", e)),
("deploy-deposit-contract", Some(matches)) => deploy_deposit_contract::run::<T>(matches)
.await
.unwrap_or_else(|e| error!("Failed to run deploy-deposit-contract command: {}", e)),
.map_err(|e| format!("Failed to transition blocks: {}", e)),
("pretty-hex", Some(matches)) => {
run_parse_hex::<T>(matches).map_err(|e| format!("Failed to pretty print hex: {}", e))
}
("deploy-deposit-contract", Some(matches)) => {
deploy_deposit_contract::run::<T>(env, matches)
.map_err(|e| format!("Failed to run deploy-deposit-contract command: {}", e))
}
("refund-deposit-contract", Some(matches)) => {
refund_deposit_contract::run::<T>(env, matches)
.await
.unwrap_or_else(|e| error!("Failed to run refund-deposit-contract command: {}", e))
.map_err(|e| format!("Failed to run refund-deposit-contract command: {}", e))
}
("eth1-genesis", Some(matches)) => eth1_genesis::run::<T>(env, matches)
.await
.unwrap_or_else(|e| error!("Failed to run eth1-genesis command: {}", e)),
.map_err(|e| format!("Failed to run eth1-genesis command: {}", e)),
("interop-genesis", Some(matches)) => interop_genesis::run::<T>(env, matches)
.unwrap_or_else(|e| error!("Failed to run interop-genesis command: {}", e)),
.map_err(|e| format!("Failed to run interop-genesis command: {}", e)),
("change-genesis-time", Some(matches)) => change_genesis_time::run::<T>(matches)
.unwrap_or_else(|e| error!("Failed to run change-genesis-time command: {}", e)),
.map_err(|e| format!("Failed to run change-genesis-time command: {}", e)),
("new-testnet", Some(matches)) => new_testnet::run::<T>(matches)
.unwrap_or_else(|e| error!("Failed to run new_testnet command: {}", e)),
.map_err(|e| format!("Failed to run new_testnet command: {}", e)),
("check-deposit-data", Some(matches)) => check_deposit_data::run::<T>(matches)
.unwrap_or_else(|e| error!("Failed to run check-deposit-data command: {}", e)),
(other, _) => error!("Unknown subcommand {}. See --help.", other),
.map_err(|e| format!("Failed to run check-deposit-data command: {}", e)),
("generate-bootnode-enr", Some(matches)) => generate_bootnode_enr::run::<T>(matches)
.map_err(|e| format!("Failed to run generate-bootnode-enr command: {}", e)),
(other, _) => Err(format!("Unknown subcommand {}. See --help.", other)),
}
}

View File

@@ -1,8 +1,10 @@
use crate::helpers::*;
use clap::ArgMatches;
use clap_utils::{
parse_optional, parse_path_with_default_in_home_dir, parse_required, parse_ssz_optional,
};
use eth2_testnet_config::Eth2TestnetConfig;
use std::path::PathBuf;
use types::{EthSpec, YamlConfig};
use types::{Address, EthSpec, YamlConfig};
pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let testnet_dir_path = parse_path_with_default_in_home_dir(
@@ -10,40 +12,44 @@ pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
"testnet-dir",
PathBuf::from(".lighthouse/testnet"),
)?;
let min_genesis_time = parse_u64_opt(matches, "min-genesis-time")?;
let min_genesis_delay = parse_u64(matches, "min-genesis-delay")?;
let min_genesis_active_validator_count =
parse_u64(matches, "min-genesis-active-validator-count")?;
let min_deposit_amount = parse_u64(matches, "min-deposit-amount")?;
let max_effective_balance = parse_u64(matches, "max-effective-balance")?;
let effective_balance_increment = parse_u64(matches, "effective-balance-increment")?;
let ejection_balance = parse_u64(matches, "ejection-balance")?;
let eth1_follow_distance = parse_u64(matches, "eth1-follow-distance")?;
let deposit_contract_deploy_block = parse_u64(matches, "deposit-contract-deploy-block")?;
let genesis_fork_version = parse_fork_opt(matches, "genesis-fork-version")?;
let deposit_contract_address = parse_address(matches, "deposit-contract-address")?;
let deposit_contract_address: Address = parse_required(matches, "deposit-contract-address")?;
let deposit_contract_deploy_block = parse_required(matches, "deposit-contract-deploy-block")?;
let overwrite_files = matches.is_present("force");
if testnet_dir_path.exists() {
return Err(format!(
"{:?} already exists, will not overwrite",
testnet_dir_path
));
if !overwrite_files {
return Err(format!(
"{:?} already exists, will not overwrite. Use --force to overwrite",
testnet_dir_path
));
}
}
let mut spec = T::default_spec();
if let Some(time) = min_genesis_time {
spec.min_genesis_time = time;
} else {
spec.min_genesis_time = time_now()?;
// Update the spec value if the flag was defined. Otherwise, leave it as the default.
macro_rules! maybe_update {
($flag: tt, $var: ident) => {
if let Some(val) = parse_optional(matches, $flag)? {
spec.$var = val
}
};
}
spec.min_deposit_amount = min_deposit_amount;
spec.min_genesis_active_validator_count = min_genesis_active_validator_count;
spec.max_effective_balance = max_effective_balance;
spec.effective_balance_increment = effective_balance_increment;
spec.ejection_balance = ejection_balance;
spec.eth1_follow_distance = eth1_follow_distance;
spec.min_genesis_delay = min_genesis_delay;
if let Some(v) = genesis_fork_version {
maybe_update!("min-genesis-time", min_genesis_time);
maybe_update!("min-deposit-amount", min_deposit_amount);
maybe_update!(
"min-genesis-active-validator-count",
min_genesis_active_validator_count
);
maybe_update!("max-effective-balance", max_effective_balance);
maybe_update!("effective-balance-increment", effective_balance_increment);
maybe_update!("ejection-balance", ejection_balance);
maybe_update!("eth1-follow_distance", eth1_follow_distance);
maybe_update!("min-genesis-delay", min_genesis_delay);
if let Some(v) = parse_ssz_optional(matches, "genesis-fork-version")? {
spec.genesis_fork_version = v;
}
@@ -55,5 +61,5 @@ pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
yaml_config: Some(YamlConfig::from_spec::<T>(&spec)),
};
testnet.write_to_file(testnet_dir_path)
testnet.write_to_file(testnet_dir_path, overwrite_files)
}

40
lcli/src/parse_ssz.rs Normal file
View File

@@ -0,0 +1,40 @@
use crate::helpers::parse_path;
use clap::ArgMatches;
use serde::Serialize;
use ssz::Decode;
use std::fs::File;
use std::io::Read;
use types::{EthSpec, SignedBeaconBlock};
pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
let type_str = matches
.value_of("type")
.ok_or_else(|| "No type supplied".to_string())?;
let path = parse_path(matches, "path")?;
info!("Type: {:?}", type_str);
let mut bytes = vec![];
let mut file = File::open(&path).map_err(|e| format!("Unable to open {:?}: {}", path, e))?;
file.read_to_end(&mut bytes)
.map_err(|e| format!("Unable to read {:?}: {}", path, e))?;
match type_str {
"SignedBeaconBlock" => decode_and_print::<SignedBeaconBlock<T>>(&bytes)?,
other => return Err(format!("Unknown type: {}", other)),
};
Ok(())
}
fn decode_and_print<T: Decode + Serialize>(bytes: &[u8]) -> Result<(), String> {
let item = T::from_ssz_bytes(&bytes).map_err(|e| format!("Ssz decode failed: {:?}", e))?;
println!(
"{}",
serde_yaml::to_string(&item)
.map_err(|e| format!("Unable to write object to YAML: {:?}", e))?
);
Ok(())
}

View File

@@ -1,12 +1,10 @@
use crate::deploy_deposit_contract::parse_password;
use clap::ArgMatches;
use environment::Environment;
use eth2_testnet_config::Eth2TestnetConfig;
use futures::compat::Future01CompatExt;
use futures::Future;
use std::path::PathBuf;
use types::EthSpec;
use web3::{
transports::Http,
transports::Ipc,
types::{Address, TransactionRequest, U256},
Web3,
};
@@ -14,97 +12,29 @@ use web3::{
/// `keccak("steal()")[0..4]`
pub const STEAL_FN_SIGNATURE: &[u8] = &[0xcf, 0x7a, 0x89, 0x65];
pub async fn run<T: EthSpec>(_env: Environment<T>, matches: &ArgMatches<'_>) -> Result<(), String> {
let endpoint = matches
.value_of("eth1-endpoint")
.ok_or_else(|| "eth1-endpoint not specified")?;
let account_index = matches
.value_of("account-index")
.ok_or_else(|| "No account-index".to_string())?
.parse::<usize>()
.map_err(|e| format!("Unable to parse account-index: {}", e))?;
let password_opt = parse_password(matches)?;
let testnet_dir = matches
.value_of("testnet-dir")
.ok_or_else(|| ())
.and_then(|dir| dir.parse::<PathBuf>().map_err(|_| ()))
.unwrap_or_else(|_| {
dirs::home_dir()
.map(|home| home.join(".lighthouse").join("testnet"))
.expect("should locate home directory")
});
let eth2_testnet_config: Eth2TestnetConfig<T> = Eth2TestnetConfig::load(testnet_dir)?;
let (_event_loop, transport) = Http::new(&endpoint).map_err(|e| {
format!(
"Failed to start HTTP transport connected to ganache: {:?}",
e
)
})?;
pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> {
let eth1_ipc_path: PathBuf = clap_utils::parse_required(matches, "eth1-ipc")?;
let from: Address = clap_utils::parse_required(matches, "from-address")?;
let contract_address: Address = clap_utils::parse_required(matches, "contract-address")?;
let (_event_loop_handle, transport) =
Ipc::new(eth1_ipc_path).map_err(|e| format!("Unable to connect to eth1 IPC: {:?}", e))?;
let web3 = Web3::new(transport);
// Convert from `types::Address` to `web3::types::Address`.
let deposit_contract = Address::from_slice(
eth2_testnet_config
.deposit_contract_address()?
.as_fixed_bytes(),
);
let from_address = web3
.eth()
.accounts()
.compat()
.await
.map_err(|e| format!("Failed to get accounts: {:?}", e))
.and_then(|accounts| {
accounts
.get(account_index)
.cloned()
.ok_or_else(|| "Insufficient accounts for deposit".to_string())
})?;
let from = if let Some(password) = password_opt {
// Unlock for only a single transaction.
let duration = None;
let result = web3
.personal()
.unlock_account(from_address, &password, duration)
.compat()
.await;
match result {
Ok(true) => from_address,
Ok(false) => return Err("Eth1 node refused to unlock account".to_string()),
Err(e) => return Err(format!("Eth1 unlock request failed: {:?}", e)),
}
} else {
from_address
};
let tx_request = TransactionRequest {
from,
to: Some(deposit_contract),
gas: Some(U256::from(400_000)),
gas_price: None,
value: Some(U256::zero()),
data: Some(STEAL_FN_SIGNATURE.into()),
nonce: None,
condition: None,
};
let tx = web3
.eth()
.send_transaction(tx_request)
.compat()
.await
.map_err(|e| format!("Failed to call deposit fn: {:?}", e))?;
info!("Refund transaction submitted: eth1_tx_hash: {:?}", tx);
env.runtime().block_on(
web3.eth()
.send_transaction(TransactionRequest {
from,
to: Some(contract_address),
gas: Some(U256::from(400_000)),
gas_price: None,
value: Some(U256::zero()),
data: Some(STEAL_FN_SIGNATURE.into()),
nonce: None,
condition: None,
})
.map_err(|e| format!("Failed to call deposit fn: {:?}", e)),
)?;
Ok(())
}