Skip to content

Commit

Permalink
Only use BlockAt in the place related to the issue.
Browse files Browse the repository at this point in the history
Fixed the changelog entry
  • Loading branch information
xgreenx committed Nov 23, 2024
1 parent 9f898ba commit 4dca2b3
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 88 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockHeight>` 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.
Expand Down Expand Up @@ -250,7 +250,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- [1983](https://github.com/FuelLabs/fuel-core/pull/1983): Add adapters for gas price service for accessing database values
- [#2033](https://github.com/FuelLabs/fuel-core/pull/2033): Remove `Option<BlockHeight>` in favor of `BlockHeightQuery` where applicable

### Breaking
- [2048](https://github.com/FuelLabs/fuel-core/pull/2048): Disable SMT for `ContractsAssets` and `ContractsState` for the production mode of the `fuel-core`. The SMT still is used in benchmarks and tests.
Expand Down
29 changes: 15 additions & 14 deletions crates/fuel-core/src/graphql_api/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use fuel_core_types::{
block::CompressedBlock,
consensus::Consensus,
primitives::{
BlockAt,
BlockId,
DaBlockHeight,
},
Expand Down Expand Up @@ -177,40 +176,42 @@ impl ReadView {

pub fn blocks(
&self,
height: BlockAt,
height: Option<BlockHeight>,
direction: IterDirection,
) -> BoxedIter<'_, StorageResult<CompressedBlock>> {
// Chain together blocks from the off-chain db and the on-chain db
// The blocks in off-chain db, if any, are from time before regenesis

if let BlockAt::Specific(inner) = height {
match (inner >= self.genesis_height, direction) {
(true, IterDirection::Forward) => self.on_chain.blocks(height, direction),
if let Some(height) = height {
match (height >= self.genesis_height, direction) {
(true, IterDirection::Forward) => {
self.on_chain.blocks(Some(height), direction)
}
(true, IterDirection::Reverse) => self
.on_chain
.blocks(height, direction)
.chain(self.off_chain.old_blocks(BlockAt::Genesis, direction))
.blocks(Some(height), direction)
.chain(self.off_chain.old_blocks(None, direction))
.into_boxed(),
(false, IterDirection::Forward) => self
.off_chain
.old_blocks(height, direction)
.chain(self.on_chain.blocks(BlockAt::Genesis, direction))
.old_blocks(Some(height), direction)
.chain(self.on_chain.blocks(None, direction))
.into_boxed(),
(false, IterDirection::Reverse) => {
self.off_chain.old_blocks(height, direction)
self.off_chain.old_blocks(Some(height), direction)
}
}
} else {
match direction {
IterDirection::Forward => self
.off_chain
.old_blocks(BlockAt::Genesis, direction)
.chain(self.on_chain.blocks(BlockAt::Genesis, direction))
.old_blocks(None, direction)
.chain(self.on_chain.blocks(None, direction))
.into_boxed(),
IterDirection::Reverse => self
.on_chain
.blocks(BlockAt::Genesis, direction)
.chain(self.off_chain.old_blocks(BlockAt::Genesis, direction))
.blocks(None, direction)
.chain(self.off_chain.old_blocks(None, direction))
.into_boxed(),
}
}
Expand Down
16 changes: 12 additions & 4 deletions crates/fuel-core/src/graphql_api/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use fuel_core_types::{
consensus::Consensus,
header::ConsensusParametersVersion,
primitives::{
BlockAt,
BlockId,
DaBlockHeight,
},
Expand Down Expand Up @@ -99,7 +98,7 @@ pub trait OffChainDatabase: Send + Sync {

fn old_blocks(
&self,
height: BlockAt,
height: Option<BlockHeight>,
direction: IterDirection,
) -> BoxedIter<'_, StorageResult<CompressedBlock>>;

Expand Down Expand Up @@ -141,7 +140,7 @@ pub trait DatabaseBlocks {

fn blocks(
&self,
height: BlockAt,
height: Option<BlockHeight>,
direction: IterDirection,
) -> BoxedIter<'_, StorageResult<CompressedBlock>>;

Expand Down Expand Up @@ -283,14 +282,14 @@ pub mod worker {
relayed_transactions::RelayedTransactionStatuses,
},
};
use derive_more::Display;
use fuel_core_services::stream::BoxStream;
use fuel_core_storage::{
Error as StorageError,
Result as StorageResult,
StorageMutate,
};
use fuel_core_types::{
blockchain::primitives::BlockAt,
fuel_tx::{
Address,
Bytes32,
Expand Down Expand Up @@ -319,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<OwnedMessageIds, Error = StorageError>
+ StorageMutate<OwnedCoins, Error = StorageError>
Expand Down
12 changes: 10 additions & 2 deletions crates/fuel-core/src/graphql_api/worker_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
fuel_core_graphql_api::{
ports::{
self,
worker::OffChainDatabaseTransaction,
worker::{
BlockAt,
OffChainDatabaseTransaction,
},
},
storage::{
blocks::FuelBlockIdsToHeights,
Expand Down Expand Up @@ -537,8 +540,13 @@ 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.into())?;
import_result_provider.block_event_at_height(next_block_height)?;

task.process_block(import_result)?
}
Expand Down
7 changes: 2 additions & 5 deletions crates/fuel-core/src/query/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use fuel_core_storage::{
Result as StorageResult,
};
use fuel_core_types::{
blockchain::{
block::CompressedBlock,
primitives::BlockAt,
},
blockchain::block::CompressedBlock,
fuel_types::BlockHeight,
};
use futures::Stream;
Expand All @@ -24,7 +21,7 @@ impl ReadView {

pub fn compressed_blocks(
&self,
height: BlockAt,
height: Option<BlockHeight>,
direction: IterDirection,
) -> impl Stream<Item = StorageResult<CompressedBlock>> + '_ {
futures::stream::iter(self.blocks(height, direction)).yield_each(self.batch_size)
Expand Down
7 changes: 3 additions & 4 deletions crates/fuel-core/src/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use fuel_core_types::{
blockchain::{
block::CompressedBlock,
header::BlockHeader,
primitives::BlockAt,
},
fuel_tx::TxId,
fuel_types::{
Expand Down Expand Up @@ -303,7 +302,7 @@ impl BlockQuery {
crate::schema::query_pagination(after, before, first, last, |start, direction| {
Ok(blocks_query(
query.as_ref(),
start.map(Into::<BlockHeight>::into).into(),
start.map(Into::into),
direction,
))
})
Expand Down Expand Up @@ -345,7 +344,7 @@ impl HeaderQuery {
crate::schema::query_pagination(after, before, first, last, |start, direction| {
Ok(blocks_query(
query.as_ref(),
start.map(Into::<BlockHeight>::into).into(),
start.map(Into::into),
direction,
))
})
Expand All @@ -355,7 +354,7 @@ impl HeaderQuery {

fn blocks_query<T>(
query: &ReadView,
height: BlockAt,
height: Option<BlockHeight>,
direction: IterDirection,
) -> impl Stream<Item = StorageResult<(U32, T)>> + '_
where
Expand Down
3 changes: 1 addition & 2 deletions crates/fuel-core/src/schema/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ impl TxQuery {
|start: &Option<SortedTxCursor>, direction| {
let start = *start;
let block_id = start.map(|sorted| sorted.block_height);
let compressed_blocks =
query.compressed_blocks(block_id.into(), direction);
let compressed_blocks = query.compressed_blocks(block_id, direction);

let all_txs = compressed_blocks
.map_ok(move |fuel_block| {
Expand Down
6 changes: 2 additions & 4 deletions crates/fuel-core/src/service/adapters/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
database::OnChainIterableKeyValueView,
fuel_core_graphql_api::ports::{
worker,
worker::BlockAt,
BlockProducerPort,
ConsensusProvider,
DatabaseMessageProof,
Expand All @@ -31,10 +32,7 @@ use fuel_core_services::stream::BoxStream;
use fuel_core_storage::Result as StorageResult;
use fuel_core_txpool::TxStatusMessage;
use fuel_core_types::{
blockchain::{
header::ConsensusParametersVersion,
primitives::BlockAt,
},
blockchain::header::ConsensusParametersVersion,
entities::relayer::message::MerkleProof,
fuel_tx::{
Bytes32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ use fuel_core_types::{
blockchain::{
block::CompressedBlock,
consensus::Consensus,
primitives::{
BlockAt,
BlockId,
},
primitives::BlockId,
},
entities::relayer::transaction::RelayedTransactionStatus,
fuel_tx::{
Expand Down Expand Up @@ -153,10 +150,9 @@ impl OffChainDatabase for OffChainIterableKeyValueView {

fn old_blocks(
&self,
height: BlockAt,
height: Option<BlockHeight>,
direction: IterDirection,
) -> BoxedIter<'_, StorageResult<CompressedBlock>> {
let height: Option<BlockHeight> = height.into();
self.iter_all_by_start::<OldFuelBlocks>(height.as_ref(), Some(direction))
.map(|r| r.map(|(_, block)| block))
.into_boxed()
Expand Down
8 changes: 2 additions & 6 deletions crates/fuel-core/src/service/adapters/graphql_api/on_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ use fuel_core_types::{
blockchain::{
block::CompressedBlock,
consensus::Consensus,
primitives::{
BlockAt,
DaBlockHeight,
},
primitives::DaBlockHeight,
},
entities::relayer::message::Message,
fuel_tx::{
Expand Down Expand Up @@ -75,10 +72,9 @@ impl DatabaseBlocks for OnChainIterableKeyValueView {

fn blocks(
&self,
height: BlockAt,
height: Option<BlockHeight>,
direction: IterDirection,
) -> BoxedIter<'_, StorageResult<CompressedBlock>> {
let height: Option<BlockHeight> = height.into();
self.iter_all_by_start::<FuelBlocks>(height.as_ref(), Some(direction))
.map(|result| result.map(|(_, block)| block))
.into_boxed()
Expand Down
14 changes: 6 additions & 8 deletions crates/fuel-core/src/service/adapters/import_result_provider.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use crate::{
database::Database,
fuel_core_graphql_api::ports::worker::BlockAt,
service::adapters::ExecutorAdapter,
};
use fuel_core_importer::ports::Validator;
use fuel_core_storage::{
not_found,
transactional::AtomicView,
};
use fuel_core_types::{
blockchain::primitives::BlockAt,
services::{
block_importer::{
ImportResult,
SharedImportResult,
},
executor::ValidationResult,
use fuel_core_types::services::{
block_importer::{
ImportResult,
SharedImportResult,
},
executor::ValidationResult,
};
use std::sync::Arc;

Expand Down
32 changes: 1 addition & 31 deletions crates/types/src/blockchain/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
use crate::{
fuel_crypto,
fuel_crypto::SecretKey,
fuel_types::{
BlockHeight,
Bytes32,
},
fuel_types::Bytes32,
};
use core::array::TryFromSliceError;
use derive_more::{
Expand Down Expand Up @@ -182,30 +179,3 @@ impl TryFrom<&'_ [u8]> for BlockId {
Ok(Self::from(TryInto::<[u8; 32]>::try_into(bytes)?))
}
}

/// 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,
}

impl From<BlockAt> for Option<BlockHeight> {
fn from(value: BlockAt) -> Self {
match value {
BlockAt::Specific(block_height) => Some(block_height),
BlockAt::Genesis => None,
}
}
}

impl From<Option<BlockHeight>> for BlockAt {
fn from(value: Option<BlockHeight>) -> Self {
match value {
Some(block_height) => BlockAt::Specific(block_height),
None => BlockAt::Genesis,
}
}
}

0 comments on commit 4dca2b3

Please sign in to comment.