zc2 0.0.48

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
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
//! A fake hub over loopback for the outbox tests: the Spaces publisher
//! routes, presigned object PUT/GET, and the receipt routes, with failures
//! injected per route. No real network.

use crate::receipt_verify::{self as rv, testkit::Hub as Signer};
use axum::body::Body;
use axum::extract::{Request, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use base64::Engine;
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, Mutex};

pub struct FakeBatch {
    pub seq: u64,
    pub inputs: Value,
    pub status: String,
    pub outputs: Vec<(String, String)>, // (name, declared sha256)
    pub receipt_hash: Option<String>,
}

pub struct FakeSpace {
    pub id: String,
    pub req: Value,
    pub status: String,
    pub mirror_polls_left: u32,
    pub digest: String,
    pub batches: Vec<FakeBatch>,
}

pub struct St {
    pub base: String,
    pub token: Option<String>,
    pub spaces: Vec<FakeSpace>,
    pub objects: HashMap<String, Vec<u8>>,
    pub calls: Vec<String>,
    /// Route key → statuses to answer with, before the route does anything.
    pub fail: HashMap<String, VecDeque<u16>>,
    /// Route keys whose operation is APPLIED and then answered with the
    /// failure: the answer is lost, the effect is not.
    pub apply_then_fail: HashSet<String>,
    pub signer: Signer,
    pub receipts: HashMap<String, Value>,
    pub resolved_digest: String,
    pub mirror_polls: u32,
    pub auto_complete: bool,
    pub with_receipt_hash: bool,
    pub tamper_output: bool,
    pub bad_receipt_outputs: bool,
    pub fail_second_batch: bool,
    pub object_auth_seen: bool,
}

pub struct FakeHub {
    pub base: String,
    pub st: Arc<Mutex<St>>,
}

fn ok(v: Value) -> (u16, Value) {
    (200, v)
}

fn err(status: u16, detail: &str) -> (u16, Value) {
    (status, json!({ "detail": detail }))
}

impl St {
    pub fn count(&self, key: &str) -> usize {
        self.calls.iter().filter(|c| c.as_str() == key).count()
    }

    fn space_json(&self, i: usize) -> Value {
        let s = &self.spaces[i];
        let n = |st: &str| s.batches.iter().filter(|b| b.status == st).count();
        json!({
            "id": s.id,
            "name": s.req["name"],
            "description": s.req["description"],
            "image_digest": s.digest,
            "mine": true,
            "status": s.status,
            "mirror": {"status": if s.mirror_polls_left == 0 {"ready"} else {"pending"}, "error": null},
            "counts": {"pending": n("pending"), "claimed": n("claimed"),
                       "succeeded": n("succeeded"), "failed": n("failed")},
        })
    }

    /// Finish every unfinished batch, as a producer node and the hub would.
    pub fn complete_all(&mut self, idx: usize) {
        let space_id = self.spaces[idx].id.clone();
        let fail2 = self.fail_second_batch;
        let n = self.spaces[idx].batches.len();
        for b in 0..n {
            if !matches!(
                self.spaces[idx].batches[b].status.as_str(),
                "pending" | "claimed"
            ) {
                continue;
            }
            let seq = self.spaces[idx].batches[b].seq;
            if fail2 && seq == 2 {
                self.spaces[idx].batches[b].status = "failed".into();
                continue;
            }
            let content = format!("result-{seq}").into_bytes();
            let sha = rv::sha256_hex(&content);
            let stored = if self.tamper_output {
                b"tampered".to_vec()
            } else {
                content
            };
            self.objects
                .insert(format!("spaces/{space_id}/out/{seq}/out.txt"), stored);
            let mut receipt_hash = None;
            if self.with_receipt_hash {
                let signed_hashes = if self.bad_receipt_outputs {
                    vec!["0".repeat(64)]
                } else {
                    vec![sha.clone()]
                };
                let r = self.signer.batch_receipt(&signed_hashes, "50.000000");
                let h = rv::sha256_hex(r.receipt_json.as_bytes());
                self.receipts.insert(h.clone(), self.signer.public_view(&r));
                receipt_hash = Some(h);
            }
            let batch = &mut self.spaces[idx].batches[b];
            batch.status = "succeeded".into();
            batch.outputs = vec![("out.txt".into(), sha)];
            batch.receipt_hash = receipt_hash;
        }
        self.spaces[idx].status = "done".into();
    }

