Skip to main content

igc_net/
topic.rs

1//! Well-known gossip topic IDs for the igc-net protocol.
2//!
3//! Topic IDs are fixed protocol constants.
4//!
5//! The values are precomputed from the canonical derivation strings and embedded
6//! directly so the implementation matches the spec's interoperability rules.
7
8/// Derivation string for the primary announcement topic.
9/// All compliant igc-net nodes MUST join this topic on startup.
10pub const ANNOUNCE_TOPIC_STR: &str = "igc-net/announce/v1";
11
12/// Derivation string for the analytics announcement topic.
13/// Nodes that publish or consume IGC_META_DOC analytics SHOULD join this topic.
14pub const ANALYTICS_TOPIC_STR: &str = "igc-net/analytics/v1";
15
16/// `BLAKE3("igc-net/announce/v1")` as a 32-byte array.
17pub const ANNOUNCE_TOPIC_ID: [u8; 32] = [
18    0x2f, 0x06, 0x56, 0x7e, 0x5d, 0x71, 0x48, 0xb5, 0x63, 0x49, 0xa7, 0x53, 0xf8, 0xb4, 0x07, 0xfb,
19    0xc3, 0x5b, 0x2f, 0x0d, 0x90, 0xff, 0x36, 0x6f, 0xf2, 0x67, 0x3d, 0x06, 0x02, 0x45, 0xdd, 0xa9,
20];
21
22/// `BLAKE3("igc-net/analytics/v1")` as a 32-byte array.
23pub const ANALYTICS_TOPIC_ID: [u8; 32] = [
24    0x4f, 0x29, 0x34, 0x5b, 0x69, 0x86, 0x88, 0x63, 0x51, 0xdb, 0xff, 0x97, 0xb4, 0x23, 0x1b, 0x7d,
25    0x9a, 0x60, 0x1a, 0x50, 0x98, 0xf1, 0x84, 0x80, 0xc2, 0xcb, 0x4d, 0x28, 0xfb, 0xe3, 0x09, 0x50,
26];
27
28/// `BLAKE3("igc-net/announce/v1")` as a 32-byte array.
29///
30/// This is the well-known gossip topic for flight announcements.
31/// The canonical hex value is defined in specs/specs_igc.md §2.
32pub fn announce_topic_id() -> [u8; 32] {
33    ANNOUNCE_TOPIC_ID
34}
35
36/// `BLAKE3("igc-net/analytics/v1")` as a 32-byte array.
37///
38/// Single well-known gossip topic for IGC_META_DOC analytics announcements.
39/// Defined in specs/specs_meta.md §11.1.
40pub fn analytics_topic_id() -> [u8; 32] {
41    ANALYTICS_TOPIC_ID
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn announce_topic_id_is_32_bytes() {
50        assert_eq!(announce_topic_id().len(), 32);
51    }
52
53    #[test]
54    fn analytics_topic_id_is_32_bytes() {
55        assert_eq!(analytics_topic_id().len(), 32);
56    }
57
58    #[test]
59    fn topic_ids_are_distinct() {
60        assert_ne!(announce_topic_id(), analytics_topic_id());
61    }
62
63    #[test]
64    fn announce_topic_id_matches_derivation() {
65        // Verify the runtime value matches a directly-computed BLAKE3.
66        let expected = *blake3::hash(ANNOUNCE_TOPIC_STR.as_bytes()).as_bytes();
67        assert_eq!(announce_topic_id(), expected);
68    }
69
70    #[test]
71    fn analytics_topic_id_matches_derivation() {
72        let expected = *blake3::hash(ANALYTICS_TOPIC_STR.as_bytes()).as_bytes();
73        assert_eq!(analytics_topic_id(), expected);
74    }
75
76    /// Snapshot test — verifies the announce topic ID against the value published
77    /// in specs/specs_igc.md §2.  If this fails, the spec and implementation have
78    /// diverged; update one of them.
79    #[test]
80    fn announce_topic_id_matches_spec_constant() {
81        let hex = hex::encode(announce_topic_id());
82        // Pre-computed: BLAKE3("igc-net/announce/v1")
83        assert_eq!(
84            hex,
85            "2f06567e5d7148b56349a753f8b407fbc35b2f0d90ff366ff2673d060245dda9"
86        );
87    }
88
89    #[test]
90    fn analytics_topic_id_matches_spec_constant() {
91        let hex = hex::encode(analytics_topic_id());
92        // Pre-computed: BLAKE3("igc-net/analytics/v1")
93        assert_eq!(
94            hex,
95            "4f29345b6986886351dbff97b4231b7d9a601a5098f18480c2cb4d28fbe30950"
96        );
97    }
98}