freenet_test_network/
network.rs

1use crate::{docker::DockerNatBackend, peer::TestPeer, Error, Result};
2use chrono::Utc;
3use freenet_stdlib::{
4    client_api::{
5        ClientRequest, ConnectedPeerInfo, HostResponse, NodeDiagnosticsConfig, NodeQuery,
6        QueryResponse, SystemMetrics, WebApi,
7    },
8    prelude::ContractKey,
9};
10use regex::Regex;
11use serde::{Deserialize, Serialize};
12use serde_json::json;
13use std::{
14    collections::{HashMap, HashSet},
15    fs,
16    path::{Path, PathBuf},
17    sync::LazyLock,
18    time::Duration,
19};
20
21/// Detailed connectivity status for a single peer
22#[derive(Debug, Clone)]
23pub struct PeerConnectivityStatus {
24    pub peer_id: String,
25    pub connections: Option<usize>,
26    pub error: Option<String>,
27}
28
29/// Detailed connectivity check result
30#[derive(Debug)]
31pub struct ConnectivityStatus {
32    pub total_peers: usize,
33    pub connected_peers: usize,
34    pub ratio: f64,
35    pub peer_status: Vec<PeerConnectivityStatus>,
36}
37
38/// A test network consisting of gateways and peer nodes
39pub struct TestNetwork {
40    pub(crate) gateways: Vec<TestPeer>,
41    pub(crate) peers: Vec<TestPeer>,
42    pub(crate) min_connectivity: f64,
43    pub(crate) run_root: PathBuf,
44    /// Docker backend for NAT simulation (if used)
45    pub(crate) docker_backend: Option<DockerNatBackend>,
46}
47
48impl TestNetwork {
49    /// Create a new network builder
50    pub fn builder() -> crate::builder::NetworkBuilder {
51        crate::builder::NetworkBuilder::new()
52    }
53
54    /// Get a gateway peer by index
55    pub fn gateway(&self, index: usize) -> &TestPeer {
56        &self.gateways[index]
57    }
58
59    /// Get a non-gateway peer by index
60    pub fn peer(&self, index: usize) -> &TestPeer {
61        &self.peers[index]
62    }
63
64    /// Get all gateway WebSocket URLs
65    pub fn gateway_ws_urls(&self) -> Vec<String> {
66        self.gateways.iter().map(|p| p.ws_url()).collect()
67    }
68
69    /// Get all peer WebSocket URLs
70    pub fn peer_ws_urls(&self) -> Vec<String> {
71        self.peers.iter().map(|p| p.ws_url()).collect()
72    }
73
74    /// Wait until the network is ready for use
75    ///
76    /// This checks that peers have formed connections and the network
77    /// is sufficiently connected for testing.
78    pub async fn wait_until_ready(&self) -> Result<()> {
79        self.wait_until_ready_with_timeout(Duration::from_secs(30))
80            .await
81    }
82
83    /// Wait until the network is ready with a custom timeout
84    pub async fn wait_until_ready_with_timeout(&self, timeout: Duration) -> Result<()> {
85        let start = std::time::Instant::now();
86        let mut last_progress_log = std::time::Instant::now();
87        let progress_interval = Duration::from_secs(10);
88
89        tracing::info!(
90            "Waiting for network connectivity (timeout: {}s, required: {}%)",
91            timeout.as_secs(),
92            (self.min_connectivity * 100.0) as u8
93        );
94
95        loop {
96            if start.elapsed() > timeout {
97                // Log final detailed status on failure
98                let status = self.check_connectivity_detailed().await;
99                let details = Self::format_connectivity_status(&status);
100                tracing::error!(
101                    "Connectivity timeout: {}/{} peers connected ({:.1}%) - {}",
102                    status.connected_peers,
103                    status.total_peers,
104                    status.ratio * 100.0,
105                    details
106                );
107                return Err(Error::ConnectivityFailed(format!(
108                    "Network did not reach {}% connectivity within {}s",
109                    (self.min_connectivity * 100.0) as u8,
110                    timeout.as_secs()
111                )));
112            }
113
114            // Check connectivity with detailed status
115            let status = self.check_connectivity_detailed().await;
116
117            if status.ratio >= self.min_connectivity {
118                tracing::info!("Network ready: {:.1}% connectivity", status.ratio * 100.0);
119                return Ok(());
120            }
121
122            // Log progress periodically (every 10 seconds)
123            if last_progress_log.elapsed() >= progress_interval {
124                let elapsed = start.elapsed().as_secs();
125                let details = Self::format_connectivity_status(&status);
126                tracing::info!(
127                    "[{}s] Connectivity: {}/{} ({:.0}%) - {}",
128                    elapsed,
129                    status.connected_peers,
130                    status.total_peers,
131                    status.ratio * 100.0,
132                    details
133                );
134                last_progress_log = std::time::Instant::now();
135            } else {
136                tracing::debug!(
137                    "Network connectivity: {}/{} ({:.1}%)",
138                    status.connected_peers,
139                    status.total_peers,
140                    status.ratio * 100.0
141                );
142            }
143
144            tokio::time::sleep(Duration::from_millis(500)).await;
145        }
146    }
147
148    /// Check current network connectivity with detailed status
149    pub async fn check_connectivity_detailed(&self) -> ConnectivityStatus {
150        let all_peers: Vec<_> = self.gateways.iter().chain(self.peers.iter()).collect();
151        let total = all_peers.len();
152
153        if total == 0 {
154            return ConnectivityStatus {
155                total_peers: 0,
156                connected_peers: 0,
157                ratio: 1.0,
158                peer_status: vec![],
159            };
160        }
161
162        let mut connected_count = 0;
163        let mut peer_status = Vec::with_capacity(total);
164
165        for peer in &all_peers {
166            match self.query_peer_connections(peer).await {
167                Ok(0) => {
168                    peer_status.push(PeerConnectivityStatus {
169                        peer_id: peer.id().to_string(),
170                        connections: Some(0),
171                        error: None,
172                    });
173                }
174                Ok(connections) => {
175                    connected_count += 1;
176                    peer_status.push(PeerConnectivityStatus {
177                        peer_id: peer.id().to_string(),
178                        connections: Some(connections),
179                        error: None,
180                    });
181                }
182                Err(e) => {
183                    peer_status.push(PeerConnectivityStatus {
184                        peer_id: peer.id().to_string(),
185                        connections: None,
186                        error: Some(e.to_string()),
187                    });
188                }
189            }
190        }
191
192        let ratio = connected_count as f64 / total as f64;
193        ConnectivityStatus {
194            total_peers: total,
195            connected_peers: connected_count,
196            ratio,
197            peer_status,
198        }
199    }
200
201    /// Check current network connectivity ratio (0.0 to 1.0)
202    async fn check_connectivity(&self) -> Result<f64> {
203        let status = self.check_connectivity_detailed().await;
204        Ok(status.ratio)
205    }
206
207    /// Format connectivity status for logging
208    fn format_connectivity_status(status: &ConnectivityStatus) -> String {
209        let mut parts: Vec<String> = status
210            .peer_status
211            .iter()
212            .map(|p| match (&p.connections, &p.error) {
213                (Some(c), _) => format!("{}:{}", p.peer_id, c),
214                (None, Some(_)) => format!("{}:err", p.peer_id),
215                (None, None) => format!("{}:?", p.peer_id),
216            })
217            .collect();
218        parts.sort();
219        parts.join(", ")
220    }
221
222    /// Query a single peer for its connection count
223    async fn query_peer_connections(&self, peer: &TestPeer) -> Result<usize> {
224        use tokio_tungstenite::connect_async;
225
226        let url = format!("{}?encodingProtocol=native", peer.ws_url());
227        let (ws_stream, _) =
228            tokio::time::timeout(std::time::Duration::from_secs(5), connect_async(&url))
229                .await
230                .map_err(|_| Error::ConnectivityFailed(format!("Timeout connecting to {}", url)))?
231                .map_err(|e| {
232                    Error::ConnectivityFailed(format!("Failed to connect to {}: {}", url, e))
233                })?;
234
235        let mut client = WebApi::start(ws_stream);
236
237        client
238            .send(ClientRequest::NodeQueries(NodeQuery::ConnectedPeers))
239            .await
240            .map_err(|e| Error::ConnectivityFailed(format!("Failed to send query: {}", e)))?;
241
242        let response = tokio::time::timeout(std::time::Duration::from_secs(5), client.recv())
243            .await
244            .map_err(|_| Error::ConnectivityFailed("Timeout waiting for response".into()))?;
245
246        let result = match response {
247            Ok(HostResponse::QueryResponse(QueryResponse::ConnectedPeers { peers })) => {
248                Ok(peers.len())
249            }
250            Ok(other) => Err(Error::ConnectivityFailed(format!(
251                "Unexpected response: {:?}",
252                other
253            ))),
254            Err(e) => Err(Error::ConnectivityFailed(format!("Query failed: {}", e))),
255        };
256
257        client.disconnect("connectivity probe").await;
258
259        result
260    }
261
262    /// Get the current network topology
263    pub async fn topology(&self) -> Result<NetworkTopology> {
264        // TODO: Query peers for their connections and build topology
265        Ok(NetworkTopology {
266            peers: vec![],
267            connections: vec![],
268        })
269    }
270
271    /// Export network information in JSON format for visualization tools
272    pub fn export_for_viz(&self) -> String {
273        let peers: Vec<_> = self
274            .gateways
275            .iter()
276            .chain(self.peers.iter())
277            .map(|p| {
278                serde_json::json!({
279                    "id": p.id(),
280                    "is_gateway": p.is_gateway(),
281                    "ws_port": p.ws_port,
282                    "network_port": p.network_port,
283                })
284            })
285            .collect();
286
287        serde_json::to_string_pretty(&serde_json::json!({
288            "peers": peers
289        }))
290        .unwrap_or_default()
291    }
292
293    /// Collect diagnostics from every peer, returning a snapshot that can be serialized to JSON
294    /// for offline analysis.
295    pub async fn collect_diagnostics(&self) -> Result<NetworkDiagnosticsSnapshot> {
296        let mut peers = Vec::with_capacity(self.gateways.len() + self.peers.len());
297        for peer in self.gateways.iter().chain(self.peers.iter()) {
298            peers.push(self.query_peer_diagnostics(peer).await);
299        }
300        peers.sort_by(|a, b| a.peer_id.cmp(&b.peer_id));
301        Ok(NetworkDiagnosticsSnapshot {
302            collected_at: Utc::now(),
303            peers,
304        })
305    }
306
307    async fn query_peer_diagnostics(&self, peer: &TestPeer) -> PeerDiagnosticsSnapshot {
308        use tokio_tungstenite::connect_async;
309
310        let mut snapshot = PeerDiagnosticsSnapshot::new(peer);
311        let url = format!("{}?encodingProtocol=native", peer.ws_url());
312        match tokio::time::timeout(std::time::Duration::from_secs(10), connect_async(&url)).await {
313            Ok(Ok((ws_stream, _))) => {
314                let mut client = WebApi::start(ws_stream);
315                let config = NodeDiagnosticsConfig {
316                    include_node_info: true,
317                    include_network_info: true,
318                    include_subscriptions: false,
319                    contract_keys: vec![],
320                    include_system_metrics: true,
321                    include_detailed_peer_info: true,
322                    include_subscriber_peer_ids: false,
323                };
324                if let Err(err) = client
325                    .send(ClientRequest::NodeQueries(NodeQuery::NodeDiagnostics {
326                        config,
327                    }))
328                    .await
329                {
330                    snapshot.error = Some(format!("failed to send diagnostics request: {err}"));
331                    return snapshot;
332                }
333                match tokio::time::timeout(std::time::Duration::from_secs(10), client.recv()).await
334                {
335                    Ok(Ok(HostResponse::QueryResponse(QueryResponse::NodeDiagnostics(
336                        response,
337                    )))) => {
338                        let node_info = response.node_info;
339                        let network_info = response.network_info;
340                        snapshot.peer_id = node_info
341                            .as_ref()
342                            .map(|info| info.peer_id.clone())
343                            .unwrap_or_else(|| peer.id().to_string());
344                        snapshot.is_gateway = node_info
345                            .as_ref()
346                            .map(|info| info.is_gateway)
347                            .unwrap_or_else(|| peer.is_gateway());
348                        snapshot.location =
349                            node_info.as_ref().and_then(|info| info.location.clone());
350                        snapshot.listening_address = node_info
351                            .as_ref()
352                            .and_then(|info| info.listening_address.clone());
353                        if let Some(info) = network_info {
354                            snapshot.active_connections = Some(info.active_connections);
355                            snapshot.connected_peer_ids = info
356                                .connected_peers
357                                .into_iter()
358                                .map(|(peer_id, _)| peer_id)
359                                .collect();
360                        }
361                        snapshot.connected_peers_detailed = response.connected_peers_detailed;
362                        snapshot.system_metrics = response.system_metrics;
363                    }
364                    Ok(Ok(other)) => {
365                        snapshot.error =
366                            Some(format!("unexpected diagnostics response: {:?}", other));
367                    }
368                    Ok(Err(err)) => {
369                        snapshot.error = Some(format!("diagnostics channel error: {err}"));
370                    }
371                    Err(_) => {
372                        snapshot.error = Some("timeout waiting for diagnostics response".into());
373                    }
374                }
375            }
376            Ok(Err(err)) => {
377                snapshot.error = Some(format!("failed to connect websocket: {err}"));
378            }
379            Err(_) => {
380                snapshot.error = Some("timeout establishing diagnostics websocket".into());
381            }
382        }
383        snapshot
384    }
385
386    /// Collect per-peer ring data (locations + adjacency) for visualization/debugging.
387    pub async fn ring_snapshot(&self) -> Result<Vec<RingPeerSnapshot>> {
388        self.collect_ring_snapshot(None).await
389    }
390
391    async fn collect_ring_snapshot(
392        &self,
393        contract_key: Option<&ContractKey>,
394    ) -> Result<Vec<RingPeerSnapshot>> {
395        let mut snapshots = Vec::with_capacity(self.gateways.len() + self.peers.len());
396        for peer in self.gateways.iter().chain(self.peers.iter()) {
397            snapshots.push(query_ring_snapshot(peer, contract_key).await?);
398        }
399        snapshots.sort_by(|a, b| a.id.cmp(&b.id));
400        Ok(snapshots)
401    }
402
403    /// Generate an interactive HTML ring visualization for the current network.
404    pub async fn write_ring_visualization<P: AsRef<Path>>(&self, output_path: P) -> Result<()> {
405        self.write_ring_visualization_internal(output_path, None)
406            .await
407    }
408
409    /// Generate an interactive HTML ring visualization for a specific contract.
410    pub async fn write_ring_visualization_for_contract<P: AsRef<Path>>(
411        &self,
412        output_path: P,
413        contract_id: &str,
414    ) -> Result<()> {
415        let contract_id_owned = contract_id.to_string();
416        let contract_key = ContractKey::from_id(contract_id_owned.clone()).map_err(|err| {
417            Error::Other(anyhow::anyhow!(
418                "invalid contract key {}: {}",
419                contract_id,
420                err
421            ))
422        })?;
423        self.write_ring_visualization_internal(
424            output_path,
425            Some((&contract_key, contract_id_owned.as_str())),
426        )
427        .await
428    }
429
430    async fn write_ring_visualization_internal<P: AsRef<Path>>(
431        &self,
432        output_path: P,
433        contract: Option<(&ContractKey, &str)>,
434    ) -> Result<()> {
435        let (snapshots, contract_viz) = if let Some((contract_key, contract_id)) = contract {
436            let snapshots = self.collect_ring_snapshot(Some(contract_key)).await?;
437            let caching_peers = snapshots
438                .iter()
439                .filter(|peer| {
440                    peer.contract
441                        .as_ref()
442                        .map(|state| state.stores_contract)
443                        .unwrap_or(false)
444                })
445                .map(|peer| peer.id.clone())
446                .collect::<Vec<_>>();
447            let contract_location = contract_location_from_key(contract_key);
448            let flow = self.collect_contract_flow(contract_id, &snapshots)?;
449            let viz = ContractVizData {
450                key: contract_id.to_string(),
451                location: contract_location,
452                caching_peers,
453                put: OperationPath {
454                    edges: flow.put_edges,
455                    completion_peer: flow.put_completion_peer,
456                },
457                update: OperationPath {
458                    edges: flow.update_edges,
459                    completion_peer: flow.update_completion_peer,
460                },
461                errors: flow.errors,
462            };
463            (snapshots, Some(viz))
464        } else {
465            (self.collect_ring_snapshot(None).await?, None)
466        };
467
468        let metrics = compute_ring_metrics(&snapshots);
469        let payload = json!({
470            "generated_at": Utc::now().to_rfc3339(),
471            "run_root": self.run_root.display().to_string(),
472            "nodes": snapshots,
473            "metrics": metrics,
474            "contract": contract_viz,
475        });
476        let data_json = serde_json::to_string(&payload).map_err(|e| Error::Other(e.into()))?;
477        let html = render_ring_template(&data_json);
478        let out_path = output_path.as_ref();
479        if let Some(parent) = out_path.parent() {
480            if !parent.exists() {
481                fs::create_dir_all(parent)?;
482            }
483        }
484        fs::write(out_path, html)?;
485        tracing::info!(path = %out_path.display(), "Wrote ring visualization");
486        Ok(())
487    }
488
489    fn collect_contract_flow(
490        &self,
491        contract_id: &str,
492        peers: &[RingPeerSnapshot],
493    ) -> Result<ContractFlowData> {
494        let mut data = ContractFlowData::default();
495        let logs = self.read_logs()?;
496        for entry in logs {
497            if !entry.message.contains(contract_id) {
498                continue;
499            }
500            if let Some(caps) = PUT_REQUEST_RE.captures(&entry.message) {
501                if &caps["key"] != contract_id {
502                    continue;
503                }
504                data.put_edges.push(ContractOperationEdge {
505                    from: caps["from"].to_string(),
506                    to: caps["to"].to_string(),
507                    timestamp: entry.timestamp.map(|ts| ts.to_rfc3339()),
508                    log_level: entry.level.clone(),
509                    log_source: Some(entry.peer_id.clone()),
510                    message: entry.message.clone(),
511                });
512                continue;
513            }
514            if let Some(caps) = PUT_COMPLETION_RE.captures(&entry.message) {
515                if &caps["key"] != contract_id {
516                    continue;
517                }
518                data.put_completion_peer = Some(caps["peer"].to_string());
519                continue;
520            }
521            if let Some(caps) = UPDATE_PROPAGATION_RE.captures(&entry.message) {
522                if &caps["contract"] != contract_id {
523                    continue;
524                }
525                let from = caps["from"].to_string();
526                let ts = entry.timestamp.map(|ts| ts.to_rfc3339());
527                let level = entry.level.clone();
528                let source = Some(entry.peer_id.clone());
529                let targets = caps["targets"].trim();
530                if !targets.is_empty() {
531                    for prefix in targets.split(',').filter(|s| !s.is_empty()) {
532                        if let Some(resolved) = resolve_peer_id(prefix, peers) {
533                            data.update_edges.push(ContractOperationEdge {
534                                from: from.clone(),
535                                to: resolved,
536                                timestamp: ts.clone(),
537                                log_level: level.clone(),
538                                log_source: source.clone(),
539                                message: entry.message.clone(),
540                            });
541                        }
542                    }
543                }
544                continue;
545            }
546            if let Some(caps) = UPDATE_NO_TARGETS_RE.captures(&entry.message) {
547                if &caps["contract"] != contract_id {
548                    continue;
549                }
550                data.errors.push(entry.message.clone());
551                continue;
552            }
553            if entry.message.contains("update will not propagate") {
554                data.errors.push(entry.message.clone());
555            }
556        }
557        Ok(data)
558    }
559
560    /// Dump logs from all peers, optionally filtered by a pattern
561    ///
562    /// If `filter` is Some, only logs containing the pattern (case-insensitive) are printed.
563    /// Logs are printed in chronological order with peer ID prefix.
564    ///
565    /// This is useful for debugging test failures - call it before assertions or in error handlers.
566    pub fn dump_logs(&self, filter: Option<&str>) {
567        match self.read_logs() {
568            Ok(mut entries) => {
569                // Sort by timestamp
570                entries.sort_by(|a, b| match (&a.timestamp, &b.timestamp) {
571                    (Some(ta), Some(tb)) => ta.cmp(tb),
572                    (Some(_), None) => std::cmp::Ordering::Less,
573                    (None, Some(_)) => std::cmp::Ordering::Greater,
574                    (None, None) => std::cmp::Ordering::Equal,
575                });
576
577                let filter_lower = filter.map(|f| f.to_lowercase());
578                let mut count = 0;
579
580                println!("--- Peer Logs {} ---",
581                    filter.map(|f| format!("(filtered: '{}')", f)).unwrap_or_default());
582
583                for entry in &entries {
584                    let matches = filter_lower
585                        .as_ref()
586                        .map(|f| entry.message.to_lowercase().contains(f))
587                        .unwrap_or(true);
588
589                    if matches {
590                        let ts = entry.timestamp_raw.as_deref().unwrap_or("?");
591                        let level = entry.level.as_deref().unwrap_or("?");
592                        println!("[{}] {} [{}] {}", entry.peer_id, ts, level, entry.message);
593                        count += 1;
594                    }
595                }
596
597                println!("--- End Peer Logs ({} entries) ---", count);
598            }
599            Err(e) => {
600                println!("--- Failed to read logs: {} ---", e);
601            }
602        }
603    }
604
605    /// Dump logs related to connection establishment and NAT traversal
606    ///
607    /// Filters for: hole punch, NAT, connect, acceptor, joiner, handshake
608    pub fn dump_connection_logs(&self) {
609        match self.read_logs() {
610            Ok(mut entries) => {
611                entries.sort_by(|a, b| match (&a.timestamp, &b.timestamp) {
612                    (Some(ta), Some(tb)) => ta.cmp(tb),
613                    (Some(_), None) => std::cmp::Ordering::Less,
614                    (None, Some(_)) => std::cmp::Ordering::Greater,
615                    (None, None) => std::cmp::Ordering::Equal,
616                });
617
618                let keywords = [
619                    "hole", "punch", "nat", "traverse", "acceptor", "joiner",
620                    "handshake", "outbound", "inbound", "connect:", "connection",
621                ];
622
623                println!("--- Connection/NAT Logs ---");
624                let mut count = 0;
625
626                for entry in &entries {
627                    let msg_lower = entry.message.to_lowercase();
628                    let matches = keywords.iter().any(|kw| msg_lower.contains(kw));
629
630                    if matches {
631                        let ts = entry.timestamp_raw.as_deref().unwrap_or("?");
632                        let level = entry.level.as_deref().unwrap_or("?");
633                        println!("[{}] {} [{}] {}", entry.peer_id, ts, level, entry.message);
634                        count += 1;
635                    }
636                }
637
638                println!("--- End Connection/NAT Logs ({} entries) ---", count);
639            }
640            Err(e) => {
641                println!("--- Failed to read logs: {} ---", e);
642            }
643        }
644    }
645
646    /// Dump iptables counters from all NAT routers (Docker NAT only)
647    ///
648    /// Prints NAT table rules and FORWARD chain counters for debugging.
649    pub async fn dump_iptables(&self) {
650        if let Some(backend) = &self.docker_backend {
651            match backend.dump_iptables_counters().await {
652                Ok(results) => {
653                    println!("--- NAT Router iptables ---");
654                    for (peer_idx, output) in results.iter() {
655                        println!("=== Peer {} NAT Router ===", peer_idx);
656                        println!("{}", output);
657                    }
658                    println!("--- End NAT Router iptables ---");
659                }
660                Err(e) => {
661                    println!("--- Failed to dump iptables: {} ---", e);
662                }
663            }
664        } else {
665            println!("--- No Docker NAT backend (iptables not available) ---");
666        }
667    }
668
669    /// Dump conntrack table from all NAT routers (Docker NAT only)
670    ///
671    /// Shows active UDP connection tracking entries for debugging NAT issues.
672    pub async fn dump_conntrack(&self) {
673        if let Some(backend) = &self.docker_backend {
674            match backend.dump_conntrack_table().await {
675                Ok(results) => {
676                    println!("--- NAT Router conntrack ---");
677                    for (peer_idx, output) in results.iter() {
678                        println!("=== Peer {} NAT Router ===", peer_idx);
679                        println!("{}", output);
680                    }
681                    println!("--- End NAT Router conntrack ---");
682                }
683                Err(e) => {
684                    println!("--- Failed to dump conntrack: {} ---", e);
685                }
686            }
687        } else {
688            println!("--- No Docker NAT backend (conntrack not available) ---");
689        }
690    }
691}
692
693impl TestNetwork {
694    pub(crate) fn new(
695        gateways: Vec<TestPeer>,
696        peers: Vec<TestPeer>,
697        min_connectivity: f64,
698        run_root: PathBuf,
699    ) -> Self {
700        Self {
701            gateways,
702            peers,
703            min_connectivity,
704            run_root,
705            docker_backend: None,
706        }
707    }
708
709    pub(crate) fn new_with_docker(
710        gateways: Vec<TestPeer>,
711        peers: Vec<TestPeer>,
712        min_connectivity: f64,
713        run_root: PathBuf,
714        docker_backend: Option<DockerNatBackend>,
715    ) -> Self {
716        Self {
717            gateways,
718            peers,
719            min_connectivity,
720            run_root,
721            docker_backend,
722        }
723    }
724
725    /// Directory containing all peer state/logs for this test network run.
726    pub fn run_root(&self) -> &std::path::Path {
727        &self.run_root
728    }
729}
730
731/// Snapshot describing diagnostics collected across the network at a moment in time.
732#[derive(Debug, Clone, Serialize, Deserialize)]
733pub struct NetworkDiagnosticsSnapshot {
734    pub collected_at: chrono::DateTime<Utc>,
735    pub peers: Vec<PeerDiagnosticsSnapshot>,
736}
737
738/// Diagnostic information for a single peer.
739#[derive(Debug, Clone, Serialize, Deserialize)]
740pub struct PeerDiagnosticsSnapshot {
741    pub peer_id: String,
742    pub is_gateway: bool,
743    pub ws_url: String,
744    pub location: Option<String>,
745    pub listening_address: Option<String>,
746    pub connected_peer_ids: Vec<String>,
747    pub connected_peers_detailed: Vec<ConnectedPeerInfo>,
748    pub active_connections: Option<usize>,
749    pub system_metrics: Option<SystemMetrics>,
750    pub error: Option<String>,
751}
752
753impl PeerDiagnosticsSnapshot {
754    fn new(peer: &TestPeer) -> Self {
755        Self {
756            peer_id: peer.id().to_string(),
757            is_gateway: peer.is_gateway(),
758            ws_url: peer.ws_url(),
759            location: None,
760            listening_address: None,
761            connected_peer_ids: Vec::new(),
762            connected_peers_detailed: Vec::new(),
763            active_connections: None,
764            system_metrics: None,
765            error: None,
766        }
767    }
768}
769
770/// Snapshot describing a peer's ring metadata and adjacency.
771#[derive(Debug, Clone, Serialize)]
772pub struct RingPeerSnapshot {
773    pub id: String,
774    pub is_gateway: bool,
775    pub ws_port: u16,
776    pub network_port: u16,
777    pub network_address: String,
778    #[serde(skip_serializing_if = "Option::is_none")]
779    pub location: Option<f64>,
780    pub connections: Vec<String>,
781    #[serde(skip_serializing_if = "Option::is_none")]
782    pub contract: Option<PeerContractStatus>,
783}
784
785/// Convert a diagnostics snapshot into the ring snapshot format used by the visualization.
786pub fn ring_nodes_from_diagnostics(snapshot: &NetworkDiagnosticsSnapshot) -> Vec<RingPeerSnapshot> {
787    snapshot
788        .peers
789        .iter()
790        .map(|peer| {
791            let location = peer
792                .location
793                .as_deref()
794                .and_then(|loc| loc.parse::<f64>().ok());
795            let (network_address, network_port) =
796                parse_listening_address(peer.listening_address.as_ref(), &peer.ws_url);
797            let ws_port = parse_ws_port(&peer.ws_url);
798            let mut connections = peer.connected_peer_ids.clone();
799            connections.retain(|id| id != &peer.peer_id);
800            connections.sort();
801            connections.dedup();
802
803            RingPeerSnapshot {
804                id: peer.peer_id.clone(),
805                is_gateway: peer.is_gateway,
806                ws_port,
807                network_port,
808                network_address,
809                location,
810                connections,
811                contract: None,
812            }
813        })
814        .collect()
815}
816
817/// Render a ring visualization from a saved diagnostics snapshot (e.g. a large soak run).
818pub fn write_ring_visualization_from_diagnostics<P: AsRef<Path>, Q: AsRef<Path>>(
819    snapshot: &NetworkDiagnosticsSnapshot,
820    run_root: P,
821    output_path: Q,
822) -> Result<()> {
823    let nodes = ring_nodes_from_diagnostics(snapshot);
824    let metrics = compute_ring_metrics(&nodes);
825    let payload = json!({
826        "generated_at": snapshot.collected_at.to_rfc3339(),
827        "run_root": run_root.as_ref().display().to_string(),
828        "nodes": nodes,
829        "metrics": metrics,
830    });
831    let data_json = serde_json::to_string(&payload).map_err(|e| Error::Other(e.into()))?;
832    let html = render_ring_template(&data_json);
833    let out_path = output_path.as_ref();
834    if let Some(parent) = out_path.parent() {
835        if !parent.exists() {
836            fs::create_dir_all(parent)?;
837        }
838    }
839    fs::write(out_path, html)?;
840    tracing::info!(path = %out_path.display(), "Wrote ring visualization from diagnostics");
841    Ok(())
842}
843
844/// Contract-specific status for a peer when a contract key is provided.
845#[derive(Debug, Clone, Serialize)]
846pub struct PeerContractStatus {
847    pub stores_contract: bool,
848    pub subscribed_locally: bool,
849    pub subscriber_peer_ids: Vec<String>,
850    pub subscriber_count: usize,
851}
852
853#[derive(Debug, Clone, Serialize)]
854pub struct ContractVizData {
855    pub key: String,
856    pub location: f64,
857    pub caching_peers: Vec<String>,
858    pub put: OperationPath,
859    pub update: OperationPath,
860    pub errors: Vec<String>,
861}
862
863#[derive(Debug, Clone, Serialize)]
864pub struct OperationPath {
865    pub edges: Vec<ContractOperationEdge>,
866    #[serde(skip_serializing_if = "Option::is_none")]
867    pub completion_peer: Option<String>,
868}
869
870#[derive(Debug, Clone, Serialize)]
871pub struct ContractOperationEdge {
872    pub from: String,
873    pub to: String,
874    #[serde(skip_serializing_if = "Option::is_none")]
875    pub timestamp: Option<String>,
876    #[serde(skip_serializing_if = "Option::is_none")]
877    pub log_level: Option<String>,
878    #[serde(skip_serializing_if = "Option::is_none")]
879    pub log_source: Option<String>,
880    pub message: String,
881}
882
883/// Aggregated metrics that help reason about small-world properties.
884#[derive(Debug, Clone, Serialize)]
885pub struct RingVizMetrics {
886    pub node_count: usize,
887    pub gateway_count: usize,
888    pub edge_count: usize,
889    pub average_degree: f64,
890    #[serde(skip_serializing_if = "Option::is_none")]
891    pub average_ring_distance: Option<f64>,
892    #[serde(skip_serializing_if = "Option::is_none")]
893    pub min_ring_distance: Option<f64>,
894    #[serde(skip_serializing_if = "Option::is_none")]
895    pub max_ring_distance: Option<f64>,
896    #[serde(skip_serializing_if = "Option::is_none")]
897    pub pct_edges_under_5pct: Option<f64>,
898    #[serde(skip_serializing_if = "Option::is_none")]
899    pub pct_edges_under_10pct: Option<f64>,
900    #[serde(skip_serializing_if = "Option::is_none")]
901    pub short_over_long_ratio: Option<f64>,
902    #[serde(skip_serializing_if = "Vec::is_empty")]
903    pub distance_histogram: Vec<RingDistanceBucket>,
904}
905
906#[derive(Debug, Clone, Serialize)]
907pub struct RingDistanceBucket {
908    pub upper_bound: f64,
909    pub count: usize,
910}
911
912/// Network topology information
913#[derive(Debug, Clone, Serialize, Deserialize)]
914pub struct NetworkTopology {
915    pub peers: Vec<PeerInfo>,
916    pub connections: Vec<Connection>,
917}
918
919/// Information about a peer in the network
920#[derive(Debug, Clone, Serialize, Deserialize)]
921pub struct PeerInfo {
922    pub id: String,
923    pub is_gateway: bool,
924    pub ws_port: u16,
925}
926
927/// A connection between two peers
928#[derive(Debug, Clone, Serialize, Deserialize)]
929pub struct Connection {
930    pub from: String,
931    pub to: String,
932}
933
934#[derive(Default)]
935struct ContractFlowData {
936    put_edges: Vec<ContractOperationEdge>,
937    put_completion_peer: Option<String>,
938    update_edges: Vec<ContractOperationEdge>,
939    update_completion_peer: Option<String>,
940    errors: Vec<String>,
941}
942
943async fn query_ring_snapshot(
944    peer: &TestPeer,
945    contract_key: Option<&ContractKey>,
946) -> Result<RingPeerSnapshot> {
947    use tokio_tungstenite::connect_async;
948
949    let url = format!("{}?encodingProtocol=native", peer.ws_url());
950    let (ws_stream, _) = tokio::time::timeout(Duration::from_secs(5), connect_async(&url))
951        .await
952        .map_err(|_| Error::ConnectivityFailed(format!("Timeout connecting to {}", peer.id())))?
953        .map_err(|e| {
954            Error::ConnectivityFailed(format!("Failed to connect to {}: {}", peer.id(), e))
955        })?;
956
957    let mut client = WebApi::start(ws_stream);
958    let diag_config = if let Some(key) = contract_key {
959        NodeDiagnosticsConfig {
960            include_node_info: true,
961            include_network_info: true,
962            include_subscriptions: true,
963            contract_keys: vec![key.clone()],
964            include_system_metrics: false,
965            include_detailed_peer_info: true,
966            include_subscriber_peer_ids: true,
967        }
968    } else {
969        NodeDiagnosticsConfig::basic_status()
970    };
971
972    client
973        .send(ClientRequest::NodeQueries(NodeQuery::NodeDiagnostics {
974            config: diag_config,
975        }))
976        .await
977        .map_err(|e| {
978            Error::ConnectivityFailed(format!(
979                "Failed to send diagnostics to {}: {}",
980                peer.id(),
981                e
982            ))
983        })?;
984
985    let response = tokio::time::timeout(Duration::from_secs(5), client.recv())
986        .await
987        .map_err(|_| {
988            Error::ConnectivityFailed(format!(
989                "Timeout waiting for diagnostics response from {}",
990                peer.id()
991            ))
992        })?;
993
994    let diag = match response {
995        Ok(HostResponse::QueryResponse(QueryResponse::NodeDiagnostics(diag))) => diag,
996        Ok(other) => {
997            client.disconnect("ring snapshot error").await;
998            return Err(Error::ConnectivityFailed(format!(
999                "Unexpected diagnostics response from {}: {:?}",
1000                peer.id(),
1001                other
1002            )));
1003        }
1004        Err(e) => {
1005            client.disconnect("ring snapshot error").await;
1006            return Err(Error::ConnectivityFailed(format!(
1007                "Diagnostics query failed for {}: {}",
1008                peer.id(),
1009                e
1010            )));
1011        }
1012    };
1013
1014    let node_info = diag.node_info.ok_or_else(|| {
1015        Error::ConnectivityFailed(format!("{} did not return node_info", peer.id()))
1016    })?;
1017
1018    let location = node_info
1019        .location
1020        .as_deref()
1021        .and_then(|value| value.parse::<f64>().ok());
1022
1023    let mut connections: Vec<String> = diag
1024        .connected_peers_detailed
1025        .into_iter()
1026        .map(|info| info.peer_id)
1027        .collect();
1028
1029    if connections.is_empty() {
1030        if let Some(network_info) = diag.network_info {
1031            connections = network_info
1032                .connected_peers
1033                .into_iter()
1034                .map(|(peer_id, _)| peer_id)
1035                .collect();
1036        }
1037    }
1038
1039    connections.retain(|conn| conn != &node_info.peer_id);
1040    connections.sort();
1041    connections.dedup();
1042
1043    let contract_status = contract_key.and_then(|key| {
1044        let (stores_contract, subscriber_count, subscriber_peer_ids) =
1045            if let Some(state) = diag.contract_states.get(key) {
1046                (
1047                    true,
1048                    state.subscribers as usize,
1049                    state.subscriber_peer_ids.clone(),
1050                )
1051            } else {
1052                (false, 0, Vec::new())
1053            };
1054        let subscribed_locally = diag
1055            .subscriptions
1056            .iter()
1057            .any(|sub| &sub.contract_key == key);
1058        if stores_contract || subscribed_locally {
1059            Some(PeerContractStatus {
1060                stores_contract,
1061                subscribed_locally,
1062                subscriber_peer_ids,
1063                subscriber_count,
1064            })
1065        } else {
1066            None
1067        }
1068    });
1069
1070    client.disconnect("ring snapshot complete").await;
1071
1072    Ok(RingPeerSnapshot {
1073        id: node_info.peer_id,
1074        is_gateway: node_info.is_gateway,
1075        ws_port: peer.ws_port,
1076        network_port: peer.network_port,
1077        network_address: peer.network_address.clone(),
1078        location,
1079        connections,
1080        contract: contract_status,
1081    })
1082}
1083
1084fn compute_ring_metrics(nodes: &[RingPeerSnapshot]) -> RingVizMetrics {
1085    let node_count = nodes.len();
1086    let gateway_count = nodes.iter().filter(|peer| peer.is_gateway).count();
1087    let mut total_degree = 0usize;
1088    let mut unique_edges: HashSet<(String, String)> = HashSet::new();
1089
1090    for node in nodes {
1091        total_degree += node.connections.len();
1092        for neighbor in &node.connections {
1093            if neighbor == &node.id {
1094                continue;
1095            }
1096            let edge = if node.id < *neighbor {
1097                (node.id.clone(), neighbor.clone())
1098            } else {
1099                (neighbor.clone(), node.id.clone())
1100            };
1101            unique_edges.insert(edge);
1102        }
1103    }
1104
1105    let average_degree = if node_count == 0 {
1106        0.0
1107    } else {
1108        total_degree as f64 / node_count as f64
1109    };
1110
1111    let mut location_lookup = HashMap::new();
1112    for node in nodes {
1113        if let Some(loc) = node.location {
1114            location_lookup.insert(node.id.clone(), loc);
1115        }
1116    }
1117
1118    let mut distances = Vec::new();
1119    for (a, b) in &unique_edges {
1120        if let (Some(loc_a), Some(loc_b)) = (location_lookup.get(a), location_lookup.get(b)) {
1121            let mut distance = (loc_a - loc_b).abs();
1122            if distance > 0.5 {
1123                distance = 1.0 - distance;
1124            }
1125            distances.push(distance);
1126        }
1127    }
1128
1129    let average_ring_distance = if distances.is_empty() {
1130        None
1131    } else {
1132        Some(distances.iter().sum::<f64>() / distances.len() as f64)
1133    };
1134    let min_ring_distance = distances.iter().cloned().reduce(f64::min);
1135    let max_ring_distance = distances.iter().cloned().reduce(f64::max);
1136    let pct_edges_under_5pct = calculate_percentage(&distances, 0.05);
1137    let pct_edges_under_10pct = calculate_percentage(&distances, 0.10);
1138
1139    let short_edges = distances.iter().filter(|d| **d <= 0.10).count();
1140    let long_edges = distances
1141        .iter()
1142        .filter(|d| **d > 0.10 && **d <= 0.50)
1143        .count();
1144    let short_over_long_ratio = if short_edges == 0 || long_edges == 0 {
1145        None
1146    } else {
1147        Some(short_edges as f64 / long_edges as f64)
1148    };
1149
1150    let mut distance_histogram = Vec::new();
1151    let bucket_bounds: Vec<f64> = (1..=5).map(|i| i as f64 * 0.10).collect(); // 0.1 buckets up to 0.5
1152    let mut cumulative = 0usize;
1153    for bound in bucket_bounds {
1154        let up_to_bound = distances.iter().filter(|d| **d <= bound).count();
1155        let count = up_to_bound.saturating_sub(cumulative);
1156        cumulative = up_to_bound;
1157        distance_histogram.push(RingDistanceBucket {
1158            upper_bound: bound,
1159            count,
1160        });
1161    }
1162
1163    RingVizMetrics {
1164        node_count,
1165        gateway_count,
1166        edge_count: unique_edges.len(),
1167        average_degree,
1168        average_ring_distance,
1169        min_ring_distance,
1170        max_ring_distance,
1171        pct_edges_under_5pct,
1172        pct_edges_under_10pct,
1173        short_over_long_ratio,
1174        distance_histogram,
1175    }
1176}
1177
1178fn calculate_percentage(distances: &[f64], threshold: f64) -> Option<f64> {
1179    if distances.is_empty() {
1180        return None;
1181    }
1182    let matching = distances.iter().filter(|value| **value < threshold).count();
1183    Some((matching as f64 / distances.len() as f64) * 100.0)
1184}
1185
1186fn parse_ws_port(ws_url: &str) -> u16 {
1187    ws_url
1188        .split("://")
1189        .nth(1)
1190        .and_then(|rest| rest.split('/').next())
1191        .and_then(|host_port| host_port.split(':').nth(1))
1192        .and_then(|port| port.parse().ok())
1193        .unwrap_or(0)
1194}
1195
1196fn parse_listening_address(addr: Option<&String>, ws_url: &str) -> (String, u16) {
1197    if let Some(addr) = addr {
1198        let mut parts = addr.split(':');
1199        let host = parts.next().unwrap_or("").to_string();
1200        let port = parts.next().and_then(|p| p.parse().ok()).unwrap_or(0);
1201        (host, port)
1202    } else {
1203        let host = ws_url
1204            .split("://")
1205            .nth(1)
1206            .and_then(|rest| rest.split('/').next())
1207            .and_then(|host_port| host_port.split(':').next())
1208            .unwrap_or_default()
1209            .to_string();
1210        (host, 0)
1211    }
1212}
1213
1214fn resolve_peer_id(prefix: &str, peers: &[RingPeerSnapshot]) -> Option<String> {
1215    let needle = prefix.trim().trim_matches(|c| c == '"' || c == '\'');
1216    peers
1217        .iter()
1218        .find(|peer| peer.id.starts_with(needle))
1219        .map(|peer| peer.id.clone())
1220}
1221
1222fn contract_location_from_key(key: &ContractKey) -> f64 {
1223    let mut value = 0.0;
1224    let mut divisor = 256.0;
1225    for byte in key.as_bytes() {
1226        value += f64::from(*byte) / divisor;
1227        divisor *= 256.0;
1228    }
1229    value.fract()
1230}
1231
1232static PUT_REQUEST_RE: LazyLock<Regex> = LazyLock::new(|| {
1233    Regex::new(r"Requesting put for contract (?P<key>\S+) from (?P<from>\S+) to (?P<to>\S+)")
1234        .expect("valid regex")
1235});
1236static PUT_COMPLETION_RE: LazyLock<Regex> = LazyLock::new(|| {
1237    Regex::new(r"Peer completed contract value put,.*key: (?P<key>\S+),.*this_peer: (?P<peer>\S+)")
1238        .expect("valid regex")
1239});
1240static UPDATE_PROPAGATION_RE: LazyLock<Regex> = LazyLock::new(|| {
1241    Regex::new(
1242        r"UPDATE_PROPAGATION: contract=(?P<contract>\S+) from=(?P<from>\S+) targets=(?P<targets>[^ ]*)\s+count=",
1243    )
1244    .expect("valid regex")
1245});
1246static UPDATE_NO_TARGETS_RE: LazyLock<Regex> = LazyLock::new(|| {
1247    Regex::new(r"UPDATE_PROPAGATION: contract=(?P<contract>\S+) from=(?P<from>\S+) NO_TARGETS")
1248        .expect("valid regex")
1249});
1250
1251const HTML_TEMPLATE: &str = r###"<!DOCTYPE html>
1252<html lang="en">
1253<head>
1254  <meta charset="UTF-8" />
1255  <title>Freenet Ring Topology</title>
1256  <style>
1257    :root {
1258      color-scheme: dark;
1259      font-family: "Inter", "Helvetica Neue", Arial, sans-serif;
1260    }
1261    body {
1262      background: #020617;
1263      color: #e2e8f0;
1264      margin: 0;
1265      padding: 32px;
1266      display: flex;
1267      justify-content: center;
1268      min-height: 100vh;
1269    }
1270    #container {
1271      max-width: 1000px;
1272      width: 100%;
1273      background: #0f172a;
1274      border-radius: 18px;
1275      padding: 28px 32px 40px;
1276      box-shadow: 0 40px 120px rgba(2, 6, 23, 0.85);
1277    }
1278    h1 {
1279      margin: 0 0 8px;
1280      font-size: 26px;
1281      letter-spacing: 0.4px;
1282      color: #f8fafc;
1283    }
1284    #meta {
1285      margin: 0 0 20px;
1286      color: #94a3b8;
1287      font-size: 14px;
1288    }
1289    canvas {
1290      width: 100%;
1291      max-width: 900px;
1292      height: auto;
1293      background: radial-gradient(circle, #020617 0%, #0f172a 70%, #020617 100%);
1294      border-radius: 16px;
1295      border: 1px solid rgba(148, 163, 184, 0.1);
1296      display: block;
1297      margin: 0 auto 24px;
1298    }
1299    #metrics {
1300      display: grid;
1301      grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
1302      gap: 12px;
1303      margin-bottom: 20px;
1304      font-size: 14px;
1305    }
1306    #metrics div {
1307      background: #1e293b;
1308      padding: 12px 14px;
1309      border-radius: 12px;
1310      border: 1px solid rgba(148, 163, 184, 0.15);
1311    }
1312    .legend {
1313      display: flex;
1314      flex-wrap: wrap;
1315      gap: 18px;
1316      margin-bottom: 24px;
1317      font-size: 14px;
1318      color: #cbd5f5;
1319    }
1320    .legend span {
1321      display: flex;
1322      align-items: center;
1323      gap: 6px;
1324    }
1325    .legend .line {
1326      width: 30px;
1327      height: 6px;
1328      border-radius: 3px;
1329      display: inline-block;
1330    }
1331    .legend .put-line {
1332      background: #22c55e;
1333    }
1334    .legend .update-line {
1335      background: #f59e0b;
1336    }
1337    .legend .contract-dot {
1338      background: #a855f7;
1339      box-shadow: 0 0 6px rgba(168, 85, 247, 0.7);
1340    }
1341    .legend .cached-dot {
1342      background: #38bdf8;
1343    }
1344    .legend .peer-dot {
1345      background: #64748b;
1346    }
1347    .legend .gateway-dot {
1348      background: #f97316;
1349    }
1350    .dot {
1351      width: 14px;
1352      height: 14px;
1353      border-radius: 50%;
1354      display: inline-block;
1355    }
1356    #contract-info {
1357      background: rgba(15, 23, 42, 0.7);
1358      border-radius: 12px;
1359      border: 1px solid rgba(148, 163, 184, 0.15);
1360      padding: 14px;
1361      margin-bottom: 20px;
1362      font-size: 13px;
1363      color: #cbd5f5;
1364    }
1365    #contract-info h2 {
1366      margin: 0 0 8px;
1367      font-size: 16px;
1368      color: #f8fafc;
1369    }
1370    #contract-info .op-block {
1371      margin-top: 10px;
1372    }
1373    #contract-info ol {
1374      padding-left: 20px;
1375      margin: 6px 0;
1376    }
1377    #contract-info .errors {
1378      margin-top: 10px;
1379      color: #f97316;
1380    }
1381    #peer-list {
1382      border-top: 1px solid rgba(148, 163, 184, 0.15);
1383      padding-top: 18px;
1384      display: grid;
1385      grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
1386      gap: 14px;
1387      font-size: 13px;
1388    }
1389    .peer {
1390      background: rgba(15, 23, 42, 0.75);
1391      padding: 12px 14px;
1392      border-radius: 12px;
1393      border: 1px solid rgba(148, 163, 184, 0.2);
1394    }
1395    .peer strong {
1396      display: block;
1397      font-size: 13px;
1398      color: #f1f5f9;
1399      margin-bottom: 6px;
1400      word-break: break-all;
1401    }
1402    .peer span {
1403      display: block;
1404      color: #94a3b8;
1405    }
1406    .chart-card {
1407      background: rgba(15, 23, 42, 0.75);
1408      padding: 12px 14px;
1409      border-radius: 12px;
1410      border: 1px solid rgba(148, 163, 184, 0.2);
1411      margin-top: 12px;
1412    }
1413    .chart-card h3 {
1414      margin: 0 0 8px;
1415      font-size: 15px;
1416      color: #e2e8f0;
1417    }
1418  </style>
1419  <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
1420</head>
1421<body>
1422  <div id="container">
1423    <h1>Freenet Ring Topology</h1>
1424    <p id="meta"></p>
1425    <canvas id="ring" width="900" height="900"></canvas>
1426    <div id="metrics"></div>
1427    <div class="chart-card" style="height: 280px;">
1428      <h3>Ring distance distribution</h3>
1429      <canvas id="histogram-chart" style="height: 220px;"></canvas>
1430    </div>
1431    <div class="legend">
1432      <span><span class="dot peer-dot"></span>Peer</span>
1433      <span><span class="dot gateway-dot"></span>Gateway</span>
1434      <span><span class="dot cached-dot"></span>Cached peer</span>
1435      <span><span class="dot contract-dot"></span>Contract</span>
1436      <span><span class="line put-line"></span>PUT path</span>
1437      <span><span class="line update-line"></span>UPDATE path</span>
1438      <span><span class="dot" style="background:#475569"></span>Connection</span>
1439    </div>
1440    <div id="contract-info"></div>
1441    <div id="peer-list"></div>
1442  </div>
1443  <script>
1444    const vizData = __DATA__;
1445    const contractData = vizData.contract ?? null;
1446    const metaEl = document.getElementById("meta");
1447    metaEl.textContent = vizData.nodes.length
1448      ? `Captured ${vizData.nodes.length} nodes · ${vizData.metrics.edge_count} edges · Generated ${vizData.generated_at} · Run root: ${vizData.run_root}`
1449      : "No peers reported diagnostics data.";
1450
1451    const metricsEl = document.getElementById("metrics");
1452    const fmtNumber = (value, digits = 2) => (typeof value === "number" ? value.toFixed(digits) : "n/a");
1453    const fmtPercent = (value, digits = 1) => (typeof value === "number" ? `${value.toFixed(digits)}%` : "n/a");
1454
1455    metricsEl.innerHTML = `
1456      <div><strong>Total nodes</strong><br/>${vizData.metrics.node_count} (gateways: ${vizData.metrics.gateway_count})</div>
1457      <div><strong>Edges</strong><br/>${vizData.metrics.edge_count}</div>
1458      <div><strong>Average degree</strong><br/>${fmtNumber(vizData.metrics.average_degree)}</div>
1459      <div><strong>Average ring distance</strong><br/>${fmtNumber(vizData.metrics.average_ring_distance, 3)}</div>
1460      <div><strong>Min / Max ring distance</strong><br/>${fmtNumber(vizData.metrics.min_ring_distance, 3)} / ${fmtNumber(vizData.metrics.max_ring_distance, 3)}</div>
1461      <div><strong>Edges &lt;5% / &lt;10%</strong><br/>${fmtPercent(vizData.metrics.pct_edges_under_5pct)} / ${fmtPercent(vizData.metrics.pct_edges_under_10pct)}</div>
1462      <div><strong>Short/long edge ratio (≤0.1 / 0.1–0.5)</strong><br/>${fmtNumber(vizData.metrics.short_over_long_ratio, 2)}</div>
1463    `;
1464
1465    const histogram = vizData.metrics.distance_histogram ?? [];
1466    const labels = histogram.map((b, idx) => {
1467      const lower = idx === 0 ? 0 : histogram[idx - 1].upper_bound;
1468      return `${lower.toFixed(1)}–${b.upper_bound.toFixed(1)}`;
1469    });
1470    const counts = histogram.map((b) => b.count);
1471
1472    if (histogram.length && window.Chart) {
1473      const ctx = document.getElementById("histogram-chart").getContext("2d");
1474      new Chart(ctx, {
1475        data: {
1476          labels,
1477          datasets: [
1478            {
1479              type: "bar",
1480              label: "Edges per bucket",
1481              data: counts,
1482              backgroundColor: "rgba(56, 189, 248, 0.75)",
1483              borderColor: "#38bdf8",
1484              borderWidth: 1,
1485            },
1486          ],
1487        },
1488        options: {
1489          responsive: true,
1490          maintainAspectRatio: false,
1491          animation: false,
1492          interaction: { mode: "index", intersect: false },
1493          plugins: {
1494            legend: { position: "bottom" },
1495            tooltip: {
1496              callbacks: {
1497                label: (ctx) => {
1498                  const label = ctx.dataset.label || "";
1499                  const value = ctx.parsed.y;
1500                  return ctx.dataset.type === "line"
1501                    ? `${label}: ${value.toFixed(1)}%`
1502                    : `${label}: ${value}`;
1503                },
1504              },
1505            },
1506          },
1507          scales: {
1508            x: { title: { display: true, text: "Ring distance (fraction of circumference)" } },
1509            y: {
1510              beginAtZero: true,
1511              title: { display: true, text: "Edge count" },
1512              grid: { color: "rgba(148,163,184,0.15)" },
1513            },
1514          },
1515        },
1516      });
1517    }
1518
1519    const peersEl = document.getElementById("peer-list");
1520    peersEl.innerHTML = vizData.nodes
1521      .map((node) => {
1522        const role = node.is_gateway ? "Gateway" : "Peer";
1523        const loc = typeof node.location === "number" ? node.location.toFixed(6) : "unknown";
1524        return `<div class="peer">
1525          <strong>${node.id}</strong>
1526          <span>${role}</span>
1527          <span>Location: ${loc}</span>
1528          <span>Degree: ${node.connections.length}</span>
1529        </div>`;
1530      })
1531      .join("");
1532
1533    const contractInfoEl = document.getElementById("contract-info");
1534    const canvas = document.getElementById("ring");
1535    const ctx = canvas.getContext("2d");
1536    const width = canvas.width;
1537    const height = canvas.height;
1538    const center = { x: width / 2, y: height / 2 };
1539    const radius = Math.min(width, height) * 0.37;
1540    const cachingPeers = new Set(contractData?.caching_peers ?? []);
1541    const putEdges = contractData?.put?.edges ?? [];
1542    const updateEdges = contractData?.update?.edges ?? [];
1543    const contractLocation = typeof (contractData?.location) === "number" ? contractData.location : null;
1544
1545    function shortId(id) {
1546      return id.slice(-6);
1547    }
1548
1549    function angleFromLocation(location) {
1550      return (location % 1) * Math.PI * 2;
1551    }
1552
1553    function polarToCartesian(angle, r = radius) {
1554      const theta = angle - Math.PI / 2;
1555      return {
1556        x: center.x + r * Math.cos(theta),
1557        y: center.y + r * Math.sin(theta),
1558      };
1559    }
1560
1561    const nodes = vizData.nodes.map((node, idx) => {
1562      const angle = typeof node.location === "number"
1563        ? angleFromLocation(node.location)
1564        : (idx / vizData.nodes.length) * Math.PI * 2;
1565      const coords = polarToCartesian(angle);
1566      return {
1567        ...node,
1568        angle,
1569        x: coords.x,
1570        y: coords.y,
1571      };
1572    });
1573    const nodeMap = new Map(nodes.map((node) => [node.id, node]));
1574
1575    function drawRingBase() {
1576      ctx.save();
1577      ctx.strokeStyle = "#475569";
1578      ctx.lineWidth = 2;
1579      ctx.beginPath();
1580      ctx.arc(center.x, center.y, radius, 0, Math.PI * 2);
1581      ctx.stroke();
1582      ctx.setLineDash([4, 8]);
1583      ctx.strokeStyle = "rgba(148,163,184,0.3)";
1584      [0, 0.25, 0.5, 0.75].forEach((loc) => {
1585        const angle = angleFromLocation(loc);
1586        const inner = polarToCartesian(angle, radius * 0.9);
1587        const outer = polarToCartesian(angle, radius * 1.02);
1588        ctx.beginPath();
1589        ctx.moveTo(inner.x, inner.y);
1590        ctx.lineTo(outer.x, outer.y);
1591        ctx.stroke();
1592        ctx.fillStyle = "#94a3b8";
1593        ctx.font = "11px 'Fira Code', monospace";
1594        ctx.textAlign = "center";
1595        ctx.fillText(loc.toFixed(2), outer.x, outer.y - 6);
1596      });
1597      ctx.restore();
1598    }
1599
1600    function drawConnections() {
1601      ctx.save();
1602      ctx.strokeStyle = "rgba(148, 163, 184, 0.35)";
1603      ctx.lineWidth = 1.2;
1604      const drawn = new Set();
1605      nodes.forEach((node) => {
1606        node.connections.forEach((neighborId) => {
1607          const neighbor = nodeMap.get(neighborId);
1608          if (!neighbor) return;
1609          const key = node.id < neighborId ? `${node.id}|${neighborId}` : `${neighborId}|${node.id}`;
1610          if (drawn.has(key)) return;
1611          drawn.add(key);
1612          ctx.beginPath();
1613          ctx.moveTo(node.x, node.y);
1614          ctx.lineTo(neighbor.x, neighbor.y);
1615          ctx.stroke();
1616        });
1617      });
1618      ctx.restore();
1619    }
1620
1621    function drawContractMarker() {
1622      if (typeof contractLocation !== "number") return;
1623      const pos = polarToCartesian(angleFromLocation(contractLocation));
1624      ctx.save();
1625      ctx.fillStyle = "#a855f7";
1626      ctx.beginPath();
1627      ctx.arc(pos.x, pos.y, 9, 0, Math.PI * 2);
1628      ctx.fill();
1629      ctx.lineWidth = 2;
1630      ctx.strokeStyle = "#f3e8ff";
1631      ctx.stroke();
1632      ctx.fillStyle = "#f3e8ff";
1633      ctx.font = "10px 'Fira Code', monospace";
1634      ctx.textAlign = "center";
1635      ctx.fillText("contract", pos.x, pos.y - 16);
1636      ctx.restore();
1637    }
1638
1639    function drawPeers() {
1640      nodes.forEach((node) => {
1641        const baseColor = node.is_gateway ? "#f97316" : "#64748b";
1642        const fill = cachingPeers.has(node.id) ? "#38bdf8" : baseColor;
1643        ctx.save();
1644        ctx.beginPath();
1645        ctx.fillStyle = fill;
1646        ctx.arc(node.x, node.y, 6.5, 0, Math.PI * 2);
1647        ctx.fill();
1648        ctx.lineWidth = 1.6;
1649        ctx.strokeStyle = "#0f172a";
1650        ctx.stroke();
1651        ctx.fillStyle = "#f8fafc";
1652        ctx.font = "12px 'Fira Code', monospace";
1653        ctx.textAlign = "center";
1654        ctx.fillText(shortId(node.id), node.x, node.y - 14);
1655        const locText = typeof node.location === "number" ? node.location.toFixed(3) : "n/a";
1656        ctx.fillStyle = "#94a3b8";
1657        ctx.font = "10px 'Fira Code', monospace";
1658        ctx.fillText(locText, node.x, node.y + 20);
1659        ctx.restore();
1660      });
1661    }
1662
1663    function drawOperationEdges(edges, color, dashed = false) {
1664      if (!edges.length) return;
1665      ctx.save();
1666      ctx.strokeStyle = color;
1667      ctx.lineWidth = 3;
1668      if (dashed) ctx.setLineDash([10, 8]);
1669      edges.forEach((edge, idx) => {
1670        const from = nodeMap.get(edge.from);
1671        const to = nodeMap.get(edge.to);
1672        if (!from || !to) return;
1673        ctx.beginPath();
1674        ctx.moveTo(from.x, from.y);
1675        ctx.lineTo(to.x, to.y);
1676        ctx.stroke();
1677        drawArrowhead(from, to, color);
1678        const midX = (from.x + to.x) / 2;
1679        const midY = (from.y + to.y) / 2;
1680        ctx.fillStyle = color;
1681        ctx.font = "11px 'Fira Code', monospace";
1682        ctx.fillText(`#${idx + 1}`, midX, midY - 4);
1683      });
1684      ctx.restore();
1685    }
1686
1687    function drawArrowhead(from, to, color) {
1688      const angle = Math.atan2(to.y - from.y, to.x - from.x);
1689      const length = 12;
1690      const spread = Math.PI / 6;
1691      ctx.save();
1692      ctx.fillStyle = color;
1693      ctx.beginPath();
1694      ctx.moveTo(to.x, to.y);
1695      ctx.lineTo(
1696        to.x - length * Math.cos(angle - spread),
1697        to.y - length * Math.sin(angle - spread)
1698      );
1699      ctx.lineTo(
1700        to.x - length * Math.cos(angle + spread),
1701        to.y - length * Math.sin(angle + spread)
1702      );
1703      ctx.closePath();
1704      ctx.fill();
1705      ctx.restore();
1706    }
1707
1708    function renderOperationList(title, edges) {
1709      if (!edges.length) {
1710        return `<div class="op-block"><strong>${title}:</strong> none recorded</div>`;
1711      }
1712      const items = edges
1713        .map((edge, idx) => {
1714          const ts = edge.timestamp
1715            ? new Date(edge.timestamp).toLocaleTimeString()
1716            : "no-ts";
1717          return `<li>#${idx + 1}: ${shortId(edge.from)} → ${shortId(edge.to)} (${ts})</li>`;
1718        })
1719        .join("");
1720      return `<div class="op-block"><strong>${title}:</strong><ol>${items}</ol></div>`;
1721    }
1722
1723    function renderContractInfo(data) {
1724      if (!data) {
1725        contractInfoEl.textContent = "No contract-specific data collected for this snapshot.";
1726        return;
1727      }
1728      const errors = data.errors?.length
1729        ? `<div class="errors"><strong>Notable events:</strong><ul>${data.errors
1730            .map((msg) => `<li>${msg}</li>`)
1731            .join("")}</ul></div>`
1732        : "";
1733      const putSummary = renderOperationList("PUT path", data.put.edges);
1734      const updateSummary = renderOperationList("UPDATE path", data.update.edges);
1735      contractInfoEl.innerHTML = `
1736        <h2>Contract ${data.key}</h2>
1737        <div><strong>Cached peers:</strong> ${data.caching_peers.length}</div>
1738        <div><strong>PUT completion:</strong> ${
1739          data.put.completion_peer ?? "pending"
1740        }</div>
1741        <div><strong>UPDATE completion:</strong> ${
1742          data.update.completion_peer ?? "pending"
1743        }</div>
1744        ${putSummary}
1745        ${updateSummary}
1746        ${errors}
1747      `;
1748    }
1749
1750    renderContractInfo(contractData);
1751
1752    if (nodes.length) {
1753      drawRingBase();
1754      drawConnections();
1755      drawContractMarker();
1756      drawOperationEdges(putEdges, "#22c55e", false);
1757      drawOperationEdges(updateEdges, "#f59e0b", true);
1758      drawPeers();
1759    } else {
1760      ctx.fillStyle = "#94a3b8";
1761      ctx.font = "16px Inter, sans-serif";
1762      ctx.fillText("No diagnostics available to render ring.", center.x - 140, center.y);
1763    }
1764  </script>
1765</body>
1766</html>
1767"###;
1768
1769fn render_ring_template(data_json: &str) -> String {
1770    HTML_TEMPLATE.replace("__DATA__", data_json)
1771}
1772
1773#[cfg(test)]
1774mod tests {
1775    use super::{compute_ring_metrics, RingPeerSnapshot};
1776
1777    #[test]
1778    fn ring_metrics_basic() {
1779        let nodes = vec![
1780            RingPeerSnapshot {
1781                id: "a".into(),
1782                is_gateway: false,
1783                ws_port: 0,
1784                network_port: 0,
1785                network_address: "127.1.0.1".into(),
1786                location: Some(0.1),
1787                connections: vec!["b".into(), "c".into()],
1788                contract: None,
1789            },
1790            RingPeerSnapshot {
1791                id: "b".into(),
1792                is_gateway: false,
1793                ws_port: 0,
1794                network_port: 0,
1795                network_address: "127.2.0.1".into(),
1796                location: Some(0.2),
1797                connections: vec!["a".into()],
1798                contract: None,
1799            },
1800            RingPeerSnapshot {
1801                id: "c".into(),
1802                is_gateway: true,
1803                ws_port: 0,
1804                network_port: 0,
1805                network_address: "127.3.0.1".into(),
1806                location: Some(0.8),
1807                connections: vec!["a".into()],
1808                contract: None,
1809            },
1810        ];
1811
1812        let metrics = compute_ring_metrics(&nodes);
1813        assert_eq!(metrics.node_count, 3);
1814        assert_eq!(metrics.gateway_count, 1);
1815        assert_eq!(metrics.edge_count, 2);
1816        assert!((metrics.average_degree - (4.0 / 3.0)).abs() < f64::EPSILON);
1817        assert!(metrics.average_ring_distance.is_some());
1818    }
1819}