casper-storage 2.1.1

Storage for a node on the Casper network.
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use datasize::DataSize;
use lmdb::{
    Cursor, Database, DatabaseFlags, Environment, RwCursor, RwTransaction,
    Transaction as LmdbTransaction,
};
use serde::de::DeserializeOwned;
#[cfg(test)]
use serde::Serialize;
use std::{collections::BTreeSet, marker::PhantomData};
use tracing::error;

use casper_types::{
    bytesrepr::{FromBytes, ToBytes},
    execution::ExecutionResult,
    Approval, BlockBody, BlockBodyV1, BlockHash, BlockHeader, BlockHeaderV1, BlockSignatures,
    BlockSignaturesV1, Deploy, DeployHash, Digest, Transaction, TransactionHash, TransferV1,
};

use super::{
    super::{
        error::BlockStoreError,
        types::{ApprovalsHashes, DeployMetadataV1, LegacyApprovalsHashes, Transfers},
        DbRawBytesSpec,
    },
    lmdb_ext::{self, LmdbExtError, TransactionExt, WriteTransactionExt},
};

pub(crate) trait VersionedKey: ToBytes {
    type Legacy: AsRef<[u8]>;

    fn legacy_key(&self) -> Option<&Self::Legacy>;
}

pub(crate) trait VersionedValue: ToBytes + FromBytes {
    type Legacy: 'static + DeserializeOwned + Into<Self>;
}

impl VersionedKey for TransactionHash {
    type Legacy = DeployHash;

    fn legacy_key(&self) -> Option<&Self::Legacy> {
        match self {
            TransactionHash::Deploy(deploy_hash) => Some(deploy_hash),
            TransactionHash::V1(_) => None,
        }
    }
}

impl VersionedKey for BlockHash {
    type Legacy = BlockHash;

    fn legacy_key(&self) -> Option<&Self::Legacy> {
        Some(self)
    }
}

impl VersionedKey for Digest {
    type Legacy = Digest;

    fn legacy_key(&self) -> Option<&Self::Legacy> {
        Some(self)
    }
}

impl VersionedValue for Transaction {
    type Legacy = Deploy;
}

impl VersionedValue for BlockHeader {
    type Legacy = BlockHeaderV1;
}

impl VersionedValue for BlockBody {
    type Legacy = BlockBodyV1;
}

impl VersionedValue for ApprovalsHashes {
    type Legacy = LegacyApprovalsHashes;
}

impl VersionedValue for ExecutionResult {
    type Legacy = DeployMetadataV1;
}

impl VersionedValue for BTreeSet<Approval> {
    type Legacy = BTreeSet<Approval>;
}

impl VersionedValue for BlockSignatures {
    type Legacy = BlockSignaturesV1;
}

impl VersionedValue for Transfers {
    type Legacy = Vec<TransferV1>;
}

/// A pair of databases, one holding the original legacy form of the data, and the other holding the
/// new versioned, future-proof form of the data.
///
/// Specific entries should generally not be repeated - they will either be held in the legacy or
/// the current DB, but not both.  Data is not migrated from legacy to current, but newly-stored
/// data will always be written to the current DB, even if it is of the type `V::Legacy`.
///
/// Exceptions to this can occur if a pre-existing legacy entry is re-stored, in which case there
/// will be a duplicated entry in the `legacy` and `current` DBs.  This should not be a common
/// occurrence though.
#[derive(Eq, PartialEq, DataSize, Debug)]
pub(crate) struct VersionedDatabases<K, V> {
    /// Legacy form of the data, with the key as `K::Legacy` type (converted to bytes using
    /// `AsRef<[u8]>`) and the value bincode-encoded.
    #[data_size(skip)]
    pub legacy: Database,
    /// Current form of the data, with the key as `K` bytesrepr-encoded and the value as `V` also
    /// bytesrepr-encoded.
    #[data_size(skip)]
    pub current: Database,
    _phantom: PhantomData<(K, V)>,
}

impl<K, V> Clone for VersionedDatabases<K, V> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<K, V> Copy for VersionedDatabases<K, V> {}

