fips-core 0.3.1

Reusable FIPS mesh, endpoint, transport, and protocol library
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
//! Spanning Tree Announce send/receive logic.
//!
//! Handles building, sending, and receiving TreeAnnounce messages,
//! including periodic root refresh and rate-limited propagation.

use std::collections::HashMap;

use crate::NodeAddr;
use crate::protocol::TreeAnnounce;

use super::{Node, NodeError};
use tracing::{debug, info, trace, warn};

impl Node {
    /// Build a TreeAnnounce from our current tree state.
    fn build_tree_announce(&self) -> Result<TreeAnnounce, NodeError> {
        let decl = self.tree_state.my_declaration().clone();
        let ancestry = self.tree_state.my_coords().clone();

        if !decl.is_signed() {
            return Err(NodeError::SendFailed {
                node_addr: *self.identity.node_addr(),
                reason: "declaration not signed".into(),
            });
        }

        Ok(TreeAnnounce::new(decl, ancestry))
    }

    /// Send a TreeAnnounce to a specific peer, respecting rate limits.
    ///
    /// If the peer is rate-limited, the announce is marked pending for
    /// delivery on the next tick cycle.
    pub(super) async fn send_tree_announce_to_peer(
        &mut self,
        peer_addr: &NodeAddr,
    ) -> Result<(), NodeError> {
        let now_ms = Self::now_ms();

        // Check rate limit
        let peer = match self.peers.get_mut(peer_addr) {
            Some(p) => p,
            None => return Err(NodeError::PeerNotFound(*peer_addr)),
        };

        if !peer.can_send_tree_announce(now_ms) {
            peer.mark_tree_announce_pending();
            self.stats_mut().tree.rate_limited += 1;
            debug!(
                peer = %self.peer_display_name(peer_addr),
                "TreeAnnounce rate-limited, marking pending"
            );
            return Ok(());
        }

        // Build and encode
        let announce = self.build_tree_announce()?;
        let encoded = announce.encode().map_err(|e| NodeError::SendFailed {
            node_addr: *peer_addr,
            reason: format!("encode failed: {}", e),
        })?;

        // Send
        if let Err(e) = self.send_encrypted_link_message(peer_addr, &encoded).await {
            self.stats_mut().tree.send_failed += 1;
            return Err(e);
        }

        self.stats_mut().tree.sent += 1;

        // Record send time
        if let Some(peer) = self.peers.get_mut(peer_addr) {
            peer.record_tree_announce_sent(now_ms);
        }

        trace!(peer = %self.peer_display_name(peer_addr), "Sent TreeAnnounce");
        Ok(())
    }

    /// Send a TreeAnnounce to all active peers.
    pub(super) async fn send_tree_announce_to_all(&mut self) {
        let peer_addrs: Vec<NodeAddr> = self.peers.keys().copied().collect();

        for peer_addr in peer_addrs {
            if let Err(e) = self.send_tree_announce_to_peer(&peer_addr).await {
                debug!(
                    peer = %self.peer_display_name(&peer_addr),
                    error = %e,
                    "Failed to send TreeAnnounce"
                );
            }
        }
    }

    /// Send pending rate-limited tree announces whose cooldown has expired.
    pub(super) async fn send_pending_tree_announces(&mut self) {
        let now_ms = Self::now_ms();

        let ready: Vec<NodeAddr> = self
            .peers
            .iter()
            .filter(|(_, peer)| {
                peer.has_pending_tree_announce() && peer.can_send_tree_announce(now_ms)
            })
            .map(|(addr, _)| *addr)
            .collect();

        for peer_addr in ready {
            if let Err(e) = self.send_tree_announce_to_peer(&peer_addr).await {
                debug!(
                    peer = %self.peer_display_name(&peer_addr),
                    error = %e,
                    "Failed to send pending TreeAnnounce"
                );
            }
        }
    }

