subduction_ephemeral 0.1.0

Ephemeral (non-persisted) messaging for Subduction
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
//! Ephemeral message types and wire encoding.
//!
//! # Wire Layout
//!
//! Every ephemeral message on the wire begins with the `SUE\x00` schema
//! header, followed by a 4-byte big-endian total size, a 1-byte tag,
//! and the variant-specific payload:
//!
//! ```text
//! ╔════════╦═══════════╦═════╦═════════════════════════════╗
//! ║ Schema ║ TotalSize ║ Tag ║         Payload             ║
//! ║   4B   ║    4B     ║ 1B  ║         variable            ║
//! ╚════════╩═══════════╩═════╩═════════════════════════════╝
//! ```
//!
//! Tags are local to the `SUE` schema — no coordination with `SUM`'s
//! tag space is needed.

use alloc::vec::Vec;

use sedimentree_core::{
    codec::{
        decode::Decode,
        encode::Encode,
        error::{Bijou64Error, DecodeError, InvalidEnumTag, InvalidSchema, SizeMismatch},
    },
    id::SedimentreeId,
};

/// Schema header for [`EphemeralMessage`] envelope: **SU**bduction **E**phemeral v0.
pub const EPHEMERAL_SCHEMA: [u8; 4] = *b"SUE\x00";

/// Minimum envelope size: `schema(4) + total_size(4) + tag(1)`.
const ENVELOPE_HEADER_SIZE: usize = 4 + 4 + 1;

mod tags {
    pub(super) const EPHEMERAL: u8 = 0x00;
    pub(super) const SUBSCRIBE: u8 = 0x01;
    pub(super) const UNSUBSCRIBE: u8 = 0x02;
    pub(super) const SUBSCRIBE_REJECTED: u8 = 0x03;
}

mod min_sizes {
    // sed_id(32) + payload_len(bijou64 min=1)
    pub(super) const EPHEMERAL: usize = 32 + 1;
    // count(2)
    pub(super) const SUBSCRIBE: usize = 2;
    // count(2)
    pub(super) const UNSUBSCRIBE: usize = 2;
    // count(2)
    pub(super) const SUBSCRIBE_REJECTED: usize = 2;
}

/// Wire message types for the ephemeral protocol.
///
/// These messages are scoped to the `SUE\x00` schema and are fully
/// independent of [`SyncMessage`]. Each variant maps to a tag byte
/// local to this schema.
///
/// [`SyncMessage`]: subduction_core::connection::message::SyncMessage
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EphemeralMessage {
    /// An ephemeral payload for a specific sedimentree topic.
    ///
    /// Fire-and-forget delivery to all subscribers of `id` (minus sender).
    Ephemeral {
        /// The sedimentree topic.
        id: SedimentreeId,
        /// Opaque application payload.
        payload: Vec<u8>,
    },

    /// Subscribe to ephemeral messages for the given sedimentree IDs.
    Subscribe {
        /// The sedimentree IDs to subscribe to.
        ids: Vec<SedimentreeId>,
    },

    /// Unsubscribe from ephemeral messages for the given sedimentree IDs.
    Unsubscribe {
        /// The sedimentree IDs to unsubscribe from.
        ids: Vec<SedimentreeId>,
    },

    /// Notification that some subscribe requests were rejected.
    ///
    /// Contains only the rejected IDs. Accepted IDs are implied by omission.
    SubscribeRejected {
        /// The sedimentree IDs that were rejected.
        ids: Vec<SedimentreeId>,
    },
}

impl EphemeralMessage {
    /// Get the sedimentree ID associated with this message, if unique.
    ///
    /// Returns `None` for multi-ID messages (subscribe/unsubscribe/rejected).
    #[must_use]
    pub const fn sedimentree_id(&self) -> Option<SedimentreeId> {
        match self {
            Self::Ephemeral { id, .. } => Some(*id),
            Self::Subscribe { .. } | Self::Unsubscribe { .. } | Self::SubscribeRejected { .. } => {
                None
            }
        }
    }

    /// Payload byte count (after the tag byte, before the envelope header).
    const fn payload_size(&self) -> usize {
        match self {
            Self::Ephemeral { payload, .. } => {
                32 + bijou64::encoded_len(payload.len() as u64) + payload.len()
            }
            Self::Subscribe { ids }
            | Self::Unsubscribe { ids }
            | Self::SubscribeRejected { ids } => 2 + ids.len() * 32,
        }
    }
}

// ── Encode ──────────────────────────────────────────────────────────────

impl Encode for EphemeralMessage {
    fn encode(&self) -> Vec<u8> {
        encode_message(self)
    }

    fn encoded_size(&self) -> usize {
        ENVELOPE_HEADER_SIZE + self.payload_size()
    }
}

