Add get_deserialized fns to block & state stores.

This commit is contained in:
Paul Hauner 2019-01-25 11:23:59 +11:00
parent 6fd3a1a03e
commit 271a4cf8a7
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 24 additions and 0 deletions

View File

@ -26,6 +26,18 @@ impl<T: ClientDB> BeaconBlockStore<T> {
Self { db }
}
pub fn get_deserialized(&self, hash: &Hash256) -> Result<Option<BeaconBlock>, 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))
}
}
}
/// Retuns an object implementing `BeaconBlockReader`, or `None` (if hash not known).
///
/// Note: Presently, this function fully deserializes a `BeaconBlock` and returns that. In the

View File

@ -19,6 +19,18 @@ impl<T: ClientDB> BeaconStateStore<T> {
Self { db }
}
pub fn get_deserialized(&self, hash: &Hash256) -> Result<Option<BeaconState>, 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))
}
}
}
/// Retuns an object implementing `BeaconStateReader`, or `None` (if hash not known).
///
/// Note: Presently, this function fully deserializes a `BeaconState` and returns that. In the