2019-03-18 06:38:23 +00:00
|
|
|
use beacon_chain::BeaconChain as RawBeaconChain;
|
|
|
|
use beacon_chain::{
|
2019-03-18 13:26:15 +00:00
|
|
|
db::ClientDB,
|
|
|
|
fork_choice::ForkChoice,
|
|
|
|
parking_lot::RwLockReadGuard,
|
|
|
|
slot_clock::SlotClock,
|
|
|
|
types::{BeaconState, ChainSpec},
|
2019-03-25 03:27:20 +00:00
|
|
|
AggregationOutcome, CheckPoint,
|
2019-03-18 06:38:23 +00:00
|
|
|
};
|
2019-03-21 20:11:04 +00:00
|
|
|
use eth2_libp2p::HelloMessage;
|
2019-03-25 03:27:20 +00:00
|
|
|
use types::{Attestation, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot};
|
2019-03-23 02:23:44 +00:00
|
|
|
|
2019-03-23 07:48:09 +00:00
|
|
|
pub use beacon_chain::{BeaconChainError, BlockProcessingOutcome};
|
2019-03-18 06:38:23 +00:00
|
|
|
|
|
|
|
/// The network's API to the beacon chain.
|
|
|
|
pub trait BeaconChain: Send + Sync {
|
2019-03-18 13:05:06 +00:00
|
|
|
fn get_spec(&self) -> &ChainSpec;
|
|
|
|
|
2019-03-18 13:26:15 +00:00
|
|
|
fn get_state(&self) -> RwLockReadGuard<BeaconState>;
|
|
|
|
|
2019-03-21 06:17:01 +00:00
|
|
|
fn slot(&self) -> Slot;
|
|
|
|
|
2019-03-18 06:38:23 +00:00
|
|
|
fn head(&self) -> RwLockReadGuard<CheckPoint>;
|
|
|
|
|
2019-03-23 02:23:44 +00:00
|
|
|
fn get_block(&self, block_root: &Hash256) -> Result<Option<BeaconBlock>, BeaconChainError>;
|
|
|
|
|
2019-03-21 06:17:01 +00:00
|
|
|
fn best_slot(&self) -> Slot;
|
|
|
|
|
|
|
|
fn best_block_root(&self) -> Hash256;
|
|
|
|
|
2019-03-18 06:38:23 +00:00
|
|
|
fn finalized_head(&self) -> RwLockReadGuard<CheckPoint>;
|
2019-03-21 06:17:01 +00:00
|
|
|
|
|
|
|
fn finalized_epoch(&self) -> Epoch;
|
|
|
|
|
|
|
|
fn hello_message(&self) -> HelloMessage;
|
2019-03-22 03:20:49 +00:00
|
|
|
|
2019-03-23 07:48:09 +00:00
|
|
|
fn process_block(&self, block: BeaconBlock)
|
|
|
|
-> Result<BlockProcessingOutcome, BeaconChainError>;
|
|
|
|
|
2019-03-25 03:27:20 +00:00
|
|
|
fn process_attestation(
|
|
|
|
&self,
|
|
|
|
attestation: Attestation,
|
|
|
|
) -> Result<AggregationOutcome, BeaconChainError>;
|
|
|
|
|
2019-03-22 03:20:49 +00:00
|
|
|
fn get_block_roots(
|
|
|
|
&self,
|
|
|
|
start_slot: Slot,
|
2019-03-24 01:49:59 +00:00
|
|
|
count: usize,
|
|
|
|
skip: usize,
|
|
|
|
) -> Result<Vec<Hash256>, BeaconChainError>;
|
|
|
|
|
|
|
|
fn get_block_headers(
|
|
|
|
&self,
|
|
|
|
start_slot: Slot,
|
|
|
|
count: usize,
|
|
|
|
skip: usize,
|
|
|
|
) -> Result<Vec<BeaconBlockHeader>, BeaconChainError>;
|
|
|
|
|
|
|
|
fn get_block_bodies(&self, roots: &[Hash256])
|
|
|
|
-> Result<Vec<BeaconBlockBody>, BeaconChainError>;
|
2019-03-23 02:23:44 +00:00
|
|
|
|
|
|
|
fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result<bool, BeaconChainError>;
|
2019-03-18 06:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U, F> BeaconChain for RawBeaconChain<T, U, F>
|
|
|
|
where
|
|
|
|
T: ClientDB + Sized,
|
|
|
|
U: SlotClock,
|
|
|
|
F: ForkChoice,
|
|
|
|
{
|
2019-03-18 13:05:06 +00:00
|
|
|
fn get_spec(&self) -> &ChainSpec {
|
|
|
|
&self.spec
|
|
|
|
}
|
|
|
|
|
2019-03-18 13:26:15 +00:00
|
|
|
fn get_state(&self) -> RwLockReadGuard<BeaconState> {
|
|
|
|
self.state.read()
|
|
|
|
}
|
|
|
|
|
2019-03-21 06:17:01 +00:00
|
|
|
fn slot(&self) -> Slot {
|
|
|
|
self.get_state().slot
|
|
|
|
}
|
|
|
|
|
2019-03-18 06:38:23 +00:00
|
|
|
fn head(&self) -> RwLockReadGuard<CheckPoint> {
|
|
|
|
self.head()
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:23:44 +00:00
|
|
|
fn get_block(&self, block_root: &Hash256) -> Result<Option<BeaconBlock>, BeaconChainError> {
|
|
|
|
self.get_block(block_root)
|
|
|
|
}
|
|
|
|
|
2019-03-21 06:17:01 +00:00
|
|
|
fn finalized_epoch(&self) -> Epoch {
|
|
|
|
self.get_state().finalized_epoch
|
|
|
|
}
|
|
|
|
|
2019-03-18 06:38:23 +00:00
|
|
|
fn finalized_head(&self) -> RwLockReadGuard<CheckPoint> {
|
|
|
|
self.finalized_head()
|
|
|
|
}
|
2019-03-21 06:17:01 +00:00
|
|
|
|
|
|
|
fn best_slot(&self) -> Slot {
|
|
|
|
self.head().beacon_block.slot
|
|
|
|
}
|
|
|
|
|
|
|
|
fn best_block_root(&self) -> Hash256 {
|
|
|
|
self.head().beacon_block_root
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hello_message(&self) -> HelloMessage {
|
|
|
|
let spec = self.get_spec();
|
|
|
|
let state = self.get_state();
|
|
|
|
|
|
|
|
HelloMessage {
|
2019-03-25 11:00:11 +00:00
|
|
|
network_id: spec.chain_id,
|
2019-03-21 06:17:01 +00:00
|
|
|
latest_finalized_root: state.finalized_root,
|
|
|
|
latest_finalized_epoch: state.finalized_epoch,
|
|
|
|
best_root: self.best_block_root(),
|
|
|
|
best_slot: self.best_slot(),
|
|
|
|
}
|
|
|
|
}
|
2019-03-22 03:20:49 +00:00
|
|
|
|
2019-03-23 07:48:09 +00:00
|
|
|
fn process_block(
|
|
|
|
&self,
|
|
|
|
block: BeaconBlock,
|
|
|
|
) -> Result<BlockProcessingOutcome, BeaconChainError> {
|
|
|
|
self.process_block(block)
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:27:20 +00:00
|
|
|
fn process_attestation(
|
|
|
|
&self,
|
|
|
|
_attestation: Attestation,
|
|
|
|
) -> Result<AggregationOutcome, BeaconChainError> {
|
|
|
|
// Awaiting a proper operations pool before we can import attestations.
|
|
|
|
//
|
|
|
|
// Returning a useless error for now.
|
|
|
|
//
|
|
|
|
// https://github.com/sigp/lighthouse/issues/281
|
|
|
|
return Err(BeaconChainError::DBInconsistent("CANNOT PROCESS".into()));
|
|
|
|
}
|
|
|
|
|
2019-03-22 03:20:49 +00:00
|
|
|
fn get_block_roots(
|
|
|
|
&self,
|
|
|
|
start_slot: Slot,
|
2019-03-24 01:49:59 +00:00
|
|
|
count: usize,
|
|
|
|
skip: usize,
|
|
|
|
) -> Result<Vec<Hash256>, BeaconChainError> {
|
|
|
|
self.get_block_roots(start_slot, count, skip)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_block_headers(
|
|
|
|
&self,
|
|
|
|
start_slot: Slot,
|
|
|
|
count: usize,
|
|
|
|
skip: usize,
|
|
|
|
) -> Result<Vec<BeaconBlockHeader>, BeaconChainError> {
|
|
|
|
let roots = self.get_block_roots(start_slot, count, skip)?;
|
|
|
|
self.get_block_headers(&roots)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_block_bodies(
|
|
|
|
&self,
|
|
|
|
roots: &[Hash256],
|
|
|
|
) -> Result<Vec<BeaconBlockBody>, BeaconChainError> {
|
|
|
|
self.get_block_bodies(roots)
|
2019-03-22 03:20:49 +00:00
|
|
|
}
|
2019-03-23 02:23:44 +00:00
|
|
|
|
|
|
|
fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result<bool, BeaconChainError> {
|
|
|
|
self.is_new_block_root(beacon_block_root)
|
|
|
|
}
|
2019-03-18 06:38:23 +00:00
|
|
|
}
|