2018-12-30 01:59:24 +00:00
|
|
|
use super::{BeaconChain, ClientDB, DBError, SlotClock};
|
2019-01-23 08:25:05 +00:00
|
|
|
use bls::Signature;
|
2019-01-30 13:39:34 +00:00
|
|
|
use log::debug;
|
2018-12-30 01:59:24 +00:00
|
|
|
use types::{
|
2019-02-01 03:48:09 +00:00
|
|
|
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),
|
2019-01-31 05:39:44 +00:00
|
|
|
SlotProcessingError(SlotProcessingError),
|
2019-02-01 03:48:09 +00:00
|
|
|
PerBlockProcessingError(BlockProcessingError),
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> BeaconChain<T, U>
|
|
|
|
where
|
|
|
|
T: ClientDB,
|
|
|
|
U: SlotClock,
|
|
|
|
{
|
2019-02-01 05:21:18 +00:00
|
|
|
/// 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.
|
2019-01-23 08:25:05 +00:00
|
|
|
pub fn produce_block(
|
2019-01-24 06:05:48 +00:00
|
|
|
&self,
|
2019-01-23 08:25:05 +00:00
|
|
|
randao_reveal: Signature,
|
2019-02-01 05:21:18 +00:00
|
|
|
) -> Result<(BeaconBlock, BeaconState), Error> {
|
2019-02-01 03:48:09 +00:00
|
|
|
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);
|
2019-01-28 06:07:13 +00:00
|
|
|
|
2019-02-01 03:48:09 +00:00
|
|
|
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();
|
2019-01-30 13:39:34 +00:00
|
|
|
|
2018-12-30 01:59:24 +00:00
|
|
|
let mut block = BeaconBlock {
|
2019-02-01 03:48:09 +00:00
|
|
|
slot: state.slot,
|
|
|
|
parent_root,
|
2018-12-30 01:59:24 +00:00
|
|
|
state_root: Hash256::zero(), // Updated after the state is calculated.
|
2019-01-23 08:25:05 +00:00
|
|
|
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(),
|
|
|
|
},
|
2019-01-23 08:25:05 +00:00
|
|
|
signature: self.spec.empty_signature.clone(), // To be completed by a validator.
|
|
|
|
body: BeaconBlockBody {
|
|
|
|
proposer_slashings: vec![],
|
|
|
|
casper_slashings: vec![],
|
2019-01-28 06:07:13 +00:00
|
|
|
attestations: attestations,
|
2019-01-23 08:25:05 +00:00
|
|
|
custody_reseeds: vec![],
|
|
|
|
custody_challenges: vec![],
|
|
|
|
custody_responses: vec![],
|
|
|
|
deposits: vec![],
|
|
|
|
exits: vec![],
|
|
|
|
},
|
2018-12-30 01:59:24 +00:00
|
|
|
};
|
|
|
|
|
2019-02-01 03:48:09 +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;
|
|
|
|
|
2019-01-30 13:39:34 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 03:48:09 +00:00
|
|
|
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)
|
2019-01-23 08:25:05 +00:00
|
|
|
}
|
|
|
|
}
|