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
use super::*;
use crate::messages::owner_preparation::{Envelope, Plan, Step};

#[path = "owner_transfer_tests.rs"]
mod owner_transfer_tests;

fn key(mesh: &Mesh, i: usize) -> String {
    config::read_private(&mesh.homes[i].path().join("owner.key")).unwrap()
}
async fn linked() -> Mesh {
    let mut mesh = Mesh::new().await;
    for _ in 0..25 {
        mesh.round(true);
        if (0..3)
            .all(|i| mesh.count(i, "network_links") == 2 && mesh.count(i, "network_events") == 0)
        {
            break;
        }
    }
    for i in 0..3 {
        assert_eq!(mesh.count(i, "network_links"), 2);
    }
    mesh
}
fn start(mesh: &mut Mesh, from: usize, to: usize, id: Uuid) {
    let credential = key(mesh, from);
    let target = mesh.stores[to].identity.node.id().to_owned();
    mesh.stores[from]
        .start_owner_preparation(&credential, id, &target)
        .unwrap();
}
fn status(mesh: &Mesh, i: usize, id: Uuid) -> Value {
    mesh.stores[i]
        .owner_preparation_status(&key(mesh, i), id)
        .unwrap()
}
fn round(mesh: &mut Mesh) {
    for store in &mut mesh.stores {
        store
            .db
            .execute("UPDATE owner_preparation_jobs SET retry_at=0", [])
            .unwrap();
        store.owner_preparation_tick().unwrap();
    }
    mesh.round(true);
}
fn ready(mesh: &mut Mesh, from: usize, id: Uuid) {
    for _ in 0..100 {
        round(mesh);
        if status(mesh, from, id)["readiness_recorded"] == true {
            return;
        }
    }
    panic!(
        "preparation did not finish: {:?}",
        (0..3).map(|i| status(mesh, i, id)).collect::<Vec<_>>()
    );
}
fn candidate(mesh: &Mesh, i: usize, id: Uuid) -> gent_crypto::succession::SignedOwnerCandidate {
    let bytes: Vec<u8> = mesh.stores[i]
        .db
        .query_row(
            "SELECT candidate FROM owner_preparation_plans WHERE id=?1",
            [id.to_string()],
            |r| r.get(0),
        )
        .unwrap();
    gent_crypto::succession::SignedOwnerCandidate::decode(&bytes).unwrap()
}
fn incoming(mesh: &Mesh, target: usize, sender: usize, envelope: &Envelope) -> Result<()> {
    let store = &mesh.stores[target];
    let tx = store.db.unchecked_transaction()?;
    store
        .network_session(&tx, store.identity.state(&tx)?)
        .import_owner_preparation(mesh.stores[sender].identity.node.id(), envelope)?;
    tx.commit()?;
    Ok(())
}
fn begin(mesh: &Mesh, id: Uuid) -> Envelope {
    let state = mesh.stores[0].identity.state(&mesh.stores[0].db).unwrap();
    let source = mesh.stores[0].identity.node.id().to_owned();
    let successor = mesh.stores[1].identity.node.id().to_owned();
    Envelope {
        version: 1,
        origin: source.clone(),
        target: successor.clone(),
        plan: Plan {
            id,
            anchor: state.anchor().clone(),
            base: state.checkpoint().clone(),
            source,
            successor,
            expires_at: now() as u64 + 900,
            auto_activate: false,
        },
        candidate: None,
        step: Step::Begin,
    }
}

#[tokio::test]
async fn encrypted_preparation_survives_restarts_and_supports_repeated_three_node_transfers() {
    let mut mesh = linked().await;
    for (epoch, from, to) in [(1, 0, 1), (2, 1, 2), (3, 2, 0)] {
        let id = Uuid::new_v4();
        start(&mut mesh, from, to, id);
        for _ in 0..3 {
            round(&mut mesh);
            for i in 0..3 {
                mesh.reopen(i);
            }
        }
        ready(&mut mesh, from, id);
        assert_eq!(candidate(&mesh, from, id), candidate(&mesh, to, id));
        for i in 0..3 {
            assert_eq!(
                mesh.stores[i]
                    .identity
                    .state(&mesh.stores[i].db)
                    .unwrap()
                    .owner_epoch(),
                epoch - 1
            );
            assert_eq!(status(&mesh, i, id)["signing_frozen"], false);
        }
        mesh.stores[from].require_provisioning_owner().unwrap();
        let source = key(&mesh, from);
        let target = key(&mesh, to);
        let proposal = candidate(&mesh, from, id);
        let authorization = mesh.stores[from]
            .authorize_owner_handoff(&source, &proposal)
            .unwrap();
        mesh.stores[from]
            .queue_owner_handoff_delivery(&source, &id.to_string())
            .unwrap();
        for _ in 0..100 {
            mesh.round(false);
            if mesh.stores[to]
                .owner_handoff_import_status(&target, &id.to_string())
                .is_ok_and(|s| s["pages_complete"] == true)
            {
                break;
            }
        }
        assert_eq!(
            mesh.stores[to]
                .owner_handoff_import_status(&target, &id.to_string())
                .unwrap()["pages_complete"],
            true
        );
        mesh.stores[to]
            .accept_owner_handoff(&target, &id.to_string(), &authorization)
            .unwrap();
        mesh.stores[to]
            .activate_owner_handoff(&target, &id.to_string())
            .unwrap();
        for _ in 0..100 {
            mesh.round(false);
            if mesh
                .stores
                .iter()
                .all(|store| store.identity.state(&store.db).unwrap().owner_epoch() == epoch)
            {
                break;
            }
        }
        for i in 0..3 {
            mesh.reopen(i);
            assert_eq!(
                mesh.stores[i]
                    .identity
                    .state(&mesh.stores[i].db)
                    .unwrap()
                    .owner_epoch(),
                epoch
            );
        }
        assert!(mesh.stores[from].require_provisioning_owner().is_err());
    }
}

