1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::fuel_core_graphql_api::ports::{
    DatabaseBlocks,
    OnChainDatabase,
};
use fuel_core_storage::{
    iter::{
        BoxedIter,
        IterDirection,
    },
    Result as StorageResult,
};
use fuel_core_types::{
    blockchain::{
        block::CompressedBlock,
        consensus::Consensus,
    },
    fuel_types::BlockHeight,
};

pub trait SimpleBlockData: Send + Sync {
    fn block(&self, id: &BlockHeight) -> StorageResult<CompressedBlock>;
}

impl<D> SimpleBlockData for D
where
    D: OnChainDatabase + DatabaseBlocks + ?Sized,
{
    fn block(&self, id: &BlockHeight) -> StorageResult<CompressedBlock> {
        self.block(id)
    }
}

pub trait BlockQueryData: Send + Sync + SimpleBlockData {
    fn latest_block_height(&self) -> StorageResult<BlockHeight>;

    fn latest_block(&self) -> StorageResult<CompressedBlock>;

    fn compressed_blocks(
        &self,
        height: Option<BlockHeight>,
        direction: IterDirection,
    ) -> BoxedIter<StorageResult<CompressedBlock>>;

    fn consensus(&self, id: &BlockHeight) -> StorageResult<Consensus>;
}

impl<D> BlockQueryData for D
where
    D: OnChainDatabase + DatabaseBlocks + ?Sized,
{
    fn latest_block_height(&self) -> StorageResult<BlockHeight> {
        self.latest_height()
    }

    fn latest_block(&self) -> StorageResult<CompressedBlock> {
        self.block(&self.latest_block_height()?)
    }

    fn compressed_blocks(
        &self,
        height: Option<BlockHeight>,
        direction: IterDirection,
    ) -> BoxedIter<StorageResult<CompressedBlock>> {
        self.blocks(height, direction)
    }

    fn consensus(&self, id: &BlockHeight) -> StorageResult<Consensus> {
        self.consensus(id)
    }
}