-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy patherror.rs
More file actions
549 lines (508 loc) · 18.2 KB
/
Copy patherror.rs
File metadata and controls
549 lines (508 loc) · 18.2 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! ## Error Module
//!
//! Defines [`Error`], the central error enum used throughout the Job Declarator Client (JDC).
//!
//! It unifies errors from:
//! - I/O operations
//! - Channels (send/recv)
//! - SV2 stack: Binary, Codec, Noise, Framing, RolesLogic
//! - Locking logic (PoisonError)
//! - Domain-specific issues
//!
//! This module ensures that all errors can be passed around consistently, including across async
//! boundaries.
use ext_config::ConfigError;
use std::{
fmt::{self, Formatter},
marker::PhantomData,
};
use stratum_apps::{
extensions_negotiation::ExtensionNegotiationError,
network_helpers,
stratum_core::{
binary_sv2, bitcoin,
channels_sv2::{
client::error::ExtendedChannelError as ExtendedChannelClientError,
server::error::{
ExtendedChannelError as ExtendedChannelServerError, GroupChannelError,
StandardChannelError,
},
},
framing_sv2,
handlers_sv2::HandlerErrorType,
mining_sv2::ExtendedExtranonceError,
noise_sv2,
parsers_sv2::ParserError,
},
utils::types::{
CanDisconnect, CanFallback, CanShutdown, DownstreamId, ExtensionType, JobId, MessageType,
RequestId, TemplateId, VardiffKey,
},
};
use tokio::{sync::broadcast, time::error::Elapsed};
pub type JDCResult<T, Owner> = Result<T, JDCError<Owner>>;
#[derive(Debug)]
pub struct ChannelManager;
#[derive(Debug)]
pub struct TemplateProvider;
#[derive(Debug)]
pub struct JobDeclarator;
#[derive(Debug)]
pub struct Upstream;
#[derive(Debug)]
pub struct Downstream;
#[derive(Debug)]
pub struct JDCError<Owner> {
pub kind: JDCErrorKind,
pub action: Action,
_owner: PhantomData<Owner>,
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
Log,
Disconnect(DownstreamId),
Fallback,
Shutdown,
}
impl CanDisconnect for Downstream {}
impl CanDisconnect for ChannelManager {}
impl CanFallback for Upstream {}
impl CanFallback for JobDeclarator {}
impl CanFallback for ChannelManager {}
impl CanShutdown for ChannelManager {}
impl CanShutdown for TemplateProvider {}
impl CanShutdown for Downstream {}
impl CanShutdown for Upstream {}
impl CanShutdown for JobDeclarator {}
impl<O> JDCError<O> {
pub fn log<E: Into<JDCErrorKind>>(kind: E) -> Self {
Self {
kind: kind.into(),
action: Action::Log,
_owner: PhantomData,
}
}
}
impl<O> JDCError<O>
where
O: CanDisconnect,
{
pub fn disconnect<E: Into<JDCErrorKind>>(kind: E, downstream_id: DownstreamId) -> Self {
Self {
kind: kind.into(),
action: Action::Disconnect(downstream_id),
_owner: PhantomData,
}
}
}
impl<O> JDCError<O>
where
O: CanFallback,
{
pub fn fallback<E: Into<JDCErrorKind>>(kind: E) -> Self {
Self {
kind: kind.into(),
action: Action::Fallback,
_owner: PhantomData,
}
}
}
impl<O> JDCError<O>
where
O: CanShutdown,
{
pub fn shutdown<E: Into<JDCErrorKind>>(kind: E) -> Self {
Self {
kind: kind.into(),
action: Action::Shutdown,
_owner: PhantomData,
}
}
}
#[derive(Debug)]
pub enum ChannelSv2Error {
ExtendedChannelClientSide(ExtendedChannelClientError),
ExtendedChannelServerSide(ExtendedChannelServerError),
ExtranonceError(ExtendedExtranonceError),
StandardChannelServerSide(StandardChannelError),
GroupChannelServerSide(GroupChannelError),
}
#[derive(Debug)]
pub enum JDCErrorKind {
/// Errors on bad CLI argument input.
BadCliArgs,
/// Errors on bad `config` TOML deserialize.
BadConfigDeserialize(ConfigError),
/// Errors from `binary_sv2` crate.
BinarySv2(binary_sv2::Error),
/// Errors on bad noise handshake.
CodecNoise(noise_sv2::Error),
/// Errors from `framing_sv2` crate.
FramingSv2(framing_sv2::Error),
/// Errors on bad `TcpStream` connection.
Io(std::io::Error),
/// Errors on bad `String` to `int` conversion.
ParseInt(std::num::ParseIntError),
Parser(ParserError),
/// Channel receiver error
ChannelErrorReceiver(async_channel::RecvError),
/// Channel sender error
ChannelErrorSender,
/// Broadcast channel receiver error
BroadcastChannelErrorReceiver(broadcast::error::RecvError),
/// Network helpers error
NetworkHelpersError(network_helpers::Error),
/// Unexpected message
UnexpectedMessage(ExtensionType, MessageType),
/// Invalid user identity
InvalidUserIdentity(String),
/// Bitcoin encode error
BitcoinEncodeError(bitcoin::consensus::encode::Error),
/// Invalid socket address
InvalidSocketAddress(String),
/// Timeout error
Timeout,
/// Declared job corresponding to request Id not found.
LastDeclareJobNotFound(RequestId),
/// No active job with job id
ActiveJobNotFound(JobId),
/// Template not found with template ID
TemplateNotFound(TemplateId),
/// Downstream not found with downstream ID
DownstreamNotFound(DownstreamId),
/// Future template not present
FutureTemplateNotPresent,
/// Last new prevhash not found
LastNewPrevhashNotFound,
/// Vardiff not found
VardiffNotFound(VardiffKey),
/// Tx data error
TxDataError,
/// Frame conversion error
FrameConversionError,
/// Failed to create custom Job
FailedToCreateCustomJob,
/// Allocate Mining job token coinbase output error
AllocateMiningJobTokenSuccessCoinbaseOutputsError,
/// Channel manager has bad coinbase outputs.
ChannelManagerHasBadCoinbaseOutputs,
/// Declared job has bad coinbase outputs.
DeclaredJobHasBadCoinbaseOutputs,
/// Extranonce size is too large
ExtranonceSizeTooLarge,
/// Extranonce size is too small
ExtranonceSizeTooSmall,
/// Could not create group channel
FailedToCreateGroupChannel(GroupChannelError),
///Channel Errors
ChannelSv2(ChannelSv2Error),
/// Extranonce prefix error
ExtranoncePrefixFactoryError(ExtendedExtranonceError),
/// Invalid unsupported extensions sequence (exceeds maximum length)
InvalidUnsupportedExtensionsSequence,
/// Invalid required extensions sequence (exceeds maximum length)
InvalidRequiredExtensionsSequence,
/// Invalid supported extensions sequence (exceeds maximum length)
InvalidSupportedExtensionsSequence,
/// Server does not support required extensions
RequiredExtensionsNotSupported(Vec<u16>),
/// Server requires extensions that the translator doesn't support
ServerRequiresUnsupportedExtensions(Vec<u16>),
/// Extension negotiation timed out waiting for response
ExtensionNegotiationTimeout,
/// BitcoinCoreSv2 cancellation token activated
BitcoinCoreSv2CancellationTokenActivated,
/// Failed to create BitcoinCoreSv2 tokio runtime
FailedToCreateBitcoinCoreTokioRuntime,
/// Failed to send CoinbaseOutputConstraints message
FailedToSendCoinbaseOutputConstraints,
/// Setup Connection Error
SetupConnectionError,
/// Endpoint changed
ChangeEndpoint,
/// Received upstream message during solo mining
UpstreamMessageDuringSoloMining,
/// Declare mining job error
DeclareMiningJobError,
/// Channel opening error
OpenMiningChannelError,
/// Standard channel opening error
OpenStandardMiningChannelError,
/// Close channel
CloseChannel,
/// Custom job error
CustomJobError,
/// Could not initiate subsystem
CouldNotInitiateSystem,
/// Invalid key
InvalidKey,
/// Upstream not found
UpstreamNotFound,
}
impl std::error::Error for JDCErrorKind {}
impl fmt::Display for JDCErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use JDCErrorKind::*;
match self {
BadCliArgs => write!(f, "Bad CLI arg input"),
BadConfigDeserialize(ref e) => write!(f, "Bad `config` TOML deserialize: `{e:?}`"),
BinarySv2(ref e) => write!(f, "Binary SV2 error: `{e:?}`"),
CodecNoise(ref e) => write!(f, "Noise error: `{e:?}"),
FramingSv2(ref e) => write!(f, "Framing SV2 error: `{e:?}`"),
Io(ref e) => write!(f, "I/O error: `{e:?}"),
ParseInt(ref e) => write!(f, "Bad convert from `String` to `int`: `{e:?}`"),
ChannelErrorReceiver(ref e) => write!(f, "Channel receive error: `{e:?}`"),
Parser(ref e) => write!(f, "Parser error: `{e:?}`"),
BroadcastChannelErrorReceiver(ref e) => {
write!(f, "Broadcast channel receive error: {e:?}")
}
ChannelErrorSender => write!(f, "Sender error"),
NetworkHelpersError(ref e) => write!(f, "Network error: {e:?}"),
UnexpectedMessage(extension_type, message_type) => {
write!(f, "Unexpected Message: {extension_type} {message_type}")
}
InvalidUserIdentity(_) => write!(f, "User ID is invalid"),
BitcoinEncodeError(_) => write!(f, "Error generated during encoding"),
InvalidSocketAddress(ref s) => write!(f, "Invalid socket address: {s}"),
Timeout => write!(f, "Time out error"),
LastDeclareJobNotFound(request_id) => {
write!(f, "last declare job not found for request id: {request_id}")
}
ActiveJobNotFound(request_id) => {
write!(f, "Active Job not found for request_id: {request_id}")
}
TemplateNotFound(template_id) => {
write!(f, "Template not found, template_id: {template_id}")
}
DownstreamNotFound(downstream_id) => {
write!(
f,
"Downstream not found with downstream_id: {downstream_id}"
)
}
FutureTemplateNotPresent => {
write!(f, "Future template not present")
}
LastNewPrevhashNotFound => {
write!(f, "Last new prevhash not found")
}
VardiffNotFound(vardiff_key) => {
write!(f, "Vardiff not found for vardiff key: {vardiff_key:?}")
}
TxDataError => {
write!(f, "Transaction data error")
}
FrameConversionError => {
write!(f, "Could not convert message to frame")
}
FailedToCreateCustomJob => {
write!(f, "failed to create custom job")
}
AllocateMiningJobTokenSuccessCoinbaseOutputsError => {
write!(
f,
"AllocateMiningJobToken.Success coinbase outputs are not deserializable"
)
}
ChannelManagerHasBadCoinbaseOutputs => {
write!(f, "Channel Manager coinbase outputs are not deserializable")
}
DeclaredJobHasBadCoinbaseOutputs => {
write!(f, "Declared job coinbase outputs are not deserializable")
}
ExtranonceSizeTooLarge => {
write!(f, "Extranonce size too large")
}
ExtranonceSizeTooSmall => {
write!(f, "Extranonce size too small")
}
FailedToCreateGroupChannel(ref e) => {
write!(f, "Failed to create group channel: {e:?}")
}
ExtranoncePrefixFactoryError(e) => {
write!(f, "Failed to create ExtranoncePrefixFactory: {e:?}")
}
ChannelSv2(channel_error) => {
write!(f, "Channel error: {channel_error:?}")
}
InvalidUnsupportedExtensionsSequence => {
write!(
f,
"Invalid unsupported extensions sequence (exceeds maximum length)"
)
}
InvalidRequiredExtensionsSequence => {
write!(
f,
"Invalid required extensions sequence (exceeds maximum length)"
)
}
InvalidSupportedExtensionsSequence => {
write!(
f,
"Invalid supported extensions sequence (exceeds maximum length)"
)
}
RequiredExtensionsNotSupported(extensions) => {
write!(
f,
"Server does not support required extensions: {extensions:?}"
)
}
ServerRequiresUnsupportedExtensions(extensions) => {
write!(f, "Server requires extensions that the translator doesn't support: {extensions:?}")
}
ExtensionNegotiationTimeout => {
write!(f, "Extension negotiation timed out waiting for response")
}
BitcoinCoreSv2CancellationTokenActivated => {
write!(f, "BitcoinCoreSv2 cancellation token activated")
}
FailedToCreateBitcoinCoreTokioRuntime => {
write!(f, "Failed to create BitcoinCoreSv2TDP tokio runtime")
}
FailedToSendCoinbaseOutputConstraints => {
write!(f, "Failed to send CoinbaseOutputConstraints message")
}
SetupConnectionError => {
write!(f, "Failed to Setup connection")
}
ChangeEndpoint => {
write!(f, "Change endpoint")
}
OpenMiningChannelError => write!(f, "failed to open mining channel"),
OpenStandardMiningChannelError => write!(f, "failed to open standard mining channel"),
DeclareMiningJobError => write!(f, "job declaration rejected by server"),
UpstreamMessageDuringSoloMining => {
write!(f, "received upstream message during solo mining mode")
}
CloseChannel => write!(f, "channel closed by upstream"),
CustomJobError => write!(f, "Custom job not acknowledged"),
CouldNotInitiateSystem => write!(f, "Could not initiate subsystem"),
InvalidKey => write!(f, "Invalid key used during noise handshake"),
UpstreamNotFound => write!(f, "Upstream not found"),
}
}
}
impl From<ParserError> for JDCErrorKind {
fn from(e: ParserError) -> Self {
JDCErrorKind::Parser(e)
}
}
impl From<binary_sv2::Error> for JDCErrorKind {
fn from(e: binary_sv2::Error) -> Self {
JDCErrorKind::BinarySv2(e)
}
}
impl From<noise_sv2::Error> for JDCErrorKind {
fn from(e: noise_sv2::Error) -> Self {
JDCErrorKind::CodecNoise(e)
}
}
impl From<framing_sv2::Error> for JDCErrorKind {
fn from(e: framing_sv2::Error) -> Self {
JDCErrorKind::FramingSv2(e)
}
}
impl From<std::io::Error> for JDCErrorKind {
fn from(e: std::io::Error) -> Self {
JDCErrorKind::Io(e)
}
}
impl From<std::num::ParseIntError> for JDCErrorKind {
fn from(e: std::num::ParseIntError) -> Self {
JDCErrorKind::ParseInt(e)
}
}
impl From<ConfigError> for JDCErrorKind {
fn from(e: ConfigError) -> Self {
JDCErrorKind::BadConfigDeserialize(e)
}
}
impl From<async_channel::RecvError> for JDCErrorKind {
fn from(e: async_channel::RecvError) -> Self {
JDCErrorKind::ChannelErrorReceiver(e)
}
}
impl From<network_helpers::Error> for JDCErrorKind {
fn from(value: network_helpers::Error) -> Self {
JDCErrorKind::NetworkHelpersError(value)
}
}
impl From<stratum_apps::stratum_core::bitcoin::consensus::encode::Error> for JDCErrorKind {
fn from(value: stratum_apps::stratum_core::bitcoin::consensus::encode::Error) -> Self {
JDCErrorKind::BitcoinEncodeError(value)
}
}
impl From<Elapsed> for JDCErrorKind {
fn from(_value: Elapsed) -> Self {
Self::Timeout
}
}
impl HandlerErrorType for JDCErrorKind {
fn parse_error(error: ParserError) -> Self {
JDCErrorKind::Parser(error)
}
fn unexpected_message(extension_type: ExtensionType, message_type: MessageType) -> Self {
JDCErrorKind::UnexpectedMessage(extension_type, message_type)
}
}
impl From<ExtendedChannelClientError> for JDCErrorKind {
fn from(value: ExtendedChannelClientError) -> Self {
JDCErrorKind::ChannelSv2(ChannelSv2Error::ExtendedChannelClientSide(value))
}
}
impl From<ExtendedChannelServerError> for JDCErrorKind {
fn from(value: ExtendedChannelServerError) -> Self {
JDCErrorKind::ChannelSv2(ChannelSv2Error::ExtendedChannelServerSide(value))
}
}
impl From<StandardChannelError> for JDCErrorKind {
fn from(value: StandardChannelError) -> Self {
JDCErrorKind::ChannelSv2(ChannelSv2Error::StandardChannelServerSide(value))
}
}
impl From<ExtendedExtranonceError> for JDCErrorKind {
fn from(value: ExtendedExtranonceError) -> Self {
JDCErrorKind::ChannelSv2(ChannelSv2Error::ExtranonceError(value))
}
}
impl From<GroupChannelError> for JDCErrorKind {
fn from(value: GroupChannelError) -> Self {
JDCErrorKind::ChannelSv2(ChannelSv2Error::GroupChannelServerSide(value))
}
}
impl From<ExtensionNegotiationError> for JDCErrorKind {
fn from(e: ExtensionNegotiationError) -> Self {
match e {
ExtensionNegotiationError::SendError => JDCErrorKind::ChannelErrorSender,
ExtensionNegotiationError::ReceiveError(_) => JDCErrorKind::UnexpectedMessage(0, 0),
ExtensionNegotiationError::Timeout => JDCErrorKind::ExtensionNegotiationTimeout,
ExtensionNegotiationError::UnexpectedMessage(ext, msg) => {
JDCErrorKind::UnexpectedMessage(ext, msg as u8)
}
ExtensionNegotiationError::HandlerError(_) => JDCErrorKind::UnexpectedMessage(0, 0),
}
}
}
impl<Owner> HandlerErrorType for JDCError<Owner> {
fn parse_error(error: ParserError) -> Self {
Self {
kind: JDCErrorKind::Parser(error),
action: Action::Log,
_owner: PhantomData,
}
}
fn unexpected_message(extension_type: ExtensionType, message_type: MessageType) -> Self {
Self {
kind: JDCErrorKind::UnexpectedMessage(extension_type, message_type),
action: Action::Log,
_owner: PhantomData,
}
}
}
impl<Owner> std::fmt::Display for JDCError<Owner> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "[{:?}/{:?}]", self.kind, self.action)
}
}