Skip to content

Commit

Permalink
Restructure the algo methods to accomodate storage txs
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Dec 3, 2024
1 parent 3679422 commit 6af9589
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 238 deletions.
27 changes: 20 additions & 7 deletions crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::utils::cumulative_percentage_change;
use std::{
cmp::max,
collections::BTreeMap,
marker::PhantomData,
num::NonZeroU64,
ops::Div,
};
Expand Down Expand Up @@ -164,10 +165,10 @@ pub struct AlgorithmUpdaterV1<U> {
pub latest_da_cost_per_byte: u128,
/// Activity of L2
pub l2_activity: L2ActivityTracker,
/// The unrecorded blocks that are used to calculate the projected cost of recording blocks
pub unrecorded_blocks: U,
/// Total unrecorded block bytes
pub unrecorded_blocks_bytes: u128,
/// The unrecorded blocks that are used to calculate the projected cost of recording blocks
pub _unrecorded_blocks: PhantomData<U>,
}

/// The `L2ActivityTracker` tracks the chain activity to determine a safety mode for setting the DA price.
Expand Down Expand Up @@ -326,9 +327,15 @@ impl<U: UnrecordedBlocks> AlgorithmUpdaterV1<U> {
heights: &[u32],
recorded_bytes: u32,
recording_cost: u128,
unrecorded_blocks: &mut U,
) -> Result<(), Error> {
if !heights.is_empty() {
self.da_block_update(heights, recorded_bytes as u128, recording_cost)?;
self.da_block_update(
heights,
recorded_bytes as u128,
recording_cost,
unrecorded_blocks,
)?;
self.recalculate_projected_cost();
self.update_da_gas_price();
}
Expand All @@ -342,6 +349,7 @@ impl<U: UnrecordedBlocks> AlgorithmUpdaterV1<U> {
capacity: NonZeroU64,
block_bytes: u64,
fee_wei: u128,
unrecorded_blocks: &mut U,
) -> Result<(), Error> {
let expected = self.l2_block_height.saturating_add(1);
if height != expected {
Expand Down Expand Up @@ -372,7 +380,7 @@ impl<U: UnrecordedBlocks> AlgorithmUpdaterV1<U> {
self.update_da_gas_price();

// metadata
self.unrecorded_blocks.insert(height, block_bytes)?;
unrecorded_blocks.insert(height, block_bytes)?;
self.unrecorded_blocks_bytes = self
.unrecorded_blocks_bytes
.saturating_add(block_bytes as u128);
Expand Down Expand Up @@ -538,8 +546,9 @@ impl<U: UnrecordedBlocks> AlgorithmUpdaterV1<U> {
heights: &[u32],
recorded_bytes: u128,
recording_cost: u128,
unrecorded_blocks: &mut U,
) -> Result<(), Error> {
self.update_unrecorded_block_bytes(heights);
self.update_unrecorded_block_bytes(heights, unrecorded_blocks);

let new_da_block_cost = self
.latest_known_total_da_cost_excess
Expand All @@ -561,10 +570,14 @@ impl<U: UnrecordedBlocks> AlgorithmUpdaterV1<U> {

// Get the bytes for all specified heights, or get none of them.
// Always remove the blocks from the unrecorded blocks so they don't build up indefinitely
fn update_unrecorded_block_bytes(&mut self, heights: &[u32]) {
fn update_unrecorded_block_bytes(
&mut self,
heights: &[u32],
unrecorded_blocks: &mut U,
) {
let mut total: u128 = 0;
for expected_height in heights {
let res = self.unrecorded_blocks.remove(expected_height);
let res = unrecorded_blocks.remove(expected_height);
match res {
Ok(Some(bytes)) => {
total = total.saturating_add(bytes as u128);
Expand Down
25 changes: 12 additions & 13 deletions crates/fuel-gas-price-algorithm/src/v1/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct UpdaterBuilder {
da_cost_per_byte: u128,
project_total_cost: u128,
latest_known_total_cost: u128,
unrecorded_blocks: Vec<BlockBytes>,
unrecorded_blocks_bytes: u64,
last_profit: i128,
second_to_last_profit: i128,
da_gas_price_factor: u64,
Expand All @@ -68,7 +68,7 @@ impl UpdaterBuilder {
da_cost_per_byte: 0,
project_total_cost: 0,
latest_known_total_cost: 0,
unrecorded_blocks: vec![],
unrecorded_blocks_bytes: 0,
last_profit: 0,
second_to_last_profit: 0,
da_gas_price_factor: 1,
Expand Down Expand Up @@ -149,8 +149,14 @@ impl UpdaterBuilder {
self
}

fn with_unrecorded_blocks(mut self, unrecorded_blocks: Vec<BlockBytes>) -> Self {
self.unrecorded_blocks = unrecorded_blocks;
fn with_unrecorded_blocks(
mut self,
unrecorded_blocks: &BTreeMap<Height, Bytes>,
) -> Self {
let unrecorded_block_bytes = unrecorded_blocks
.iter()
.fold(0u64, |acc, (_, bytes)| acc + bytes);
self.unrecorded_blocks_bytes = unrecorded_block_bytes;
self
}

Expand Down Expand Up @@ -183,11 +189,6 @@ impl UpdaterBuilder {
latest_da_cost_per_byte: self.da_cost_per_byte,
projected_total_da_cost: self.project_total_cost,
latest_known_total_da_cost_excess: self.latest_known_total_cost,
unrecorded_blocks: self
.unrecorded_blocks
.iter()
.map(|b| (b.height, b.block_bytes))
.collect(),
last_profit: self.last_profit,
second_to_last_profit: self.second_to_last_profit,
min_da_gas_price: self.min_da_gas_price,
Expand All @@ -196,10 +197,8 @@ impl UpdaterBuilder {
.try_into()
.expect("Should never be non-zero"),
l2_activity: self.l2_activity,
unrecorded_blocks_bytes: self
.unrecorded_blocks
.iter()
.fold(0u128, |acc, b| acc + u128::from(b.block_bytes)),
unrecorded_blocks_bytes: self.unrecorded_blocks_bytes as u128,
_unrecorded_blocks: Default::default(),
}
}
}
Expand Down
Loading

0 comments on commit 6af9589

Please sign in to comment.