diff --git a/beacon_node/rest_api/src/beacon.rs b/beacon_node/rest_api/src/beacon.rs index 23ef12aa8..d74ab2ed9 100644 --- a/beacon_node/rest_api/src/beacon.rs +++ b/beacon_node/rest_api/src/beacon.rs @@ -191,13 +191,7 @@ pub fn get_state(req: Request) -> ApiResult Ok(query) => { // We have *some* parameters, just check them. let query_params = ["root", "slot"]; - match query.first_of(&query_params) { - Ok((k, v)) => (k, v), - Err(e) => { - // Wrong parameters provided, or another error, return the error. - return Err(e); - } - } + query.first_of(&query_params)? } Err(ApiError::InvalidQueryParams(_)) => { // No parameters provided at all, use current slot. diff --git a/beacon_node/rest_api/src/error.rs b/beacon_node/rest_api/src/error.rs index 138affae4..82dc73da0 100644 --- a/beacon_node/rest_api/src/error.rs +++ b/beacon_node/rest_api/src/error.rs @@ -1,4 +1,6 @@ use crate::BoxFut; +use futures::future::IntoFuture; +use futures::Future; use hyper::{Body, Method, Request, Response, Server, StatusCode}; use std::error::Error as StdError; diff --git a/beacon_node/rest_api/src/lib.rs b/beacon_node/rest_api/src/lib.rs index 97ed971c2..3903d0ea1 100644 --- a/beacon_node/rest_api/src/lib.rs +++ b/beacon_node/rest_api/src/lib.rs @@ -21,6 +21,7 @@ use client_network::NetworkMessage; use client_network::Service as NetworkService; use error::{ApiError, ApiResult}; use eth2_config::Eth2Config; +use futures::future::IntoFuture; use hyper::rt::Future; use hyper::server::conn::AddrStream; use hyper::service::{MakeService, Service}; @@ -33,7 +34,6 @@ use std::sync::Arc; use tokio::runtime::TaskExecutor; use tokio::sync::mpsc; use url_query::UrlQuery; -use futures::future::IntoFuture; pub use beacon::{BlockResponse, HeadResponse, StateResponse}; pub use config::Config as ApiConfig; @@ -51,6 +51,14 @@ pub struct ApiService { eth2_config: Arc, } +fn into_boxfut(item: F) -> BoxFut +where + F: IntoFuture, Error = ApiError>, + F::Future: Send, +{ + Box::new(item.into_future()) +} + impl Service for ApiService { type ReqBody = Body; type ResBody = Body; @@ -82,20 +90,25 @@ impl Service for ApiService { // Route the request to the correct handler. let result = match (req.method(), path.as_ref()) { // Methods for Client - (&Method::GET, "/node/version") => Box::new(node::get_version(req).into_future()), - (&Method::GET, "/node/genesis_time") => Box::new(node::get_genesis_time::(req).into_future()), - (&Method::GET, "/node/syncing") => Box::new(helpers::implementation_pending_response(req).into_future()), - /* + (&Method::GET, "/node/version") => into_boxfut(node::get_version(req)), + (&Method::GET, "/node/genesis_time") => into_boxfut(node::get_genesis_time::(req)), + (&Method::GET, "/node/syncing") => { + into_boxfut(helpers::implementation_pending_response(req)) + } // Methods for Network - (&Method::GET, "/network/enr") => network::get_enr::(req), - (&Method::GET, "/network/peer_count") => network::get_peer_count::(req), - (&Method::GET, "/network/peer_id") => network::get_peer_id::(req), - (&Method::GET, "/network/peers") => network::get_peer_list::(req), - (&Method::GET, "/network/listen_port") => network::get_listen_port::(req), - (&Method::GET, "/network/listen_addresses") => network::get_listen_addresses::(req), - + (&Method::GET, "/network/enr") => into_boxfut(network::get_enr::(req)), + (&Method::GET, "/network/peer_count") => into_boxfut(network::get_peer_count::(req)), + (&Method::GET, "/network/peer_id") => into_boxfut(network::get_peer_id::(req)), + (&Method::GET, "/network/peers") => into_boxfut(network::get_peer_list::(req)), + (&Method::GET, "/network/listen_port") => { + into_boxfut(network::get_listen_port::(req)) + } + (&Method::GET, "/network/listen_addresses") => { + into_boxfut(network::get_listen_addresses::(req)) + } /* + // Methods for Beacon Node (&Method::GET, "/beacon/head") => beacon::get_head::(req), (&Method::GET, "/beacon/block") => beacon::get_block::(req), diff --git a/beacon_node/rest_api/src/network.rs b/beacon_node/rest_api/src/network.rs index e037d43f0..26e5623c2 100644 --- a/beacon_node/rest_api/src/network.rs +++ b/beacon_node/rest_api/src/network.rs @@ -1,5 +1,7 @@ +use crate::error::{ApiError, ApiResult}; use crate::helpers::*; -use crate::{ApiError, BoxFut, NetworkService}; +use crate::response_builder::ResponseBuilder; +use crate::NetworkService; use beacon_chain::BeaconChainTypes; use eth2_libp2p::{Enr, Multiaddr, PeerId}; use hyper::{Body, Request}; @@ -8,81 +10,70 @@ use std::sync::Arc; /// HTTP handler to return the list of libp2p multiaddr the client is listening on. /// /// Returns a list of `Multiaddr`, serialized according to their `serde` impl. -pub fn get_listen_addresses(req: Request) -> BoxFut { +pub fn get_listen_addresses(req: Request) -> ApiResult { let network = req .extensions() .get::>>() .expect("The network service should always be there, we put it there"); let multiaddresses: Vec = network.listen_multiaddrs(); - success_response_json(req, &multiaddresses) + ResponseBuilder::new(&req).body_json(&multiaddresses) } /// HTTP handler to return the network port the client is listening on. /// /// Returns the TCP port number in its plain form (which is also valid JSON serialization) -pub fn get_listen_port(req: Request) -> BoxFut { +pub fn get_listen_port(req: Request) -> ApiResult { let network = req .extensions() .get::>>() .expect("The network service should always be there, we put it there") .clone(); - - success_response(req, &network.listen_port()) + ResponseBuilder::new(&req).body(&network.listen_port()) } /// HTTP handler to return the Discv5 ENR from the client's libp2p service. /// /// ENR is encoded as base64 string. -pub fn get_enr(req: Request) -> BoxFut { +pub fn get_enr(req: Request) -> ApiResult { let network = req .extensions() .get::>>() .expect("The network service should always be there, we put it there"); - - let enr: Enr = network.local_enr(); - success_response_json(req, &enr.to_base64()) + ResponseBuilder::new(&req).body_json(&network.local_enr().to_base64()) } /// HTTP handler to return the `PeerId` from the client's libp2p service. /// /// PeerId is encoded as base58 string. -pub fn get_peer_id(req: Request) -> BoxFut { +pub fn get_peer_id(req: Request) -> ApiResult { let network = req .extensions() .get::>>() .expect("The network service should always be there, we put it there"); - - let peer_id: PeerId = network.local_peer_id(); - - success_response_json(req, &peer_id.to_base58()) + ResponseBuilder::new(&req).body_json(&network.local_peer_id().to_base58()) } /// HTTP handler to return the number of peers connected in the client's libp2p service. -pub fn get_peer_count(req: Request) -> BoxFut { +pub fn get_peer_count(req: Request) -> ApiResult { let network = req .extensions() .get::>>() .expect("The network service should always be there, we put it there"); - - let connected_peers: usize = network.connected_peers(); - - success_response(req, &connected_peers) + ResponseBuilder::new(&req).body(&network.connected_peers()) } /// HTTP handler to return the list of peers connected to the client's libp2p service. /// /// Peers are presented as a list of `PeerId::to_string()`. -pub fn get_peer_list(req: Request) -> BoxFut { +pub fn get_peer_list(req: Request) -> ApiResult { let network = req .extensions() .get::>>() .expect("The network service should always be there, we put it there"); - let connected_peers: Vec = network .connected_peer_set() .iter() .map(PeerId::to_string) .collect(); - - success_response_json(req, &connected_peers) + ResponseBuilder::new(&req).body_json(&connected_peers) } diff --git a/beacon_node/rest_api/src/node.rs b/beacon_node/rest_api/src/node.rs index 433aae6cf..5ef35f9a0 100644 --- a/beacon_node/rest_api/src/node.rs +++ b/beacon_node/rest_api/src/node.rs @@ -1,4 +1,5 @@ use crate::helpers::*; +use crate::response_builder::ResponseBuilder; use crate::{ApiResult, BoxFut}; use beacon_chain::BeaconChainTypes; use hyper::{Body, Request}; @@ -6,13 +7,12 @@ use version; /// Read the version string from the current Lighthouse build. pub fn get_version(req: Request) -> ApiResult { - success_response_2_json(req, &version::version()) + ResponseBuilder::new(&req).body_json(&version::version()) } /// Read the genesis time from the current beacon chain state. pub fn get_genesis_time(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; let head_state = get_head_state(beacon_chain)?; - let gen_time: u64 = head_state.genesis_time; - success_response_2(req, &gen_time) + ResponseBuilder::new(&req).body(&head_state.genesis_time) } diff --git a/beacon_node/rest_api/src/validator.rs b/beacon_node/rest_api/src/validator.rs index dfa35e298..7d0fbdabd 100644 --- a/beacon_node/rest_api/src/validator.rs +++ b/beacon_node/rest_api/src/validator.rs @@ -1,6 +1,6 @@ use crate::helpers::*; use crate::response_builder::ResponseBuilder; -use crate::{ApiError, ApiResult, UrlQuery, BoxFut}; +use crate::{ApiError, ApiResult, BoxFut, UrlQuery}; use beacon_chain::{BeaconChainTypes, BlockProcessingOutcome}; use bls::{AggregateSignature, PublicKey, Signature}; use futures::future::Future; @@ -197,7 +197,7 @@ pub fn get_new_beacon_block(req: Request) - pub fn publish_beacon_block(req: Request) -> BoxFut { let _ = try_future!(check_content_type_for_json(&req)); let log = get_logger_from_request(&req); - let (beacon_chain, _head_state) = try_future!(get_beacon_chain_from_request::(&req)); + let beacon_chain = try_future!(get_beacon_chain_from_request::(&req)); // Get the network sending channel from the request, for later transmission let network_chan = req .extensions() @@ -250,8 +250,6 @@ pub fn publish_beacon_block(req: Request) - }).and_then(|_| { response_builder.body_json(&()) })) - - } /// HTTP Handler to produce a new Attestation from the current state, ready to be signed by a validator. diff --git a/validator_client/src/block_producer/mod.rs b/validator_client/src/block_producer/mod.rs index 0716d740c..bb9c5741d 100644 --- a/validator_client/src/block_producer/mod.rs +++ b/validator_client/src/block_producer/mod.rs @@ -63,12 +63,12 @@ impl<'a, B: BeaconNodeBlock, S: Signer, E: EthSpec> BlockProducer<'a, B, S, E> { pub fn handle_produce_block(&mut self) { match self.produce_block() { Ok(ValidatorEvent::BlockProduced(slot)) => info!( - log, + self.log, "Block produced"; "validator" => format!("{}", self.signer), "slot" => slot, ), - Err(e) => error!(log, "Block production error"; "Error" => format!("{:?}", e)), + Err(e) => error!(self.log, "Block production error"; "Error" => format!("{:?}", e)), Ok(ValidatorEvent::SignerRejection(_slot)) => { error!(self.log, "Block production error"; "Error" => "Signer Could not sign the block".to_string()) }