essential_builder/
error.rs

1//! Error type declarations for block building.
2
3use essential_builder_db as builder_db;
4use essential_check::solution::PredicatesError;
5use essential_node as node;
6use essential_node_db as node_db;
7use essential_types::{predicate::PredicateDecodeError, ContentAddress, Key};
8use thiserror::Error;
9
10/// Any errors that might occur within [`crate::build_block_fifo`].
11#[derive(Debug, Error)]
12pub enum BuildBlockError {
13    /// A builder DB query error occurred.
14    #[error("A builder DB query error occurred: {0}")]
15    BuilderQuery(#[from] builder_db::error::AcquireThenQueryError),
16    /// A builder DB rusqlite error occurred.
17    #[error("A builder DB rusqlite error occurred: {0}")]
18    BuilderRusqlite(#[from] builder_db::error::AcquireThenRusqliteError),
19    /// A node DB rusqlite error occurred.
20    #[error("A node DB rusqlite error occurred: {0}")]
21    NodeRusqlite(#[from] node::db::pool::AcquireThenRusqliteError),
22    /// Failed to check and apply a sequence of solution sets.
23    #[error("Failed to check and apply solution sets: {0}")]
24    CheckSets(#[from] CheckSetsError),
25    /// System time produced a non-monotonic timestamp.
26    #[error("System time produced non-monotonic timestamp")]
27    TimestampNotMonotonic,
28    /// System time is out of range of `Word`.
29    #[error("System timestamp is out of range of `Word`")]
30    TimestampOutOfRange,
31    /// Failed to retrieve the last block header.
32    #[error("Failed to retrieve the last block header")]
33    LastBlockHeader(#[from] node::db::pool::AcquireThenError<LastBlockHeaderError>),
34    /// The next block number would be out of `u64` range.
35    #[error("The next block number would be out of `u64` range")]
36    BlockNumberOutOfRange,
37}
38
39/// Errors that can occur while retrieving the last block header.
40#[derive(Debug, Error)]
41pub enum LastBlockHeaderError {
42    /// A rusqlite error occurred.
43    #[error("A rusqlite error occurred: {0}")]
44    Rusqlite(#[from] rusqlite::Error),
45    /// A node DB query error occurred.
46    #[error("A node DB query error occurred: {0}")]
47    Query(#[from] node_db::QueryError),
48    /// The node DB contained no number for the last finalized block.
49    #[error("The node DB contained no number for the last finalized block")]
50    NoNumberForLastFinalizedBlock,
51    /// The node DB contained no timestamp for the last finalized block.
52    #[error("The node DB contained no timestamp for the last finalized block")]
53    NoTimestampForLastFinalizedBlock,
54}
55
56/// Any errors that might occur within `check_sets`.
57#[derive(Debug, Error)]
58pub enum CheckSetsError {
59    /// An error occurred while checking a solution set.
60    #[error("an error occurred while attempting to apply a set: {0}")]
61    CheckSolution(#[from] CheckSetError),
62}
63
64/// Any errors that might occur within `crate::check_set`.
65#[derive(Debug, Error)]
66pub enum CheckSetError {
67    /// A rusqlite error occurred.
68    #[error("a rusqlite error occurred: {0}")]
69    Rusqlite(#[from] rusqlite::Error),
70    /// A node DB query failed.
71    #[error("a node DB query failed: {0}")]
72    NodeQuery(#[from] node::db::pool::AcquireThenQueryError),
73}
74
75/// An error occurred while fetching a solution set's predicates.
76#[derive(Debug, Error)]
77pub enum SetPredicatesError {
78    /// An error occurred while querying the node DB.
79    #[error("an error occurred while querying for a predicate from the node DB: {0}")]
80    QueryPredicate(#[from] QueryPredicateError),
81    /// The node DB is missing a required predicate.
82    #[error("the node DB is missing a required predicate ({0})")]
83    PredicateDoesNotExist(ContentAddress),
84}
85
86/// An error occurred while fetching a predicate's programs.
87#[derive(Debug, Error)]
88pub enum PredicateProgramsError {
89    /// An error occurred while querying the node DB.
90    #[error("an error occurred while querying for a program from the node DB: {0}")]
91    QueryProgram(#[from] QueryProgramError),
92    /// The node DB is missing a required predicate.
93    #[error("the node DB is missing a required program ({0})")]
94    ProgramDoesNotExist(ContentAddress),
95}
96
97/// Represents the reason why a [`SolutionSet`][essential_types::solution::SolutionSet] is invalid.
98#[derive(Debug, Error)]
99pub enum InvalidSet {
100    /// Solution set specified a predicate to solve that does not exist.
101    #[error("Solution set specified a predicate to solve that does not exist")]
102    PredicateDoesNotExist(ContentAddress),
103    /// Solution set contains a predicate that specified a program that does not exist.
104    #[error("Solution set contains a predicate that specified a program that does not exist")]
105    ProgramDoesNotExist(ContentAddress),
106    /// Solution set specified a predicate that exists, but was invalid when reading from contract
107    /// registry state.
108    #[error(
109        "Solution set specified a predicate that was invalid when reading from contract registry state"
110    )]
111    PredicateInvalid,
112    /// Solution set contains a predicate that specified a program that exists,
113    /// but was invalid when reading from program registry state.
114    #[error(
115        "Solution set contains a predicate that specified a program that was invalid when reading from program registry state"
116    )]
117    ProgramInvalid,
118    /// Validation of the solution set predicates failed.
119    #[error("Validation of the solution set predicates failed: {0}")]
120    Predicates(PredicatesError<StateReadError>),
121}
122
123/// Any errors that might occur in the [`View`][crate::state::View]'s
124/// [`StateRead`][essential_check::state_read_vm::StateRead] implementation.
125#[derive(Debug, Error)]
126pub enum StateReadError {
127    /// A state query to the underlying DB connection pool failed.
128    #[error("a state query failed: {0}")]
129    Query(#[from] node::db::pool::AcquireThenQueryError),
130    /// No entry exists for the given key.
131    #[error("No entry exists for the given key {0:?}")]
132    NoEntry(Key),
133    /// Key out of range.
134    #[error("A key would be out of range: `key` {key:?}, `num_values` {num_values}")]
135    OutOfRange { key: Key, num_values: usize },
136}
137
138/// Any errors that might occur while querying for predicates.
139#[derive(Debug, Error)]
140pub enum QueryPredicateError {
141    /// A DB query failure occurred.
142    #[error("failed to query the node DB: {0}")]
143    ConnPoolQuery(#[from] node::db::pool::AcquireThenQueryError),
144    /// The queried predicate is missing the word that encodes its length.
145    #[error("the queried predicate is missing the word that encodes its length")]
146    MissingLenBytes,
147    /// The queried predicate length was invalid.
148    #[error("the queried predicate length was invalid")]
149    InvalidLenBytes,
150    /// Failed to decode the queried predicate.
151    #[error("failed to decode the queried predicate: {0}")]
152    Decode(#[from] PredicateDecodeError),
153}
154
155/// Any errors that might occur while querying for programs.
156#[derive(Debug, Error)]
157pub enum QueryProgramError {
158    /// A DB query failure occurred.
159    #[error("failed to query the node DB: {0}")]
160    ConnPoolQuery(#[from] node::db::pool::AcquireThenQueryError),
161    /// The queried program is missing the word that encodes its length.
162    #[error("the queried program is missing the word that encodes its length")]
163    MissingLenBytes,
164    /// The queried program length was invalid.
165    #[error("the queried program length was invalid")]
166    InvalidLenBytes,
167}