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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! The module contains definition of storage tables used by default implementation of fuel
//! services.

use crate::Mappable;
use fuel_core_types::{
    blockchain::{
        block::CompressedBlock,
        consensus::Consensus,
        header::{
            ConsensusParametersVersion,
            StateTransitionBytecodeVersion,
        },
    },
    entities::{
        coins::coin::CompressedCoin,
        contract::ContractUtxoInfo,
        relayer::message::Message,
    },
    fuel_tx::{
        Bytes32,
        ConsensusParameters,
        Transaction,
        TxId,
        UtxoId,
    },
    fuel_types::{
        BlockHeight,
        ContractId,
        Nonce,
    },
};
pub use fuel_vm_private::storage::{
    ContractsAssets,
    ContractsRawCode,
    ContractsState,
    UploadedBytecodes,
};

/// The table of blocks generated by Fuels validators.
/// Right now, we have only that type of block, but we will support others in the future.
pub struct FuelBlocks;

impl Mappable for FuelBlocks {
    /// Unique identifier of the fuel block.
    type Key = Self::OwnedKey;
    type OwnedKey = BlockHeight;
    type Value = Self::OwnedValue;
    type OwnedValue = CompressedBlock;
}

/// The latest UTXO info of the contract. The contract's UTXO represents the unique id of the state.
/// After each transaction, old UTXO is consumed, and new UTXO is produced. UTXO is used as an
/// input to the next transaction related to the `ContractId` smart contract.
pub struct ContractsLatestUtxo;

impl Mappable for ContractsLatestUtxo {
    type Key = Self::OwnedKey;
    type OwnedKey = ContractId;
    /// The latest UTXO info
    type Value = Self::OwnedValue;
    type OwnedValue = ContractUtxoInfo;
}

/// The table of consensus metadata associated with sealed (finalized) blocks
pub struct SealedBlockConsensus;

impl Mappable for SealedBlockConsensus {
    type Key = Self::OwnedKey;
    type OwnedKey = BlockHeight;
    type Value = Self::OwnedValue;
    type OwnedValue = Consensus;
}

/// The storage table of coins. Each [`CompressedCoin`]
/// is represented by unique `UtxoId`.
pub struct Coins;

impl Mappable for Coins {
    type Key = Self::OwnedKey;
    type OwnedKey = UtxoId;
    type Value = Self::OwnedValue;
    type OwnedValue = CompressedCoin;
}

/// The storage table of bridged Ethereum message.
pub struct Messages;

impl Mappable for Messages {
    type Key = Self::OwnedKey;
    type OwnedKey = Nonce;
    type Value = Self::OwnedValue;
    type OwnedValue = Message;
}

/// The storage table of confirmed transactions.
pub struct Transactions;

impl Mappable for Transactions {
    type Key = Self::OwnedKey;
    type OwnedKey = TxId;
    type Value = Self::OwnedValue;
    type OwnedValue = Transaction;
}

/// The storage table of processed transactions that were executed in the past.
/// The table helps to drop duplicated transactions.
pub struct ProcessedTransactions;

impl Mappable for ProcessedTransactions {
    type Key = Self::OwnedKey;
    type OwnedKey = TxId;
    type Value = Self::OwnedValue;
    type OwnedValue = ();
}

/// The storage table of consensus parameters.
pub struct ConsensusParametersVersions;

impl Mappable for ConsensusParametersVersions {
    type Key = Self::OwnedKey;
    type OwnedKey = ConsensusParametersVersion;
    type Value = Self::OwnedValue;
    type OwnedValue = ConsensusParameters;
}

/// The storage table of state transition bytecodes.
pub struct StateTransitionBytecodeVersions;

impl Mappable for StateTransitionBytecodeVersions {
    type Key = Self::OwnedKey;
    type OwnedKey = StateTransitionBytecodeVersion;
    type Value = Self::OwnedValue;
    /// The Merkle root of the bytecode from the [`UploadedBytecodes`].
    type OwnedValue = Bytes32;
}

/// The module contains definition of merkle-related tables.
pub mod merkle {
    use crate::{
        Mappable,
        MerkleRoot,
    };
    use fuel_core_types::{
        fuel_merkle::{
            binary,
            sparse,
        },
        fuel_tx::ContractId,
        fuel_types::BlockHeight,
    };

    /// The key for the corresponding `DenseMerkleMetadata` type.
    /// The `Latest` variant is used to have the access to the latest dense Merkle tree.
    #[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize)]
    pub enum DenseMetadataKey<PrimaryKey> {
        /// The primary key of the `DenseMerkleMetadata`.
        Primary(PrimaryKey),
        #[default]
        /// The latest `DenseMerkleMetadata` of the table.
        Latest,
    }

    #[cfg(feature = "test-helpers")]
    impl<PrimaryKey> rand::distributions::Distribution<DenseMetadataKey<PrimaryKey>>
        for rand::distributions::Standard
    where
        rand::distributions::Standard: rand::distributions::Distribution<PrimaryKey>,
    {
        fn sample<R: rand::Rng + ?Sized>(
            &self,
            rng: &mut R,
        ) -> DenseMetadataKey<PrimaryKey> {
            DenseMetadataKey::Primary(rng.gen())
        }
    }

