Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reader bin for interpretting data from fetcher #2537

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Cargo.lock
data
14 changes: 11 additions & 3 deletions crates/fuel-gas-price-algorithm/gas-price-data-fetcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ mod layer2;
mod summary;
mod types;

const SENTRY_NODE_GRAPHQL_RESULTS_PER_QUERY: usize = 5_000;
// const SENTRY_NODE_GRAPHQL_RESULTS_PER_QUERY: usize = 5_000;
const SENTRY_NODE_GRAPHQL_RESULTS_PER_QUERY: usize = 40;

#[derive(Parser)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -121,10 +122,17 @@ async fn main() -> anyhow::Result<()> {
let sentry_node_client = layer2::BlockFetcher::new(sentry_node_endpoint)?;
let blocks_range = start_block_included..end_block_excluded;

let blocks_with_gas_consumed = sentry_node_client
tracing::info!(
"Retrieving L2 data for blocks: {}..{}",
start_block_included,
end_block_excluded
);
let res = sentry_node_client
.get_l2_block_data(blocks_range, SENTRY_NODE_GRAPHQL_RESULTS_PER_QUERY)
.await?;
.await;
tracing::info!("results: {:?}", res);

let blocks_with_gas_consumed = res?;
for (
_block_height,
Layer2BlockData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl IntoIterator for BlockCommitterCosts {
}
}

#[derive(Debug)]
pub struct Layer2BlockData {
pub block_height: BlockHeight,
pub block_size: BytesSize,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cargo.lock
data
10 changes: 10 additions & 0 deletions crates/fuel-gas-price-algorithm/gas-price-data-reader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "gas-price-data-reader"
version = "0.1.0"
edition = "2021"

[dependencies]
csv = "1.3.1"
serde = { version = "1.0.217", features = ["derive"] }

[workspace]
84 changes: 84 additions & 0 deletions crates/fuel-gas-price-algorithm/gas-price-data-reader/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::collections::HashMap;

const WEI_PER_ETH: f64 = 1_000_000_000_000_000_000.;
// l1_block_number,l1_blob_fee_wei,l2_block_number,l2_gas_fullness,l2_gas_capacity,l2_byte_size,l2_byte_capacity
// 21403864,509018984154240,9099900,0,30000000,488,260096
// 21403864,509018984154240,9099901,1073531,30000000,3943,260096
// 21403864,509018984154240,9099902,0,30000000,488,260096
// parse data
#[derive(Debug, serde::Deserialize, Eq, PartialEq, Hash)]
struct Record {
l1_block_number: u64,
l1_blob_fee_wei: u128,
l2_block_number: u64,
l2_gas_fullness: u64,
l2_gas_capacity: u64,
l2_byte_size: u64,
l2_byte_capacity: u64,
}
fn get_records_from_csv_file(file_path: &str) -> Vec<Record> {
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.from_path(file_path)
.unwrap();
let headers = csv::StringRecord::from(vec![
"l1_block_number",
"l1_blob_fee_wei",
"l2_block_number",
"l2_gas_fullness",
"l2_gas_capacity",
"l2_byte_size",
"l2_byte_capacity",
]);
let records = rdr
.records()
.skip(1)
.map(|r| r.unwrap().deserialize(Some(&headers)).unwrap())
.collect::<Vec<Record>>();
records
}

const ENV_VAR_NAME: &str = "BLOCK_HISTORY_FILE";
fn get_path_to_file() -> String {
if let Some(path) = std::env::var_os(ENV_VAR_NAME) {
return path.to_str().unwrap().to_string();
} else {
let maybe_path = std::env::args().nth(1);
if let Some(path) = maybe_path {
return path;
} else {
panic!("Please provide a path to the file or set the {ENV_VAR_NAME} environment variable");
}
}
}

fn main() {
let path = get_path_to_file();
let records = get_records_from_csv_file(&path);
let length = records.len();
let costs = records
.iter()
.map(|r| (r.l1_block_number, r.l1_blob_fee_wei))
.collect::<HashMap<_, _>>();
let total_costs: u128 = costs.values().sum();
let total_l2_gas = records.iter().map(|r| r.l2_gas_fullness).sum::<u64>();

// println!("Average cost: {}", average);
println!("Length: {}", length);
println!("Total cost: {}", total_costs);
println!("Total cost (ETH): {}", total_costs as f64 / WEI_PER_ETH);
println!(
"Average cost per l2 block: {}",
total_costs / length as u128
);
println!(
"Average cost per l2 block (ETH): {}",
(total_costs as f64 / length as f64) / WEI_PER_ETH
);
// get cost per l2 gas fullness
let average_cost_per_l2_gas_fullness = total_costs / total_l2_gas as u128;
println!(
"Average cost per l2 gas: {}",
average_cost_per_l2_gas_fullness
);
}
Loading