fuel_core/
state.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
use crate::{
    database::database_description::DatabaseDescription,
    state::{
        generic_database::GenericDatabase,
        iterable_key_value_view::IterableKeyValueViewWrapper,
        key_value_view::KeyValueViewWrapper,
    },
};
use fuel_core_storage::{
    iter::{
        IterDirection,
        IterableStore,
    },
    kv_store::StorageColumn,
    transactional::Changes,
    Result as StorageResult,
};
use std::fmt::Debug;

pub mod data_source;
pub mod generic_database;
#[cfg(feature = "rocksdb")]
pub mod historical_rocksdb;
pub mod in_memory;
pub mod iterable_key_value_view;
pub mod key_value_view;
#[cfg(feature = "rocksdb")]
pub mod rocks_db;
#[cfg(feature = "rocksdb")]
pub mod rocks_db_key_iterator;

pub type ColumnType<Description> = <Description as DatabaseDescription>::Column;

/// A type extends the `KeyValueView`, allowing iteration over the storage.
pub type IterableKeyValueView<Column> =
    GenericDatabase<IterableKeyValueViewWrapper<Column>>;

/// The basic view available for the key value storage.
pub type KeyValueView<Column> = GenericDatabase<KeyValueViewWrapper<Column>>;

impl<Column> IterableKeyValueView<Column>
where
    Column: StorageColumn + 'static,
{
    /// Downgrades the `IterableKeyValueView` into the `KeyValueView`.
    pub fn into_key_value_view(self) -> KeyValueView<Column> {
        let iterable = self.into_inner();
        let storage = KeyValueViewWrapper::new(iterable);
        KeyValueView::from_storage(storage)
    }
}

pub trait TransactableStorage<Height>: IterableStore + Debug + Send + Sync {
    /// Commits the changes into the storage.
    fn commit_changes(
        &self,
        height: Option<Height>,
        changes: Changes,
    ) -> StorageResult<()>;

    fn view_at_height(
        &self,
        height: &Height,
    ) -> StorageResult<KeyValueView<Self::Column>>;

    fn latest_view(&self) -> StorageResult<IterableKeyValueView<Self::Column>>;

    fn rollback_block_to(&self, height: &Height) -> StorageResult<()>;
}

// It is used only to allow conversion of the `StorageTransaction` into the `DataSource`.
#[cfg(feature = "test-helpers")]
impl<Height, S> TransactableStorage<Height>
    for fuel_core_storage::transactional::StorageTransaction<S>
where
    S: IterableStore + Debug + Send + Sync,
{
    fn commit_changes(&self, _: Option<Height>, _: Changes) -> StorageResult<()> {
        unimplemented!()
    }

    fn view_at_height(&self, _: &Height) -> StorageResult<KeyValueView<Self::Column>> {
        unimplemented!()
    }

    fn latest_view(&self) -> StorageResult<IterableKeyValueView<Self::Column>> {
        unimplemented!()
    }

    fn rollback_block_to(&self, _: &Height) -> StorageResult<()> {
        unimplemented!()
    }
}