Break store fns into smaller pieces

This commit is contained in:
Paul Hauner 2019-05-29 17:53:13 +10:00
parent 9f1039a350
commit 0b719e1523
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF

View File

@ -25,15 +25,23 @@ pub fn get_block_at_preceeding_slot<T: Store>(
slot: Slot, slot: Slot,
start_root: Hash256, start_root: Hash256,
) -> Result<Option<(Hash256, BeaconBlock)>, Error> { ) -> Result<Option<(Hash256, BeaconBlock)>, Error> {
let mut root = start_root; Ok(match get_at_preceeding_slot(store, slot, start_root)? {
Some((hash, bytes)) => Some((hash, BeaconBlock::from_ssz_bytes(&bytes)?)),
None => None,
})
}
fn get_at_preceeding_slot<T: Store>(
store: &T,
slot: Slot,
mut root: Hash256,
) -> Result<Option<(Hash256, Vec<u8>)>, Error> {
loop { loop {
if let Some(bytes) = get_block_bytes(store, root)? { if let Some(bytes) = get_block_bytes(store, root)? {
let this_slot = read_slot_from_block_bytes(&bytes)?; let this_slot = read_slot_from_block_bytes(&bytes)?;
if this_slot == slot { if this_slot == slot {
let block = BeaconBlock::from_ssz_bytes(&bytes)?; break Ok(Some((root, bytes)));
break Ok(Some((root, block)));
} else if this_slot < slot { } else if this_slot < slot {
break Ok(None); break Ok(None);
} else { } else {