Port lcli to stable futures

This commit is contained in:
pawan
2020-03-01 14:18:05 +05:30
committed by Age Manning
parent decd29adfb
commit 640251e7bc
6 changed files with 2876 additions and 2729 deletions

5464
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -19,7 +19,7 @@ state_processing = { path = "../eth2/state_processing" }
eth2_ssz = "0.1.2" eth2_ssz = "0.1.2"
regex = "1.3.1" regex = "1.3.1"
eth1_test_rig = { path = "../tests/eth1_test_rig" } eth1_test_rig = { path = "../tests/eth1_test_rig" }
futures = "0.1.25" futures = {version = "0.3", features = ["compat"]}
environment = { path = "../lighthouse/environment" } environment = { path = "../lighthouse/environment" }
web3 = "0.8.0" web3 = "0.8.0"
eth2_testnet_config = { path = "../eth2/utils/eth2_testnet_config" } eth2_testnet_config = { path = "../eth2/utils/eth2_testnet_config" }
@@ -27,3 +27,4 @@ dirs = "2.0"
genesis = { path = "../beacon_node/genesis" } genesis = { path = "../beacon_node/genesis" }
deposit_contract = { path = "../eth2/utils/deposit_contract" } deposit_contract = { path = "../eth2/utils/deposit_contract" }
tree_hash = { path = "../eth2/utils/tree_hash" } tree_hash = { path = "../eth2/utils/tree_hash" }
tokio = { version = "0.3", features = ["full"] }

View File

