Remove BeaconChain wrapper trait from rpc

This commit is contained in:
Paul Hauner 2019-05-30 18:35:27 +10:00
parent 0590504261
commit 5a5eebca06
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
6 changed files with 15 additions and 84 deletions

View File

@ -1,4 +1,4 @@
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
use beacon_chain::{BeaconChain, BeaconChainTypes};
use futures::Future;
use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink};
use protos::services::{
@ -34,7 +34,7 @@ impl<T: BeaconChainTypes> AttestationService for AttestationServiceInstance<T> {
// verify the slot, drop lock on state afterwards
{
let slot_requested = req.get_slot();
let state = self.chain.get_state();
let state = &self.chain.head().beacon_state;
// Start by performing some checks
// Check that the AttestionData is for the current slot (otherwise it will not be valid)

View File

@ -1,4 +1,4 @@
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
use beacon_chain::{BeaconChain, BeaconChainTypes};
use crossbeam_channel;
use eth2_libp2p::PubsubMessage;
use futures::Future;

View File

@ -1,71 +0,0 @@
use beacon_chain::BeaconChain as RawBeaconChain;
use beacon_chain::{
parking_lot::{RwLockReadGuard, RwLockWriteGuard},
types::{BeaconState, ChainSpec, Signature},
AttestationValidationError, BlockProductionError,
};
pub use beacon_chain::{BeaconChainError, BeaconChainTypes, BlockProcessingOutcome};
use types::{Attestation, AttestationData, BeaconBlock};
/// The RPC's API to the beacon chain.
pub trait BeaconChain<T: BeaconChainTypes>: Send + Sync {
fn get_spec(&self) -> &ChainSpec;
fn get_state(&self) -> RwLockReadGuard<BeaconState<T::EthSpec>>;
fn get_mut_state(&self) -> RwLockWriteGuard<BeaconState<T::EthSpec>>;
fn process_block(&self, block: BeaconBlock)
-> Result<BlockProcessingOutcome, BeaconChainError>;
fn produce_block(
&self,
randao_reveal: Signature,
) -> Result<(BeaconBlock, BeaconState<T::EthSpec>), BlockProductionError>;
fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, BeaconChainError>;
fn process_attestation(
&self,
attestation: Attestation,
) -> Result<(), AttestationValidationError>;
}
impl<T: BeaconChainTypes> BeaconChain<T> for RawBeaconChain<T> {
fn get_spec(&self) -> &ChainSpec {
&self.spec
}
fn get_state(&self) -> RwLockReadGuard<BeaconState<T::EthSpec>> {
self.state.read()
}
fn get_mut_state(&self) -> RwLockWriteGuard<BeaconState<T::EthSpec>> {
self.state.write()
}
fn process_block(
&self,
block: BeaconBlock,
) -> Result<BlockProcessingOutcome, BeaconChainError> {
self.process_block(block)
}
fn produce_block(
&self,
randao_reveal: Signature,
) -> Result<(BeaconBlock, BeaconState<T::EthSpec>), BlockProductionError> {
self.produce_block(randao_reveal)
}
fn produce_attestation_data(&self, shard: u64) -> Result<AttestationData, BeaconChainError> {
self.produce_attestation_data(shard)
}
fn process_attestation(
&self,
attestation: Attestation,
) -> Result<(), AttestationValidationError> {
self.process_attestation(attestation)
}
}

View File

@ -1,10 +1,11 @@
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
use beacon_chain::{BeaconChain, BeaconChainTypes};
use futures::Future;
use grpcio::{RpcContext, UnarySink};
use protos::services::{Empty, Fork, NodeInfoResponse};
use protos::services_grpc::BeaconNodeService;
use slog::{trace, warn};
use std::sync::Arc;
use types::EthSpec;
#[derive(Clone)]
pub struct BeaconNodeServiceInstance<T: BeaconChainTypes> {
@ -22,7 +23,7 @@ impl<T: BeaconChainTypes> BeaconNodeService for BeaconNodeServiceInstance<T> {
node_info.set_version(version::version());
// get the chain state
let state = self.chain.get_state();
let state = &self.chain.head().beacon_state;
let state_fork = state.fork.clone();
let genesis_time = state.genesis_time;
@ -32,10 +33,12 @@ impl<T: BeaconChainTypes> BeaconNodeService for BeaconNodeServiceInstance<T> {
fork.set_current_version(state_fork.current_version.to_vec());
fork.set_epoch(state_fork.epoch.into());
let spec = T::EthSpec::spec();
node_info.set_fork(fork);
node_info.set_genesis_time(genesis_time);
node_info.set_genesis_slot(self.chain.get_spec().genesis_slot.as_u64());
node_info.set_chain_id(u32::from(self.chain.get_spec().chain_id));
node_info.set_genesis_slot(spec.genesis_slot.as_u64());
node_info.set_chain_id(u32::from(spec.chain_id));
// send the node_info the requester
let error_log = self.log.clone();

View File

@ -1,15 +1,14 @@
mod attestation;
mod beacon_block;
pub mod beacon_chain;
mod beacon_node;
pub mod config;
mod validator;
use self::attestation::AttestationServiceInstance;
use self::beacon_block::BeaconBlockServiceInstance;
use self::beacon_chain::{BeaconChain, BeaconChainTypes};
use self::beacon_node::BeaconNodeServiceInstance;
use self::validator::ValidatorServiceInstance;
use beacon_chain::{BeaconChain, BeaconChainTypes};
pub use config::Config as RPCConfig;
use futures::Future;
use grpcio::{Environment, ServerBuilder};

View File

@ -1,4 +1,4 @@
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
use beacon_chain::{BeaconChain, BeaconChainTypes};
use bls::PublicKey;
use futures::Future;
use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink};
@ -7,7 +7,7 @@ use protos::services_grpc::ValidatorService;
use slog::{trace, warn};
use ssz::Decode;
use std::sync::Arc;
use types::{Epoch, RelativeEpoch};
use types::{Epoch, EthSpec, RelativeEpoch};
#[derive(Clone)]
pub struct ValidatorServiceInstance<T: BeaconChainTypes> {
@ -29,8 +29,8 @@ impl<T: BeaconChainTypes> ValidatorService for ValidatorServiceInstance<T> {
let validators = req.get_validators();
trace!(self.log, "RPC request"; "endpoint" => "GetValidatorDuties", "epoch" => req.get_epoch());
let spec = self.chain.get_spec();
let state = self.chain.get_state();
let spec = T::EthSpec::spec();
let state = &self.chain.head().beacon_state;
let epoch = Epoch::from(req.get_epoch());
let mut resp = GetDutiesResponse::new();
let resp_validators = resp.mut_active_validators();