gent-daemon 0.1.7

Local daemon, storage and synchronization for Gent chat clusters.
Documentation
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
use super::*;
use crate::encrypted::{Received, ordered::OrderedEndpoint};

fn queued(pair: &mut Pair) -> (SignedOwnerCandidate, SignedOwnerTransition, Vec<u8>) {
    let candidate = prepare(pair);
    prepare_readiness(pair, &candidate);
    let key = credential(pair, 0);
    let authorization = pair.stores[0]
        .authorize_owner_handoff(&key, &candidate)
        .unwrap();
    pair.stores[0]
        .queue_owner_handoff_delivery(&key, &candidate.candidate.id)
        .unwrap();
    let group = pair.stores[0]
        .db
        .query_row(
            "SELECT group_id FROM owner_deliveries WHERE id=?1",
            [&candidate.candidate.id],
            |r| r.get(0),
        )
        .unwrap();
    (candidate, authorization, group)
}

fn publish(pair: &Pair, group: &[u8], timestamp: u64) -> Option<gent_crypto::stream::Packet> {
    let mut endpoint =
        OrderedEndpoint::open_runtime(pair.homes[0].path(), &pair.stores[0].identity.node).unwrap();
    endpoint
        .prepare_configuration(group, None, timestamp)
        .unwrap()
        .or_else(|| endpoint.prepare_owner_handoff(group, timestamp).unwrap())
}

fn receive(pair: &Pair, packet: &gent_crypto::stream::Packet, timestamp: u64) -> Vec<Received> {
    let mut endpoint =
        OrderedEndpoint::open_runtime(pair.homes[1].path(), &pair.stores[1].identity.node).unwrap();
    endpoint.stage_configuration(packet, timestamp).unwrap();
    endpoint
        .drain_configuration(packet.group(), 1, timestamp)
        .unwrap()
}

#[tokio::test]
async fn authentic_transport_cannot_import_an_unsigned_or_changed_journal() {
    use crate::encrypted::ordered::configuration::handoff::test_packets;
    for variant in 0..5 {
        let mut pair = Pair::new().await;
        let (candidate, authorization, group) = queued(&mut pair);
        let id = &candidate.candidate.id;
        let manifest = pair.stores[0]
            .owner_handoff_manifest(&credential(&pair, 0), id)
            .unwrap();
        let timestamp = now() as u64;
        let mut record = json!({"version":1,"handoff":{"kind":"manifest","id":id,"authorization":authorization,"manifest":manifest}});
        match variant {
            0 => record["handoff"]["authorization"]["signature"][0] = json!(0),
            1 => record["handoff"]["manifest"]["candidate"][0] = json!(0),
            2 => record["handoff"]["id"] = json!(Uuid::new_v4().to_string()),
            3 => record = json!({"version":1,"handoff":{"kind":"message","id":id,"text":"bypass"}}),
            4 => {
                let packet = publish(&pair, &group, timestamp).unwrap();
                receive(&pair, &packet, timestamp);
                record = json!({"version":1,"handoff":{"kind":"page","id":id,"index":0,"data":STANDARD.encode(b"tampered")}});
            }
            _ => unreachable!(),
        }
        // Ensure a deterministic mutation even if the original byte was zero.
        if variant == 0 {
            record["handoff"]["authorization"]["signature"][0] =
                json!(authorization.signature[0] ^ 1);
        }
        if variant == 1 {
            record["handoff"]["manifest"]["candidate"][0] = json!(manifest.candidate[0] ^ 1);
        }
        let packet = test_packets::publish(
            &mut OrderedEndpoint::open_runtime(pair.homes[0].path(), &pair.stores[0].identity.node)
                .unwrap(),
            &group,
            &serde_json::to_vec(&record).unwrap(),
            timestamp,
        )
        .unwrap();
        assert!(pair.stores[1].sync_receive(&packet).is_err());
        let next =
            OrderedEndpoint::open_runtime(pair.homes[1].path(), &pair.stores[1].identity.node)
                .unwrap()
                .configuration_status(&group, timestamp)
                .unwrap()
                .next;
        assert_eq!(next, packet.sequence());
        assert_eq!(
            pair.stores[1]
                .db
                .query_row("SELECT COUNT(*) FROM owner_journal_pages", [], |r| r
                    .get::<_, usize>(0))
                .unwrap(),
            0
        );
        assert_eq!(
            pair.stores[1]
                .identity
                .state(&pair.stores[1].db)
                .unwrap()
                .checkpoint(),
            &candidate.candidate.base
        );
    }
}