#[tokio::test]
async fn preparation_effects_resume_when_each_response_outbox_commit_fails() {
    for (phase, node) in [
        ("candidate", 1),
        ("package", 0),
        ("prepared", 1),
        ("grant", 0),
        ("invitation", 1),
        ("confirmed", 2),
        ("ready", 1),
    ] {
        let mut mesh = linked().await;
        let id = Uuid::new_v4();
        mesh.stores[node].db.execute_batch(&format!("CREATE TRIGGER fail_response BEFORE INSERT ON network_events WHEN json_extract(NEW.body,'$.event.envelope.step.step')='{phase}' BEGIN SELECT RAISE(ABORT,'response failure'); END;")).unwrap();
        start(&mut mesh, 0, 1, id);
        let mut failed = false;
        for _ in 0..50 {
            round(&mut mesh);
            failed=mesh.stores[node].db.query_row("SELECT EXISTS(SELECT 1 FROM owner_preparation_jobs WHERE error LIKE '%response failure%') OR EXISTS(SELECT 1 FROM owner_preparation_plans WHERE error LIKE '%response failure%')",[],|r|r.get(0)).unwrap();
            if failed {
                break;
            }
        }
        assert!(failed, "did not reach {phase} failure");
        let saved = std::fs::read(
            mesh.homes[1]
                .path()
                .join(format!("owner-candidates/{id}.key")),
        )
        .unwrap();
        let packages: Vec<_> = (0..3).map(|i| mesh.count(i, "key_packages")).collect();
        for i in 0..3 {
            mesh.reopen(i);
        }
        mesh.stores[node]
            .db
            .execute_batch("DROP TRIGGER fail_response")
            .unwrap();
        ready(&mut mesh, 0, id);
        assert_eq!(
            saved,
            std::fs::read(
                mesh.homes[1]
                    .path()
                    .join(format!("owner-candidates/{id}.key"))
            )
            .unwrap()
        );
        assert_eq!(status(&mesh, 0, id)["signing_frozen"], false);
        // Completed work is inert, including native restarts and exact retries.
        let after: Vec<_> = (0..3).map(|i| mesh.count(i, "key_packages")).collect();
        for i in 0..3 {
            assert!(after[i] >= packages[i]);
            mesh.reopen(i);
        }
        start(&mut mesh, 0, 1, id);
        for _ in 0..3 {
            round(&mut mesh);
        }
        assert_eq!(
            after,
            (0..3)
                .map(|i| mesh.count(i, "key_packages"))
                .collect::<Vec<_>>()
        );
    }
}

#[tokio::test]
async fn preparation_rejects_wrong_credentials_roles_candidates_and_conflicting_retries() {
    let mut mesh = linked().await;
    let id = Uuid::new_v4();
    let target = mesh.stores[1].identity.node.id().to_owned();
    assert!(
        mesh.stores[0]
            .start_owner_preparation("wrong", id, &target)
            .is_err()
    );
    let joined = key(&mesh, 2);
    assert!(
        mesh.stores[2]
            .start_owner_preparation(&joined, id, &target)
            .is_err()
    );
    assert_eq!(mesh.count(0, "owner_preparation_plans"), 0);
    let envelope = begin(&mesh, id);
    assert!(incoming(&mesh, 1, 2, &envelope).is_err());
    let mut spoof = envelope.clone();
    spoof.origin = mesh.stores[2].identity.node.id().into();
    spoof.plan.source = spoof.origin.clone();
    assert!(incoming(&mesh, 1, 2, &spoof).is_err());
    let mut future = envelope.clone();
    future.plan.expires_at += 900;
    assert!(incoming(&mesh, 1, 0, &future).is_err());
    assert_eq!(mesh.count(1, "owner_preparation_plans"), 0);
    // Saving an authenticated Begin and its job is one transaction.
    mesh.stores[1].db.execute_batch("CREATE TRIGGER fail_admit BEFORE INSERT ON owner_preparation_jobs BEGIN SELECT RAISE(ABORT,'admission failure'); END;").unwrap();
    assert!(incoming(&mesh, 1, 0, &envelope).is_err());
    assert_eq!(mesh.count(1, "owner_preparation_plans"), 0);
    mesh.stores[1]
        .db
        .execute_batch("DROP TRIGGER fail_admit")
        .unwrap();
    incoming(&mesh, 1, 0, &envelope).unwrap();
    incoming(&mesh, 1, 0, &envelope).unwrap();
    assert_eq!(mesh.count(1, "owner_preparation_jobs"), 1);
    let mut changed = envelope.clone();
    changed.plan.expires_at -= 1;
    assert!(incoming(&mesh, 1, 0, &changed).is_err());
    // A successor cannot introduce itself on the source without a local plan.
    mesh.stores[1].owner_preparation_tick().unwrap();
    let mut proposal = envelope.clone();
    proposal.origin = target.clone();
    proposal.target = proposal.plan.source.clone();
    proposal.step = Step::Candidate;
    proposal.candidate = Some(candidate(&mesh, 1, id));
    assert!(incoming(&mesh, 0, 1, &proposal).is_err());
    assert_eq!(mesh.count(0, "owner_preparation_plans"), 0);
    // A forged candidate is rejected before either plan or job admission.
    let mut offer = proposal.clone();
    offer.origin = envelope.origin.clone();
    offer.target = mesh.stores[2].identity.node.id().into();
    offer.step = Step::Offer;
    offer.candidate.as_mut().unwrap().key_signature[0] ^= 1;
    assert!(incoming(&mesh, 2, 0, &offer).is_err());
    assert_eq!(mesh.count(2, "owner_preparation_plans"), 0);
}

