Skip to content

Commit b7e89e9

Browse files
authored
Add syncronization for Gas Price Database and On Chain Database on startup (#2041)
Closes #2030 In the unlikely situation that the metadata db is behind the on chain database, we should apply the values from each block to the algorithm updater to produce reasonable values. For now, we should be able to use the updater deterministically, but this might become more difficult as we include the DA values in the updater. We also define the behavior for when the gas price db is beyond the on-chain db. For now, we are assuming that the blocks produced with the gas prices in the db were either reverted or not properly committed to the on_chain db, and we just revert the gas price db to the height of the on chain db. i.e. **the on chain db is the source of truth.** ## Checklist - [ ] Breaking changes are clearly marked as such in the PR description and changelog - [ ] New behavior is reflected in tests - [ ] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [ ] 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 25f269e commit b7e89e9

File tree

19 files changed

+558
-82
lines changed

19 files changed

+558
-82
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717
- "gas-price-threshold-percent" - the threshold percent for determining if the gas price will be increase or decreased
1818
And the following CLI flags are serving a new purpose
1919
- "min-gas-price" - the minimum gas price that the gas price algorithm will return
20+
- [2041](https://github.com/FuelLabs/fuel-core/pull/2041): Add code for startup of the gas price algorithm updater so
21+
the gas price db on startup is always in sync with the on chain db
22+
2023
## [Version 0.31.0]
2124

2225
### Added

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/fuel-core/src/graphql_api/ports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub trait P2pPort: Send + Sync {
236236
#[async_trait::async_trait]
237237
pub trait GasPriceEstimate: Send + Sync {
238238
/// The worst case scenario for gas price at a given horizon
239-
async fn worst_case_gas_price(&self, height: BlockHeight) -> u64;
239+
async fn worst_case_gas_price(&self, height: BlockHeight) -> Option<u64>;
240240
}
241241

242242
/// Trait for getting VM memory.

crates/fuel-core/src/schema/gas_price.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ impl EstimateGasPriceQuery {
105105
let gas_price_provider = ctx.data_unchecked::<GasPriceProvider>();
106106
let gas_price = gas_price_provider
107107
.worst_case_gas_price(target_block.into())
108-
.await;
108+
.await
109+
.ok_or(async_graphql::Error::new(format!(
110+
"Failed to estimate gas price for block, algorithm not yet set: {target_block:?}"
111+
)))?;
109112

110113
Ok(EstimateGasPrice {
111114
gas_price: gas_price.into(),

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use fuel_core_producer::block_producer::gas_price::GasPriceProvider as ProducerG
77
use fuel_core_txpool::ports::GasPriceProvider as TxPoolGasPriceProvider;
88
use fuel_core_types::{
99
fuel_types::BlockHeight,
10-
services::txpool::Result as TxPoolResult,
10+
services::txpool::{
11+
Error as TxPoolError,
12+
Result as TxPoolResult,
13+
},
1114
};
1215

1316
pub type Result<T, E = Error> = std::result::Result<T, E>;
@@ -53,7 +56,7 @@ impl<A> FuelGasPriceProvider<A>
5356
where
5457
A: GasPriceAlgorithm + Send + Sync,
5558
{
56-
async fn next_gas_price(&self) -> u64 {
59+
async fn next_gas_price(&self) -> Option<u64> {
5760
self.algorithm.next_gas_price().await
5861
}
5962
}
@@ -64,7 +67,9 @@ where
6467
A: GasPriceAlgorithm + Send + Sync,
6568
{
6669
async fn next_gas_price(&self) -> anyhow::Result<u64> {
67-
Ok(self.next_gas_price().await)
70+
self.next_gas_price()
71+
.await
72+
.ok_or(anyhow::anyhow!("No gas price available"))
6873
}
6974
}
7075

@@ -74,7 +79,11 @@ where
7479
A: GasPriceAlgorithm + Send + Sync,
7580
{
7681
async fn next_gas_price(&self) -> TxPoolResult<u64> {
77-
Ok(self.next_gas_price().await)
82+
self.next_gas_price()
83+
.await
84+
.ok_or(TxPoolError::GasPriceNotFound(
85+
"Gas price not set yet".to_string(),
86+
))
7887
}
7988
}
8089

@@ -83,7 +92,7 @@ impl<A> GraphqlGasPriceEstimate for FuelGasPriceProvider<A>
8392
where
8493
A: GasPriceAlgorithm + Send + Sync,
8594
{
86-
async fn worst_case_gas_price(&self, height: BlockHeight) -> u64 {
95+
async fn worst_case_gas_price(&self, height: BlockHeight) -> Option<u64> {
8796
self.algorithm.worst_case_gas_price(height).await
8897
}
8998
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn build_provider<A>(algorithm: A) -> FuelGasPriceProvider<A>
1515
where
1616
A: Send + Sync,
1717
{
18-
let algorithm = SharedGasPriceAlgo::new(algorithm);
18+
let algorithm = SharedGasPriceAlgo::new_with_algorithm(algorithm);
1919
FuelGasPriceProvider::new(algorithm)
2020
}
2121

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ async fn estimate_gas_price__happy_path() {
1111

1212
// when
1313
let expected_price = algo.worst_case_gas_price(next_height);
14-
let actual_price = gas_price_provider.worst_case_gas_price(next_height).await;
14+
let actual_price = gas_price_provider
15+
.worst_case_gas_price(next_height)
16+
.await
17+
.unwrap();
1518

1619
// then
1720
assert_eq!(expected_price, actual_price);

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
@@ -13,7 +13,7 @@ async fn gas_price__if_requested_block_height_is_latest_return_gas_price() {
1313

1414
// when
1515
let expected_price = algo.next_gas_price();
16-
let actual_price = gas_price_provider.next_gas_price().await;
16+
let actual_price = gas_price_provider.next_gas_price().await.unwrap();
1717

1818
// then
1919
assert_eq!(expected_price, actual_price);

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
@@ -13,7 +13,7 @@ async fn gas_price__if_requested_block_height_is_latest_return_gas_price() {
1313

1414
// when
1515
let expected_price = algo.next_gas_price();
16-
let actual_price = gas_price_provider.next_gas_price().await;
16+
let actual_price = gas_price_provider.next_gas_price().await.unwrap();
1717

1818
// then
1919
assert_eq!(expected_price, actual_price);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ impl worker::TxPool for TxPoolAdapter {
165165

166166
#[async_trait::async_trait]
167167
impl GasPriceEstimate for StaticGasPrice {
168-
async fn worst_case_gas_price(&self, _height: BlockHeight) -> u64 {
169-
self.gas_price
168+
async fn worst_case_gas_price(&self, _height: BlockHeight) -> Option<u64> {
169+
Some(self.gas_price)
170170
}
171171
}
172172

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(clippy::let_unit_value)]
2+
23
use super::{
34
adapters::P2PAdapter,
45
genesis::create_genesis_block,
@@ -32,23 +33,31 @@ use crate::{
3233
SubServices,
3334
},
3435
};
36+
#[allow(unused_imports)]
3537
use fuel_core_gas_price_service::fuel_gas_price_updater::{
3638
fuel_core_storage_adapter::FuelL2BlockSource,
3739
Algorithm,
40+
AlgorithmV0,
3841
FuelGasPriceUpdater,
3942
UpdaterMetadata,
4043
V0Metadata,
4144
};
4245
use fuel_core_poa::Trigger;
46+
use fuel_core_services::{
47+
RunnableService,
48+
ServiceRunner,
49+
};
4350
use fuel_core_storage::{
44-
structured_storage::StructuredStorage,
51+
self,
4552
transactional::AtomicView,
4653
};
4754
#[cfg(feature = "relayer")]
4855
use fuel_core_types::blockchain::primitives::DaBlockHeight;
4956
use std::sync::Arc;
5057
use tokio::sync::Mutex;
5158

59+
mod algorithm_updater;
60+
5261
pub type PoAService =
5362
fuel_core_poa::Service<TxPoolAdapter, BlockProducerAdapter, BlockImporterAdapter>;
5463
#[cfg(feature = "p2p")]
@@ -176,24 +185,20 @@ pub fn init_sub_services(
176185
#[cfg(not(feature = "p2p"))]
177186
let p2p_adapter = P2PAdapter::new();
178187

179-
let updater_metadata = UpdaterMetadata::V0(V0Metadata {
180-
new_exec_price: config.starting_gas_price,
181-
min_exec_gas_price: config.min_gas_price,
182-
exec_gas_price_change_percent: config.gas_price_change_percent,
183-
l2_block_height: last_height.into(),
184-
l2_block_fullness_threshold_percent: config.gas_price_threshold_percent,
185-
});
186188
let genesis_block_height = *genesis_block.header().height();
187189
let settings = consensus_parameters_provider.clone();
188190
let block_stream = importer_adapter.events_shared_result();
189-
let l2_block_source =
190-
FuelL2BlockSource::new(genesis_block_height, settings, block_stream);
191-
let metadata_storage = StructuredStorage::new(database.gas_price().clone());
192-
let update_algo =
193-
FuelGasPriceUpdater::init(updater_metadata, l2_block_source, metadata_storage)?;
194-
let gas_price_service =
195-
fuel_core_gas_price_service::new_service(last_height, update_algo)?;
196-
let next_algo = gas_price_service.shared.clone();
191+
192+
let gas_price_init = algorithm_updater::InitializeTask::new(
193+
config.clone(),
194+
genesis_block_height,
195+
settings,
196+
block_stream,
197+
database.gas_price().clone(),
198+
database.on_chain().clone(),
199+
)?;
200+
let next_algo = gas_price_init.shared_data();
201+
let gas_price_service = ServiceRunner::new(gas_price_init);
197202

198203
let gas_price_provider = FuelGasPriceProvider::new(next_algo);
199204
let txpool = fuel_core_txpool::new_service(

0 commit comments

Comments
 (0)