Skip to content
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions bin/fuel-core/src/cli/run/gas_price.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Args;
#[cfg(feature = "production")]
use fuel_core::service::sub_services::DEFAULT_GAS_PRICE_CHANGE_PERCENT;
use fuel_core_types::clamped_percentage::ClampedPercentage;
use url::Url;

#[derive(Debug, Clone, Args)]
Expand All @@ -19,21 +20,21 @@ pub struct GasPriceArgs {
/// The percentage change in gas price per block
#[cfg_attr(
feature = "production",
arg(long = "gas-price-change-percent", default_value_t = DEFAULT_GAS_PRICE_CHANGE_PERCENT, env)
arg(long = "gas-price-change-percent", default_value_t = ClampedPercentage::new(DEFAULT_GAS_PRICE_CHANGE_PERCENT.try_into().unwrap()), env)
)]
#[cfg_attr(
not(feature = "production"),
arg(long = "gas-price-change-percent", default_value = "0", env)
)]
pub gas_price_change_percent: u16,
pub gas_price_change_percent: ClampedPercentage,

/// The minimum allowed gas price
#[arg(long = "min-gas-price", default_value = "0", env)]
pub min_gas_price: u64,

/// The percentage threshold for gas price increase
#[arg(long = "gas-price-threshold-percent", default_value = "50", env)]
pub gas_price_threshold_percent: u8,
pub gas_price_threshold_percent: ClampedPercentage,

/// Minimum DA gas price
#[cfg_attr(
Expand Down
3 changes: 2 additions & 1 deletion bin/fuel-core/src/cli/run/tx_pool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Clap configuration related to TxPool service.