impl<K, V> VersionedDatabases<K, V>
where
    K: VersionedKey + std::fmt::Display,
    V: VersionedValue + 'static,
{
    pub(super) fn new(
        env: &Environment,
        legacy_name: &str,
        current_name: &str,
    ) -> Result<Self, lmdb::Error> {
        Ok(VersionedDatabases {
            legacy: env.create_db(Some(legacy_name), DatabaseFlags::empty())?,
            current: env.create_db(Some(current_name), DatabaseFlags::empty())?,
            _phantom: PhantomData,
        })
    }

    pub(super) fn put(
        &self,
        txn: &mut RwTransaction,
        key: &K,
        value: &V,
        overwrite: bool,
    ) -> Result<bool, LmdbExtError> {
        txn.put_value_bytesrepr(self.current, key, value, overwrite)
    }

    pub(super) fn get<Tx: LmdbTransaction>(
        &self,
        txn: &Tx,
        key: &K,
    ) -> Result<Option<V>, LmdbExtError> {
        match txn.get_value_bytesrepr(self.current, key) {
            Ok(Some(value)) => return Ok(Some(value)),
            Ok(None) => {
                // check legacy db
            }
            Err(err) => {
                error!(%err, "versioned_database: failed to retrieve record from current db");
                return Err(err);
            }
        }

        let legacy_key = match key.legacy_key() {
            Some(key) => key,
            None => return Ok(None),
        };

        Ok(txn
            .get_value::<_, V::Legacy>(self.legacy, legacy_key)?
            .map(Into::into))
    }

    pub(super) fn get_raw<Tx: LmdbTransaction>(
        &self,
        txn: &Tx,
        key: &[u8],
    ) -> Result<Option<DbRawBytesSpec>, LmdbExtError> {
        if key.is_empty() {
            return Ok(None);
        }
        let value = txn.get(self.current, &key);
        match value {
            Ok(raw_bytes) => Ok(Some(DbRawBytesSpec::new_current(raw_bytes))),
            Err(lmdb::Error::NotFound) => {
                let value = txn.get(self.legacy, &key);
                match value {
                    Ok(raw_bytes) => Ok(Some(DbRawBytesSpec::new_legacy(raw_bytes))),
                    Err(lmdb::Error::NotFound) => Ok(None),
                    Err(err) => Err(err.into()),
                }
            }
            Err(err) => Err(err.into()),
        }
    }

    pub(super) fn exists<Tx: LmdbTransaction>(
        &self,
        txn: &Tx,
        key: &K,
    ) -> Result<bool, LmdbExtError> {
        if txn.value_exists_bytesrepr(self.current, key)? {
            return Ok(true);
        }

        let legacy_key = match key.legacy_key() {
            Some(key) => key,
            None => return Ok(false),
        };

        txn.value_exists(self.legacy, legacy_key)
    }

    /// Deletes the value under `key` from both the current and legacy DBs.
    ///
    /// Returns `Ok` if the value is successfully deleted from either or both the DBs, or if the
    /// value did not exist in either.
    pub(super) fn delete(&self, txn: &mut RwTransaction, key: &K) -> Result<(), LmdbExtError> {
        let serialized_key = lmdb_ext::serialize_bytesrepr(key)?;
        let current_result = match txn.del(self.current, &serialized_key, None) {
            Ok(_) | Err(lmdb::Error::NotFound) => Ok(()),
            Err(error) => Err(error.into()),
        };
        // Avoid returning early for the case where `current_result` is Ok, since some
        // `VersionedDatabases` could possibly have the same entry in both DBs.

        let legacy_key = match key.legacy_key() {
            Some(key) => key,
            None => return current_result,
        };

        let legacy_result = match txn.del(self.legacy, legacy_key, None) {
            Ok(_) | Err(lmdb::Error::NotFound) => Ok(()),
            Err(error) => Err(error.into()),
        };

        match (current_result, legacy_result) {
            (Err(error), _) => Err(error),
            (_, Err(error)) => Err(error),
            (Ok(_), Ok(_)) => Ok(()),
        }
    }

    /// Iterates every row in the current database, deserializing the value and calling `f` with the
    /// cursor and the parsed value.
    pub(super) fn for_each_value_in_current<'a, F>(
        &self,
        txn: &'a mut RwTransaction,
        f: &mut F,
    ) -> Result<(), BlockStoreError>
    where
        F: FnMut(&mut RwCursor<'a>, V) -> Result<(), BlockStoreError>,
    {
        let mut cursor = txn
            .open_rw_cursor(self.current)
            .map_err(|err| BlockStoreError::InternalStorage(Box::new(err)))?;
        for row in cursor.iter() {
            let (_, raw_val) =
                row.map_err(|err| BlockStoreError::InternalStorage(Box::new(err)))?;
            let value: V = lmdb_ext::deserialize_bytesrepr(raw_val)
                .map_err(|err| BlockStoreError::InternalStorage(Box::new(err)))?;
            f(&mut cursor, value)?;
        }
        Ok(())
    }

    /// Iterates every row in the legacy database, deserializing the value and calling `f` with the
    /// cursor and the parsed value.
    pub(super) fn for_each_value_in_legacy<'a, F>(
        &self,
        txn: &'a mut RwTransaction,
        f: &mut F,
    ) -> Result<(), BlockStoreError>
    where
        F: FnMut(&mut RwCursor<'a>, V) -> Result<(), BlockStoreError>,
    {
        let mut cursor = txn
            .open_rw_cursor(self.legacy)
            .map_err(|err| BlockStoreError::InternalStorage(Box::new(err)))?;
        for row in cursor.iter() {
            let (_, raw_val) =
                row.map_err(|err| BlockStoreError::InternalStorage(Box::new(err)))?;
            let value: V::Legacy = lmdb_ext::deserialize(raw_val)
                .map_err(|err| BlockStoreError::InternalStorage(Box::new(err)))?;
            f(&mut cursor, value.into())?;
        }
        Ok(())
    }

    /// Writes to the `legacy` database.
    #[cfg(test)]
    pub(super) fn put_legacy(
        &self,
        txn: &mut RwTransaction,
        legacy_key: &K::Legacy,
        legacy_value: &V::Legacy,
        overwrite: bool,
    ) -> bool
    where
        V::Legacy: Serialize,
    {
        txn.put_value(self.legacy, legacy_key, legacy_value, overwrite)
            .expect("should put legacy value")
    }
}

