fuel_core/
combined_database.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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#[cfg(feature = "rocksdb")]
use crate::state::{
    historical_rocksdb::StateRewindPolicy,
    rocks_db::DatabaseConfig,
};

use crate::{
    database::{
        database_description::{
            gas_price::GasPriceDatabase,
            off_chain::OffChain,
            on_chain::OnChain,
            relayer::Relayer,
        },
        Database,
        GenesisDatabase,
        Result as DatabaseResult,
    },
    service::DbType,
};
#[cfg(feature = "test-helpers")]
use fuel_core_chain_config::{
    StateConfig,
    StateConfigBuilder,
};
#[cfg(feature = "backup")]
use fuel_core_services::TraceErr;
#[cfg(feature = "test-helpers")]
use fuel_core_storage::tables::{
    Coins,
    ContractsAssets,
    ContractsLatestUtxo,
    ContractsRawCode,
    ContractsState,
    Messages,
};
use fuel_core_storage::Result as StorageResult;
use fuel_core_types::fuel_types::BlockHeight;
use std::path::PathBuf;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CombinedDatabaseConfig {
    pub database_path: PathBuf,
    pub database_type: DbType,
    #[cfg(feature = "rocksdb")]
    pub database_config: DatabaseConfig,
    #[cfg(feature = "rocksdb")]
    pub state_rewind_policy: StateRewindPolicy,
}

/// A database that combines the on-chain, off-chain and relayer databases into one entity.
#[derive(Default, Clone)]
pub struct CombinedDatabase {
    on_chain: Database<OnChain>,
    off_chain: Database<OffChain>,
    relayer: Database<Relayer>,
    gas_price: Database<GasPriceDatabase>,
}

impl CombinedDatabase {
    pub fn new(
        on_chain: Database<OnChain>,
        off_chain: Database<OffChain>,
        relayer: Database<Relayer>,
        gas_price: Database<GasPriceDatabase>,
    ) -> Self {
        Self {
            on_chain,
            off_chain,
            relayer,
            gas_price,
        }
    }

    #[cfg(feature = "rocksdb")]
    pub fn prune(path: &std::path::Path) -> crate::database::Result<()> {
        crate::state::rocks_db::RocksDb::<OnChain>::prune(path)?;
        crate::state::rocks_db::RocksDb::<OffChain>::prune(path)?;
        crate::state::rocks_db::RocksDb::<Relayer>::prune(path)?;
        crate::state::rocks_db::RocksDb::<GasPriceDatabase>::prune(path)?;
        Ok(())
    }

    #[cfg(feature = "backup")]
    pub fn backup(
        db_dir: &std::path::Path,
        backup_dir: &std::path::Path,
    ) -> crate::database::Result<()> {
        use tempfile::TempDir;

        let temp_backup_dir = TempDir::new()
            .trace_err("Failed to create temporary backup directory")
            .map_err(|e| anyhow::anyhow!(e))?;

        Self::backup_databases(db_dir, temp_backup_dir.path())?;

        std::fs::rename(temp_backup_dir.path(), backup_dir)
            .trace_err("Failed to move temporary backup directory")
            .map_err(|e| anyhow::anyhow!(e))?;

        Ok(())
    }

    #[cfg(feature = "backup")]
    fn backup_databases(
        db_dir: &std::path::Path,
        temp_dir: &std::path::Path,
    ) -> crate::database::Result<()> {
        crate::state::rocks_db::RocksDb::<OnChain>::backup(db_dir, temp_dir)
            .trace_err("Failed to backup on-chain database")?;

        crate::state::rocks_db::RocksDb::<OffChain>::backup(db_dir, temp_dir)
            .trace_err("Failed to backup off-chain database")?;

        crate::state::rocks_db::RocksDb::<Relayer>::backup(db_dir, temp_dir)
            .trace_err("Failed to backup relayer database")?;

        crate::state::rocks_db::RocksDb::<GasPriceDatabase>::backup(db_dir, temp_dir)
            .trace_err("Failed to backup gas-price database")?;

        Ok(())
    }

    #[cfg(feature = "backup")]
    pub fn restore(
        restore_to: &std::path::Path,
        backup_dir: &std::path::Path,
    ) -> crate::database::Result<()> {
        use tempfile::TempDir;

        let temp_restore_dir = TempDir::new()
            .trace_err("Failed to create temporary restore directory")
            .map_err(|e| anyhow::anyhow!(e))?;

        Self::restore_database(backup_dir, temp_restore_dir.path())?;

        std::fs::rename(temp_restore_dir.path(), restore_to)
            .trace_err("Failed to move temporary restore directory")
            .map_err(|e| anyhow::anyhow!(e))?;

        // we don't return a CombinedDatabase here
        // because the consumer can use any db config while opening it
        Ok(())
    }

