diff --git a/CHANGELOG.md b/CHANGELOG.md index 17329a646a..29d283eac8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [2376](https://github.com/FuelLabs/fuel-core/pull/2376): Add a way to fetch transactions in P2P without specifying a peer. - [2327](https://github.com/FuelLabs/fuel-core/pull/2327): Add more services tests and more checks of the pool. Also add an high level documentation for users of the pool and contributors. - [2416](https://github.com/FuelLabs/fuel-core/issues/2416): Define the `GasPriceServiceV1` task. - +- [2033](https://github.com/FuelLabs/fuel-core/pull/2033): Remove `Option` in favor of `BlockHeightQuery` where applicable. ### Fixed - [2366](https://github.com/FuelLabs/fuel-core/pull/2366): The `importer_gas_price_for_block` metric is properly collected. diff --git a/crates/fuel-core/src/graphql_api/ports.rs b/crates/fuel-core/src/graphql_api/ports.rs index 077a48d163..307904c9a9 100644 --- a/crates/fuel-core/src/graphql_api/ports.rs +++ b/crates/fuel-core/src/graphql_api/ports.rs @@ -282,6 +282,7 @@ pub mod worker { relayed_transactions::RelayedTransactionStatuses, }, }; + use derive_more::Display; use fuel_core_services::stream::BoxStream; use fuel_core_storage::{ Error as StorageError, @@ -317,6 +318,15 @@ pub mod worker { fn transaction(&mut self) -> Self::Transaction<'_>; } + /// Represents either the Genesis Block or a block at a specific height + #[derive(Copy, Clone, Debug, Display, PartialEq, Eq, Hash, Ord, PartialOrd)] + pub enum BlockAt { + /// Block at a specific height + Specific(BlockHeight), + /// Genesis block + Genesis, + } + pub trait OffChainDatabaseTransaction: StorageMutate + StorageMutate @@ -369,7 +379,7 @@ pub mod worker { /// Return the import result at the given height. fn block_event_at_height( &self, - height: Option, + height: BlockAt, ) -> anyhow::Result; } diff --git a/crates/fuel-core/src/graphql_api/worker_service.rs b/crates/fuel-core/src/graphql_api/worker_service.rs index 8d5b0bc923..6c29e49e7e 100644 --- a/crates/fuel-core/src/graphql_api/worker_service.rs +++ b/crates/fuel-core/src/graphql_api/worker_service.rs @@ -10,7 +10,10 @@ use crate::{ fuel_core_graphql_api::{ ports::{ self, - worker::OffChainDatabaseTransaction, + worker::{ + BlockAt, + OffChainDatabaseTransaction, + }, }, storage::{ blocks::FuelBlockIdsToHeights, @@ -537,6 +540,11 @@ where let next_block_height = off_chain_height.map(|height| BlockHeight::new(height.saturating_add(1))); + let next_block_height = match next_block_height { + Some(block_height) => BlockAt::Specific(block_height), + None => BlockAt::Genesis, + }; + let import_result = import_result_provider.block_event_at_height(next_block_height)?; diff --git a/crates/fuel-core/src/schema/block.rs b/crates/fuel-core/src/schema/block.rs index 7cdfeff2d5..061fcb6e7d 100644 --- a/crates/fuel-core/src/schema/block.rs +++ b/crates/fuel-core/src/schema/block.rs @@ -45,8 +45,10 @@ use fuel_core_types::{ header::BlockHeader, }, fuel_tx::TxId, - fuel_types, - fuel_types::BlockHeight, + fuel_types::{ + self, + BlockHeight, + }, }; use futures::{ Stream, diff --git a/crates/fuel-core/src/service/adapters/graphql_api.rs b/crates/fuel-core/src/service/adapters/graphql_api.rs index ff96e484ed..43cd047179 100644 --- a/crates/fuel-core/src/service/adapters/graphql_api.rs +++ b/crates/fuel-core/src/service/adapters/graphql_api.rs @@ -9,6 +9,7 @@ use crate::{ database::OnChainIterableKeyValueView, fuel_core_graphql_api::ports::{ worker, + worker::BlockAt, BlockProducerPort, ConsensusProvider, DatabaseMessageProof, @@ -218,7 +219,7 @@ impl worker::BlockImporter for GraphQLBlockImporter { fn block_event_at_height( &self, - height: Option, + height: BlockAt, ) -> anyhow::Result { self.import_result_provider_adapter.result_at_height(height) } diff --git a/crates/fuel-core/src/service/adapters/import_result_provider.rs b/crates/fuel-core/src/service/adapters/import_result_provider.rs index fb1c699db8..3506ca2bdd 100644 --- a/crates/fuel-core/src/service/adapters/import_result_provider.rs +++ b/crates/fuel-core/src/service/adapters/import_result_provider.rs @@ -1,5 +1,6 @@ use crate::{ database::Database, + fuel_core_graphql_api::ports::worker::BlockAt, service::adapters::ExecutorAdapter, }; use fuel_core_importer::ports::Validator; @@ -7,15 +8,12 @@ use fuel_core_storage::{ not_found, transactional::AtomicView, }; -use fuel_core_types::{ - fuel_types::BlockHeight, - services::{ - block_importer::{ - ImportResult, - SharedImportResult, - }, - executor::ValidationResult, +use fuel_core_types::services::{ + block_importer::{ + ImportResult, + SharedImportResult, }, + executor::ValidationResult, }; use std::sync::Arc; @@ -37,38 +35,42 @@ impl ImportResultProvider { impl ImportResultProvider { pub fn result_at_height( &self, - height: Option, + height: BlockAt, ) -> anyhow::Result { - if let Some(height) = height { - let sealed_block = self - .on_chain_database - .latest_view()? - .get_sealed_block_by_height(&height)? - .ok_or(not_found!("SealedBlock"))?; + match height { + BlockAt::Specific(height) => { + let sealed_block = self + .on_chain_database + .latest_view()? + .get_sealed_block_by_height(&height)? + .ok_or(not_found!("SealedBlock"))?; - let ValidationResult { tx_status, events } = self - .executor_adapter - .validate(&sealed_block.entity)? - .into_result(); - let result = ImportResult::new_from_local(sealed_block, tx_status, events); - Ok(Arc::new(result)) - } else { - let genesis_height = self - .on_chain_database - .latest_view()? - .genesis_height()? - .ok_or(not_found!("Genesis height"))?; - let sealed_block = self - .on_chain_database - .latest_view()? - .get_sealed_block_by_height(&genesis_height)? - .ok_or(not_found!("SealedBlock"))?; + let ValidationResult { tx_status, events } = self + .executor_adapter + .validate(&sealed_block.entity)? + .into_result(); + let result = + ImportResult::new_from_local(sealed_block, tx_status, events); + Ok(Arc::new(result)) + } + BlockAt::Genesis => { + let genesis_height = self + .on_chain_database + .latest_view()? + .genesis_height()? + .ok_or(not_found!("Genesis height"))?; + let sealed_block = self + .on_chain_database + .latest_view()? + .get_sealed_block_by_height(&genesis_height)? + .ok_or(not_found!("SealedBlock"))?; - Ok(Arc::new(ImportResult::new_from_local( - sealed_block, - vec![], - vec![], - ))) + Ok(Arc::new(ImportResult::new_from_local( + sealed_block, + vec![], + vec![], + ))) + } } } }