diff --git a/crates/fuel-core/src/service/adapters/gas_price_adapters.rs b/crates/fuel-core/src/service/adapters/gas_price_adapters.rs index 014dc98456a..a521deb2571 100644 --- a/crates/fuel-core/src/service/adapters/gas_price_adapters.rs +++ b/crates/fuel-core/src/service/adapters/gas_price_adapters.rs @@ -17,6 +17,7 @@ use fuel_core_gas_price_service::{ GasPriceData, GasPriceServiceConfig, L2Data, + VersionSpecificConfig, }, }; use fuel_core_storage::{ @@ -64,12 +65,15 @@ impl GasPriceData for Database { impl From for GasPriceServiceConfig { fn from(value: Config) -> Self { - GasPriceServiceConfig::new( - value.min_gas_price, + // TODO: we need to alter fuel-core configuration to include versioned config, after which + // we can directly pass it into the constructor. + let version_specific_config = VersionSpecificConfig::new_v0( value.starting_gas_price, + value.min_gas_price, value.gas_price_change_percent, value.gas_price_threshold_percent, - ) + ); + GasPriceServiceConfig::new(version_specific_config) } } diff --git a/crates/services/gas_price_service/src/ports.rs b/crates/services/gas_price_service/src/ports.rs index cefa85f4e01..0fcc9dd3ca9 100644 --- a/crates/services/gas_price_service/src/ports.rs +++ b/crates/services/gas_price_service/src/ports.rs @@ -1,6 +1,10 @@ -use crate::common::{ - updater_metadata::UpdaterMetadata, - utils::Result, +use crate::{ + common::{ + updater_metadata::UpdaterMetadata, + utils::Result, + }, + v0::metadata::V0MetadataInitializer, + v1::metadata::V1MetadataInitializer, }; use fuel_core_storage::Result as StorageResult; use fuel_core_types::{ @@ -30,25 +34,65 @@ pub trait GasPriceData: Send + Sync { fn latest_height(&self) -> Option; } -pub struct GasPriceServiceConfig { - pub min_gas_price: u64, - pub starting_gas_price: u64, - pub gas_price_change_percent: u64, - pub gas_price_threshold_percent: u64, +pub enum VersionSpecificConfig { + V0(V0MetadataInitializer), + V1(V1MetadataInitializer), } -impl GasPriceServiceConfig { - pub fn new( - min_gas_price: u64, +impl VersionSpecificConfig { + pub fn new_v0( starting_gas_price: u64, + min_gas_price: u64, gas_price_change_percent: u64, gas_price_threshold_percent: u64, ) -> Self { - Self { - min_gas_price, + Self::V0(V0MetadataInitializer { starting_gas_price, + min_gas_price, gas_price_change_percent, gas_price_threshold_percent, + }) + } + + pub fn new_v1(metadata: V1MetadataInitializer) -> Self { + Self::V1(metadata) + } + + /// Extract V0MetadataInitializer if it is of V0 version + pub fn v0(self) -> Option { + if let VersionSpecificConfig::V0(v0) = self { + Some(v0) + } else { + None + } + } + + /// Extract V1MetadataInitializer if it is of V1 version + pub fn v1(self) -> Option { + if let VersionSpecificConfig::V1(v1) = self { + Some(v1) + } else { + None } } } + +pub struct GasPriceServiceConfig { + version_specific_config: VersionSpecificConfig, +} + +impl GasPriceServiceConfig { + pub fn new(version_specific_config: VersionSpecificConfig) -> Self { + Self { + version_specific_config, + } + } + + pub fn v0(self) -> Option { + self.version_specific_config.v0() + } + + pub fn v1(self) -> Option { + self.version_specific_config.v1() + } +} diff --git a/crates/services/gas_price_service/src/v0/metadata.rs b/crates/services/gas_price_service/src/v0/metadata.rs index 0d21b7b06e0..17cb1b20735 100644 --- a/crates/services/gas_price_service/src/v0/metadata.rs +++ b/crates/services/gas_price_service/src/v0/metadata.rs @@ -17,6 +17,25 @@ pub struct V0Metadata { pub l2_block_fullness_threshold_percent: u64, } +pub struct V0MetadataInitializer { + pub starting_gas_price: u64, + pub min_gas_price: u64, + pub gas_price_change_percent: u64, + pub gas_price_threshold_percent: u64, +} + +impl V0MetadataInitializer { + pub fn initialize(&self, l2_block_height: u32) -> V0Metadata { + V0Metadata { + new_exec_price: self.starting_gas_price.max(self.min_gas_price), + min_exec_gas_price: self.min_gas_price, + exec_gas_price_change_percent: self.gas_price_change_percent, + l2_block_height, + l2_block_fullness_threshold_percent: self.gas_price_threshold_percent, + } + } +} + impl From for AlgorithmUpdaterV0 { fn from(metadata: V0Metadata) -> Self { Self { diff --git a/crates/services/gas_price_service/src/v0/uninitialized_task.rs b/crates/services/gas_price_service/src/v0/uninitialized_task.rs index 4a7851ea2b4..61700f0ba97 100644 --- a/crates/services/gas_price_service/src/v0/uninitialized_task.rs +++ b/crates/services/gas_price_service/src/v0/uninitialized_task.rs @@ -22,7 +22,10 @@ use crate::{ MetadataStorage, }, v0::{ - metadata::V0Metadata, + metadata::{ + V0Metadata, + V0MetadataInitializer, + }, service::GasPriceServiceV0, }, }; @@ -52,7 +55,7 @@ pub use fuel_gas_price_algorithm::v0::AlgorithmV0; pub type SharedV0Algorithm = SharedGasPriceAlgo; pub struct UninitializedTask { - pub config: GasPriceServiceConfig, + pub config: V0MetadataInitializer, pub genesis_block_height: BlockHeight, pub settings: SettingsProvider, pub gas_price_db: GasPriceStore, @@ -64,16 +67,10 @@ pub struct UninitializedTask { } fn get_default_metadata( - config: &GasPriceServiceConfig, + config: &V0MetadataInitializer, latest_block_height: u32, ) -> V0Metadata { - V0Metadata { - new_exec_price: config.starting_gas_price.max(config.min_gas_price), - min_exec_gas_price: config.min_gas_price, - exec_gas_price_change_percent: config.gas_price_change_percent, - l2_block_height: latest_block_height, - l2_block_fullness_threshold_percent: config.gas_price_threshold_percent, - } + config.initialize(latest_block_height) } impl @@ -86,7 +83,7 @@ where SettingsProvider: GasPriceSettingsProvider, { pub fn new( - config: GasPriceServiceConfig, + config: V0MetadataInitializer, genesis_block_height: BlockHeight, settings: SettingsProvider, block_stream: BoxStream, @@ -359,8 +356,9 @@ where GasPriceData + Modifiable + KeyValueInspect + Clone, SettingsProvider: GasPriceSettingsProvider, { + let v0_config = config.v0().ok_or(anyhow::anyhow!("Expected V0 config"))?; let gas_price_init = UninitializedTask::new( - config, + v0_config, genesis_block_height, settings, block_stream, diff --git a/crates/services/gas_price_service/src/v1/metadata.rs b/crates/services/gas_price_service/src/v1/metadata.rs index 4f354aac256..c7c6eb1b87e 100644 --- a/crates/services/gas_price_service/src/v1/metadata.rs +++ b/crates/services/gas_price_service/src/v1/metadata.rs @@ -52,6 +52,55 @@ pub struct V1Metadata { pub latest_da_cost_per_byte: u128, } +pub struct V1MetadataInitializer { + new_exec_gas_price: u64, + min_exec_gas_price: u64, + exec_gas_price_change_percent: u16, + l2_block_fullness_threshold_percent: u8, + gas_price_factor: u64, + min_da_gas_price: u64, + max_da_gas_price_change_percent: u16, + da_p_component: i64, + da_d_component: i64, +} + +impl V1MetadataInitializer { + pub fn initialize( + &self, + l2_block_height: u32, + da_block_height: u32, + ) -> anyhow::Result { + let gas_price_factor = NonZeroU64::new(self.gas_price_factor) + .ok_or_else(|| anyhow::anyhow!("gas_price_factor must be non-zero"))?; + let metadata = V1Metadata { + #[allow(clippy::arithmetic_side_effects)] + new_scaled_exec_price: self.new_exec_gas_price * gas_price_factor.get(), + min_exec_gas_price: self.min_exec_gas_price, + exec_gas_price_change_percent: self.exec_gas_price_change_percent, + l2_block_height, + l2_block_fullness_threshold_percent: ClampedPercentage::new( + self.l2_block_fullness_threshold_percent, + ), + #[allow(clippy::arithmetic_side_effects)] + new_scaled_da_gas_price: self.min_da_gas_price * gas_price_factor.get(), + gas_price_factor, + min_da_gas_price: self.min_da_gas_price, + max_da_gas_price_change_percent: self.max_da_gas_price_change_percent, + total_da_rewards_excess: 0, + da_recorded_block_height: da_block_height, + latest_known_total_da_cost_excess: 0, + projected_total_da_cost: 0, + da_p_component: self.da_p_component, + da_d_component: self.da_d_component, + last_profit: 0, + second_to_last_profit: 0, + latest_da_cost_per_byte: 0, + }; + + Ok(metadata) + } +} + impl From for AlgorithmUpdaterV1 { fn from(metadata: V1Metadata) -> Self { Self {