#[tokio::test]
async fn signed_history_precedes_delivery_and_cyclic_handoffs_use_the_current_epoch() {
    let mut pair = Pair::new().await;
    configure(
        &mut pair,
        0,
        &Uuid::new_v4().to_string(),
        ConfigurationEdit::Renew,
    )
    .unwrap();
    // The successor already accepted this checkpoint by a trusted local route,
    // but its predecessor stream still owes the historical record.
    let state = pair.stores[0].identity.state(&pair.stores[0].db).unwrap();
    pair.stores[1]
        .identity
        .apply(&pair.stores[1].db, state.signed().clone())
        .unwrap();
    let (candidate, authorization, group) = queued(&mut pair);
    let packet = publish(&pair, &group, now() as u64).unwrap();
    assert!(matches!(
        receive(&pair, &packet, now() as u64)[0],
        Received::ConfigurationPart { complete: true, .. }
    ));
    owner_activation_tests::configuration_only(&mut pair, 0, 1);
    let key = credential(&pair, 1);
    pair.stores[1]
        .accept_owner_handoff(&key, &candidate.candidate.id, &authorization)
        .unwrap();
    pair.stores[1]
        .activate_owner_handoff(&key, &candidate.candidate.id)
        .unwrap();
    pair.pump();
    for (from, to) in [(1, 0), (0, 1)] {
        let (candidate, _, _) = owner_activation_tests::complete(&mut pair, from, to);
        let id = &candidate.candidate.id;
        pair.stores[from]
            .queue_owner_handoff_delivery(&credential(&pair, from), id)
            .unwrap();
        let wire: Vec<u8> = pair.stores[from].db.query_row("SELECT c.grant FROM owner_deliveries d JOIN configuration_streams c USING(group_id) WHERE d.id=?1", [id], |r| r.get(0)).unwrap();
        let grant = gent_crypto::SignedConfigurationGrant::decode(&wire).unwrap();
        assert_eq!(
            grant.grant.owner_transitions.len()
                + usize::from(grant.grant.owner_candidate.is_some()),
            pair.stores[from]
                .identity
                .state(&pair.stores[from].db)
                .unwrap()
                .owner_epoch() as usize
        );
        owner_activation_tests::configuration_only(&mut pair, from, to);
        pair.stores[to]
            .activate_owner_handoff(&credential(&pair, to), id)
            .unwrap();
        pair.pump();
    }
}