    #[cfg(feature = "backup")]
    fn restore_database(
        backup_dir: &std::path::Path,
        temp_restore_dir: &std::path::Path,
    ) -> crate::database::Result<()> {
        crate::state::rocks_db::RocksDb::<OnChain>::restore(temp_restore_dir, backup_dir)
            .trace_err("Failed to backup on-chain database")?;

        crate::state::rocks_db::RocksDb::<OffChain>::restore(
            temp_restore_dir,
            backup_dir,
        )
        .trace_err("Failed to backup off-chain database")?;

        crate::state::rocks_db::RocksDb::<Relayer>::restore(temp_restore_dir, backup_dir)
            .trace_err("Failed to backup relayer database")?;

        crate::state::rocks_db::RocksDb::<GasPriceDatabase>::restore(
            temp_restore_dir,
            backup_dir,
        )
        .trace_err("Failed to backup gas-price database")?;

        Ok(())
    }

    #[cfg(feature = "rocksdb")]
    pub fn open(
        path: &std::path::Path,
        state_rewind_policy: StateRewindPolicy,
        database_config: DatabaseConfig,
    ) -> crate::database::Result<Self> {
        // Split the fds in equitable manner between the databases

        let max_fds = match database_config.max_fds {
            -1 => -1,
            _ => database_config.max_fds.saturating_div(4),
        };

        // TODO: Use different cache sizes for different databases
        let on_chain = Database::open_rocksdb(
            path,
            state_rewind_policy,
            DatabaseConfig {
                max_fds,
                ..database_config
            },
        )?;
        let off_chain = Database::open_rocksdb(
            path,
            state_rewind_policy,
            DatabaseConfig {
                max_fds,
                ..database_config
            },
        )?;
        let relayer = Database::open_rocksdb(
            path,
            StateRewindPolicy::NoRewind,
            DatabaseConfig {
                max_fds,
                ..database_config
            },
        )?;
        let gas_price = Database::open_rocksdb(
            path,
            state_rewind_policy,
            DatabaseConfig {
                max_fds,
                ..database_config
            },
        )?;
        Ok(Self {
            on_chain,
            off_chain,
            relayer,
            gas_price,
        })
    }

    /// A test-only temporary rocksdb database with given rewind policy.
    #[cfg(feature = "rocksdb")]
    pub fn temp_database_with_state_rewind_policy(
        state_rewind_policy: StateRewindPolicy,
        database_config: DatabaseConfig,
    ) -> DatabaseResult<Self> {
        Ok(Self {
            on_chain: Database::rocksdb_temp(state_rewind_policy, database_config)?,
            off_chain: Database::rocksdb_temp(state_rewind_policy, database_config)?,
            relayer: Default::default(),
            gas_price: Default::default(),
        })
    }

    pub fn from_config(config: &CombinedDatabaseConfig) -> DatabaseResult<Self> {
        let combined_database = match config.database_type {
            #[cfg(feature = "rocksdb")]
            DbType::RocksDb => {
                // use a default tmp rocksdb if no path is provided
                if config.database_path.as_os_str().is_empty() {
                    tracing::warn!(
                        "No RocksDB path configured, initializing database with a tmp directory"
                    );
                    CombinedDatabase::temp_database_with_state_rewind_policy(
                        config.state_rewind_policy,
                        config.database_config,
                    )?
                } else {
                    tracing::info!(
                        "Opening database {:?} with cache size \"{:?}\" and state rewind policy \"{:?}\"",
                        config.database_path,
                        config.database_config.cache_capacity,
                        config.state_rewind_policy,
                    );
                    CombinedDatabase::open(
                        &config.database_path,
                        config.state_rewind_policy,
                        config.database_config,
                    )?
                }
            }
            DbType::InMemory => CombinedDatabase::in_memory(),
            #[cfg(not(feature = "rocksdb"))]
            _ => CombinedDatabase::in_memory(),
        };

        Ok(combined_database)
    }

    pub fn in_memory() -> Self {
        Self::new(
            Database::in_memory(),
            Database::in_memory(),
            Database::in_memory(),
            Database::in_memory(),
        )
    }