@@ -1,12 +1,13 @@
use clap::ArgMatches; use clap::ArgMatches;
use environment::Environment; use environment::Environment;
use eth1_test_rig::DepositContract; use eth1_test_rig::DepositContract;
use futures::compat::Future01CompatExt;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use types::EthSpec; use types::EthSpec;
use web3::{transports::Http, Web3}; use web3::{transports::Http, Web3};
pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> { pub async fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> {
let confirmations = matches let confirmations = matches
.value_of("confirmations") .value_of("confirmations")
.ok_or_else(|| "Confirmations not specified")? .ok_or_else(|| "Confirmations not specified")?
@@ -32,9 +33,11 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
// //
// We only need the deposit block to put a lower bound on the block number we need to search // We only need the deposit block to put a lower bound on the block number we need to search
// for deposit logs. // for deposit logs.
let deploy_block = env let deploy_block = web3
.runtime() .eth()
.block_on(web3.eth().block_number()) .block_number()
.compat()
.await
.map_err(|e| format!("Failed to get block number: {}", e))?; .map_err(|e| format!("Failed to get block number: {}", e))?;
info!("Present eth1 block number is {}", deploy_block); info!("Present eth1 block number is {}", deploy_block);
@@ -46,13 +49,8 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
confirmations confirmations
); );
let deposit_contract = env let deposit_contract = DepositContract::deploy_testnet(web3, confirmations, password)
.runtime() .await
.block_on(DepositContract::deploy_testnet(
web3,
confirmations,
password,
))
.map_err(|e| format!("Failed to deploy contract: {}", e))?; .map_err(|e| format!("Failed to deploy contract: {}", e))?;
info!( info!(

View File

@@ -1,7 +1,6 @@
use clap::ArgMatches; use clap::ArgMatches;
use environment::Environment; use environment::Environment;
use eth2_testnet_config::Eth2TestnetConfig; use eth2_testnet_config::Eth2TestnetConfig;
use futures::Future;
use genesis::{Eth1Config, Eth1GenesisService}; use genesis::{Eth1Config, Eth1GenesisService};
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Duration; use std::time::Duration;
@@ -10,7 +9,10 @@ use types::EthSpec;
/// Interval between polling the eth1 node for genesis information. /// Interval between polling the eth1 node for genesis information.
pub const ETH1_GENESIS_UPDATE_INTERVAL: Duration = Duration::from_millis(7_000); pub const ETH1_GENESIS_UPDATE_INTERVAL: Duration = Duration::from_millis(7_000);
pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> { pub async fn run<T: EthSpec>(
mut env: Environment<T>,
matches: &ArgMatches<'_>,
) -> Result<(), String> {
let endpoint = matches let endpoint = matches
.value_of("eth1-endpoint") .value_of("eth1-endpoint")
.ok_or_else(|| "eth1-endpoint not specified")?; .ok_or_else(|| "eth1-endpoint not specified")?;
@@ -49,19 +51,17 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
let genesis_service = Eth1GenesisService::new(config, env.core_context().log.clone()); let genesis_service = Eth1GenesisService::new(config, env.core_context().log.clone());
let future = genesis_service let _ = genesis_service
.wait_for_genesis_state(ETH1_GENESIS_UPDATE_INTERVAL, spec) .wait_for_genesis_state(ETH1_GENESIS_UPDATE_INTERVAL, spec)
.await
.map(move |genesis_state| { .map(move |genesis_state| {
eth2_testnet_config.genesis_state = Some(genesis_state); eth2_testnet_config.genesis_state = Some(genesis_state);
eth2_testnet_config.force_write_to_file(testnet_dir) eth2_testnet_config.force_write_to_file(testnet_dir)
}); })
.map_err(|e| format!("Failed to find genesis: {}", e))?;
info!("Starting service to produce genesis BeaconState from eth1"); info!("Starting service to produce genesis BeaconState from eth1");
info!("Connecting to eth1 http endpoint: {}", endpoint); info!("Connecting to eth1 http endpoint: {}", endpoint);
env.runtime()
.block_on(future)
.map_err(|e| format!("Failed to find genesis: {}", e))??;
Ok(()) Ok(())
} }

View File

@@ -22,7 +22,8 @@ use std::time::{SystemTime, UNIX_EPOCH};
use transition_blocks::run_transition_blocks; use transition_blocks::run_transition_blocks;
use types::{test_utils::TestingBeaconStateBuilder, EthSpec, MainnetEthSpec, MinimalEthSpec}; use types::{test_utils::TestingBeaconStateBuilder, EthSpec, MainnetEthSpec, MinimalEthSpec};
fn main() { #[tokio::main]
async fn main() {
simple_logger::init_with_level(Level::Info).expect("logger should initialize"); simple_logger::init_with_level(Level::Info).expect("logger should initialize");
let matches = App::new("Lighthouse CLI Tool") let matches = App::new("Lighthouse CLI Tool")
@@ -393,9 +394,9 @@ fn main() {
} }
match matches.value_of("spec") { match matches.value_of("spec") {
Some("minimal") => run_with_spec!(EnvironmentBuilder::minimal()), Some("minimal") => run_with_spec!(EnvironmentBuilder::minimal()).await,
Some("mainnet") => run_with_spec!(EnvironmentBuilder::mainnet()), Some("mainnet") => run_with_spec!(EnvironmentBuilder::mainnet()).await,
Some("interop") => run_with_spec!(EnvironmentBuilder::interop()), Some("interop") => run_with_spec!(EnvironmentBuilder::interop()).await,
spec => { spec => {
// This path should be unreachable due to slog having a `default_value` // This path should be unreachable due to slog having a `default_value`
unreachable!("Unknown spec configuration: {:?}", spec); unreachable!("Unknown spec configuration: {:?}", spec);
@@ -403,7 +404,7 @@ fn main() {
} }
} }
fn run<T: EthSpec>(env_builder: EnvironmentBuilder<T>, matches: &ArgMatches) { async fn run<T: EthSpec>(env_builder: EnvironmentBuilder<T>, matches: &ArgMatches<'_>) {
let env = env_builder let env = env_builder
.multi_threaded_tokio_runtime() .multi_threaded_tokio_runtime()
.expect("should start tokio runtime") .expect("should start tokio runtime")
@@ -456,13 +457,16 @@ fn run<T: EthSpec>(env_builder: EnvironmentBuilder<T>, matches: &ArgMatches) {
.unwrap_or_else(|e| error!("Failed to pretty print hex: {}", e)), .unwrap_or_else(|e| error!("Failed to pretty print hex: {}", e)),
("deploy-deposit-contract", Some(matches)) => { ("deploy-deposit-contract", Some(matches)) => {
deploy_deposit_contract::run::<T>(env, matches) deploy_deposit_contract::run::<T>(env, matches)
.await
.unwrap_or_else(|e| error!("Failed to run deploy-deposit-contract command: {}", e)) .unwrap_or_else(|e| error!("Failed to run deploy-deposit-contract command: {}", e))
} }
("refund-deposit-contract", Some(matches)) => { ("refund-deposit-contract", Some(matches)) => {
refund_deposit_contract::run::<T>(env, matches) refund_deposit_contract::run::<T>(env, matches)
.await
.unwrap_or_else(|e| error!("Failed to run refund-deposit-contract command: {}", e)) .unwrap_or_else(|e| error!("Failed to run refund-deposit-contract command: {}", e))
} }
("eth1-genesis", Some(matches)) => eth1_genesis::run::<T>(env, matches) ("eth1-genesis", Some(matches)) => eth1_genesis::run::<T>(env, matches)
.await
.unwrap_or_else(|e| error!("Failed to run eth1-genesis command: {}", e)), .unwrap_or_else(|e| error!("Failed to run eth1-genesis command: {}", e)),
("interop-genesis", Some(matches)) => interop_genesis::run::<T>(env, matches) ("interop-genesis", Some(matches)) => interop_genesis::run::<T>(env, matches)
.unwrap_or_else(|e| error!("Failed to run interop-genesis command: {}", e)), .unwrap_or_else(|e| error!("Failed to run interop-genesis command: {}", e)),

View File

@@ -2,7 +2,7 @@ use crate::deploy_deposit_contract::parse_password;
use clap::ArgMatches; use clap::ArgMatches;
use environment::Environment; use environment::Environment;
use eth2_testnet_config::Eth2TestnetConfig; use eth2_testnet_config::Eth2TestnetConfig;
use futures::{future, Future}; use futures::compat::Future01CompatExt;
use std::path::PathBuf; use std::path::PathBuf;
use types::EthSpec; use types::EthSpec;
use web3::{ use web3::{
@@ -14,7 +14,7 @@ use web3::{
/// `keccak("steal()")[0..4]` /// `keccak("steal()")[0..4]`
pub const STEAL_FN_SIGNATURE: &[u8] = &[0xcf, 0x7a, 0x89, 0x65]; pub const STEAL_FN_SIGNATURE: &[u8] = &[0xcf, 0x7a, 0x89, 0x65];
pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<(), String> { pub async fn run<T: EthSpec>(_env: Environment<T>, matches: &ArgMatches<'_>) -> Result<(), String> {
let endpoint = matches let endpoint = matches
.value_of("eth1-endpoint") .value_of("eth1-endpoint")
.ok_or_else(|| "eth1-endpoint not specified")?; .ok_or_else(|| "eth1-endpoint not specified")?;
@@ -46,8 +46,7 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
) )
})?; })?;
let web3_1 = Web3::new(transport); let web3 = Web3::new(transport);
let web3_2 = web3_1.clone();
// Convert from `types::Address` to `web3::types::Address`. // Convert from `types::Address` to `web3::types::Address`.
let deposit_contract = Address::from_slice( let deposit_contract = Address::from_slice(
@@ -56,39 +55,37 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
.as_fixed_bytes(), .as_fixed_bytes(),
); );
let future = web3_1 let from_address = web3
.eth() .eth()
.accounts() .accounts()
.compat()
.await
.map_err(|e| format!("Failed to get accounts: {:?}", e)) .map_err(|e| format!("Failed to get accounts: {:?}", e))
.and_then(move |accounts| { .and_then(|accounts| {
accounts accounts
.get(account_index) .get(account_index)
.cloned() .cloned()
.ok_or_else(|| "Insufficient accounts for deposit".to_string()) .ok_or_else(|| "Insufficient accounts for deposit".to_string())
}) })?;
.and_then(move |from_address| {
let future: Box<dyn Future<Item = Address, Error = String> + Send> = let from = if let Some(password) = password_opt {
if let Some(password) = password_opt {
// Unlock for only a single transaction. // Unlock for only a single transaction.
let duration = None; let duration = None;
let future = web3_1 let result = web3
.personal() .personal()
.unlock_account(from_address, &password, duration) .unlock_account(from_address, &password, duration)
.then(move |result| match result { .compat()
Ok(true) => Ok(from_address), .await;
Ok(false) => Err("Eth1 node refused to unlock account".to_string()), match result {
Err(e) => Err(format!("Eth1 unlock request failed: {:?}", e)), 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)),
Box::new(future) }
} else { } else {
Box::new(future::ok(from_address)) from_address
}; };
future
})
.and_then(move |from| {
let tx_request = TransactionRequest { let tx_request = TransactionRequest {
from, from,
to: Some(deposit_contract), to: Some(deposit_contract),
@@ -100,17 +97,14 @@ pub fn run<T: EthSpec>(mut env: Environment<T>, matches: &ArgMatches) -> Result<
condition: None, condition: None,
}; };
web3_2 let tx = web3
.eth() .eth()
.send_transaction(tx_request) .send_transaction(tx_request)
.map_err(|e| format!("Failed to call deposit fn: {:?}", e)) .compat()
}) .await
.map(move |tx| info!("Refund transaction submitted: eth1_tx_hash: {:?}", tx)) .map_err(|e| format!("Failed to call deposit fn: {:?}", e))?;
.map_err(move |e| error!("Unable to submit refund transaction: error: {}", e));
env.runtime() info!("Refund transaction submitted: eth1_tx_hash: {:?}", tx);
.block_on(future)
.map_err(|()| "Failed to send transaction".to_string())?;
Ok(()) Ok(())
} }