#[tokio::test]
async fn encrypted_delivery_resumes_after_expiry_and_restart_without_activating() {
    let mut pair = Pair::new().await;
    let origin = pair.stores[0].identity.node.id().to_owned();
    let result = serde_json::to_vec(&crate::messages::EditResult::Rejected {
        reason: "r".repeat(1024),
    })
    .unwrap();
    let fingerprint = hash(&serde_json::to_string(&ConfigurationEdit::Renew).unwrap());
    pair.stores[0].db.execute("WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<600) INSERT INTO configuration_edits SELECT ?1,printf('00000000-0000-4000-8000-%012x',x),?1,?2,?3,?4,1 FROM n", params![origin, fingerprint, br#"{"op":"renew"}"#.as_slice(), result]).unwrap();
    let (candidate, authorization, group) = queued(&mut pair);
    let keys = [credential(&pair, 0), credential(&pair, 1)];
    let id = &candidate.candidate.id;
    let state = pair.stores[0].identity.state(&pair.stores[0].db).unwrap();
    let timestamp = state.state().expires_at + 1;
    let manifest = pair.stores[0].owner_handoff_manifest(&keys[0], id).unwrap();
    assert!(manifest.pages.len() >= 3);
    assert!(
        OrderedEndpoint::open_runtime(pair.homes[0].path(), &pair.stores[0].identity.node)
            .unwrap()
            .send(
                &pair.group(),
                Uuid::new_v4(),
                b"ordinary traffic",
                timestamp
            )
            .is_err()
    );
    let mut records = 0;
    while let Some(packet) = publish(&pair, &group, timestamp) {
        assert!(
            !packet
                .encode()
                .unwrap()
                .windows(id.len())
                .any(|w| w == id.as_bytes())
        );
        if records == 1 {
            pair.reopen(0);
            pair.reopen(1);
        }
        let received = receive(&pair, &packet, timestamp);
        assert_eq!(received, vec![Received::OwnerHandoff]);
        // An exact packet replay cannot allocate another import or ratchet.
        assert!(receive(&pair, &packet, timestamp).is_empty());
        let receipt =
            OrderedEndpoint::open_runtime(pair.homes[1].path(), &pair.stores[1].identity.node)
                .unwrap()
                .receipt(&group, timestamp)
                .unwrap();
        OrderedEndpoint::open_runtime(pair.homes[0].path(), &pair.stores[0].identity.node)
            .unwrap()
            .confirm(&receipt, pair.stores[1].identity.node.id(), timestamp)
            .unwrap();
        records += 1;
    }
    assert_eq!(records, manifest.pages.len() + 1);
    assert_eq!(
        pair.stores[0]
            .queue_owner_handoff_delivery(&keys[0], id)
            .unwrap()["publication_complete"],
        true
    );
    assert_eq!(
        pair.stores[1]
            .owner_handoff_import_status(&keys[1], id)
            .unwrap()["pages_complete"],
        true
    );
    assert_eq!(
        pair.stores[1]
            .identity
            .state(&pair.stores[1].db)
            .unwrap()
            .checkpoint(),
        &candidate.candidate.base
    );
    assert_eq!(status(&mut pair, 1)["completed_candidates"], 0);
    pair.stores[1]
        .accept_owner_handoff(&keys[1], id, &authorization)
        .unwrap();
    pair.stores[1].activate_owner_handoff(&keys[1], id).unwrap();
}

#[tokio::test]
async fn publication_and_receive_failures_preserve_ratchets_cursors_and_retries() {
    let mut pair = Pair::new().await;
    let (candidate, _, group) = queued(&mut pair);
    let timestamp = now() as u64;
    let before = OrderedEndpoint::open_runtime(pair.homes[0].path(), &pair.stores[0].identity.node)
        .unwrap()
        .configuration_status(&group, timestamp)
        .unwrap();
    pair.stores[0].db.execute_batch("CREATE TRIGGER fail_delivery BEFORE UPDATE ON owner_deliveries BEGIN SELECT RAISE(ABORT,'delivery failure'); END;").unwrap();
    assert!(
        OrderedEndpoint::open_runtime(pair.homes[0].path(), &pair.stores[0].identity.node)
            .unwrap()
            .prepare_owner_handoff(&group, timestamp)
            .is_err()
    );
    assert_eq!(
        OrderedEndpoint::open_runtime(pair.homes[0].path(), &pair.stores[0].identity.node)
            .unwrap()
            .configuration_status(&group, timestamp)
            .unwrap(),
        before
    );
    pair.stores[0]
        .db
        .execute_batch("DROP TRIGGER fail_delivery")
        .unwrap();
    let packet = publish(&pair, &group, timestamp).unwrap();
    assert_eq!(packet.sequence(), before.head + 1);
    pair.stores[1].db.execute_batch("CREATE TRIGGER fail_import BEFORE INSERT ON owner_journals BEGIN SELECT RAISE(ABORT,'import failure'); END;").unwrap();
    assert!(pair.stores[1].sync_receive(&packet).is_err());
    let receiver =
        OrderedEndpoint::open_runtime(pair.homes[1].path(), &pair.stores[1].identity.node)
            .unwrap()
            .configuration_status(&group, timestamp)
            .unwrap();
    assert_eq!(receiver.next, packet.sequence());
    assert_eq!(receiver.pending, 1);
    pair.stores[1]
        .db
        .execute_batch("DROP TRIGGER fail_import")
        .unwrap();
    pair.reopen(1);
    assert_eq!(
        pair.stores[1].sync_next(&group).unwrap(),
        packet.sequence() + 1
    );
    let packet = publish(&pair, &group, timestamp).unwrap();
    pair.stores[1].db.execute_batch("CREATE TRIGGER fail_page BEFORE INSERT ON owner_journal_pages BEGIN SELECT RAISE(ABORT,'page failure'); END;").unwrap();
    assert!(pair.stores[1].sync_receive(&packet).is_err());
    assert_eq!(
        pair.stores[1]
            .owner_handoff_import_status(&credential(&pair, 1), &candidate.candidate.id)
            .unwrap()["received"],
        0
    );
    pair.stores[1]
        .db
        .execute_batch("DROP TRIGGER fail_page")
        .unwrap();
    assert_eq!(
        pair.stores[1].sync_next(&group).unwrap(),
        packet.sequence() + 1
    );
    assert_eq!(
        pair.stores[1]
            .owner_handoff_import_status(&credential(&pair, 1), &candidate.candidate.id)
            .unwrap()["pages_complete"],
        true
    );
}

#[tokio::test]
async fn delivery_requires_issued_authority_local_owner_and_exact_prepared_candidate() {
    let mut pair = Pair::new().await;
    let key = credential(&pair, 0);
    let unissued = prepare(&mut pair);
    assert!(
        pair.stores[0]
            .queue_owner_handoff_delivery(&key, &unissued.candidate.id)
            .is_err()
    );
    let (candidate, _, group) = queued(&mut pair);
    let id = &candidate.candidate.id;
    assert!(
        pair.stores[0]
            .queue_owner_handoff_delivery(&pair.keys[0], id)
            .is_err()
    );
    assert!(
        pair.stores[1]
            .queue_owner_handoff_delivery(&credential(&pair, 1), id)
            .is_err()
    );
    let original: Vec<u8> = pair.stores[1]
        .db
        .query_row(
            "SELECT candidate FROM owner_candidates WHERE id=?1",
            [id],
            |r| r.get(0),
        )
        .unwrap();
    pair.stores[1]
        .db
        .execute(
            "UPDATE owner_candidates SET candidate=?1 WHERE id=?2",
            params![unissued.encode().unwrap(), id],
        )
        .unwrap();
    let packet = publish(&pair, &group, now() as u64).unwrap();
    assert!(pair.stores[1].sync_receive(&packet).is_err());
    pair.stores[1]
        .db
        .execute(
            "UPDATE owner_candidates SET candidate=?1 WHERE id=?2",
            params![original, id],
        )
        .unwrap();
    pair.stores[1].sync_next(&group).unwrap();
    owner_activation_tests::configuration_only(&mut pair, 0, 1);
    assert_eq!(
        pair.stores[1]
            .owner_handoff_import_status(&credential(&pair, 1), id)
            .unwrap()["pages_complete"],
        true
    );
}

#[tokio::test]
async fn delivery_after_local_activation_accepts_only_existing_identical_artifacts() {
    let mut pair = Pair::new().await;
    let candidate = prepare(&mut pair);
    let authorization = issue(&mut pair, &candidate);
    let keys = [credential(&pair, 0), credential(&pair, 1)];
    let id = &candidate.candidate.id;
    pair.stores[1]
        .accept_owner_handoff(&keys[1], id, &authorization)
        .unwrap();
    pair.stores[1].activate_owner_handoff(&keys[1], id).unwrap();
    configure(
        &mut pair,
        1,
        &Uuid::new_v4().to_string(),
        ConfigurationEdit::Renew,
    )
    .unwrap();
    // These are newly encrypted application records carrying already imported
    // artifacts, not packet replays caught by the ordered receive layer.
    pair.stores[0]
        .queue_owner_handoff_delivery(&keys[0], id)
        .unwrap();
    owner_activation_tests::configuration_only(&mut pair, 0, 1);
    let page = pair.stores[0].owner_handoff_page(&keys[0], id, 0).unwrap();
    pair.stores[1]
        .db
        .execute(
            "DELETE FROM owner_journal_pages WHERE id=?1 AND page=0",
            [id],
        )
        .unwrap();
    assert!(
        pair.stores[1]
            .import_owner_handoff_page(&keys[1], id, 0, &page)
            .unwrap_err()
            .to_string()
            .contains("cannot repair")
    );
}

#[tokio::test]
async fn delivery_schema_migrates_and_missing_current_storage_fails_closed() {
    let mut pair = Pair::new().await;
    let (candidate, _, _) = queued(&mut pair);
    pair.stores[0]
        .db
        .execute_batch("DROP TABLE owner_deliveries; PRAGMA user_version=23;")
        .unwrap();
    pair.reopen(0);
    assert_eq!(status(&mut pair, 0)["signing_frozen"], true);
    pair.stores[0]
        .queue_owner_handoff_delivery(&credential(&pair, 0), &candidate.candidate.id)
        .unwrap();
    pair.reopen(0);
    assert_eq!(status(&mut pair, 0)["delivery"]["jobs"], 1);
    pair.stores[0]
        .db
        .execute_batch("DROP TABLE owner_deliveries")
        .unwrap();
    assert!(
        Store::open(pair.homes[0].path())
            .err()
            .unwrap()
            .to_string()
            .contains("delivery storage is missing")
    );
}