use fuel_core_types::{
clamped_percentage::ClampedPercentage,
fuel_tx::{
Address,
ContractId,
Expand Down Expand Up @@ -88,7 +89,7 @@ pub struct TxPoolArgs {

/// The max percentage of the `TxPool` that can be used by the `PendingPool`.
#[clap(long = "tx-pending-pool-size-percentage", default_value = "50", env)]
pub tx_pending_pool_size_percentage: u16,
pub tx_pending_pool_size_percentage: ClampedPercentage,
}

#[cfg(test)]
Expand Down
30 changes: 20 additions & 10 deletions crates/fuel-core/src/service/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use fuel_core_types::{
Consensus,
},
},
clamped_percentage::ClampedPercentage,
fuel_tx::{
Bytes32,
Transaction,
Expand Down Expand Up @@ -119,6 +120,7 @@ impl StaticGasPrice {
mod universal_gas_price_provider_tests {
#![allow(non_snake_case)]

use fuel_core_types::clamped_percentage::ClampedPercentage;
use proptest::proptest;

use super::*;
Expand All @@ -127,7 +129,7 @@ mod universal_gas_price_provider_tests {
gas_price: u64,
starting_height: u32,
block_horizon: u32,
percentage: u16,
percentage: ClampedPercentage,
) {
// given
let subject =
Expand All @@ -141,8 +143,9 @@ mod universal_gas_price_provider_tests {
let mut actual = gas_price;

for _ in 0..block_horizon {
let change_amount =
actual.saturating_mul(percentage as u64).saturating_div(100);
let change_amount = actual
.saturating_mul(*percentage as u64)
.saturating_div(100);
actual = actual.saturating_add(change_amount);
}

Expand All @@ -157,6 +160,8 @@ mod universal_gas_price_provider_tests {
block_horizon in 0..10_000u32,
percentage: u16,
) {
#[allow(clippy::cast_possible_truncation)]
let percentage = ClampedPercentage::new(percentage as u8);
_worst_case__correctly_calculates_value(
gas_price,
starting_height,
Expand All @@ -174,6 +179,9 @@ mod universal_gas_price_provider_tests {
block_horizon in 0..10_000u32,
percentage: u16
) {
// Convert u16 to ClampedPercentage
#[allow(clippy::cast_possible_truncation)]
let percentage = ClampedPercentage::new(percentage as u8);
// given
let subject = UniversalGasPriceProvider::new(starting_height, gas_price, percentage);

Expand All @@ -190,7 +198,7 @@ mod universal_gas_price_provider_tests {
fn _next_gas_price__correctly_calculates_value(
gas_price: u64,
starting_height: u32,
percentage: u16,
percentage: ClampedPercentage,
) {
// given
let subject =
Expand All @@ -201,7 +209,7 @@ mod universal_gas_price_provider_tests {

// then
let change_amount = gas_price
.saturating_mul(percentage as u64)
.saturating_mul(*percentage as u64)
.saturating_div(100);
let actual = gas_price.saturating_add(change_amount);

Expand All @@ -215,6 +223,8 @@ mod universal_gas_price_provider_tests {
starting_height: u32,
percentage: u16,
) {
#[allow(clippy::cast_possible_truncation)]
let percentage = ClampedPercentage::new(percentage as u8);
_next_gas_price__correctly_calculates_value(
gas_price,
starting_height,
Expand All @@ -232,7 +242,7 @@ pub struct UniversalGasPriceProvider<Height, GasPrice> {
/// Shared state of latest gas price data
latest_gas_price: LatestGasPrice<Height, GasPrice>,
/// The max percentage the gas price can increase per block
percentage: u16,
percentage: ClampedPercentage,
}

impl<Height, GasPrice> Clone for UniversalGasPriceProvider<Height, GasPrice> {
Expand All @@ -246,7 +256,7 @@ impl<Height, GasPrice> Clone for UniversalGasPriceProvider<Height, GasPrice> {

impl<Height, GasPrice> UniversalGasPriceProvider<Height, GasPrice> {
#[cfg(test)]
pub fn new(height: Height, price: GasPrice, percentage: u16) -> Self {
pub fn new(height: Height, price: GasPrice, percentage: ClampedPercentage) -> Self {
let latest_gas_price = LatestGasPrice::new(height, price);
Self {
latest_gas_price,
Expand All @@ -256,7 +266,7 @@ impl<Height, GasPrice> UniversalGasPriceProvider<Height, GasPrice> {

pub fn new_from_inner(
inner: LatestGasPrice<Height, GasPrice>,
percentage: u16,
percentage: ClampedPercentage,
) -> Self {
Self {
latest_gas_price: inner,
Expand All @@ -277,7 +287,7 @@ impl UniversalGasPriceProvider<u32, u64> {
let percentage = self.percentage;

let change = latest_price
.saturating_mul(percentage as u64)
.saturating_mul(*percentage as u64)
.saturating_div(100);

latest_price.saturating_add(change)
Expand All @@ -298,7 +308,7 @@ impl GasPriceEstimate for UniversalGasPriceProvider<u32, u64> {
let worst = cumulative_percentage_change(
best_gas_price,
best_height,
percentage as u64,
*percentage as u64,
height.into(),
);
Some(worst)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(non_snake_case)]

use super::*;
use fuel_core_types::clamped_percentage::ClampedPercentage;

#[cfg(test)]
mod producer_gas_price_tests;
Expand All @@ -9,7 +10,7 @@ fn build_provider<A>(
algorithm: A,
height: u32,
price: u64,
percentage: u16,
percentage: ClampedPercentage,
) -> FuelGasPriceProvider<A, u32, u64>
where
A: Send + Sync,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use fuel_core_gas_price_service::{
static_updater::StaticAlgorithm,
};
use fuel_core_producer::block_producer::gas_price::GasPriceProvider;
use fuel_core_types::clamped_percentage::ClampedPercentage;

#[test]
fn production_gas_price__if_requested_block_height_is_latest_return_gas_price() {
// given
let price = 33;
let algo = StaticAlgorithm::new(price);
let gas_price_provider = build_provider(algo.clone(), 0, price, 10);
let gas_price_provider =
build_provider(algo.clone(), 0, price, ClampedPercentage::new(10));

// when
let expected_price = algo.next_gas_price();
Expand All @@ -25,15 +27,15 @@ fn dry_run_gas_price__calculates_correctly_based_on_percentage() {
// given
let height = 123;
let price = 33;
let percentage = 10;
let percentage = ClampedPercentage::new(10);
let algo = StaticAlgorithm::new(price);
let gas_price_provider = build_provider(algo.clone(), height, price, percentage);

// when
let actual = gas_price_provider.dry_run_gas_price().unwrap();

// then
let change_amount = price.saturating_mul(percentage as u64).saturating_div(100);
let change_amount = price.saturating_mul(*percentage as u64).saturating_div(100);
let expected = price + change_amount;
assert_eq!(expected, actual);
}
6 changes: 3 additions & 3 deletions crates/fuel-core/src/service/adapters/gas_price_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ impl From<Config> for V1AlgorithmConfig {
V1AlgorithmConfig {
new_exec_gas_price: starting_exec_gas_price.max(min_exec_gas_price),
min_exec_gas_price,
exec_gas_price_change_percent,
l2_block_fullness_threshold_percent: exec_gas_price_threshold_percent,
exec_gas_price_change_percent: *exec_gas_price_change_percent as u16,
l2_block_fullness_threshold_percent: *exec_gas_price_threshold_percent,
min_da_gas_price,
max_da_gas_price,
max_da_gas_price_change_percent,
max_da_gas_price_change_percent: *max_da_gas_price_change_percent as u16,
da_p_component: da_gas_price_p_component,
da_d_component: da_gas_price_d_component,
normal_range_size: activity_normal_range_size,
Expand Down
17 changes: 11 additions & 6 deletions crates/fuel-core/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use fuel_core_tx_status_manager::config::Config as TxStatusManagerConfig;
use fuel_core_txpool::config::Config as TxPoolConfig;
use fuel_core_types::{
blockchain::header::StateTransitionBytecodeVersion,
clamped_percentage::ClampedPercentage,
signer::SignMode,
};

Expand Down Expand Up @@ -305,16 +306,16 @@ pub enum DbType {
#[derive(Clone, Debug)]
pub struct GasPriceConfig {
pub starting_exec_gas_price: u64,
pub exec_gas_price_change_percent: u16,
pub exec_gas_price_change_percent: ClampedPercentage,
pub min_exec_gas_price: u64,
pub exec_gas_price_threshold_percent: u8,
pub exec_gas_price_threshold_percent: ClampedPercentage,
pub da_committer_url: Option<url::Url>,
pub da_poll_interval: Option<Duration>,
pub da_gas_price_factor: NonZeroU64,
pub starting_recorded_height: Option<u32>,
pub min_da_gas_price: u64,
pub max_da_gas_price: u64,
pub max_da_gas_price_change_percent: u16,
pub max_da_gas_price_change_percent: ClampedPercentage,
pub da_gas_price_p_component: i64,
pub da_gas_price_d_component: i64,
pub gas_price_metrics: bool,
Expand All @@ -335,14 +336,18 @@ impl GasPriceConfig {

GasPriceConfig {
starting_exec_gas_price: starting_gas_price,
exec_gas_price_change_percent: gas_price_change_percent,
exec_gas_price_change_percent: ClampedPercentage::new(
gas_price_change_percent,
),
min_exec_gas_price: min_gas_price,
exec_gas_price_threshold_percent: gas_price_threshold_percent,
exec_gas_price_threshold_percent: ClampedPercentage::new(
gas_price_threshold_percent,
),
da_gas_price_factor: NonZeroU64::new(100).expect("100 is not zero"),
starting_recorded_height: None,
min_da_gas_price: 0,
max_da_gas_price: 1,
max_da_gas_price_change_percent: 0,
max_da_gas_price_change_percent: ClampedPercentage::new(0),
da_gas_price_p_component: 0,
da_gas_price_d_component: 0,
gas_price_metrics,
Expand Down
14 changes: 9 additions & 5 deletions crates/fuel-core/src/service/sub_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use fuel_core_storage::{
};
#[cfg(feature = "relayer")]
use fuel_core_types::blockchain::primitives::DaBlockHeight;
use fuel_core_types::signer::SignMode;
use fuel_core_types::{
clamped_percentage::ClampedPercentage,
signer::SignMode,
};

use fuel_core_compression_service::service::new_service as new_compression_service;

Expand Down Expand Up @@ -262,10 +265,11 @@ pub fn init_sub_services(
database.on_chain().clone(),
)?;
let (gas_price_algo, latest_gas_price) = gas_price_service_v1.shared.clone();
let universal_gas_price_provider = UniversalGasPriceProvider::new_from_inner(
latest_gas_price,
DEFAULT_GAS_PRICE_CHANGE_PERCENT,
);
#[allow(clippy::cast_possible_truncation)]
let clamped_percentage =
ClampedPercentage::new(DEFAULT_GAS_PRICE_CHANGE_PERCENT as u8);
let universal_gas_price_provider =
UniversalGasPriceProvider::new_from_inner(latest_gas_price, clamped_percentage);

let producer_gas_price_provider = FuelGasPriceProvider::new(
gas_price_algo.clone(),
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-gas-price-algorithm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ name = "fuel_gas_price_algorithm"
path = "src/lib.rs"

[dependencies]
fuel-core-types = { path = "../types", features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }
tracing = { workspace = true }
Expand Down
Loading
Loading