fuel_core_storage/
tables.rs

1//! The module contains definition of storage tables used by default implementation of fuel
2//! services.
3
4use crate::Mappable;
5use fuel_core_types::{
6    blockchain::{
7        block::CompressedBlock,
8        consensus::Consensus,
9        header::{
10            ConsensusParametersVersion,
11            StateTransitionBytecodeVersion,
12        },
13    },
14    entities::{
15        coins::coin::CompressedCoin,
16        contract::ContractUtxoInfo,
17        relayer::message::Message,
18    },
19    fuel_tx::{
20        Bytes32,
21        ConsensusParameters,
22        Transaction,
23        TxId,
24        UtxoId,
25    },
26    fuel_types::{
27        BlockHeight,
28        ContractId,
29        Nonce,
30    },
31};
32pub use fuel_vm_private::storage::{
33    BlobData,
34    ContractsAssets,
35    ContractsRawCode,
36    ContractsState,
37    UploadedBytecodes,
38};
39
40/// The table of blocks generated by Fuels validators.
41/// Right now, we have only that type of block, but we will support others in the future.
42pub struct FuelBlocks;
43
44impl Mappable for FuelBlocks {
45    /// Unique identifier of the fuel block.
46    type Key = Self::OwnedKey;
47    type OwnedKey = BlockHeight;
48    type Value = Self::OwnedValue;
49    type OwnedValue = CompressedBlock;
50}
51
52/// The latest UTXO info of the contract. The contract's UTXO represents the unique id of the state.
53/// After each transaction, old UTXO is consumed, and new UTXO is produced. UTXO is used as an
54/// input to the next transaction related to the `ContractId` smart contract.
55pub struct ContractsLatestUtxo;
56
57impl Mappable for ContractsLatestUtxo {
58    type Key = Self::OwnedKey;
59    type OwnedKey = ContractId;
60    /// The latest UTXO info
61    type Value = Self::OwnedValue;
62    type OwnedValue = ContractUtxoInfo;
63}
64
65/// The table of consensus metadata associated with sealed (finalized) blocks
66pub struct SealedBlockConsensus;
67
68impl Mappable for SealedBlockConsensus {
69    type Key = Self::OwnedKey;
70    type OwnedKey = BlockHeight;
71    type Value = Self::OwnedValue;
72    type OwnedValue = Consensus;
73}
74
75/// The storage table of coins. Each [`CompressedCoin`]
76/// is represented by unique `UtxoId`.
77pub struct Coins;
78
79impl Mappable for Coins {
80    type Key = Self::OwnedKey;
81    type OwnedKey = UtxoId;
82    type Value = Self::OwnedValue;
83    type OwnedValue = CompressedCoin;
84}
85
86/// The storage table of bridged Ethereum message.
87pub struct Messages;
88
89impl Mappable for Messages {
90    type Key = Self::OwnedKey;
91    type OwnedKey = Nonce;
92    type Value = Self::OwnedValue;
93    type OwnedValue = Message;
94}
95
96/// The storage table of confirmed transactions.
97pub struct Transactions;
98
99impl Mappable for Transactions {
100    type Key = Self::OwnedKey;
101    type OwnedKey = TxId;
102    type Value = Self::OwnedValue;
103    type OwnedValue = Transaction;
104}
105
106/// The storage table of processed transactions that were executed in the past.
107/// The table helps to drop duplicated transactions.
108pub struct ProcessedTransactions;
109
110impl Mappable for ProcessedTransactions {
111    type Key = Self::OwnedKey;
112    type OwnedKey = TxId;
113    type Value = Self::OwnedValue;
114    type OwnedValue = ();
115}
116
117/// The storage table of consensus parameters.
118pub struct ConsensusParametersVersions;
119
120impl Mappable for ConsensusParametersVersions {
121    type Key = Self::OwnedKey;
122    type OwnedKey = ConsensusParametersVersion;
123    type Value = Self::OwnedValue;
124    type OwnedValue = ConsensusParameters;
125}
126
127/// The storage table of state transition bytecodes.
128pub struct StateTransitionBytecodeVersions;
129
130impl Mappable for StateTransitionBytecodeVersions {
131    type Key = Self::OwnedKey;
132    type OwnedKey = StateTransitionBytecodeVersion;
133    type Value = Self::OwnedValue;
134    /// The Merkle root of the bytecode from the [`UploadedBytecodes`].
135    type OwnedValue = Bytes32;
136}
137
138/// The module contains definition of merkle-related tables.
139pub mod merkle {
140    use crate::{
141        Mappable,
142        MerkleRoot,
143    };
144    use fuel_core_types::{
145        fuel_merkle::{
146            binary,
147            sparse,
148        },
149        fuel_types::BlockHeight,
150    };
151
152    /// The key for the corresponding `DenseMerkleMetadata` type.
153    /// The `Latest` variant is used to have the access to the latest dense Merkle tree.
154    #[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize)]
155    pub enum DenseMetadataKey<PrimaryKey> {
156        /// The primary key of the `DenseMerkleMetadata`.
157        Primary(PrimaryKey),
158        #[default]
159        /// The latest `DenseMerkleMetadata` of the table.
160        Latest,
161    }
162
163    #[cfg(feature = "test-helpers")]
164    impl<PrimaryKey> rand::distributions::Distribution<DenseMetadataKey<PrimaryKey>>
165        for rand::distributions::Standard
166    where
167        rand::distributions::Standard: rand::distributions::Distribution<PrimaryKey>,
168    {
169        fn sample<R: rand::Rng + ?Sized>(
170            &self,
171            rng: &mut R,
172        ) -> DenseMetadataKey<PrimaryKey> {
173            DenseMetadataKey::Primary(rng.r#gen())
174        }
175    }
176
177    /// Metadata for dense Merkle trees
178    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
179    pub enum DenseMerkleMetadata {
180        /// V1 Dense Merkle Metadata
181        V1(DenseMerkleMetadataV1),
182    }
183
184    impl Default for DenseMerkleMetadata {
185        fn default() -> Self {
186            Self::V1(Default::default())
187        }
188    }
189
190    impl DenseMerkleMetadata {
191        /// Create a new dense Merkle metadata object from the given Merkle
192        /// root and version
193        pub fn new(root: MerkleRoot, version: u64) -> Self {
194            let metadata = DenseMerkleMetadataV1 { root, version };
195            Self::V1(metadata)
196        }
197
198        /// Get the Merkle root of the dense Metadata
199        pub fn root(&self) -> &MerkleRoot {
200            match self {
201                DenseMerkleMetadata::V1(metadata) => &metadata.root,
202            }
203        }
204
205        /// Get the version of the dense Metadata
206        pub fn version(&self) -> u64 {
207            match self {
208                DenseMerkleMetadata::V1(metadata) => metadata.version,
209            }
210        }
211    }
212
213    /// Metadata for dense Merkle trees
214    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
215    pub struct DenseMerkleMetadataV1 {
216        /// The root hash of the dense Merkle tree structure
217        pub root: MerkleRoot,
218        /// The version of the dense Merkle tree structure is equal to the number of
219        /// leaves. Every time we append a new leaf to the Merkle tree data set, we
220        /// increment the version number.
221        pub version: u64,
222    }
223
224    impl Default for DenseMerkleMetadataV1 {
225        fn default() -> Self {
226            let empty_merkle_tree = binary::root_calculator::MerkleRootCalculator::new();
227            Self {
228                root: empty_merkle_tree.root(),
229                version: 0,
230            }
231        }
232    }
233
234    impl From<DenseMerkleMetadataV1> for DenseMerkleMetadata {
235        fn from(value: DenseMerkleMetadataV1) -> Self {
236            Self::V1(value)
237        }
238    }
239
240    /// Metadata for sparse Merkle trees
241    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
242    pub enum SparseMerkleMetadata {
243        /// V1 Sparse Merkle Metadata
244        V1(SparseMerkleMetadataV1),
245    }
246
247    impl Default for SparseMerkleMetadata {
248        fn default() -> Self {
249            Self::V1(Default::default())
250        }
251    }
252
253    impl SparseMerkleMetadata {
254        /// Create a new sparse Merkle metadata object from the given Merkle
255        /// root
256        pub fn new(root: MerkleRoot) -> Self {
257            let metadata = SparseMerkleMetadataV1 { root };
258            Self::V1(metadata)
259        }
260
261        /// Get the Merkle root stored in the metadata
262        pub fn root(&self) -> &MerkleRoot {
263            match self {
264                SparseMerkleMetadata::V1(metadata) => &metadata.root,
265            }
266        }
267    }
268
269    /// Metadata V1 for sparse Merkle trees
270    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
271    pub struct SparseMerkleMetadataV1 {
272        /// The root hash of the sparse Merkle tree structure
273        pub root: MerkleRoot,
274    }
275
276    impl Default for SparseMerkleMetadataV1 {
277        fn default() -> Self {
278            let empty_merkle_tree = sparse::in_memory::MerkleTree::new();
279            Self {
280                root: empty_merkle_tree.root(),
281            }
282        }
283    }
284
285    impl From<SparseMerkleMetadataV1> for SparseMerkleMetadata {
286        fn from(value: SparseMerkleMetadataV1) -> Self {
287            Self::V1(value)
288        }
289    }
290
291    /// The table of BMT data for Fuel blocks.
292    pub struct FuelBlockMerkleData;
293
294    impl Mappable for FuelBlockMerkleData {
295        type Key = u64;
296        type OwnedKey = Self::Key;
297        type Value = binary::Primitive;
298        type OwnedValue = Self::Value;
299    }
300
301    /// The metadata table for [`FuelBlockMerkleData`] table.
302    pub struct FuelBlockMerkleMetadata;
303
304    impl Mappable for FuelBlockMerkleMetadata {
305        type Key = DenseMetadataKey<BlockHeight>;
306        type OwnedKey = Self::Key;
307        type Value = DenseMerkleMetadata;
308        type OwnedValue = Self::Value;
309    }
310}