gent-daemon 0.2.1

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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Owner-authorized configuration recovery and committed handoff journals.
//! Application messages never gain an expired-lease exemption.
use super::*;
use gent_crypto::{MAX_STATE_BYTES, OwnerKey, SignedConfigurationGrant};
pub(crate) mod handoff;
pub mod standby;

pub const MAX_FRAGMENT_BYTES: usize = 256 * 1024;
pub const MAX_PENDING_CONFIGURATION_BYTES: usize = 8 * 1024 * 1024;
const MAX_REASSEMBLY_BYTES: usize = 8 * 1024 * 1024;

/// Contains private identity metadata. Exchange through protected pairing only.
#[derive(Clone)]
pub struct ConfigurationInvitation {
    pub packet: Packet,
    pub grant: SignedConfigurationGrant,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Fragment {
    version: u8,
    revision: u64,
    digest: Digest,
    total: usize,
    offset: usize,
    data: String,
}

#[derive(Deserialize)]
#[serde(untagged)]
enum ConfigurationRecord {
    State(Fragment),
    Handoff(handoff::Record),
}

pub(super) struct Progress {
    pub(super) grant: SignedConfigurationGrant,
    revision: u64,
    hash: Digest,
    offset: usize,
}

#[derive(Clone)]
pub struct ConfigurationStream {
    pub group: Vec<u8>,
    pub outgoing: bool,
}

/// A replacement stream carries the next owner's activation. Older streams
/// finish their accepted epoch without racing that separately prepared route.
pub(crate) fn publication_limit(
    signed: &SignedConfigurationGrant,
    state: &VerifiedState,
) -> Option<u64> {
    let epoch =
        signed.grant.owner_transitions.len() + usize::from(signed.grant.owner_candidate.is_some());
    state
        .state()
        .owner_transitions
        .get(epoch)
        .map(|signed| signed.transition.base.revision)
}

impl OrderedEndpoint<'_> {
    /// Only established, still-authorized configuration streams are eligible.
    /// Expiry alone does not hide the recovery route; known revocation does.
    pub fn configuration_streams(&mut self, now: u64) -> Result<Vec<ConfigurationStream>> {
        self.inner.transact_mode(now, true, |session| {
            let mut query = session.db.prepare(
                "SELECT group_id FROM configuration_streams ORDER BY group_id LIMIT 4097",
            )?;
            let groups = query
                .query_map([], |r| r.get::<_, Vec<u8>>(0))?
                .collect::<rusqlite::Result<Vec<_>>>()?;
            ensure!(groups.len() <= 4096, "too many configuration streams");
            let mut streams = Vec::new();
            for group in groups {
                if let Ok(stream) = checked_stream(session, &group, Purpose::Configuration) {
                    if stream.writer == session.signer.id()
                        && !progress(session.db, &group)?
                            .grant
                            .can_publish(&session.state)?
                    {
                        continue;
                    }
                    streams.push(ConfigurationStream {
                        group,
                        outgoing: stream.writer == session.signer.id(),
                    });
                }
            }
            Ok(streams)
        })
    }

    pub fn authority_state(&self) -> Result<VerifiedState> {
        self.inner.authority_state()
    }

    /// Grants full configuration access to one trusted daemon. Both node keys
    /// and the exact initial checkpoint are signed by the separate owner key.
    pub fn create_configuration(
        &mut self,
        owner: &OwnerKey,
        request: Uuid,
        peer: (&str, &[u8]),
        now: u64,
    ) -> Result<ConfigurationInvitation> {
        self.inner.transact(now, |session| {
            ensure!(
                owner.public_key() == session.state.state().owner_key,
                "wrong owner authority"
            );
            let raw_exists: bool = session.db.query_row(
                "SELECT EXISTS(SELECT 1 FROM outbox WHERE id=?1)",
                [request.to_string()],
                |row| row.get(0),
            )?;
            let saved = outgoing_request(session.db, request)?;
            ensure!(
                !raw_exists || saved.is_some(),
                "cannot adopt a raw MLS operation"
            );
            if saved.is_none() {
                ensure!(
                    !crate::store::handoff::frozen(session.db, &session.state)?,
                    "owner handoff is issued; configuration grants are frozen"
                );
            }
            let welcome = session.create_group(request, &[peer])?;
            if let Some(packet) = saved {
                checked_stream(session, packet.group(), Purpose::Configuration)?;
                return Ok(ConfigurationInvitation {
                    grant: progress(session.db, packet.group())?.grant,
                    packet,
                });
            }
            let packet =
                session
                    .signer
                    .sign_stream_packet(&welcome.group_id, 0, [0; 32], &welcome.bytes)?;
            let grant = owner.authorize_configuration_stream(
                &session.state,
                packet.group(),
                packet.digest()?,
                (session.signer.id(), peer.0),
                now,
            )?;
            initialize_stream(
                session,
                &packet,
                session.signer.id(),
                session.signer.public_key(),
                roster_hash(&[session.signer.id(), peer.0])?,
            )?;
            initialize_configuration(session.db, &grant)?;
            save_outgoing(session.db, request, &packet)?;
            Ok(ConfigurationInvitation { packet, grant })
        })
    }

