style: move function below impl (#9398)

This commit is contained in:
Matthias Seitz
2024-07-09 18:17:17 +02:00
committed by GitHub
parent d39069027c
commit 387587a1bb

View File

@@ -1,6 +1,5 @@
use alloy_primitives::Bytes;
use async_trait::async_trait;
use futures::future::BoxFuture;
use jsonrpsee::core::RpcResult;
use reth_primitives::{Address, BlockId, BlockNumberOrTag, TxHash, B256};
use reth_rpc_api::{EthApiServer, OtterscanServer};
@@ -22,6 +21,7 @@ use revm_inspectors::{
transfer::{TransferInspector, TransferKind},
};
use revm_primitives::ExecutionResult;
use std::future::Future;
const API_LEVEL: u64 = 8;
@@ -31,41 +31,6 @@ pub struct OtterscanApi<Eth> {
eth: Eth,
}
/// 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.
async fn binary_search<'a, F>(low: u64, high: u64, check: F) -> RpcResult<u64>
where
F: Fn(u64) -> BoxFuture<'a, RpcResult<bool>>,
{
let mut low = low;
let mut high = high;
let mut num = high;
while low <= high {
let mid = (low + high) / 2;
if check(mid).await? {
high = mid - 1;
num = mid;
} else {
low = mid + 1
}
}
Ok(num)
}
impl<Eth> OtterscanApi<Eth> {
/// Creates a new instance of `Otterscan`.
pub const fn new(eth: Eth) -> Self {
@@ -290,7 +255,7 @@ where
// perform a binary search over the block range to find the block in which the sender's
// nonce reached the requested nonce.
let num = binary_search(1, self.eth.block_number()?.saturating_to(), |mid| {
Box::pin(async move {
async move {
let mid_nonce =
EthApiServer::transaction_count(&self.eth, sender, Some(mid.into()))
.await?
@@ -301,7 +266,7 @@ where
// the transaction whose nonce is the pre-state, so need to compare with `nonce`(no
// equal).
Ok(mid_nonce > nonce)
})
}
})
.await?;
@@ -374,6 +339,42 @@ where
}
}
/// 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.
async fn binary_search<F, Fut>(low: u64, high: u64, check: F) -> RpcResult<u64>
where
F: Fn(u64) -> Fut,
Fut: Future<Output = RpcResult<bool>>,
{
let mut low = low;
let mut high = high;
let mut num = high;
while low <= high {
let mid = (low + high) / 2;
if check(mid).await? {
high = mid - 1;
num = mid;
} else {
low = mid + 1
}
}
Ok(num)
}
#[cfg(test)]
mod tests {
use super::*;