Skip to content

Commit

Permalink
Cleanup code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Oct 31, 2024
1 parent 128a9ba commit 5f19932
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
14 changes: 11 additions & 3 deletions crates/fuel-gas-price-algorithm/gas-price-analysis/src/charts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ pub fn draw_bytes_and_cost_per_block(
Ok(())
}

fn one_gwei_i128() -> i128 {
i128::from(ONE_GWEI)
}

fn one_gwei_u128() -> u128 {
u128::from(ONE_GWEI)
}

pub fn draw_profit(
drawing_area: &DrawingArea<BitMapBackend, Shift>,
actual_profit: &[i128],
Expand All @@ -268,14 +276,14 @@ pub fn draw_profit(
const PROJECTED_PROFIT_COLOR: RGBColor = RED;
const PESSIMISTIC_BLOCK_COST_COLOR: RGBColor = BLUE;
let actual_profit_gwei: Vec<_> =
actual_profit.iter().map(|x| x / ONE_GWEI as i128).collect();
actual_profit.iter().map(|x| x / one_gwei_i128()).collect();
let projected_profit_gwei: Vec<_> = projected_profit
.iter()
.map(|x| x / ONE_GWEI as i128)
.map(|x| x / one_gwei_i128())
.collect();
let pessimistic_block_costs_gwei: Vec<_> = pessimistic_block_costs
.iter()
.map(|x| x / ONE_GWEI as u128)
.map(|x| x / one_gwei_u128())
.collect();
let min = *std::cmp::min(
actual_profit_gwei
Expand Down
9 changes: 6 additions & 3 deletions crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl AlgorithmUpdaterV1 {
heights: &[u32],
recording_cost: u128,
) -> Result<(), Error> {
let recorded_bytes = self.drain_l2_block_bytes_for_range(heights)?;
let recorded_bytes = self.drain_l2_block_bytes_for_heights(heights)?;
let new_cost_per_byte: u128 = recording_cost.checked_div(recorded_bytes).ok_or(
Error::CouldNotCalculateCostPerByte {
bytes: recorded_bytes,
Expand All @@ -525,7 +525,10 @@ impl AlgorithmUpdaterV1 {
Ok(())
}

fn drain_l2_block_bytes_for_range(&mut self, heights: &[u32]) -> Result<u128, Error> {
fn drain_l2_block_bytes_for_heights(
&mut self,
heights: &[u32],
) -> Result<u128, Error> {
let mut total: u128 = 0;
for expected_height in heights {
let bytes = self.unrecorded_blocks.remove(expected_height).ok_or(
Expand All @@ -543,7 +546,7 @@ impl AlgorithmUpdaterV1 {
let projection_portion: u128 = self
.unrecorded_blocks
.iter()
.map(|(_, &bytes)| bytes as u128)
.map(|(_, &bytes)| u128::from(bytes))
.fold(0_u128, |acc, n| acc.saturating_add(n))
.saturating_mul(self.latest_da_cost_per_byte);
self.projected_total_da_cost = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::v1::{
fn update_da_record_data__throws_error_if_receives_a_block_missing_from_unrecorded_blocks(
) {
// given
let recorded_range: Vec<u32> = (1u32..3).collect();
let recorded_heights: Vec<u32> = (1u32..3).collect();
let recorded_cost = 200;
let unrecorded_blocks = vec![BlockBytes {
height: 1,
Expand All @@ -22,7 +22,7 @@ fn update_da_record_data__throws_error_if_receives_a_block_missing_from_unrecord

// when
let actual_error = updater
.update_da_record_data(&recorded_range, recorded_cost)
.update_da_record_data(&recorded_heights, recorded_cost)
.unwrap_err();

// then
Expand All @@ -46,10 +46,10 @@ fn update_da_record_data__updates_cost_per_byte() {

let new_cost_per_byte = 100;
let recorded_cost = (block_bytes * new_cost_per_byte) as u128;
let recorded_range: Vec<u32> = (1u32..2).collect();
let recorded_heights: Vec<u32> = (1u32..2).collect();
// when
updater
.update_da_record_data(&recorded_range, recorded_cost)
.update_da_record_data(&recorded_heights, recorded_cost)
.unwrap();

// then
Expand Down Expand Up @@ -87,11 +87,11 @@ fn update_da_record_data__updates_known_total_cost() {
.with_unrecorded_blocks(unrecorded_blocks)
.build();

let recorded_range: Vec<u32> = (11u32..14).collect();
let recorded_heights: Vec<u32> = (11u32..14).collect();
let recorded_cost = 300;
// when
updater
.update_da_record_data(&recorded_range, recorded_cost)
.update_da_record_data(&recorded_heights, recorded_cost)
.unwrap();

// then
Expand Down Expand Up @@ -138,11 +138,11 @@ fn update_da_record_data__if_da_height_matches_l2_height_projected_and_known_mat
let new_cost_per_byte = 100;
let block_cost = block_bytes * new_cost_per_byte;

let recorded_range: Vec<u32> = (11u32..14).collect();
let recorded_heights: Vec<u32> = (11u32..14).collect();
let recorded_cost = block_cost * 3;
// when
updater
.update_da_record_data(&recorded_range, recorded_cost)
.update_da_record_data(&recorded_heights, recorded_cost)
.unwrap();

// then
Expand Down Expand Up @@ -211,11 +211,11 @@ fn update_da_record_data__da_block_updates_projected_total_cost_with_known_and_g
});
let min = recorded_heights.iter().min().unwrap();
let max = recorded_heights.iter().max().unwrap();
let recorded_range: Vec<u32> = (*min..(max + 1)).collect();
let recorded_heights: Vec<u32> = (*min..(max + 1)).collect();

// when
updater
.update_da_record_data(&recorded_range, recorded_cost as u128)
.update_da_record_data(&recorded_heights, recorded_cost as u128)
.unwrap();

// then
Expand Down Expand Up @@ -261,14 +261,14 @@ fn update_da_record_data__updates_known_total_cost_if_blocks_are_out_of_order()
.build();
let new_cost_per_byte = 100;
let recorded_cost = 2 * (block_bytes * new_cost_per_byte) as u128;
let recorded_range: Vec<u32> = vec![2, 3];
let recorded_heights: Vec<u32> = vec![3, 2];

// when
updater
.update_da_record_data(&recorded_range, recorded_cost)
.update_da_record_data(&recorded_heights, recorded_cost)
.unwrap();

// and
// then
let expected = updater.latest_known_total_da_cost_excess
+ (block_bytes * new_cost_per_byte) as u128;
let actual = updater.projected_total_da_cost;
Expand Down Expand Up @@ -306,11 +306,11 @@ fn update_da_record_data__updates_projected_total_cost_if_blocks_are_out_of_orde
.build();
let new_cost_per_byte = 100;
let recorded_cost = 2 * (block_bytes * new_cost_per_byte) as u128;
let recorded_range: Vec<u32> = vec![2, 3];
let recorded_heights: Vec<u32> = vec![3, 2];

// when
updater
.update_da_record_data(&recorded_range, recorded_cost)
.update_da_record_data(&recorded_heights, recorded_cost)
.unwrap();

// then
Expand Down Expand Up @@ -345,11 +345,11 @@ fn update_da_record_data__updates_unrecorded_blocks() {
.build();
let new_cost_per_byte = 100;
let recorded_cost = 2 * (block_bytes * new_cost_per_byte) as u128;
let recorded_range: Vec<u32> = vec![2, 3];
let recorded_heights: Vec<u32> = vec![3, 2];

// when
updater
.update_da_record_data(&recorded_range, recorded_cost)
.update_da_record_data(&recorded_heights, recorded_cost)
.unwrap();

// then
Expand Down

0 comments on commit 5f19932

Please sign in to comment.