From c8a97f94b28531610f3091fc4ff5b88ef6611b26 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Tue, 27 Oct 2020 14:25:11 -0700 Subject: [PATCH] state: clean up SledState query impls --- zebra-state/src/sled_state.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/zebra-state/src/sled_state.rs b/zebra-state/src/sled_state.rs index 9b7eaf66..95bbff3b 100644 --- a/zebra-state/src/sled_state.rs +++ b/zebra-state/src/sled_state.rs @@ -1,6 +1,6 @@ //! The primary implementation of the `zebra_state::Service` built upon sled -use std::{collections::HashMap, convert::TryInto, sync::Arc}; +use std::{collections::HashMap, sync::Arc}; use tracing::trace; use zebra_chain::transparent; @@ -13,7 +13,7 @@ use crate::{BoxError, Config, HashOrHeight, QueuedBlock}; mod sled_format; -use sled_format::{SledDeserialize, SledSerialize}; +use sled_format::{FromSled, SledDeserialize, SledSerialize}; /// The finalized part of the chain state, stored in sled. /// @@ -294,9 +294,10 @@ impl FinalizedState { let heights = crate::util::block_locator_heights(tip_height); let mut hashes = Vec::with_capacity(heights.len()); + for height in heights { if let Some(hash) = self.hash_by_height.zs_get(&height)? { - hashes.push(hash) + hashes.push(hash); } } @@ -304,15 +305,16 @@ impl FinalizedState { } pub fn tip(&self) -> Result, BoxError> { - Ok(self.hash_by_height.iter().rev().next().transpose()?.map( - |(height_bytes, hash_bytes)| { - let height = block::Height(u32::from_be_bytes( - height_bytes.as_ref().try_into().unwrap(), - )); - let hash = block::Hash(hash_bytes.as_ref().try_into().unwrap()); - (height, hash) - }, - )) + if let Some((height_bytes, hash_bytes)) = + self.hash_by_height.iter().rev().next().transpose()? + { + let height = block::Height::from_ivec(height_bytes)?; + let hash = block::Hash::from_ivec(hash_bytes)?; + + Ok(Some((height, hash))) + } else { + Ok(None) + } } pub fn depth(&self, hash: block::Hash) -> Result, BoxError> { @@ -335,10 +337,7 @@ impl FinalizedState { }, }; - match self.block_by_height.zs_get(&height)? { - Some(block) => Ok(Some(block)), - None => Ok(None), - } + Ok(self.block_by_height.zs_get(&height)?) } /// Returns the `transparent::Output` pointed to by the given