From 4419e4dac98af325901a1becdee27366c936ead1 Mon Sep 17 00:00:00 2001 From: Mitch Turner Date: Fri, 25 Oct 2024 14:25:46 +0300 Subject: [PATCH] Create function for converting old metadata into new metadata --- .../services/gas_price_service/src/ports.rs | 20 +++---- .../gas_price_service/src/v0/metadata.rs | 11 +--- .../gas_price_service/src/v0/service.rs | 4 +- .../gas_price_service/src/v0/tests.rs | 10 ++-- .../src/v0/uninitialized_task.rs | 8 +-- .../gas_price_service/src/v1/metadata.rs | 57 ++++++++++--------- 6 files changed, 52 insertions(+), 58 deletions(-) diff --git a/crates/services/gas_price_service/src/ports.rs b/crates/services/gas_price_service/src/ports.rs index 0fcc9dd3ca9..c40e5969fc8 100644 --- a/crates/services/gas_price_service/src/ports.rs +++ b/crates/services/gas_price_service/src/ports.rs @@ -3,8 +3,8 @@ use crate::{ updater_metadata::UpdaterMetadata, utils::Result, }, - v0::metadata::V0MetadataInitializer, - v1::metadata::V1MetadataInitializer, + v0::metadata::V0AlgorithmConfig, + v1::metadata::V1AlgorithmConfig, }; use fuel_core_storage::Result as StorageResult; use fuel_core_types::{ @@ -35,8 +35,8 @@ pub trait GasPriceData: Send + Sync { } pub enum VersionSpecificConfig { - V0(V0MetadataInitializer), - V1(V1MetadataInitializer), + V0(V0AlgorithmConfig), + V1(V1AlgorithmConfig), } impl VersionSpecificConfig { @@ -46,7 +46,7 @@ impl VersionSpecificConfig { gas_price_change_percent: u64, gas_price_threshold_percent: u64, ) -> Self { - Self::V0(V0MetadataInitializer { + Self::V0(V0AlgorithmConfig { starting_gas_price, min_gas_price, gas_price_change_percent, @@ -54,12 +54,12 @@ impl VersionSpecificConfig { }) } - pub fn new_v1(metadata: V1MetadataInitializer) -> Self { + pub fn new_v1(metadata: V1AlgorithmConfig) -> Self { Self::V1(metadata) } /// Extract V0MetadataInitializer if it is of V0 version - pub fn v0(self) -> Option { + pub fn v0(self) -> Option { if let VersionSpecificConfig::V0(v0) = self { Some(v0) } else { @@ -68,7 +68,7 @@ impl VersionSpecificConfig { } /// Extract V1MetadataInitializer if it is of V1 version - pub fn v1(self) -> Option { + pub fn v1(self) -> Option { if let VersionSpecificConfig::V1(v1) = self { Some(v1) } else { @@ -88,11 +88,11 @@ impl GasPriceServiceConfig { } } - pub fn v0(self) -> Option { + pub fn v0(self) -> Option { self.version_specific_config.v0() } - pub fn v1(self) -> Option { + 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 47568fe1035..f42e33e041c 100644 --- a/crates/services/gas_price_service/src/v0/metadata.rs +++ b/crates/services/gas_price_service/src/v0/metadata.rs @@ -8,22 +8,13 @@ pub struct V0Metadata { pub l2_block_height: u32, } -pub struct V0MetadataInitializer { +pub struct V0AlgorithmConfig { 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), - l2_block_height, - } - } -} - impl From for V0Metadata { fn from(updater: AlgorithmUpdaterV0) -> Self { Self { diff --git a/crates/services/gas_price_service/src/v0/service.rs b/crates/services/gas_price_service/src/v0/service.rs index ead8db01f6f..b8d95650cd7 100644 --- a/crates/services/gas_price_service/src/v0/service.rs +++ b/crates/services/gas_price_service/src/v0/service.rs @@ -166,7 +166,7 @@ mod tests { ports::MetadataStorage, v0::{ algorithm::SharedV0Algorithm, - metadata::V0MetadataInitializer, + metadata::V0AlgorithmConfig, service::GasPriceServiceV0, uninitialized_task::initialize_algorithm, }, @@ -261,7 +261,7 @@ mod tests { }; let metadata_storage = FakeMetadata::empty(); let l2_block_height = 0; - let config = V0MetadataInitializer { + let config = V0AlgorithmConfig { starting_gas_price: 100, min_gas_price: 10, gas_price_change_percent: 10, diff --git a/crates/services/gas_price_service/src/v0/tests.rs b/crates/services/gas_price_service/src/v0/tests.rs index 35c13c46695..972cd9d6b51 100644 --- a/crates/services/gas_price_service/src/v0/tests.rs +++ b/crates/services/gas_price_service/src/v0/tests.rs @@ -21,8 +21,8 @@ use crate::{ }, v0::{ metadata::{ + V0AlgorithmConfig, V0Metadata, - V0MetadataInitializer, }, service::GasPriceServiceV0, uninitialized_task::{ @@ -116,8 +116,8 @@ impl MetadataStorage for ErroringMetadata { } } -fn arb_config() -> V0MetadataInitializer { - V0MetadataInitializer { +fn arb_config() -> V0AlgorithmConfig { + V0AlgorithmConfig { starting_gas_price: 100, min_gas_price: 0, gas_price_change_percent: 10, @@ -132,8 +132,8 @@ fn arb_metadata() -> V0Metadata { } } -fn different_arb_config() -> V0MetadataInitializer { - V0MetadataInitializer { +fn different_arb_config() -> V0AlgorithmConfig { + V0AlgorithmConfig { starting_gas_price: 200, min_gas_price: 0, gas_price_change_percent: 20, 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 07b22c0f2b2..a1a8a09af7d 100644 --- a/crates/services/gas_price_service/src/v0/uninitialized_task.rs +++ b/crates/services/gas_price_service/src/v0/uninitialized_task.rs @@ -23,8 +23,8 @@ use crate::{ v0::{ algorithm::SharedV0Algorithm, metadata::{ + V0AlgorithmConfig, V0Metadata, - V0MetadataInitializer, }, service::GasPriceServiceV0, }, @@ -48,7 +48,7 @@ use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0; pub use fuel_gas_price_algorithm::v0::AlgorithmV0; pub struct UninitializedTask { - pub config: V0MetadataInitializer, + pub config: V0AlgorithmConfig, pub genesis_block_height: BlockHeight, pub settings: SettingsProvider, pub gas_price_db: GasPriceStore, @@ -69,7 +69,7 @@ where SettingsProvider: GasPriceSettingsProvider, { pub fn new( - config: V0MetadataInitializer, + config: V0AlgorithmConfig, genesis_block_height: BlockHeight, settings: SettingsProvider, block_stream: BoxStream, @@ -188,7 +188,7 @@ where } pub fn initialize_algorithm( - config: &V0MetadataInitializer, + config: &V0AlgorithmConfig, latest_block_height: u32, metadata_storage: &Metadata, ) -> GasPriceResult<(AlgorithmUpdaterV0, SharedV0Algorithm)> diff --git a/crates/services/gas_price_service/src/v1/metadata.rs b/crates/services/gas_price_service/src/v1/metadata.rs index f7a57cc25c8..729e9b43db5 100644 --- a/crates/services/gas_price_service/src/v1/metadata.rs +++ b/crates/services/gas_price_service/src/v1/metadata.rs @@ -1,3 +1,4 @@ +use crate::v0::metadata::V0Metadata; use fuel_gas_price_algorithm::v1::{ AlgorithmUpdaterV1, ClampedPercentage, @@ -34,46 +35,48 @@ 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) +impl V1Metadata { + pub fn construct_from_v0_metadata( + v0_metadata: V0Metadata, + config: V1AlgorithmConfig, + ) -> anyhow::Result { + let gas_price_factor = NonZeroU64::new(config.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(), - l2_block_height, - #[allow(clippy::arithmetic_side_effects)] - new_scaled_da_gas_price: self.min_da_gas_price * gas_price_factor.get(), + let metadata = Self { + new_scaled_exec_price: v0_metadata + .new_exec_price + .saturating_mul(gas_price_factor.get()), + l2_block_height: v0_metadata.l2_block_height, + new_scaled_da_gas_price: config + .min_da_gas_price + .saturating_mul(gas_price_factor.get()), gas_price_factor, total_da_rewards_excess: 0, - da_recorded_block_height: da_block_height, + // TODO: Set to `None` after: + // https://github.com/FuelLabs/fuel-core/issues/2397 + da_recorded_block_height: 0, latest_known_total_da_cost_excess: 0, projected_total_da_cost: 0, last_profit: 0, second_to_last_profit: 0, latest_da_cost_per_byte: 0, }; - Ok(metadata) } } +pub struct V1AlgorithmConfig { + 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 From for V1Metadata { fn from(updater: AlgorithmUpdaterV1) -> Self { Self {