diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index b1e0c3e009ca..4a930e9c17bf 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -209,12 +209,12 @@ where fn find_block_by_hash(&self, hash: H256, source: BlockSource) -> Result> { let block = match source { BlockSource::Any => { - // check pending source first - // Note: it's fine to return the unsealed block because the caller already has the - // hash - let mut block = self.tree.block_by_hash(hash).map(|block| block.unseal()); + // check database first + let mut block = self.database.provider()?.block_by_hash(hash)?; if block.is_none() { - block = self.database.provider()?.block_by_hash(hash)?; + // Note: it's fine to return the unsealed block because the caller already has + // the hash + block = self.tree.block_by_hash(hash).map(|block| block.unseal()); } block } @@ -541,14 +541,17 @@ where fn state_by_block_hash(&self, block: BlockHash) -> Result> { trace!(target: "providers::blockchain", ?block, "Getting state by block hash"); + let mut state = self.history_by_block_hash(block); - // check tree first - if let Some(pending) = self.tree.find_pending_state_provider(block) { - trace!(target: "providers::blockchain", "Returning pending state provider"); - return self.pending_with_provider(pending) + // we failed to get the state by hash, from disk, hash block be the pending block + if state.is_err() { + if let Ok(Some(pending)) = self.pending_state_by_hash(block) { + // we found pending block by hash + state = Ok(pending) + } } - // not found in tree, check database - self.history_by_block_hash(block) + + state } /// Storage provider for pending state. @@ -658,6 +661,10 @@ where self.tree.find_canonical_ancestor(hash) } + fn is_canonical(&self, hash: BlockHash) -> std::result::Result { + self.tree.is_canonical(hash) + } + fn lowest_buffered_ancestor(&self, hash: BlockHash) -> Option { self.tree.lowest_buffered_ancestor(hash) } @@ -666,10 +673,6 @@ where self.tree.canonical_tip() } - fn is_canonical(&self, hash: BlockHash) -> std::result::Result { - self.tree.is_canonical(hash) - } - fn pending_blocks(&self) -> (BlockNumber, Vec) { self.tree.pending_blocks() }