forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
197 lines (180 loc) · 7.1 KB
/
error.rs
File metadata and controls
197 lines (180 loc) · 7.1 KB
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
//! Errors interfacing with [`OpProofsStore`](crate::OpProofsStore) type.
use alloy_primitives::B256;
use reth_db::DatabaseError;
use reth_execution_errors::BlockExecutionError;
use reth_provider::ProviderError;
use reth_trie_common::Nibbles;
use std::sync::Arc;
use thiserror::Error;
/// Error type for storage operations
#[derive(Debug, Clone, Error)]
pub enum OpProofsStorageError {
/// No blocks found
#[error("No blocks found")]
NoBlocksFound,
/// Parent block number is less than earliest stored block number
#[error("Parent block number is less than earliest stored block number")]
UnknownParent,
/// Block is out of order
#[error(
"Block {block_number} is out of order (parent: {parent_block_hash}, latest stored block hash: {latest_block_hash})"
)]
OutOfOrder {
/// The block number being inserted
block_number: u64,
/// The parent hash of the block being inserted
parent_block_hash: B256,
/// block hash of the latest stored block
latest_block_hash: B256,
},
/// Block update failed since parent state
#[error(
"Cannot execute block updates for block {block_number} without parent state {parent_block_number} (latest stored block number: {latest_block_number})"
)]
MissingParentBlock {
/// The block number being executed
block_number: u64,
/// The parent state of the block being executed
parent_block_number: u64,
/// Latest stored block number
latest_block_number: u64,
},
/// State root mismatch
#[error(
"State root mismatch for block {block_number} (have: {current_state_hash}, expected: {expected_state_hash})"
)]
StateRootMismatch {
/// Block number
block_number: u64,
/// Have state root
current_state_hash: B256,
/// Expected state root
expected_state_hash: B256,
},
/// No change set for block
#[error("No change set found for block {0}")]
NoChangeSetForBlock(u64),
/// Missing account trie history for a specific path at a specific block number
#[error("Missing account trie history for path {0:?} at block {1}")]
MissingAccountTrieHistory(Nibbles, u64),
/// Missing storage trie history for a specific address and path at a specific block number
#[error("Missing storage trie history for address {0:?}, path {1:?} at block {2}")]
MissingStorageTrieHistory(B256, Nibbles, u64),
/// Missing hashed account history for a specific key at a specific block number
#[error("Missing hashed account history for key {0:?} at block {1}")]
MissingHashedAccountHistory(B256, u64),
/// Missing hashed storage history for a specific address and key at a specific block number
#[error(
"Missing hashed storage history for address {hashed_address:?}, key {hashed_storage_key:?} at block {block_number}"
)]
MissingHashedStorageHistory {
/// The hashed address
hashed_address: B256,
/// The hashed storage key
hashed_storage_key: B256,
/// The block number
block_number: u64,
},
/// Attempted to unwind to a block beyond the earliest stored block
#[error(
"Attempted to unwind to block {unwind_block_number} beyond earliest stored block {earliest_block_number}"
)]
UnwindBeyondEarliest {
/// The block number being unwound to
unwind_block_number: u64,
/// The earliest stored block number
earliest_block_number: u64,
},
/// Error occurred while interacting with the database.
#[error(transparent)]
DatabaseError(DatabaseError),
/// Error occurred while trying to acquire a lock.
#[error("failed lock attempt")]
TryLockError,
/// Error occurred during block execution.
#[error(transparent)]
ExecutionError(Arc<BlockExecutionError>),
/// Error occurred while interacting with the provider.
#[error(transparent)]
ProviderError(Arc<ProviderError>),
/// Initialization detected inconsistent state between proofs storage and source DB.
#[error(
"Initialization Proofs storage detected inconsistent state. Storage does not match source DB. \
Please clear proofs data and retry initialization."
)]
InitializeStorageInconsistentState,
/// Other error with a message.
#[error("{0}")]
Other(String),
}
impl From<BlockExecutionError> for OpProofsStorageError {
fn from(error: BlockExecutionError) -> Self {
Self::ExecutionError(Arc::new(error))
}
}
impl From<ProviderError> for OpProofsStorageError {
fn from(error: ProviderError) -> Self {
Self::ProviderError(Arc::new(error))
}
}
impl From<OpProofsStorageError> for DatabaseError {
fn from(error: OpProofsStorageError) -> Self {
match error {
OpProofsStorageError::DatabaseError(err) => err,
_ => Self::Custom(Arc::new(error)),
}
}
}
impl From<DatabaseError> for OpProofsStorageError {
fn from(error: DatabaseError) -> Self {
if let DatabaseError::Custom(ref err) = error &&
let Some(err) = err.downcast_ref::<Self>()
{
return err.clone();
}
Self::DatabaseError(error)
}
}
/// Result type for storage operations
pub type OpProofsStorageResult<T> = Result<T, OpProofsStorageError>;
#[cfg(test)]
mod test {
use super::*;
use reth_execution_errors::BlockValidationError;
#[test]
fn test_op_proofs_store_error_to_db_error() {
let original_error = OpProofsStorageError::NoBlocksFound;
let db_error = DatabaseError::from(original_error);
assert!(matches!(db_error, DatabaseError::Custom(_)));
let converted_error = OpProofsStorageError::from(db_error);
assert!(matches!(converted_error, OpProofsStorageError::NoBlocksFound))
}
#[test]
fn test_db_error_to_op_proofs_store_error() {
let original_error = DatabaseError::Decode;
let op_proofs_store_error = OpProofsStorageError::from(original_error);
assert!(matches!(
op_proofs_store_error,
OpProofsStorageError::DatabaseError(DatabaseError::Decode)
));
let converted_error = DatabaseError::from(op_proofs_store_error);
assert!(matches!(converted_error, DatabaseError::Decode))
}
#[test]
fn test_conversion_from_block_execution_error() {
let block_execution_error =
BlockExecutionError::Validation(BlockValidationError::IncrementBalanceFailed);
let op_proofs_store_error = OpProofsStorageError::from(block_execution_error);
assert!(
matches!(op_proofs_store_error, OpProofsStorageError::ExecutionError(err) if matches!(*err, BlockExecutionError::Validation(BlockValidationError::IncrementBalanceFailed)))
)
}
#[test]
fn test_conversion_from_provider_error() {
let provider_error = ProviderError::SenderRecoveryError;
let op_proofs_store_error = OpProofsStorageError::from(provider_error);
assert!(
matches!(op_proofs_store_error, OpProofsStorageError::ProviderError(err) if matches!(*err, ProviderError::SenderRecoveryError))
)
}
}