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

perf(rpc): fetch range of blocks and return empty if unchanged #4592

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
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
27 changes: 17 additions & 10 deletions crates/rpc/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ where
let info = self.inner.provider.chain_info()?;
let best_number = info.best_number;

// start_block is the block from which we should start fetching changes, the next block from
// the last time changes were polled, in other words the best block at last poll + 1
let (start_block, kind) = {
let mut filters = self.inner.active_filters.inner.lock().await;
let filter = filters.get_mut(&id).ok_or(FilterError::FilterNotFound(id))?;

if filter.block > best_number {
// no new blocks since the last poll
return Ok(FilterChanges::Empty)
}

// update filter
// we fetch all changes from [filter.block..best_block], so we advance the filter's
// block to `best_block +1`
// block to `best_block +1`, the next from which we should start fetching changes again
let mut block = best_number + 1;
std::mem::swap(&mut filter.block, &mut block);
filter.last_poll_timestamp = Instant::now();
Expand All @@ -90,15 +97,14 @@ where
Err(EthApiError::Unsupported("pending transaction filter not supported").into())
}
FilterKind::Block => {
let mut block_hashes = Vec::new();
for block_num in start_block..best_number {
let block_hash = self
.inner
.provider
.block_hash(block_num)?
.ok_or(EthApiError::UnknownBlockNumber)?;
block_hashes.push(block_hash);
}
// Note: we need to fetch the block hashes from inclusive range
// [start_block..best_block]
let end_block = best_number + 1;
let block_hashes = self
.inner
.provider
.canonical_hashes_range(start_block, end_block)
.map_err(|_| EthApiError::UnknownBlockNumber)?;
Ok(FilterChanges::Hashes(block_hashes))
}
FilterKind::Log(filter) => {
Expand All @@ -117,6 +123,7 @@ where
FilterBlockOption::AtBlockHash(_) => {
// blockHash is equivalent to fromBlock = toBlock = the block number with
// hash blockHash
// get_logs_in_block_range is inclusive
(start_block, best_number)
}
};
Expand Down
4 changes: 4 additions & 0 deletions crates/storage/provider/src/traits/block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ pub trait BlockHashReader: Send + Sync {
}

/// Get headers in range of block hashes or numbers
///
/// Returns the available hashes of that range.
///
/// Note: The range is `start..end`, so the expected result is `[start..end)`
fn canonical_hashes_range(&self, start: BlockNumber, end: BlockNumber) -> Result<Vec<H256>>;
}
Loading