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("VDF error: {reason}")]
123 VdfError { reason: String },
124
125 #[error("Key generation failed: {reason}")]
127 KeyGenerationFailed { reason: String },
128
129 #[error("Encryption failed: {reason}")]
131 EncryptionFailed { reason: String },
132
133 #[error("Decryption failed: {reason}")]
135 DecryptionFailed { reason: String },
136}
137
138#[derive(Error, Debug)]
140pub enum StorageError {
141 #[error("Database operation failed: {reason}")]
143 DatabaseOperation { reason: String },
144
145 #[error("Key not found: {key}")]
147 KeyNotFound { key: String },
148
149 #[error("Failed to serialize data: {reason}")]
151 SerializationFailed { reason: String },
152
153 #[error("Failed to deserialize data: {reason}")]
155 DeserializationFailed { reason: String },
156
157 #[error("Database corruption detected: {reason}")]
159 Corruption { reason: String },
160
161 #[error("Attempted to write to read-only database")]
163 ReadOnly,
164
165 #[error("Transaction failed: {reason}")]
167 TransactionFailed { reason: String },
168}
169
170#[derive(Error, Debug)]
172pub enum SerializationError {
173 #[error("JSON error: {0}")]
175 Json(#[from] serde_json::Error),
176
177 #[error("Binary serialization error: {0}")]
179 Binary(#[from] bincode::Error),
180
181 #[error("Invalid message format: expected {expected}, got {actual}")]
183 InvalidFormat { expected: String, actual: String },
184
185 #[error("Missing required field: {field}")]
187 MissingField { field: String },
188
189 #[error("Field validation failed for {field}: {reason}")]
191 FieldValidation { field: String, reason: String },
192}
193
194impl ChaincraftError {
195 pub fn validation<T: Into<String>>(msg: T) -> Self {
197 ChaincraftError::Validation(msg.into())
198 }
199
200 pub fn consensus<T: Into<String>>(msg: T) -> Self {
202 ChaincraftError::Consensus(msg.into())
203 }
204
205 pub fn config<T: Into<String>>(msg: T) -> Self {
207 ChaincraftError::Config(msg.into())
208 }
209
210 pub fn generic<T: Into<String>>(msg: T) -> Self {
212 ChaincraftError::Generic(msg.into())
213 }
214}
215
216impl From<serde_json::Error> for ChaincraftError {
217 fn from(err: serde_json::Error) -> Self {
218 ChaincraftError::Serialization(SerializationError::Json(err))
219 }
220}