    fn route(
        &mut self,
        method: &str,
        path: &str,
        headers_auth: Option<String>,
        body: Vec<u8>,
        query: &str,
    ) -> (String, (u16, Value), Option<Vec<u8>>) {
        let seg: Vec<&str> = path.trim_matches('/').split('/').collect();
        let authed = || {
            self.token.is_none()
                || headers_auth.as_deref()
                    == self
                        .token
                        .as_ref()
                        .map(|t| format!("Bearer {t}"))
                        .as_deref()
        };
        let find = |s: &St, id: &str| s.spaces.iter().position(|x| x.id == id);
        match (method, seg.as_slice()) {
            ("GET", ["api", "receipts", "pubkeys"]) => {
                ("pubkeys".into(), ok(self.signer.pubkeys_doc()), None)
            }
            ("GET", ["api", "receipts", h]) => (
                "receipt".into(),
                self.receipts
                    .get(*h)
                    .cloned()
                    .map(ok)
                    .unwrap_or_else(|| err(404, "receipt not found")),
                None,
            ),
            ("PUT", ["obj", rest @ ..]) => {
                let key = rest.join("/");
                if headers_auth.is_some() {
                    self.object_auth_seen = true;
                }
                (
                    "put".into(),
                    ok(json!({})),
                    Some({
                        self.objects.insert(key, body);
                        vec![]
                    }),
                )
            }
            ("GET", ["obj", rest @ ..]) => {
                if headers_auth.is_some() {
                    self.object_auth_seen = true;
                }
                match self.objects.get(&rest.join("/")) {
                    Some(b) => ("download".into(), ok(json!({})), Some(b.clone())),
                    None => ("download".into(), err(404, "no object"), None),
                }
            }
            (_, ["api", "spaces", ..]) if !authed() => {
                ("auth".into(), err(401, "Invalid or expired token"), None)
            }
            ("POST", ["api", "spaces"]) => {
                let req: Value = serde_json::from_slice(&body).unwrap_or_default();
                let image = req["image_ref"].as_str().unwrap_or("");
                let digest = image
                    .split_once('@')
                    .map(|(_, d)| d.to_string())
                    .unwrap_or_else(|| self.resolved_digest.clone());
                let id = format!("sp-{}", self.spaces.len() + 1);
                self.spaces.push(FakeSpace {
                    id,
                    req,
                    status: "draft".into(),
                    mirror_polls_left: self.mirror_polls,
                    digest,
                    batches: vec![],
                });
                let i = self.spaces.len() - 1;
                ("create".into(), (201, self.space_json(i)), None)
            }
            ("GET", ["api", "spaces"]) => {
                let items: Vec<Value> =
                    (0..self.spaces.len()).map(|i| self.space_json(i)).collect();
                ("list".into(), ok(json!({ "items": items })), None)
            }
            ("GET", ["api", "spaces", id]) => match find(self, id) {
                Some(i) => {
                    if self.spaces[i].mirror_polls_left > 0 {
                        self.spaces[i].mirror_polls_left -= 1;
                    }
                    if self.auto_complete && self.spaces[i].status == "live" {
                        self.complete_all(i);
                    }
                    ("get".into(), ok(self.space_json(i)), None)
                }
                None => ("get".into(), err(404, "No such Space"), None),
            },
            ("POST", ["api", "spaces", id, "inputs:presign"]) => match find(self, id) {
                Some(_) => {
                    let req: Value = serde_json::from_slice(&body).unwrap_or_default();
                    let ups: Vec<Value> = req["files"]
                        .as_array()
                        .into_iter()
                        .flatten()
                        .map(|f| {
                            let n = f["name"].as_str().unwrap_or("");
                            json!({"name": n, "key": format!("hub://spaces/{id}/in/{n}"),
                               "url": format!("{}/obj/spaces/{id}/in/{n}", self.base)})
                        })
                        .collect();
                    ("presign".into(), ok(json!({ "uploads": ups })), None)
                }
                None => ("presign".into(), err(404, "No such Space"), None),
            },
            ("POST", ["api", "spaces", id, "batches"]) => match find(self, id) {
                Some(i) => {
                    let mut parsed = vec![];
                    for line in String::from_utf8_lossy(&body)
                        .lines()
                        .filter(|l| !l.trim().is_empty())
                    {
                        let doc: Value = serde_json::from_str(line).unwrap_or_default();
                        for uri in doc["inputs"]
                            .as_object()
                            .into_iter()
                            .flatten()
                            .map(|(_, v)| v.as_str().unwrap_or(""))
                        {
                            let key = uri.strip_prefix("hub://").unwrap_or("");
                            if !uri.starts_with(&format!("hub://spaces/{id}/in/"))
                                || !self.objects.contains_key(key)
                            {
                                return (
                                    "batches_post".into(),
                                    err(400, &format!("object not found: {uri}")),
                                    None,
                                );
                            }
                        }
                        parsed.push(doc["inputs"].clone());
                    }
                    let last = self.spaces[i].batches.len() as u64;
                    let n = parsed.len() as u64;
                    for (k, inputs) in parsed.into_iter().enumerate() {
                        self.spaces[i].batches.push(FakeBatch {
                            seq: last + k as u64 + 1,
                            inputs,
                            status: "pending".into(),
                            outputs: vec![],
                            receipt_hash: None,
                        });
                    }
                    (
                        "batches_post".into(),
                        ok(json!({"added": n, "first_seq": last + 1, "last_seq": last + n})),
                        None,
                    )
                }
                None => ("batches_post".into(), err(404, "No such Space"), None),
            },
            ("GET", ["api", "spaces", id, "batches"]) => match find(self, id) {
                Some(i) => {
                    let want = query.split('&').find_map(|p| p.strip_prefix("status="));
                    let sid = self.spaces[i].id.clone();
                    let items: Vec<Value> = self.spaces[i].batches.iter()
                        .filter(|b| want.is_none_or(|w| b.status == w))
                        .map(|b| {
                            let mut v = json!({
                                "seq": b.seq, "status": b.status, "inputs": [],
                                "outputs": b.outputs.iter().map(|(n, _)| (n.clone(), json!(format!("{}/obj/spaces/{sid}/out/{}/{n}", self.base, b.seq)))).collect::<serde_json::Map<_,_>>(),
                                "output_hashes": b.outputs.iter().map(|(n, h)| (n.clone(), json!(h))).collect::<serde_json::Map<_,_>>(),
                            });
                            if let Some(h) = &b.receipt_hash {
                                v["receipt_hash"] = json!(h);
                            }
                            v
                        }).collect();
                    (
                        "batches_list".into(),
                        ok(json!({"items": items, "next_cursor": null})),
                        None,
                    )
                }
                None => ("batches_list".into(), err(404, "No such Space"), None),
            },
            ("POST", ["api", "spaces", id, "publish"]) => match find(self, id) {
                Some(i) if self.spaces[i].status != "draft" => (
                    "publish".into(),
                    err(409, "Space is live, not a draft"),
                    None,
                ),
                Some(i) if self.spaces[i].mirror_polls_left > 0 => (
                    "publish".into(),
                    err(409, "Image mirror is pending, not ready"),
                    None,
                ),
                Some(i) => {
                    self.spaces[i].status = "live".into();
                    ("publish".into(), ok(self.space_json(i)), None)
                }
                None => ("publish".into(), err(404, "No such Space"), None),
            },
            ("POST", ["api", "spaces", id, "cancel"]) => match find(self, id) {
                Some(i) if matches!(self.spaces[i].status.as_str(), "done" | "cancelled") => (
                    "cancel".into(),
                    err(409, "Cannot move a done Space to cancelled"),
                    None,
                ),
                Some(i) => {
                    self.spaces[i].status = "cancelled".into();
                    ("cancel".into(), ok(self.space_json(i)), None)
                }
                None => ("cancel".into(), err(404, "No such Space"), None),
            },
            _ => ("unknown".into(), err(404, "no such route"), None),
        }
    }
}

