Skip to content

Commit

Permalink
Use range for v1 algorithm commit blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Dec 18, 2024
1 parent eb29a3a commit fc107cc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 160 deletions.
13 changes: 8 additions & 5 deletions crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{
cmp::max,
collections::BTreeMap,
num::NonZeroU64,
ops::Div,
ops::{
Div,
RangeInclusive,
},
};

#[cfg(test)]
Expand Down Expand Up @@ -349,7 +352,7 @@ impl core::ops::Deref for ClampedPercentage {
impl AlgorithmUpdaterV1 {
pub fn update_da_record_data<U: UnrecordedBlocks>(
&mut self,
heights: &[u32],
heights: RangeInclusive<u32>,
recorded_bytes: u32,
recording_cost: u128,
unrecorded_blocks: &mut U,
Expand Down Expand Up @@ -584,7 +587,7 @@ impl AlgorithmUpdaterV1 {

fn da_block_update<U: UnrecordedBlocks>(
&mut self,
heights: &[u32],
heights: RangeInclusive<u32>,
recorded_bytes: u128,
recording_cost: u128,
unrecorded_blocks: &mut U,
Expand Down Expand Up @@ -613,13 +616,13 @@ impl AlgorithmUpdaterV1 {
// Always remove the blocks from the unrecorded blocks so they don't build up indefinitely
fn update_unrecorded_block_bytes<U: UnrecordedBlocks>(
&mut self,
heights: &[u32],
heights: RangeInclusive<u32>,
unrecorded_blocks: &mut U,
) -> Result<(), Error> {
let mut total: u128 = 0;
for expected_height in heights {
let maybe_bytes = unrecorded_blocks
.remove(expected_height)
.remove(&expected_height)
.map_err(Error::CouldNotRemoveUnrecordedBlock)?;

if let Some(bytes) = maybe_bytes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::BTreeMap;
fn update_da_record_data__if_receives_batch_with_unknown_blocks_will_include_known_blocks_with_previous_cost(
) {
// given
let recorded_heights: Vec<u32> = (1u32..3).collect();
let recorded_heights = 1u32..=2;
let recorded_cost = 1_000_000;
let recorded_bytes = 500;
let block_bytes = 1000;
Expand All @@ -22,7 +22,7 @@ fn update_da_record_data__if_receives_batch_with_unknown_blocks_will_include_kno
// when
updater
.update_da_record_data(
&recorded_heights,
recorded_heights,
recorded_bytes,
recorded_cost,
&mut unrecorded_blocks,
Expand All @@ -41,7 +41,7 @@ fn update_da_record_data__if_receives_batch_with_unknown_blocks_will_include_kno
fn update_da_record_data__if_receives_batch_with_unknown_blocks_will_never_increase_cost_more_than_recorded_cost(
) {
// given
let recorded_heights: Vec<u32> = (1u32..3).collect();
let recorded_heights = 1u32..=2;
let recorded_cost = 200;
let block_bytes = 1000;
let recorded_bytes = 500;
Expand All @@ -58,7 +58,7 @@ fn update_da_record_data__if_receives_batch_with_unknown_blocks_will_never_incre
// when
updater
.update_da_record_data(
&recorded_heights,
recorded_heights,
recorded_bytes,
recorded_cost,
&mut unrecorded_blocks,
Expand Down Expand Up @@ -87,11 +87,11 @@ fn update_da_record_data__updates_cost_per_byte() {
let new_cost_per_byte = 100;
let recorded_bytes = 500;
let recorded_cost = (recorded_bytes * new_cost_per_byte) as u128;
let recorded_heights: Vec<u32> = (1u32..2).collect();
let recorded_heights = 1u32..=1;
// when
updater
.update_da_record_data(
&recorded_heights,
recorded_heights,
recorded_bytes,
recorded_cost,
&mut unrecorded_blocks,
Expand Down Expand Up @@ -121,13 +121,13 @@ fn update_da_record_data__updates_known_total_cost() {
.with_unrecorded_blocks(&unrecorded_blocks)
.build();

let recorded_heights: Vec<u32> = (11u32..14).collect();
let recorded_heights = 11u32..=13;
let recorded_bytes = 500;
let recorded_cost = 300;
// when
updater
.update_da_record_data(
&recorded_heights,
recorded_heights,
recorded_bytes,
recorded_cost,
&mut unrecorded_blocks,
Expand Down Expand Up @@ -166,13 +166,13 @@ 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_heights: Vec<u32> = (11u32..14).collect();
let recorded_heights = 11u32..=13;
let recorded_bytes = 500;
let recorded_cost = block_cost * 3;
// when
updater
.update_da_record_data(
&recorded_heights,
recorded_heights,
recorded_bytes,
recorded_cost,
&mut unrecorded_blocks,
Expand Down Expand Up @@ -216,15 +216,15 @@ fn update_da_record_data__da_block_updates_projected_total_cost_with_known_and_g
.build();

let new_cost_per_byte = 100;
let recorded_heights = vec![11, 12, 13];
let recorded_heights = 11u32..=13;
let recorded_bytes = 500;
let recorded_cost = recorded_bytes * new_cost_per_byte;
let recorded_bytes = 500;

// when
updater
.update_da_record_data(
&recorded_heights,
recorded_heights,
recorded_bytes,
recorded_cost,
&mut unrecorded_blocks,
Expand All @@ -242,124 +242,6 @@ fn update_da_record_data__da_block_updates_projected_total_cost_with_known_and_g
assert_eq!(actual, expected);
}

#[test]
fn update_da_record_data__updates_known_total_cost_if_blocks_are_out_of_order() {
// given
let da_cost_per_byte = 20;
let block_bytes = 1000;
let mut unrecorded_blocks: BTreeMap<_, _> =
[(1, block_bytes), (2, block_bytes), (3, block_bytes)]
.into_iter()
.collect();
let old_known_total_cost = 500;
let old_projected_total_cost =
old_known_total_cost + (block_bytes as u128 * da_cost_per_byte * 3);
let old_da_cost_per_byte = 20;
let mut updater = UpdaterBuilder::new()
.with_da_cost_per_byte(da_cost_per_byte)
.with_unrecorded_blocks(&unrecorded_blocks)
.with_da_cost_per_byte(old_da_cost_per_byte)
.with_known_total_cost(old_known_total_cost)
.with_projected_total_cost(old_projected_total_cost)
.build();
let new_cost_per_byte = 100;
let recorded_bytes = 500;
let recorded_cost = recorded_bytes * new_cost_per_byte;
let recorded_heights: Vec<u32> = vec![3, 2];

// when
updater
.update_da_record_data(
&recorded_heights,
recorded_bytes,
recorded_cost as u128,
&mut unrecorded_blocks,
)
.unwrap();

// then
let expected = updater.latest_known_total_da_cost_excess
+ (block_bytes as u128 * new_cost_per_byte as u128);
let actual = updater.projected_total_da_cost;
assert_eq!(actual, expected);
}

#[test]
fn update_da_record_data__updates_projected_total_cost_if_blocks_are_out_of_order() {
// given
let da_cost_per_byte = 20;
let block_bytes = 1000;
let mut unrecorded_blocks: BTreeMap<_, _> =
[(1, block_bytes), (2, block_bytes), (3, block_bytes)]
.into_iter()
.collect();
let old_known_total_cost = 500;
let old_projected_total_cost =
old_known_total_cost + (block_bytes as u128 * da_cost_per_byte * 3);
let old_da_cost_per_byte = 20;
let mut updater = UpdaterBuilder::new()
.with_da_cost_per_byte(da_cost_per_byte)
.with_unrecorded_blocks(&unrecorded_blocks)
.with_da_cost_per_byte(old_da_cost_per_byte)
.with_known_total_cost(old_known_total_cost)
.with_projected_total_cost(old_projected_total_cost)
.build();
let new_cost_per_byte = 100;
let recorded_bytes = 500;
let recorded_cost = recorded_bytes * new_cost_per_byte;
let recorded_heights: Vec<u32> = vec![3, 2];

// when
updater
.update_da_record_data(
&recorded_heights,
recorded_bytes,
recorded_cost as u128,
&mut unrecorded_blocks,
)
.unwrap();

// then
let expected = updater.latest_known_total_da_cost_excess
+ (block_bytes as u128 * new_cost_per_byte as u128);
let actual = updater.projected_total_da_cost;
assert_eq!(actual, expected);
}

#[test]
fn update_da_record_data__updates_unrecorded_blocks() {
// given
let da_cost_per_byte = 20;
let block_bytes = 1000;
let mut unrecorded_blocks: BTreeMap<_, _> =
[(1, block_bytes), (2, block_bytes), (3, block_bytes)]
.into_iter()
.collect();
let mut updater = UpdaterBuilder::new()
.with_da_cost_per_byte(da_cost_per_byte)
.with_unrecorded_blocks(&unrecorded_blocks)
.build();
let new_cost_per_byte = 100;
let recorded_bytes = 500;
let recorded_cost = 2 * (recorded_bytes * new_cost_per_byte) as u128;
let recorded_heights: Vec<u32> = vec![3, 2];

// when
updater
.update_da_record_data(
&recorded_heights,
recorded_bytes,
recorded_cost,
&mut unrecorded_blocks,
)
.unwrap();

// then
let expected = vec![(1, block_bytes)];
let actual: Vec<_> = unrecorded_blocks.into_iter().collect();
assert_eq!(actual, expected);
}

#[test]
fn update_da_record_data__da_block_lowers_da_gas_price() {
// given
Expand Down Expand Up @@ -394,15 +276,15 @@ fn update_da_record_data__da_block_lowers_da_gas_price() {
);
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_range = *min..=*max;
let recorded_bytes = 500;

let old_da_gas_price = updater.new_scaled_da_gas_price;

// when
updater
.update_da_record_data(
&recorded_range,
recorded_range,
recorded_bytes,
recorded_cost as u128,
&mut unrecorded_blocks,
Expand Down Expand Up @@ -451,15 +333,15 @@ fn update_da_record_data__da_block_increases_da_gas_price() {

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_range = *min..=*max;
let recorded_bytes = 500;

let old_da_gas_price = updater.new_scaled_da_gas_price;

// when
updater
.update_da_record_data(
&recorded_range,
recorded_range,
recorded_bytes,
recorded_cost as u128,
&mut unrecorded_blocks,
Expand Down Expand Up @@ -507,15 +389,15 @@ fn update_da_record_data__da_block_will_not_change_da_gas_price() {
);
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_range = *min..=*max;
let recorded_bytes = 500;

let old_da_gas_price = updater.new_scaled_da_gas_price;

// when
updater
.update_da_record_data(
&recorded_range,
recorded_range,
recorded_bytes,
recorded_cost as u128,
&mut unrecorded_blocks,
Expand Down
12 changes: 7 additions & 5 deletions crates/services/gas_price_service/src/v1/da_source_service.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::v1::da_source_service::service::DaBlockCostsSource;
use std::time::Duration;
use std::{
ops::RangeInclusive,
time::Duration,
};

pub mod block_committer_costs;
#[cfg(test)]
pub mod dummy_costs;
pub mod service;

#[derive(Debug, Default, Clone, Eq, Hash, PartialEq)]
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct DaBlockCosts {
pub bundle_id: u32,
// TODO: Should this be a range?
pub l2_blocks: Vec<u32>,
pub l2_blocks: RangeInclusive<u32>,
pub bundle_size_bytes: u32,
pub blob_cost_wei: u128,
}
Expand All @@ -34,7 +36,7 @@ mod tests {
// given
let expected_da_cost = DaBlockCosts {
bundle_id: 1,
l2_blocks: (0..10).collect(),
l2_blocks: 0..=9,
bundle_size_bytes: 1024 * 128,
blob_cost_wei: 2,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl From<&RawDaBlockCosts> for DaBlockCosts {
DaBlockCosts {
bundle_id,
// construct a vec of l2 blocks from the start_height to the end_height
l2_blocks: (start_height..=end_height).collect(),
l2_blocks: start_height..=end_height,
bundle_size_bytes: size_bytes,
blob_cost_wei: cost_wei,
}
Expand Down Expand Up @@ -121,7 +121,7 @@ impl From<RawDaBlockCosts> for DaBlockCosts {
fn from(value: RawDaBlockCosts) -> Self {
Self {
bundle_id: value.id,
l2_blocks: (value.start_height..=value.end_height).collect(),
l2_blocks: value.start_height..=value.end_height,
bundle_size_bytes: value.size,
blob_cost_wei: value.cost,
}
Expand Down Expand Up @@ -525,7 +525,7 @@ mod tests {
}

#[tokio::test]
async fn request_da_block_cost__when_last_value_is_some__then_get_costs_by_seqno_is_called(
async fn request_da_block_cost__when_last_value_is_some__then_get_costs_by_l2_block_number_is_called(
) {
// given
let mut da_block_costs = test_da_block_costs();
Expand All @@ -539,9 +539,8 @@ mod tests {
let actual = block_committer.request_da_block_costs().await.unwrap();

// then
assert_ne!(
da_block_costs_len as usize,
actual.first().unwrap().l2_blocks.len()
);
let l2_blocks = actual.first().unwrap().l2_blocks.clone();
let range_len = l2_blocks.end() - l2_blocks.start();
assert_ne!(da_block_costs_len, range_len);
}
}
Loading

0 comments on commit fc107cc

Please sign in to comment.