Skip to content

Commit

Permalink
chore(gas_price_service): split into 2 concrete types, v0 and v1
Browse files Browse the repository at this point in the history
fix: remove block height from error
  • Loading branch information
rymnc committed Sep 27, 2024
1 parent 4a55b7d commit 7f895c0
Show file tree
Hide file tree
Showing 34 changed files with 1,076 additions and 1,218 deletions.
4 changes: 2 additions & 2 deletions crates/fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub use fuel_core_database::Error;
pub type Result<T> = core::result::Result<T, Error>;

// TODO: Extract `Database` and all belongs into `fuel-core-database`.
use crate::database::database_description::gas_price::GasPriceDatabase;
#[cfg(feature = "rocksdb")]
use crate::state::{
historical_rocksdb::{
Expand All @@ -74,10 +75,9 @@ use crate::state::{
},
rocks_db::RocksDb,
};
use fuel_core_gas_price_service::common::fuel_core_storage_adapter::storage::GasPriceMetadata;
#[cfg(feature = "rocksdb")]
use std::path::Path;
use fuel_core_gas_price_service::fuel_gas_price_updater::fuel_core_storage_adapter::storage::GasPriceMetadata;
use crate::database::database_description::gas_price::GasPriceDatabase;

// Storages implementation
pub mod balances;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fuel_core_gas_price_service::fuel_gas_price_updater::fuel_core_storage_adapter::storage::GasPriceColumn;
use fuel_core_types::fuel_types::BlockHeight;
use crate::database::database_description::DatabaseDescription;
use fuel_core_gas_price_service::common::fuel_core_storage_adapter::storage::GasPriceColumn;
use fuel_core_types::fuel_types::BlockHeight;

#[derive(Clone, Copy, Debug)]
pub struct GasPriceDatabase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::fuel_core_graphql_api::ports::GasPriceEstimate as GraphqlGasPriceEstimate;
use fuel_core_gas_price_service::{
use fuel_core_gas_price_service::common::gas_price_algorithm::{
GasPriceAlgorithm,
SharedGasPriceAlgo,
};

use fuel_core_producer::block_producer::gas_price::GasPriceProvider as ProducerGasPriceProvider;
use fuel_core_txpool::{
ports::GasPriceProvider as TxPoolGasPriceProvider,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::service::adapters::fuel_gas_price_provider::tests::build_provider;
use fuel_core_gas_price_service::{
common::gas_price_algorithm::GasPriceAlgorithm,
static_updater::StaticAlgorithm,
GasPriceAlgorithm,
};

#[tokio::test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::service::adapters::fuel_gas_price_provider::tests::build_provider;
use fuel_core_gas_price_service::{
common::gas_price_algorithm::GasPriceAlgorithm,
static_updater::StaticAlgorithm,
GasPriceAlgorithm,
};

#[tokio::test]
Expand Down
8 changes: 5 additions & 3 deletions crates/fuel-core/src/service/adapters/gas_price_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use crate::{
service::adapters::ConsensusParametersProvider,
};
use fuel_core_gas_price_service::{
fuel_gas_price_updater::{
common::{
fuel_core_storage_adapter::{
GasPriceSettings,
GasPriceSettingsProvider,
},
Error as GasPriceError,
Result as GasPriceResult,
utils::{
Error as GasPriceError,
Result as GasPriceResult,
},
},
ports::{
GasPriceData,
Expand Down
27 changes: 8 additions & 19 deletions crates/fuel-core/src/service/sub_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,14 @@ use crate::{
SubServices,
},
};
#[allow(unused_imports)]
use fuel_core_gas_price_service::fuel_gas_price_updater::{
algorithm_updater,
fuel_core_storage_adapter::FuelL2BlockSource,
Algorithm,
use fuel_core_gas_price_service::v0::uninitialized_task::{
new_gas_price_service_v0,
AlgorithmV0,
FuelGasPriceUpdater,
UpdaterMetadata,
V0Metadata,
};
use fuel_core_poa::{
signer::SignMode,
Trigger,
};
use fuel_core_services::{
RunnableService,
ServiceRunner,
};
use fuel_core_storage::{
self,
transactional::AtomicView,
Expand All @@ -79,15 +69,15 @@ pub type TxPoolSharedState = fuel_core_txpool::service::SharedState<
P2PAdapter,
Database,
ExecutorAdapter,
FuelGasPriceProvider<Algorithm>,
FuelGasPriceProvider<AlgorithmV0>,
ConsensusParametersProvider,
SharedMemoryPool,
>;
pub type BlockProducerService = fuel_core_producer::block_producer::Producer<
Database,
TxPoolAdapter,
ExecutorAdapter,
FuelGasPriceProvider<Algorithm>,
FuelGasPriceProvider<AlgorithmV0>,
ConsensusParametersProvider,
>;

Expand Down Expand Up @@ -199,18 +189,17 @@ pub fn init_sub_services(
let settings = consensus_parameters_provider.clone();
let block_stream = importer_adapter.events_shared_result();

let gas_price_init = algorithm_updater::InitializeTask::new(
let gas_price_service_v0 = new_gas_price_service_v0(
config.clone().into(),
genesis_block_height,
settings,
block_stream,
database.gas_price().clone(),
database.on_chain().clone(),
)?;
let next_algo = gas_price_init.shared_data();
let gas_price_service = ServiceRunner::new(gas_price_init);

let gas_price_provider = FuelGasPriceProvider::new(next_algo);
let gas_price_provider =
FuelGasPriceProvider::new(gas_price_service_v0.shared.clone());
let txpool = fuel_core_txpool::new_service(
config.txpool.clone(),
database.on_chain().clone(),
Expand Down Expand Up @@ -345,7 +334,7 @@ pub fn init_sub_services(
#[allow(unused_mut)]
// `FuelService` starts and shutdowns all sub-services in the `services` order
let mut services: SubServices = vec![
Box::new(gas_price_service),
Box::new(gas_price_service_v0),
Box::new(txpool),
Box::new(consensus_parameters_provider_service),
];
Expand Down
5 changes: 5 additions & 0 deletions crates/services/gas_price_service/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod fuel_core_storage_adapter;
pub mod gas_price_algorithm;
pub mod l2_block_source;
pub mod updater_metadata;
pub mod utils;
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use crate::fuel_gas_price_updater::{
use crate::common::utils::{
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,
common::{
fuel_core_storage_adapter::storage::{
GasPriceColumn,
GasPriceMetadata,
},
updater_metadata::UpdaterMetadata,
},
ports::MetadataStorage,
};
Expand All @@ -40,17 +38,12 @@ use fuel_core_types::{
},
Transaction,
},
services::block_importer::SharedImportResult,
};
use std::cmp::min;
use tokio_stream::StreamExt;

#[cfg(test)]
mod metadata_tests;

#[cfg(test)]
mod l2_source_tests;

pub mod storage;

impl<Storage> MetadataStorage for StructuredStorage<Storage>
Expand All @@ -61,53 +54,34 @@ where
fn get_metadata(
&self,
block_height: &BlockHeight,
) -> Result<Option<UpdaterMetadata>> {
) -> GasPriceResult<Option<UpdaterMetadata>> {
let metadata = self
.storage::<GasPriceMetadata>()
.get(block_height)
.map_err(|err| Error::CouldNotFetchMetadata {
.map_err(|err| GasPriceError::CouldNotFetchMetadata {
source_error: err.into(),
})?;
Ok(metadata.map(|inner| inner.into_owned()))
}

fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> Result<()> {
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(&block_height, metadata)
.map_err(|err| Error::CouldNotSetMetadata {
.map_err(|err| GasPriceError::CouldNotSetMetadata {
block_height,
source_error: err.into(),
})?;
tx.commit()
.map_err(|err| GasPriceError::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,
committed_block_stream: BoxStream<SharedImportResult>,
}

impl<Settings> FuelL2BlockSource<Settings> {
pub fn new(
genesis_block_height: BlockHeight,
gas_price_settings: Settings,
committed_block_stream: BoxStream<SharedImportResult>,
) -> Self {
Self {
genesis_block_height,
gas_price_settings,
committed_block_stream,
}
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct GasPriceSettings {
pub gas_price_factor: u64,
Expand All @@ -117,7 +91,7 @@ pub trait GasPriceSettingsProvider: Send + Sync + Clone {
fn settings(
&self,
param_version: &ConsensusParametersVersion,
) -> Result<GasPriceSettings>;
) -> GasPriceResult<GasPriceSettings>;
}

pub fn get_block_info(
Expand All @@ -126,9 +100,8 @@ pub fn get_block_info(
block_gas_limit: u64,
) -> GasPriceResult<BlockInfo> {
let (fee, gas_price) = mint_values(block)?;
let height = *block.header().height();
let used_gas =
block_used_gas(height, fee, gas_price, gas_price_factor, block_gas_limit)?;
block_used_gas(fee, gas_price, gas_price_factor, block_gas_limit)?;
let info = BlockInfo::Block {
height: (*block.header().height()).into(),
gas_used: used_gas,
Expand All @@ -143,13 +116,11 @@ fn mint_values(block: &Block<Transaction>) -> GasPriceResult<(u64, u64)> {
.last()
.and_then(|tx| tx.as_mint())
.ok_or(GasPriceError::CouldNotFetchL2Block {
block_height: *block.header().height(),
source_error: anyhow!("Block has no mint transaction"),
})?;
Ok((*mint.mint_amount(), *mint.gas_price()))
}
fn block_used_gas(
block_height: BlockHeight,
fee: u64,
gas_price: u64,
gas_price_factor: u64,
Expand All @@ -158,7 +129,6 @@ fn block_used_gas(
let scaled_fee =
fee.checked_mul(gas_price_factor)
.ok_or(GasPriceError::CouldNotFetchL2Block {
block_height,
source_error: anyhow!(
"Failed to scale fee by gas price factor, overflow"
),
Expand All @@ -168,41 +138,3 @@ fn block_used_gas(
let used_gas = min(approximate, max_used_gas);
Ok(used_gas)
}

#[async_trait::async_trait]
impl<Settings> L2BlockSource for FuelL2BlockSource<Settings>
where
Settings: GasPriceSettingsProvider + Send + Sync,
{
async fn get_l2_block(&mut self, height: BlockHeight) -> GasPriceResult<BlockInfo> {
let block = &self
.committed_block_stream
.next()
.await
.ok_or({
GasPriceError::CouldNotFetchL2Block {
block_height: height,
source_error: anyhow!("No committed block found"),
}
})?
.sealed_block
.entity;

match block.header().height().cmp(&self.genesis_block_height) {
std::cmp::Ordering::Less => Err(GasPriceError::CouldNotFetchL2Block {
block_height: height,
source_error: anyhow!("Block precedes expected genesis block height"),
}),
std::cmp::Ordering::Equal => Ok(BlockInfo::GenesisBlock),
std::cmp::Ordering::Greater => {
let param_version = block.header().consensus_parameters_version;

let GasPriceSettings {
gas_price_factor,
block_gas_limit,
} = self.gas_price_settings.settings(&param_version)?;
get_block_info(block, gas_price_factor, block_gas_limit)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#![allow(non_snake_case)]

use super::*;
use crate::fuel_gas_price_updater::{
fuel_core_storage_adapter::storage::GasPriceColumn,
AlgorithmUpdater,
UpdaterMetadata,
};
use fuel_core_storage::{
structured_storage::test::InMemoryStorage,
transactional::{
Expand All @@ -28,7 +23,7 @@ fn arb_metadata_with_l2_height(l2_height: BlockHeight) -> UpdaterMetadata {
l2_block_height: l2_height.into(),
l2_block_fullness_threshold_percent: 0,
};
AlgorithmUpdater::V0(inner).into()
inner.into()
}

fn database() -> StorageTransaction<InMemoryStorage<GasPriceColumn>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::fuel_gas_price_updater::UpdaterMetadata;
use crate::common::updater_metadata::UpdaterMetadata;
use fuel_core_storage::{
blueprint::plain::Plain,
codec::{
Expand All @@ -10,6 +10,7 @@ use fuel_core_storage::{
Mappable,
};
use fuel_core_types::fuel_types::BlockHeight;

#[repr(u32)]
#[derive(
Copy,
Expand Down
Loading

0 comments on commit 7f895c0

Please sign in to comment.