diff --git a/beacon_node/rpc/src/beacon_chain.rs b/beacon_node/rpc/src/beacon_chain.rs new file mode 100644 index 000000000..9b2681876 --- /dev/null +++ b/beacon_node/rpc/src/beacon_chain.rs @@ -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; +} + +impl BeaconChain for RawBeaconChain +where + T: ClientDB + Sized, + U: SlotClock, + F: ForkChoice, +{ + fn get_spec(&self) -> &ChainSpec { + &self.spec + } + + fn get_state(&self) -> RwLockReadGuard { + self.state.read() + } +} diff --git a/beacon_node/rpc/src/beacon_node.rs b/beacon_node/rpc/src/beacon_node.rs index 8dd7721fd..5a542cb90 100644 --- a/beacon_node/rpc/src/beacon_node.rs +++ b/beacon_node/rpc/src/beacon_node.rs @@ -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 grpcio::{RpcContext, UnarySink}; use protos::services::{Empty, Fork, NodeInfo}; @@ -7,22 +7,12 @@ 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 struct BeaconNodeServiceInstance { + pub chain: Arc, pub log: slog::Logger, } -impl BeaconNodeService for BeaconNodeServiceInstance -where - T: ClientDB + Clone, - U: SlotClock + Clone, - F: ForkChoice + Clone, -{ +impl BeaconNodeService for BeaconNodeServiceInstance { /// Provides basic node information. fn info(&mut self, ctx: RpcContext, _req: Empty, sink: UnarySink) { trace!(self.log, "Node info requested via RPC"); @@ -30,7 +20,7 @@ where 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(); + let state_fork = self.chain.get_state().fork.clone(); // build the rpc fork struct let mut fork = Fork::new(); fork.set_previous_version(state_fork.previous_version.to_vec()); @@ -38,7 +28,7 @@ where fork.set_epoch(state_fork.epoch.into()); 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 let error_log = self.log.clone(); diff --git a/beacon_node/rpc/src/lib.rs b/beacon_node/rpc/src/lib.rs index 2f2abcc1f..4565abf7a 100644 --- a/beacon_node/rpc/src/lib.rs +++ b/beacon_node/rpc/src/lib.rs @@ -1,12 +1,13 @@ mod beacon_block; +pub mod beacon_chain; mod beacon_node; pub mod config; mod validator; use self::beacon_block::BeaconBlockServiceInstance; +use self::beacon_chain::BeaconChain; 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::{ @@ -16,16 +17,11 @@ use std::sync::Arc; use slog::{info, o}; -pub fn start_server( +pub fn start_server( config: &RPCConfig, - beacon_chain: Arc>, + beacon_chain: Arc, log: &slog::Logger, -) -> Server -where - T: ClientDB + Clone + 'static, - U: SlotClock + Clone + 'static, - F: ForkChoice + Clone + 'static, -{ +) -> Server { let log = log.new(o!("Service"=>"RPC")); let env = Arc::new(Environment::new(1));