Skip to content

Commit

Permalink
Improve vision into ophaned blocks, add more data
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Jan 9, 2025
1 parent 5d71959 commit d08820c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct L2BlockDataSource {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing::info!("Starting gas price data fetcher");
let Arg {
block_committer_endpoint,
l2_block_data_source,
Expand Down
27 changes: 19 additions & 8 deletions crates/fuel-gas-price-algorithm/gas-price-data-reader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ const WEI_PER_ETH: f64 = 1_000_000_000_000_000_000.;
#[derive(Debug, serde::Deserialize, Eq, PartialEq, Hash)]
struct Record {
l1_block_number: u64,
l1_blob_fee_wei: u128,
l1_blob_fee_wei: u64,
l1_blob_size_bytes: u64,
blob_start_l2_block_number: u32,
l2_block_number: u64,
l2_gas_fullness: u64,
l2_gas_capacity: u64,
l2_byte_size: u64,
l2_byte_size: u32,
l2_byte_capacity: u64,
l2_block_transactions_count: u64,
l2_gas_price: u64,
l2_fee: u64,
}
fn get_records_from_csv_file(file_path: &str) -> Vec<Record> {
let mut rdr = csv::ReaderBuilder::new()
Expand All @@ -24,11 +29,16 @@ fn get_records_from_csv_file(file_path: &str) -> Vec<Record> {
let headers = csv::StringRecord::from(vec![
"l1_block_number",
"l1_blob_fee_wei",
"l1_blob_size_bytes",
"blob_start_l2_block_number",
"l2_block_number",
"l2_gas_fullness",
"l2_gas_capacity",
"l2_byte_size",
"l2_byte_capacity",
"l2_block_transactions_count",
"l2_gas_price",
"l2_fee",
]);
let records = rdr
.records()
Expand Down Expand Up @@ -60,25 +70,26 @@ fn main() {
.iter()
.map(|r| (r.l1_block_number, r.l1_blob_fee_wei))
.collect::<HashMap<_, _>>();
let total_costs: u128 = costs.values().sum();
let total_costs: u64 = 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: {}", total_costs / length as u64);
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;
let average_cost_per_l2_gas_fullness = total_costs / total_l2_gas as u64;
println!(
"Average cost per l2 gas: {}",
average_cost_per_l2_gas_fullness
);
// cost per byte
let total_bytes: u32 = records.iter().map(|r| r.l2_byte_size).sum();
let cost_per_byte = total_costs as f64 / total_bytes as f64;
println!("Cost per byte: {}", cost_per_byte);
}
4 changes: 3 additions & 1 deletion crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ impl AlgorithmUpdaterV1 {
unrecorded_blocks: &mut U,
) -> Result<(), Error> {
let mut total: u128 = 0;
tracing::info!("removing for range: {:?}", heights);
for expected_height in heights {
let maybe_bytes = unrecorded_blocks
.remove(&expected_height)
Expand All @@ -642,8 +643,9 @@ impl AlgorithmUpdaterV1 {
);
}
}
tracing::info!("removing total: {}", total);
self.unrecorded_blocks_bytes = self.unrecorded_blocks_bytes.saturating_sub(total);

tracing::info!("unrecorded_blocks_bytes: {}", self.unrecorded_blocks_bytes);
Ok(())
}

Expand Down
72 changes: 62 additions & 10 deletions crates/services/gas_price_service/simulation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use serde_reflection::{
TracerConfig,
};
use std::{
collections::HashMap,
env,
path::PathBuf,
};
Expand Down Expand Up @@ -124,13 +125,17 @@ impl From<&Record> for BlockInfo {
struct SimulationResults {
gas_price: Vec<u64>,
profits: Vec<i128>,
costs: Vec<u128>,
rewards: Vec<u128>,
}

impl SimulationResults {
fn new() -> Self {
Self {
gas_price: Vec::new(),
profits: Vec::new(),
costs: Vec::new(),
rewards: Vec::new(),
}
}
}
Expand All @@ -143,6 +148,14 @@ impl SimulationResults {
pub fn add_profit(&mut self, profit: i128) {
self.profits.push(profit);
}

pub fn add_cost(&mut self, cost: u128) {
self.costs.push(cost);
}

pub fn add_reward(&mut self, reward: u128) {
self.rewards.push(reward);
}
}

fn get_data(source: &DataSource) -> anyhow::Result<Data> {
Expand Down Expand Up @@ -181,11 +194,8 @@ fn get_data(source: &DataSource) -> anyhow::Result<Data> {
.unique()
.collect();
tracing::info!("l1_blocks: {:?}", l1_blocks);
let mut orphaned_l2_blocks = HashMap::new();
for record in records.into_iter() {
// let blocks: Vec<_> = block_records.into_iter().collect();
// let mut blocks_iter = blocks.iter().peekable();

// while let Some(block_record) = blocks_iter.next() {
let l2_block: BlockInfo = (&record).into();

if record.l1_block_number != 0 {
Expand Down Expand Up @@ -218,10 +228,28 @@ fn get_data(source: &DataSource) -> anyhow::Result<Data> {
})
.collect();

data.push((l2_block, da_block_costs));
// }
data.push((l2_block, da_block_costs.clone()));
orphaned_l2_blocks.insert(record.l2_block_number, l2_block);
for costs in da_block_costs {
for height in costs.l2_blocks.clone() {
orphaned_l2_blocks.remove(&(height as u64));
}
}
}

let total_orphaned = orphaned_l2_blocks.len();
let total_orphaned_bytes = orphaned_l2_blocks
.values()
.filter_map(|block| {
if let BlockInfo::Block { block_bytes, .. } = block {
Some(*block_bytes)
} else {
None
}
})
.sum::<u64>();

tracing::info!("Total orphaned L2 blocks: {}", total_orphaned);
tracing::info!("Total orphaned L2 block bytes: {}", total_orphaned_bytes);
let data = Data { inner: data };
Ok(data)
}
Expand All @@ -236,8 +264,10 @@ async fn simulation(
service_controller.advance(block, maybe_costs).await?;
let gas_price = service_controller.gas_price();
results.add_gas_price(gas_price);
let profit = service_controller.profit()?;
let (profit, cost, reward) = service_controller.profit_cost_reward()?;
results.add_profit(profit);
results.add_cost(cost);
results.add_reward(reward);
}
tracing::info!("Finished simulation");
Ok(results)
Expand All @@ -254,9 +284,31 @@ fn display_results(results: SimulationResults) -> anyhow::Result<()> {
// println!("Gas prices (Wei): {:?}", results.gas_price);
// println!("Profits (ETH): {:?}", profits_eth);
let max_gas_price = results.gas_price.iter().max();
let max_profit = profits_eth.iter().max_by(|a, b| a.partial_cmp(b).unwrap());
let min_gas_price = results.gas_price.iter().min();
let max_profit_eth = profits_eth.iter().max_by(|a, b| a.partial_cmp(b).unwrap());
let min_profit_eth = profits_eth.iter().min_by(|a, b| a.partial_cmp(b).unwrap());
let final_profit = profits_eth.last().unwrap();
let final_cost = results
.costs
.last()
.map(|x| *x as f64 / WEI_PER_ETH)
.unwrap();
let final_reward = results
.rewards
.last()
.map(|x| *x as f64 / WEI_PER_ETH)
.unwrap();
println!("Max gas price: {:?}", max_gas_price);
println!("Max profit ETH: {:?}", max_profit);
println!("Min gas price: {:?}", min_gas_price);
println!("Final gas price: {:?}", results.gas_price.last().unwrap());

println!("Max profit ETH: {:?}", max_profit_eth);
println!("Min profit ETH: {:?}", min_profit_eth);
println!("Final profit: {:?}", final_profit);

println!("Final cost: {:?}", final_cost);

println!("Final reward: {:?}", final_reward);
Ok(())
}

Expand Down
23 changes: 16 additions & 7 deletions crates/services/gas_price_service/simulation/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ fn read_config_from_file(_config_path: &str) -> V1AlgorithmConfig {
l2_block_fullness_threshold_percent: 0,
gas_price_factor: NonZero::new(100).unwrap(),
min_da_gas_price: 1_000,
max_da_gas_price: u64::MAX,
max_da_gas_price_change_percent: 10,
da_p_component: 1,
da_d_component: 1,
da_p_component: 100_000_000__000_000_000,
da_d_component: 1_000_000__000_000_000,
normal_range_size: 0,
capped_range_size: 0,
decrease_range_size: 0,
Expand All @@ -72,7 +73,9 @@ fn read_metadata_from_file(_metadata_path: &str, starting_height: u32) -> V1Meta
// TODO: read from file and/or CLI
let l2_block_height = starting_height - 1;
let gas_price_factor = 100;
let new_scaled_da_gas_price = 1_000_000 * gas_price_factor;
// let new_scaled_da_gas_price = 3_547_063 * gas_price_factor / 1_000;
// let new_scaled_da_gas_price = 15_893_241 * gas_price_factor / 1_000;
let new_scaled_da_gas_price = 1_000 * gas_price_factor;
V1Metadata {
new_scaled_exec_price: 0,
l2_block_height,
Expand All @@ -82,7 +85,7 @@ fn read_metadata_from_file(_metadata_path: &str, starting_height: u32) -> V1Meta
latest_known_total_da_cost_excess: 0,
last_profit: 0,
second_to_last_profit: 0,
latest_da_cost_per_byte: 0,
latest_da_cost_per_byte: 625504961,
unrecorded_block_bytes: 0,
}
}
Expand Down Expand Up @@ -140,15 +143,21 @@ impl ServiceController {
self.service.next_block_algorithm().next_gas_price()
}

pub fn profit(&self) -> anyhow::Result<i128> {
pub fn profit_cost_reward(&self) -> anyhow::Result<(i128, u128, u128)> {
let latest_height = self.service.latest_l2_block();
let metadata = self
.service
.storage_tx_provider()
.get_metadata(&latest_height)?
.ok_or(anyhow::anyhow!("no metadata found"))?;
if let Some(profit) = metadata.v1().map(|m| m.last_profit) {
Ok(profit)
if let Some(values) = metadata.v1().map(|m| {
(
m.last_profit,
m.latest_known_total_da_cost_excess,
m.total_da_rewards_excess,
)
}) {
Ok(values)
} else {
Err(anyhow::anyhow!("no profit found"))
}
Expand Down

0 comments on commit d08820c

Please sign in to comment.