Skip to content

Commit

Permalink
chore: use structured storage strat
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc committed Sep 23, 2024
1 parent 1c296b4 commit 100b5b8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 124 deletions.
40 changes: 25 additions & 15 deletions crates/fuel-core/src/service/adapters/gas_price_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use fuel_core_gas_price_service::{
ports::{
GasPriceData,
L2Data,
MetadataStorage,
},
};
use fuel_core_storage::{
Expand Down Expand Up @@ -58,31 +59,40 @@ impl L2Data for OnChainIterableKeyValueView {
}

impl GasPriceData for Database<GasPriceDatabase> {
fn latest_height(&self) -> Option<BlockHeight> {
HistoricalView::latest_height(self)
}
}

impl MetadataStorage for Database<GasPriceDatabase> {
fn get_metadata(
&self,
block_height: &BlockHeight,
) -> StorageResult<Option<UpdaterMetadata>> {
self.storage::<GasPriceMetadata>()
) -> GasPriceResult<Option<UpdaterMetadata>> {
let metadata = self
.storage::<GasPriceMetadata>()
.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::<GasPriceMetadata>()
.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<BlockHeight> {
HistoricalView::latest_height(self)
}

fn rollback_last_block(&self) -> StorageResult<()> {
self.rollback_last_block()
}
}

impl GasPriceSettingsProvider for ConsensusParametersProvider {
Expand Down
70 changes: 29 additions & 41 deletions crates/fuel-core/src/service/sub_services/algorithm_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use fuel_core_gas_price_service::{
},
fuel_core_storage_adapter::{
get_block_info,
storage::GasPriceColumn,
FuelL2BlockSource,
GasPriceSettings,
GasPriceSettingsProvider,
Expand All @@ -27,6 +28,7 @@ use fuel_core_gas_price_service::{
ports::{
GasPriceData,
L2Data,
MetadataStorage,
},
GasPriceService,
SharedGasPriceAlgo,
Expand All @@ -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,
Expand All @@ -48,7 +55,7 @@ use fuel_core_types::{

type Updater<GasPriceStore> = FuelGasPriceUpdater<
FuelL2BlockSource<ConsensusParametersProvider>,
GasPriceStore,
StructuredStorage<GasPriceStore>,
DaBlockCostsSharedState,
>;

Expand All @@ -70,7 +77,7 @@ impl<L2DataStore, L2DataStoreView, GasPriceStore>
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
GasPriceStore: GasPriceData,
GasPriceStore: GasPriceData + MetadataStorage,
{
pub fn new(
config: Config,
Expand Down Expand Up @@ -121,7 +128,7 @@ fn get_best_algo<GasPriceStore>(
default_metadata: UpdaterMetadata,
) -> anyhow::Result<Algorithm>
where
GasPriceStore: GasPriceData,
GasPriceStore: MetadataStorage + GasPriceData,
{
let best_metadata: UpdaterMetadata =
if let Some(height) = gas_price_db.latest_height() {
Expand All @@ -141,7 +148,10 @@ impl<L2DataStore, L2DataStoreView, GasPriceStore> RunnableService
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
GasPriceStore: GasPriceData,
GasPriceStore: GasPriceData
+ MetadataStorage
+ KeyValueInspect<Column = GasPriceColumn>
+ Modifiable,
{
const NAME: &'static str = "GasPriceUpdater";
type SharedData = SharedGasPriceAlgo<Algorithm>;
Expand Down Expand Up @@ -192,7 +202,10 @@ pub fn get_synced_gas_price_updater<L2DataStore, L2DataStoreView, GasPriceStore>
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
GasPriceStore: GasPriceData,
GasPriceStore: GasPriceData
+ MetadataStorage
+ KeyValueInspect<Column = GasPriceColumn>
+ Modifiable,
{
let mut first_run = false;
let latest_block_height: u32 = on_chain_db
Expand All @@ -202,32 +215,24 @@ 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;
latest_block_height
};
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)
Expand All @@ -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,
Expand All @@ -265,7 +271,7 @@ fn sync_gas_price_db_with_on_chain_storage<L2DataStore, L2DataStoreView, GasPric
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
GasPriceStore: GasPriceData,
GasPriceStore: MetadataStorage,
{
let metadata =
gas_price_db
Expand Down Expand Up @@ -298,12 +304,12 @@ fn sync_v0_metadata<L2DataStore, L2DataStoreView, GasPriceStore>(
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<LatestView = L2DataStore>,
GasPriceStore: GasPriceData,
GasPriceStore: MetadataStorage,
{
let first = metadata_height.saturating_add(1);
let view = on_chain_db.latest_view()?;
Expand All @@ -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<GasPriceStore>(
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(())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
ports::GasPriceData,
ports::MetadataStorage,
GasPriceAlgorithm,
UpdateAlgorithm,
};
Expand Down Expand Up @@ -203,7 +203,7 @@ impl From<AlgorithmUpdater> for UpdaterMetadata {

impl<L2, Metadata, DaBlockCosts> FuelGasPriceUpdater<L2, Metadata, DaBlockCosts>
where
Metadata: GasPriceData,
Metadata: MetadataStorage,
DaBlockCosts: GetDaBlockCosts,
{
pub fn init(
Expand Down Expand Up @@ -307,7 +307,7 @@ impl<L2, Metadata, DaBlockCosts> UpdateAlgorithm
for FuelGasPriceUpdater<L2, Metadata, DaBlockCosts>
where
L2: L2BlockSource,
Metadata: GasPriceData,
Metadata: MetadataStorage,
DaBlockCosts: GetDaBlockCosts,
{
type Algorithm = Algorithm;
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -34,6 +53,41 @@ mod l2_source_tests;

pub mod storage;

impl<Storage> MetadataStorage for StructuredStorage<Storage>
where
Storage: KeyValueInspect<Column = GasPriceColumn> + Modifiable,
Storage: Send + Sync,
{
fn get_metadata(
&self,
block_height: &BlockHeight,
) -> Result<Option<UpdaterMetadata>> {
let metadata = self
.storage::<GasPriceMetadata>()
.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::<GasPriceMetadata>()
.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<Settings> {
genesis_block_height: BlockHeight,
gas_price_settings: Settings,
Expand Down
Loading

0 comments on commit 100b5b8

Please sign in to comment.