Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gas_price_service): define port for L2 data #2224

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions crates/fuel-core/src/service/adapters/gas_price_adapters.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
use crate::service::adapters::ConsensusParametersProvider;
use fuel_core_gas_price_service::fuel_gas_price_updater::{
fuel_core_storage_adapter::{
GasPriceSettings,
GasPriceSettingsProvider,
use crate::{
database::OnChainIterableKeyValueView,
service::adapters::ConsensusParametersProvider,
};
use fuel_core_gas_price_service::{
fuel_gas_price_updater::{
fuel_core_storage_adapter::{
GasPriceSettings,
GasPriceSettingsProvider,
},
Error as GasPriceError,
Result as GasPriceResult,
},
ports::L2Data,
};
use fuel_core_storage::Result as StorageResult;
use fuel_core_types::{
blockchain::{
block::Block,
header::ConsensusParametersVersion,
},
Error as GasPriceError,
Result as GasPriceResult,
fuel_tx::Transaction,
fuel_types::BlockHeight,
};
use fuel_core_types::blockchain::header::ConsensusParametersVersion;

#[cfg(test)]
mod tests;

impl L2Data for OnChainIterableKeyValueView {
fn latest_height(&self) -> StorageResult<BlockHeight> {
self.latest_height()
}

fn get_block(
&self,
height: &BlockHeight,
) -> StorageResult<Option<Block<Transaction>>> {
self.get_full_block(height)
}
}

impl GasPriceSettingsProvider for ConsensusParametersProvider {
fn settings(
&self,
Expand Down
115 changes: 66 additions & 49 deletions crates/fuel-core/src/service/sub_services/algorithm_updater.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
database::{
database_description::{
gas_price::GasPriceDatabase,
on_chain::OnChain,
},
database_description::gas_price::GasPriceDatabase,
Database,
RegularStage,
},
Expand All @@ -21,18 +18,22 @@ use fuel_core_gas_price_service::{
DaBlockCostsSharedState,
},
fuel_core_storage_adapter::{
get_block_info,
storage::GasPriceMetadata,
FuelL2BlockSource,
GasPriceSettings,
GasPriceSettingsProvider,
},
Algorithm,
AlgorithmUpdater,
AlgorithmUpdaterV0,
BlockInfo,
FuelGasPriceUpdater,
MetadataStorage,
UpdaterMetadata,
V0Metadata,
},
ports::L2Data,
GasPriceService,
SharedGasPriceAlgo,
};
Expand All @@ -43,19 +44,15 @@ use fuel_core_services::{
StateWatcher,
};
use fuel_core_storage::{
not_found,
structured_storage::StructuredStorage,
tables::{
FuelBlocks,
Transactions,
},
transactional::{
AtomicView,
HistoricalView,
},
StorageAsRef,
};
use fuel_core_types::{
fuel_tx::field::MintAmount,
fuel_types::BlockHeight,
services::block_importer::SharedImportResult,
};
Expand All @@ -66,12 +63,12 @@ type Updater = FuelGasPriceUpdater<
DaBlockCostsSharedState,
>;

pub struct InitializeTask {
pub struct InitializeTask<L2DataStoreView> {
pub config: Config,
pub genesis_block_height: BlockHeight,
pub settings: ConsensusParametersProvider,
pub gas_price_db: Database<GasPriceDatabase, RegularStage<GasPriceDatabase>>,
pub on_chain_db: Database<OnChain, RegularStage<OnChain>>,
pub on_chain_db: L2DataStoreView,
pub block_stream: BoxStream<SharedImportResult>,
pub shared_algo: SharedGasPriceAlgo<Algorithm>,
pub da_block_costs_provider: DaBlockCostsProvider<DummyDaBlockCosts>,
Expand All @@ -82,19 +79,22 @@ type MetadataStorageAdapter =

type Task = GasPriceService<Algorithm, Updater>;

impl InitializeTask {
impl<L2DataStore, L2DataStoreView> InitializeTask<L2DataStoreView>
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
{
pub fn new(
config: Config,
genesis_block_height: BlockHeight,
settings: ConsensusParametersProvider,
block_stream: BoxStream<SharedImportResult>,
gas_price_db: Database<GasPriceDatabase, RegularStage<GasPriceDatabase>>,
on_chain_db: Database<OnChain, RegularStage<OnChain>>,
on_chain_db: L2DataStoreView,
) -> anyhow::Result<Self> {
let latest_block_height = on_chain_db
.latest_height()
.unwrap_or(genesis_block_height)
.into();
let view = on_chain_db.latest_view()?;
let latest_block_height =
view.latest_height().unwrap_or(genesis_block_height).into();
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);
Expand Down Expand Up @@ -147,7 +147,11 @@ fn get_best_algo(
Ok(algo)
}
#[async_trait::async_trait]
impl RunnableService for InitializeTask {
impl<L2DataStore, L2DataStoreView> RunnableService for InitializeTask<L2DataStoreView>
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
{
const NAME: &'static str = "GasPriceUpdater";
type SharedData = SharedGasPriceAlgo<Algorithm>;
type Task = Task;
Expand All @@ -162,17 +166,15 @@ impl RunnableService for InitializeTask {
_state_watcher: &StateWatcher,
_params: Self::TaskParams,
) -> anyhow::Result<Self::Task> {
let starting_height = self
.on_chain_db
.latest_height()
.unwrap_or(self.genesis_block_height);
let view = self.on_chain_db.latest_view()?;
let starting_height = view.latest_height().unwrap_or(self.genesis_block_height);

let updater = get_synced_gas_price_updater(
self.config,
self.genesis_block_height,
self.settings,
self.gas_price_db,
self.on_chain_db,
&self.on_chain_db,
self.block_stream,
self.da_block_costs_provider.shared_state,
)?;
Expand All @@ -187,17 +189,22 @@ impl RunnableService for InitializeTask {
}
}

pub fn get_synced_gas_price_updater(
pub fn get_synced_gas_price_updater<L2DataStore, L2DataStoreView>(
config: Config,
genesis_block_height: BlockHeight,
settings: ConsensusParametersProvider,
mut gas_price_db: Database<GasPriceDatabase, RegularStage<GasPriceDatabase>>,
on_chain_db: Database<OnChain, RegularStage<OnChain>>,
on_chain_db: &L2DataStoreView,
block_stream: BoxStream<SharedImportResult>,
da_block_costs: DaBlockCostsSharedState,
) -> anyhow::Result<Updater> {
) -> anyhow::Result<Updater>
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
{
let mut first_run = false;
let latest_block_height: u32 = on_chain_db
.latest_view()?
.latest_height()
.unwrap_or(genesis_block_height)
.into();
Expand Down Expand Up @@ -257,15 +264,19 @@ pub fn get_synced_gas_price_updater(
}
}

fn sync_metadata_storage_with_on_chain_storage(
fn sync_metadata_storage_with_on_chain_storage<L2DataStore, L2DataStoreView>(
settings: &ConsensusParametersProvider,
metadata_storage: &mut StructuredStorage<
Database<GasPriceDatabase, RegularStage<GasPriceDatabase>>,
>,
on_chain_db: Database<OnChain, RegularStage<OnChain>>,
on_chain_db: &L2DataStoreView,
metadata_height: u32,
latest_block_height: u32,
) -> anyhow::Result<()> {
) -> anyhow::Result<()>
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
{
let metadata = metadata_storage
.get_metadata(&metadata_height.into())?
.ok_or(anyhow::anyhow!(
Expand All @@ -290,41 +301,47 @@ fn sync_metadata_storage_with_on_chain_storage(
Ok(())
}

fn sync_v0_metadata(
fn sync_v0_metadata<L2DataStore, L2DataStoreView>(
settings: &ConsensusParametersProvider,
on_chain_db: Database<OnChain, RegularStage<OnChain>>,
on_chain_db: &L2DataStoreView,
metadata_height: u32,
latest_block_height: u32,
updater: &mut AlgorithmUpdaterV0,
metadata_storage: &mut StructuredStorage<
Database<GasPriceDatabase, RegularStage<GasPriceDatabase>>,
>,
) -> anyhow::Result<()> {
) -> anyhow::Result<()>
where
L2DataStore: L2Data,
L2DataStoreView: AtomicView<LatestView = L2DataStore>,
{
let first = metadata_height.saturating_add(1);
let view = on_chain_db.latest_view()?;
for height in first..=latest_block_height {
let block = view
.storage::<FuelBlocks>()
.get(&height.into())?
.ok_or(anyhow::anyhow!("Expected block to exist"))?;
let last_tx_id = block.transactions().last().ok_or(anyhow::anyhow!(
"Expected block to have at least one transaction"
))?;
.get_block(&height.into())?
.ok_or(not_found!("FullBlock"))?;
let param_version = block.header().consensus_parameters_version;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When InitializeTask will be moved into gas-price-service, instead of duplicating logic we can call GasPriceTaskV0::process_l2_block(block) inside of InitializeTask::into_task. Just a reminder to not forget

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's on the roadmap :)

let params = settings.settings(&param_version)?;
let mint = view
.storage::<Transactions>()
.get(last_tx_id)?
.ok_or(anyhow::anyhow!("Expected tx to exist for id: {last_tx_id}"))?
.as_mint()
.ok_or(anyhow::anyhow!("Expected tx to be a mint"))?
.to_owned();
let block_gas_used = mint.mint_amount();
let block_gas_capacity = params.block_gas_limit.try_into()?;
updater.update_l2_block_data(height, *block_gas_used, block_gas_capacity)?;

let GasPriceSettings {
gas_price_factor,
block_gas_limit,
} = settings.settings(&param_version)?;
let block_gas_capacity = block_gas_limit.try_into()?;

let block_gas_used =
match get_block_info(&block, gas_price_factor, block_gas_limit)? {
BlockInfo::GenesisBlock => {
Err(anyhow::anyhow!("should not be genesis block"))?
}
BlockInfo::Block { gas_used, .. } => gas_used,
};

updater.update_l2_block_data(height, block_gas_used, block_gas_capacity)?;
let metadata = AlgorithmUpdater::V0(updater.clone()).into();
metadata_storage.set_metadata(metadata)?;
}

Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub trait GasPriceSettingsProvider {
) -> Result<GasPriceSettings>;
}

fn get_block_info(
pub fn get_block_info(
block: &Block<Transaction>,
gas_price_factor: u64,
block_gas_limit: u64,
Expand Down
1 change: 1 addition & 0 deletions crates/services/gas_price_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tokio::sync::RwLock;
pub mod static_updater;

pub mod fuel_gas_price_updater;
pub mod ports;

/// The service that updates the gas price algorithm.
pub struct GasPriceService<A, U> {
Expand Down
14 changes: 14 additions & 0 deletions crates/services/gas_price_service/src/ports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use fuel_core_storage::Result as StorageResult;
use fuel_core_types::{
blockchain::block::Block,
fuel_tx::Transaction,
fuel_types::BlockHeight,
};

pub trait L2Data: Send + Sync {
fn latest_height(&self) -> StorageResult<BlockHeight>;
fn get_block(
&self,
height: &BlockHeight,
) -> StorageResult<Option<Block<Transaction>>>;
}
Loading