Add http server to beacon node w/ hello world

This commit is contained in:
Paul Hauner 2019-05-25 14:31:13 +10:00
parent 389951530a
commit 8dd07dd7d2
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
6 changed files with 163 additions and 2 deletions

View File

@ -24,6 +24,7 @@ members = [
"beacon_node", "beacon_node",
"beacon_node/store", "beacon_node/store",
"beacon_node/client", "beacon_node/client",
"beacon_node/http_server",
"beacon_node/network", "beacon_node/network",
"beacon_node/eth2-libp2p", "beacon_node/eth2-libp2p",
"beacon_node/rpc", "beacon_node/rpc",

View File

@ -8,6 +8,7 @@ edition = "2018"
beacon_chain = { path = "../beacon_chain" } beacon_chain = { path = "../beacon_chain" }
network = { path = "../network" } network = { path = "../network" }
store = { path = "../store" } store = { path = "../store" }
http_server = { path = "../http_server" }
rpc = { path = "../rpc" } rpc = { path = "../rpc" }
fork_choice = { path = "../../eth2/fork_choice" } fork_choice = { path = "../../eth2/fork_choice" }
types = { path = "../../eth2/types" } types = { path = "../../eth2/types" }

View File

@ -1,5 +1,6 @@
use clap::ArgMatches; use clap::ArgMatches;
use fork_choice::ForkChoiceAlgorithm; use fork_choice::ForkChoiceAlgorithm;
use http_server::HttpServerConfig;
use network::NetworkConfig; use network::NetworkConfig;
use slog::error; use slog::error;
use std::fs; use std::fs;
@ -27,7 +28,7 @@ pub struct ClientConfig {
pub db_type: DBType, pub db_type: DBType,
pub db_name: PathBuf, pub db_name: PathBuf,
pub rpc_conf: rpc::RPCConfig, pub rpc_conf: rpc::RPCConfig,
//pub ipc_conf: pub http_conf: HttpServerConfig, //pub ipc_conf:
} }
impl Default for ClientConfig { impl Default for ClientConfig {
@ -55,6 +56,7 @@ impl Default for ClientConfig {
// default db name for disk-based dbs // default db name for disk-based dbs
db_name: data_dir.join("chain_db"), db_name: data_dir.join("chain_db"),
rpc_conf: rpc::RPCConfig::default(), rpc_conf: rpc::RPCConfig::default(),
http_conf: HttpServerConfig::default(),
} }
} }
} }

View File

@ -98,7 +98,7 @@ impl<TClientType: ClientTypes> Client<TClientType> {
Some(rpc::start_server( Some(rpc::start_server(
&config.rpc_conf, &config.rpc_conf,
executor, executor,
network_send, network_send.clone(),
beacon_chain.clone(), beacon_chain.clone(),
&log, &log,
)) ))
@ -106,6 +106,17 @@ impl<TClientType: ClientTypes> Client<TClientType> {
None None
}; };
// Start the `http_server` service.
//
// Note: presently we are ignoring the config and _always_ starting a HTTP server.
http_server::start_service(
&config.http_conf,
executor,
network_send,
beacon_chain.clone(),
&log,
);
let (slot_timer_exit_signal, exit) = exit_future::signal(); let (slot_timer_exit_signal, exit) = exit_future::signal();
if let Ok(Some(duration_to_next_slot)) = beacon_chain.slot_clock.duration_to_next_slot() { if let Ok(Some(duration_to_next_slot)) = beacon_chain.slot_clock.duration_to_next_slot() {
// set up the validator work interval - start at next slot and proceed every slot // set up the validator work interval - start at next slot and proceed every slot

View File

@ -0,0 +1,31 @@
[package]
name = "http_server"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
bls = { path = "../../eth2/utils/bls" }
beacon_chain = { path = "../beacon_chain" }
iron = "^0.6"
router = "^0.6"
network = { path = "../network" }
eth2-libp2p = { path = "../eth2-libp2p" }
version = { path = "../version" }
types = { path = "../../eth2/types" }
ssz = { path = "../../eth2/utils/ssz" }
slot_clock = { path = "../../eth2/utils/slot_clock" }
protos = { path = "../../protos" }
fork_choice = { path = "../../eth2/fork_choice" }
grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] }
protobuf = "2.0.2"
clap = "2.32.0"
store = { path = "../store" }
dirs = "1.0.3"
futures = "0.1.23"
slog = "^2.2.3"
slog-term = "^2.4.0"
slog-async = "^2.3.0"
tokio = "0.1.17"
exit-future = "0.1.4"
crossbeam-channel = "0.3.8"

View File

@ -0,0 +1,115 @@
use beacon_chain::BeaconChain;
use futures::Future;
use grpcio::{Environment, ServerBuilder};
use network::NetworkMessage;
use protos::services_grpc::{
create_attestation_service, create_beacon_block_service, create_beacon_node_service,
create_validator_service,
};
use slog::{info, o, warn};
use std::net::Ipv4Addr;
use std::sync::Arc;
use tokio::runtime::TaskExecutor;
use types::EthSpec;
use iron::prelude::*;
use iron::{status::Status, Handler, IronResult, Request, Response};
use router::Router;
#[derive(PartialEq, Clone, Debug)]
pub struct HttpServerConfig {
pub enabled: bool,
pub listen_address: String,
/*
pub listen_address: Ipv4Addr,
pub port: u16,
*/
}
impl Default for HttpServerConfig {
fn default() -> Self {
Self {
enabled: false,
listen_address: "127.0.0.1:5051".to_string(),
/*
listen_address: Ipv4Addr::new(127, 0, 0, 1),
port: 5051,
*/
}
}
}
pub struct IndexHandler {
message: String,
}
impl Handler for IndexHandler {
fn handle(&self, _: &mut Request) -> IronResult<Response> {
Ok(Response::with((Status::Ok, self.message.clone())))
}
}
pub fn create_iron_http_server() -> Iron<Router> {
let index_handler = IndexHandler {
message: "Hello world".to_string(),
};
let mut router = Router::new();
router.get("/", index_handler, "index");
Iron::new(router)
}
pub fn start_service<T, U, F, E>(
config: &HttpServerConfig,
executor: &TaskExecutor,
network_chan: crossbeam_channel::Sender<NetworkMessage>,
beacon_chain: Arc<BeaconChain<T, U, F, E>>,
log: &slog::Logger,
) -> exit_future::Signal
where
T: store::Store,
U: slot_clock::SlotClock,
F: fork_choice::ForkChoice,
E: EthSpec,
{
let log = log.new(o!("Service"=>"RPC"));
let env = Arc::new(Environment::new(1));
// Create:
// - `shutdown_trigger` a one-shot to shut down this service.
// - `wait_for_shutdown` a future that will wait until someone calls shutdown.
let (shutdown_trigger, wait_for_shutdown) = exit_future::signal();
let iron = create_iron_http_server();
let spawn_rpc = {
let result = iron.http(config.listen_address.clone());
if result.is_ok() {
info!(log, "HTTP server running on {}", config.listen_address);
} else {
warn!(
log,
"HTTP server failed to start on {}", config.listen_address
);
}
wait_for_shutdown.and_then(move |_| {
info!(log, "HTTP server shutting down");
// TODO: shutdown server.
/*
server
.shutdown()
.wait()
.map(|_| ())
.map_err(|e| warn!(log, "RPC server failed to shutdown: {:?}", e))?;
Ok(())
*/
info!(log, "HTTP server exited");
Ok(())
})
};
executor.spawn(spawn_rpc);
shutdown_trigger
}