dig-message 0.2.0

The DIG Network generic base message protocol: the byte-deterministic Chia-Streamable envelope + InnerMessage, length-framed size-bounded framing, additive compression (raw + zstd, decompression-bomb guarded), and the extensible message-type registry that every directed (1:1 / group) e2e-sealed peer message rides.
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
//! The extensible message-type registry (SPEC §4) — the runtime seam through which every downstream
//! subsystem (dig-chat, dig-email, dig-video, peer-RPC, IPC) plugs its own message types into the one
//! base protocol WITHOUT dig-message depending on any of them.
//!
//! Three pieces make the seam:
//! - [`MessageBand`] + [`MessageType::band`] — the normative id allocation (each subsystem owns a
//!   256-wide band), so a reader can classify any id it sees.
//! - [`MessageKind`] — the compile-time contract a type declares: its reserved id + its typed,
//!   byte-deterministic [`Streamable`] payload.
//! - [`MessageRegistry`] — the runtime table that maps a [`MessageType`] to a decode-and-handle
//!   closure, populated additively.
//!
//! Forward compatibility (SPEC §4, the §5.1 additive-only spirit) is the load-bearing property: a
//! `message_type` the registry has never seen MUST fail CLEANLY — [`MessageError::UnsupportedType`] for
//! a request/stream shape (so the caller replies with an error), a silent drop for a one-shot/response
//! shape — and MUST NEVER panic. An old reader therefore keeps working when newer senders introduce
//! new types.

use std::collections::HashMap;

use chia_traits::Streamable;

use crate::envelope::{InteractionShape, MessageType};
use crate::error::{MessageError, Result};

/// The base of the core band (handshake / ack / error / keepalive) (SPEC §4).
pub const BAND_CORE: u32 = 0x0000_0000;
/// The base of the peer-RPC band (peer-to-peer request/response) (SPEC §4).
pub const BAND_PEER_RPC: u32 = 0x0000_0100;
/// The base of the dig-chat band (SPEC §4, #768).
pub const BAND_DIG_CHAT: u32 = 0x0000_0200;
/// The base of the dig-email band (SPEC §4, #794).
pub const BAND_DIG_EMAIL: u32 = 0x0000_0300;
/// The base of the dig-video-chat signaling band (SPEC §4, #795).
pub const BAND_DIG_VIDEO: u32 = 0x0000_0400;
/// The base of the presence / directed data-request band (SPEC §4).
pub const BAND_PRESENCE: u32 = 0x0000_0500;
/// The base of the dig-ipc-protocol band (authenticated local dig-app ↔ dig-node IPC) (SPEC §4).
pub const BAND_IPC: u32 = 0x0000_0600;
/// The base of the experimental / vendor band — never shipped as a canonical type (SPEC §4).
pub const BAND_EXPERIMENTAL: u32 = 0x1000_0000;

/// A subsystem's reserved id band (SPEC §4). Each named band is owned by one subsystem and allocated
/// additively within it; ids that fall in no allocated subsystem band classify as [`MessageBand::Reserved`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MessageBand {
    /// Handshake, ack, error, keepalive (`0x0000_0000..=0x0000_00FF`).
    Core,
    /// Peer-to-peer request/response (`0x0000_0100..=0x0000_01FF`).
    PeerRpc,
    /// dig-chat (`0x0000_0200..=0x0000_02FF`).
    DigChat,
    /// dig-email (`0x0000_0300..=0x0000_03FF`).
    DigEmail,
    /// dig-video-chat signaling (`0x0000_0400..=0x0000_04FF`).
    DigVideo,
    /// Presence / directed data-request (`0x0000_0500..=0x0000_05FF`).
    Presence,
    /// dig-ipc-protocol (`0x0000_0600..=0x0000_06FF`).
    Ipc,
    /// Experimental / vendor, never canonical (`>= 0x1000_0000`).
    Experimental,
    /// A currently-unallocated id, reserved for a future subsystem band (SPEC §4).
    Reserved,
}

impl MessageType {
    /// Core: connection handshake (SPEC §4 core band).
    pub const CORE_HANDSHAKE: MessageType = MessageType(BAND_CORE);
    /// Core: generic acknowledgement (SPEC §4 core band).
    pub const CORE_ACK: MessageType = MessageType(BAND_CORE + 1);
    /// Core: protocol-level error report (SPEC §4 core band).
    pub const CORE_ERROR: MessageType = MessageType(BAND_CORE + 2);
    /// Core: liveness keepalive (SPEC §4 core band).
    pub const CORE_KEEPALIVE: MessageType = MessageType(BAND_CORE + 3);