    pub fn join_configuration(
        &mut self,
        invitation: &ConfigurationInvitation,
        now: u64,
    ) -> Result<()> {
        let packet = &invitation.packet;
        let signed = &invitation.grant;
        self.inner.transact_mode(now, true, |session| {
            signed.verify(session.state.anchor())?;
            let grant = &signed.grant;
            ensure!(
                grant.reader == session.signer.id()
                    && grant.reader_key == session.signer.public_key(),
                "configuration grant belongs to another reader"
            );
            ensure!(
                packet.sequence() == 0
                    && packet.group() == grant.group
                    && packet.digest()? == grant.origin,
                "configuration grant does not match Welcome"
            );
            packet.verify(&grant.writer_key)?;
            if let Some(saved) = stream(session.db, packet.group())? {
                authorize(session, packet.group(), &saved)?;
                ensure!(
                    progress(session.db, packet.group())?.grant == *signed,
                    "conflicting configuration grant"
                );
                return Ok(());
            }
            signed.verify_initial(&session.state, now)?;
            let raw = Outgoing {
                id: Uuid::nil(),
                group_id: packet.group().into(),
                bytes: packet.ciphertext().into(),
            };
            session.join(&raw, &[&grant.writer, &grant.reader], Some(&grant.writer))?;
            initialize_stream(
                session,
                packet,
                &grant.writer,
                grant.writer_key,
                roster_hash(&[&grant.writer, &grant.reader])?,
            )?;
            initialize_configuration(session.db, signed)
        })
    }

    /// Publish the next bounded fragment of already accepted owner history.
    /// Applying a new local signed revision and its history is a separate owner
    /// transaction; this outbox then catches every configured peer up to it.
    pub fn next_configuration(
        &mut self,
        group: &[u8],
        request: Uuid,
        now: u64,
    ) -> Result<Option<Packet>> {
        ensure!(!request.is_nil(), "nil request ID");
        self.prepare_configuration(group, Some(request), now)
    }

    /// Daemon publication commits its allocated ID with the fragment and cursor.
    pub(crate) fn prepare_configuration(
        &mut self,
        group: &[u8],
        request: Option<Uuid>,
        now: u64,
    ) -> Result<Option<Packet>> {
        // Already accepted signed history must remain publishable after lease
        // expiry, including when its quota is preventing an owner renewal.
        // The recovery grant and current saved node/key membership still apply.
        self.inner.transact_mode(now, true, |session| {
            let saved = checked_stream(session, group, Purpose::Configuration)?;
            ensure!(saved.writer == session.signer.id(), "only the configuration writer can publish");
            if let Some(request) = request {
                let prior: Option<Vec<u8>> = session.db.query_row("SELECT group_id FROM configuration_requests WHERE id=?1", [request.to_string()], |row| row.get(0)).optional()?;
                if let Some(prior) = prior {
                    ensure!(prior == group, "conflicting configuration request ID");
                    return Ok(Some(outgoing_request(session.db, request)?.context("missing configuration packet")?));
                }
                ensure!(!requests::managed(request), "transport request is unallocated or retired; it cannot publish configuration");
            }
            let cursor = progress(session.db, group)?;
            ensure!(cursor.grant.can_publish(&session.state)?, "standby configuration candidate is not active");
            let revision = cursor.revision.checked_add(1).context("revision overflow")?;
            if publication_limit(&cursor.grant, &session.state).is_some_and(|last| revision > last) {
                return Ok(None);
            }
            let Some(record) = history(session.db, revision)? else { return Ok(None); };
            let checkpoint = record.verified_checkpoint(session.state.anchor())?;
            ensure!(record.state.previous == Some(cursor.hash), "configuration publication history fork");
            let wire = record.encode()?;
            ensure!(cursor.offset < wire.len(), "invalid configuration cursor");
            let end = (cursor.offset + MAX_FRAGMENT_BYTES).min(wire.len());
            let fragment = Fragment { version:1, revision, digest:Sha256::digest(&wire).into(), total:wire.len(), offset:cursor.offset, data:STANDARD.encode(&wire[cursor.offset..end]) };
            let body = serde_json::to_vec(&fragment)?;
            let request = match request {
                Some(request) => request,
                None => requests::allocate(session.db, group, &body, None)?,
            };
            let packet = publish_in(session, group, Purpose::Configuration, |session| session.send(group, request, &body))?;
            let (revision,hash,offset) = if end == wire.len() { (revision, checkpoint.state_hash,0) } else { (cursor.revision,cursor.hash,end) };
            session.db.execute("UPDATE configuration_streams SET revision=?1,state_hash=?2,offset=?3 WHERE group_id=?4", params![revision,hash.as_slice(),offset,group])?;
            session.db.execute("INSERT INTO configuration_requests VALUES(?1,?2)", params![request.to_string(),group])?;
            Ok(Some(packet))
        })
    }