    /// Handle an inbound TreeAnnounce from an authenticated peer.
    ///
    /// 1. Decode the message
    /// 2. Verify the sender's declaration signature (pubkey from handshake)
    /// 3. Update the peer's tree state
    /// 4. Re-evaluate parent selection
    /// 5. If parent changed: increment seq, sign, recompute coords, announce to all
    pub(super) async fn handle_tree_announce(&mut self, from: &NodeAddr, payload: &[u8]) {
        self.stats_mut().tree.received += 1;

        let announce = match TreeAnnounce::decode(payload) {
            Ok(a) => a,
            Err(e) => {
                self.stats_mut().tree.decode_error += 1;
                debug!(from = %self.peer_display_name(from), error = %e, "Malformed TreeAnnounce");
                return;
            }
        };

        // Verify sender's declaration signature using their known pubkey
        let pubkey = match self.peers.get(from) {
            Some(peer) => peer.pubkey(),
            None => {
                self.stats_mut().tree.unknown_peer += 1;
                debug!(from = %self.peer_display_name(from), "TreeAnnounce from unknown peer");
                return;
            }
        };

        // The declaring node_addr in the announce should match the sender
        if announce.declaration.node_addr() != from {
            self.stats_mut().tree.addr_mismatch += 1;
            debug!(
                from = %self.peer_display_name(from),
                declared = %announce.declaration.node_addr(),
                "TreeAnnounce node_addr mismatch"
            );
            return;
        }

        if let Err(e) = announce.declaration.verify(&pubkey) {
            self.stats_mut().tree.sig_failed += 1;
            warn!(
                from = %self.peer_display_name(from),
                error = %e,
                "TreeAnnounce signature verification failed"
            );
            return;
        }

        if let Err(e) = announce.validate_semantics() {
            warn!(
                from = %self.peer_display_name(from),
                error = %e,
                "Rejected TreeAnnounce with invalid ancestry"
            );
            return;
        }

        let now_ms = Self::now_ms();

        // Update peer's tree state in ActivePeer
        if let Some(peer) = self.peers.get_mut(from) {
            peer.update_tree_position(
                announce.declaration.clone(),
                announce.ancestry.clone(),
                now_ms,
            );
        }

        // Update in TreeState
        let updated = self
            .tree_state
            .update_peer(announce.declaration.clone(), announce.ancestry.clone());

        if !updated {
            self.stats_mut().tree.stale += 1;
            debug!(from = %self.peer_display_name(from), "TreeAnnounce not fresher than existing, ignored");
            return;
        }

        self.stats_mut().tree.accepted += 1;

        debug!(
            from = %self.peer_display_name(from),
            seq = announce.declaration.sequence(),
            depth = announce.ancestry.depth(),
            root = %announce.ancestry.root_id(),
            "Processed TreeAnnounce"
        );

        // Bloom filter exchange initiation is handled at handshake completion
        // ([handshake.rs] mark_update_needed on the new peer) and on actual
        // content changes via [bloom.rs::handle_filter_announce]'s
        // `mark_changed_peers`. Marking the peer on every received TreeAnnounce
        // is redundant — and under high TreeAnnounce churn (rapid mid-chain
        // swap propagation) it amplifies bloom traffic proportionally with
        // the tree announce rate, even when the local outgoing filter
        // content has not changed.

        // Re-evaluate parent selection with current link costs.
        // Exclude peers without MMP RTT data — they are not yet eligible
        // as parent candidates (prevents oscillation from optimistic defaults).
        let peer_costs: HashMap<NodeAddr, f64> = self
            .peers
            .iter()
            .filter(|(_, peer)| peer.has_srtt())
            .map(|(addr, peer)| (*addr, peer.link_cost()))
            .collect();
        if let Some(new_parent) = self.tree_state.evaluate_parent(&peer_costs) {
            let new_seq = self.tree_state.my_declaration().sequence() + 1;
            let timestamp = crate::time::now_secs();

            let flap_dampened = self.tree_state.set_parent(new_parent, new_seq, timestamp);
            // recompute_coords may demote to self_root if the new path would be
            // invalid; sign AFTER recompute so the signature covers the final
            // declaration.
            self.tree_state.recompute_coords();
            if let Err(e) = self.tree_state.sign_declaration(&self.identity) {
                warn!(error = %e, "Failed to sign declaration after parent switch");
                return;
            }
            self.coord_cache.clear();
            self.reset_discovery_backoff();

            self.stats_mut().tree.parent_switched += 1;
            self.stats_mut().tree.parent_switches += 1;

            info!(
                new_parent = %self.peer_display_name(&new_parent),
                new_seq = new_seq,
                new_root = %self.tree_state.root(),
                depth = self.tree_state.my_coords().depth(),
                "Parent switched, flushed coord cache, announcing to all peers"
            );
            if flap_dampened {
                self.stats_mut().tree.flap_dampened += 1;
                warn!("Flap dampening engaged: excessive parent switches detected");
            }

            self.send_tree_announce_to_all().await;

            // Tree structure changed — trigger bloom filter exchange with all peers
            let all_peers: Vec<NodeAddr> = self.peers.keys().copied().collect();
            self.bloom_state.mark_all_updates_needed(all_peers);
        } else if !self.tree_state.is_root() && self.tree_state.should_be_root() {
            // Self is the smallest visible NodeAddr — promote to root rather
            // than continuing to advertise a stale ancestry rooted elsewhere.
            self.tree_state.become_root();
            if let Err(e) = self.tree_state.sign_declaration(&self.identity) {
                warn!(error = %e, "Failed to sign self-root declaration");
                return;
            }
            self.coord_cache.clear();
            self.reset_discovery_backoff();
            self.stats_mut().tree.parent_switched += 1;
            self.stats_mut().tree.parent_switches += 1;
            info!(
                new_root = %self.tree_state.root(),
                "Self-promoted to root: smallest visible NodeAddr"
            );
            self.send_tree_announce_to_all().await;
            let all_peers: Vec<NodeAddr> = self.peers.keys().copied().collect();
            self.bloom_state.mark_all_updates_needed(all_peers);
        } else if !self.tree_state.is_root()
            && *self.tree_state.my_declaration().parent_id() == *from
        {
            // Check for loop: if parent's ancestry now contains us, drop parent
            if let Some(parent_coords) = self.tree_state.peer_coords(from)
                && parent_coords.contains(self.identity.node_addr())
            {
                self.stats_mut().tree.loop_detected += 1;
                warn!(
                    parent = %self.peer_display_name(from),
                    "Parent ancestry contains us — loop detected, dropping parent"
                );
                let peer_costs: HashMap<NodeAddr, f64> = self
                    .peers
                    .iter()
                    .filter(|(_, peer)| peer.has_srtt())
                    .map(|(addr, peer)| (*addr, peer.link_cost()))
                    .collect();
                if self.tree_state.handle_parent_lost(&peer_costs) {
                    if let Err(e) = self.tree_state.sign_declaration(&self.identity) {
                        warn!(error = %e, "Failed to sign declaration after loop detection");
                        return;
                    }
                    self.coord_cache.clear();
                    self.reset_discovery_backoff();
                    self.send_tree_announce_to_all().await;
                }
                return;
            }

            // Our parent's ancestry changed but we're keeping the same parent.
            // Recompute our own coordinates (which derive from parent's ancestry)
            // and re-announce so downstream nodes stay current.
            //
            // Compare the full address path (not just root + depth) so that a
            // mid-chain ancestor swap also triggers re-announce. A reroute that
            // replaces an interior ancestor without changing the root or the
            // path length leaves both `root` and `depth` unchanged but still
            // alters our coords; downstream peers must learn the new path or
            // they will route into a phantom intermediate that no longer
            // exists on our parent's tree.
            let old_root = *self.tree_state.root();
            let old_depth = self.tree_state.my_coords().depth();
            let old_addrs: Vec<NodeAddr> =
                self.tree_state.my_coords().node_addrs().copied().collect();

            let new_seq = self.tree_state.my_declaration().sequence() + 1;
            let timestamp = crate::time::now_secs();

            self.tree_state.set_parent(*from, new_seq, timestamp);
            self.tree_state.recompute_coords();
            if let Err(e) = self.tree_state.sign_declaration(&self.identity) {
                warn!(error = %e, "Failed to sign declaration after parent update");
                return;
            }
            self.coord_cache.clear();
            self.reset_discovery_backoff();

            let new_addrs: Vec<NodeAddr> =
                self.tree_state.my_coords().node_addrs().copied().collect();

            if old_addrs != new_addrs {
                self.stats_mut().tree.ancestry_changed += 1;
                info!(
                    parent = %self.peer_display_name(from),
                    old_root = %old_root,
                    new_root = %self.tree_state.root(),
                    old_depth = old_depth,
                    new_depth = self.tree_state.my_coords().depth(),
                    "Parent ancestry changed, re-announcing"
                );
                self.send_tree_announce_to_all().await;

                // Bloom contents do not depend on path structure, only on
                // identity sets. Our parent_id is unchanged in this branch,
                // so our tree-peer set is unchanged and our outgoing filter
                // content is unchanged. Use mark_changed_peers, which
                // checks for actual content delta against last_sent_filters,
                // instead of mark_all_updates_needed, which marks
                // unconditionally regardless of whether content changed.
                let peer_addrs: Vec<NodeAddr> = self.peers.keys().copied().collect();
                let peer_filters = self.peer_inbound_filters();
                self.bloom_state
                    .mark_changed_peers(from, &peer_addrs, &peer_filters);
            }
        }
    }

