fuel_core/database/
genesis_progress.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::graphql_api::storage::Column as OffChainColumn;

use super::{
    database_description::{
        off_chain::OffChain,
        on_chain::OnChain,
        DatabaseDescription,
    },
    GenesisDatabase,
};
use fuel_core_chain_config::GenesisCommitment;
use fuel_core_executor::refs::ContractRef;
use fuel_core_storage::{
    blueprint::plain::Plain,
    codec::postcard::Postcard,
    column::Column,
    iter::IteratorOverTable,
    structured_storage::TableWithBlueprint,
    tables::{
        Coins,
        ContractsLatestUtxo,
        Messages,
        ProcessedTransactions,
    },
    Error as StorageError,
    Mappable,
    MerkleRoot,
    Result,
    StorageAsMut,
    StorageInspect,
    StorageMutate,
};
use fuel_core_types::fuel_merkle::binary::root_calculator::MerkleRootCalculator;

pub struct GenesisMetadata<Description>(core::marker::PhantomData<Description>);

impl<Description> Mappable for GenesisMetadata<Description> {
    type Key = str;
    type OwnedKey = String;
    type Value = Self::OwnedValue;
    type OwnedValue = usize;
}

impl TableWithBlueprint for GenesisMetadata<OnChain> {
    type Blueprint = Plain<Postcard, Postcard>;
    type Column = <OnChain as DatabaseDescription>::Column;
    fn column() -> Self::Column {
        Column::GenesisMetadata
    }
}

impl TableWithBlueprint for GenesisMetadata<OffChain> {
    type Blueprint = Plain<Postcard, Postcard>;
    type Column = <OffChain as DatabaseDescription>::Column;
    fn column() -> Self::Column {
        OffChainColumn::GenesisMetadata
    }
}

pub trait GenesisProgressInspect<Description> {
    fn genesis_progress(&self, key: &str) -> Option<usize>;
}

pub trait GenesisProgressMutate<Description> {
    fn update_genesis_progress(
        &mut self,
        key: &str,
        processed_group: usize,
    ) -> Result<()>;
}

impl<S, DbDesc> GenesisProgressInspect<DbDesc> for S
where
    S: StorageInspect<GenesisMetadata<DbDesc>, Error = StorageError>,
    DbDesc: DatabaseDescription,
{
    fn genesis_progress(
        &self,
        key: &<GenesisMetadata<DbDesc> as Mappable>::Key,
    ) -> Option<usize> {
        Some(
            StorageInspect::<GenesisMetadata<DbDesc>>::get(self, key)
                .ok()??
                .into_owned(),
        )
    }
}

impl<S, DbDesc> GenesisProgressMutate<DbDesc> for S
where
    S: StorageMutate<GenesisMetadata<DbDesc>, Error = StorageError>,
{
    fn update_genesis_progress(
        &mut self,
        key: &<GenesisMetadata<DbDesc> as Mappable>::Key,
        processed_group: usize,
    ) -> Result<()> {
        self.storage_as_mut::<GenesisMetadata<DbDesc>>()
            .insert(key, &processed_group)?;

        Ok(())
    }
}

impl GenesisDatabase {
    pub fn genesis_coins_root(&self) -> Result<MerkleRoot> {
        let coins = self.iter_all::<Coins>(None);

        let mut root_calculator = MerkleRootCalculator::new();
        for coin in coins {
            let (utxo_id, coin) = coin?;
            root_calculator.push(coin.uncompress(utxo_id).root()?.as_slice());
        }

        Ok(root_calculator.root())
    }

    pub fn genesis_messages_root(&self) -> Result<MerkleRoot> {
        let messages = self.iter_all::<Messages>(None);

        let mut root_calculator = MerkleRootCalculator::new();
        for message in messages {
            let (_, message) = message?;
            root_calculator.push(message.root()?.as_slice());
        }

        Ok(root_calculator.root())
    }

    pub fn genesis_contracts_root(&self) -> Result<MerkleRoot> {
        let contracts = self.iter_all::<ContractsLatestUtxo>(None);

        let mut root_calculator = MerkleRootCalculator::new();
        for contract in contracts {
            let (contract_id, _) = contract?;
            let root = ContractRef::new(self, contract_id).root()?;
            root_calculator.push(root.as_slice());
        }

        Ok(root_calculator.root())
    }

    pub fn processed_transactions_root(&self) -> Result<MerkleRoot> {
        let txs = self.iter_all::<ProcessedTransactions>(None);

        let mut root_calculator = MerkleRootCalculator::new();
        for tx in txs {
            let (tx_id, _) = tx?;
            root_calculator.push(tx_id.as_slice());
        }

        Ok(root_calculator.root())
    }
}