From 80ecafaae4c1df40df3b39c940e33ef63bc6f1e7 Mon Sep 17 00:00:00 2001 From: Pawan Dhananjay Date: Wed, 23 Sep 2020 01:19:58 +0000 Subject: [PATCH] Add `--staking` flag (#1641) ## Issue Addressed Closes #1472 ## Proposed Changes Add `--staking` ~~and`staking-with-eth1-endpoint`~~ flag to improve UX for stakers. Co-authored-by: Paul Hauner --- beacon_node/client/src/builder.rs | 9 ++++++++- beacon_node/eth1/src/http.rs | 13 +++++++++++++ beacon_node/src/cli.rs | 13 +++++++++++++ beacon_node/src/config.rs | 23 +++++++++++++++++++++-- book/src/become-a-validator-source.md | 7 +++++-- 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/beacon_node/client/src/builder.rs b/beacon_node/client/src/builder.rs index 16e7a0264..f12d70e90 100644 --- a/beacon_node/client/src/builder.rs +++ b/beacon_node/client/src/builder.rs @@ -599,8 +599,15 @@ where .ok_or_else(|| "caching_eth1_backend requires a chain spec".to_string())?; // Check if the eth1 endpoint we connect to is on the correct network id. + // Note: This check also effectively checks the eth1 http connection before the beacon chain + // is completely started and fails loudly if there is an issue. let network_id = - eth1::http::get_network_id(&config.endpoint, Duration::from_millis(15_000)).await?; + eth1::http::check_eth1_endpoint(&config.endpoint, Duration::from_millis(15_000)) + .await + .map_err(|_| "Error connecting to eth1 node.\n\ + Please ensure that you have an eth1 http server running locally on localhost:8545 \ + or pass an external endpoint using `--eth1-endpoint `.\n\ + Also ensure that `eth` and `net` apis are enabled on the eth1 http server.".to_string())?; if network_id != config.network_id { return Err(format!( diff --git a/beacon_node/eth1/src/http.rs b/beacon_node/eth1/src/http.rs index 6dffdaa7c..77f705cd7 100644 --- a/beacon_node/eth1/src/http.rs +++ b/beacon_node/eth1/src/http.rs @@ -55,6 +55,19 @@ impl FromStr for Eth1NetworkId { } } +/// Checks that the provided eth1 node has all the relevant api endpoints open +/// and returns the network id. +pub async fn check_eth1_endpoint( + endpoint: &str, + timeout: Duration, +) -> Result { + // Checks that the "eth" api works as expected. + let _block_number = get_block_number(endpoint, timeout).await?; + // Checks that the "net" api works as expected. + let network_id = get_network_id(endpoint, timeout).await?; + Ok(network_id) +} + /// Get the eth1 network id of the given endpoint. pub async fn get_network_id(endpoint: &str, timeout: Duration) -> Result { let response_body = send_rpc_request(endpoint, "net_version", json!([]), timeout).await?; diff --git a/beacon_node/src/cli.rs b/beacon_node/src/cli.rs index ff64a600c..9f6ee79b1 100644 --- a/beacon_node/src/cli.rs +++ b/beacon_node/src/cli.rs @@ -193,6 +193,19 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { .takes_value(true), ) + /* + * Standard staking flags + */ + + .arg( + Arg::with_name("staking") + .long("staking") + .help("Standard option for a staking beacon node. Equivalent to \ + `lighthouse bn --http --eth1 `. This will enable the http server on localhost:5052 \ + and try connecting to an eth1 node on localhost:8545") + .takes_value(false) + ) + /* * Eth1 Integration */ diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index b33a14001..42b3b8277 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -4,7 +4,7 @@ use clap_utils::BAD_TESTNET_DIR_MESSAGE; use client::{config::DEFAULT_DATADIR, ClientConfig, ClientGenesis}; use eth2_libp2p::{multiaddr::Protocol, Enr, Multiaddr, NetworkConfig, PeerIdSerialized}; use eth2_testnet_config::Eth2TestnetConfig; -use slog::{crit, info, Logger}; +use slog::{crit, info, warn, Logger}; use ssz::Encode; use std::cmp; use std::fs; @@ -83,6 +83,16 @@ pub fn get_config( false, )?; + /* + * Staking flag + * Note: the config values set here can be overwritten by other more specific cli params + */ + + if cli_args.is_present("staking") { + client_config.rest_api.enabled = true; + client_config.sync_eth1_chain = true; + } + /* * Http server */ @@ -112,6 +122,15 @@ pub fn get_config( client_config.rest_api.allow_origin = allow_origin.to_string(); } + // Log a warning indicating an open HTTP server if it wasn't specified explicitly + // (e.g. using the --staking flag). + if cli_args.is_present("staking") { + warn!( + log, + "Running HTTP server on port {}", client_config.rest_api.port + ); + } + /* * Websocket server */ @@ -426,7 +445,7 @@ pub fn set_network_config( if cli_args.is_present("disable-discovery") { config.disable_discovery = true; - slog::warn!(log, "Discovery is disabled. New peers will not be found"); + warn!(log, "Discovery is disabled. New peers will not be found"); } Ok(()) diff --git a/book/src/become-a-validator-source.md b/book/src/become-a-validator-source.md index ce44d0c5f..8b1169855 100644 --- a/book/src/become-a-validator-source.md +++ b/book/src/become-a-validator-source.md @@ -47,7 +47,7 @@ the internet and maintains a view of the chain. Start your beacon node with: ```bash - lighthouse --testnet medalla beacon --eth1 --http + lighthouse --testnet medalla beacon --staking ``` > The `--testnet` parameter is optional. Omitting it will default to the @@ -55,8 +55,11 @@ Start your beacon node with: > Current values are either `altona` or `medalla`. This is true for all the > following commands in this document. ->Note: the `--http` flag enables the HTTP API for the validator client. And the `--eth1` flag tells the beacon node that it should sync with an Ethereum1 node (e.g. Geth). These flags are only required if you wish to run a validator. +You can also pass an external http endpoint (e.g. Infura) for the Eth1 node using the `--eth1-endpoint` flag: +```bash + lighthouse --testnet medalla beacon --staking --eth1-endpoint +``` Your beacon node has started syncing when you see the following (truncated) log: