pub struct AlgorithmUpdaterV1 {Show 20 fields
pub new_scaled_exec_price: u64,
pub min_exec_gas_price: u64,
pub exec_gas_price_change_percent: u16,
pub l2_block_height: u32,
pub l2_block_fullness_threshold_percent: ClampedPercentage,
pub new_scaled_da_gas_price: u64,
pub gas_price_factor: NonZeroU64,
pub min_da_gas_price: u64,
pub max_da_gas_price: u64,
pub max_da_gas_price_change_percent: u16,
pub total_da_rewards: u128,
pub latest_known_total_da_cost: u128,
pub projected_total_da_cost: u128,
pub da_p_component: i64,
pub da_d_component: i64,
pub last_profit: i128,
pub second_to_last_profit: i128,
pub latest_da_cost_per_byte: u128,
pub l2_activity: L2ActivityTracker,
pub unrecorded_blocks_bytes: u128,
}
Expand description
The state of the algorithm used to update the gas price algorithm for each block
Because there will always be a delay between blocks submitted to the L2 chain and the blocks being recorded on the DA chain, the updater needs to make “projections” about the cost of recording any given block to the DA chain. This is done by tracking the cost per byte of recording for the most recent blocks, and using the known bytes of the unrecorded blocks to estimate the cost for that block. Every time the DA recording is updated, the projections are recalculated.
This projection will inevitably lead to error in the gas price calculation. Special care should be taken to account for the worst case scenario when calculating the parameters of the algorithm.
An algorithm for calculating the gas price for the next block
The algorithm breaks up the gas price into two components:
- The execution gas price, which is used to cover the cost of executing the next block as well as moderating the congestion of the network by increasing the price when traffic is high.
- The data availability (DA) gas price, which is used to cover the cost of recording the block on the DA chain
The execution gas price is calculated based on the fullness of the last received l2 block. Each block has a capacity threshold, and if the block is above this threshold, the gas price is increased. If it is below the threshold, the gas price is decreased. The gas price can only change by a fixed amount each block.
The DA gas price is calculated based on the profit of previous blocks. The profit is the difference between the rewards from the DA portion of the gas price and the cost of recording the blocks on the DA chain. The algorithm uses a naive PID controller to calculate the change in the DA gas price. The “P” portion of the new gas price is “proportional” to the profit, either negative or positive. The “D” portion is derived from the slope or change in the profits since the last block.
if p > 0 and dp/db > 0, decrease if p > 0 and dp/db < 0, hold/moderate if p < 0 and dp/db < 0, increase if p < 0 and dp/db > 0, hold/moderate
The DA portion also uses a moving average of the profits over the last avg_window
blocks
instead of the actual profit. Setting the avg_window
to 1 will effectively disable the
moving average.
Fields§
§new_scaled_exec_price: u64
The gas price (scaled by the gas_price_factor
) to cover the execution of the next block
min_exec_gas_price: u64
The lowest the algorithm allows the exec gas price to go
exec_gas_price_change_percent: u16
The Percentage the execution gas price will change in a single block, either increase or decrease
based on the fullness of the last L2 block. Using u16
because it can go above 100% and
possibly over 255%
l2_block_height: u32
The height of the next L2 block
l2_block_fullness_threshold_percent: ClampedPercentage
The threshold of gas usage above and below which the gas price will increase or decrease This is a percentage of the total capacity of the L2 block
new_scaled_da_gas_price: u64
The gas price (scaled by the gas_price_factor
) to cover the DA commitment of the next block
gas_price_factor: NonZeroU64
Scale factor for the gas price.
min_da_gas_price: u64
The lowest the algorithm allows the da gas price to go
max_da_gas_price: u64
The highest the algorithm allows the da gas price to go
max_da_gas_price_change_percent: u16
The maximum percentage that the DA portion of the gas price can change in a single block
Using u16
because it can go above 100% and possibly over 255%
total_da_rewards: u128
The cumulative reward from the DA portion of the gas price
latest_known_total_da_cost: u128
The cumulative cost of recording L2 blocks on the DA chain as of the last recorded block
projected_total_da_cost: u128
The predicted cost of recording L2 blocks on the DA chain as of the last L2 block
(This value is added on top of the latest_known_total_da_cost
if the L2 height is higher)
da_p_component: i64
The P component of the PID control for the DA gas price
da_d_component: i64
The D component of the PID control for the DA gas price
last_profit: i128
The last profit
second_to_last_profit: i128
The profit before last
latest_da_cost_per_byte: u128
The latest known cost per byte for recording blocks on the DA chain
l2_activity: L2ActivityTracker
Activity of L2
unrecorded_blocks_bytes: u128
Total unrecorded block bytes
Implementations§
Source§impl AlgorithmUpdaterV1
impl AlgorithmUpdaterV1
pub fn update_da_record_data<U: UnrecordedBlocks>( &mut self, heights: RangeInclusive<u32>, recorded_bytes: u32, recording_cost: u128, unrecorded_blocks: &mut U, ) -> Result<(), Error>
pub fn update_l2_block_data<U: UnrecordedBlocks>( &mut self, height: u32, used: u64, capacity: NonZeroU64, block_bytes: u64, fee_wei: u128, unrecorded_blocks: &mut U, ) -> Result<(), Error>
pub fn algorithm(&self) -> AlgorithmV1
Trait Implementations§
Source§impl Clone for AlgorithmUpdaterV1
impl Clone for AlgorithmUpdaterV1
Source§fn clone(&self) -> AlgorithmUpdaterV1
fn clone(&self) -> AlgorithmUpdaterV1
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more