2019-05-25 10:51:15 +00:00
|
|
|
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
|
2019-03-22 02:37:24 +00:00
|
|
|
use futures::Future;
|
|
|
|
use grpcio::{RpcContext, UnarySink};
|
2019-03-26 04:04:39 +00:00
|
|
|
use protos::services::{Empty, Fork, NodeInfoResponse};
|
2019-03-22 02:37:24 +00:00
|
|
|
use protos::services_grpc::BeaconNodeService;
|
2019-03-22 12:01:10 +00:00
|
|
|
use slog::{trace, warn};
|
2019-03-22 02:37:24 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2019-05-25 10:51:15 +00:00
|
|
|
pub struct BeaconNodeServiceInstance<T: BeaconChainTypes> {
|
|
|
|
pub chain: Arc<BeaconChain<T>>,
|
2019-03-22 02:37:24 +00:00
|
|
|
pub log: slog::Logger,
|
|
|
|
}
|
|
|
|
|
2019-05-25 10:51:15 +00:00
|
|
|
impl<T: BeaconChainTypes> BeaconNodeService for BeaconNodeServiceInstance<T> {
|
2019-03-22 02:37:24 +00:00
|
|
|
/// Provides basic node information.
|
2019-03-26 04:04:39 +00:00
|
|
|
fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink<NodeInfoResponse>) {
|
2019-03-22 02:37:24 +00:00
|
|
|
trace!(self.log, "Node info requested via RPC");
|
|
|
|
|
2019-03-22 12:01:10 +00:00
|
|
|
// build the response
|
2019-03-26 04:04:39 +00:00
|
|
|
let mut node_info = NodeInfoResponse::new();
|
2019-03-22 02:37:24 +00:00
|
|
|
node_info.set_version(version::version());
|
2019-03-22 12:01:10 +00:00
|
|
|
|
|
|
|
// get the chain state
|
|
|
|
let state = self.chain.get_state();
|
|
|
|
let state_fork = state.fork.clone();
|
2019-04-03 05:23:09 +00:00
|
|
|
let genesis_time = state.genesis_time;
|
2019-03-22 12:01:10 +00:00
|
|
|
|
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());
|
|
|
|
|
2019-03-22 12:01:10 +00:00
|
|
|
node_info.set_fork(fork);
|
|
|
|
node_info.set_genesis_time(genesis_time);
|
2019-03-26 04:44:28 +00:00
|
|
|
node_info.set_genesis_slot(self.chain.get_spec().genesis_slot.as_u64());
|
2019-04-03 05:23:09 +00:00
|
|
|
node_info.set_chain_id(u32::from(self.chain.get_spec().chain_id));
|
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)
|
|
|
|
}
|
|
|
|
}
|