Skip to content

Commit

Permalink
feat(gas_price_service_v1): include max_da_gas_price to control vol…
Browse files Browse the repository at this point in the history
…atility
  • Loading branch information
rymnc committed Jan 8, 2025
1 parent 31fd51f commit 0108782
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 4 deletions.
7 changes: 7 additions & 0 deletions bin/fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ pub struct Command {
#[arg(long = "min-da-gas-price", default_value = "10000000", env)]
pub min_da_gas_price: u64,

/// Maximum DA gas price
/// DEV: ensure that the max_da_gas_price default is > then the min_da_gas_price default
#[arg(long = "max-da-gas-price", default_value = "10000001", env)]
pub max_da_gas_price: u64,

/// P component of DA gas price calculation
/// **NOTE**: This is the **inverse** gain of a typical P controller.
/// Increasing this value will reduce gas price fluctuations.
Expand Down Expand Up @@ -330,6 +335,7 @@ impl Command {
min_gas_price,
gas_price_threshold_percent,
min_da_gas_price,
max_da_gas_price,
da_p_component,
da_d_component,
max_da_gas_price_change_percent,
Expand Down Expand Up @@ -646,6 +652,7 @@ impl Command {
memory_pool_size,
da_gas_price_factor: NonZeroU64::new(100).expect("100 is not zero"),
min_da_gas_price,
max_da_gas_price,
max_da_gas_price_change_percent,
da_p_component,
da_d_component,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl From<Config> for GasPriceServiceConfig {
value.exec_gas_price_threshold_percent,
value.da_gas_price_factor,
value.min_da_gas_price,
value.max_da_gas_price,
value.max_da_gas_price_change_percent,
value.da_p_component,
value.da_d_component,
Expand Down
2 changes: 2 additions & 0 deletions crates/fuel-core/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct Config {
pub memory_pool_size: usize,
pub da_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 da_p_component: i64,
pub da_d_component: i64,
Expand Down Expand Up @@ -210,6 +211,7 @@ impl Config {
memory_pool_size: 4,
da_gas_price_factor: NonZeroU64::new(100).expect("100 is not zero"),
min_da_gas_price: 0,
max_da_gas_price: 1,
max_da_gas_price_change_percent: 0,
da_p_component: 0,
da_d_component: 0,
Expand Down
21 changes: 17 additions & 4 deletions crates/fuel-gas-price-algorithm/src/v1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::utils::cumulative_percentage_change;
use std::{
cmp::max,
cmp::{
max,
min,
},
collections::BTreeMap,
num::NonZeroU64,
ops::{
Expand Down Expand Up @@ -147,6 +150,8 @@ pub struct AlgorithmUpdaterV1 {
pub gas_price_factor: NonZeroU64,
/// The lowest the algorithm allows the da gas price to go
pub min_da_gas_price: u64,
/// The highest the algorithm allows the da gas price to go
pub max_da_gas_price: u64,
/// 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%
pub max_da_gas_price_change_percent: u16,
Expand Down Expand Up @@ -514,9 +519,12 @@ impl AlgorithmUpdaterV1 {
scaled_da_change,
maybe_new_scaled_da_gas_price
);
self.new_scaled_da_gas_price = max(
self.min_scaled_da_gas_price(),
maybe_new_scaled_da_gas_price,
self.new_scaled_da_gas_price = min(
max(
self.min_scaled_da_gas_price(),
maybe_new_scaled_da_gas_price,
),
self.max_scaled_da_gas_price(),
);
}

Expand All @@ -540,6 +548,11 @@ impl AlgorithmUpdaterV1 {
.saturating_mul(self.gas_price_factor.into())
}

fn max_scaled_da_gas_price(&self) -> u64 {
self.max_da_gas_price
.saturating_mul(self.gas_price_factor.into())
}

fn p(&self) -> i128 {
let upcast_p = i128::from(self.da_p_component);
let checked_p = self.last_profit.checked_div(upcast_p);
Expand Down
8 changes: 8 additions & 0 deletions crates/fuel-gas-price-algorithm/src/v1/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct BlockBytes {
pub struct UpdaterBuilder {
min_exec_gas_price: u64,
min_da_gas_price: u64,
max_da_gas_price: u64,
starting_exec_gas_price: u64,
starting_da_gas_price: u64,
exec_gas_price_change_percent: u16,
Expand Down Expand Up @@ -53,6 +54,7 @@ impl UpdaterBuilder {
Self {
min_exec_gas_price: 0,
min_da_gas_price: 0,
max_da_gas_price: 1,
starting_exec_gas_price: 1,
starting_da_gas_price: 1,
exec_gas_price_change_percent: 0,
Expand Down Expand Up @@ -86,6 +88,11 @@ impl UpdaterBuilder {
self
}

fn with_max_da_gas_price(mut self, max_price: u64) -> Self {
self.max_da_gas_price = max_price;
self
}

fn with_starting_exec_gas_price(mut self, starting_da_gas_price: u64) -> Self {
self.starting_exec_gas_price = starting_da_gas_price;
self
Expand Down Expand Up @@ -192,6 +199,7 @@ impl UpdaterBuilder {
last_profit: self.last_profit,
second_to_last_profit: self.second_to_last_profit,
min_da_gas_price: self.min_da_gas_price,
max_da_gas_price: self.max_da_gas_price,
gas_price_factor: self
.da_gas_price_factor
.try_into()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ fn update_l2_block_data__never_drops_below_minimum_da_gas_price() {
let starting_exec_gas_price = 0;
let last_da_gas_price = 100;
let min_da_gas_price = 100;
let max_da_gas_price = min_da_gas_price + 1;
let starting_cost = 0;
let latest_gas_per_byte = 0; // DA is free
let da_p_component = 100;
Expand All @@ -507,6 +508,7 @@ fn update_l2_block_data__never_drops_below_minimum_da_gas_price() {
.with_da_cost_per_byte(latest_gas_per_byte as u128)
.with_last_profit(last_profit, avg_window)
.with_min_da_gas_price(min_da_gas_price)
.with_max_da_gas_price(max_da_gas_price)
.build();

// when
Expand Down
2 changes: 2 additions & 0 deletions crates/services/gas_price_service/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl GasPriceServiceConfig {
l2_block_fullness_threshold_percent: u8,
gas_price_factor: NonZeroU64,
min_da_gas_price: u64,
max_da_gas_price: u64,
max_da_gas_price_change_percent: u16,
da_p_component: i64,
da_d_component: i64,
Expand All @@ -114,6 +115,7 @@ impl GasPriceServiceConfig {
l2_block_fullness_threshold_percent,
gas_price_factor,
min_da_gas_price,
max_da_gas_price,
max_da_gas_price_change_percent,
da_p_component,
da_d_component,
Expand Down
3 changes: 3 additions & 0 deletions crates/services/gas_price_service/src/v1/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct V1AlgorithmConfig {
// https://github.com/FuelLabs/fuel-core/issues/2481
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 da_p_component: i64,
pub da_d_component: i64,
Expand Down Expand Up @@ -108,6 +109,7 @@ pub fn updater_from_config(value: &V1AlgorithmConfig) -> AlgorithmUpdaterV1 {
.l2_block_fullness_threshold_percent
.into(),
min_da_gas_price: value.min_da_gas_price,
max_da_gas_price: value.max_da_gas_price,
max_da_gas_price_change_percent: value.max_da_gas_price_change_percent,
da_p_component: value.da_p_component,
da_d_component: value.da_d_component,
Expand Down Expand Up @@ -166,6 +168,7 @@ pub fn v1_algorithm_from_metadata(
.l2_block_fullness_threshold_percent
.into(),
min_da_gas_price: config.min_da_gas_price,
max_da_gas_price: config.max_da_gas_price,
max_da_gas_price_change_percent: config.max_da_gas_price_change_percent,
da_p_component: config.da_p_component,
da_d_component: config.da_d_component,
Expand Down
3 changes: 3 additions & 0 deletions crates/services/gas_price_service/src/v1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ mod tests {
l2_block_fullness_threshold_percent: 20,
gas_price_factor: NonZeroU64::new(10).unwrap(),
min_da_gas_price: 10,
max_da_gas_price: 11,
max_da_gas_price_change_percent: 20,
da_p_component: 4,
da_d_component: 2,
Expand Down Expand Up @@ -635,6 +636,7 @@ mod tests {
l2_block_fullness_threshold_percent: 20,
gas_price_factor: NonZeroU64::new(10).unwrap(),
min_da_gas_price: 0,
max_da_gas_price: 1,
max_da_gas_price_change_percent: 100,
da_p_component: 4,
da_d_component: 2,
Expand Down Expand Up @@ -714,6 +716,7 @@ mod tests {
l2_block_fullness_threshold_percent: 20,
gas_price_factor: NonZeroU64::new(10).unwrap(),
min_da_gas_price: 0,
max_da_gas_price: 1,
max_da_gas_price_change_percent: 100,
da_p_component: 4,
da_d_component: 2,
Expand Down
2 changes: 2 additions & 0 deletions crates/services/gas_price_service/src/v1/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ fn zero_threshold_arbitrary_config() -> V1AlgorithmConfig {
l2_block_fullness_threshold_percent: 0,
gas_price_factor: NonZeroU64::new(100).unwrap(),
min_da_gas_price: 0,
max_da_gas_price: 1,
max_da_gas_price_change_percent: 0,
da_p_component: 0,
da_d_component: 0,
Expand Down Expand Up @@ -327,6 +328,7 @@ fn different_arb_config() -> V1AlgorithmConfig {
l2_block_fullness_threshold_percent: 0,
gas_price_factor: NonZeroU64::new(100).unwrap(),
min_da_gas_price: 0,
max_da_gas_price: 1,
max_da_gas_price_change_percent: 0,
da_p_component: 0,
da_d_component: 0,
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ async fn produce_block__raises_gas_price() {
node_config.da_d_component = 0;
node_config.max_da_gas_price_change_percent = 0;
node_config.min_da_gas_price = 0;
node_config.max_da_gas_price = 1;

let srv = FuelService::new_node(node_config.clone()).await.unwrap();
let client = FuelClient::from(srv.bound_address);
Expand Down Expand Up @@ -257,6 +258,7 @@ async fn produce_block__lowers_gas_price() {
node_config.da_d_component = 0;
node_config.max_da_gas_price_change_percent = 0;
node_config.min_da_gas_price = 0;
node_config.max_da_gas_price = 1;

let srv = FuelService::new_node(node_config.clone()).await.unwrap();
let client = FuelClient::from(srv.bound_address);
Expand Down Expand Up @@ -705,6 +707,7 @@ fn node_config_with_da_committer_url(url: &str) -> Config {
let starting_gas_price = 10_000_000;
node_config.block_producer.coinbase_recipient = Some([5; 32].into());
node_config.min_da_gas_price = starting_gas_price;
node_config.max_da_gas_price = starting_gas_price + 1;
node_config.max_da_gas_price_change_percent = 15;
node_config.block_production = Trigger::Never;
node_config.da_committer_url = Some(url.to_string());
Expand Down

0 comments on commit 0108782

Please sign in to comment.