diff --git a/crates/rpc/rpc/src/otterscan.rs b/crates/rpc/rpc/src/otterscan.rs index cb68d0494926f..86272b1fba87e 100644 --- a/crates/rpc/rpc/src/otterscan.rs +++ b/crates/rpc/rpc/src/otterscan.rs @@ -33,6 +33,10 @@ pub struct OtterscanApi { /// 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 where F: Fn(u64) -> BoxFuture<'a, RpcResult>, @@ -281,3 +285,27 @@ where Err(internal_rpc_err("unimplemented")) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn test_binary_search() { + // in the middle + let num = binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 5) })).await; + assert_eq!(num, Ok(5)); + + // in the upper + let num = binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 7) })).await; + assert_eq!(num, Ok(7)); + + // in the lower + let num = binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 1) })).await; + assert_eq!(num, Ok(1)); + + // high than the upper + let num = binary_search(1, 10, |mid| Box::pin(async move { Ok(mid >= 11) })).await; + assert_eq!(num, Ok(10)); + } +}