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 parking_lot::{RwLock, RwLockReadGuard};
use slot_clock::SlotClock;
use ssz::ssz_encode;
use state_processing::per_block_processing::errors::{
AttestationValidationError, AttesterSlashingValidationError, DepositValidationError,
ExitValidationError, ProposerSlashingValidationError, TransferValidationError,
@ -229,7 +228,7 @@ where
let new_state_root = state.get_state_root(earliest_historic_slot)?;
// 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,
_ => break,
}
@ -256,7 +255,7 @@ where
///
/// May return a database 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.
@ -582,7 +581,7 @@ where
// Load the blocks parent block from the database, returning invalid if that block is not
// found.
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,
None => {
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.
let parent_state_root = parent_block.state_root;
let parent_state = self
.state_store
.get_deserialized(&parent_state_root)?
.store
.get(&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
// significantly lower exposure surface to DoS attacks.
// 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() {
if let Err(e) = per_slot_processing(&mut state, &self.spec) {
return Ok(BlockProcessingOutcome::InvalidBlock(
@ -629,8 +628,8 @@ where
}
// Store the block and state.
self.block_store.put(&block_root, &ssz_encode(&block)[..])?;
self.state_store.put(&state_root, &ssz_encode(&state)[..])?;
self.store.put(&block_root, &block)?;
self.store.put(&state_root, &state)?;
// run the fork_choice add_block logic
self.fork_choice
@ -723,15 +722,15 @@ where
.find_head(&present_head, &self.spec)?;
if new_head != present_head {
let block = self
.block_store
.get_deserialized(&new_head)?
let block: BeaconBlock = self
.store
.get(&new_head)?
.ok_or_else(|| Error::MissingBeaconBlock(new_head))?;
let block_root = block.canonical_root();
let state = self
.state_store
.get_deserialized(&block.state_root)?
let state: BeaconState<E> = self
.store
.get(&block.state_root)?
.ok_or_else(|| Error::MissingBeaconState(block.state_root))?;
let state_root = state.canonical_root();
@ -746,7 +745,7 @@ where
/// 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> {
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.
@ -772,19 +771,14 @@ where
break; // Genesis has been reached.
}
let beacon_block = self
.block_store
.get_deserialized(&beacon_block_root)?
.ok_or_else(|| {
let beacon_block: BeaconBlock =
self.store.get(&beacon_block_root)?.ok_or_else(|| {
Error::DBInconsistent(format!("Missing block {}", beacon_block_root))
})?;
let beacon_state_root = beacon_block.state_root;
let beacon_state = self
.state_store
.get_deserialized(&beacon_state_root)?
.ok_or_else(|| {
Error::DBInconsistent(format!("Missing state {}", beacon_state_root))
})?;
let beacon_state = self.store.get(&beacon_state_root)?.ok_or_else(|| {
Error::DBInconsistent(format!("Missing state {}", beacon_state_root))
})?;
let slot = CheckPoint {
beacon_block,
@ -805,7 +799,7 @@ where
impl From<DBError> for Error {
fn from(e: DBError) -> Error {
Error::DBError(e.message)
Error::DBError(e)
}
}

View File

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

View File

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