fuel_core/database/
sealed_block.rs

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::database::OnChainIterableKeyValueView;
use fuel_core_storage::{
    iter::{
        IterDirection,
        IteratorOverTable,
    },
    not_found,
    tables::{
        FuelBlocks,
        SealedBlockConsensus,
    },
    Result as StorageResult,
    StorageAsRef,
};
use fuel_core_types::{
    blockchain::{
        block::CompressedBlock,
        consensus::{
            Consensus,
            Genesis,
        },
        SealedBlock,
        SealedBlockHeader,
    },
    fuel_types::BlockHeight,
    services::p2p::Transactions,
};
use std::ops::Range;

impl OnChainIterableKeyValueView {
    /// Returns `SealedBlock` by `height`.
    /// Reusable across different trait implementations
    pub fn get_sealed_block_by_height(
        &self,
        height: &BlockHeight,
    ) -> StorageResult<Option<SealedBlock>> {
        // combine the block and consensus metadata into a sealed fuel block type
        let consensus = self.storage::<SealedBlockConsensus>().get(height)?;

        if let Some(consensus) = consensus {
            let block = self.get_full_block(height)?.ok_or(not_found!(FuelBlocks))?;

            let sealed_block = SealedBlock {
                entity: block,
                consensus: consensus.into_owned(),
            };

            Ok(Some(sealed_block))
        } else {
            Ok(None)
        }
    }

    pub fn genesis_height(&self) -> StorageResult<Option<BlockHeight>> {
        self.iter_all_keys::<FuelBlocks>(Some(IterDirection::Forward))
            .next()
            .transpose()
    }

    pub fn genesis_block(&self) -> StorageResult<Option<CompressedBlock>> {
        Ok(self
            .iter_all::<FuelBlocks>(Some(IterDirection::Forward))
            .next()
            .transpose()?
            .map(|(_, block)| block))
    }

    pub fn get_genesis(&self) -> StorageResult<Genesis> {
        let pair = self
            .iter_all::<SealedBlockConsensus>(Some(IterDirection::Forward))
            .next()
            .transpose()?;

        if let Some((_, Consensus::Genesis(genesis))) = pair {
            Ok(genesis)
        } else {
            Err(not_found!(SealedBlockConsensus))
        }
    }

    pub fn get_sealed_block_headers(
        &self,
        block_height_range: Range<u32>,
    ) -> StorageResult<Option<Vec<SealedBlockHeader>>> {
        let headers = block_height_range
            .map(BlockHeight::from)
            .map(|height| self.get_sealed_block_header(&height))
            .collect::<StorageResult<Option<Vec<_>>>>()?;
        Ok(headers)
    }

    pub fn get_sealed_block_header(
        &self,
        height: &BlockHeight,
    ) -> StorageResult<Option<SealedBlockHeader>> {
        let consensus = self.storage::<SealedBlockConsensus>().get(height)?;

        if let Some(consensus) = consensus {
            let header = self
                .storage::<FuelBlocks>()
                .get(height)?
                .ok_or(not_found!(FuelBlocks))?; // This shouldn't happen if a block has been sealed

            let sealed_block = SealedBlockHeader {
                entity: header.into_owned().header().clone(),
                consensus: consensus.into_owned(),
            };

            Ok(Some(sealed_block))
        } else {
            Ok(None)
        }
    }

    pub fn get_transactions_on_blocks(
        &self,
        block_height_range: Range<u32>,
    ) -> StorageResult<Option<Vec<Transactions>>> {
        let transactions = block_height_range
            .into_iter()
            .map(BlockHeight::from)
            .map(|block_height| {
                let transactions = self
                    .get_full_block(&block_height)?
                    .map(|block| block.into_inner().1)
                    .map(Transactions);
                Ok(transactions)
            })
            .collect::<StorageResult<_>>()?;
        Ok(transactions)
    }
}