use crate::fuel_core_graphql_api::ports::OnChainDatabase; use fuel_core_storage::{ iter::{ BoxedIter, IterDirection, }, not_found, tables::{ FuelBlocks, SealedBlockConsensus, }, Result as StorageResult, StorageAsRef, }; use fuel_core_types::{ blockchain::{ block::CompressedBlock, consensus::Consensus, }, fuel_types::BlockHeight, }; pub trait SimpleBlockData: Send + Sync { fn block(&self, id: &BlockHeight) -> StorageResult; } impl SimpleBlockData for D { fn block(&self, id: &BlockHeight) -> StorageResult { let block = self .storage::() .get(id)? .ok_or_else(|| not_found!(FuelBlocks))? .into_owned(); Ok(block) } } pub trait BlockQueryData: Send + Sync + SimpleBlockData { fn latest_block_height(&self) -> StorageResult; fn latest_block(&self) -> StorageResult; fn compressed_blocks( &self, height: Option, direction: IterDirection, ) -> BoxedIter>; fn consensus(&self, id: &BlockHeight) -> StorageResult; } impl BlockQueryData for D { fn latest_block_height(&self) -> StorageResult { self.latest_height() } fn latest_block(&self) -> StorageResult { self.block(&self.latest_block_height()?) } fn compressed_blocks( &self, height: Option, direction: IterDirection, ) -> BoxedIter> { self.blocks(height, direction) } fn consensus(&self, id: &BlockHeight) -> StorageResult { self.storage::() .get(id) .map(|c| c.map(|c| c.into_owned()))? .ok_or(not_found!(SealedBlockConsensus)) } }