Remove block and state readers from db crate

This commit is contained in:
Paul Hauner 2019-03-17 17:54:43 +11:00
parent 6b3cdc34dd
commit 8b08e9dd2e
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 12 additions and 47 deletions

View File

@ -2,7 +2,7 @@ use super::BLOCKS_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError}; use super::{ClientDB, DBError};
use ssz::Decodable; use ssz::Decodable;
use std::sync::Arc; use std::sync::Arc;
use types::{readers::BeaconBlockReader, BeaconBlock, Hash256, Slot}; use types::{BeaconBlock, Hash256, Slot};
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum BeaconBlockAtSlotError { pub enum BeaconBlockAtSlotError {
@ -38,23 +38,6 @@ impl<T: ClientDB> BeaconBlockStore<T> {
} }
} }
/// Retuns an object implementing `BeaconBlockReader`, or `None` (if hash not known).
///
/// Note: Presently, this function fully deserializes a `BeaconBlock` and returns that. In the
/// future, it would be ideal to return an object capable of reading directly from serialized
/// SSZ bytes.
pub fn get_reader(&self, hash: &Hash256) -> Result<Option<impl BeaconBlockReader>, DBError> {
match self.get(&hash)? {
None => Ok(None),
Some(ssz) => {
let (block, _) = BeaconBlock::ssz_decode(&ssz, 0).map_err(|_| DBError {
message: "Bad BeaconBlock SSZ.".to_string(),
})?;
Ok(Some(block))
}
}
}
/// Retrieve the block at a slot given a "head_hash" and a slot. /// Retrieve the block at a slot given a "head_hash" and a slot.
/// ///
/// A "head_hash" must be a block hash with a slot number greater than or equal to the desired /// A "head_hash" must be a block hash with a slot number greater than or equal to the desired
@ -72,17 +55,17 @@ impl<T: ClientDB> BeaconBlockStore<T> {
&self, &self,
head_hash: &Hash256, head_hash: &Hash256,
slot: Slot, slot: Slot,
) -> Result<Option<(Hash256, impl BeaconBlockReader)>, BeaconBlockAtSlotError> { ) -> Result<Option<(Hash256, BeaconBlock)>, BeaconBlockAtSlotError> {
let mut current_hash = *head_hash; let mut current_hash = *head_hash;
loop { loop {
if let Some(block_reader) = self.get_reader(&current_hash)? { if let Some(block) = self.get_deserialized(&current_hash)? {
if block_reader.slot() == slot { if block.slot == slot {
break Ok(Some((current_hash, block_reader))); break Ok(Some((current_hash, block)));
} else if block_reader.slot() < slot { } else if block.slot < slot {
break Ok(None); break Ok(None);
} else { } else {
current_hash = block_reader.parent_root(); current_hash = block.previous_block_root;
} }
} else { } else {
break Err(BeaconBlockAtSlotError::UnknownBeaconBlock(current_hash)); break Err(BeaconBlockAtSlotError::UnknownBeaconBlock(current_hash));
@ -228,7 +211,7 @@ mod tests {
for i in 0..block_count { for i in 0..block_count {
let mut block = BeaconBlock::random_for_test(&mut rng); let mut block = BeaconBlock::random_for_test(&mut rng);
block.parent_root = parent_hashes[i]; block.previous_block_root = parent_hashes[i];
block.slot = slots[i]; block.slot = slots[i];
let ssz = ssz_encode(&block); let ssz = ssz_encode(&block);
@ -240,12 +223,12 @@ mod tests {
// Test that certain slots can be reached from certain hashes. // Test that certain slots can be reached from certain hashes.
let test_cases = vec![(4, 4), (4, 3), (4, 2), (4, 1), (4, 0)]; let test_cases = vec![(4, 4), (4, 3), (4, 2), (4, 1), (4, 0)];
for (hashes_index, slot_index) in test_cases { for (hashes_index, slot_index) in test_cases {
let (matched_block_hash, reader) = bs let (matched_block_hash, block) = bs
.block_at_slot(&hashes[hashes_index], slots[slot_index]) .block_at_slot(&hashes[hashes_index], slots[slot_index])
.unwrap() .unwrap()
.unwrap(); .unwrap();
assert_eq!(matched_block_hash, hashes[slot_index]); assert_eq!(matched_block_hash, hashes[slot_index]);
assert_eq!(reader.slot(), slots[slot_index]); assert_eq!(block.slot, slots[slot_index]);
} }
let ssz = bs.block_at_slot(&hashes[4], Slot::new(2)).unwrap(); let ssz = bs.block_at_slot(&hashes[4], Slot::new(2)).unwrap();

View File

@ -2,7 +2,7 @@ use super::STATES_DB_COLUMN as DB_COLUMN;
use super::{ClientDB, DBError}; use super::{ClientDB, DBError};
use ssz::Decodable; use ssz::Decodable;
use std::sync::Arc; use std::sync::Arc;
use types::{readers::BeaconStateReader, BeaconState, Hash256}; use types::{BeaconState, Hash256};
pub struct BeaconStateStore<T> pub struct BeaconStateStore<T>
where where
@ -30,23 +30,6 @@ impl<T: ClientDB> BeaconStateStore<T> {
} }
} }
} }
/// Retuns an object implementing `BeaconStateReader`, or `None` (if hash not known).
///
/// Note: Presently, this function fully deserializes a `BeaconState` and returns that. In the
/// future, it would be ideal to return an object capable of reading directly from serialized
/// SSZ bytes.
pub fn get_reader(&self, hash: &Hash256) -> Result<Option<impl BeaconStateReader>, DBError> {
match self.get(&hash)? {
None => Ok(None),
Some(ssz) => {
let (state, _) = BeaconState::ssz_decode(&ssz, 0).map_err(|_| DBError {
message: "Bad State SSZ.".to_string(),
})?;
Ok(Some(state))
}
}
}
} }
#[cfg(test)] #[cfg(test)]
@ -72,8 +55,7 @@ mod tests {
store.put(&state_root, &ssz_encode(&state)).unwrap(); store.put(&state_root, &ssz_encode(&state)).unwrap();
let reader = store.get_reader(&state_root).unwrap().unwrap(); let decoded = store.get_deserialized(&state_root).unwrap().unwrap();
let decoded = reader.into_beacon_state().unwrap();
assert_eq!(state, decoded); assert_eq!(state, decoded);
} }