Skip to main content

rns_core/transport/
tables.rs

1use alloc::vec::Vec;
2
3use super::types::InterfaceId;
4
5/// Entry in the path table, keyed by destination_hash.
6#[derive(Debug, Clone)]
7pub struct PathEntry {
8    pub timestamp: f64,
9    pub next_hop: [u8; 16],
10    pub hops: u8,
11    pub expires: f64,
12    pub random_blobs: Vec<[u8; 10]>,
13    pub receiving_interface: InterfaceId,
14    pub packet_hash: [u8; 32],
15    /// Original announce raw bytes (pre-hop-increment) for cache/retransmission.
16    pub announce_raw: Option<Vec<u8>>,
17}
18
19/// Entry in the announce table, keyed by destination_hash.
20#[derive(Debug, Clone)]
21pub struct AnnounceEntry {
22    pub timestamp: f64,
23    pub retransmit_timeout: f64,
24    pub retries: u8,
25    pub received_from: [u8; 16],
26    pub hops: u8,
27    pub packet_raw: Vec<u8>,
28    pub packet_data: Vec<u8>,
29    pub destination_hash: [u8; 16],
30    pub context_flag: u8,
31    pub local_rebroadcasts: u8,
32    pub block_rebroadcasts: bool,
33    pub attached_interface: Option<InterfaceId>,
34}
35
36/// Entry in the reverse table, keyed by truncated packet hash.
37#[derive(Debug, Clone)]
38pub struct ReverseEntry {
39    pub receiving_interface: InterfaceId,
40    pub outbound_interface: InterfaceId,
41    pub timestamp: f64,
42}
43
44/// Entry in the link table, keyed by link_id.
45#[derive(Debug, Clone)]
46pub struct LinkEntry {
47    pub timestamp: f64,
48    pub next_hop_transport_id: [u8; 16],
49    pub next_hop_interface: InterfaceId,
50    pub remaining_hops: u8,
51    pub received_interface: InterfaceId,
52    pub taken_hops: u8,
53    pub destination_hash: [u8; 16],
54    pub validated: bool,
55    pub proof_timeout: f64,
56}
57
58/// A pending discovery path request — stored when a path request arrives
59/// on a DISCOVER_PATHS_FOR interface for an unknown destination.
60#[derive(Debug, Clone)]
61pub struct DiscoveryPathRequest {
62    pub timestamp: f64,
63    pub requesting_interface: InterfaceId,
64}
65
66/// Entry in the announce rate table, keyed by destination_hash.
67#[derive(Debug, Clone)]
68pub struct RateEntry {
69    pub last: f64,
70    pub rate_violations: u32,
71    pub blocked_until: f64,
72    pub timestamps: Vec<f64>,
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_path_entry_creation() {
81        let entry = PathEntry {
82            timestamp: 1000.0,
83            next_hop: [0xAA; 16],
84            hops: 3,
85            expires: 2000.0,
86            random_blobs: Vec::new(),
87            receiving_interface: InterfaceId(1),
88            packet_hash: [0xBB; 32],
89            announce_raw: None,
90        };
91        assert_eq!(entry.hops, 3);
92        assert_eq!(entry.receiving_interface, InterfaceId(1));
93    }
94
95    #[test]
96    fn test_link_entry_creation() {
97        let entry = LinkEntry {
98            timestamp: 100.0,
99            next_hop_transport_id: [0x11; 16],
100            next_hop_interface: InterfaceId(2),
101            remaining_hops: 5,
102            received_interface: InterfaceId(3),
103            taken_hops: 2,
104            destination_hash: [0x22; 16],
105            validated: false,
106            proof_timeout: 200.0,
107        };
108        assert!(!entry.validated);
109        assert_eq!(entry.remaining_hops, 5);
110    }
111
112    #[test]
113    fn test_rate_entry_creation() {
114        let entry = RateEntry {
115            last: 50.0,
116            rate_violations: 0,
117            blocked_until: 0.0,
118            timestamps: Vec::new(),
119        };
120        assert_eq!(entry.rate_violations, 0);
121    }
122}