2019-03-22 02:51:17 +00:00
|
|
|
use crate::beacon_chain::BeaconChain;
|
2019-03-22 02:37:24 +00:00
|
|
|
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)]
|
2019-03-22 02:51:17 +00:00
|
|
|
pub struct BeaconNodeServiceInstance {
|
|
|
|
pub chain: Arc<BeaconChain>,
|
2019-03-22 02:37:24 +00:00
|
|
|
pub log: slog::Logger,
|
|
|
|
}
|
|
|
|
|
2019-03-22 02:51:17 +00:00
|
|
|
impl BeaconNodeService for BeaconNodeServiceInstance {
|
2019-03-22 02:37:24 +00:00
|
|
|
/// Provides basic node information.
|
|
|
|
fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink<NodeInfo>) {
|
|
|
|
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
|
2019-03-22 02:51:17 +00:00
|
|
|
let state_fork = self.chain.get_state().fork.clone();
|
2019-03-22 02:37:24 +00:00
|
|
|
// 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);
|
|
|
|
|
2019-03-22 02:51:17 +00:00
|
|
|
node_info.set_chain_id(self.chain.get_spec().chain_id as u32);
|
2019-03-22 02:37:24 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|