1use std::net::SocketAddr;
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, ChaincraftError>;
8
9#[derive(Error, Debug)]
11pub enum ChaincraftError {
12 #[error("Network error: {0}")]
14 Network(#[from] NetworkError),
15
16 #[error("Cryptographic error: {0}")]
18 Crypto(#[from] CryptoError),
19
20 #[error("Storage error: {0}")]
22 Storage(#[from] StorageError),
23
24 #[error("Serialization error: {0}")]
26 Serialization(#[from] SerializationError),
27
28 #[error("Validation error: {0}")]
30 Validation(String),
31
32 #[error("Consensus error: {0}")]
34 Consensus(String),
35
36 #[error("Configuration error: {0}")]
38 Config(String),
39
40 #[error("IO error: {0}")]
42 Io(#[from] std::io::Error),
43
44 #[error("{0}")]
46 Generic(String),
47}
48
49#[derive(Error, Debug)]
51pub enum NetworkError {
52 #[error("Failed to bind to {addr}: {source}")]
54 BindFailed {
55 addr: SocketAddr,
56 source: std::io::Error,
57 },
58
59 #[error("Failed to connect to {addr}: {source}")]
61 ConnectionFailed {
62 addr: SocketAddr,
63 source: std::io::Error,
64 },
65
66 #[error("Peer {addr} is banned until {expires_at}")]
68 PeerBanned {
69 addr: SocketAddr,
70 expires_at: chrono::DateTime<chrono::Utc>,
71 },
72
73 #[error("Message size {size} exceeds maximum {max_size}")]
75 MessageTooLarge { size: usize, max_size: usize },
76
77 #[error("Invalid message format: {reason}")]
79 InvalidMessage { reason: String },
80
81 #[error("Operation timed out after {duration:?}")]
83 Timeout { duration: std::time::Duration },
84
85 #[error("No peers available for operation")]
87 NoPeersAvailable,
88}
89
90#[derive(Error, Debug)]
92pub enum CryptoError {
93 #[error("Invalid signature")]
95 InvalidSignature,
96
97 #[error("Invalid public key: {reason}")]
99 InvalidPublicKey { reason: String },
100
101 #[error("Invalid private key: {reason}")]
103 InvalidPrivateKey { reason: String },
104
105 #[error("Hash verification failed")]
107 HashVerificationFailed,
108
109 #[error("Proof of work verification failed")]
111 ProofOfWorkFailed,
112
113 #[error("VRF verification failed")]
115 VrfVerificationFailed,
116
117 #[error("VDF verification failed")]
119 VdfVerificationFailed,
120
121 #[error("Key generation failed: {reason}")]
123 KeyGenerationFailed { reason: String },
124
125 #[error("Encryption failed: {reason}")]
127 EncryptionFailed { reason: String },
128
129 #[error("Decryption failed: {reason}")]
131 DecryptionFailed { reason: String },
132}
133
134#[derive(Error, Debug)]
136pub enum StorageError {
137 #[error("Database operation failed: {reason}")]
139 DatabaseOperation { reason: String },
140
141 #[error("Key not found: {key}")]
143 KeyNotFound { key: String },
144
145 #[error("Failed to serialize data: {reason}")]
147 SerializationFailed { reason: String },
148
149 #[error("Failed to deserialize data: {reason}")]
151 DeserializationFailed { reason: String },
152
153 #[error("Database corruption detected: {reason}")]
155 Corruption { reason: String },
156
157 #[error("Attempted to write to read-only database")]
159 ReadOnly,
160
161 #[error("Transaction failed: {reason}")]
163 TransactionFailed { reason: String },
164}
165
166#[derive(Error, Debug)]
168pub enum SerializationError {
169 #[error("JSON error: {0}")]
171 Json(#[from] serde_json::Error),
172
173 #[error("Binary serialization error: {0}")]
175 Binary(#[from] bincode::Error),
176
177 #[error("Invalid message format: expected {expected}, got {actual}")]
179 InvalidFormat { expected: String, actual: String },
180
181 #[error("Missing required field: {field}")]
183 MissingField { field: String },
184
185 #[error("Field validation failed for {field}: {reason}")]
187 FieldValidation { field: String, reason: String },
188}
189
190impl ChaincraftError {
191 pub fn validation<T: Into<String>>(msg: T) -> Self {
193 ChaincraftError::Validation(msg.into())
194 }
195
196 pub fn consensus<T: Into<String>>(msg: T) -> Self {
198 ChaincraftError::Consensus(msg.into())
199 }
200
201 pub fn config<T: Into<String>>(msg: T) -> Self {
203 ChaincraftError::Config(msg.into())
204 }
205
206 pub fn generic<T: Into<String>>(msg: T) -> Self {
208 ChaincraftError::Generic(msg.into())
209 }
210}
211
212impl From<serde_json::Error> for ChaincraftError {
213 fn from(err: serde_json::Error) -> Self {
214 ChaincraftError::Serialization(SerializationError::Json(err))
215 }
216}