    /// Classify this id into its reserved [`MessageBand`] (SPEC §4). Total — every `u32` maps to a
    /// band, so a reader can always classify an id it does not otherwise recognize.
    #[must_use]
    pub fn band(self) -> MessageBand {
        match self.0 {
            0x0000_0000..=0x0000_00FF => MessageBand::Core,
            0x0000_0100..=0x0000_01FF => MessageBand::PeerRpc,
            0x0000_0200..=0x0000_02FF => MessageBand::DigChat,
            0x0000_0300..=0x0000_03FF => MessageBand::DigEmail,
            0x0000_0400..=0x0000_04FF => MessageBand::DigVideo,
            0x0000_0500..=0x0000_05FF => MessageBand::Presence,
            0x0000_0600..=0x0000_06FF => MessageBand::Ipc,
            0x1000_0000..=0xFFFF_FFFF => MessageBand::Experimental,
            _ => MessageBand::Reserved,
        }
    }
}

/// The compile-time contract a message type declares to plug into the registry (SPEC §4). A downstream
/// crate implements it for each of its payload types; dig-message never depends on that crate.
///
/// The [`Payload`](MessageKind::Payload) is a Chia-[`Streamable`] type so its bytes are byte-
/// deterministic across the Rust and wasm/JS targets (SPEC §1); [`TYPE_ID`](MessageKind::TYPE_ID) is
/// the reserved id from the owning subsystem's band ([`MessageBand`]).
pub trait MessageKind {
    /// The reserved [`MessageType`] id for this kind (from the subsystem's band, SPEC §4).
    const TYPE_ID: MessageType;

    /// The typed, byte-deterministic payload carried by this message type (SPEC §1, §4).
    type Payload: Streamable;
}

/// The outcome of dispatching a decoded payload (SPEC §4). Both variants are success — an unknown
/// one-shot dropping is the intended forward-compat behavior, not an error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dispatch {
    /// A registered handler decoded and processed the payload.
    Handled,
    /// The type was unknown and the shape was one-shot/response, so the message was silently dropped
    /// (SPEC §4). No handler ran and no error is surfaced.
    Dropped,
}

/// A registered decode-and-handle closure: it decodes the payload bytes into the kind's Streamable
/// payload and processes it, propagating any decode/handler failure as a [`MessageError`].
type Handler = Box<dyn Fn(&[u8]) -> Result<()> + Send + Sync>;

/// The runtime message-type table (SPEC §4). Maps a [`MessageType`] to its decode-and-handle closure,
/// populated additively by each subsystem at startup.
///
/// It is deliberately additive-only: re-registering an already-present id is refused with
/// [`MessageError::DuplicateType`] rather than silently overwriting, upholding the SPEC §4 rule that an
/// id, once assigned, is never renumbered or repurposed. Registering a NEW id never disturbs the
/// handlers already present.
#[derive(Default)]
pub struct MessageRegistry {
    handlers: HashMap<MessageType, Handler>,
}

impl MessageRegistry {
    /// An empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a [`MessageKind`] with the closure that processes its decoded payload.
    ///
    /// The stored handler decodes the on-wire bytes into `K::Payload` (Streamable) before invoking
    /// `handler`, so callers work with the typed payload, never raw bytes.
    ///
    /// # Errors
    /// [`MessageError::DuplicateType`] if the kind's [`MessageKind::TYPE_ID`] is already registered
    /// (SPEC §4 additive-only — never silently repurpose an id).
    pub fn register<K, F>(&mut self, handler: F) -> Result<()>
    where
        K: MessageKind,
        F: Fn(K::Payload) -> Result<()> + Send + Sync + 'static,
    {
        let type_id = K::TYPE_ID;
        if self.handlers.contains_key(&type_id) {
            return Err(MessageError::DuplicateType(type_id.0));
        }
        let handler: Handler = Box::new(move |bytes: &[u8]| {
            let payload =
                K::Payload::from_bytes(bytes).map_err(|e| MessageError::Codec(e.to_string()))?;
            handler(payload)
        });
        self.handlers.insert(type_id, handler);
        Ok(())
    }

    /// Whether a [`MessageType`] has a registered handler.
    #[must_use]
    pub fn contains(&self, message_type: MessageType) -> bool {
        self.handlers.contains_key(&message_type)
    }

