From 100b5b86e092e00e466befb1070d570fa0935f8e Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:53:09 +0530 Subject: [PATCH] chore: use structured storage strat --- .../service/adapters/gas_price_adapters.rs | 40 +++++++---- .../service/sub_services/algorithm_updater.rs | 70 ++++++++----------- .../src/fuel_gas_price_updater.rs | 6 +- .../fuel_core_storage_adapter.rs | 54 ++++++++++++++ .../metadata_tests.rs | 44 ++---------- .../src/fuel_gas_price_updater/tests.rs | 21 ++---- .../services/gas_price_service/src/ports.rs | 19 ++--- 7 files changed, 130 insertions(+), 124 deletions(-) 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 9e0c7f1800b..22689b94f0c 100644 --- a/crates/fuel-core/src/service/adapters/gas_price_adapters.rs +++ b/crates/fuel-core/src/service/adapters/gas_price_adapters.rs @@ -16,6 +16,7 @@ use fuel_core_gas_price_service::{ ports::{ GasPriceData, L2Data, + MetadataStorage, }, }; use fuel_core_storage::{ @@ -58,31 +59,40 @@ impl L2Data for OnChainIterableKeyValueView { } impl GasPriceData for Database { + fn latest_height(&self) -> Option { + HistoricalView::latest_height(self) + } +} + +impl MetadataStorage for Database { fn get_metadata( &self, block_height: &BlockHeight, - ) -> StorageResult> { - self.storage::() + ) -> GasPriceResult> { + let metadata = self + .storage::() .get(block_height) - .map(|metadata| metadata.map(|metadata| metadata.as_ref().clone())) + .map_err(|err| GasPriceError::CouldNotFetchMetadata { + source_error: err.into(), + })?; + Ok(metadata.map(|inner| inner.into_owned())) } - fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> StorageResult<()> { - let height = metadata.l2_block_height(); + fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> GasPriceResult<()> { + let block_height = metadata.l2_block_height(); let mut tx = self.write_transaction(); tx.storage_as_mut::() - .insert(&height, metadata)?; - tx.commit()?; + .insert(&block_height, metadata) + .map_err(|err| GasPriceError::CouldNotSetMetadata { + block_height, + source_error: err.into(), + })?; + tx.commit().map_err(|err| GasPriceError::CouldNotSetMetadata { + block_height, + source_error: err.into(), + })?; Ok(()) } - - fn latest_height(&self) -> Option { - HistoricalView::latest_height(self) - } - - fn rollback_last_block(&self) -> StorageResult<()> { - self.rollback_last_block() - } } impl GasPriceSettingsProvider for ConsensusParametersProvider { 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 324775532e2..326792f7c9d 100644 --- a/crates/fuel-core/src/service/sub_services/algorithm_updater.rs +++ b/crates/fuel-core/src/service/sub_services/algorithm_updater.rs @@ -12,6 +12,7 @@ use fuel_core_gas_price_service::{ }, fuel_core_storage_adapter::{ get_block_info, + storage::GasPriceColumn, FuelL2BlockSource, GasPriceSettings, GasPriceSettingsProvider, @@ -27,6 +28,7 @@ use fuel_core_gas_price_service::{ ports::{ GasPriceData, L2Data, + MetadataStorage, }, GasPriceService, SharedGasPriceAlgo, @@ -38,8 +40,13 @@ use fuel_core_services::{ StateWatcher, }; use fuel_core_storage::{ + kv_store::KeyValueInspect, not_found, - transactional::AtomicView, + structured_storage::StructuredStorage, + transactional::{ + AtomicView, + Modifiable, + }, }; use fuel_core_types::{ fuel_types::BlockHeight, @@ -48,7 +55,7 @@ use fuel_core_types::{ type Updater = FuelGasPriceUpdater< FuelL2BlockSource, - GasPriceStore, + StructuredStorage, DaBlockCostsSharedState, >; @@ -70,7 +77,7 @@ impl where L2DataStore: L2Data, L2DataStoreView: AtomicView, - GasPriceStore: GasPriceData, + GasPriceStore: GasPriceData + MetadataStorage, { pub fn new( config: Config, @@ -121,7 +128,7 @@ fn get_best_algo( default_metadata: UpdaterMetadata, ) -> anyhow::Result where - GasPriceStore: GasPriceData, + GasPriceStore: MetadataStorage + GasPriceData, { let best_metadata: UpdaterMetadata = if let Some(height) = gas_price_db.latest_height() { @@ -141,7 +148,10 @@ impl RunnableService where L2DataStore: L2Data, L2DataStoreView: AtomicView, - GasPriceStore: GasPriceData, + GasPriceStore: GasPriceData + + MetadataStorage + + KeyValueInspect + + Modifiable, { const NAME: &'static str = "GasPriceUpdater"; type SharedData = SharedGasPriceAlgo; @@ -192,7 +202,10 @@ pub fn get_synced_gas_price_updater where L2DataStore: L2Data, L2DataStoreView: AtomicView, - GasPriceStore: GasPriceData, + GasPriceStore: GasPriceData + + MetadataStorage + + KeyValueInspect + + Modifiable, { let mut first_run = false; let latest_block_height: u32 = on_chain_db @@ -202,7 +215,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; @@ -210,24 +223,16 @@ 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 l2_block_source = FuelL2BlockSource::new(genesis_block_height, settings.clone(), block_stream); if BlockHeight::from(latest_block_height) == genesis_block_height || first_run { + let metadata_storage = StructuredStorage::new(gas_price_db); + let updater = FuelGasPriceUpdater::new( default_metadata.into(), l2_block_source, - gas_price_db, + metadata_storage, da_block_costs, ); Ok(updater) @@ -241,11 +246,12 @@ where latest_block_height, )?; } + let metadata_storage = StructuredStorage::new(gas_price_db); FuelGasPriceUpdater::init( latest_block_height.into(), l2_block_source, - gas_price_db, + metadata_storage, da_block_costs, config.min_gas_price, config.gas_price_change_percent, @@ -265,7 +271,7 @@ fn sync_gas_price_db_with_on_chain_storage, - GasPriceStore: GasPriceData, + GasPriceStore: MetadataStorage, { let metadata = gas_price_db @@ -298,12 +304,12 @@ fn sync_v0_metadata( metadata_height: u32, latest_block_height: u32, updater: &mut AlgorithmUpdaterV0, - gas_price_db: &mut GasPriceStore, + metadata_storage: &mut GasPriceStore, ) -> anyhow::Result<()> where L2DataStore: L2Data, L2DataStoreView: AtomicView, - GasPriceStore: GasPriceData, + GasPriceStore: MetadataStorage, { let first = metadata_height.saturating_add(1); let view = on_chain_db.latest_view()?; @@ -329,26 +335,8 @@ where updater.update_l2_block_data(height, block_gas_used, block_gas_capacity)?; let metadata = AlgorithmUpdater::V0(updater.clone()).into(); - gas_price_db.set_metadata(&metadata)?; + metadata_storage.set_metadata(&metadata)?; } Ok(()) } - -fn revert_gas_price_db_to_height( - gas_price_db: &mut GasPriceStore, - height: BlockHeight, -) -> 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(); - 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.rs b/crates/services/gas_price_service/src/fuel_gas_price_updater.rs index b025b0cc06a..8cc7c37e26e 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,5 +1,5 @@ use crate::{ - ports::GasPriceData, + ports::MetadataStorage, GasPriceAlgorithm, UpdateAlgorithm, }; @@ -203,7 +203,7 @@ impl From for UpdaterMetadata { impl FuelGasPriceUpdater where - Metadata: GasPriceData, + Metadata: MetadataStorage, DaBlockCosts: GetDaBlockCosts, { pub fn init( @@ -307,7 +307,7 @@ impl UpdateAlgorithm for FuelGasPriceUpdater where L2: L2BlockSource, - Metadata: GasPriceData, + Metadata: MetadataStorage, 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 1bf8ce5eae9..3b64c17c68a 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,14 +1,33 @@ use crate::fuel_gas_price_updater::{ BlockInfo, Error as GasPriceError, + Error, L2BlockSource, 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, + transactional::{ + Modifiable, + WriteTransaction, + }, + StorageAsMut, + StorageAsRef, +}; use fuel_core_types::{ blockchain::{ block::Block, @@ -34,6 +53,41 @@ 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 649ce292b72..b0775bcfe4e 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,27 +1,17 @@ #![allow(non_snake_case)] use super::*; -use crate::{ - fuel_gas_price_updater::{ - fuel_core_storage_adapter::storage::{ - GasPriceColumn, - GasPriceMetadata, - }, - AlgorithmUpdater, - UpdaterMetadata, - }, - ports::GasPriceData, +use crate::fuel_gas_price_updater::{ + fuel_core_storage_adapter::storage::GasPriceColumn, + AlgorithmUpdater, + UpdaterMetadata, }; use fuel_core_storage::{ structured_storage::test::InMemoryStorage, transactional::{ IntoTransaction, - Modifiable, StorageTransaction, }, - Result as StorageResult, - StorageAsMut, - StorageAsRef, }; use fuel_gas_price_algorithm::v0::AlgorithmUpdaterV0; @@ -40,32 +30,6 @@ fn database() -> StorageTransaction> { InMemoryStorage::default().into_transaction() } -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())) - } - - fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> StorageResult<()> { - self.storage::() - .insert(&metadata.l2_block_height(), metadata)?; - self.commit_changes(self.changes().clone())?; - Ok(()) - } - - fn latest_height(&self) -> Option { - todo!() - } - - fn rollback_last_block(&self) -> StorageResult<()> { - todo!() - } -} - #[tokio::test] async fn get_metadata__returns_none_if_does_not_exist() { // given 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 c49f1bb35cd..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 @@ -1,7 +1,6 @@ #![allow(non_snake_case)] use super::*; -use fuel_core_storage::Result as StorageResult; use std::sync::Arc; use tokio::sync::mpsc::Receiver; @@ -38,26 +37,16 @@ impl FakeMetadata { } } -impl GasPriceData for FakeMetadata { - fn get_metadata( - &self, - _block_height: &BlockHeight, - ) -> StorageResult> { - Ok(self.inner.lock().unwrap().clone()) +impl MetadataStorage for FakeMetadata { + fn get_metadata(&self, _: &BlockHeight) -> Result> { + let metadata = self.inner.lock().unwrap().clone(); + Ok(metadata) } - fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> StorageResult<()> { + fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> Result<()> { *self.inner.lock().unwrap() = Some(metadata.clone()); Ok(()) } - - fn latest_height(&self) -> Option { - None - } - - fn rollback_last_block(&self) -> StorageResult<()> { - Ok(()) - } } fn arb_metadata() -> UpdaterMetadata { diff --git a/crates/services/gas_price_service/src/ports.rs b/crates/services/gas_price_service/src/ports.rs index b074840beb1..868fd8d9815 100644 --- a/crates/services/gas_price_service/src/ports.rs +++ b/crates/services/gas_price_service/src/ports.rs @@ -1,4 +1,7 @@ -use crate::fuel_gas_price_updater::UpdaterMetadata; +use crate::fuel_gas_price_updater::{ + Result, + UpdaterMetadata, +}; use fuel_core_storage::Result as StorageResult; use fuel_core_types::{ blockchain::block::Block, @@ -14,14 +17,12 @@ pub trait L2Data: Send + Sync { ) -> StorageResult>>; } -pub trait GasPriceData: Send + Sync { - fn get_metadata( - &self, - block_height: &BlockHeight, - ) -> StorageResult>; - fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> StorageResult<()>; +pub trait MetadataStorage: Send + Sync { + fn get_metadata(&self, block_height: &BlockHeight) + -> Result>; + fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> Result<()>; +} +pub trait GasPriceData: Send + Sync { fn latest_height(&self) -> Option; - - fn rollback_last_block(&self) -> StorageResult<()>; }