Skip to content

Commit 3f1b469

Browse files
committed
chore(gas_price_service): split into v0 and v1 and squash FuelGasPriceUpdater type into GasPriceService (#2256)
> [!WARNING] > 🚧🚧 This is PR 6/n in refactoring the gas price service. Now that the `algorithm_updater` is a part of `fuel-core-gas-price-service` we have squashed it into the `GasPriceService` using the `UninitializedTask` struct. We don't implement `RunnableService` *or* `RunnableTask` for the `UninitializedTask` struct, merely use it as a wrapper to generate the `GasPriceServiceV0` 🚧🚧 ## Linked Issues/PRs <!-- List of related issues/PRs --> - FuelLabs/fuel-core#2246 - FuelLabs/fuel-core#2245 - FuelLabs/fuel-core#2230 - FuelLabs/fuel-core#2226 - FuelLabs/fuel-core#2224 - FuelLabs/fuel-core#2192 ## Description <!-- List of detailed changes --> - created common module containing storage adapter, l2 block source and some updater metadata stuff (linked to storage) - created v0 module containing impl of GasPriceAlgorithm for AlgorithmV0, and V0metadata stuff - created v1 module containing impl of GasPriceAlgorithm for AlgorithmV1 & da block costs source (V1Metadata didn’t exist before so i didn’t create it) - fuel-gas-price-updater will be nuked, an UninitializedGasPriceServiceV(x) for each version that takes in all the args needed ```mermaid graph TD A["lib.rs (entry point)"] B[common] B1[utils] B2[storage] B3[l2_block_source] C[ports] D[v0] E[v1] F[v0/algorithm] G[v1/algorithm] H[v0/service] I[v1/da_source] J[v0/metadata] K[v1/service] L[v0/uninitialized] A --> B B --> B1 B --> B2 B --> B3 B --> C C --> D C --> E D --> F D --> H D --> J D --> L E --> G E --> I E --> K F --> H G --> I H --> J J --> L L --> M[SharedV0Algorithm] L --> N[GasPriceServiceV0] subgraph Common B B1 B2 B3 end subgraph Ports C end subgraph V0 D F H J L end subgraph V1 E G I K end ``` ## 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? ---------
1 parent 20ffce8 commit 3f1b469

35 files changed

+1276
-1354
lines changed

crates/fuel-core/src/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub use fuel_core_database::Error;
6565
pub type Result<T> = core::result::Result<T, Error>;
6666

6767
// TODO: Extract `Database` and all belongs into `fuel-core-database`.
68+
use crate::database::database_description::gas_price::GasPriceDatabase;
6869
#[cfg(feature = "rocksdb")]
6970
use crate::state::{
7071
historical_rocksdb::{
@@ -74,10 +75,9 @@ use crate::state::{
7475
},
7576
rocks_db::RocksDb,
7677
};
78+
use fuel_core_gas_price_service::common::fuel_core_storage_adapter::storage::GasPriceMetadata;
7779
#[cfg(feature = "rocksdb")]
7880
use std::path::Path;
79-
use fuel_core_gas_price_service::fuel_gas_price_updater::fuel_core_storage_adapter::storage::GasPriceMetadata;
80-
use crate::database::database_description::gas_price::GasPriceDatabase;
8181

8282
// Storages implementation
8383
pub mod balances;

crates/fuel-core/src/database/database_description/gas_price.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use fuel_core_gas_price_service::fuel_gas_price_updater::fuel_core_storage_adapter::storage::GasPriceColumn;
2-
use fuel_core_types::fuel_types::BlockHeight;
31
use crate::database::database_description::DatabaseDescription;
2+
use fuel_core_gas_price_service::common::fuel_core_storage_adapter::storage::GasPriceColumn;
3+
use fuel_core_types::fuel_types::BlockHeight;
44

55
#[derive(Clone, Copy, Debug)]
66
pub struct GasPriceDatabase;

crates/fuel-core/src/service/adapters/fuel_gas_price_provider.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::fuel_core_graphql_api::ports::GasPriceEstimate as GraphqlGasPriceEstimate;
2-
use fuel_core_gas_price_service::{
2+
use fuel_core_gas_price_service::common::gas_price_algorithm::{
33
GasPriceAlgorithm,
44
SharedGasPriceAlgo,
55
};
6+
67
use fuel_core_producer::block_producer::gas_price::GasPriceProvider as ProducerGasPriceProvider;
78
use fuel_core_txpool::{
89
ports::GasPriceProvider as TxPoolGasPriceProvider,

crates/fuel-core/src/service/adapters/fuel_gas_price_provider/tests/producer_gas_price_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::service::adapters::fuel_gas_price_provider::tests::build_provider;
22
use fuel_core_gas_price_service::{
3+
common::gas_price_algorithm::GasPriceAlgorithm,
34
static_updater::StaticAlgorithm,
4-
GasPriceAlgorithm,
55
};
66

77
#[tokio::test]

crates/fuel-core/src/service/adapters/fuel_gas_price_provider/tests/tx_pool_gas_price_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::service::adapters::fuel_gas_price_provider::tests::build_provider;
22
use fuel_core_gas_price_service::{
3+
common::gas_price_algorithm::GasPriceAlgorithm,
34
static_updater::StaticAlgorithm,
4-
GasPriceAlgorithm,
55
};
66

77
#[tokio::test]

crates/fuel-core/src/service/adapters/gas_price_adapters.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ use crate::{
33
service::adapters::ConsensusParametersProvider,
44
};
55
use fuel_core_gas_price_service::{
6-
fuel_gas_price_updater::{
6+
common::{
77
fuel_core_storage_adapter::{
88
GasPriceSettings,
99
GasPriceSettingsProvider,
1010
},
11-
Error as GasPriceError,
12-
Result as GasPriceResult,
11+
utils::{
12+
Error as GasPriceError,
13+
Result as GasPriceResult,
14+
},
1315
},
1416
ports::{
1517
GasPriceData,

crates/fuel-core/src/service/sub_services.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,14 @@ use crate::{
3838
SubServices,
3939
},
4040
};
41-
#[allow(unused_imports)]
42-
use fuel_core_gas_price_service::fuel_gas_price_updater::{
43-
algorithm_updater,
44-
fuel_core_storage_adapter::FuelL2BlockSource,
45-
Algorithm,
41+
use fuel_core_gas_price_service::v0::uninitialized_task::{
42+
new_gas_price_service_v0,
4643
AlgorithmV0,
47-
FuelGasPriceUpdater,
48-
UpdaterMetadata,
49-
V0Metadata,
5044
};
5145
use fuel_core_poa::{
5246
signer::SignMode,
5347
Trigger,
5448
};
55-
use fuel_core_services::{
56-
RunnableService,
57-
ServiceRunner,
58-
};
5949
use fuel_core_storage::{
6050
self,
6151
transactional::AtomicView,
@@ -79,15 +69,15 @@ pub type TxPoolSharedState = fuel_core_txpool::service::SharedState<
7969
P2PAdapter,
8070
Database,
8171
ExecutorAdapter,
82-
FuelGasPriceProvider<Algorithm>,
72+
FuelGasPriceProvider<AlgorithmV0>,
8373
ConsensusParametersProvider,
8474
SharedMemoryPool,
8575
>;
8676
pub type BlockProducerService = fuel_core_producer::block_producer::Producer<
8777
Database,
8878
TxPoolAdapter,
8979
ExecutorAdapter,
90-
FuelGasPriceProvider<Algorithm>,
80+
FuelGasPriceProvider<AlgorithmV0>,
9181
ConsensusParametersProvider,
9282
>;
9383

@@ -199,18 +189,17 @@ pub fn init_sub_services(
199189
let settings = consensus_parameters_provider.clone();
200190
let block_stream = importer_adapter.events_shared_result();
201191

202-
let gas_price_init = algorithm_updater::InitializeTask::new(
192+
let gas_price_service_v0 = new_gas_price_service_v0(
203193
config.clone().into(),
204194
genesis_block_height,
205195
settings,
206196
block_stream,
207197
database.gas_price().clone(),
208198
database.on_chain().clone(),
209199
)?;
210-
let next_algo = gas_price_init.shared_data();
211-
let gas_price_service = ServiceRunner::new(gas_price_init);
212200

213-
let gas_price_provider = FuelGasPriceProvider::new(next_algo);
201+
let gas_price_provider =
202+
FuelGasPriceProvider::new(gas_price_service_v0.shared.clone());
214203
let txpool = fuel_core_txpool::new_service(
215204
config.txpool.clone(),
216205
database.on_chain().clone(),
@@ -345,7 +334,7 @@ pub fn init_sub_services(
345334
#[allow(unused_mut)]
346335
// `FuelService` starts and shutdowns all sub-services in the `services` order
347336
let mut services: SubServices = vec![
348-
Box::new(gas_price_service),
337+
Box::new(gas_price_service_v0),
349338
Box::new(txpool),
350339
Box::new(consensus_parameters_provider_service),
351340
];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod fuel_core_storage_adapter;
2+
pub mod gas_price_algorithm;
3+
pub mod l2_block_source;
4+
pub mod updater_metadata;
5+
pub mod utils;
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
use crate::fuel_gas_price_updater::{
1+
use crate::common::utils::{
22
BlockInfo,
33
Error as GasPriceError,
4-
Error,
5-
L2BlockSource,
6-
Result,
74
Result as GasPriceResult,
8-
UpdaterMetadata,
95
};
106
use anyhow::anyhow;
11-
use fuel_core_services::stream::BoxStream;
127
use fuel_core_types::fuel_types::BlockHeight;
138

149
use crate::{
15-
fuel_gas_price_updater::fuel_core_storage_adapter::storage::{
16-
GasPriceColumn,
17-
GasPriceMetadata,
10+
common::{
11+
fuel_core_storage_adapter::storage::{
12+
GasPriceColumn,
13+
GasPriceMetadata,
14+
},
15+
updater_metadata::UpdaterMetadata,
1816
},
1917
ports::MetadataStorage,
2018
};
@@ -40,17 +38,12 @@ use fuel_core_types::{
4038
},
4139
Transaction,
4240
},
43-
services::block_importer::SharedImportResult,
4441
};
4542
use std::cmp::min;
46-
use tokio_stream::StreamExt;
4743

4844
#[cfg(test)]
4945
mod metadata_tests;
5046

51-
#[cfg(test)]
52-
mod l2_source_tests;
53-
5447
pub mod storage;
5548

5649
impl<Storage> MetadataStorage for StructuredStorage<Storage>
@@ -61,53 +54,30 @@ where
6154
fn get_metadata(
6255
&self,
6356
block_height: &BlockHeight,
64-
) -> Result<Option<UpdaterMetadata>> {
57+
) -> GasPriceResult<Option<UpdaterMetadata>> {
6558
let metadata = self
6659
.storage::<GasPriceMetadata>()
6760
.get(block_height)
68-
.map_err(|err| Error::CouldNotFetchMetadata {
61+
.map_err(|err| GasPriceError::CouldNotFetchMetadata {
6962
source_error: err.into(),
7063
})?;
7164
Ok(metadata.map(|inner| inner.into_owned()))
7265
}
7366

74-
fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> Result<()> {
67+
fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> GasPriceResult<()> {
7568
let block_height = metadata.l2_block_height();
7669
let mut tx = self.write_transaction();
7770
tx.storage_as_mut::<GasPriceMetadata>()
7871
.insert(&block_height, metadata)
79-
.map_err(|err| Error::CouldNotSetMetadata {
72+
.and_then(|_| tx.commit())
73+
.map_err(|err| GasPriceError::CouldNotSetMetadata {
8074
block_height,
8175
source_error: err.into(),
8276
})?;
83-
tx.commit().map_err(|err| Error::CouldNotSetMetadata {
84-
block_height,
85-
source_error: err.into(),
86-
})?;
8777
Ok(())
8878
}
8979
}
9080

91-
pub struct FuelL2BlockSource<Settings> {
92-
genesis_block_height: BlockHeight,
93-
gas_price_settings: Settings,
94-
committed_block_stream: BoxStream<SharedImportResult>,
95-
}
96-
97-
impl<Settings> FuelL2BlockSource<Settings> {
98-
pub fn new(
99-
genesis_block_height: BlockHeight,
100-
gas_price_settings: Settings,
101-
committed_block_stream: BoxStream<SharedImportResult>,
102-
) -> Self {
103-
Self {
104-
genesis_block_height,
105-
gas_price_settings,
106-
committed_block_stream,
107-
}
108-
}
109-
}
110-
11181
#[derive(Debug, Clone, PartialEq)]
11282
pub struct GasPriceSettings {
11383
pub gas_price_factor: u64,
@@ -117,7 +87,7 @@ pub trait GasPriceSettingsProvider: Send + Sync + Clone {
11787
fn settings(
11888
&self,
11989
param_version: &ConsensusParametersVersion,
120-
) -> Result<GasPriceSettings>;
90+
) -> GasPriceResult<GasPriceSettings>;
12191
}
12292

12393
pub fn get_block_info(
@@ -126,9 +96,7 @@ pub fn get_block_info(
12696
block_gas_limit: u64,
12797
) -> GasPriceResult<BlockInfo> {
12898
let (fee, gas_price) = mint_values(block)?;
129-
let height = *block.header().height();
130-
let used_gas =
131-
block_used_gas(height, fee, gas_price, gas_price_factor, block_gas_limit)?;
99+
let used_gas = block_used_gas(fee, gas_price, gas_price_factor, block_gas_limit)?;
132100
let info = BlockInfo::Block {
133101
height: (*block.header().height()).into(),
134102
gas_used: used_gas,
@@ -143,13 +111,11 @@ fn mint_values(block: &Block<Transaction>) -> GasPriceResult<(u64, u64)> {
143111
.last()
144112
.and_then(|tx| tx.as_mint())
145113
.ok_or(GasPriceError::CouldNotFetchL2Block {
146-
block_height: *block.header().height(),
147114
source_error: anyhow!("Block has no mint transaction"),
148115
})?;
149116
Ok((*mint.mint_amount(), *mint.gas_price()))
150117
}
151118
fn block_used_gas(
152-
block_height: BlockHeight,
153119
fee: u64,
154120
gas_price: u64,
155121
gas_price_factor: u64,
@@ -158,7 +124,6 @@ fn block_used_gas(
158124
let scaled_fee =
159125
fee.checked_mul(gas_price_factor)
160126
.ok_or(GasPriceError::CouldNotFetchL2Block {
161-
block_height,
162127
source_error: anyhow!(
163128
"Failed to scale fee by gas price factor, overflow"
164129
),
@@ -168,41 +133,3 @@ fn block_used_gas(
168133
let used_gas = min(approximate, max_used_gas);
169134
Ok(used_gas)
170135
}
171-
172-
#[async_trait::async_trait]
173-
impl<Settings> L2BlockSource for FuelL2BlockSource<Settings>
174-
where
175-
Settings: GasPriceSettingsProvider + Send + Sync,
176-
{
177-
async fn get_l2_block(&mut self, height: BlockHeight) -> GasPriceResult<BlockInfo> {
178-
let block = &self
179-
.committed_block_stream
180-
.next()
181-
.await
182-
.ok_or({
183-
GasPriceError::CouldNotFetchL2Block {
184-
block_height: height,
185-
source_error: anyhow!("No committed block found"),
186-
}
187-
})?
188-
.sealed_block
189-
.entity;
190-
191-
match block.header().height().cmp(&self.genesis_block_height) {
192-
std::cmp::Ordering::Less => Err(GasPriceError::CouldNotFetchL2Block {
193-
block_height: height,
194-
source_error: anyhow!("Block precedes expected genesis block height"),
195-
}),
196-
std::cmp::Ordering::Equal => Ok(BlockInfo::GenesisBlock),
197-
std::cmp::Ordering::Greater => {
198-
let param_version = block.header().consensus_parameters_version;
199-
200-
let GasPriceSettings {
201-
gas_price_factor,
202-
block_gas_limit,
203-
} = self.gas_price_settings.settings(&param_version)?;
204-
get_block_info(block, gas_price_factor, block_gas_limit)
205-
}
206-
}
207-
}
208-
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#![allow(non_snake_case)]
22

33
use super::*;
4-
use crate::fuel_gas_price_updater::{
5-
fuel_core_storage_adapter::storage::GasPriceColumn,
6-
AlgorithmUpdater,
7-
UpdaterMetadata,
8-
};
94
use fuel_core_storage::{
105
structured_storage::test::InMemoryStorage,
116
transactional::{
@@ -28,7 +23,7 @@ fn arb_metadata_with_l2_height(l2_height: BlockHeight) -> UpdaterMetadata {
2823
l2_block_height: l2_height.into(),
2924
l2_block_fullness_threshold_percent: 0,
3025
};
31-
AlgorithmUpdater::V0(inner).into()
26+
inner.into()
3227
}
3328

3429
fn database() -> StorageTransaction<InMemoryStorage<GasPriceColumn>> {

0 commit comments

Comments
 (0)