fn encode_message(msg: &EphemeralMessage) -> Vec<u8> {
    let payload_size = msg.payload_size();
    let total_size = ENVELOPE_HEADER_SIZE + payload_size;

    let mut buf = Vec::with_capacity(total_size);

    buf.extend_from_slice(&EPHEMERAL_SCHEMA);

    #[allow(clippy::cast_possible_truncation)]
    buf.extend_from_slice(&(total_size as u32).to_be_bytes());

    match msg {
        EphemeralMessage::Ephemeral { id, payload } => {
            buf.push(tags::EPHEMERAL);
            encode_ephemeral(&mut buf, id, payload);
        }
        EphemeralMessage::Subscribe { ids } => {
            buf.push(tags::SUBSCRIBE);
            encode_id_list(&mut buf, ids);
        }
        EphemeralMessage::Unsubscribe { ids } => {
            buf.push(tags::UNSUBSCRIBE);
            encode_id_list(&mut buf, ids);
        }
        EphemeralMessage::SubscribeRejected { ids } => {
            buf.push(tags::SUBSCRIBE_REJECTED);
            encode_id_list(&mut buf, ids);
        }
    }

    buf
}

fn encode_ephemeral(buf: &mut Vec<u8>, id: &SedimentreeId, payload: &[u8]) {
    buf.extend_from_slice(id.as_bytes());
    #[allow(clippy::cast_possible_truncation)]
    bijou64::encode(payload.len() as u64, buf);
    buf.extend_from_slice(payload);
}

fn encode_id_list(buf: &mut Vec<u8>, ids: &[SedimentreeId]) {
    #[allow(clippy::cast_possible_truncation)]
    buf.extend_from_slice(&(ids.len() as u16).to_be_bytes());
    for id in ids {
        buf.extend_from_slice(id.as_bytes());
    }
}

// ── Decode ──────────────────────────────────────────────────────────────

impl Decode for EphemeralMessage {
    const MIN_SIZE: usize = ENVELOPE_HEADER_SIZE;

    fn try_decode(buf: &[u8]) -> Result<Self, DecodeError> {
        decode_message(buf)
    }
}

#[allow(clippy::indexing_slicing)] // Length validated before access
fn decode_message(bytes: &[u8]) -> Result<EphemeralMessage, DecodeError> {
    if bytes.len() < ENVELOPE_HEADER_SIZE {
        return Err(DecodeError::MessageTooShort {
            type_name: "EphemeralMessage envelope",
            need: ENVELOPE_HEADER_SIZE,
            have: bytes.len(),
        });
    }

    let schema: [u8; 4] =
        bytes
            .get(0..4)
            .and_then(|s| s.try_into().ok())
            .ok_or(DecodeError::MessageTooShort {
                type_name: "EphemeralMessage schema",
                need: 4,
                have: bytes.len(),
            })?;
    if schema != EPHEMERAL_SCHEMA {
        return Err(InvalidSchema {
            expected: EPHEMERAL_SCHEMA,
            got: schema,
        }
        .into());
    }

    let total_size = u32::from_be_bytes(bytes.get(4..8).and_then(|s| s.try_into().ok()).ok_or(
        DecodeError::MessageTooShort {
            type_name: "EphemeralMessage total_size",
            need: 8,
            have: bytes.len(),
        },
    )?) as usize;
    if bytes.len() != total_size {
        return Err(SizeMismatch {
            declared: total_size,
            actual: bytes.len(),
        }
        .into());
    }

    let tag = *bytes.get(8).ok_or(DecodeError::MessageTooShort {
        type_name: "EphemeralMessage tag",
        need: 9,
        have: bytes.len(),
    })?;
    let payload = bytes
        .get(ENVELOPE_HEADER_SIZE..)
        .ok_or(DecodeError::MessageTooShort {
            type_name: "EphemeralMessage payload",
            need: ENVELOPE_HEADER_SIZE,
            have: bytes.len(),
        })?;

    let (min_payload_size, type_name) = match tag {
        tags::EPHEMERAL => (min_sizes::EPHEMERAL, "Ephemeral"),
        tags::SUBSCRIBE => (min_sizes::SUBSCRIBE, "EphemeralSubscribe"),
        tags::UNSUBSCRIBE => (min_sizes::UNSUBSCRIBE, "EphemeralUnsubscribe"),
        tags::SUBSCRIBE_REJECTED => (min_sizes::SUBSCRIBE_REJECTED, "EphemeralSubscribeRejected"),
        _ => {
            return Err(InvalidEnumTag {
                tag,
                type_name: "EphemeralMessage",
            }
            .into());
        }
    };

    if payload.len() < min_payload_size {
        return Err(DecodeError::MessageTooShort {
            type_name,
            need: ENVELOPE_HEADER_SIZE + min_payload_size,
            have: bytes.len(),
        });
    }

    match tag {
        tags::EPHEMERAL => decode_ephemeral(payload),
        tags::SUBSCRIBE => decode_id_list(payload).map(|ids| EphemeralMessage::Subscribe { ids }),
        tags::UNSUBSCRIBE => {
            decode_id_list(payload).map(|ids| EphemeralMessage::Unsubscribe { ids })
        }
        tags::SUBSCRIBE_REJECTED => {
            decode_id_list(payload).map(|ids| EphemeralMessage::SubscribeRejected { ids })
        }
        _ => unreachable!("tag validated above"),
    }
}

