From 7c706a3c5f2eb712162045e6041710540e3461d7 Mon Sep 17 00:00:00 2001 From: Siyuan Han Date: Tue, 10 Sep 2024 00:03:39 +0800 Subject: [PATCH] fix: make file db benchmark runable (#364) * fix: make file_db benchmark runable * chore: cargo fmt --- benches/file_db.rs | 9 ++++++--- benches/get_balance.rs | 6 +++--- benches/get_code.rs | 6 +++--- benches/harness.rs | 31 ++++++++++++++++--------------- benches/sync.rs | 10 ++++------ 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/benches/file_db.rs b/benches/file_db.rs index 00137156..a32dbf6b 100644 --- a/benches/file_db.rs +++ b/benches/file_db.rs @@ -1,3 +1,4 @@ +use alloy::primitives::b256; use config::Config; use consensus::database::{Database, FileDB}; use criterion::{criterion_group, criterion_main, Criterion}; @@ -16,7 +17,8 @@ criterion_main!(file_db); /// Benchmark saving/writing a checkpoint to the file db. pub fn save_checkpoint(c: &mut Criterion) { c.bench_function("save_checkpoint", |b| { - let checkpoint: &[u8] = &[1, 2, 3]; + let checkpoint: alloy::primitives::FixedBytes<32> = + b256!("c7fc7b2f4b548bfc9305fa80bc1865ddc6eea4557f0a80507af5dc34db7bd9ce"); b.iter(|| { let data_dir = Some(tempdir().unwrap().into_path()); let config = Config { @@ -39,8 +41,9 @@ pub fn load_checkpoint(c: &mut Criterion) { ..Default::default() }; let db = FileDB::new(&config).unwrap(); - let written_checkpoint = vec![1; 32]; - db.save_checkpoint(&written_checkpoint.clone()).unwrap(); + let written_checkpoint = + b256!("c7fc7b2f4b548bfc9305fa80bc1865ddc6eea4557f0a80507af5dc34db7bd9ce"); + db.save_checkpoint(written_checkpoint.clone()).unwrap(); // Then read from the db b.iter(|| { diff --git a/benches/get_balance.rs b/benches/get_balance.rs index cc025129..745831c6 100644 --- a/benches/get_balance.rs +++ b/benches/get_balance.rs @@ -1,5 +1,5 @@ +use alloy::primitives::Address; use criterion::{criterion_group, criterion_main, Criterion}; -use ethers::prelude::*; use helios::types::BlockTag; use std::str::FromStr; @@ -32,7 +32,7 @@ pub fn bench_mainnet_get_balance(c: &mut Criterion) { // Execute the benchmark asynchronously. b.to_async(rt).iter(|| async { let inner = std::sync::Arc::clone(&client); - inner.get_balance(&addr, block).await.unwrap() + inner.get_balance(addr, block).await.unwrap() }) }); } @@ -64,7 +64,7 @@ pub fn bench_goerli_get_balance(c: &mut Criterion) { // Execute the benchmark asynchronously. b.to_async(rt).iter(|| async { let inner = std::sync::Arc::clone(&client); - inner.get_balance(&addr, block).await.unwrap() + inner.get_balance(addr, block).await.unwrap() }) }); } diff --git a/benches/get_code.rs b/benches/get_code.rs index f8dd9004..b2c923b9 100644 --- a/benches/get_code.rs +++ b/benches/get_code.rs @@ -1,5 +1,5 @@ +use alloy::primitives::Address; use criterion::{criterion_group, criterion_main, Criterion}; -use ethers::prelude::*; use helios::types::BlockTag; use std::str::FromStr; @@ -32,7 +32,7 @@ pub fn bench_mainnet_get_code(c: &mut Criterion) { // Execute the benchmark asynchronously. b.to_async(rt).iter(|| async { let inner = std::sync::Arc::clone(&client); - inner.get_code(&addr, block).await.unwrap() + inner.get_code(addr, block).await.unwrap() }) }); } @@ -57,7 +57,7 @@ pub fn bench_goerli_get_code(c: &mut Criterion) { // Execute the benchmark asynchronously. b.to_async(rt).iter(|| async { let inner = std::sync::Arc::clone(&client); - inner.get_code(&addr, block).await.unwrap() + inner.get_code(addr, block).await.unwrap() }) }); } diff --git a/benches/harness.rs b/benches/harness.rs index 0af21fc2..7586300a 100644 --- a/benches/harness.rs +++ b/benches/harness.rs @@ -1,14 +1,12 @@ #![allow(dead_code)] - -use std::str::FromStr; - use ::client::Client; - -use ethers::{ - abi::Address, - types::{H256, U256}, +use alloy::primitives::{Address, B256, U256}; +use helios::{ + client::{self, FileDB}, + config::networks, + types::BlockTag, }; -use helios::{client, config::networks, types::BlockTag}; +use std::{path::PathBuf, str::FromStr}; /// Fetches the latest mainnet checkpoint from the fallback service. /// @@ -16,7 +14,7 @@ use helios::{client, config::networks, types::BlockTag}; /// The `build` method will fetch a list of [CheckpointFallbackService](config::CheckpointFallbackService)s from a community-mainained list by ethPandaOps. /// This list is NOT guaranteed to be secure, but is provided in good faith. /// The raw list can be found here: https://github.com/ethpandaops/checkpoint-sync-health-checks/blob/master/_data/endpoints.yaml -pub async fn fetch_mainnet_checkpoint() -> eyre::Result { +pub async fn fetch_mainnet_checkpoint() -> eyre::Result { let cf = config::CheckpointFallback::new().build().await.unwrap(); cf.fetch_latest_checkpoint(&networks::Network::MAINNET) .await @@ -28,23 +26,26 @@ pub async fn fetch_mainnet_checkpoint() -> eyre::Result { /// The client is parameterized with a [FileDB](client::FileDB). /// It will also use the environment variable `MAINNET_EXECUTION_RPC` to connect to a mainnet node. /// The client will use `https://www.lightclientdata.org` as the consensus RPC. -pub fn construct_mainnet_client(rt: &tokio::runtime::Runtime) -> eyre::Result { +pub fn construct_mainnet_client(rt: &tokio::runtime::Runtime) -> eyre::Result> { rt.block_on(inner_construct_mainnet_client()) } -pub async fn inner_construct_mainnet_client() -> eyre::Result { +pub async fn inner_construct_mainnet_client() -> eyre::Result> { let benchmark_rpc_url = std::env::var("MAINNET_EXECUTION_RPC")?; let mut client = client::ClientBuilder::new() .network(networks::Network::MAINNET) .consensus_rpc("https://www.lightclientdata.org") .execution_rpc(&benchmark_rpc_url) .load_external_fallback() + .data_dir(PathBuf::from("/tmp/helios")) .build()?; client.start().await?; Ok(client) } -pub async fn construct_mainnet_client_with_checkpoint(checkpoint: &str) -> eyre::Result { +pub async fn construct_mainnet_client_with_checkpoint( + checkpoint: B256, +) -> eyre::Result> { let benchmark_rpc_url = std::env::var("MAINNET_EXECUTION_RPC")?; let mut client = client::ClientBuilder::new() .network(networks::Network::MAINNET) @@ -74,7 +75,7 @@ pub fn construct_runtime() -> tokio::runtime::Runtime { /// The client is parameterized with a [FileDB](client::FileDB). /// It will also use the environment variable `GOERLI_EXECUTION_RPC` to connect to a mainnet node. /// The client will use `http://testing.prater.beacon-api.nimbus.team` as the consensus RPC. -pub fn construct_goerli_client(rt: &tokio::runtime::Runtime) -> eyre::Result { +pub fn construct_goerli_client(rt: &tokio::runtime::Runtime) -> eyre::Result> { rt.block_on(async { let benchmark_rpc_url = std::env::var("GOERLI_EXECUTION_RPC")?; let mut client = client::ClientBuilder::new() @@ -91,13 +92,13 @@ pub fn construct_goerli_client(rt: &tokio::runtime::Runtime) -> eyre::Result, address: &str, ) -> eyre::Result { rt.block_on(async { let block = BlockTag::Latest; let address = Address::from_str(address)?; - client.get_balance(&address, block).await + client.get_balance(address, block).await }) } diff --git a/benches/sync.rs b/benches/sync.rs index 95de1f76..42700b91 100644 --- a/benches/sync.rs +++ b/benches/sync.rs @@ -1,5 +1,5 @@ +use alloy::primitives::Address; use criterion::{criterion_group, criterion_main, Criterion}; -use ethers::types::Address; use helios::types::BlockTag; mod harness; @@ -19,13 +19,12 @@ criterion_group! { pub fn bench_full_sync(c: &mut Criterion) { // Externally, let's fetch the latest checkpoint from our fallback service so as not to benchmark the checkpoint fetch. let checkpoint = harness::await_future(harness::fetch_mainnet_checkpoint()).unwrap(); - let checkpoint = hex::encode(checkpoint); // On client construction, it will sync to the latest checkpoint using our fetched checkpoint. c.bench_function("full_sync", |b| { b.to_async(harness::construct_runtime()).iter(|| async { let _client = std::sync::Arc::new( - harness::construct_mainnet_client_with_checkpoint(&checkpoint) + harness::construct_mainnet_client_with_checkpoint(checkpoint) .await .unwrap(), ); @@ -38,13 +37,12 @@ pub fn bench_full_sync(c: &mut Criterion) { pub fn bench_full_sync_with_call(c: &mut Criterion) { // Externally, let's fetch the latest checkpoint from our fallback service so as not to benchmark the checkpoint fetch. let checkpoint = harness::await_future(harness::fetch_mainnet_checkpoint()).unwrap(); - let checkpoint = hex::encode(checkpoint); // On client construction, it will sync to the latest checkpoint using our fetched checkpoint. c.bench_function("full_sync_call", |b| { b.to_async(harness::construct_runtime()).iter(|| async { let client = std::sync::Arc::new( - harness::construct_mainnet_client_with_checkpoint(&checkpoint) + harness::construct_mainnet_client_with_checkpoint(checkpoint) .await .unwrap(), ); @@ -52,7 +50,7 @@ pub fn bench_full_sync_with_call(c: &mut Criterion) { .parse::
() .unwrap(); let block = BlockTag::Latest; - client.get_balance(&addr, block).await.unwrap() + client.get_balance(addr, block).await.unwrap() }) }); }