Skip to main content

signet_hot/db/
errors.rs

1use alloy::primitives::B256;
2use signet_storage_types::IntegerListError;
3
4/// A result type for history operations.
5pub type HistoryResult<T, E> = Result<T, HistoryError<E>>;
6
7/// Error type for history operations.
8///
9/// This error is returned by methods that append or unwind history,
10/// and includes both chain consistency errors and database errors.
11#[derive(Debug, thiserror::Error)]
12pub enum HistoryError<E: std::error::Error> {
13    /// Block number doesn't extend the chain contiguously.
14    #[error("non-contiguous block: expected {expected}, got {got}")]
15    NonContiguousBlock {
16        /// The expected block number (current tip + 1).
17        expected: u64,
18        /// The actual block number provided.
19        got: u64,
20    },
21    /// Parent hash doesn't match current tip or previous block in range.
22
23    #[error("parent hash mismatch: expected {expected}, got {got}")]
24    ParentHashMismatch {
25        /// The expected parent hash.
26        expected: B256,
27        /// The actual parent hash provided.
28        got: B256,
29    },
30
31    /// Invoked `load_genesis` on a non-empty database.
32    #[error("cannot initialize genesis on a non-empty database")]
33    DbNotEmpty,
34
35    /// Empty header range provided to a method that requires at least one
36    /// header.
37    #[error("empty header range provided")]
38    EmptyRange,
39
40    /// No blocks exist in the database.
41    #[error("no blocks in database")]
42    NoBlocks,
43
44    /// The requested height is outside the stored block range.
45    #[error("height {height} outside stored range {first}..={last}")]
46    HeightOutOfRange {
47        /// The requested height.
48        height: u64,
49        /// The first stored block number.
50        first: u64,
51        /// The last stored block number.
52        last: u64,
53    },
54
55    /// Database error.
56    #[error("{0}")]
57    Db(#[from] E),
58
59    /// Integer List error.
60    #[error(transparent)]
61    IntList(IntegerListError),
62}
63
64impl<E: std::error::Error> HistoryError<E> {
65    /// Helper to create a database error
66    pub const fn intlist(err: IntegerListError) -> Self {
67        HistoryError::IntList(err)
68    }
69
70    /// Map the database error variant to a different error type.
71    ///
72    /// All non-[`Db`](Self::Db) variants are converted directly. The `Db`
73    /// variant is transformed using the provided closure.
74    pub fn map_db<F: std::error::Error>(self, f: impl FnOnce(E) -> F) -> HistoryError<F> {
75        match self {
76            Self::Db(e) => HistoryError::Db(f(e)),
77            Self::IntList(e) => HistoryError::IntList(e),
78            Self::NonContiguousBlock { expected, got } => {
79                HistoryError::NonContiguousBlock { expected, got }
80            }
81            Self::ParentHashMismatch { expected, got } => {
82                HistoryError::ParentHashMismatch { expected, got }
83            }
84            Self::DbNotEmpty => HistoryError::DbNotEmpty,
85            Self::EmptyRange => HistoryError::EmptyRange,
86            Self::NoBlocks => HistoryError::NoBlocks,
87            Self::HeightOutOfRange { height, first, last } => {
88                HistoryError::HeightOutOfRange { height, first, last }
89            }
90        }
91    }
92}