#[cfg(test)]
mod tests {
    use crate::block_store::lmdb::lmdb_block_store::new_environment;
    use lmdb::WriteFlags;
    use std::collections::HashMap;

    use tempfile::TempDir;

    use casper_types::testing::TestRng;

    use super::*;

    struct Fixture {
        rng: TestRng,
        env: Environment,
        dbs: VersionedDatabases<TransactionHash, Transaction>,
        random_transactions: HashMap<TransactionHash, Transaction>,
        legacy_transactions: HashMap<DeployHash, Deploy>,
        _data_dir: TempDir,
    }

    impl Fixture {
        fn new() -> Fixture {
            let rng = TestRng::new();
            let data_dir = TempDir::new().expect("should create temp dir");
            let env = new_environment(1024 * 1024, data_dir.path()).unwrap();
            let dbs = VersionedDatabases::new(&env, "legacy", "current").unwrap();
            let mut fixture = Fixture {
                rng,
                env,
                dbs,
                random_transactions: HashMap::new(),
                legacy_transactions: HashMap::new(),
                _data_dir: data_dir,
            };
            for _ in 0..3 {
                let transaction = Transaction::random(&mut fixture.rng);
                assert!(fixture
                    .random_transactions
                    .insert(transaction.hash(), transaction)
                    .is_none());
                let deploy = Deploy::random(&mut fixture.rng);
                assert!(fixture
                    .legacy_transactions
                    .insert(*deploy.hash(), deploy)
                    .is_none());
            }
            fixture
        }
    }

