Skip to content

Commit

Permalink
Use bytes from unrecorded_blocks rather from the block from DA (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner authored Oct 2, 2024
1 parent 4ba9da8 commit c039c28
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ pub fn draw_profit(
.x_label_area_size(40)
.y_label_area_size(100)
.right_y_label_area_size(100)
.build_cartesian_2d(0..actual_profit_gwei.len(), min..max)
.build_cartesian_2d(0..projected_profit.len(), min..max)
.unwrap()
.set_secondary_coord(
0..actual_profit_gwei.len(),
0..projected_profit.len(),
0..*pessimistic_block_costs_gwei.iter().max().unwrap(),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use fuel_gas_price_algorithm::v1::{
AlgorithmUpdaterV1,
RecordedBlock,
};
use std::num::NonZeroU64;

use super::*;
use fuel_gas_price_algorithm::v1::AlgorithmUpdaterV1;
use std::{
collections::BTreeMap,
num::NonZeroU64,
ops::Range,
};

pub mod da_cost_per_byte;

Expand All @@ -24,6 +24,13 @@ pub struct Simulator {
da_cost_per_byte: Vec<u64>,
}

// (usize, ((u64, u64), &'a Option<(Range<u32>, u128)
struct BlockData {
fullness: u64,
bytes: u64,
maybe_da_block: Option<(Range<u32>, u128)>,
}

impl Simulator {
pub fn new(da_cost_per_byte: Vec<u64>) -> Self {
Simulator { da_cost_per_byte }
Expand All @@ -49,7 +56,14 @@ impl Simulator {
&fullness_and_bytes,
);

let blocks = l2_blocks.zip(da_blocks.iter()).enumerate();
let blocks = l2_blocks
.zip(da_blocks.iter())
.map(|((fullness, bytes), maybe_da_block)| BlockData {
fullness,
bytes,
maybe_da_block: maybe_da_block.clone(),
})
.enumerate();

let updater = self.build_updater(da_p_component, da_d_component);

Expand Down Expand Up @@ -90,7 +104,7 @@ impl Simulator {
latest_da_cost_per_byte: 0,
projected_total_da_cost: 0,
latest_known_total_da_cost_excess: 0,
unrecorded_blocks: vec![],
unrecorded_blocks: BTreeMap::new(),
da_p_component,
da_d_component,
last_profit: 0,
Expand All @@ -104,8 +118,7 @@ impl Simulator {
capacity: u64,
max_block_bytes: u64,
fullness_and_bytes: Vec<(u64, u64)>,
// blocks: Enumerate<Zip<Iter<(u64, u64)>, Iter<Option<Vec<RecordedBlock>>>>>,
blocks: impl Iterator<Item = (usize, ((u64, u64), &'a Option<Vec<RecordedBlock>>))>,
blocks: impl Iterator<Item = (usize, BlockData)>,
mut updater: AlgorithmUpdaterV1,
) -> SimulationResults {
let mut gas_prices = vec![];
Expand All @@ -115,13 +128,18 @@ impl Simulator {
let mut projected_cost_totals = vec![];
let mut actual_costs = vec![];
let mut pessimistic_costs = vec![];
for (index, ((fullness, bytes), da_block)) in blocks {
for (index, block_data) in blocks {
let BlockData {
fullness,
bytes,
maybe_da_block: da_block,
} = block_data;
let height = index as u32 + 1;
exec_gas_prices.push(updater.new_scaled_exec_price);
da_gas_prices.push(updater.new_scaled_da_gas_price);
let gas_price = updater.algorithm().calculate();
gas_prices.push(gas_price);
let total_fee = gas_price * fullness;
let total_fee = gas_price as u128 * fullness as u128;
updater
.update_l2_block_data(
height,
Expand All @@ -137,13 +155,13 @@ impl Simulator {
projected_cost_totals.push(updater.projected_total_da_cost);

// Update DA blocks on the occasion there is one
if let Some(da_blocks) = &da_block {
let mut total_cost = updater.latest_known_total_da_cost_excess;
for block in da_blocks {
total_cost += block.block_cost as u128;
actual_costs.push(total_cost);
if let Some((range, cost)) = da_block {
for height in range.to_owned() {
updater
.update_da_record_data(height..(height + 1), cost)
.unwrap();
actual_costs.push(updater.latest_known_total_da_cost_excess)
}
updater.update_da_record_data(&da_blocks).unwrap();
}
}
let (fullness_without_capacity, bytes): (Vec<_>, Vec<_>) =
Expand Down Expand Up @@ -187,7 +205,7 @@ impl Simulator {
da_recording_rate: usize,
da_finalization_rate: usize,
fullness_and_bytes: &Vec<(u64, u64)>,
) -> Vec<Option<Vec<RecordedBlock>>> {
) -> Vec<Option<(Range<u32>, u128)>> {
let l2_blocks_with_no_da_blocks =
std::iter::repeat(None).take(da_finalization_rate);
let (_, da_blocks) = fullness_and_bytes
Expand All @@ -199,12 +217,9 @@ impl Simulator {
|(mut delayed, mut recorded),
(index, ((_fullness, bytes), cost_per_byte))| {
let total_cost = *bytes * cost_per_byte;

let height = index as u32 + 1;
let converted = RecordedBlock {
height,
block_bytes: *bytes,
block_cost: total_cost as u64,
};
let converted = (height, bytes, total_cost);
delayed.push(converted);
if delayed.len() == da_recording_rate {
recorded.push(Some(delayed));
Expand All @@ -215,7 +230,16 @@ impl Simulator {
}
},
);
l2_blocks_with_no_da_blocks.chain(da_blocks).collect()
let da_block_ranges = da_blocks.into_iter().map(|maybe_recorded_blocks| {
maybe_recorded_blocks.map(|list| {
let heights_iter = list.iter().map(|(height, _, _)| *height);
let min = heights_iter.clone().min().unwrap();
let max = heights_iter.max().unwrap();
let cost: u128 = list.iter().map(|(_, _, cost)| *cost as u128).sum();
(min..(max + 1), cost)
})
});
l2_blocks_with_no_da_blocks.chain(da_block_ranges).collect()
}
}

Expand Down
6 changes: 0 additions & 6 deletions crates/fuel-gas-price-algorithm/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ pub struct AlgorithmUpdaterV0 {
pub l2_block_fullness_threshold_percent: u64,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
pub struct BlockBytes {
pub height: u32,
pub block_bytes: u64,
}

impl AlgorithmUpdaterV0 {
pub fn new(
new_exec_price: u64,
Expand Down
Loading

0 comments on commit c039c28

Please sign in to comment.