    /// Periodic tree maintenance, called from the tick handler.
    ///
    /// Sends pending rate-limited announces and checks for periodic
    /// parent re-evaluation based on current MMP link costs.
    pub(super) async fn check_tree_state(&mut self) {
        self.send_pending_tree_announces().await;
        self.check_periodic_parent_reeval().await;
    }

    /// Periodic parent re-evaluation based on current MMP link costs.
    ///
    /// Self-paces using `last_parent_reeval` and the configured
    /// `reeval_interval_secs`. When a better parent is found, follows
    /// the same switch flow as TreeAnnounce-triggered switches.
    async fn check_periodic_parent_reeval(&mut self) {
        let interval_secs = self.config.node.tree.reeval_interval_secs;
        if interval_secs == 0 {
            return;
        }

        // Need at least 2 peers for a meaningful comparison
        if self.peers.len() < 2 {
            return;
        }

        let now = crate::time::instant_now();
        let interval = std::time::Duration::from_secs(interval_secs);

        if let Some(last) = self.last_parent_reeval
            && now.duration_since(last) < interval
        {
            return;
        }

        self.last_parent_reeval = Some(now);

        let peer_costs: HashMap<NodeAddr, f64> = self
            .peers
            .iter()
            .filter(|(_, peer)| peer.has_srtt())
            .map(|(addr, peer)| (*addr, peer.link_cost()))
            .collect();

        if let Some(new_parent) = self.tree_state.evaluate_parent(&peer_costs) {
            let new_seq = self.tree_state.my_declaration().sequence() + 1;
            let timestamp = crate::time::now_secs();

            let flap_dampened = self.tree_state.set_parent(new_parent, new_seq, timestamp);
            self.tree_state.recompute_coords();
            if let Err(e) = self.tree_state.sign_declaration(&self.identity) {
                warn!(error = %e, "Failed to sign declaration after periodic parent re-eval");
                return;
            }
            self.coord_cache.clear();
            self.reset_discovery_backoff();

            self.stats_mut().tree.parent_switched += 1;
            self.stats_mut().tree.parent_switches += 1;

            info!(
                new_parent = %self.peer_display_name(&new_parent),
                new_seq = new_seq,
                new_root = %self.tree_state.root(),
                depth = self.tree_state.my_coords().depth(),
                trigger = "periodic",
                "Parent switched via periodic cost re-evaluation"
            );
            if flap_dampened {
                self.stats_mut().tree.flap_dampened += 1;
                warn!("Flap dampening engaged: excessive parent switches detected");
            }

            self.send_tree_announce_to_all().await;

            let all_peers: Vec<NodeAddr> = self.peers.keys().copied().collect();
            self.bloom_state.mark_all_updates_needed(all_peers);
        } else if !self.tree_state.is_root() && self.tree_state.should_be_root() {
            self.tree_state.become_root();
            if let Err(e) = self.tree_state.sign_declaration(&self.identity) {
                warn!(error = %e, "Failed to sign self-root declaration in periodic reeval");
                return;
            }
            self.coord_cache.clear();
            self.reset_discovery_backoff();
            self.stats_mut().tree.parent_switched += 1;
            self.stats_mut().tree.parent_switches += 1;
            info!(
                new_root = %self.tree_state.root(),
                trigger = "periodic",
                "Self-promoted to root in periodic reeval: smallest visible NodeAddr"
            );
            self.send_tree_announce_to_all().await;
            let all_peers: Vec<NodeAddr> = self.peers.keys().copied().collect();
            self.bloom_state.mark_all_updates_needed(all_peers);
        }
    }

