Skip to content

Commit

Permalink
feat(gas_price_service): create runnable task for expensive backgroun…
Browse files Browse the repository at this point in the history
…d polling for da metadata (#2163)

> [!WARNING]
> 🏗️ This PR is 2/3 of including block committer "da"ta in v1 of the gas
price algo. We have a dependency on the api of the block committer,
which will warrant tweaking of the `DaMetadataResponse` type. The next
PR will hook it up with cli args 🚧



## Linked Issues/PRs
<!-- List of related issues/PRs -->
- follow up to: #2155
- #2139


## Description
<!-- List of detailed changes -->
- specified a Service that polls an ingestor at specified intervals to
fetch the da metadata in the background
- specified a "sync"/"non-expensive" api for the data, which marks it as
stale/None after fetching it

## 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
- [x] 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 <[email protected]>
  • Loading branch information
rymnc and MitchTurner authored Sep 13, 2024
1 parent f308bae commit 8c67681
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- [2135](https://github.com/FuelLabs/fuel-core/pull/2135): Added metrics logging for number of blocks served over the p2p req/res protocol.
- [2151](https://github.com/FuelLabs/fuel-core/pull/2151): Added limitations on gas used during dry_run in API.
- [2163](https://github.com/FuelLabs/fuel-core/pull/2163): Added runnable task for fetching block committer data.

### Changed

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 25 additions & 12 deletions crates/fuel-core/src/service/sub_services/algorithm_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ use crate::{

use fuel_core_gas_price_service::{
fuel_gas_price_updater::{
da_source_adapter::{
dummy_costs::DummyDaBlockCosts,
DaBlockCostsProvider,
DaBlockCostsSharedState,
},
fuel_core_storage_adapter::{
storage::GasPriceMetadata,
FuelL2BlockSource,
GasPriceSettingsProvider,
},
fuel_da_source_adapter::FuelDaSource,
Algorithm,
AlgorithmUpdater,
AlgorithmUpdaterV0,
Expand All @@ -35,6 +39,7 @@ use fuel_core_gas_price_service::{
use fuel_core_services::{
stream::BoxStream,
RunnableService,
Service,
StateWatcher,
};
use fuel_core_storage::{
Expand All @@ -58,7 +63,7 @@ use fuel_core_types::{
type Updater = FuelGasPriceUpdater<
FuelL2BlockSource<ConsensusParametersProvider>,
MetadataStorageAdapter,
FuelDaSource,
DaBlockCostsSharedState,
>;

pub struct InitializeTask {
Expand All @@ -69,19 +74,13 @@ pub struct InitializeTask {
pub on_chain_db: Database<OnChain, RegularStage<OnChain>>,
pub block_stream: BoxStream<SharedImportResult>,
pub shared_algo: SharedGasPriceAlgo<Algorithm>,
pub da_block_costs_provider: DaBlockCostsProvider<DummyDaBlockCosts>,
}

type MetadataStorageAdapter =
StructuredStorage<Database<GasPriceDatabase, RegularStage<GasPriceDatabase>>>;

type Task = GasPriceService<
Algorithm,
FuelGasPriceUpdater<
FuelL2BlockSource<ConsensusParametersProvider>,
MetadataStorageAdapter,
FuelDaSource,
>,
>;
type Task = GasPriceService<Algorithm, Updater>;

impl InitializeTask {
pub fn new(
Expand All @@ -99,6 +98,12 @@ impl InitializeTask {
let default_metadata = get_default_metadata(&config, latest_block_height);
let algo = get_best_algo(&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 =
DummyDaBlockCosts::new(Err(anyhow::anyhow!("Not used")));
let da_block_costs_provider =
DaBlockCostsProvider::new(da_block_costs_source, None);

let task = Self {
config,
genesis_block_height,
Expand All @@ -107,6 +112,7 @@ impl InitializeTask {
on_chain_db,
block_stream,
shared_algo,
da_block_costs_provider,
};
Ok(task)
}
Expand Down Expand Up @@ -168,7 +174,13 @@ impl RunnableService for InitializeTask {
self.gas_price_db,
self.on_chain_db,
self.block_stream,
self.da_block_costs_provider.shared_state,
)?;

self.da_block_costs_provider
.service
.start_and_await()
.await?;
let inner_service =
GasPriceService::new(starting_height, updater, self.shared_algo).await;
Ok(inner_service)
Expand All @@ -182,6 +194,7 @@ pub fn get_synced_gas_price_updater(
mut gas_price_db: Database<GasPriceDatabase, RegularStage<GasPriceDatabase>>,
on_chain_db: Database<OnChain, RegularStage<OnChain>>,
block_stream: BoxStream<SharedImportResult>,
da_block_costs: DaBlockCostsSharedState,
) -> anyhow::Result<Updater> {
let mut first_run = false;
let latest_block_height: u32 = on_chain_db
Expand Down Expand Up @@ -217,7 +230,7 @@ pub fn get_synced_gas_price_updater(
default_metadata.into(),
l2_block_source,
metadata_storage,
FuelDaSource,
da_block_costs,
);
Ok(updater)
} else {
Expand All @@ -235,7 +248,7 @@ pub fn get_synced_gas_price_updater(
latest_block_height.into(),
l2_block_source,
metadata_storage,
FuelDaSource,
da_block_costs,
config.min_gas_price,
config.gas_price_change_percent,
config.gas_price_threshold_percent,
Expand Down
1 change: 1 addition & 0 deletions crates/services/gas_price_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fuel-core-types = { workspace = true, features = ["std"] }
fuel-gas-price-algorithm = { workspace = true }
futures = { workspace = true }
num_enum = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
serde = { workspace = true }
strum = { workspace = true, features = ["derive"] }
strum_macros = { workspace = true }
Expand Down
38 changes: 19 additions & 19 deletions crates/services/gas_price_service/src/fuel_gas_price_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ pub use fuel_gas_price_algorithm::{
mod tests;

pub mod fuel_core_storage_adapter;
pub mod fuel_da_source_adapter;

pub struct FuelGasPriceUpdater<L2, Metadata, DaSource> {
pub mod da_source_adapter;

pub struct FuelGasPriceUpdater<L2, Metadata, DaBlockCosts> {
inner: AlgorithmUpdater,
l2_block_source: L2,
metadata_storage: Metadata,
#[allow(dead_code)]
da_source: DaSource,
da_block_costs: DaBlockCosts,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -53,18 +54,18 @@ impl AlgorithmUpdater {
}
}

impl<L2, Metadata, DaSource> FuelGasPriceUpdater<L2, Metadata, DaSource> {
impl<L2, Metadata, DaBlockCosts> FuelGasPriceUpdater<L2, Metadata, DaBlockCosts> {
pub fn new(
inner: AlgorithmUpdater,
l2_block_source: L2,
metadata_storage: Metadata,
da_source: DaSource,
da_block_costs: DaBlockCosts,
) -> Self {
Self {
inner,
l2_block_source,
metadata_storage,
da_source,
da_block_costs,
}
}
}
Expand Down Expand Up @@ -113,16 +114,15 @@ pub trait L2BlockSource: Send + Sync {
async fn get_l2_block(&mut self, height: BlockHeight) -> Result<BlockInfo>;
}

#[derive(Debug, Default)]
pub struct DaCommitDetails {
#[derive(Debug, Default, Clone, Eq, Hash, PartialEq)]
pub struct DaBlockCosts {
pub l2_block_range: core::ops::Range<u32>,
pub blob_size_bytes: u32,
pub blob_cost_wei: u32,
pub partial_block_heights: Option<[u32; 2]>,
pub blob_cost_wei: u128,
}

pub trait DaCommitSource: Send + Sync {
fn get_da_commit_details(&mut self) -> Result<Option<DaCommitDetails>>;
pub trait GetDaBlockCosts: Send + Sync {
fn get(&self) -> Result<Option<DaBlockCosts>>;
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -206,16 +206,16 @@ pub trait MetadataStorage: Send + Sync {
fn set_metadata(&mut self, metadata: UpdaterMetadata) -> Result<()>;
}

impl<L2, Metadata, DaSource> FuelGasPriceUpdater<L2, Metadata, DaSource>
impl<L2, Metadata, DaBlockCosts> FuelGasPriceUpdater<L2, Metadata, DaBlockCosts>
where
Metadata: MetadataStorage,
DaSource: DaCommitSource,
DaBlockCosts: GetDaBlockCosts,
{
pub fn init(
target_block_height: BlockHeight,
l2_block_source: L2,
metadata_storage: Metadata,
da_source: DaSource,
da_block_costs: DaBlockCosts,
min_exec_gas_price: u64,
exec_gas_price_change_percent: u64,
l2_block_fullness_threshold_percent: u64,
Expand All @@ -242,7 +242,7 @@ where
inner,
l2_block_source,
metadata_storage,
da_source,
da_block_costs,
};
Ok(updater)
}
Expand Down Expand Up @@ -306,12 +306,12 @@ where
}

#[async_trait::async_trait]
impl<L2, Metadata, DaSource> UpdateAlgorithm
for FuelGasPriceUpdater<L2, Metadata, DaSource>
impl<L2, Metadata, DaBlockCosts> UpdateAlgorithm
for FuelGasPriceUpdater<L2, Metadata, DaBlockCosts>
where
L2: L2BlockSource,
Metadata: MetadataStorage + Send + Sync,
DaSource: DaCommitSource,
DaBlockCosts: GetDaBlockCosts,
{
type Algorithm = Algorithm;

Expand Down
Loading

0 comments on commit 8c67681

Please sign in to comment.