fuel_core_database/
lib.rs

1//! The crate `fuel-core-storage` contains storage types, primitives, tables used by `fuel-core`.
2//! This crate doesn't contain the actual implementation of the storage. It works around the
3//! `Database` and is used by services to provide a default implementation. Primitives
4//! defined here are used by services but are flexible enough to customize the
5//! logic when the `Database` is known.
6
7#![deny(clippy::arithmetic_side_effects)]
8#![deny(clippy::cast_possible_truncation)]
9#![deny(missing_docs)]
10#![deny(unused_crate_dependencies)]
11#![deny(warnings)]
12#![deny(unused_variables)]
13
14use fuel_core_storage::Error as StorageError;
15use fuel_core_types::services::executor::Error as ExecutorError;
16
17/// The error occurred during work with any of databases.
18#[derive(Debug, derive_more::Display, derive_more::From)]
19#[non_exhaustive]
20#[allow(missing_docs)]
21pub enum Error {
22    /// Error occurred during serialization or deserialization of the entity.
23    #[display(fmt = "error performing serialization or deserialization")]
24    Codec,
25    /// The version of database or data is invalid (possibly not migrated).
26    #[display(
27        fmt = "Invalid database version, expected {expected:#x}, found {found:#x}"
28    )]
29    InvalidDatabaseVersion {
30        /// the current database version
31        found: u32,
32        /// the database version expected by this build of fuel-core
33        expected: u32,
34    },
35    /// Multiple heights found in the commit to the database.
36    #[display(fmt = "Multiple heights found in the commit {heights:?}")]
37    MultipleHeightsInCommit {
38        /// List of heights found in the commit.
39        heights: Vec<u64>,
40    },
41    /// Failed to advance the height.
42    #[display(fmt = "Failed to advance the height")]
43    FailedToAdvanceHeight,
44    /// The new and old heights are not linked.
45    #[display(
46        fmt = "New and old heights are not linked: prev_height: {prev_height:#x}, new_height: {new_height:#x}"
47    )]
48    HeightsAreNotLinked {
49        /// The old height.
50        prev_height: u64,
51        /// The new height.
52        new_height: u64,
53    },
54    /// The new height is not found, but the old height is set.
55    #[display(
56        fmt = "The new height is not found, but the old height is set: prev_height: {prev_height:#x}"
57    )]
58    NewHeightIsNotSet {
59        /// The old height known by the database.
60        prev_height: u64,
61    },
62    #[display(
63        fmt = "The historical database doesn't have history for the requested height {requested_height:#x}"
64    )]
65    NoHistoryForRequestedHeight { requested_height: u64 },
66    #[display(fmt = "Reached the end of the history")]
67    ReachedEndOfHistory,
68
69    #[cfg(feature = "backup")]
70    #[display(fmt = "BackupEngine initialization error: {}", _0)]
71    BackupEngineInitError(anyhow::Error),
72
73    #[cfg(feature = "backup")]
74    #[display(fmt = "Backup error: {}", _0)]
75    BackupError(anyhow::Error),
76
77    #[cfg(feature = "backup")]
78    #[display(fmt = "Restore error: {}", _0)]
79    RestoreError(anyhow::Error),
80
81    /// Not related to database error.
82    #[from]
83    Other(anyhow::Error),
84}
85
86#[cfg(feature = "test-helpers")]
87impl PartialEq for Error {
88    fn eq(&self, other: &Self) -> bool {
89        self.to_string().eq(&other.to_string())
90    }
91}
92
93impl From<Error> for anyhow::Error {
94    fn from(error: Error) -> Self {
95        anyhow::Error::msg(error)
96    }
97}
98
99impl From<Error> for StorageError {
100    fn from(e: Error) -> Self {
101        StorageError::DatabaseError(Box::new(e))
102    }
103}
104
105impl From<Error> for ExecutorError {
106    fn from(e: Error) -> Self {
107        ExecutorError::StorageError(format!("{}", StorageError::from(e)))
108    }
109}
110
111#[cfg(test)]
112fuel_core_trace::enable_tracing!();