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

fix(gas_price_service_v1): block committer api format #2506

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ae2b760
fix(gas_price_service_v1): block committer api format
rymnc Dec 17, 2024
f01a94c
Merge branch 'chore/add-tests-for-v1-gas-service' into fix/block-comm…
MitchTurner Dec 17, 2024
1ed6d98
Move to latest recorded block height concept instead of bundle id
MitchTurner Dec 17, 2024
5175631
Update the mock server, move to use in UTs, write some UTs
MitchTurner Dec 17, 2024
a701116
Fix spelling
MitchTurner Dec 17, 2024
13880ed
Fix integ test
MitchTurner Dec 17, 2024
05574d5
Remove tracing subscriber
MitchTurner Dec 17, 2024
519533d
Lint toml file
MitchTurner Dec 17, 2024
6956ca3
Update crates/services/gas_price_service/src/common/fuel_core_storage…
MitchTurner Dec 17, 2024
a9e5067
Update crates/services/gas_price_service/src/v1/da_source_service/blo…
MitchTurner Dec 17, 2024
a19b0b7
Update crates/services/gas_price_service/src/v1/da_source_service/blo…
MitchTurner Dec 17, 2024
295e5e9
Update crates/services/gas_price_service/src/v1/da_source_service/ser…
MitchTurner Dec 17, 2024
2c4cb2c
Update crates/services/gas_price_service/src/v1/da_source_service/blo…
MitchTurner Dec 17, 2024
5744f3d
Update crates/services/gas_price_service/src/v1/da_source_service/blo…
MitchTurner Dec 17, 2024
24637d7
Appease Clippy-sama
MitchTurner Dec 17, 2024
c215b04
Fix import signature
MitchTurner Dec 18, 2024
7a8e1b4
lint toml
MitchTurner Dec 18, 2024
4655de6
Change latest recorded block to only have one key, add some logs
MitchTurner Dec 18, 2024
eef1e9a
Add more logs
MitchTurner Dec 18, 2024
eb29a3a
Remove test that has different behavior now
MitchTurner Dec 18, 2024
fc107cc
Use range for v1 algorithm commit blocks
MitchTurner Dec 18, 2024
38eebde
Nitpick `From` impls and add comment
MitchTurner Dec 18, 2024
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
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
7 changes: 7 additions & 0 deletions crates/services/gas_price_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ fuel-core-storage = { workspace = true, features = ["std"] }
fuel-core-types = { workspace = true, features = ["std"] }
fuel-gas-price-algorithm = { workspace = true }
futures = { workspace = true }
mockito = { version = "1.6.1", optional = true }
num_enum = { workspace = true }
parking_lot = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
serde = { workspace = true }
serde_json = { workspace = true, optional = true }
strum = { workspace = true, features = ["derive"] }
strum_macros = { workspace = true }
thiserror = { workspace = true }
Expand All @@ -32,4 +34,9 @@ tracing = { workspace = true }
fuel-core-services = { workspace = true, features = ["test-helpers"] }
fuel-core-storage = { workspace = true, features = ["test-helpers"] }
fuel-core-types = { path = "./../../types", features = ["test-helpers"] }
mockito = { version = "1.6.1" }
serde_json = { workspace = true }
tracing-subscriber = { workspace = true }

[features]
test-helpers = ["dep:mockito", "dep:serde_json"]
Loading
Loading