    pub fn check_version(&self) -> StorageResult<()> {
        self.on_chain.check_version()?;
        self.off_chain.check_version()?;
        self.relayer.check_version()?;
        self.gas_price.check_version()?;
        Ok(())
    }

    pub fn on_chain(&self) -> &Database<OnChain> {
        &self.on_chain
    }

    #[cfg(any(feature = "test-helpers", test))]
    pub fn on_chain_mut(&mut self) -> &mut Database<OnChain> {
        &mut self.on_chain
    }

    pub fn off_chain(&self) -> &Database<OffChain> {
        &self.off_chain
    }

    #[cfg(any(feature = "test-helpers", test))]
    pub fn off_chain_mut(&mut self) -> &mut Database<OffChain> {
        &mut self.off_chain
    }

    pub fn relayer(&self) -> &Database<Relayer> {
        &self.relayer
    }

    #[cfg(any(feature = "test-helpers", test))]
    pub fn relayer_mut(&mut self) -> &mut Database<Relayer> {
        &mut self.relayer
    }

    pub fn gas_price(&self) -> &Database<GasPriceDatabase> {
        &self.gas_price
    }

    #[cfg(any(feature = "test-helpers", test))]
    pub fn gas_price_mut(&mut self) -> &mut Database<GasPriceDatabase> {
        &mut self.gas_price
    }

    #[cfg(feature = "test-helpers")]
    pub fn read_state_config(&self) -> StorageResult<StateConfig> {
        use fuel_core_chain_config::AddTable;
        use fuel_core_producer::ports::BlockProducerDatabase;
        use fuel_core_storage::transactional::AtomicView;
        use fuel_core_types::fuel_vm::BlobData;
        use itertools::Itertools;
        let mut builder = StateConfigBuilder::default();

        macro_rules! add_tables {
            ($($table: ty),*) => {
                $(
                    let table = self
                        .on_chain()
                        .entries::<$table>(None, fuel_core_storage::iter::IterDirection::Forward)
                        .try_collect()?;
                    builder.add(table);
                )*
            };
        }

        add_tables!(
            Coins,
            Messages,
            BlobData,
            ContractsAssets,
            ContractsState,
            ContractsRawCode,
            ContractsLatestUtxo
        );

        let view = self.on_chain().latest_view()?;
        let latest_block = view.latest_block()?;
        let blocks_root =
            view.block_header_merkle_root(latest_block.header().height())?;
        let state_config =
            builder.build(Some(fuel_core_chain_config::LastBlockConfig::from_header(
                latest_block.header(),
                blocks_root,
            )))?;

        Ok(state_config)
    }

    /// Rollbacks the state of the blockchain to a specific block height.
    pub fn rollback_to<S>(
        &self,
        target_block_height: BlockHeight,
        shutdown_listener: &mut S,
    ) -> anyhow::Result<()>
    where
        S: ShutdownListener,
    {
        while !shutdown_listener.is_cancelled() {
            let on_chain_height = self
                .on_chain()
                .latest_height_from_metadata()?
                .ok_or(anyhow::anyhow!("on-chain database doesn't have height"))?;

            let off_chain_height = self
                .off_chain()
                .latest_height_from_metadata()?
                .ok_or(anyhow::anyhow!("off-chain database doesn't have height"))?;

            let gas_price_chain_height =
                self.gas_price().latest_height_from_metadata()?;

            let gas_price_rolled_back = gas_price_chain_height.is_none()
                || gas_price_chain_height.expect("We checked height before")
                    == target_block_height;

            if on_chain_height == target_block_height
                && off_chain_height == target_block_height
                && gas_price_rolled_back
            {
                break;
            }

            if on_chain_height < target_block_height {
                return Err(anyhow::anyhow!(
                    "on-chain database height({on_chain_height}) \
                    is less than target height({target_block_height})"
                ));
            }

            if off_chain_height < target_block_height {
                return Err(anyhow::anyhow!(
                    "off-chain database height({off_chain_height}) \
                    is less than target height({target_block_height})"
                ));
            }

            if let Some(gas_price_chain_height) = gas_price_chain_height {
                if gas_price_chain_height < target_block_height {
                    return Err(anyhow::anyhow!(
                        "gas-price-chain database height({gas_price_chain_height}) \
                        is less than target height({target_block_height})"
                    ));
                }
            }

            if on_chain_height > target_block_height {
                self.on_chain().rollback_last_block()?;
            }

            if off_chain_height > target_block_height {
                self.off_chain().rollback_last_block()?;
            }

            if let Some(gas_price_chain_height) = gas_price_chain_height {
                if gas_price_chain_height > target_block_height {
                    self.gas_price().rollback_last_block()?;
                }
            }
        }

        if shutdown_listener.is_cancelled() {
            return Err(anyhow::anyhow!(
                "Stop the rollback due to shutdown signal received"
            ));
        }

        Ok(())
    }

