-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds a binary search to get block by timestamp
- Loading branch information
Showing
4 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,4 @@ tokio = "1.32.0" | |
|
||
[dev-dependencies] | ||
hex = "0.4" | ||
pretty_assertions = "1" | ||
pretty_assertions = "1.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::sync::Arc; | ||
|
||
use chrono::{prelude::*, Datelike, TimeZone, Utc}; | ||
use ethers::providers::{Http, Provider}; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[tokio::test] | ||
async fn request_first_block_unlimited() { | ||
let provider_l2 = Provider::<Http>::try_from("https://mainnet.era.zksync.io").unwrap(); | ||
let client_l2 = Arc::new(provider_l2); | ||
|
||
let date = "2023-10-5T12:00:00Z".parse::<DateTime<Utc>>().unwrap(); | ||
|
||
let previous_midnight = Utc | ||
.with_ymd_and_hms(date.year(), date.month(), date.day(), 0, 0, 0) | ||
.unwrap(); | ||
|
||
let res = client::get_block_number_by_timestamp(previous_midnight, None, client_l2) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
// First block on 2023-10-5 https://explorer.zksync.io/block/15560287 | ||
assert_eq!(res, 15560287.into()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn request_first_block_limited() { | ||
let provider_l2 = Provider::<Http>::try_from("https://mainnet.era.zksync.io").unwrap(); | ||
let client_l2 = Arc::new(provider_l2); | ||
|
||
let date = "2023-10-5T12:00:00Z".parse::<DateTime<Utc>>().unwrap(); | ||
|
||
let previous_midnight = Utc | ||
.with_ymd_and_hms(date.year(), date.month(), date.day(), 0, 0, 0) | ||
.unwrap(); | ||
|
||
let res = | ||
client::get_block_number_by_timestamp(previous_midnight, Some(15000000.into()), client_l2) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
// First block on 2023-10-5 https://explorer.zksync.io/block/15560287 | ||
assert_eq!(res, 15560287.into()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn request_from_the_future() { | ||
let provider_l2 = Provider::<Http>::try_from("https://mainnet.era.zksync.io").unwrap(); | ||
let client_l2 = Arc::new(provider_l2); | ||
|
||
let date = Utc::now(); | ||
|
||
let previous_midnight = Utc | ||
.with_ymd_and_hms(date.year(), date.month(), date.day() + 1, 0, 0, 0) | ||
.unwrap(); | ||
|
||
let res = | ||
client::get_block_number_by_timestamp(previous_midnight, Some(15000000.into()), client_l2) | ||
.await | ||
.unwrap(); | ||
|
||
// No first block in the next day | ||
assert_eq!(res, None); | ||
} |