async fn handle(State(st): State<Arc<Mutex<St>>>, req: Request) -> Response {
    let method = req.method().to_string();
    let path = req.uri().path().to_string();
    let query = req.uri().query().unwrap_or("").to_string();
    let auth = req
        .headers()
        .get("authorization")
        .and_then(|h| h.to_str().ok())
        .map(str::to_string);
    let checksum = req
        .headers()
        .get("x-amz-checksum-sha256")
        .and_then(|h| h.to_str().ok())
        .map(str::to_string);
    let body = axum::body::to_bytes(req.into_body(), 64 << 20)
        .await
        .unwrap_or_default()
        .to_vec();
    let mut s = st.lock().unwrap();
    // The checksum binding a presigned PUT enforces.
    if method == "PUT" {
        let want = base64::engine::general_purpose::STANDARD
            .encode(hex::decode(rv::sha256_hex(&body)).unwrap());
        if checksum.as_deref() != Some(want.as_str()) {
            s.calls.push("put_rejected".into());
            return (StatusCode::BAD_REQUEST, "checksum mismatch").into_response();
        }
    }
    let injected = |s: &mut St, key: &str| s.fail.get_mut(key).and_then(VecDeque::pop_front);
    // Resolve the route key first, with a dry classification, so a failure
    // can be injected before or after the effect.
    let (key, mut result, raw) = {
        // Peek at the key without effects by routing a clone-free probe:
        // injection happens on the key the real route reports, so route
        // first for "apply then fail" keys and last for the others.
        let probe = route_key(&method, &path);
        if let Some(code) = (!s.apply_then_fail.contains(&probe))
            .then(|| injected(&mut s, &probe))
            .flatten()
        {
            s.calls.push(probe);
            return (
                StatusCode::from_u16(code).unwrap(),
                "{\"detail\":\"injected\"}",
            )
                .into_response();
        }
        s.route(&method, &path, auth, body, &query)
    };
    s.calls.push(key.clone());
    if s.apply_then_fail.contains(&key) {
        if let Some(code) = injected(&mut s, &key) {
            result = (code, json!({"detail": "answer lost"}));
        }
    }
    let (status, v) = result;
    let status = StatusCode::from_u16(status).unwrap();
    match raw {
        Some(bytes) if status.is_success() && method == "GET" => Response::builder()
            .status(status)
            .body(Body::from(bytes))
            .unwrap(),
        _ => (status, axum::Json(v)).into_response(),
    }
}

