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};
|
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;
|
2019-05-10 04:47:09 +00:00
|
|
|
use types::EthSpec;
|
2019-03-22 02:37:24 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2019-05-10 04:47:09 +00:00
|
|
|
pub struct BeaconNodeServiceInstance<B: EthSpec> {
|
2019-05-08 09:38:18 +00:00
|
|
|
pub chain: Arc<BeaconChain<B>>,
|
2019-03-22 02:37:24 +00:00
|
|
|
pub log: slog::Logger,
|
|
|
|
}
|
|
|
|
|
2019-05-10 04:47:09 +00:00
|
|
|
impl<B: EthSpec> BeaconNodeService for BeaconNodeServiceInstance<B> {
|
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)
|
|
|
|
}
|
|
|
|
}
|