    /// The number of registered types.
    #[must_use]
    pub fn len(&self) -> usize {
        self.handlers.len()
    }

    /// Whether no types are registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.handlers.is_empty()
    }

    /// Route a decoded payload to its registered handler, applying the SPEC §4 unknown-type rule.
    ///
    /// `payload` is the already-opened, decompressed type-payload bytes (the seal/decompression are
    /// WU2/§1.1 concerns upstream of dispatch). `shape` decides how an unknown type fails.
    ///
    /// # Errors
    /// - [`MessageError::UnsupportedType`] when the type is unregistered AND the shape is a request or
    ///   stream frame (so the caller can reply UNSUPPORTED_TYPE).
    /// - Whatever the registered handler returns (a decode failure or a handler-reported error).
    ///
    /// An unregistered type with a one-shot/response shape is NOT an error: it returns
    /// [`Dispatch::Dropped`] (silent drop). Dispatch NEVER panics on an unknown type.
    pub fn dispatch(
        &self,
        message_type: MessageType,
        shape: InteractionShape,
        payload: &[u8],
    ) -> Result<Dispatch> {
        match self.handlers.get(&message_type) {
            Some(handler) => handler(payload).map(|()| Dispatch::Handled),
            None => match shape {
                InteractionShape::Request | InteractionShape::StreamFrame => {
                    Err(MessageError::UnsupportedType(message_type.0))
                }
                InteractionShape::OneShot | InteractionShape::Response => Ok(Dispatch::Dropped),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chia_streamable_macro::Streamable;
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::sync::Arc;

    /// A minimal Streamable payload standing in for a real dig-chat text message in these tests.
    #[derive(Debug, Clone, PartialEq, Eq, Streamable)]
    struct ChatText {
        body: Vec<u8>,
    }

    /// A dig-chat text message kind, reserved in the dig-chat band (SPEC §4).
    struct ChatTextKind;
    impl MessageKind for ChatTextKind {
        const TYPE_ID: MessageType = MessageType(BAND_DIG_CHAT);
        type Payload = ChatText;
    }

    /// A second, distinct kind (peer-RPC ping) used to prove additive registration is non-disturbing.
    #[derive(Debug, Clone, PartialEq, Eq, Streamable)]
    struct Ping {
        nonce: u64,
    }
    struct PingKind;
    impl MessageKind for PingKind {
        const TYPE_ID: MessageType = MessageType(BAND_PEER_RPC);
        type Payload = Ping;
    }

    #[test]
    fn register_then_dispatch_decodes_and_routes_to_the_handler() {
        let seen = Arc::new(AtomicU32::new(0));
        let sink = Arc::clone(&seen);

        let mut registry = MessageRegistry::new();
        registry
            .register::<ChatTextKind, _>(move |msg: ChatText| {
                sink.store(msg.body.len() as u32, Ordering::SeqCst);
                Ok(())
            })
            .unwrap();

        let payload = ChatText {
            body: vec![1, 2, 3, 4, 5],
        }
        .to_bytes()
        .unwrap();

        let outcome = registry
            .dispatch(ChatTextKind::TYPE_ID, InteractionShape::Request, &payload)
            .unwrap();

        assert_eq!(outcome, Dispatch::Handled);
        assert_eq!(
            seen.load(Ordering::SeqCst),
            5,
            "handler saw the decoded payload"
        );
    }

    #[test]
    fn unknown_type_request_returns_unsupported_type() {
        let registry = MessageRegistry::new();
        let unknown = MessageType(BAND_DIG_EMAIL + 0x42);
        assert_eq!(
            registry
                .dispatch(unknown, InteractionShape::Request, &[])
                .unwrap_err(),
            MessageError::UnsupportedType(unknown.0)
        );
    }

    #[test]
    fn unknown_type_stream_frame_returns_unsupported_type() {
        let registry = MessageRegistry::new();
        let unknown = MessageType(BAND_DIG_VIDEO + 7);
        assert_eq!(
            registry
                .dispatch(unknown, InteractionShape::StreamFrame, &[])
                .unwrap_err(),
            MessageError::UnsupportedType(unknown.0)
        );
    }

    #[test]
    fn unknown_type_one_shot_is_silently_dropped_not_an_error() {
        let registry = MessageRegistry::new();
        let unknown = MessageType(BAND_EXPERIMENTAL);
        assert_eq!(
            registry
                .dispatch(unknown, InteractionShape::OneShot, &[])
                .unwrap(),
            Dispatch::Dropped
        );
    }

    #[test]
    fn unknown_type_response_is_silently_dropped() {
        let registry = MessageRegistry::new();
        let unknown = MessageType(BAND_PRESENCE + 1);
        assert_eq!(
            registry
                .dispatch(unknown, InteractionShape::Response, &[])
                .unwrap(),
            Dispatch::Dropped
        );
    }

    #[test]
    fn registering_a_new_type_never_disturbs_existing_registrations() {
        let mut registry = MessageRegistry::new();
        registry
            .register::<ChatTextKind, _>(|_: ChatText| Ok(()))
            .unwrap();
        assert!(registry.contains(ChatTextKind::TYPE_ID));

        // Additively add a second, unrelated type.
        registry.register::<PingKind, _>(|_: Ping| Ok(())).unwrap();

        assert_eq!(registry.len(), 2);
        assert!(
            registry.contains(ChatTextKind::TYPE_ID),
            "the first type is undisturbed"
        );
        assert!(registry.contains(PingKind::TYPE_ID));
    }

    #[test]
    fn re_registering_the_same_id_is_refused_additive_only() {
        let mut registry = MessageRegistry::new();
        registry
            .register::<ChatTextKind, _>(|_: ChatText| Ok(()))
            .unwrap();
        assert_eq!(
            registry
                .register::<ChatTextKind, _>(|_: ChatText| Ok(()))
                .unwrap_err(),
            MessageError::DuplicateType(ChatTextKind::TYPE_ID.0)
        );
        assert_eq!(
            registry.len(),
            1,
            "the failed re-registration left the table unchanged"
        );
    }

    #[test]
    fn a_handler_decode_failure_propagates_and_never_panics() {
        let mut registry = MessageRegistry::new();
        registry.register::<PingKind, _>(|_: Ping| Ok(())).unwrap();
        // A `Ping` is a u64 (8 bytes); a 3-byte frame cannot decode.
        let err = registry
            .dispatch(PingKind::TYPE_ID, InteractionShape::Request, &[1, 2, 3])
            .unwrap_err();
        assert!(matches!(err, MessageError::Codec(_)));
    }

    #[test]
    fn a_handler_reported_error_propagates() {
        let mut registry = MessageRegistry::new();
        registry
            .register::<PingKind, _>(|_: Ping| Err(MessageError::UnsupportedType(0)))
            .unwrap();
        let payload = Ping { nonce: 9 }.to_bytes().unwrap();
        assert!(registry
            .dispatch(PingKind::TYPE_ID, InteractionShape::OneShot, &payload)
            .is_err());
    }

    #[test]
    fn every_reserved_band_classifies_at_its_boundaries() {
        let cases = [
            (BAND_CORE, MessageBand::Core),
            (BAND_CORE + 0xFF, MessageBand::Core),
            (BAND_PEER_RPC, MessageBand::PeerRpc),
            (BAND_PEER_RPC + 0xFF, MessageBand::PeerRpc),
            (BAND_DIG_CHAT, MessageBand::DigChat),
            (BAND_DIG_CHAT + 0xFF, MessageBand::DigChat),
            (BAND_DIG_EMAIL, MessageBand::DigEmail),
            (BAND_DIG_VIDEO, MessageBand::DigVideo),
            (BAND_PRESENCE, MessageBand::Presence),
            (BAND_IPC, MessageBand::Ipc),
            (BAND_IPC + 0xFF, MessageBand::Ipc),
            (BAND_EXPERIMENTAL, MessageBand::Experimental),
            (0xFFFF_FFFF, MessageBand::Experimental),
        ];
        for (id, expected) in cases {
            assert_eq!(MessageType(id).band(), expected, "id {id:#010x}");
        }
    }

    #[test]
    fn unallocated_ids_classify_as_reserved() {
        // Just past the last named subsystem band (IPC ends at 0x06FF) and below experimental.
        for id in [0x0000_0700, 0x0000_1000, 0x00FF_FFFF, 0x0FFF_FFFF] {
            assert_eq!(
                MessageType(id).band(),
                MessageBand::Reserved,
                "id {id:#010x}"
            );
        }
    }

    #[test]
    fn named_core_type_constants_land_in_the_core_band() {
        for mt in [
            MessageType::CORE_HANDSHAKE,
            MessageType::CORE_ACK,
            MessageType::CORE_ERROR,
            MessageType::CORE_KEEPALIVE,
        ] {
            assert_eq!(mt.band(), MessageBand::Core);
        }
    }
}