Skip to main content

eqty_faucet_client/
lib.rs

1//! Reusable, bounded headless faucet flow. The public service need not exist for local tests.
2
3mod api;
4mod mining;
5mod state;
6
7#[cfg(any(test, feature = "test-support"))]
8pub mod test_support;
9
10#[cfg(test)]
11mod tests;
12
13pub use api::Api;
14pub use mining::{Event, FaucetClient, MiningOptions, MiningSummary, StopReason};
15pub use state::{PendingPayout, SavedState, StateStore};
16pub use tokio_util::sync::CancellationToken;
17
18use eqty_faucet_protocol::{ErrorCode, ProtocolError};
19use thiserror::Error;
20
21#[derive(Debug, Error)]
22pub enum ClientError {
23    #[error("invalid client configuration: {0}")]
24    Configuration(&'static str),
25    #[error(
26        "saved session belongs to another endpoint, asset or recipient; use a separate state directory"
27    )]
28    SessionMismatch,
29    #[error("another faucet process owns this state directory")]
30    StateLocked,
31    #[error("state directory or file is insecure, invalid or a symbolic link")]
32    InsecureState,
33    #[error("state file is corrupt or belongs to an unsupported version")]
34    InvalidState,
35    #[error("filesystem operation failed: {0}")]
36    Io(#[from] std::io::Error),
37    #[error("network request failed")]
38    Network(#[source] reqwest::Error),
39    #[error("unexpected HTTP response ({0}); no redirect was followed")]
40    Http(u16),
41    #[error("service returned {code:?} (HTTP {status})")]
42    Remote {
43        code: ErrorCode,
44        status: u16,
45        retry_after: Option<u64>,
46    },
47    #[error("service response violates the faucet protocol")]
48    InvalidResponse,
49    #[error("service response exceeds the size limit")]
50    ResponseTooLarge,
51    #[error("no saved session; run mine with a recipient first")]
52    NoSession,
53    #[error("payout definitively failed; inspect status before starting another payout")]
54    PayoutFailed,
55    #[error("CPU worker failed")]
56    Worker,
57    #[error(transparent)]
58    Protocol(#[from] ProtocolError),
59    #[error(transparent)]
60    Pow(#[from] eqty_faucet_pow::PowError),
61}
62
63impl ClientError {
64    pub fn exit_code(&self) -> i32 {
65        match self {
66            Self::Configuration(_) | Self::SessionMismatch | Self::NoSession => 2,
67            Self::Remote {
68                code:
69                    ErrorCode::CapacityExhausted
70                    | ErrorCode::TreasuryUnavailable
71                    | ErrorCode::InsufficientCredit,
72                ..
73            } => 3,
74            Self::Network(_)
75            | Self::Http(429 | 502 | 503 | 504)
76            | Self::Remote {
77                status: 429 | 502 | 503 | 504,
78                ..
79            } => 4,
80            _ => 5,
81        }
82    }
83
84    pub(crate) fn retry_delay(&self, attempt: usize) -> Option<std::time::Duration> {
85        let delay = match self {
86            Self::Network(_) | Self::Http(429 | 502 | 503 | 504) => 1u64 << attempt.min(4),
87            Self::Remote {
88                code: ErrorCode::Overloaded | ErrorCode::PayoutPending,
89                retry_after,
90                ..
91            } => retry_after.unwrap_or(1u64 << attempt.min(4)),
92            _ => return None,
93        };
94        Some(std::time::Duration::from_secs(delay.clamp(1, 30)))
95    }
96}