namada_storage 0.150.1

Namada ledger storage
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! This crate provides
//!
//! - [`StorageRead`] and [`StorageWrite`] (high-level) and [`DB`] (low-level)
//!   traits
//! - `MockDB` [`DB`] implementation for testing
//! - [`collections`] with generic lazy collections for storage
//! - [`conversion_state`] for shielded token rewards
//! - helpers for storage iteration

#![doc(html_favicon_url = "https://dev.namada.net/master/favicon.png")]
#![doc(html_logo_url = "https://dev.namada.net/master/rustdoc-logo.png")]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(rustdoc::private_intra_doc_links)]
#![warn(
    missing_docs,
    rust_2018_idioms,
    clippy::cast_sign_loss,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_lossless,
    clippy::arithmetic_side_effects,
    clippy::dbg_macro,
    clippy::print_stdout,
    clippy::print_stderr
)]

pub mod collections;
pub mod conversion_state;
mod db;
mod error;
pub mod mockdb;
pub mod tx_queue;
pub mod types;

pub use db::{Error as DbError, Result as DbResult, *};
pub use error::{CustomError, Error, OptionExt, Result, ResultExt};
use namada_core::address::Address;
use namada_core::borsh::{BorshDeserialize, BorshSerialize, BorshSerializeExt};
use namada_core::chain::ChainId;
pub use namada_core::chain::{
    BlockHash, BlockHeader, BlockHeight, Epoch, Epochs,
};
pub use namada_core::hash::{Hash, StorageHasher};
pub use namada_core::storage::*;

/// Common storage read interface
pub trait StorageRead {
    /// Storage read prefix iterator
    type PrefixIter<'iter>
    where
        Self: 'iter;

    /// Storage read Borsh encoded value. It will try to read from the storage
    /// and decode it if found.
    fn read<T: BorshDeserialize>(&self, key: &Key) -> Result<Option<T>> {
        let bytes = self.read_bytes(key)?;
        match bytes {
            Some(bytes) => {
                let val = T::try_from_slice(&bytes).into_storage_result()?;
                Ok(Some(val))
            }
            None => Ok(None),
        }
    }

    /// Storage read raw bytes. It will try to read from the storage.
    fn read_bytes(&self, key: &Key) -> Result<Option<Vec<u8>>>;

    /// Storage `has_key` in. It will try to read from the storage.
    fn has_key(&self, key: &Key) -> Result<bool>;

    /// Storage prefix iterator ordered by the storage keys. It will try to get
    /// an iterator from the storage.
    ///
    /// For a more user-friendly iterator API, use [`fn@iter_prefix`] or
    /// [`fn@iter_prefix_bytes`] instead.
    fn iter_prefix<'iter>(
        &'iter self,
        prefix: &Key,
    ) -> Result<Self::PrefixIter<'iter>>;

    /// Storage prefix iterator. It will try to read from the storage.
    fn iter_next<'iter>(
        &'iter self,
        iter: &mut Self::PrefixIter<'iter>,
    ) -> Result<Option<(String, Vec<u8>)>>;

    /// Getting the chain ID.
    fn get_chain_id(&self) -> Result<ChainId>;

    /// Getting the block height. The height is that of the block to which the
    /// current transaction is being applied.
    fn get_block_height(&self) -> Result<BlockHeight>;

    /// Getting the block header.
    fn get_block_header(
        &self,
        height: BlockHeight,
    ) -> Result<Option<BlockHeader>>;

    /// Getting the block epoch. The epoch is that of the block to which the
    /// current transaction is being applied.
    fn get_block_epoch(&self) -> Result<Epoch>;

    /// Given the information about predecessor block epochs
    fn get_pred_epochs(&self) -> Result<Epochs>;

    /// Get the transaction index.
    fn get_tx_index(&self) -> Result<TxIndex>;

    /// Get the native token address
    fn get_native_token(&self) -> Result<Address>;

    /// Get the height of the first block of the current epoch.
    fn get_current_epoch_start_height(&self) -> Result<BlockHeight> {
        Ok(self
            .get_pred_epochs()?
            .first_block_heights()
            .last()
            .copied()
            .expect("The block height of the current epoch should be known"))
    }

    /// Get the height of the first block of the given epoch.
    fn get_epoch_start_height(
        &self,
        epoch: Epoch,
    ) -> Result<Option<BlockHeight>> {
        let epochs = self.get_pred_epochs()?;
        Ok(epochs.get_start_height_of_epoch(epoch))
    }

    /// Given the epoch at the given block height.
    fn get_epoch_at_height(
        &self,
        height: BlockHeight,
    ) -> Result<Option<Epoch>> {
        let epochs = self.get_pred_epochs()?;
        Ok(epochs.get_epoch(height))
    }
}