    /// This function is fundamentally different from `rollback_to` in that it
    /// will rollback the off-chain/gas-price databases if they are ahead of the
    /// on-chain database. If they don't have a height or are behind the on-chain
    /// we leave it to the caller to decide how to bring them up to date.
    /// We don't rollback the on-chain database as it is the source of truth.
    /// The target height of the rollback is the latest height of the on-chain database.
    pub fn sync_aux_db_heights<S>(&self, shutdown_listener: &mut S) -> anyhow::Result<()>
    where
        S: ShutdownListener,
    {
        while !shutdown_listener.is_cancelled() {
            let on_chain_height = match self.on_chain().latest_height_from_metadata()? {
                Some(height) => height,
                None => break, // Exit loop if on-chain height is None
            };

            let off_chain_height = self.off_chain().latest_height_from_metadata()?;
            let gas_price_height = self.gas_price().latest_height_from_metadata()?;

            // Handle off-chain rollback if necessary
            if let Some(off_height) = off_chain_height {
                if off_height > on_chain_height {
                    self.off_chain().rollback_last_block()?;
                }
            }

            // Handle gas price rollback if necessary
            if let Some(gas_height) = gas_price_height {
                if gas_height > on_chain_height {
                    self.gas_price().rollback_last_block()?;
                }
            }

            // If both off-chain and gas price heights are synced, break
            if off_chain_height.map_or(true, |h| h <= on_chain_height)
                && gas_price_height.map_or(true, |h| h <= on_chain_height)
            {
                break;
            }
        }

        Ok(())
    }
}

/// A trait for listening to shutdown signals.
pub trait ShutdownListener {
    /// Returns true if the shutdown signal has been received.
    fn is_cancelled(&self) -> bool;
}

/// A genesis database that combines the on-chain, off-chain and relayer
/// genesis databases into one entity.
#[derive(Default, Clone)]
pub struct CombinedGenesisDatabase {
    pub on_chain: GenesisDatabase<OnChain>,
    pub off_chain: GenesisDatabase<OffChain>,
}

impl CombinedGenesisDatabase {
    pub fn on_chain(&self) -> &GenesisDatabase<OnChain> {
        &self.on_chain
    }

    pub fn off_chain(&self) -> &GenesisDatabase<OffChain> {
        &self.off_chain
    }
}

#[allow(non_snake_case)]
#[cfg(feature = "backup")]
#[cfg(test)]
mod tests {
    use super::*;
    use fuel_core_storage::StorageAsMut;
    use fuel_core_types::{
        entities::coins::coin::CompressedCoin,
        fuel_tx::UtxoId,
    };
    use tempfile::TempDir;

    #[test]
    fn backup_and_restore__works_correctly__happy_path() {
        // given
        let db_dir = TempDir::new().unwrap();
        let mut combined_db = CombinedDatabase::open(
            db_dir.path(),
            StateRewindPolicy::NoRewind,
            DatabaseConfig::config_for_tests(),
        )
        .unwrap();
        let key = UtxoId::new(Default::default(), Default::default());
        let expected_value = CompressedCoin::default();

        let on_chain_db = combined_db.on_chain_mut();
        on_chain_db
            .storage_as_mut::<Coins>()
            .insert(&key, &expected_value)
            .unwrap();
        drop(combined_db);

        // when
        let backup_dir = TempDir::new().unwrap();
        CombinedDatabase::backup(db_dir.path(), backup_dir.path()).unwrap();

        // then
        let restore_dir = TempDir::new().unwrap();
        CombinedDatabase::restore(restore_dir.path(), backup_dir.path()).unwrap();
        let restored_db = CombinedDatabase::open(
            restore_dir.path(),
            StateRewindPolicy::NoRewind,
            DatabaseConfig::config_for_tests(),
        )
        .unwrap();

        let mut restored_on_chain_db = restored_db.on_chain();
        let restored_value = restored_on_chain_db
            .storage::<Coins>()
            .get(&key)
            .unwrap()
            .unwrap()
            .into_owned();
        assert_eq!(expected_value, restored_value);

        // cleanup
        std::fs::remove_dir_all(db_dir.path()).unwrap();
        std::fs::remove_dir_all(backup_dir.path()).unwrap();
        std::fs::remove_dir_all(restore_dir.path()).unwrap();
    }

