reth_rpc_eth_types::utils

Function binary_search

Source
pub async fn binary_search<F, Fut, E>(
    low: u64,
    high: u64,
    check: F,
) -> Result<u64, E>
where F: Fn(u64) -> Fut, Fut: Future<Output = Result<bool, E>>,
Expand description

Performs a binary search within a given block range to find the desired block number.

The binary search is performed by calling the provided asynchronous check closure on the blocks of the range. The closure should return a future representing the result of performing the desired logic at a given block. The future resolves to an bool where:

  • true indicates that the condition has been matched, but we can try to find a lower block to make the condition more matchable.
  • false indicates that the condition not matched, so the target is not present in the current block and should continue searching in a higher range.

Args:

  • low: The lower bound of the block range (inclusive).
  • high: The upper bound of the block range (inclusive).
  • check: A closure that performs the desired logic at a given block.