Skip to main content

veil_sdk/
types.rs

1use serde::{Deserialize, Serialize};
2
3// ── Job lifecycle ─────────────────────────────────────────────────────────────
4
5/// All possible job statuses returned by the gateway.
6#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
7#[serde(rename_all = "lowercase")]
8pub enum JobStatus {
9    Queued,
10    Running,
11    Proving,
12    Done,
13    Failed,
14    Settled,
15}
16
17impl JobStatus {
18    /// Returns true for terminal states that require no further polling.
19    pub fn is_terminal(&self) -> bool {
20        matches!(self, Self::Failed | Self::Settled)
21    }
22
23    /// Returns true if the job completed successfully.
24    pub fn is_success(&self) -> bool {
25        matches!(self, Self::Settled)
26    }
27}
28
29impl std::fmt::Display for JobStatus {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        let s = match self {
32            Self::Queued => "queued",
33            Self::Running => "running",
34            Self::Proving => "proving",
35            Self::Done => "done",
36            Self::Failed => "failed",
37            Self::Settled => "settled",
38        };
39        write!(f, "{s}")
40    }
41}
42
43// ── /v1/jobs ─────────────────────────────────────────────────────────────────
44
45/// Body for POST /v1/jobs.
46#[derive(Debug, Clone, Serialize)]
47pub(crate) struct SubmitJobRequest {
48    pub input_data: Vec<Vec<f64>>,
49    pub model_id: String,
50}
51
52/// Response from POST /v1/jobs.
53#[derive(Debug, Clone, Deserialize)]
54pub(crate) struct SubmitJobResponse {
55    pub job_id: String,
56    pub status: String,
57}
58
59/// Response from GET /v1/jobs/{id}.
60///
61/// Note: `attestation_hash` requires the gateway's `JobStatusResponse` to
62/// include the field. Add `attestation_hash: Option<String>` to that struct
63/// and populate it from the DB row. Until then this field will always be None.
64#[derive(Debug, Clone, Deserialize)]
65pub struct Job {
66    pub job_id: String,
67    pub status: JobStatus,
68    pub proof_path: Option<String>,
69    pub tx_hash: Option<String>,
70    /// Populated once the gateway exposes it (see impl guide §6c).
71    pub attestation_hash: Option<String>,
72    /// Present when status is `failed`.
73    pub reason: Option<String>,
74}
75
76// ── /v1/jobs/{id}/proof ──────────────────────────────────────────────────────
77
78/// Response from GET /v1/jobs/{id}/proof.
79#[derive(Debug, Clone, Deserialize)]
80pub struct Proof {
81    pub job_id: String,
82    pub proof_hex: String,
83    pub size_bytes: usize,
84}
85
86// ── /v1/models ───────────────────────────────────────────────────────────────
87
88/// Body for POST /v1/models.
89#[derive(Debug, Clone, Serialize)]
90pub struct RegisterModelRequest {
91    pub name: String,
92    pub version: String,
93    /// Base64-encoded ONNX model file.
94    pub artifact_b64: String,
95    /// Expected input dimensions, e.g. `[1, 4]`.
96    pub input_shape: Vec<u64>,
97}
98
99/// Response from POST /v1/models.
100#[derive(Debug, Clone, Deserialize)]
101pub struct RegisterModelResponse {
102    pub model_id: String,
103    pub on_chain_model_id: String,
104    pub ipfs_cid: String,
105    pub gateway_url: String,
106    pub on_chain_hash: String,
107}
108
109// ── /healthz ─────────────────────────────────────────────────────────────────
110
111/// Response from GET /healthz.
112#[derive(Debug, Clone, Deserialize)]
113pub struct Health {
114    pub status: String,
115    pub version: String,
116    pub settle_enabled: bool,
117    pub db: String,
118}
119
120impl Health {
121    /// Returns true when the gateway reports a healthy state.
122    pub fn is_healthy(&self) -> bool {
123        self.status == "ok" && self.db == "connected"
124    }
125}
126
127// ── High-level result ─────────────────────────────────────────────────────────
128
129/// Returned by `VeilClient::verify_inference`.
130#[derive(Debug, Clone)]
131pub struct VerifyResult {
132    /// Gateway job identifier.
133    pub job_id: String,
134    /// Terminal status (`done` or `settled`).
135    pub status: JobStatus,
136    /// On-chain transaction hash. Present once the proof is settled.
137    pub tx_hash: Option<String>,
138    /// keccak256(modelId ‖ inputHash ‖ outputHash).
139    /// Populated once the gateway exposes the field (see impl guide §6c).
140    pub attestation_hash: Option<String>,
141    /// Wall-clock milliseconds from job submission to terminal state.
142    pub elapsed_ms: u64,
143}