Add beacon_chain trait for gRPC server
This commit is contained in:
parent
ee6a0ccb92
commit
858cf4f1f4
31
beacon_node/rpc/src/beacon_chain.rs
Normal file
31
beacon_node/rpc/src/beacon_chain.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use beacon_chain::BeaconChain as RawBeaconChain;
|
||||||
|
use beacon_chain::{
|
||||||
|
db::ClientDB,
|
||||||
|
fork_choice::ForkChoice,
|
||||||
|
parking_lot::RwLockReadGuard,
|
||||||
|
slot_clock::SlotClock,
|
||||||
|
types::{BeaconState, ChainSpec},
|
||||||
|
CheckPoint,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The RPC's API to the beacon chain.
|
||||||
|
pub trait BeaconChain: Send + Sync {
|
||||||
|
fn get_spec(&self) -> &ChainSpec;
|
||||||
|
|
||||||
|
fn get_state(&self) -> RwLockReadGuard<BeaconState>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U, F> BeaconChain for RawBeaconChain<T, U, F>
|
||||||
|
where
|
||||||
|
T: ClientDB + Sized,
|
||||||
|
U: SlotClock,
|
||||||
|
F: ForkChoice,
|
||||||
|
{
|
||||||
|
fn get_spec(&self) -> &ChainSpec {
|
||||||
|
&self.spec
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_state(&self) -> RwLockReadGuard<BeaconState> {
|
||||||
|
self.state.read()
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain};
|
use crate::beacon_chain::BeaconChain;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use grpcio::{RpcContext, UnarySink};
|
use grpcio::{RpcContext, UnarySink};
|
||||||
use protos::services::{Empty, Fork, NodeInfo};
|
use protos::services::{Empty, Fork, NodeInfo};
|
||||||
@ -7,22 +7,12 @@ use slog::{debug, trace, warn};
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BeaconNodeServiceInstance<T, U, F>
|
pub struct BeaconNodeServiceInstance {
|
||||||
where
|
pub chain: Arc<BeaconChain>,
|
||||||
T: ClientDB + Clone,
|
|
||||||
U: SlotClock + Clone,
|
|
||||||
F: ForkChoice + Clone,
|
|
||||||
{
|
|
||||||
pub chain: Arc<BeaconChain<T, U, F>>,
|
|
||||||
pub log: slog::Logger,
|
pub log: slog::Logger,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U, F> BeaconNodeService for BeaconNodeServiceInstance<T, U, F>
|
impl BeaconNodeService for BeaconNodeServiceInstance {
|
||||||
where
|
|
||||||
T: ClientDB + Clone,
|
|
||||||
U: SlotClock + Clone,
|
|
||||||
F: ForkChoice + Clone,
|
|
||||||
{
|
|
||||||
/// Provides basic node information.
|
/// Provides basic node information.
|
||||||
fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink<NodeInfo>) {
|
fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink<NodeInfo>) {
|
||||||
trace!(self.log, "Node info requested via RPC");
|
trace!(self.log, "Node info requested via RPC");
|
||||||
@ -30,7 +20,7 @@ where
|
|||||||
let mut node_info = NodeInfo::new();
|
let mut node_info = NodeInfo::new();
|
||||||
node_info.set_version(version::version());
|
node_info.set_version(version::version());
|
||||||
// get the chain state fork
|
// get the chain state fork
|
||||||
let state_fork = self.chain.state.read().fork.clone();
|
let state_fork = self.chain.get_state().fork.clone();
|
||||||
// build the rpc fork struct
|
// build the rpc fork struct
|
||||||
let mut fork = Fork::new();
|
let mut fork = Fork::new();
|
||||||
fork.set_previous_version(state_fork.previous_version.to_vec());
|
fork.set_previous_version(state_fork.previous_version.to_vec());
|
||||||
@ -38,7 +28,7 @@ where
|
|||||||
fork.set_epoch(state_fork.epoch.into());
|
fork.set_epoch(state_fork.epoch.into());
|
||||||
node_info.set_fork(fork);
|
node_info.set_fork(fork);
|
||||||
|
|
||||||
node_info.set_chain_id(self.chain.spec.chain_id as u32);
|
node_info.set_chain_id(self.chain.get_spec().chain_id as u32);
|
||||||
|
|
||||||
// send the node_info the requester
|
// send the node_info the requester
|
||||||
let error_log = self.log.clone();
|
let error_log = self.log.clone();
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
mod beacon_block;
|
mod beacon_block;
|
||||||
|
pub mod beacon_chain;
|
||||||
mod beacon_node;
|
mod beacon_node;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
mod validator;
|
mod validator;
|
||||||
|
|
||||||
use self::beacon_block::BeaconBlockServiceInstance;
|
use self::beacon_block::BeaconBlockServiceInstance;
|
||||||
|
use self::beacon_chain::BeaconChain;
|
||||||
use self::beacon_node::BeaconNodeServiceInstance;
|
use self::beacon_node::BeaconNodeServiceInstance;
|
||||||
use self::validator::ValidatorServiceInstance;
|
use self::validator::ValidatorServiceInstance;
|
||||||
use beacon_chain::{db::ClientDB, fork_choice::ForkChoice, slot_clock::SlotClock, BeaconChain};
|
|
||||||
pub use config::Config as RPCConfig;
|
pub use config::Config as RPCConfig;
|
||||||
use grpcio::{Environment, Server, ServerBuilder};
|
use grpcio::{Environment, Server, ServerBuilder};
|
||||||
use protos::services_grpc::{
|
use protos::services_grpc::{
|
||||||
@ -16,16 +17,11 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use slog::{info, o};
|
use slog::{info, o};
|
||||||
|
|
||||||
pub fn start_server<T, U, F>(
|
pub fn start_server(
|
||||||
config: &RPCConfig,
|
config: &RPCConfig,
|
||||||
beacon_chain: Arc<BeaconChain<T, U, F>>,
|
beacon_chain: Arc<BeaconChain>,
|
||||||
log: &slog::Logger,
|
log: &slog::Logger,
|
||||||
) -> Server
|
) -> Server {
|
||||||
where
|
|
||||||
T: ClientDB + Clone + 'static,
|
|
||||||
U: SlotClock + Clone + 'static,
|
|
||||||
F: ForkChoice + Clone + 'static,
|
|
||||||
{
|
|
||||||
let log = log.new(o!("Service"=>"RPC"));
|
let log = log.new(o!("Service"=>"RPC"));
|
||||||
let env = Arc::new(Environment::new(1));
|
let env = Arc::new(Environment::new(1));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user