/// Common storage write interface
pub trait StorageWrite {
    /// Write a value to be encoded with Borsh at the given key to storage.
    fn write<T: BorshSerialize>(&mut self, key: &Key, val: T) -> Result<()> {
        let bytes = val.serialize_to_vec();
        self.write_bytes(key, bytes)
    }

    /// Write a value as bytes at the given key to storage.
    fn write_bytes(&mut self, key: &Key, val: impl AsRef<[u8]>) -> Result<()>;

    /// Delete a value at the given key from storage.
    fn delete(&mut self, key: &Key) -> Result<()>;

    /// Delete all key-vals with a matching prefix.
    fn delete_prefix(&mut self, prefix: &Key) -> Result<()>
    where
        Self: StorageRead + Sized,
    {
        let keys = iter_prefix_bytes(self, prefix)?
            .map(|res| {
                let (key, _val) = res?;
                Ok(key)
            })
            .collect::<Result<Vec<Key>>>();
        for key in keys? {
            // Skip validity predicates as they cannot be deleted
            if key.is_validity_predicate().is_none() {
                self.delete(&key)?;
            }
        }
        Ok(())
    }
}

/// Iterate items matching the given prefix, ordered by the storage keys.
pub fn iter_prefix_bytes<'a>(
    storage: &'a impl StorageRead,
    prefix: &Key,
) -> Result<impl Iterator<Item = Result<(Key, Vec<u8>)>> + 'a> {
    let mut iter = storage.iter_prefix(prefix)?;
    let iter = std::iter::from_fn(move || {
        match storage.iter_next(&mut iter) {
            Ok(Some((key, val))) => {
                let key = match Key::parse(key).into_storage_result() {
                    Ok(key) => key,
                    Err(err) => {
                        // Propagate key encoding errors into Iterator's Item
                        return Some(Err(err));
                    }
                };
                Some(Ok((key, val)))
            }
            Ok(None) => None,
            Err(err) => {
                // Propagate `iter_next` errors into Iterator's Item
                Some(Err(err))
            }
        }
    });
    Ok(iter)
}

/// Iterate Borsh encoded items matching the given prefix, ordered by the
/// storage keys.
pub fn iter_prefix<'a, T>(
    storage: &'a impl StorageRead,
    prefix: &Key,
) -> Result<impl Iterator<Item = Result<(Key, T)>> + 'a>
where
    T: BorshDeserialize,
{
    let mut iter = storage.iter_prefix(prefix)?;
    let iter = std::iter::from_fn(move || {
        match storage.iter_next(&mut iter) {
            Ok(Some((key, val))) => {
                let key = match Key::parse(key).into_storage_result() {
                    Ok(key) => key,
                    Err(err) => {
                        // Propagate key encoding errors into Iterator's Item
                        return Some(Err(err));
                    }
                };
                let val = match T::try_from_slice(&val).into_storage_result() {
                    Ok(val) => val,
                    Err(err) => {
                        // Propagate val encoding errors into Iterator's Item
                        return Some(Err(err));
                    }
                };
                Some(Ok((key, val)))
            }
            Ok(None) => None,
            Err(err) => {
                // Propagate `iter_next` errors into Iterator's Item
                Some(Err(err))
            }
        }
    });
    Ok(iter)
}

