Skip to content

Commit

Permalink
rpcdaemon: find min block after binary search in erigon_getBlockByTim…
Browse files Browse the repository at this point in the history
…estamp (#1738)
  • Loading branch information
lupin012 authored Jan 5, 2024
1 parent 43f818f commit 2651b6a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions silkworm/rpc/commands/erigon_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,21 @@ Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::js
block_number = core::kEarliestBlockNumber;
} else {
// Good-old binary search to find the lowest block header matching timestamp
const auto matching_block_number = co_await binary_search(current_block_number, [&](uint64_t i) -> Task<bool> {
const auto header = co_await chain_storage->read_canonical_header(i);
auto matching_block_number = co_await binary_search(current_block_number, [&](uint64_t bn) -> Task<bool> {
const auto header = co_await chain_storage->read_canonical_header(bn);
co_return header->timestamp >= timestamp;
});
// TODO(canepat) we should try to avoid this block header lookup (just done in search)
const auto matching_header = co_await chain_storage->read_canonical_header(matching_block_number);
if (matching_header->timestamp > timestamp) {
block_number = matching_block_number - 1;
} else {
block_number = matching_block_number;
auto matching_header = co_await chain_storage->read_canonical_header(matching_block_number);
while (matching_header->timestamp > timestamp) {
const auto header = co_await chain_storage->read_canonical_header(matching_block_number - 1);
if (!header || header->timestamp < timestamp) {
break;
}
matching_block_number = matching_block_number - 1;
matching_header = header;
}
block_number = matching_block_number;
}

// Lookup and return the matching block
Expand Down

0 comments on commit 2651b6a

Please sign in to comment.