#[tokio::test]
async fn preparation_becomes_stale_without_stopping_signing_when_configuration_changes() {
    let mut mesh = linked().await;
    let id = Uuid::new_v4();
    start(&mut mesh, 0, 1, id);
    ready(&mut mesh, 0, id);
    let proposal = candidate(&mesh, 0, id);
    let credential = key(&mesh, 0);
    mesh.stores[0]
        .execute(Request::Owner {
            credential: credential.clone(),
            action: OwnerOp::Configure {
                request_id: Uuid::new_v4().to_string(),
                edit: ConfigurationEdit::RenameNode {
                    name: "changed-during-preparation".into(),
                },
            },
        })
        .unwrap();
    for _ in 0..10 {
        round(&mut mesh);
    }
    assert_eq!(status(&mesh, 0, id)["current"], false);
    assert!(
        mesh.stores[0]
            .authorize_owner_handoff(&credential, &proposal)
            .is_err()
    );
    mesh.stores[0].require_provisioning_owner().unwrap();
    let next = Uuid::new_v4();
    start(&mut mesh, 0, 1, next);
    ready(&mut mesh, 0, next);
    assert_ne!(
        proposal.candidate.owner_key,
        candidate(&mesh, 0, next).candidate.owner_key
    );
}

#[tokio::test]
async fn preparation_schema_migrates_old_storage_and_refuses_current_storage_loss() {
    let mut mesh = linked().await;
    mesh.stores[0].db.execute_batch("DROP TABLE owner_preparation_jobs; DROP TABLE owner_preparation_plans; PRAGMA user_version=24;").unwrap();
    mesh.reopen(0);
    assert_eq!(mesh.count(0, "owner_preparation_jobs"), 0);
    assert_eq!(
        mesh.stores[0]
            .db
            .pragma_query_value(None, "user_version", |r| r.get::<_, u64>(0))
            .unwrap(),
        26
    );
    mesh.stores[0]
        .db
        .execute_batch("DROP TABLE owner_preparation_jobs;")
        .unwrap();
    assert!(Store::open(mesh.homes[0].path()).is_err());
}

#[tokio::test]
async fn preparation_start_is_atomic_and_full_plan_storage_preserves_exact_retries() {
    let mut mesh = linked().await;
    let source = key(&mesh, 0);
    let target = mesh.stores[1].identity.node.id().to_owned();
    let id = Uuid::new_v4();
    mesh.stores[0].db.execute_batch("CREATE TRIGGER fail_start BEFORE INSERT ON network_events BEGIN SELECT RAISE(ABORT,'start failure'); END;").unwrap();
    assert!(
        mesh.stores[0]
            .start_owner_preparation(&source, id, &target)
            .is_err()
    );
    assert_eq!(mesh.count(0, "owner_preparation_plans"), 0);
    mesh.stores[0]
        .db
        .execute_batch("DROP TRIGGER fail_start")
        .unwrap();
    start(&mut mesh, 0, 1, id);
    for _ in 1..64 {
        start(&mut mesh, 0, 1, Uuid::new_v4());
    }
    assert!(
        mesh.stores[0]
            .start_owner_preparation(&source, Uuid::new_v4(), &target)
            .is_err()
    );
    assert_eq!(mesh.count(0, "owner_preparation_plans"), 64);
    mesh.reopen(0);
    start(&mut mesh, 0, 1, id);
    let other = mesh.stores[2].identity.node.id().to_owned();
    assert!(
        mesh.stores[0]
            .start_owner_preparation(&source, id, &other)
            .is_err()
    );
    assert_eq!(mesh.count(0, "owner_preparation_plans"), 64);
    mesh.stores[0].require_provisioning_owner().unwrap();
}