/// Iterate Borsh encoded items matching the given prefix and passing the given
/// `filter` predicate, ordered by the storage keys.
///
/// The `filter` predicate is a function from a storage key to bool and only
/// the items that return `true` will be returned from the iterator.
///
/// Note that this is preferable over the regular `iter_prefix` combined with
/// the iterator's `filter` function as it avoids trying to decode values that
/// don't pass the filter. For `iter_prefix_bytes`, `filter` works fine.
pub fn iter_prefix_with_filter_map<'a, K, T, F>(
    storage: &'a impl StorageRead,
    prefix: &Key,
    filter_map: F,
) -> Result<impl Iterator<Item = Result<(K, T)>> + 'a>
where
    T: BorshDeserialize,
    F: Fn(&Key) -> Option<K> + 'a,
{
    let mut iter = storage.iter_prefix(prefix)?;
    let iter = std::iter::from_fn(move || {
        // The loop is for applying filter - we `continue` when the current key
        // doesn't pass the predicate.
        loop {
            match storage.iter_next(&mut iter) {
                Ok(Some((key, val))) => {
                    let key = match Key::parse(key).into_storage_result() {
                        Ok(key) => key,
                        Err(err) => {
                            // Propagate key encoding errors into Iterator's
                            // Item
                            return Some(Err(err));
                        }
                    };
                    // Check the predicate
                    let Some(mapped_key) = filter_map(&key) else {
                        continue;
                    };
                    let val =
                        match T::try_from_slice(&val).into_storage_result() {
                            Ok(val) => val,
                            Err(err) => {
                                // Propagate val encoding errors into Iterator's
                                // Item
                                return Some(Err(err));
                            }
                        };
                    return Some(Ok((mapped_key, val)));
                }
                Ok(None) => return None,
                Err(err) => {
                    // Propagate `iter_next` errors into Iterator's Item
                    return Some(Err(err));
                }
            }
        }
    });
    Ok(iter)
}

/// Iterate Borsh encoded items matching the given prefix and passing the given
/// `filter` predicate, ordered by the storage keys.
///
/// The `filter` predicate is a function from a storage key to bool and only
/// the items that return `true` will be returned from the iterator.
///
/// Note that this is preferable over the regular `iter_prefix` combined with
/// the iterator's `filter` function as it avoids trying to decode values that
/// don't pass the filter. For `iter_prefix_bytes`, `filter` works fine.
pub fn iter_prefix_with_filter<'a, T, F>(
    storage: &'a impl StorageRead,
    prefix: &Key,
    filter: F,
) -> Result<impl Iterator<Item = Result<(Key, T)>> + 'a>
where
    T: BorshDeserialize,
    F: Fn(&Key) -> bool + 'a,
{
    let mut iter = storage.iter_prefix(prefix)?;
    let iter = std::iter::from_fn(move || {
        // The loop is for applying filter - we `continue` when the current key
        // doesn't pass the predicate.
        loop {
            match storage.iter_next(&mut iter) {
                Ok(Some((key, val))) => {
                    let key = match Key::parse(key).into_storage_result() {
                        Ok(key) => key,
                        Err(err) => {
                            // Propagate key encoding errors into Iterator's
                            // Item
                            return Some(Err(err));
                        }
                    };
                    // Check the predicate
                    if !filter(&key) {
                        continue;
                    }
                    let val =
                        match T::try_from_slice(&val).into_storage_result() {
                            Ok(val) => val,
                            Err(err) => {
                                // Propagate val encoding errors into Iterator's
                                // Item
                                return Some(Err(err));
                            }
                        };
                    return Some(Ok((key, val)));
                }
                Ok(None) => return None,
                Err(err) => {
                    // Propagate `iter_next` errors into Iterator's Item
                    return Some(Err(err));
                }
            }
        }
    });
    Ok(iter)
}

/// Helpers for testing components that depend on storage
#[cfg(any(test, feature = "testing"))]
pub mod testing {
    use conversion_state::ReadConversionState;
    use namada_core::address;
    use namada_core::chain::ChainId;
    use namada_core::collections::HashMap;
    pub use namada_core::storage::testing::*;

    use super::mockdb::MockDB;
    use super::*;
    use crate::conversion_state::{ConversionState, WithConversionState};

    /// Storage with a mock DB for testing
    pub struct TestStorage {
        db: MockDB,
        chain_id: ChainId,
        height: BlockHeight,
        epoch: Epoch,
        pred_epochs: Epochs,
        native_token: Address,
        conversion_state: ConversionState,
        merkle_tree_key_filter: fn(&Key) -> bool,
        mock_block_headers: HashMap<BlockHeight, BlockHeader>,
    }

