2018-12-30 01:59:24 +00:00
|
|
|
use super::{BeaconChain, ClientDB, DBError, SlotClock};
|
|
|
|
use slot_clock::TestingSlotClockError;
|
|
|
|
use types::{
|
|
|
|
readers::{BeaconBlockReader, BeaconStateReader},
|
|
|
|
BeaconBlock, BeaconState, Hash256,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
|
|
|
DBError(String),
|
|
|
|
PresentSlotIsNone,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> BeaconChain<T, U>
|
|
|
|
where
|
|
|
|
T: ClientDB,
|
|
|
|
U: SlotClock,
|
|
|
|
Error: From<<U as SlotClock>::Error>,
|
|
|
|
{
|
|
|
|
pub fn produce_block(&mut self) -> Result<(BeaconBlock, BeaconState), Error> {
|
2019-01-08 07:04:42 +00:00
|
|
|
/*
|
|
|
|
* Important: this code is a big stub and only exists to ensure that tests pass.
|
|
|
|
*
|
|
|
|
* https://github.com/sigp/lighthouse/issues/107
|
|
|
|
*/
|
2018-12-30 01:59:24 +00:00
|
|
|
let present_slot = self
|
|
|
|
.slot_clock
|
|
|
|
.present_slot()?
|
|
|
|
.ok_or(Error::PresentSlotIsNone)?;
|
|
|
|
let parent_root = self.canonical_leaf_block;
|
2019-01-04 07:30:24 +00:00
|
|
|
let parent_block_reader = self
|
2018-12-30 01:59:24 +00:00
|
|
|
.block_store
|
2019-01-04 07:30:24 +00:00
|
|
|
.get_reader(&parent_root)?
|
|
|
|
.ok_or_else(|| Error::DBError("Block not found.".to_string()))?;
|
|
|
|
let parent_state_reader = self
|
2018-12-30 01:59:24 +00:00
|
|
|
.state_store
|
2019-01-04 07:30:24 +00:00
|
|
|
.get_reader(&parent_block_reader.state_root())?
|
|
|
|
.ok_or_else(|| Error::DBError("State not found.".to_string()))?;
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2019-01-04 07:30:24 +00:00
|
|
|
let parent_block = parent_block_reader
|
|
|
|
.into_beacon_block()
|
|
|
|
.ok_or_else(|| Error::DBError("Bad parent block SSZ.".to_string()))?;
|
2018-12-30 01:59:24 +00:00
|
|
|
let mut block = BeaconBlock {
|
|
|
|
slot: present_slot,
|
|
|
|
parent_root,
|
|
|
|
state_root: Hash256::zero(), // Updated after the state is calculated.
|
2019-01-04 07:30:24 +00:00
|
|
|
..parent_block
|
2018-12-30 01:59:24 +00:00
|
|
|
};
|
|
|
|
|
2019-01-04 07:30:24 +00:00
|
|
|
let parent_state = parent_state_reader
|
|
|
|
.into_beacon_state()
|
|
|
|
.ok_or_else(|| Error::DBError("Bad parent block SSZ.".to_string()))?;
|
2018-12-30 01:59:24 +00:00
|
|
|
let state = BeaconState {
|
|
|
|
slot: present_slot,
|
2019-01-04 07:30:24 +00:00
|
|
|
..parent_state
|
2018-12-30 01:59:24 +00:00
|
|
|
};
|
|
|
|
let state_root = state.canonical_root();
|
|
|
|
|
|
|
|
block.state_root = state_root;
|
|
|
|
|
|
|
|
Ok((block, state))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DBError> for Error {
|
|
|
|
fn from(e: DBError) -> Error {
|
|
|
|
Error::DBError(e.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TestingSlotClockError> for Error {
|
|
|
|
fn from(_: TestingSlotClockError) -> Error {
|
|
|
|
unreachable!(); // Testing clock never throws an error.
|
|
|
|
}
|
|
|
|
}
|