Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hit database first for lookups #4127

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions crates/storage/provider/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ where
fn find_block_by_hash(&self, hash: H256, source: BlockSource) -> Result<Option<Block>> {
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
}
Expand Down Expand Up @@ -541,14 +541,17 @@ where

fn state_by_block_hash(&self, block: BlockHash) -> Result<StateProviderBox<'_>> {
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.
Expand Down Expand Up @@ -658,6 +661,10 @@ where
self.tree.find_canonical_ancestor(hash)
}

fn is_canonical(&self, hash: BlockHash) -> std::result::Result<bool, Error> {
self.tree.is_canonical(hash)
}

fn lowest_buffered_ancestor(&self, hash: BlockHash) -> Option<SealedBlockWithSenders> {
self.tree.lowest_buffered_ancestor(hash)
}
Expand All @@ -666,10 +673,6 @@ where
self.tree.canonical_tip()
}

fn is_canonical(&self, hash: BlockHash) -> std::result::Result<bool, Error> {
self.tree.is_canonical(hash)
}

fn pending_blocks(&self) -> (BlockNumber, Vec<BlockHash>) {
self.tree.pending_blocks()
}
Expand Down
Loading