fn decode_ephemeral(payload: &[u8]) -> Result<EphemeralMessage, DecodeError> {
    let mut offset = 0;

    let id = SedimentreeId::new(read_array::<32>(payload, &mut offset)?);
    let payload_len = read_bijou64_as_usize(payload, &mut offset)?;

    let data = payload
        .get(offset..offset + payload_len)
        .ok_or(DecodeError::MessageTooShort {
            type_name: "Ephemeral payload data",
            need: offset + payload_len,
            have: payload.len(),
        })?
        .to_vec();

    Ok(EphemeralMessage::Ephemeral { id, payload: data })
}

fn decode_id_list(payload: &[u8]) -> Result<Vec<SedimentreeId>, DecodeError> {
    let mut offset = 0;

    let count = read_u16(payload, &mut offset)? as usize;

    let mut ids = Vec::with_capacity(count);
    for _ in 0..count {
        ids.push(SedimentreeId::new(read_array::<32>(payload, &mut offset)?));
    }

    Ok(ids)
}

// ── Decode helpers ──────────────────────────────────────────────────────

fn read_u16(buf: &[u8], offset: &mut usize) -> Result<u16, DecodeError> {
    let bytes: [u8; 2] = buf
        .get(*offset..*offset + 2)
        .and_then(|s| s.try_into().ok())
        .ok_or(DecodeError::MessageTooShort {
            type_name: "u16",
            need: *offset + 2,
            have: buf.len(),
        })?;
    *offset += 2;
    Ok(u16::from_be_bytes(bytes))
}

fn read_array<const N: usize>(buf: &[u8], offset: &mut usize) -> Result<[u8; N], DecodeError> {
    let bytes: [u8; N] = buf
        .get(*offset..*offset + N)
        .and_then(|s| s.try_into().ok())
        .ok_or(DecodeError::MessageTooShort {
            type_name: "array",
            need: *offset + N,
            have: buf.len(),
        })?;
    *offset += N;
    Ok(bytes)
}

fn read_bijou64_as_usize(buf: &[u8], offset: &mut usize) -> Result<usize, DecodeError> {
    let remaining = buf.get(*offset..).ok_or(DecodeError::MessageTooShort {
        type_name: "bijou64",
        need: *offset + 1,
        have: buf.len(),
    })?;
    let (value, consumed) = bijou64::decode(remaining).map_err(|kind| Bijou64Error {
        offset: *offset,
        kind,
    })?;
    *offset += consumed;
    #[allow(clippy::cast_possible_truncation)]
    Ok(value as usize)
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
    use super::*;

    #[test]
    fn ephemeral_roundtrip() {
        let id = SedimentreeId::new([0xAB; 32]);
        let payload = vec![1, 2, 3, 4, 5];
        let msg = EphemeralMessage::Ephemeral {
            id,
            payload: payload.clone(),
        };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn subscribe_roundtrip() {
        let ids = vec![
            SedimentreeId::new([0x01; 32]),
            SedimentreeId::new([0x02; 32]),
        ];
        let msg = EphemeralMessage::Subscribe { ids: ids.clone() };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn unsubscribe_roundtrip() {
        let ids = vec![SedimentreeId::new([0xFF; 32])];
        let msg = EphemeralMessage::Unsubscribe { ids };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn subscribe_rejected_roundtrip() {
        let ids = vec![SedimentreeId::new([0x42; 32])];
        let msg = EphemeralMessage::SubscribeRejected { ids };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn empty_payload_roundtrip() {
        let msg = EphemeralMessage::Ephemeral {
            id: SedimentreeId::new([0x00; 32]),
            payload: vec![],
        };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn empty_subscribe_roundtrip() {
        let msg = EphemeralMessage::Subscribe { ids: vec![] };

        let encoded = msg.encode();
        let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");

        assert_eq!(decoded, msg);
    }

    #[test]
    fn wrong_schema_rejected() {
        let msg = EphemeralMessage::Subscribe { ids: vec![] };
        let mut encoded = msg.encode();
        // Tamper the schema: SUE -> SUM
        encoded[2] = b'M';

        let err = EphemeralMessage::try_decode(&encoded).unwrap_err();
        assert!(
            matches!(err, DecodeError::InvalidSchema(_)),
            "expected InvalidSchema, got {err:?}"
        );
    }

    #[test]
    fn invalid_tag_rejected() {
        let msg = EphemeralMessage::Subscribe { ids: vec![] };
        let mut encoded = msg.encode();
        // Tamper the tag byte (offset 8)
        encoded[8] = 0xFF;

        let err = EphemeralMessage::try_decode(&encoded).unwrap_err();
        assert!(
            matches!(err, DecodeError::InvalidEnumTag(_)),
            "expected InvalidEnumTag, got {err:?}"
        );
    }

    #[test]
    fn schema_bytes_are_correct() {
        assert_eq!(&EPHEMERAL_SCHEMA, b"SUE\x00");
    }
}