Skip to content

Commit

Permalink
chore(gas_price_service): move algorithm_updater to fuel-core-gas-pri…
Browse files Browse the repository at this point in the history
…ce-service (#2246)

> [!NOTE]
> This is PR 5/n in cleaning up the gas price service.
> Please review #2245 before
getting to this one ;)

## Linked Issues/PRs
<!-- List of related issues/PRs -->
- #2245

## Description
<!-- List of detailed changes -->
- 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 <james.mitchell.turner@gmail.com>
Co-authored-by: Green Baneling <XgreenX9999@gmail.com>
  • Loading branch information
3 people authored Sep 25, 2024
1 parent 65378d0 commit 794ca4d
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 127 deletions.
15 changes: 15 additions & 0 deletions crates/fuel-core/src/combined_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,21 @@ impl CombinedDatabase {

Ok(())
}

pub fn sync_aux_db_heights<S>(&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.
Expand Down
1 change: 1 addition & 0 deletions crates/fuel-core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 34 additions & 2 deletions crates/fuel-core/src/service/adapters/gas_price_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand All @@ -39,6 +54,23 @@ impl L2Data for OnChainIterableKeyValueView {
}
}

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

impl From<Config> 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,
Expand Down
5 changes: 2 additions & 3 deletions crates/fuel-core/src/service/sub_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 10 additions & 12 deletions crates/services/gas_price_service/src/fuel_gas_price_updater.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
ports::MetadataStorage,
GasPriceAlgorithm,
UpdateAlgorithm,
};
Expand All @@ -22,6 +23,7 @@ mod tests;

pub mod fuel_core_storage_adapter;

pub mod algorithm_updater;
pub mod da_source_adapter;

pub struct FuelGasPriceUpdater<L2, Metadata, DaBlockCosts> {
Expand Down Expand Up @@ -200,12 +202,6 @@ impl From<AlgorithmUpdater> for UpdaterMetadata {
}
}

pub trait MetadataStorage: Send + Sync {
fn get_metadata(&self, block_height: &BlockHeight)
-> Result<Option<UpdaterMetadata>>;
fn set_metadata(&mut self, metadata: UpdaterMetadata) -> Result<()>;
}

impl<L2, Metadata, DaBlockCosts> FuelGasPriceUpdater<L2, Metadata, DaBlockCosts>
where
Metadata: MetadataStorage,
Expand All @@ -220,12 +216,13 @@ where
exec_gas_price_change_percent: u64,
l2_block_fullness_threshold_percent: u64,
) -> Result<Self> {
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(
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -310,7 +308,7 @@ impl<L2, Metadata, DaBlockCosts> UpdateAlgorithm
for FuelGasPriceUpdater<L2, Metadata, DaBlockCosts>
where
L2: L2BlockSource,
Metadata: MetadataStorage + Send + Sync,
Metadata: MetadataStorage,
DaBlockCosts: GetDaBlockCosts,
{
type Algorithm = Algorithm;
Expand Down
Loading

0 comments on commit 794ca4d

Please sign in to comment.