    fn merklize_all_keys(_key: &Key) -> bool {
        true
    }

    #[allow(clippy::derivable_impls)]
    impl Default for TestStorage {
        fn default() -> Self {
            Self {
                db: Default::default(),
                chain_id: ChainId::default(),
                height: BlockHeight::first(),
                epoch: Epoch::default(),
                pred_epochs: Epochs::default(),
                native_token: address::testing::nam(),
                conversion_state: ConversionState::default(),
                merkle_tree_key_filter: merklize_all_keys,
                mock_block_headers: Default::default(),
            }
        }
    }

    impl TestStorage {
        /// Set mock a block header in [`TestStorage`].
        pub fn set_mock_block_header(
            &mut self,
            height: BlockHeight,
            header: BlockHeader,
        ) {
            self.mock_block_headers.insert(height, header);
        }
    }

    impl StorageRead for TestStorage {
        type PrefixIter<'iter>
            = PrefixIter<'iter>
        where
            Self: 'iter;

        fn read_bytes(&self, key: &Key) -> Result<Option<Vec<u8>>> {
            self.db.read_subspace_val(key).into_storage_result()
        }

        fn has_key(&self, key: &Key) -> Result<bool> {
            Ok(self.read_bytes(key)?.is_some())
        }

        fn iter_prefix<'iter>(
            &'iter self,
            prefix: &Key,
        ) -> Result<Self::PrefixIter<'iter>> {
            let storage_iter = self.db.iter_prefix(Some(prefix));
            Ok(PrefixIter {
                db_iter: storage_iter,
            })
        }

        fn iter_next<'iter>(
            &'iter self,
            iter: &mut Self::PrefixIter<'iter>,
        ) -> Result<Option<(String, Vec<u8>)>> {
            Ok(iter.next())
        }

        fn get_chain_id(&self) -> Result<ChainId> {
            Ok(self.chain_id.clone())
        }

        fn get_block_height(&self) -> Result<BlockHeight> {
            Ok(self.height)
        }

        fn get_block_header(
            &self,
            height: BlockHeight,
        ) -> Result<Option<BlockHeader>> {
            Ok(self.mock_block_headers.get(&height).cloned())
        }

        fn get_block_epoch(&self) -> Result<Epoch> {
            Ok(self.epoch)
        }

        fn get_pred_epochs(&self) -> Result<Epochs> {
            Ok(self.pred_epochs.clone())
        }

        fn get_tx_index(&self) -> Result<TxIndex> {
            Ok(TxIndex::default())
        }

        fn get_native_token(&self) -> Result<Address> {
            Ok(self.native_token.clone())
        }
    }

    impl StorageWrite for TestStorage {
        fn write_bytes(
            &mut self,
            key: &Key,
            val: impl AsRef<[u8]>,
        ) -> Result<()> {
            let is_key_merklized = (self.merkle_tree_key_filter)(key);
            self.db
                .write_subspace_val(self.height, key, val, is_key_merklized)
                .into_storage_result()?;
            Ok(())
        }

        fn delete(&mut self, key: &Key) -> Result<()> {
            let is_key_merklized = (self.merkle_tree_key_filter)(key);
            self.db
                .delete_subspace_val(self.height, key, is_key_merklized)
                .into_storage_result()?;
            Ok(())
        }
    }

    impl ReadConversionState for TestStorage {
        fn conversion_state(&self) -> &ConversionState {
            &self.conversion_state
        }
    }

    impl WithConversionState for TestStorage {
        fn conversion_state_mut(&mut self) -> &mut ConversionState {
            &mut self.conversion_state
        }
    }

    /// Prefix iterator for [`TestStorage`].
    #[derive(Debug)]
    pub struct PrefixIter<'iter> {
        /// DB iterator
        pub db_iter: <MockDB as DBIter<'iter>>::PrefixIter,
    }

    impl Iterator for PrefixIter<'_> {
        type Item = (String, Vec<u8>);

        fn next(&mut self) -> Option<Self::Item> {
            self.db_iter.next().map(|(key, val, _gas)| (key, val))
        }
    }
}