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 85f4bf9f03..58e24cfb2d 100644 --- a/crates/fuel-core/src/service/adapters/gas_price_adapters.rs +++ b/crates/fuel-core/src/service/adapters/gas_price_adapters.rs @@ -2,20 +2,30 @@ use crate::service::adapters::ConsensusParametersProvider; use fuel_core_gas_price_service::{ fuel_gas_price_updater::{ fuel_core_storage_adapter::{ + storage::GasPriceMetadata, GasPriceSettings, GasPriceSettingsProvider, }, Error as GasPriceError, Result as GasPriceResult, + UpdaterMetadata, + }, + ports::{ + GasPriceData, + L2Data, }, - ports::L2Data, }; use fuel_core_storage::{ tables::{ FuelBlocks, Transactions, }, + transactional::{ + HistoricalView, + WriteTransaction, + }, Result as StorageResult, + StorageAsMut, StorageAsRef, }; use fuel_core_types::{ @@ -30,7 +40,11 @@ use fuel_core_types::{ fuel_types::BlockHeight, }; -use crate::database::OnChainIterableKeyValueView; +use crate::database::{ + database_description::gas_price::GasPriceDatabase, + Database, + OnChainIterableKeyValueView, +}; #[cfg(test)] mod tests; @@ -53,6 +67,34 @@ impl L2Data for OnChainIterableKeyValueView { } } +impl GasPriceData for Database { + fn get_metadata( + &self, + block_height: &BlockHeight, + ) -> StorageResult> { + self.storage::() + .get(block_height) + .map(|metadata| metadata.map(|metadata| metadata.as_ref().clone())) + } + + fn set_metadata(&mut self, metadata: UpdaterMetadata) -> StorageResult<()> { + let height = metadata.l2_block_height(); + let mut tx = self.write_transaction(); + tx.storage_as_mut::() + .insert(&height, &metadata)?; + tx.commit()?; + Ok(()) + } + + fn latest_height(&self) -> Option { + HistoricalView::latest_height(self) + } + + fn rollback_last_block(&self) -> StorageResult<()> { + self.rollback_last_block() + } +} + impl GasPriceSettingsProvider for ConsensusParametersProvider { fn settings( &self, diff --git a/crates/fuel-core/src/service/sub_services/algorithm_updater.rs b/crates/fuel-core/src/service/sub_services/algorithm_updater.rs index 4679a40c6e..bbe792eb04 100644 --- a/crates/fuel-core/src/service/sub_services/algorithm_updater.rs +++ b/crates/fuel-core/src/service/sub_services/algorithm_updater.rs @@ -1,13 +1,6 @@ -use crate::{ - database::{ - database_description::gas_price::GasPriceDatabase, - Database, - RegularStage, - }, - service::{ - adapters::ConsensusParametersProvider, - Config, - }, +use crate::service::{ + adapters::ConsensusParametersProvider, + Config, }; use fuel_core_gas_price_service::{ @@ -18,7 +11,6 @@ use fuel_core_gas_price_service::{ DaBlockCostsSharedState, }, fuel_core_storage_adapter::{ - storage::GasPriceMetadata, FuelL2BlockSource, GasPriceSettingsProvider, }, @@ -26,11 +18,13 @@ use fuel_core_gas_price_service::{ AlgorithmUpdater, AlgorithmUpdaterV0, FuelGasPriceUpdater, - MetadataStorage, UpdaterMetadata, V0Metadata, }, - ports::L2Data, + ports::{ + GasPriceData, + L2Data, + }, GasPriceService, SharedGasPriceAlgo, }; @@ -40,53 +34,45 @@ use fuel_core_services::{ Service, StateWatcher, }; -use fuel_core_storage::{ - structured_storage::StructuredStorage, - transactional::{ - AtomicView, - HistoricalView, - }, - StorageAsRef, -}; +use fuel_core_storage::transactional::AtomicView; use fuel_core_types::{ fuel_tx::field::MintAmount, fuel_types::BlockHeight, services::block_importer::SharedImportResult, }; -type Updater = FuelGasPriceUpdater< +type Updater = FuelGasPriceUpdater< FuelL2BlockSource, - MetadataStorageAdapter, + GasPriceStore, DaBlockCostsSharedState, >; -pub struct InitializeTask { +pub struct InitializeTask { pub config: Config, pub genesis_block_height: BlockHeight, pub settings: ConsensusParametersProvider, - pub gas_price_db: Database>, + 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, { pub fn new( config: Config, genesis_block_height: BlockHeight, settings: ConsensusParametersProvider, block_stream: BoxStream, - gas_price_db: Database>, + gas_price_db: GasPriceStore, on_chain_db: L2DataStoreView, ) -> anyhow::Result { let view = on_chain_db.latest_view()?; @@ -125,16 +111,17 @@ 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: &GasPriceStore, default_metadata: UpdaterMetadata, -) -> anyhow::Result { +) -> anyhow::Result +where + GasPriceStore: GasPriceData, +{ let best_metadata: UpdaterMetadata = if let Some(height) = gas_price_db.latest_height() { gas_price_db - .storage::() - .get(&height)? - .map(|m| m.into_owned()) + .get_metadata(&height)? .unwrap_or(default_metadata) } else { default_metadata @@ -144,14 +131,16 @@ 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, { const NAME: &'static str = "GasPriceUpdater"; type SharedData = SharedGasPriceAlgo; - type Task = Task; + type Task = Task; type TaskParams = (); fn shared_data(&self) -> Self::SharedData { @@ -186,18 +175,19 @@ where } } -pub fn get_synced_gas_price_updater( +pub fn get_synced_gas_price_updater( config: Config, genesis_block_height: BlockHeight, settings: ConsensusParametersProvider, - mut gas_price_db: Database>, + mut 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, { let mut first_run = false; let latest_block_height: u32 = on_chain_db @@ -225,7 +215,6 @@ where .into(); } - let mut metadata_storage = StructuredStorage::new(gas_price_db); let l2_block_source = FuelL2BlockSource::new(genesis_block_height, settings.clone(), block_stream); @@ -233,15 +222,15 @@ where let updater = FuelGasPriceUpdater::new( default_metadata.into(), l2_block_source, - metadata_storage, + gas_price_db, da_block_costs, ); 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, + &mut gas_price_db, on_chain_db, metadata_height, latest_block_height, @@ -251,7 +240,7 @@ where FuelGasPriceUpdater::init( latest_block_height.into(), l2_block_source, - metadata_storage, + gas_price_db, da_block_costs, config.min_gas_price, config.gas_price_change_percent, @@ -261,11 +250,9 @@ where } } -fn sync_metadata_storage_with_on_chain_storage( +fn sync_gas_price_db_with_on_chain_storage( settings: &ConsensusParametersProvider, - metadata_storage: &mut StructuredStorage< - Database>, - >, + gas_price_db: &mut GasPriceStore, on_chain_db: &L2DataStoreView, metadata_height: u32, latest_block_height: u32, @@ -273,12 +260,14 @@ fn sync_metadata_storage_with_on_chain_storage( where L2DataStore: L2Data, L2DataStoreView: AtomicView, + GasPriceStore: GasPriceData, { - let metadata = metadata_storage - .get_metadata(&metadata_height.into())? - .ok_or(anyhow::anyhow!( - "Expected metadata to exist for height: {metadata_height}" - ))?; + let metadata = + gas_price_db + .get_metadata(&metadata_height.into())? + .ok_or(anyhow::anyhow!( + "Expected metadata to exist for height: {metadata_height}" + ))?; let mut inner: AlgorithmUpdater = metadata.into(); match &mut inner { AlgorithmUpdater::V0(ref mut updater) => { @@ -288,7 +277,7 @@ where metadata_height, latest_block_height, updater, - metadata_storage, + gas_price_db, )?; } AlgorithmUpdater::V1(_) => { @@ -298,19 +287,18 @@ where Ok(()) } -fn sync_v0_metadata( +fn sync_v0_metadata( settings: &ConsensusParametersProvider, on_chain_db: &L2DataStoreView, metadata_height: u32, latest_block_height: u32, updater: &mut AlgorithmUpdaterV0, - metadata_storage: &mut StructuredStorage< - Database>, - >, + gas_price_db: &mut GasPriceStore, ) -> anyhow::Result<()> where L2DataStore: L2Data, L2DataStoreView: AtomicView, + GasPriceStore: GasPriceData, { let first = metadata_height.saturating_add(1); let view = on_chain_db.latest_view()?; @@ -333,15 +321,18 @@ where let block_gas_capacity = params.block_gas_limit.try_into()?; updater.update_l2_block_data(height, *block_gas_used, block_gas_capacity)?; let metadata = AlgorithmUpdater::V0(updater.clone()).into(); - metadata_storage.set_metadata(metadata)?; + gas_price_db.set_metadata(metadata)?; } Ok(()) } -fn revert_gas_price_db_to_height( - gas_price_db: &mut Database>, +fn revert_gas_price_db_to_height( + gas_price_db: &mut GasPriceStore, height: BlockHeight, -) -> anyhow::Result<()> { +) -> anyhow::Result<()> +where + GasPriceStore: GasPriceData, +{ 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(); 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 84609ac097..ba2976a4eb 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::GasPriceData, GasPriceAlgorithm, UpdateAlgorithm, }; @@ -200,15 +201,9 @@ 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, + Metadata: GasPriceData, DaBlockCosts: GetDaBlockCosts, { pub fn init( @@ -220,12 +215,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( @@ -310,7 +306,7 @@ impl UpdateAlgorithm for FuelGasPriceUpdater where L2: L2BlockSource, - Metadata: MetadataStorage + Send + Sync, + Metadata: GasPriceData, DaBlockCosts: GetDaBlockCosts, { type Algorithm = Algorithm; 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 21ac241be9..69829efe13 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,29 +1,12 @@ use crate::fuel_gas_price_updater::{ - fuel_core_storage_adapter::storage::{ - GasPriceColumn, - GasPriceMetadata, - }, BlockInfo, - Error, Error as GasPriceError, L2BlockSource, - MetadataStorage, Result, Result as GasPriceResult, - UpdaterMetadata, }; use anyhow::anyhow; use fuel_core_services::stream::BoxStream; -use fuel_core_storage::{ - kv_store::KeyValueInspect, - structured_storage::StructuredStorage, - transactional::{ - Modifiable, - WriteTransaction, - }, - StorageAsMut, - StorageAsRef, -}; use fuel_core_types::fuel_types::BlockHeight; use fuel_core_types::{ @@ -51,41 +34,6 @@ mod l2_source_tests; pub mod storage; -impl MetadataStorage for StructuredStorage -where - Storage: KeyValueInspect + Modifiable, - Storage: Send + Sync, -{ - fn get_metadata( - &self, - block_height: &BlockHeight, - ) -> Result> { - let metadata = self - .storage::() - .get(block_height) - .map_err(|err| Error::CouldNotFetchMetadata { - source_error: err.into(), - })?; - Ok(metadata.map(|inner| inner.into_owned())) - } - - 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) - .map_err(|err| Error::CouldNotSetMetadata { - block_height, - source_error: err.into(), - })?; - tx.commit().map_err(|err| Error::CouldNotSetMetadata { - block_height, - source_error: err.into(), - })?; - Ok(()) - } -} - pub struct FuelL2BlockSource { genesis_block_height: BlockHeight, gas_price_settings: Settings, 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 79e6ed748c..371d2f6189 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,26 +1,30 @@ #![allow(non_snake_case)] -use crate::fuel_gas_price_updater::{ - fuel_core_storage_adapter::storage::GasPriceColumn, - AlgorithmUpdater, +use super::*; +use crate::{ + fuel_gas_price_updater::{ + fuel_core_storage_adapter::storage::{ + GasPriceColumn, + GasPriceMetadata, + }, + AlgorithmUpdater, + UpdaterMetadata, + }, + ports::GasPriceData, }; use fuel_core_storage::{ structured_storage::test::InMemoryStorage, transactional::{ IntoTransaction, + Modifiable, StorageTransaction, }, + Result as StorageResult, StorageAsMut, + StorageAsRef, }; use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0; -use super::*; - -fn arb_metadata() -> UpdaterMetadata { - let height = 111231u32.into(); - arb_metadata_with_l2_height(height) -} - fn arb_metadata_with_l2_height(l2_height: BlockHeight) -> UpdaterMetadata { let inner = AlgorithmUpdaterV0 { new_exec_price: 100, @@ -36,23 +40,30 @@ fn database() -> StorageTransaction> { InMemoryStorage::default().into_transaction() } -#[tokio::test] -async fn get_metadata__can_get_most_recent_version() { - // given - let mut database = database(); - let block_height: BlockHeight = 1u32.into(); - let metadata = arb_metadata(); - database - .storage_as_mut::() - .insert(&block_height, &metadata) - .unwrap(); +impl GasPriceData for StorageTransaction> { + fn get_metadata( + &self, + block_height: &BlockHeight, + ) -> StorageResult> { + self.storage_as_ref::() + .get(block_height) + .map(|v| v.map(|v| v.as_ref().clone())) + } - // when - let actual = database.get_metadata(&block_height).unwrap(); + fn set_metadata(&mut self, metadata: UpdaterMetadata) -> StorageResult<()> { + self.storage::() + .insert(&metadata.l2_block_height(), &metadata)?; + self.commit_changes(self.changes().clone())?; + Ok(()) + } - // then - let expected = Some(metadata); - assert_eq!(expected, actual); + fn latest_height(&self) -> Option { + todo!() + } + + fn rollback_last_block(&self) -> StorageResult<()> { + todo!() + } } #[tokio::test] 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 6a4a669e05..6ef8b5c699 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 @@ -1,6 +1,7 @@ #![allow(non_snake_case)] use super::*; +use fuel_core_storage::Result as StorageResult; use std::sync::Arc; use tokio::sync::mpsc::Receiver; @@ -37,16 +38,24 @@ impl FakeMetadata { } } -impl MetadataStorage for FakeMetadata { +impl GasPriceData for FakeMetadata { fn get_metadata( &self, _block_height: &BlockHeight, - ) -> Result> { + ) -> StorageResult> { Ok(self.inner.lock().unwrap().clone()) } - fn set_metadata(&mut self, metadata: UpdaterMetadata) -> Result<()> { - let _ = self.inner.lock().unwrap().replace(metadata); + fn set_metadata(&mut self, metadata: UpdaterMetadata) -> StorageResult<()> { + *self.inner.lock().unwrap() = Some(metadata); + Ok(()) + } + + fn latest_height(&self) -> Option { + None + } + + fn rollback_last_block(&self) -> StorageResult<()> { Ok(()) } } diff --git a/crates/services/gas_price_service/src/ports.rs b/crates/services/gas_price_service/src/ports.rs index cd22c83be2..79b86d1a5e 100644 --- a/crates/services/gas_price_service/src/ports.rs +++ b/crates/services/gas_price_service/src/ports.rs @@ -1,3 +1,4 @@ +use crate::fuel_gas_price_updater::UpdaterMetadata; use fuel_core_storage::Result as StorageResult; use fuel_core_types::{ blockchain::block::CompressedBlock, @@ -13,3 +14,15 @@ pub trait L2Data: Send + Sync { fn get_block(&self, height: &BlockHeight) -> StorageResult>; fn get_transaction(&self, tx_id: &TxId) -> StorageResult>; } + +pub trait GasPriceData: Send + Sync { + fn get_metadata( + &self, + block_height: &BlockHeight, + ) -> StorageResult>; + fn set_metadata(&mut self, metadata: UpdaterMetadata) -> StorageResult<()>; + + fn latest_height(&self) -> Option; + + fn rollback_last_block(&self) -> StorageResult<()>; +}