    pub fn rotate_configuration(
        &mut self,
        group: &[u8],
        request: Uuid,
        now: u64,
    ) -> Result<Packet> {
        self.inner.transact(now, |session| {
            ensure!(
                progress(session.db, group)?
                    .grant
                    .can_publish(&session.state)?,
                "standby configuration candidate is not active"
            );
            publish_in(session, group, Purpose::Configuration, |session| {
                session.rotate(group, request)
            })
        })
    }

    pub fn stage_configuration(&mut self, packet: &Packet, now: u64) -> Result<Staged> {
        self.inner.transact_mode(now, true, |session| {
            stage_in(session, packet, Purpose::Configuration)
        })
    }
    pub fn drain_configuration(
        &mut self,
        group: &[u8],
        limit: usize,
        now: u64,
    ) -> Result<Vec<Received>> {
        self.drain_mode(group, limit, now, Purpose::Configuration)
    }
    pub fn configuration_status(&mut self, group: &[u8], now: u64) -> Result<StreamStatus> {
        self.status_mode(group, now, Purpose::Configuration)
    }
    pub fn configuration_outbox(
        &mut self,
        group: &[u8],
        after: Option<u64>,
        limit: usize,
        now: u64,
    ) -> Result<Vec<Packet>> {
        self.outbox_mode(group, after, limit, now, Purpose::Configuration)
    }
}

fn initialize_configuration(db: &Connection, signed: &SignedConfigurationGrant) -> Result<()> {
    let grant = &signed.grant;
    ensure!(
        db.query_row("SELECT COUNT(*) FROM configuration_streams", [], |r| r
            .get::<_, usize>(0))?
            < 4096,
        "configuration stream capacity reached"
    );
    db.execute(
        "UPDATE streams SET purpose='configuration' WHERE group_id=?1",
        [&grant.group],
    )?;
    db.execute(
        "INSERT INTO configuration_streams VALUES(?1,?2,?3,?4,0)",
        params![
            grant.group,
            signed.encode()?,
            grant.base.revision,
            grant.base.state_hash.as_slice()
        ],
    )?;
    Ok(())
}

pub(super) fn progress(db: &Connection, group: &[u8]) -> Result<Progress> {
    let (grant, revision, hash, offset): (Vec<u8>, u64, Digest, usize) = db.query_row(
        "SELECT grant,revision,state_hash,offset FROM configuration_streams WHERE group_id=?1",
        [group],
        |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
    )?;
    Ok(Progress {
        grant: SignedConfigurationGrant::decode(&grant)?,
        revision,
        hash,
        offset,
    })
}

pub(super) fn authorize(session: &Session<'_>, group: &[u8], stream: &Stream) -> Result<()> {
    ensure!(
        stream.purpose == Purpose::Configuration.name(),
        "wrong stream purpose"
    );
    let signed = progress(session.db, group)?.grant;
    signed.verify(session.state.anchor())?;
    let grant = &signed.grant;
    ensure!(
        grant.group == group
            && grant.origin == stream.origin
            && grant.writer == stream.writer
            && grant.writer_key == stream.key,
        "configuration stream pin mismatch"
    );
    ensure!(
        (session.signer.id() == grant.writer && session.signer.public_key() == grant.writer_key)
            || (session.signer.id() == grant.reader
                && session.signer.public_key() == grant.reader_key),
        "configuration grant belongs to other endpoints"
    );
    authorize_saved_node(&session.state, &grant.writer, &grant.writer_key)?;
    authorize_saved_node(&session.state, &grant.reader, &grant.reader_key)?;
    Ok(())
}