    /// Handle tree state cleanup when a peer is removed.
    ///
    /// Called from `remove_active_peer`. If the removed peer was our parent,
    /// attempts to find an alternative or becomes root.
    ///
    /// Returns `true` if our tree state changed (caller should announce).
    pub(super) fn handle_peer_removal_tree_cleanup(&mut self, node_addr: &NodeAddr) -> bool {
        let was_parent =
            !self.tree_state.is_root() && self.tree_state.my_declaration().parent_id() == node_addr;

        self.tree_state.remove_peer(node_addr);

        if was_parent {
            self.stats_mut().tree.parent_losses += 1;
            let peer_costs: HashMap<NodeAddr, f64> = self
                .peers
                .iter()
                .filter(|(_, peer)| peer.has_srtt())
                .map(|(addr, peer)| (*addr, peer.link_cost()))
                .collect();
            let changed = self.tree_state.handle_parent_lost(&peer_costs);
            if changed {
                // Re-sign the new declaration
                if let Err(e) = self.tree_state.sign_declaration(&self.identity) {
                    warn!(error = %e, "Failed to sign declaration after parent loss");
                }
                info!(
                    new_root = %self.tree_state.root(),
                    is_root = self.tree_state.is_root(),
                    "Tree state updated after parent loss"
                );
            }
            changed
        } else {
            false
        }
    }
}