Skip to content

Commit

Permalink
Updates to price algorithm (#2292)
Browse files Browse the repository at this point in the history
## Description
This PR changes a couple of minor things w.r.t to price algorithm.
1. It changes the order of operations in `recalculate_projected_cost()`
so it uses a single multiplication
2. Replaces `sum()` with `fold()`+`saturating_add()`
3. Minor renames for clarity

### Before requesting review
- [X] I have reviewed the code myself
  • Loading branch information
rafal-ch authored Oct 7, 2024
1 parent 8c92d37 commit 761ddb4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl Simulator {
let bytes_and_costs: Vec<_> = bytes
.iter()
.zip(self.da_cost_per_byte.iter())
.map(|(bytes, cost_per_byte)| (*bytes, (*bytes * cost_per_byte) as u64))
.map(|(bytes, da_cost_per_byte)| (*bytes, (*bytes * da_cost_per_byte) as u64))
.collect();

let actual_profit: Vec<i128> = actual_costs
Expand Down
19 changes: 9 additions & 10 deletions crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ impl AlgorithmUpdaterV1 {
self.l2_block_height = height;

// rewards
self.update_rewards(fee_wei);
self.update_da_rewards(fee_wei);
let rewards = self.clamped_rewards_as_i128();

// costs
self.update_projected_cost(block_bytes);
self.update_projected_da_cost(block_bytes);
let projected_total_da_cost = self.clamped_projected_cost_as_i128();

// profit
Expand All @@ -238,13 +238,13 @@ impl AlgorithmUpdaterV1 {
}
}

fn update_rewards(&mut self, fee_wei: u128) {
fn update_da_rewards(&mut self, fee_wei: u128) {
let block_da_reward = self.da_portion_of_fee(fee_wei);
self.total_da_rewards_excess =
self.total_da_rewards_excess.saturating_add(block_da_reward);
}

fn update_projected_cost(&mut self, block_bytes: u64) {
fn update_projected_da_cost(&mut self, block_bytes: u64) {
let block_projected_da_cost =
(block_bytes as u128).saturating_mul(self.latest_da_cost_per_byte);
self.projected_total_da_cost = self
Expand Down Expand Up @@ -388,10 +388,10 @@ impl AlgorithmUpdaterV1 {
},
)?;
self.da_recorded_block_height = last;
let new_block_cost = self
let new_da_block_cost = self
.latest_known_total_da_cost_excess
.saturating_add(range_cost);
self.latest_known_total_da_cost_excess = new_block_cost;
self.latest_known_total_da_cost_excess = new_da_block_cost;
self.latest_da_cost_per_byte = new_cost_per_byte;
Ok(())
}
Expand Down Expand Up @@ -420,10 +420,9 @@ impl AlgorithmUpdaterV1 {
let projection_portion: u128 = self
.unrecorded_blocks
.iter()
.map(|(_, &bytes)| {
(bytes as u128).saturating_mul(self.latest_da_cost_per_byte)
})
.sum();
.map(|(_, &bytes)| (bytes as u128))
.fold(0_u128, |acc, n| acc.saturating_add(n))
.saturating_mul(self.latest_da_cost_per_byte);
self.projected_total_da_cost = self
.latest_known_total_da_cost_excess
.saturating_add(projection_portion);
Expand Down

0 comments on commit 761ddb4

Please sign in to comment.