    #[test]
    fn backup__when_backup_fails_it_should_not_leave_any_residue() {
        use std::os::unix::fs::PermissionsExt;

        // given
        let db_dir = TempDir::new().unwrap();
        let mut combined_db = CombinedDatabase::open(
            db_dir.path(),
            StateRewindPolicy::NoRewind,
            DatabaseConfig::config_for_tests(),
        )
        .unwrap();
        let key = UtxoId::new(Default::default(), Default::default());
        let expected_value = CompressedCoin::default();

        let on_chain_db = combined_db.on_chain_mut();
        on_chain_db
            .storage_as_mut::<Coins>()
            .insert(&key, &expected_value)
            .unwrap();
        drop(combined_db);

        // when
        // we set the permissions of db_dir to not allow reading
        std::fs::set_permissions(db_dir.path(), std::fs::Permissions::from_mode(0o030))
            .unwrap();
        let backup_dir = TempDir::new().unwrap();

        // then
        CombinedDatabase::backup(db_dir.path(), backup_dir.path())
            .expect_err("Backup should fail");
        let backup_dir_contents = std::fs::read_dir(backup_dir.path()).unwrap();
        assert_eq!(backup_dir_contents.count(), 0);

        // cleanup
        std::fs::set_permissions(db_dir.path(), std::fs::Permissions::from_mode(0o770))
            .unwrap();
        std::fs::remove_dir_all(db_dir.path()).unwrap();
        std::fs::remove_dir_all(backup_dir.path()).unwrap();
    }

    #[test]
    fn restore__when_restore_fails_it_should_not_leave_any_residue() {
        use std::os::unix::fs::PermissionsExt;

        // given
        let db_dir = TempDir::new().unwrap();
        let mut combined_db = CombinedDatabase::open(
            db_dir.path(),
            StateRewindPolicy::NoRewind,
            DatabaseConfig::config_for_tests(),
        )
        .unwrap();
        let key = UtxoId::new(Default::default(), Default::default());
        let expected_value = CompressedCoin::default();

        let on_chain_db = combined_db.on_chain_mut();
        on_chain_db
            .storage_as_mut::<Coins>()
            .insert(&key, &expected_value)
            .unwrap();
        drop(combined_db);

        let backup_dir = TempDir::new().unwrap();
        CombinedDatabase::backup(db_dir.path(), backup_dir.path()).unwrap();

        // when
        // we set the permissions of backup_dir to not allow reading
        std::fs::set_permissions(
            backup_dir.path(),
            std::fs::Permissions::from_mode(0o030),
        )
        .unwrap();
        let restore_dir = TempDir::new().unwrap();

        // then
        CombinedDatabase::restore(restore_dir.path(), backup_dir.path())
            .expect_err("Restore should fail");
        let restore_dir_contents = std::fs::read_dir(restore_dir.path()).unwrap();
        assert_eq!(restore_dir_contents.count(), 0);

        // cleanup
        std::fs::set_permissions(
            backup_dir.path(),
            std::fs::Permissions::from_mode(0o770),
        )
        .unwrap();
        std::fs::remove_dir_all(db_dir.path()).unwrap();
        std::fs::remove_dir_all(backup_dir.path()).unwrap();
        std::fs::remove_dir_all(restore_dir.path()).unwrap();
    }

    #[test]
    fn backup__cannot_backup_while_db_is_opened() {
        // given
        let db_dir = TempDir::new().unwrap();
        let mut combined_db = CombinedDatabase::open(
            db_dir.path(),
            StateRewindPolicy::NoRewind,
            DatabaseConfig::config_for_tests(),
        )
        .unwrap();
        let key = UtxoId::new(Default::default(), Default::default());
        let expected_value = CompressedCoin::default();

        let on_chain_db = combined_db.on_chain_mut();
        on_chain_db
            .storage_as_mut::<Coins>()
            .insert(&key, &expected_value)
            .unwrap();

        // when
        let backup_dir = TempDir::new().unwrap();
        // no drop for combined_db

        // then
        CombinedDatabase::backup(db_dir.path(), backup_dir.path())
            .expect_err("Backup should fail");

        // cleanup
        std::fs::remove_dir_all(db_dir.path()).unwrap();
        std::fs::remove_dir_all(backup_dir.path()).unwrap();
    }
}