/// The route key of a request, without routing it.
fn route_key(method: &str, path: &str) -> String {
    let seg: Vec<&str> = path.trim_matches('/').split('/').collect();
    match (method, seg.as_slice()) {
        ("POST", ["api", "spaces"]) => "create",
        ("GET", ["api", "spaces"]) => "list",
        ("GET", ["api", "spaces", _]) => "get",
        ("POST", ["api", "spaces", _, "inputs:presign"]) => "presign",
        ("POST", ["api", "spaces", _, "batches"]) => "batches_post",
        ("GET", ["api", "spaces", _, "batches"]) => "batches_list",
        ("POST", ["api", "spaces", _, "publish"]) => "publish",
        ("POST", ["api", "spaces", _, "cancel"]) => "cancel",
        ("PUT", ["obj", ..]) => "put",
        ("GET", ["obj", ..]) => "download",
        ("GET", ["api", "receipts", "pubkeys"]) => "pubkeys",
        ("GET", ["api", "receipts", _]) => "receipt",
        _ => "unknown",
    }
    .to_string()
}

impl FakeHub {
    pub fn start(token: Option<&str>) -> FakeHub {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        listener.set_nonblocking(true).unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        let st = Arc::new(Mutex::new(St {
            base: base.clone(),
            token: token.map(str::to_string),
            spaces: vec![],
            objects: HashMap::new(),
            calls: vec![],
            fail: HashMap::new(),
            apply_then_fail: HashSet::new(),
            signer: Signer::new(),
            receipts: HashMap::new(),
            resolved_digest: format!("sha256:{}", "c".repeat(64)),
            mirror_polls: 1,
            auto_complete: true,
            with_receipt_hash: true,
            tamper_output: false,
            bad_receipt_outputs: false,
            fail_second_batch: false,
            object_auth_seen: false,
        }));
        let app = axum::Router::new().fallback(handle).with_state(st.clone());
        std::thread::spawn(move || {
            let rt = tokio::runtime::Builder::new_multi_thread()
                .worker_threads(2)
                .enable_all()
                .build()
                .unwrap();
            rt.block_on(async move {
                let l = tokio::net::TcpListener::from_std(listener).unwrap();
                axum::serve(l, app).await.unwrap();
            });
        });
        FakeHub { base, st }
    }

    pub fn with<R>(&self, f: impl FnOnce(&mut St) -> R) -> R {
        f(&mut self.st.lock().unwrap())
    }

    pub fn count(&self, key: &str) -> usize {
        self.with(|s| s.count(key))
    }
}