    #[test]
    fn should_put() {
        let fixture = Fixture::new();
        let (transaction_hash, transaction) = fixture.random_transactions.iter().next().unwrap();

        // Should return `true` on first `put`.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        assert!(fixture
            .dbs
            .put(&mut txn, transaction_hash, transaction, true)
            .unwrap());

        // Should return `false` on duplicate `put` if not set to overwrite.
        assert!(!fixture
            .dbs
            .put(&mut txn, transaction_hash, transaction, false)
            .unwrap());

        // Should return `true` on duplicate `put` if set to overwrite.
        assert!(fixture
            .dbs
            .put(&mut txn, transaction_hash, transaction, true)
            .unwrap());
    }

    #[test]
    fn should_get() {
        let mut fixture = Fixture::new();
        let (transaction_hash, transaction) = fixture.random_transactions.iter().next().unwrap();
        let (deploy_hash, deploy) = fixture.legacy_transactions.iter().next().unwrap();

        // Inject the deploy into the legacy DB and store the random transaction in the current DB.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        assert!(fixture.dbs.put_legacy(&mut txn, deploy_hash, deploy, true));
        assert!(fixture
            .dbs
            .put(&mut txn, transaction_hash, transaction, true)
            .unwrap());
        txn.commit().unwrap();

        // Should get the deploy.
        let txn = fixture.env.begin_ro_txn().unwrap();
        assert_eq!(
            fixture
                .dbs
                .get(&txn, &TransactionHash::from(*deploy_hash))
                .unwrap(),
            Some(Transaction::from(deploy.clone()))
        );

        // Should get the random transaction.
        assert_eq!(
            fixture.dbs.get(&txn, transaction_hash).unwrap(),
            Some(transaction.clone())
        );

        // Should return `Ok(None)` for non-existent data.
        let random_hash = Transaction::random(&mut fixture.rng).hash();
        assert!(fixture.dbs.get(&txn, &random_hash).unwrap().is_none());
    }

    #[test]
    fn should_exist() {
        let mut fixture = Fixture::new();
        let (transaction_hash, transaction) = fixture.random_transactions.iter().next().unwrap();
        let (deploy_hash, deploy) = fixture.legacy_transactions.iter().next().unwrap();

        // Inject the deploy into the legacy DB and store the random transaction in the current DB.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        assert!(fixture.dbs.put_legacy(&mut txn, deploy_hash, deploy, true));
        assert!(fixture
            .dbs
            .put(&mut txn, transaction_hash, transaction, true)
            .unwrap());
        txn.commit().unwrap();

        // The deploy should exist.
        let txn = fixture.env.begin_ro_txn().unwrap();
        assert!(fixture
            .dbs
            .exists(&txn, &TransactionHash::from(*deploy_hash))
            .unwrap());

        // The random transaction should exist.
        assert!(fixture.dbs.exists(&txn, transaction_hash).unwrap());

        // Random data should not exist.
        let random_hash = Transaction::random(&mut fixture.rng).hash();
        assert!(!fixture.dbs.exists(&txn, &random_hash).unwrap());
    }

    #[test]
    fn should_delete() {
        let mut fixture = Fixture::new();
        let (transaction_hash, transaction) = fixture.random_transactions.iter().next().unwrap();
        let (deploy_hash, deploy) = fixture.legacy_transactions.iter().next().unwrap();

        // Inject the deploy into the legacy DB and store the random transaction in the current DB.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        assert!(fixture.dbs.put_legacy(&mut txn, deploy_hash, deploy, true));
        assert!(fixture
            .dbs
            .put(&mut txn, transaction_hash, transaction, true)
            .unwrap());
        // Also store the legacy deploy in the `current` DB.  While being an edge case, we still
        // need to ensure that deleting removes both copies of the deploy.
        assert!(fixture
            .dbs
            .put(
                &mut txn,
                &TransactionHash::from(*deploy_hash),
                &Transaction::from(deploy.clone()),
                true
            )
            .unwrap());
        txn.commit().unwrap();

        // Should delete the deploy.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        fixture
            .dbs
            .delete(&mut txn, &TransactionHash::from(*deploy_hash))
            .unwrap();
        assert!(!fixture
            .dbs
            .exists(&txn, &TransactionHash::from(*deploy_hash))
            .unwrap());

        // Should delete the random transaction.
        fixture.dbs.delete(&mut txn, transaction_hash).unwrap();
        assert!(!fixture.dbs.exists(&txn, transaction_hash).unwrap());

        // Should report success when attempting to delete non-existent data.
        let random_hash = Transaction::random(&mut fixture.rng).hash();
        fixture.dbs.delete(&mut txn, &random_hash).unwrap();
    }

    #[test]
    fn should_iterate_current() {
        let fixture = Fixture::new();

        // Store all random transactions.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        for (transaction_hash, transaction) in fixture.random_transactions.iter() {
            assert!(fixture
                .dbs
                .put(&mut txn, transaction_hash, transaction, true)
                .unwrap());
        }
        txn.commit().unwrap();

        // Iterate `current`, deleting each cursor entry and gathering the visited values in a map.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        let mut visited = HashMap::new();
        let mut visitor = |cursor: &mut RwCursor, transaction: Transaction| {
            cursor.del(WriteFlags::empty()).unwrap();
            let _ = visited.insert(transaction.hash(), transaction);
            Ok(())
        };
        fixture
            .dbs
            .for_each_value_in_current(&mut txn, &mut visitor)
            .unwrap();
        txn.commit().unwrap();

        // Ensure all values were visited and the DB doesn't contain them any more.
        assert_eq!(visited, fixture.random_transactions);
        let txn = fixture.env.begin_ro_txn().unwrap();
        for transaction_hash in fixture.random_transactions.keys() {
            assert!(!fixture.dbs.exists(&txn, transaction_hash).unwrap());
        }

        // Ensure a second run is a no-op.
        let mut visitor = |_cursor: &mut RwCursor, _transaction: Transaction| {
            panic!("should never get called");
        };
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        fixture
            .dbs
            .for_each_value_in_current(&mut txn, &mut visitor)
            .unwrap();
    }

    #[test]
    fn should_iterate_legacy() {
        let fixture = Fixture::new();

        // Store all legacy transactions.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        for (deploy_hash, deploy) in fixture.legacy_transactions.iter() {
            assert!(fixture.dbs.put_legacy(&mut txn, deploy_hash, deploy, true));
        }
        txn.commit().unwrap();

        // Iterate `legacy`, deleting each cursor entry and gathering the visited values in a map.
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        let mut visited = HashMap::new();
        let mut visitor = |cursor: &mut RwCursor, transaction: Transaction| {
            cursor.del(WriteFlags::empty()).unwrap();
            match transaction {
                Transaction::Deploy(deploy) => {
                    let _ = visited.insert(*deploy.hash(), deploy);
                }
                Transaction::V1(_) => unreachable!(),
            }
            Ok(())
        };
        fixture
            .dbs
            .for_each_value_in_legacy(&mut txn, &mut visitor)
            .unwrap();
        txn.commit().unwrap();

        // Ensure all values were visited and the DB doesn't contain them any more.
        assert_eq!(visited, fixture.legacy_transactions);
        let txn = fixture.env.begin_ro_txn().unwrap();
        for deploy_hash in fixture.legacy_transactions.keys() {
            assert!(!fixture
                .dbs
                .exists(&txn, &TransactionHash::from(*deploy_hash))
                .unwrap());
        }

        // Ensure a second run is a no-op.
        let mut visitor = |_cursor: &mut RwCursor, _transaction: Transaction| {
            panic!("should never get called");
        };
        let mut txn = fixture.env.begin_rw_txn().unwrap();
        fixture
            .dbs
            .for_each_value_in_legacy(&mut txn, &mut visitor)
            .unwrap();
    }

    #[test]
    fn should_get_on_empty_key() {
        let fixture = Fixture::new();
        let txn = fixture.env.begin_ro_txn().unwrap();
        let key = vec![];
        let res = fixture.dbs.get_raw(&txn, &key);
        assert!(matches!(res, Ok(None)));
    }
}