Update beacon_chain to latest DB

This commit is contained in:
Paul Hauner 2019-05-21 17:27:06 +10:00
parent 78368cc2cd
commit 058829b64d
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
3 changed files with 44 additions and 54 deletions

View File

@ -7,7 +7,6 @@ use operation_pool::DepositInsertStatus;
use operation_pool::OperationPool; use operation_pool::OperationPool;
use parking_lot::{RwLock, RwLockReadGuard}; use parking_lot::{RwLock, RwLockReadGuard};
use slot_clock::SlotClock; use slot_clock::SlotClock;
use ssz::ssz_encode;
use state_processing::per_block_processing::errors::{ use state_processing::per_block_processing::errors::{
AttestationValidationError, AttesterSlashingValidationError, DepositValidationError, AttestationValidationError, AttesterSlashingValidationError, DepositValidationError,
ExitValidationError, ProposerSlashingValidationError, TransferValidationError, ExitValidationError, ProposerSlashingValidationError, TransferValidationError,
@ -229,7 +228,7 @@ where
let new_state_root = state.get_state_root(earliest_historic_slot)?; let new_state_root = state.get_state_root(earliest_historic_slot)?;
// Break if the DB is unable to load the state. // Break if the DB is unable to load the state.
state = match self.state_store.get_deserialized(&new_state_root) { state = match self.store.get(&new_state_root) {
Ok(Some(state)) => state, Ok(Some(state)) => state,
_ => break, _ => break,
} }
@ -256,7 +255,7 @@ where
/// ///
/// May return a database error. /// May return a database error.
pub fn get_block(&self, block_root: &Hash256) -> Result<Option<BeaconBlock>, Error> { pub fn get_block(&self, block_root: &Hash256) -> Result<Option<BeaconBlock>, Error> {
Ok(self.block_store.get_deserialized(block_root)?) Ok(self.store.get(block_root)?)
} }
/// Update the canonical head to some new values. /// Update the canonical head to some new values.
@ -582,7 +581,7 @@ where
// Load the blocks parent block from the database, returning invalid if that block is not // Load the blocks parent block from the database, returning invalid if that block is not
// found. // found.
let parent_block_root = block.previous_block_root; let parent_block_root = block.previous_block_root;
let parent_block = match self.block_store.get_deserialized(&parent_block_root)? { let parent_block: BeaconBlock = match self.store.get(&parent_block_root)? {
Some(previous_block_root) => previous_block_root, Some(previous_block_root) => previous_block_root,
None => { None => {
return Ok(BlockProcessingOutcome::InvalidBlock( return Ok(BlockProcessingOutcome::InvalidBlock(
@ -595,15 +594,15 @@ where
// It is an error because if know the parent block we should also know the parent state. // It is an error because if know the parent block we should also know the parent state.
let parent_state_root = parent_block.state_root; let parent_state_root = parent_block.state_root;
let parent_state = self let parent_state = self
.state_store .store
.get_deserialized(&parent_state_root)? .get(&parent_state_root)?
.ok_or_else(|| Error::DBInconsistent(format!("Missing state {}", parent_state_root)))?; .ok_or_else(|| Error::DBInconsistent(format!("Missing state {}", parent_state_root)))?;
// TODO: check the block proposer signature BEFORE doing a state transition. This will // TODO: check the block proposer signature BEFORE doing a state transition. This will
// significantly lower exposure surface to DoS attacks. // significantly lower exposure surface to DoS attacks.
// Transition the parent state to the block slot. // Transition the parent state to the block slot.
let mut state = parent_state; let mut state: BeaconState<E> = parent_state;
for _ in state.slot.as_u64()..block.slot.as_u64() { for _ in state.slot.as_u64()..block.slot.as_u64() {
if let Err(e) = per_slot_processing(&mut state, &self.spec) { if let Err(e) = per_slot_processing(&mut state, &self.spec) {
return Ok(BlockProcessingOutcome::InvalidBlock( return Ok(BlockProcessingOutcome::InvalidBlock(
@ -629,8 +628,8 @@ where
} }
// Store the block and state. // Store the block and state.
self.block_store.put(&block_root, &ssz_encode(&block)[..])?; self.store.put(&block_root, &block)?;
self.state_store.put(&state_root, &ssz_encode(&state)[..])?; self.store.put(&state_root, &state)?;
// run the fork_choice add_block logic // run the fork_choice add_block logic
self.fork_choice self.fork_choice
@ -723,15 +722,15 @@ where
.find_head(&present_head, &self.spec)?; .find_head(&present_head, &self.spec)?;
if new_head != present_head { if new_head != present_head {
let block = self let block: BeaconBlock = self
.block_store .store
.get_deserialized(&new_head)? .get(&new_head)?
.ok_or_else(|| Error::MissingBeaconBlock(new_head))?; .ok_or_else(|| Error::MissingBeaconBlock(new_head))?;
let block_root = block.canonical_root(); let block_root = block.canonical_root();
let state = self let state: BeaconState<E> = self
.state_store .store
.get_deserialized(&block.state_root)? .get(&block.state_root)?
.ok_or_else(|| Error::MissingBeaconState(block.state_root))?; .ok_or_else(|| Error::MissingBeaconState(block.state_root))?;
let state_root = state.canonical_root(); let state_root = state.canonical_root();
@ -746,7 +745,7 @@ where
/// Returns `true` if the given block root has not been processed. /// Returns `true` if the given block root has not been processed.
pub fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result<bool, Error> { pub fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result<bool, Error> {
Ok(!self.block_store.exists(beacon_block_root)?) Ok(!self.store.exists::<BeaconBlock>(beacon_block_root)?)
} }
/// Dumps the entire canonical chain, from the head to genesis to a vector for analysis. /// Dumps the entire canonical chain, from the head to genesis to a vector for analysis.
@ -772,17 +771,12 @@ where
break; // Genesis has been reached. break; // Genesis has been reached.
} }
let beacon_block = self let beacon_block: BeaconBlock =
.block_store self.store.get(&beacon_block_root)?.ok_or_else(|| {
.get_deserialized(&beacon_block_root)?
.ok_or_else(|| {
Error::DBInconsistent(format!("Missing block {}", beacon_block_root)) Error::DBInconsistent(format!("Missing block {}", beacon_block_root))
})?; })?;
let beacon_state_root = beacon_block.state_root; let beacon_state_root = beacon_block.state_root;
let beacon_state = self let beacon_state = self.store.get(&beacon_state_root)?.ok_or_else(|| {
.state_store
.get_deserialized(&beacon_state_root)?
.ok_or_else(|| {
Error::DBInconsistent(format!("Missing state {}", beacon_state_root)) Error::DBInconsistent(format!("Missing state {}", beacon_state_root))
})?; })?;
@ -805,7 +799,7 @@ where
impl From<DBError> for Error { impl From<DBError> for Error {
fn from(e: DBError) -> Error { fn from(e: DBError) -> Error {
Error::DBError(e.message) Error::DBError(e)
} }
} }

View File

@ -20,7 +20,7 @@ pub enum BeaconChainError {
UnableToReadSlot, UnableToReadSlot,
BeaconStateError(BeaconStateError), BeaconStateError(BeaconStateError),
DBInconsistent(String), DBInconsistent(String),
DBError(String), DBError(db::Error),
ForkChoiceError(ForkChoiceError), ForkChoiceError(ForkChoiceError),
MissingBeaconBlock(Hash256), MissingBeaconBlock(Hash256),
MissingBeaconState(Hash256), MissingBeaconState(Hash256),

View File

@ -3,7 +3,6 @@
// testnet. These are examples. Also. there is code duplication which can/should be cleaned up. // testnet. These are examples. Also. there is code duplication which can/should be cleaned up.
use crate::BeaconChain; use crate::BeaconChain;
use db::stores::{BeaconBlockStore, BeaconStateStore};
use db::{DiskDB, MemoryDB}; use db::{DiskDB, MemoryDB};
use fork_choice::BitwiseLMDGhost; use fork_choice::BitwiseLMDGhost;
use slot_clock::SystemTimeSlotClock; use slot_clock::SystemTimeSlotClock;
@ -26,14 +25,9 @@ pub fn initialise_beacon_chain(
FoundationEthSpec, FoundationEthSpec,
>, >,
> { > {
// set up the db let path = db_name.expect("db_name cannot be None.");
let db = Arc::new(DiskDB::open( let store = DiskDB::open(path).expect("Unable to open DB.");
db_name.expect("Database directory must be included"), let store = Arc::new(store);
None,
));
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, &spec); let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, &spec);
let (genesis_state, _keypairs) = state_builder.build(); let (genesis_state, _keypairs) = state_builder.build();
@ -49,14 +43,13 @@ pub fn initialise_beacon_chain(
) )
.expect("Unable to load SystemTimeSlotClock"); .expect("Unable to load SystemTimeSlotClock");
// Choose the fork choice // Choose the fork choice
let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); let fork_choice = BitwiseLMDGhost::new(store.clone());
// Genesis chain // Genesis chain
//TODO: Handle error correctly //TODO: Handle error correctly
Arc::new( Arc::new(
BeaconChain::from_genesis( BeaconChain::from_genesis(
state_store.clone(), store,
block_store.clone(),
slot_clock, slot_clock,
genesis_state, genesis_state,
genesis_block, genesis_block,
@ -79,9 +72,7 @@ pub fn initialise_test_beacon_chain_with_memory_db(
FewValidatorsEthSpec, FewValidatorsEthSpec,
>, >,
> { > {
let db = Arc::new(MemoryDB::open()); let store = Arc::new(MemoryDB::open());
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, spec); let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, spec);
let (genesis_state, _keypairs) = state_builder.build(); let (genesis_state, _keypairs) = state_builder.build();
@ -97,14 +88,13 @@ pub fn initialise_test_beacon_chain_with_memory_db(
) )
.expect("Unable to load SystemTimeSlotClock"); .expect("Unable to load SystemTimeSlotClock");
// Choose the fork choice // Choose the fork choice
let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); let fork_choice = BitwiseLMDGhost::new(store.clone());
// Genesis chain // Genesis chain
//TODO: Handle error correctly //TODO: Handle error correctly
Arc::new( Arc::new(
BeaconChain::from_genesis( BeaconChain::from_genesis(
state_store.clone(), store,
block_store.clone(),
slot_clock, slot_clock,
genesis_state, genesis_state,
genesis_block, genesis_block,
@ -119,16 +109,23 @@ pub fn initialise_test_beacon_chain_with_memory_db(
pub fn initialise_test_beacon_chain_with_disk_db( pub fn initialise_test_beacon_chain_with_disk_db(
spec: &ChainSpec, spec: &ChainSpec,
db_name: Option<&PathBuf>, db_name: Option<&PathBuf>,
) -> Arc<BeaconChain<DiskDB, SystemTimeSlotClock, BitwiseLMDGhost<DiskDB>>> { ) -> Arc<
let db = Arc::new(DiskDB::open(db_name.expect("Must have DB path"), None)); BeaconChain<
let block_store = Arc::new(BeaconBlockStore::new(db.clone())); DiskDB,
let state_store = Arc::new(BeaconStateStore::new(db.clone())); SystemTimeSlotClock,
BitwiseLMDGhost<DiskDB, FewValidatorsEthSpec>,
FewValidatorsEthSpec,
>,
> {
let path = db_name.expect("db_name cannot be None.");
let store = DiskDB::open(path).expect("Unable to open DB.");
let store = Arc::new(store);
let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, spec); let state_builder = TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(8, spec);
let (genesis_state, _keypairs) = state_builder.build(); let (genesis_state, _keypairs) = state_builder.build();
let mut genesis_block = BeaconBlock::empty(spec); let mut genesis_block = BeaconBlock::empty(spec);
genesis_block.state_root = Hash256::from_slice(&genesis_state.hash_tree_root()); genesis_block.state_root = Hash256::from_slice(&genesis_state.tree_hash_root());
// Slot clock // Slot clock
let slot_clock = SystemTimeSlotClock::new( let slot_clock = SystemTimeSlotClock::new(
@ -138,14 +135,13 @@ pub fn initialise_test_beacon_chain_with_disk_db(
) )
.expect("Unable to load SystemTimeSlotClock"); .expect("Unable to load SystemTimeSlotClock");
// Choose the fork choice // Choose the fork choice
let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone()); let fork_choice = BitwiseLMDGhost::new(store.clone());
// Genesis chain // Genesis chain
//TODO: Handle error correctly //TODO: Handle error correctly
Arc::new( Arc::new(
BeaconChain::from_genesis( BeaconChain::from_genesis(
state_store.clone(), store,
block_store.clone(),
slot_clock, slot_clock,
genesis_state, genesis_state,
genesis_block, genesis_block,