From 794ca4de3955fc249cdb5e24585d96525d3e9576 Mon Sep 17 00:00:00 2001 From: Aaryamann Challani <43716372+rymnc@users.noreply.github.com> Date: Wed, 25 Sep 2024 19:35:44 +0530 Subject: [PATCH] chore(gas_price_service): move algorithm_updater to fuel-core-gas-price-service (#2246) > [!NOTE] > This is PR 5/n in cleaning up the gas price service. > Please review https://github.com/FuelLabs/fuel-core/pull/2245 before getting to this one ;) ## Linked Issues/PRs - https://github.com/FuelLabs/fuel-core/pull/2245 ## Description - Moves the `algorithm_updater` into `fuel-core-gas-price-service` now that it has no dependencies on fuel-core and relies on the ports we have created in previous PRs. ## Checklist - [x] Breaking changes are clearly marked as such in the PR description and changelog - [x] New behavior is reflected in tests - [x] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [x] I have reviewed the code myself - [ ] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams [Add or remove entries as needed] - [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) - [ ] [Sway compiler](https://github.com/FuelLabs/sway/) - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) (for out-of-organization contributors, the person merging the PR will do this) - [ ] Someone else? --------- Co-authored-by: Mitchell Turner Co-authored-by: Green Baneling --- crates/fuel-core/src/combined_database.rs | 15 ++ crates/fuel-core/src/service.rs | 1 + .../service/adapters/gas_price_adapters.rs | 36 +++- crates/fuel-core/src/service/sub_services.rs | 5 +- .../src/fuel_gas_price_updater.rs | 22 ++- .../algorithm_updater.rs | 165 ++++++++---------- .../fuel_core_storage_adapter.rs | 24 +-- .../l2_source_tests.rs | 1 + .../metadata_tests.rs | 7 +- .../src/fuel_gas_price_updater/tests.rs | 12 +- .../services/gas_price_service/src/ports.rs | 40 +++++ 11 files changed, 201 insertions(+), 127 deletions(-) rename crates/{fuel-core/src/service/sub_services => services/gas_price_service/src/fuel_gas_price_updater}/algorithm_updater.rs (71%) diff --git a/crates/fuel-core/src/combined_database.rs b/crates/fuel-core/src/combined_database.rs index 1cc42680bee..a5f527969fd 100644 --- a/crates/fuel-core/src/combined_database.rs +++ b/crates/fuel-core/src/combined_database.rs @@ -311,6 +311,21 @@ impl CombinedDatabase { Ok(()) } + + pub fn sync_aux_db_heights(&self, shutdown_listener: &mut S) -> anyhow::Result<()> + where + S: ShutdownListener, + { + if let Some(on_chain_height) = self.on_chain().latest_height_from_metadata()? { + // todo(https://github.com/FuelLabs/fuel-core/issues/2239): This is a temporary fix + let res = self.rollback_to(on_chain_height, shutdown_listener); + if res.is_err() { + tracing::warn!("Failed to rollback auxiliary databases to on-chain database height: {:?}", res); + } + }; + + Ok(()) + } } /// A trait for listening to shutdown signals. diff --git a/crates/fuel-core/src/service.rs b/crates/fuel-core/src/service.rs index 4894a55f699..b0e4bb04593 100644 --- a/crates/fuel-core/src/service.rs +++ b/crates/fuel-core/src/service.rs @@ -134,6 +134,7 @@ impl FuelService { // initialize sub services tracing::info!("Initializing sub services"); + database.sync_aux_db_heights(shutdown_listener)?; let (services, shared) = sub_services::init_sub_services(&config, database)?; let sub_services = Arc::new(services); 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 b504335f798..34eeb6dbe36 100644 --- a/crates/fuel-core/src/service/adapters/gas_price_adapters.rs +++ b/crates/fuel-core/src/service/adapters/gas_price_adapters.rs @@ -11,9 +11,16 @@ use fuel_core_gas_price_service::{ Error as GasPriceError, Result as GasPriceResult, }, - ports::L2Data, + ports::{ + GasPriceData, + GasPriceServiceConfig, + L2Data, + }, +}; +use fuel_core_storage::{ + transactional::HistoricalView, + Result as StorageResult, }; -use fuel_core_storage::Result as StorageResult; use fuel_core_types::{ blockchain::{ block::Block, @@ -23,6 +30,14 @@ use fuel_core_types::{ fuel_types::BlockHeight, }; +use crate::{ + database::{ + database_description::gas_price::GasPriceDatabase, + Database, + }, + service::Config, +}; + #[cfg(test)] mod tests; @@ -39,6 +54,23 @@ impl L2Data for OnChainIterableKeyValueView { } } +impl GasPriceData for Database { + fn latest_height(&self) -> Option { + HistoricalView::latest_height(self) + } +} + +impl From for GasPriceServiceConfig { + fn from(value: Config) -> Self { + GasPriceServiceConfig::new( + value.min_gas_price, + value.starting_gas_price, + value.gas_price_change_percent, + value.gas_price_threshold_percent, + ) + } +} + impl GasPriceSettingsProvider for ConsensusParametersProvider { fn settings( &self, diff --git a/crates/fuel-core/src/service/sub_services.rs b/crates/fuel-core/src/service/sub_services.rs index 5c4aaf05e23..ea3e62a9cd6 100644 --- a/crates/fuel-core/src/service/sub_services.rs +++ b/crates/fuel-core/src/service/sub_services.rs @@ -40,6 +40,7 @@ use crate::{ }; #[allow(unused_imports)] use fuel_core_gas_price_service::fuel_gas_price_updater::{ + algorithm_updater, fuel_core_storage_adapter::FuelL2BlockSource, Algorithm, AlgorithmV0, @@ -64,8 +65,6 @@ use fuel_core_types::blockchain::primitives::DaBlockHeight; use std::sync::Arc; use tokio::sync::Mutex; -mod algorithm_updater; - pub type PoAService = fuel_core_poa::Service< TxPoolAdapter, BlockProducerAdapter, @@ -201,7 +200,7 @@ pub fn init_sub_services( let block_stream = importer_adapter.events_shared_result(); let gas_price_init = algorithm_updater::InitializeTask::new( - config.clone(), + config.clone().into(), genesis_block_height, settings, block_stream, diff --git a/crates/services/gas_price_service/src/fuel_gas_price_updater.rs b/crates/services/gas_price_service/src/fuel_gas_price_updater.rs index 90017c6b9e6..b5fde86f2e5 100644 --- a/crates/services/gas_price_service/src/fuel_gas_price_updater.rs +++ b/crates/services/gas_price_service/src/fuel_gas_price_updater.rs @@ -1,4 +1,5 @@ use crate::{ + ports::MetadataStorage, GasPriceAlgorithm, UpdateAlgorithm, }; @@ -22,6 +23,7 @@ mod tests; pub mod fuel_core_storage_adapter; +pub mod algorithm_updater; pub mod da_source_adapter; pub struct FuelGasPriceUpdater { @@ -200,12 +202,6 @@ impl From for UpdaterMetadata { } } -pub trait MetadataStorage: Send + Sync { - fn get_metadata(&self, block_height: &BlockHeight) - -> Result>; - fn set_metadata(&mut self, metadata: UpdaterMetadata) -> Result<()>; -} - impl FuelGasPriceUpdater where Metadata: MetadataStorage, @@ -220,12 +216,13 @@ where exec_gas_price_change_percent: u64, l2_block_fullness_threshold_percent: u64, ) -> Result { - let old_metadata = metadata_storage.get_metadata(&target_block_height)?.ok_or( - Error::CouldNotInitUpdater(anyhow::anyhow!( + let old_metadata = metadata_storage + .get_metadata(&target_block_height) + .map_err(|err| Error::CouldNotInitUpdater(anyhow::anyhow!(err)))? + .ok_or(Error::CouldNotInitUpdater(anyhow::anyhow!( "No metadata found for block height: {:?}", target_block_height - )), - )?; + )))?; let inner = match old_metadata { UpdaterMetadata::V0(old) => { let v0 = AlgorithmUpdaterV0::new( @@ -256,8 +253,9 @@ where } async fn set_metadata(&mut self) -> anyhow::Result<()> { + let metadata = self.inner.clone().into(); self.metadata_storage - .set_metadata(self.inner.clone().into()) + .set_metadata(&metadata) .map_err(|err| anyhow!(err)) } @@ -310,7 +308,7 @@ impl UpdateAlgorithm for FuelGasPriceUpdater where L2: L2BlockSource, - Metadata: MetadataStorage + Send + Sync, + Metadata: MetadataStorage, DaBlockCosts: GetDaBlockCosts, { type Algorithm = Algorithm; diff --git a/crates/fuel-core/src/service/sub_services/algorithm_updater.rs b/crates/services/gas_price_service/src/fuel_gas_price_updater/algorithm_updater.rs similarity index 71% rename from crates/fuel-core/src/service/sub_services/algorithm_updater.rs rename to crates/services/gas_price_service/src/fuel_gas_price_updater/algorithm_updater.rs index f1d8e7308d7..b1e2c956416 100644 --- a/crates/fuel-core/src/service/sub_services/algorithm_updater.rs +++ b/crates/services/gas_price_service/src/fuel_gas_price_updater/algorithm_updater.rs @@ -1,16 +1,4 @@ use crate::{ - database::{ - database_description::gas_price::GasPriceDatabase, - Database, - RegularStage, - }, - service::{ - adapters::ConsensusParametersProvider, - Config, - }, -}; - -use fuel_core_gas_price_service::{ fuel_gas_price_updater::{ da_source_adapter::{ dummy_costs::DummyDaBlockCosts, @@ -19,21 +7,24 @@ use fuel_core_gas_price_service::{ }, fuel_core_storage_adapter::{ get_block_info, - storage::GasPriceMetadata, + storage::GasPriceColumn, FuelL2BlockSource, GasPriceSettings, GasPriceSettingsProvider, }, Algorithm, AlgorithmUpdater, - AlgorithmUpdaterV0, BlockInfo, FuelGasPriceUpdater, - MetadataStorage, UpdaterMetadata, V0Metadata, }, - ports::L2Data, + ports::{ + GasPriceData, + GasPriceServiceConfig, + L2Data, + MetadataStorage, + }, GasPriceService, SharedGasPriceAlgo, }; @@ -44,59 +35,61 @@ use fuel_core_services::{ StateWatcher, }; use fuel_core_storage::{ + kv_store::KeyValueInspect, not_found, structured_storage::StructuredStorage, transactional::{ AtomicView, - HistoricalView, + Modifiable, }, - StorageAsRef, }; use fuel_core_types::{ fuel_types::BlockHeight, services::block_importer::SharedImportResult, }; +use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0; -type Updater = FuelGasPriceUpdater< - FuelL2BlockSource, - MetadataStorageAdapter, +type Updater = FuelGasPriceUpdater< + FuelL2BlockSource, + StructuredStorage, DaBlockCostsSharedState, >; -pub struct InitializeTask { - pub config: Config, +pub struct InitializeTask { + pub config: GasPriceServiceConfig, pub genesis_block_height: BlockHeight, - pub settings: ConsensusParametersProvider, - pub gas_price_db: Database>, + pub settings: SettingsProvider, + pub gas_price_db: GasPriceStore, pub on_chain_db: L2DataStoreView, pub block_stream: BoxStream, pub shared_algo: SharedGasPriceAlgo, pub da_block_costs_provider: DaBlockCostsProvider, } -type MetadataStorageAdapter = - StructuredStorage>>; - -type Task = GasPriceService; +type Task = + GasPriceService>; -impl InitializeTask +impl + InitializeTask where L2DataStore: L2Data, L2DataStoreView: AtomicView, + GasPriceStore: GasPriceData + Modifiable + KeyValueInspect, + SettingsProvider: GasPriceSettingsProvider, { pub fn new( - config: Config, + config: GasPriceServiceConfig, genesis_block_height: BlockHeight, - settings: ConsensusParametersProvider, + settings: SettingsProvider, block_stream: BoxStream, - gas_price_db: Database>, + mut gas_price_db: GasPriceStore, on_chain_db: L2DataStoreView, ) -> anyhow::Result { let view = on_chain_db.latest_view()?; let latest_block_height = view.latest_height().unwrap_or(genesis_block_height).into(); let default_metadata = get_default_metadata(&config, latest_block_height); - let algo = get_best_algo(&gas_price_db, default_metadata)?; + let algo = get_best_algo(&mut gas_price_db, default_metadata)?; let shared_algo = SharedGasPriceAlgo::new_with_algorithm(algo); // there's no use of this source yet, so we can safely return an error let da_block_costs_source = @@ -118,7 +111,10 @@ where } } -fn get_default_metadata(config: &Config, latest_block_height: u32) -> UpdaterMetadata { +fn get_default_metadata( + config: &GasPriceServiceConfig, + latest_block_height: u32, +) -> UpdaterMetadata { UpdaterMetadata::V0(V0Metadata { new_exec_price: config.starting_gas_price.max(config.min_gas_price), min_exec_gas_price: config.min_gas_price, @@ -128,16 +124,18 @@ fn get_default_metadata(config: &Config, latest_block_height: u32) -> UpdaterMet }) } -fn get_best_algo( - gas_price_db: &Database>, +fn get_best_algo( + gas_price_db: &mut GasPriceStore, default_metadata: UpdaterMetadata, -) -> anyhow::Result { +) -> anyhow::Result +where + GasPriceStore: GasPriceData + Modifiable + KeyValueInspect, +{ let best_metadata: UpdaterMetadata = if let Some(height) = gas_price_db.latest_height() { - gas_price_db - .storage::() - .get(&height)? - .map(|m| m.into_owned()) + let metadata_storage = StructuredStorage::new(gas_price_db); + metadata_storage + .get_metadata(&height)? .unwrap_or(default_metadata) } else { default_metadata @@ -147,14 +145,17 @@ fn get_best_algo( Ok(algo) } #[async_trait::async_trait] -impl RunnableService for InitializeTask +impl RunnableService + for InitializeTask where L2DataStore: L2Data, L2DataStoreView: AtomicView, + GasPriceStore: GasPriceData + Modifiable + KeyValueInspect, + SettingsProvider: GasPriceSettingsProvider, { const NAME: &'static str = "GasPriceUpdater"; type SharedData = SharedGasPriceAlgo; - type Task = Task; + type Task = Task; type TaskParams = (); fn shared_data(&self) -> Self::SharedData { @@ -189,18 +190,25 @@ where } } -pub fn get_synced_gas_price_updater( - config: Config, +pub fn get_synced_gas_price_updater< + L2DataStore, + L2DataStoreView, + GasPriceStore, + SettingsProvider, +>( + config: GasPriceServiceConfig, genesis_block_height: BlockHeight, - settings: ConsensusParametersProvider, - mut gas_price_db: Database>, + settings: SettingsProvider, + gas_price_db: GasPriceStore, on_chain_db: &L2DataStoreView, block_stream: BoxStream, da_block_costs: DaBlockCostsSharedState, -) -> anyhow::Result +) -> anyhow::Result> where L2DataStore: L2Data, L2DataStoreView: AtomicView, + GasPriceStore: GasPriceData + Modifiable + KeyValueInspect, + SettingsProvider: GasPriceSettingsProvider, { let mut first_run = false; let latest_block_height: u32 = on_chain_db @@ -210,7 +218,7 @@ where .into(); let maybe_metadata_height = gas_price_db.latest_height(); - let mut metadata_height = if let Some(metadata_height) = maybe_metadata_height { + let metadata_height = if let Some(metadata_height) = maybe_metadata_height { metadata_height.into() } else { first_run = true; @@ -218,20 +226,11 @@ where }; let default_metadata = get_default_metadata(&config, latest_block_height); - if metadata_height > latest_block_height { - revert_gas_price_db_to_height(&mut gas_price_db, latest_block_height.into())?; - metadata_height = gas_price_db - .latest_height() - .ok_or(anyhow::anyhow!( - "Metadata DB height should match the latest block height" - ))? - .into(); - } - - let mut metadata_storage = StructuredStorage::new(gas_price_db); let l2_block_source = FuelL2BlockSource::new(genesis_block_height, settings.clone(), block_stream); + let mut metadata_storage = StructuredStorage::new(gas_price_db); + if BlockHeight::from(latest_block_height) == genesis_block_height || first_run { let updater = FuelGasPriceUpdater::new( default_metadata.into(), @@ -242,7 +241,7 @@ where Ok(updater) } else { if latest_block_height > metadata_height { - sync_metadata_storage_with_on_chain_storage( + sync_gas_price_db_with_on_chain_storage( &settings, &mut metadata_storage, on_chain_db, @@ -264,11 +263,14 @@ where } } -fn sync_metadata_storage_with_on_chain_storage( - settings: &ConsensusParametersProvider, - metadata_storage: &mut StructuredStorage< - Database>, - >, +fn sync_gas_price_db_with_on_chain_storage< + L2DataStore, + L2DataStoreView, + GasPriceStore, + SettingsProvider, +>( + settings: &SettingsProvider, + metadata_storage: &mut StructuredStorage, on_chain_db: &L2DataStoreView, metadata_height: u32, latest_block_height: u32, @@ -276,6 +278,8 @@ fn sync_metadata_storage_with_on_chain_storage( where L2DataStore: L2Data, L2DataStoreView: AtomicView, + GasPriceStore: GasPriceData + Modifiable + KeyValueInspect, + SettingsProvider: GasPriceSettingsProvider, { let metadata = metadata_storage .get_metadata(&metadata_height.into())? @@ -301,19 +305,19 @@ where Ok(()) } -fn sync_v0_metadata( - settings: &ConsensusParametersProvider, +fn sync_v0_metadata( + settings: &SettingsProvider, on_chain_db: &L2DataStoreView, metadata_height: u32, latest_block_height: u32, updater: &mut AlgorithmUpdaterV0, - metadata_storage: &mut StructuredStorage< - Database>, - >, + metadata_storage: &mut StructuredStorage, ) -> anyhow::Result<()> where L2DataStore: L2Data, L2DataStoreView: AtomicView, + GasPriceStore: GasPriceData + Modifiable + KeyValueInspect, + SettingsProvider: GasPriceSettingsProvider, { let first = metadata_height.saturating_add(1); let view = on_chain_db.latest_view()?; @@ -339,23 +343,8 @@ where updater.update_l2_block_data(height, block_gas_used, block_gas_capacity)?; let metadata = AlgorithmUpdater::V0(updater.clone()).into(); - metadata_storage.set_metadata(metadata)?; + metadata_storage.set_metadata(&metadata)?; } Ok(()) } - -fn revert_gas_price_db_to_height( - gas_price_db: &mut Database>, - height: BlockHeight, -) -> anyhow::Result<()> { - if let Some(gas_price_db_height) = gas_price_db.latest_height() { - let gas_price_db_height: u32 = gas_price_db_height.into(); - let height: u32 = height.into(); - let diff = gas_price_db_height.saturating_sub(height); - for _ in 0..diff { - gas_price_db.rollback_last_block()?; - } - } - Ok(()) -} diff --git a/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter.rs b/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter.rs index a1e4de8ed80..93fb0f7551c 100644 --- a/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter.rs +++ b/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter.rs @@ -1,19 +1,23 @@ use crate::fuel_gas_price_updater::{ - fuel_core_storage_adapter::storage::{ - GasPriceColumn, - GasPriceMetadata, - }, BlockInfo, - Error, Error as GasPriceError, + Error, L2BlockSource, - MetadataStorage, Result, Result as GasPriceResult, UpdaterMetadata, }; use anyhow::anyhow; use fuel_core_services::stream::BoxStream; +use fuel_core_types::fuel_types::BlockHeight; + +use crate::{ + fuel_gas_price_updater::fuel_core_storage_adapter::storage::{ + GasPriceColumn, + GasPriceMetadata, + }, + ports::MetadataStorage, +}; use fuel_core_storage::{ kv_store::KeyValueInspect, structured_storage::StructuredStorage, @@ -24,8 +28,6 @@ use fuel_core_storage::{ StorageAsMut, StorageAsRef, }; -use fuel_core_types::fuel_types::BlockHeight; - use fuel_core_types::{ blockchain::{ block::Block, @@ -69,11 +71,11 @@ where Ok(metadata.map(|inner| inner.into_owned())) } - fn set_metadata(&mut self, metadata: UpdaterMetadata) -> Result<()> { + fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> Result<()> { let block_height = metadata.l2_block_height(); let mut tx = self.write_transaction(); tx.storage_as_mut::() - .insert(&block_height, &metadata) + .insert(&block_height, metadata) .map_err(|err| Error::CouldNotSetMetadata { block_height, source_error: err.into(), @@ -111,7 +113,7 @@ pub struct GasPriceSettings { pub gas_price_factor: u64, pub block_gas_limit: u64, } -pub trait GasPriceSettingsProvider { +pub trait GasPriceSettingsProvider: Send + Sync + Clone { fn settings( &self, param_version: &ConsensusParametersVersion, diff --git a/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/l2_source_tests.rs b/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/l2_source_tests.rs index 06bc49b342e..8d25fcb0c59 100644 --- a/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/l2_source_tests.rs +++ b/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/l2_source_tests.rs @@ -34,6 +34,7 @@ use std::{ }; use tokio_stream::wrappers::ReceiverStream; +#[derive(Clone)] struct FakeSettings { gas_price_factor: u64, block_gas_limit: u64, diff --git a/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/metadata_tests.rs b/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/metadata_tests.rs index 79e6ed748ce..070f7087d91 100644 --- a/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/metadata_tests.rs +++ b/crates/services/gas_price_service/src/fuel_gas_price_updater/fuel_core_storage_adapter/metadata_tests.rs @@ -1,8 +1,10 @@ #![allow(non_snake_case)] +use super::*; use crate::fuel_gas_price_updater::{ fuel_core_storage_adapter::storage::GasPriceColumn, AlgorithmUpdater, + UpdaterMetadata, }; use fuel_core_storage::{ structured_storage::test::InMemoryStorage, @@ -10,12 +12,9 @@ use fuel_core_storage::{ IntoTransaction, StorageTransaction, }, - StorageAsMut, }; use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0; -use super::*; - fn arb_metadata() -> UpdaterMetadata { let height = 111231u32.into(); arb_metadata_with_l2_height(height) @@ -79,7 +78,7 @@ async fn set_metadata__can_set_metadata() { // when let actual = database.get_metadata(&block_height).unwrap(); assert_eq!(None, actual); - database.set_metadata(metadata.clone()).unwrap(); + database.set_metadata(&metadata).unwrap(); let actual = database.get_metadata(&block_height).unwrap(); // then diff --git a/crates/services/gas_price_service/src/fuel_gas_price_updater/tests.rs b/crates/services/gas_price_service/src/fuel_gas_price_updater/tests.rs index 6a4a669e059..9c4ab7daff2 100644 --- a/crates/services/gas_price_service/src/fuel_gas_price_updater/tests.rs +++ b/crates/services/gas_price_service/src/fuel_gas_price_updater/tests.rs @@ -38,15 +38,13 @@ impl FakeMetadata { } impl MetadataStorage for FakeMetadata { - fn get_metadata( - &self, - _block_height: &BlockHeight, - ) -> Result> { - Ok(self.inner.lock().unwrap().clone()) + fn get_metadata(&self, _: &BlockHeight) -> Result> { + let metadata = self.inner.lock().unwrap().clone(); + Ok(metadata) } - fn set_metadata(&mut self, metadata: UpdaterMetadata) -> Result<()> { - let _ = self.inner.lock().unwrap().replace(metadata); + fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> Result<()> { + *self.inner.lock().unwrap() = Some(metadata.clone()); Ok(()) } } diff --git a/crates/services/gas_price_service/src/ports.rs b/crates/services/gas_price_service/src/ports.rs index 7b4169c9454..f5d394d36fd 100644 --- a/crates/services/gas_price_service/src/ports.rs +++ b/crates/services/gas_price_service/src/ports.rs @@ -1,3 +1,7 @@ +use crate::fuel_gas_price_updater::{ + Result, + UpdaterMetadata, +}; use fuel_core_storage::Result as StorageResult; use fuel_core_types::{ blockchain::block::Block, @@ -12,3 +16,39 @@ pub trait L2Data: Send + Sync { height: &BlockHeight, ) -> StorageResult>>; } + +pub trait MetadataStorage: Send + Sync { + fn get_metadata(&self, block_height: &BlockHeight) + -> Result>; + fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> Result<()>; +} + +/// Provides the latest block height. +/// This is used to determine the latest block height that has been processed by the gas price service. +/// We need this to fetch the gas price data for the latest block. +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, +} + +impl GasPriceServiceConfig { + pub fn new( + min_gas_price: u64, + starting_gas_price: u64, + gas_price_change_percent: u64, + gas_price_threshold_percent: u64, + ) -> Self { + Self { + min_gas_price, + starting_gas_price, + gas_price_change_percent, + gas_price_threshold_percent, + } + } +}