lighthouse/beacon_node/beacon_chain/tests/utils/direct_beacon_node.rs

72 lines
2.4 KiB
Rust
Raw Normal View History

2019-01-25 00:30:06 +00:00
use beacon_chain::block_processing::{Error as ProcessingError, Outcome as ProcessingOutcome};
2019-01-24 06:05:48 +00:00
use beacon_chain::{block_production::Error as BlockProductionError, BeaconChain};
2019-01-25 00:30:06 +00:00
use block_producer::{
BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError, PublishOutcome,
};
2019-01-24 06:05:48 +00:00
use db::ClientDB;
use slot_clock::SlotClock;
2019-01-25 00:30:06 +00:00
use std::sync::Arc;
2019-01-24 06:05:48 +00:00
use types::{BeaconBlock, PublicKey, Signature};
2019-01-25 00:30:06 +00:00
pub struct DirectBeaconNode<T: ClientDB, U: SlotClock> {
beacon_chain: Arc<BeaconChain<T, U>>,
2019-01-24 06:05:48 +00:00
}
2019-01-25 00:30:06 +00:00
impl<T: ClientDB, U: SlotClock> DirectBeaconNode<T, U> {
pub fn new(beacon_chain: Arc<BeaconChain<T, U>>) -> Self {
2019-01-24 06:05:48 +00:00
Self { beacon_chain }
}
}
2019-01-25 00:30:06 +00:00
impl<T: ClientDB, U: SlotClock> BeaconBlockNode for DirectBeaconNode<T, U>
2019-01-24 06:05:48 +00:00
where
BlockProductionError: From<<U>::Error>,
2019-01-25 00:30:06 +00:00
ProcessingError: From<<U as SlotClock>::Error>,
2019-01-24 06:05:48 +00:00
{
fn proposer_nonce(&self, pubkey: &PublicKey) -> Result<u64, BeaconBlockNodeError> {
let validator_index = self
.beacon_chain
.validator_index(pubkey)
.ok_or_else(|| BeaconBlockNodeError::RemoteFailure("pubkey unknown.".to_string()))?;
self.beacon_chain
.proposer_slots(validator_index)
.ok_or_else(|| {
BeaconBlockNodeError::RemoteFailure("validator_index unknown.".to_string())
})
}
fn produce_beacon_block(
&self,
slot: u64,
randao_reveal: &Signature,
) -> Result<Option<BeaconBlock>, BeaconBlockNodeError>
where {
let (block, _state) = self
.beacon_chain
.produce_block(randao_reveal.clone())
.map_err(|e| BeaconBlockNodeError::RemoteFailure(format!("{:?}", e)))?;
if block.slot == slot {
Ok(Some(block))
} else {
Err(BeaconBlockNodeError::RemoteFailure(
"Unable to produce at non-current slot.".to_string(),
))
}
}
2019-01-25 00:30:06 +00:00
fn publish_beacon_block(
&self,
block: BeaconBlock,
) -> Result<PublishOutcome, BeaconBlockNodeError> {
match self.beacon_chain.process_block(block) {
Ok(ProcessingOutcome::ValidBlock(_)) => Ok(PublishOutcome::ValidBlock),
Ok(ProcessingOutcome::InvalidBlock(reason)) => {
Ok(PublishOutcome::InvalidBlock(format!("{:?}", reason)))
}
Err(error) => Err(BeaconBlockNodeError::RemoteFailure(format!("{:?}", error))),
}
2019-01-24 06:05:48 +00:00
}
}