    /// Metadata for dense Merkle trees
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
    pub enum DenseMerkleMetadata {
        /// V1 Dense Merkle Metadata
        V1(DenseMerkleMetadataV1),
    }

    impl Default for DenseMerkleMetadata {
        fn default() -> Self {
            Self::V1(Default::default())
        }
    }

    impl DenseMerkleMetadata {
        /// Create a new dense Merkle metadata object from the given Merkle
        /// root and version
        pub fn new(root: MerkleRoot, version: u64) -> Self {
            let metadata = DenseMerkleMetadataV1 { root, version };
            Self::V1(metadata)
        }

        /// Get the Merkle root of the dense Metadata
        pub fn root(&self) -> &MerkleRoot {
            match self {
                DenseMerkleMetadata::V1(metadata) => &metadata.root,
            }
        }

        /// Get the version of the dense Metadata
        pub fn version(&self) -> u64 {
            match self {
                DenseMerkleMetadata::V1(metadata) => metadata.version,
            }
        }
    }

    /// Metadata for dense Merkle trees
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
    pub struct DenseMerkleMetadataV1 {
        /// The root hash of the dense Merkle tree structure
        pub root: MerkleRoot,
        /// The version of the dense Merkle tree structure is equal to the number of
        /// leaves. Every time we append a new leaf to the Merkle tree data set, we
        /// increment the version number.
        pub version: u64,
    }

    impl Default for DenseMerkleMetadataV1 {
        fn default() -> Self {
            let empty_merkle_tree = binary::root_calculator::MerkleRootCalculator::new();
            Self {
                root: empty_merkle_tree.root(),
                version: 0,
            }
        }
    }

    impl From<DenseMerkleMetadataV1> for DenseMerkleMetadata {
        fn from(value: DenseMerkleMetadataV1) -> Self {
            Self::V1(value)
        }
    }

    /// Metadata for sparse Merkle trees
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
    pub enum SparseMerkleMetadata {
        /// V1 Sparse Merkle Metadata
        V1(SparseMerkleMetadataV1),
    }

    impl Default for SparseMerkleMetadata {
        fn default() -> Self {
            Self::V1(Default::default())
        }
    }

    impl SparseMerkleMetadata {
        /// Create a new sparse Merkle metadata object from the given Merkle
        /// root
        pub fn new(root: MerkleRoot) -> Self {
            let metadata = SparseMerkleMetadataV1 { root };
            Self::V1(metadata)
        }

        /// Get the Merkle root stored in the metadata
        pub fn root(&self) -> &MerkleRoot {
            match self {
                SparseMerkleMetadata::V1(metadata) => &metadata.root,
            }
        }
    }

    /// Metadata V1 for sparse Merkle trees
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
    pub struct SparseMerkleMetadataV1 {
        /// The root hash of the sparse Merkle tree structure
        pub root: MerkleRoot,
    }

    impl Default for SparseMerkleMetadataV1 {
        fn default() -> Self {
            let empty_merkle_tree = sparse::in_memory::MerkleTree::new();
            Self {
                root: empty_merkle_tree.root(),
            }
        }
    }

    impl From<SparseMerkleMetadataV1> for SparseMerkleMetadata {
        fn from(value: SparseMerkleMetadataV1) -> Self {
            Self::V1(value)
        }
    }

    /// The table of BMT data for Fuel blocks.
    pub struct FuelBlockMerkleData;

    impl Mappable for FuelBlockMerkleData {
        type Key = u64;
        type OwnedKey = Self::Key;
        type Value = binary::Primitive;
        type OwnedValue = Self::Value;
    }

    /// The metadata table for [`FuelBlockMerkleData`] table.
    pub struct FuelBlockMerkleMetadata;

    impl Mappable for FuelBlockMerkleMetadata {
        type Key = DenseMetadataKey<BlockHeight>;
        type OwnedKey = Self::Key;
        type Value = DenseMerkleMetadata;
        type OwnedValue = Self::Value;
    }

    /// The table of SMT data for Contract assets.
    pub struct ContractsAssetsMerkleData;

    impl Mappable for ContractsAssetsMerkleData {
        type Key = [u8; 32];
        type OwnedKey = Self::Key;
        type Value = sparse::Primitive;
        type OwnedValue = Self::Value;
    }

    /// The metadata table for [`ContractsAssetsMerkleData`] table
    pub struct ContractsAssetsMerkleMetadata;

    impl Mappable for ContractsAssetsMerkleMetadata {
        type Key = ContractId;
        type OwnedKey = Self::Key;
        type Value = SparseMerkleMetadata;
        type OwnedValue = Self::Value;
    }

    /// The table of SMT data for Contract state.
    pub struct ContractsStateMerkleData;

    impl Mappable for ContractsStateMerkleData {
        type Key = [u8; 32];
        type OwnedKey = Self::Key;
        type Value = sparse::Primitive;
        type OwnedValue = Self::Value;
    }

    /// The metadata table for [`ContractsStateMerkleData`] table
    pub struct ContractsStateMerkleMetadata;

    impl Mappable for ContractsStateMerkleMetadata {
        type Key = ContractId;
        type OwnedKey = Self::Key;
        type Value = SparseMerkleMetadata;
        type OwnedValue = Self::Value;
    }
}