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

Remove the normalize_rewards_and_costs() function #2293

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 0 additions & 35 deletions crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ impl AlgorithmUpdaterV1 {
if !height_range.is_empty() {
self.da_block_update(height_range, range_cost)?;
self.recalculate_projected_cost();
// TODO: https://github.com/FuelLabs/fuel-core/issues/2264
// self.normalize_rewards_and_costs();
}
Ok(())
}
Expand Down Expand Up @@ -445,37 +443,4 @@ impl AlgorithmUpdaterV1 {
for_height: self.l2_block_height,
}
}

// TODO: This breaks our simulation now that we are using an extended finalization period. We
// can either keep this function and fix the simulation, or we might decide to remove it.
// https://github.com/FuelLabs/fuel-core/issues/2264
// We only need to track the difference between the rewards and costs after we have true DA data
// Normalize, or zero out the lower value and subtract it from the higher value
fn _normalize_rewards_and_costs(&mut self) {
let (excess, projected_cost_excess) =
if self.total_da_rewards_excess > self.latest_known_total_da_cost_excess {
(
self.total_da_rewards_excess
.saturating_sub(self.latest_known_total_da_cost_excess),
self.projected_total_da_cost
.saturating_sub(self.latest_known_total_da_cost_excess),
)
} else {
(
self.latest_known_total_da_cost_excess
.saturating_sub(self.total_da_rewards_excess),
self.projected_total_da_cost
.saturating_sub(self.total_da_rewards_excess),
)
};

self.projected_total_da_cost = projected_cost_excess;
if self.total_da_rewards_excess > self.latest_known_total_da_cost_excess {
self.total_da_rewards_excess = excess;
self.latest_known_total_da_cost_excess = 0;
} else {
self.total_da_rewards_excess = 0;
self.latest_known_total_da_cost_excess = excess;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ use crate::v1::{
},
Error,
};
use proptest::{
prelude::Rng,
prop_compose,
proptest,
};
use rand::SeedableRng;
use std::ops::Range;

#[test]
fn update_da_record_data__increases_block() {
Expand Down Expand Up @@ -277,162 +270,3 @@ fn update_da_record_data__da_block_updates_projected_total_cost_with_known_and_g
let expected = new_known_total_cost + guessed_part;
assert_eq!(actual, expected as u128);
}

#[derive(Debug, Clone)]
pub struct RecordedBlock {
pub height: u32,
pub block_cost: u64,
}

prop_compose! {
fn arb_vec_of_da_blocks()(last_da_block: u32, count in 1..123usize, rng_seed: u64) -> (Vec<BlockBytes>, (Range<u32>, u128)) {
let rng = &mut rand::rngs::StdRng::seed_from_u64(rng_seed);
let mut unrecorded_blocks = Vec::with_capacity(count);
let mut recorded_blocks = Vec::with_capacity(count);
for i in 0..count {
let height = last_da_block + 1 + i as u32;
let block_bytes = rng.gen_range(100..131_072);
let cost_per_byte = rng.gen_range(1..1000000);
let block_cost = block_bytes * cost_per_byte;
unrecorded_blocks.push(BlockBytes {
height,
block_bytes,
});
recorded_blocks.push(RecordedBlock {
height,
block_cost,
});
}
let recorded_heights = recorded_blocks
.iter()
.map(|block| block.height)
.collect::<Vec<u32>>();
let min = recorded_heights.iter().min().unwrap();
let max = recorded_heights.iter().max().unwrap();
let recorded_range = *min..(max + 1);
let recorded_cost = recorded_blocks
.iter()
.map(|block| block.block_cost as u128)
.sum();

(unrecorded_blocks, (recorded_range, recorded_cost))
}
}

prop_compose! {
fn reward_greater_than_cost_with_da_blocks()(cost: u64, extra: u64, (unrecorded_blocks, (recorded_range, recorded_cost)) in arb_vec_of_da_blocks()) -> (u128, u128, Vec<BlockBytes>, (Range<u32>, u128)) {
let reward = cost as u128 + recorded_cost + extra as u128;
(cost as u128, reward, unrecorded_blocks, (recorded_range, recorded_cost))
}
}

proptest! {
// https://github.com/FuelLabs/fuel-core/issues/2264
#[ignore]
#[test]
fn update_da_record_data__when_reward_is_greater_than_cost_will_zero_cost_and_subtract_from_reward(
(cost, reward, unrecorded_blocks, (recorded_range, recorded_cost)) in reward_greater_than_cost_with_da_blocks()
) {
_update_da_record_data__when_reward_is_greater_than_cost_will_zero_cost_and_subtract_from_reward(
cost,
reward,
unrecorded_blocks,
recorded_range,
recorded_cost
)
}
}

fn _update_da_record_data__when_reward_is_greater_than_cost_will_zero_cost_and_subtract_from_reward(
known_total_cost: u128,
total_rewards: u128,
unrecorded_blocks: Vec<BlockBytes>,
recorded_range: Range<u32>,
recorded_cost: u128,
) {
// given
let da_cost_per_byte = 20;
let da_recorded_block_height = recorded_range.start - 1;
let l2_block_height = 15;
let mut updater = UpdaterBuilder::new()
.with_da_cost_per_byte(da_cost_per_byte)
.with_da_recorded_block_height(da_recorded_block_height)
.with_l2_block_height(l2_block_height)
.with_known_total_cost(known_total_cost)
.with_total_rewards(total_rewards)
.with_unrecorded_blocks(unrecorded_blocks)
.build();

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

// then
let expected = total_rewards - recorded_cost - known_total_cost;
let actual = updater.total_da_rewards_excess;
assert_eq!(actual, expected);

let expected = 0;
let actual = updater.latest_known_total_da_cost_excess;
assert_eq!(actual, expected);
}

prop_compose! {
fn cost_greater_than_reward_with_da_blocks()(reward: u64, extra: u64, (unrecorded_blocks, (recorded_range, recorded_cost)) in arb_vec_of_da_blocks()) -> (u128, u128, Vec<BlockBytes>, (Range<u32>, u128)) {
let cost = reward as u128 + recorded_cost + extra as u128;
(cost, reward as u128, unrecorded_blocks, (recorded_range, recorded_cost))
}
}

proptest! {
// https://github.com/FuelLabs/fuel-core/issues/2264
#[ignore]
#[test]
fn update_da_record_data__when_cost_is_greater_than_reward_will_zero_reward_and_subtract_from_cost(
(cost, reward, unrecorded_blocks, (recorded_range, recorded_cost)) in cost_greater_than_reward_with_da_blocks()
) {
_update_da_record_data__when_cost_is_greater_than_reward_will_zero_reward_and_subtract_from_cost(
cost,
reward,
unrecorded_blocks,
recorded_range,
recorded_cost
)
}
}

fn _update_da_record_data__when_cost_is_greater_than_reward_will_zero_reward_and_subtract_from_cost(
known_total_cost: u128,
total_rewards: u128,
unrecorded_blocks: Vec<BlockBytes>,
recorded_range: Range<u32>,
recorded_cost: u128,
) {
// given
let da_cost_per_byte = 20;
let da_recorded_block_height = recorded_range.start - 1;
let l2_block_height = 15;
let mut updater = UpdaterBuilder::new()
.with_da_cost_per_byte(da_cost_per_byte)
.with_da_recorded_block_height(da_recorded_block_height)
.with_l2_block_height(l2_block_height)
.with_known_total_cost(known_total_cost)
.with_total_rewards(total_rewards)
.with_unrecorded_blocks(unrecorded_blocks)
.build();

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

// then
let expected = 0;
let actual = updater.total_da_rewards_excess;
assert_eq!(actual, expected);

let expected = known_total_cost + recorded_cost - total_rewards;
let actual = updater.latest_known_total_da_cost_excess;
assert_eq!(actual, expected);
}
Loading