lighthouse/beacon_node/beacon_chain/src/block_production.rs

103 lines
2.9 KiB
Rust
Raw Normal View History

2018-12-30 01:59:24 +00:00
use super::{BeaconChain, ClientDB, DBError, SlotClock};
use bls::Signature;
use log::debug;
2018-12-30 01:59:24 +00:00
use types::{
beacon_state::{BlockProcessingError, SlotProcessingError},
2019-01-24 01:10:03 +00:00
BeaconBlock, BeaconBlockBody, BeaconState, Eth1Data, Hash256,
2018-12-30 01:59:24 +00:00
};
#[derive(Debug, PartialEq)]
pub enum Error {
DBError(String),
SlotProcessingError(SlotProcessingError),
PerBlockProcessingError(BlockProcessingError),
2018-12-30 01:59:24 +00:00
}
impl<T, U> BeaconChain<T, U>
where
T: ClientDB,
U: SlotClock,
{
/// Produce a new block at the present slot.
///
/// The produced block will not be inheriently valid, it must be signed by a block producer.
/// Block signing is out of the scope of this function and should be done by a separate program.
pub fn produce_block(
2019-01-24 06:05:48 +00:00
&self,
randao_reveal: Signature,
) -> Result<(BeaconBlock, BeaconState), Error> {
debug!("Starting block production...");
let mut state = self.state.read().clone();
debug!("Finding attesatations for new block...");
let attestations = self
.attestation_aggregator
.read()
.get_attestations_for_state(&state, &self.spec);
debug!(
"Inserting {} attestation(s) into new block.",
attestations.len()
);
let parent_root = state
.get_block_root(state.slot.saturating_sub(1), &self.spec)
// TODO: fix unwrap
.unwrap()
.clone();
2018-12-30 01:59:24 +00:00
let mut block = BeaconBlock {
slot: state.slot,
parent_root,
2018-12-30 01:59:24 +00:00
state_root: Hash256::zero(), // Updated after the state is calculated.
randao_reveal: randao_reveal,
2019-01-24 01:10:03 +00:00
eth1_data: Eth1Data {
// TODO: replace with real data
deposit_root: Hash256::zero(),
block_hash: Hash256::zero(),
},
signature: self.spec.empty_signature.clone(), // To be completed by a validator.
body: BeaconBlockBody {
proposer_slashings: vec![],
casper_slashings: vec![],
attestations: attestations,
custody_reseeds: vec![],
custody_challenges: vec![],
custody_responses: vec![],
deposits: vec![],
exits: vec![],
},
2018-12-30 01:59:24 +00:00
};
state.per_block_processing_without_verifying_block_signature(&block, &self.spec)?;
2018-12-30 01:59:24 +00:00
let state_root = state.canonical_root();
block.state_root = state_root;
debug!("Block produced.");
2018-12-30 01:59:24 +00:00
Ok((block, state))
}
}
impl From<DBError> for Error {
fn from(e: DBError) -> Error {
Error::DBError(e.message)
}
}
impl From<SlotProcessingError> for Error {
fn from(e: SlotProcessingError) -> Error {
Error::SlotProcessingError(e)
}
}
impl From<BlockProcessingError> for Error {
fn from(e: BlockProcessingError) -> Error {
Error::PerBlockProcessingError(e)
}
}