From ee6a0ccb92bca9a49b023533155e8b837ab8bfe0 Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 22 Mar 2019 13:37:24 +1100 Subject: [PATCH] Initial build of server-side BeaconNodeService RPC --- beacon_node/client/src/lib.rs | 2 +- beacon_node/rpc/Cargo.toml | 8 ++--- beacon_node/rpc/src/beacon_node.rs | 50 ++++++++++++++++++++++++++++++ beacon_node/rpc/src/lib.rs | 28 +++++++++++++++-- 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 beacon_node/rpc/src/beacon_node.rs diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 914e47fcf..d8da18cae 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -61,7 +61,7 @@ impl Client { // spawn the RPC server if config.rpc_conf.enabled { - rpc::start_server(&config.rpc_conf, &log); + rpc::start_server(&config.rpc_conf, beacon_chain.clone(), &log); } Ok(Client { diff --git a/beacon_node/rpc/Cargo.toml b/beacon_node/rpc/Cargo.toml index 4c3333ee1..acb68972c 100644 --- a/beacon_node/rpc/Cargo.toml +++ b/beacon_node/rpc/Cargo.toml @@ -7,7 +7,10 @@ edition = "2018" [dependencies] bls = { path = "../../eth2/utils/bls" } beacon_chain = { path = "../beacon_chain" } - +version = { path = "../version" } +types = { path = "../../eth2/types" } +ssz = { path = "../../eth2/utils/ssz" } +slot_clock = { path = "../../eth2/utils/slot_clock" } protos = { path = "../../protos" } grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] } protobuf = "2.0.2" @@ -16,8 +19,5 @@ db = { path = "../db" } dirs = "1.0.3" futures = "0.1.23" slog = "^2.2.3" -slot_clock = { path = "../../eth2/utils/slot_clock" } slog-term = "^2.4.0" slog-async = "^2.3.0" -types = { path = "../../eth2/types" } -ssz = { path = "../../eth2/utils/ssz" } diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs new file mode 100644 index 000000000..8dd7721fd --- /dev/null +++ b/beacon_node/rpc/src/beacon_node.rs @@ -0,0 +1,50 @@ +use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain}; +use futures::Future; +use grpcio::{RpcContext, UnarySink}; +use protos::services::{Empty, Fork, NodeInfo}; +use protos::services_grpc::BeaconNodeService; +use slog::{debug, trace, warn}; +use std::sync::Arc; + +#[derive(Clone)] +pub struct BeaconNodeServiceInstance +where + T: ClientDB + Clone, + U: SlotClock + Clone, + F: ForkChoice + Clone, +{ + pub chain: Arc>, + pub log: slog::Logger, +} + +impl BeaconNodeService for BeaconNodeServiceInstance +where + T: ClientDB + Clone, + U: SlotClock + Clone, + F: ForkChoice + Clone, +{ + /// Provides basic node information. + fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { + trace!(self.log, "Node info requested via RPC"); + + let mut node_info = NodeInfo::new(); + node_info.set_version(version::version()); + // get the chain state fork + let state_fork = self.chain.state.read().fork.clone(); + // build the rpc fork struct + let mut fork = Fork::new(); + fork.set_previous_version(state_fork.previous_version.to_vec()); + fork.set_current_version(state_fork.current_version.to_vec()); + fork.set_epoch(state_fork.epoch.into()); + node_info.set_fork(fork); + + node_info.set_chain_id(self.chain.spec.chain_id as u32); + + // send the node_info the requester + let error_log = self.log.clone(); + let f = sink + .success(node_info) + .map_err(move |e| warn!(error_log, "failed to reply {:?}", e)); + ctx.spawn(f) + } +} diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 7f776d7d8..2f2abcc1f 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -1,20 +1,44 @@ mod beacon_block; +mod beacon_node; pub mod config; mod validator; use self::beacon_block::BeaconBlockServiceInstance; +use self::beacon_node::BeaconNodeServiceInstance; use self::validator::ValidatorServiceInstance; +use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain}; pub use config::Config as RPCConfig; use grpcio::{Environment, Server, ServerBuilder}; -use protos::services_grpc::{create_beacon_block_service, create_validator_service}; +use protos::services_grpc::{ + create_beacon_block_service, create_beacon_node_service, create_validator_service, +}; use std::sync::Arc; use slog::{info, o}; -pub fn start_server(config: &RPCConfig, log: &slog::Logger) -> Server { +pub fn start_server( + config: &RPCConfig, + beacon_chain: Arc>, + log: &slog::Logger, +) -> Server +where + T: ClientDB + Clone + 'static, + U: SlotClock + Clone + 'static, + F: ForkChoice + Clone + 'static, +{ let log = log.new(o!("Service"=>"RPC")); let env = Arc::new(Environment::new(1)); + // build the individual rpc services + + let beacon_node_service = { + let instance = BeaconNodeServiceInstance { + chain: beacon_chain.clone(), + log: log.clone(), + }; + create_beacon_node_service(instance) + }; + let beacon_block_service = { let instance = BeaconBlockServiceInstance { log: log.clone() }; create_beacon_block_service(instance)