fn history(db: &Connection, revision: u64) -> Result<Option<SignedState>> {
    let wire: Option<Vec<u8>> = db
        .query_row(
            "SELECT state FROM authority_history WHERE revision=?1",
            [revision],
            |row| row.get(0),
        )
        .optional()?;
    wire.map(|wire| SignedState::decode(&wire)).transpose()
}

pub(super) fn receive(
    session: &Session<'_>,
    group: &[u8],
    wire: &[u8],
    writer: &str,
) -> Result<Received> {
    session.receive_with(
        group,
        wire,
        Some(writer),
        |_, _, body| match serde_json::from_slice::<ConfigurationRecord>(&body)? {
            ConfigurationRecord::State(part) => receive_fragment(session, group, part),
            ConfigurationRecord::Handoff(record) => handoff::receive(session, group, record),
        },
    )
}

fn receive_fragment(session: &Session<'_>, group: &[u8], part: Fragment) -> Result<Received> {
    let cursor = progress(session.db, group)?;
    ensure!(
        part.version == 1
            && part.revision == cursor.revision + 1
            && part.total > 0
            && part.total <= MAX_STATE_BYTES
            && part.offset < part.total,
        "invalid configuration fragment"
    );
    let bytes = STANDARD.decode(&part.data)?;
    ensure!(
        bytes.len() == (part.total - part.offset).min(MAX_FRAGMENT_BYTES),
        "invalid configuration fragment length"
    );
    let saved: Option<(u64, Digest, usize, Vec<u8>)> = session
        .db
        .query_row(
            "SELECT revision,digest,total,bytes FROM configuration_fragments WHERE group_id=?1",
            [group],
            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
        )
        .optional()?;
    let mut wire = if let Some((revision, digest, total, wire)) = saved {
        ensure!(
            revision == part.revision
                && digest == part.digest
                && total == part.total
                && wire.len() == part.offset,
            "configuration fragment conflict or gap"
        );
        wire
    } else {
        ensure!(part.offset == 0, "missing configuration prefix");
        // Reserve the complete record up front. Already admitted partial records
        // must always have enough space to finish and release their reservation.
        if bytes.len() < part.total {
            let reserved: usize = session.db.query_row(
                "SELECT COALESCE(SUM(total),0) FROM configuration_fragments",
                [],
                |row| row.get(0),
            )?;
            ensure!(
                reserved + part.total <= MAX_REASSEMBLY_BYTES,
                "configuration reassembly limit reached"
            );
        }
        Vec::new()
    };
    wire.extend_from_slice(&bytes);
    if wire.len() < part.total {
        session.db.execute("INSERT INTO configuration_fragments VALUES(?1,?2,?3,?4,?5) ON CONFLICT(group_id) DO UPDATE SET bytes=excluded.bytes", params![group,part.revision,part.digest.as_slice(),part.total,wire])?;
        return Ok(Received::ConfigurationPart {
            revision: part.revision,
            complete: false,
        });
    }
    ensure!(
        Sha256::digest(&wire).as_slice() == part.digest,
        "configuration fragment digest mismatch"
    );
    let update = SignedState::decode(&wire)?;
    let checkpoint = update.verified_checkpoint(session.state.anchor())?;
    if cursor.grant.grant.owner_candidate.is_some() {
        let publication = VerifiedState::restore(
            session.state.anchor().clone(),
            checkpoint.clone(),
            update.clone(),
        )?;
        ensure!(
            cursor.grant.can_publish(&publication)?,
            "configuration update does not activate its standby candidate"
        );
    }
    ensure!(
        checkpoint.revision == part.revision && update.state.previous == Some(cursor.hash),
        "configuration history fork or gap"
    );
    if checkpoint.revision <= session.state.checkpoint().revision {
        let accepted = super::super::history::checkpoint(
            session.db,
            checkpoint.revision,
            session.state.anchor(),
        )?;
        ensure!(
            accepted == checkpoint,
            "conflicting accepted authority history"
        );
    } else {
        let mut next = session.state.clone();
        next.advance_history(std::slice::from_ref(&update))?;
        crate::store::handoff::admit_epochs(
            session.db,
            session.signer,
            &next,
            false,
            Some(&cursor.grant),
        )?;
        persist_authority(session.db, session.signer, &next, &[update])?;
    }
    session.db.execute(
        "UPDATE configuration_streams SET revision=?1,state_hash=?2 WHERE group_id=?3",
        params![checkpoint.revision, checkpoint.state_hash.as_slice(), group],
    )?;
    session.db.execute(
        "DELETE FROM configuration_fragments WHERE group_id=?1",
        [group],
    )?;
    Ok(Received::ConfigurationPart {
        revision: checkpoint.revision,
        complete: true,
    })
}