p2p-foundation 0.1.6

Complete P2P networking foundation with sparkly interactive help system, DHT inboxes with infinite TTL, embedded Flutter PWA, native app support, three-word addresses, and built-in AI capabilities
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
//! Transport Layer
//!
//! This module provides transport protocol implementations for the P2P Foundation.
//! It supports QUIC and TCP transports with automatic selection, connection pooling,
//! and performance monitoring.

pub mod tcp;
pub mod quic;
pub mod tunneled;

use crate::{PeerId, Multiaddr, P2PError, Result};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock};
use tracing::{debug, info, warn};

/// Transport protocol types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TransportType {
    /// QUIC transport protocol
    QUIC,
    /// TCP transport protocol  
    TCP,
}

/// Transport selection strategy
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TransportSelection {
    /// Automatically select best transport
    Auto,
    /// Prefer specific transport with fallback
    Prefer(TransportType),
    /// Force specific transport only
    Force(TransportType),
}

/// Connection quality metrics
#[derive(Debug, Clone)]
pub struct ConnectionQuality {
    /// Round-trip latency
    pub latency: Duration,
    /// Throughput in Mbps
    pub throughput_mbps: f64,
    /// Packet loss percentage
    pub packet_loss: f64,
    /// Jitter (latency variation)
    pub jitter: Duration,
    /// Connection establishment time
    pub connect_time: Duration,
}

/// Connection information
#[derive(Debug, Clone)]
pub struct ConnectionInfo {
    /// Transport type being used
    pub transport_type: TransportType,
    /// Local address
    pub local_addr: Multiaddr,
    /// Remote address
    pub remote_addr: Multiaddr,
    /// Whether connection is encrypted
    pub is_encrypted: bool,
    /// Cipher suite being used
    pub cipher_suite: String,
    /// Whether 0-RTT was used
    pub used_0rtt: bool,
    /// Connection establishment time
    pub established_at: Instant,
    /// Last activity timestamp
    pub last_activity: Instant,
}

/// Connection pool information
#[derive(Debug, Clone)]
pub struct ConnectionPoolInfo {
    /// Number of active connections
    pub active_connections: usize,
    /// Total connections ever created
    pub total_connections: usize,
    /// Bytes sent through pool
    pub bytes_sent: u64,
    /// Bytes received through pool
    pub bytes_received: u64,
}

/// Connection pool statistics
#[derive(Debug, Clone)]
pub struct ConnectionPoolStats {
    /// Messages sent per connection
    pub messages_per_connection: HashMap<String, usize>,
    /// Bytes per connection
    pub bytes_per_connection: HashMap<String, u64>,
    /// Average latency per connection
    pub latency_per_connection: HashMap<String, Duration>,
}

/// Message received from transport
#[derive(Debug, Clone)]
pub struct TransportMessage {
    /// Sender peer ID
    pub sender: PeerId,
    /// Message data
    pub data: Vec<u8>,
    /// Protocol identifier
    pub protocol: String,
    /// Timestamp when received
    pub received_at: Instant,
}

/// Transport trait for protocol implementations
#[async_trait]
pub trait Transport: Send + Sync {
    /// Start listening on the given address
    async fn listen(&self, addr: SocketAddr) -> Result<Vec<Multiaddr>>;
    
    /// Accept incoming connections (for server-side)
    async fn accept(&self) -> Result<Box<dyn Connection>>;
    
    /// Connect to a remote peer
    async fn connect(&self, addr: &Multiaddr) -> Result<Box<dyn Connection>>;
    
    /// Connect with specific transport options
    async fn connect_with_options(&self, addr: &Multiaddr, options: TransportOptions) -> Result<Box<dyn Connection>>;
    
    /// Get supported addresses for this transport
    fn supported_addresses(&self) -> Vec<String>;
    
    /// Get transport type
    fn transport_type(&self) -> TransportType;
    
    /// Check if address is supported
    fn supports_address(&self, addr: &Multiaddr) -> bool;
}

/// Connection trait for active connections
#[async_trait]
pub trait Connection: Send + Sync {
    /// Send data over the connection
    async fn send(&mut self, data: &[u8]) -> Result<()>;
    
    /// Receive data from the connection
    async fn receive(&mut self) -> Result<Vec<u8>>;
    
    /// Get connection info
    async fn info(&self) -> ConnectionInfo;
    
    /// Close the connection
    async fn close(&mut self) -> Result<()>;
    
    /// Check if connection is alive
    async fn is_alive(&self) -> bool;
    
    /// Measure connection quality
    async fn measure_quality(&self) -> Result<ConnectionQuality>;
    
    /// Get local address
    fn local_addr(&self) -> Multiaddr;
    
    /// Get remote address
    fn remote_addr(&self) -> Multiaddr;
}

/// Transport configuration options
#[derive(Debug, Clone)]
pub struct TransportOptions {
    /// Enable 0-RTT for QUIC
    pub enable_0rtt: bool,
    /// Force encryption
    pub require_encryption: bool,
    /// Connection timeout
    pub connect_timeout: Duration,
    /// Keep-alive interval
    pub keep_alive: Duration,
    /// Maximum message size
    pub max_message_size: usize,
}

/// Transport manager coordinates different transport protocols
pub struct TransportManager {
    /// Available transports
    transports: HashMap<TransportType, Arc<dyn Transport>>,
    /// Active connections
    connections: Arc<RwLock<HashMap<PeerId, Arc<Mutex<ConnectionPool>>>>>,
    /// Transport selection strategy
    selection: TransportSelection,
    /// Configuration options
    options: TransportOptions,
}

/// Connection pool for a specific peer
struct ConnectionPool {
    /// Active connections
    connections: Vec<Arc<Mutex<Box<dyn Connection>>>>,
    /// Connection info cache (reserved for future use)
    _info_cache: HashMap<String, ConnectionInfo>,
    /// Pool statistics
    stats: ConnectionPoolStats,
    /// Pool configuration
    max_connections: usize,
    /// Round-robin index for load balancing
    round_robin_index: usize,
}

impl TransportManager {
    /// Create a new transport manager
    pub fn new(selection: TransportSelection, options: TransportOptions) -> Self {
        Self {
            transports: HashMap::new(),
            connections: Arc::new(RwLock::new(HashMap::new())),
            selection,
            options,
        }
    }
    
    /// Register a transport implementation
    pub fn register_transport(&mut self, transport: Arc<dyn Transport>) {
        let transport_type = transport.transport_type();
        self.transports.insert(transport_type, transport);
        info!("Registered transport: {:?}", transport_type);
    }
    
    /// Connect to a peer using the best available transport
    pub async fn connect(&self, addr: &Multiaddr) -> Result<PeerId> {
        let transport_type = self.select_transport(addr).await?;
        let transport = self.transports.get(&transport_type)
            .ok_or_else(|| P2PError::Transport(format!("Transport {:?} not available", transport_type)))?;
        
        debug!("Connecting to {} using {:?}", addr, transport_type);
        
        let connection = transport.connect_with_options(addr, self.options.clone()).await?;
        let peer_id = format!("peer_from_{}", addr); // Placeholder peer ID extraction
        
        // Add to connection pool
        self.add_connection(peer_id.clone(), connection).await?;
        
        info!("Connected to peer {} via {:?}", peer_id, transport_type);
        Ok(peer_id)
    }
    
    /// Connect with specific transport
    pub async fn connect_with_transport(&self, addr: &Multiaddr, transport_type: TransportType) -> Result<PeerId> {
        let transport = self.transports.get(&transport_type)
            .ok_or_else(|| P2PError::Transport(format!("Transport {:?} not available", transport_type)))?;
        
        let connection = transport.connect_with_options(addr, self.options.clone()).await?;
        let peer_id = format!("peer_from_{}", addr);
        
        self.add_connection(peer_id.clone(), connection).await?;
        Ok(peer_id)
    }
    
    /// Send message to a peer
    pub async fn send_message(&self, peer_id: &PeerId, data: Vec<u8>) -> Result<()> {
        let connections = self.connections.read().await;
        let pool = connections.get(peer_id)
            .ok_or_else(|| P2PError::Network(format!("No connection to peer {}", peer_id)))?;
        
        let mut pool_guard = pool.lock().await;
        let connection = pool_guard.get_connection()?;
        
        let mut conn_guard = connection.lock().await;
        conn_guard.send(&data).await?;
        
        debug!("Sent {} bytes to peer {}", data.len(), peer_id);
        Ok(())
    }
    
    /// Get connection info for a peer
    pub async fn get_connection_info(&self, peer_id: &PeerId) -> Result<ConnectionInfo> {
        let connections = self.connections.read().await;
        let pool = connections.get(peer_id)
            .ok_or_else(|| P2PError::Network(format!("No connection to peer {}", peer_id)))?;
        
        let mut pool_guard = pool.lock().await;
        let connection = pool_guard.get_connection()?;
        let conn_guard = connection.lock().await;
        
        Ok(conn_guard.info().await)
    }
    
    /// Get connection pool info
    pub async fn get_connection_pool_info(&self, peer_id: &PeerId) -> Result<ConnectionPoolInfo> {
        let connections = self.connections.read().await;
        let pool = connections.get(peer_id)
            .ok_or_else(|| P2PError::Network(format!("No connection to peer {}", peer_id)))?;
        
        let pool_guard = pool.lock().await;
        Ok(ConnectionPoolInfo {
            active_connections: pool_guard.connections.len(),
            total_connections: pool_guard.stats.messages_per_connection.len(),
            bytes_sent: pool_guard.stats.bytes_per_connection.values().sum(),
            bytes_received: 0, // TODO: Track separately
        })
    }
    
    /// Get connection pool statistics
    pub async fn get_connection_pool_stats(&self, peer_id: &PeerId) -> Result<ConnectionPoolStats> {
        let connections = self.connections.read().await;
        let pool = connections.get(peer_id)
            .ok_or_else(|| P2PError::Network(format!("No connection to peer {}", peer_id)))?;
        
        let pool_guard = pool.lock().await;
        Ok(pool_guard.stats.clone())
    }
    
    /// Measure connection quality
    pub async fn measure_connection_quality(&self, peer_id: &PeerId) -> Result<ConnectionQuality> {
        let connections = self.connections.read().await;
        let pool = connections.get(peer_id)
            .ok_or_else(|| P2PError::Network(format!("No connection to peer {}", peer_id)))?;
        
        let mut pool_guard = pool.lock().await;
        let connection = pool_guard.get_connection()?;
        let conn_guard = connection.lock().await;
        
        conn_guard.measure_quality().await
    }
    
    /// Switch transport for a peer
    pub async fn switch_transport(&self, peer_id: &PeerId, _new_transport: TransportType) -> Result<()> {
        // This is a placeholder implementation
        // In reality, this would establish a new connection with the new transport
        // and gracefully migrate the existing connection
        
        warn!("Transport switching not yet fully implemented for peer {}", peer_id);
        Ok(())
    }
    
    /// Select best transport for an address
    async fn select_transport(&self, addr: &Multiaddr) -> Result<TransportType> {
        match &self.selection {
            TransportSelection::Force(transport_type) => {
                if self.transports.contains_key(transport_type) {
                    Ok(*transport_type)
                } else {
                    Err(P2PError::Transport(format!("Forced transport {:?} not available", transport_type)))
                }
            }
            TransportSelection::Prefer(preferred) => {
                if self.transports.contains_key(preferred) {
                    Ok(*preferred)
                } else {
                    // Fall back to any available transport
                    self.auto_select_transport(addr).await
                }
            }
            TransportSelection::Auto => {
                self.auto_select_transport(addr).await
            }
        }
    }
    
    /// Auto-select best transport based on address and conditions
    async fn auto_select_transport(&self, addr: &Multiaddr) -> Result<TransportType> {
        // Strongly prefer QUIC if available (better performance, 0-RTT, multiplexing)
        if self.transports.contains_key(&TransportType::QUIC) {
            if let Some(transport) = self.transports.get(&TransportType::QUIC) {
                if transport.supports_address(addr) {
                    debug!("Selected QUIC transport for {} (preferred for P2P)", addr);
                    return Ok(TransportType::QUIC);
                }
            }
        }
        
        // Fall back to TCP only as last resort
        if self.transports.contains_key(&TransportType::TCP) {
            if let Some(transport) = self.transports.get(&TransportType::TCP) {
                if transport.supports_address(addr) {
                    warn!("Falling back to TCP transport for {}. QUIC would provide better performance.", addr);
                    return Ok(TransportType::TCP);
                }
            }
        }
        
        Err(P2PError::Transport("No suitable transport available. Consider using QUIC-compatible addresses.".to_string()))
    }
    
    /// Add connection to pool
    async fn add_connection(&self, peer_id: PeerId, connection: Box<dyn Connection>) -> Result<()> {
        let mut connections = self.connections.write().await;
        
        let pool = connections.entry(peer_id.clone()).or_insert_with(|| {
            Arc::new(Mutex::new(ConnectionPool::new(3))) // Default max 3 connections per peer
        });
        
        let mut pool_guard = pool.lock().await;
        pool_guard.add_connection(connection).await?;
        
        Ok(())
    }
}

impl ConnectionPool {
    /// Create a new connection pool
    fn new(max_connections: usize) -> Self {
        Self {
            connections: Vec::new(),
            _info_cache: HashMap::new(),
            stats: ConnectionPoolStats {
                messages_per_connection: HashMap::new(),
                bytes_per_connection: HashMap::new(),
                latency_per_connection: HashMap::new(),
            },
            max_connections,
            round_robin_index: 0,
        }
    }
    
    /// Add a connection to the pool
    async fn add_connection(&mut self, connection: Box<dyn Connection>) -> Result<()> {
        if self.connections.len() >= self.max_connections {
            // Remove oldest connection
            self.connections.remove(0);
        }
        
        let conn_id = format!("conn_{}", self.connections.len());
        self.stats.messages_per_connection.insert(conn_id.clone(), 0);
        self.stats.bytes_per_connection.insert(conn_id.clone(), 0);
        self.stats.latency_per_connection.insert(conn_id, Duration::from_millis(0));
        
        self.connections.push(Arc::new(Mutex::new(connection)));
        Ok(())
    }
    
    /// Get a connection using round-robin load balancing
    fn get_connection(&mut self) -> Result<Arc<Mutex<Box<dyn Connection>>>> {
        if self.connections.is_empty() {
            return Err(P2PError::Network("No connections available".to_string()));
        }
        
        let connection = self.connections[self.round_robin_index % self.connections.len()].clone();
        self.round_robin_index += 1;
        
        // Update stats
        let conn_id = format!("conn_{}", self.round_robin_index % self.connections.len());
        if let Some(count) = self.stats.messages_per_connection.get_mut(&conn_id) {
            *count += 1;
        }
        
        Ok(connection)
    }
}

impl fmt::Display for TransportType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TransportType::QUIC => write!(f, "quic"),
            TransportType::TCP => write!(f, "tcp"),
        }
    }
}

impl Default for TransportSelection {
    fn default() -> Self {
        // Default to preferring QUIC with TCP fallback
        TransportSelection::Prefer(TransportType::QUIC)
    }
}

impl Default for TransportOptions {
    fn default() -> Self {
        Self {
            enable_0rtt: true,
            require_encryption: true,
            connect_timeout: Duration::from_secs(30),
            keep_alive: Duration::from_secs(60),
            max_message_size: 64 * 1024 * 1024, // 64MB
        }
    }
}

impl Default for ConnectionQuality {
    fn default() -> Self {
        Self {
            latency: Duration::from_millis(50),
            throughput_mbps: 100.0,
            packet_loss: 0.0,
            jitter: Duration::from_millis(5),
            connect_time: Duration::from_millis(100),
        }
    }
}


/// Legacy transport types module for backward compatibility
pub mod transport_types {
    pub use super::TransportType;
}

// Re-export transport implementations
pub use tcp::TcpTransport;
pub use quic::QuicTransport;

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use tokio::time::Duration;

    /// Mock transport implementation for testing
    struct MockTransport {
        transport_type: TransportType,
        should_fail: bool,
        supports_all: bool,
    }

    impl MockTransport {
        fn new(transport_type: TransportType) -> Self {
            Self {
                transport_type,
                should_fail: false,
                supports_all: true,
            }
        }

        fn with_failure(mut self) -> Self {
            self.should_fail = true;
            self
        }

        fn with_limited_support(mut self) -> Self {
            self.supports_all = false;
            self
        }
    }

    #[async_trait]
    impl Transport for MockTransport {
        async fn listen(&self, _addr: SocketAddr) -> Result<Vec<Multiaddr>> {
            if self.should_fail {
                return Err(P2PError::Transport("Listen failed".to_string()));
            }
            Ok(vec!["/ip4/127.0.0.1/tcp/9000".to_string()])
        }

        async fn connect(&self, addr: &Multiaddr) -> Result<Box<dyn Connection>> {
            if self.should_fail {
                return Err(P2PError::Transport("Connection failed".to_string()));
            }
            Ok(Box::new(MockConnection::new(addr.clone())))
        }

        async fn connect_with_options(&self, addr: &Multiaddr, _options: TransportOptions) -> Result<Box<dyn Connection>> {
            self.connect(addr).await
        }

        async fn accept(&self) -> Result<Box<dyn Connection>> {
            if self.should_fail {
                return Err(P2PError::Transport("Accept failed".to_string()));
            }
            Ok(Box::new(MockConnection::new("/ip4/127.0.0.1/tcp/9000".to_string())))
        }

        fn supported_addresses(&self) -> Vec<String> {
            if self.supports_all {
                vec!["/ip4/0.0.0.0/tcp/0".to_string(), "/ip6/::/tcp/0".to_string()]
            } else {
                vec!["/ip4/0.0.0.0/tcp/0".to_string()]
            }
        }

        fn transport_type(&self) -> TransportType {
            self.transport_type
        }

        fn supports_address(&self, addr: &Multiaddr) -> bool {
            if !self.supports_all && addr.contains("ip6") {
                return false;
            }
            addr.contains("tcp") || addr.contains("quic")
        }
    }

    /// Mock connection implementation for testing
    struct MockConnection {
        remote_addr: Multiaddr,
        is_alive: bool,
        bytes_sent: AtomicUsize,
        bytes_received: AtomicUsize,
    }

    impl MockConnection {
        fn new(remote_addr: Multiaddr) -> Self {
            Self {
                remote_addr,
                is_alive: true,
                bytes_sent: AtomicUsize::new(0),
                bytes_received: AtomicUsize::new(0),
            }
        }
    }

    #[async_trait]
    impl Connection for MockConnection {
        async fn send(&mut self, data: &[u8]) -> Result<()> {
            if !self.is_alive {
                return Err(P2PError::Network("Connection closed".to_string()));
            }
            self.bytes_sent.fetch_add(data.len(), Ordering::Relaxed);
            Ok(())
        }

        async fn receive(&mut self) -> Result<Vec<u8>> {
            if !self.is_alive {
                return Err(P2PError::Network("Connection closed".to_string()));
            }
            let data = b"mock_response".to_vec();
            self.bytes_received.fetch_add(data.len(), Ordering::Relaxed);
            Ok(data)
        }

        async fn info(&self) -> ConnectionInfo {
            ConnectionInfo {
                transport_type: TransportType::QUIC,
                local_addr: "/ip4/127.0.0.1/tcp/9000".to_string(),
                remote_addr: self.remote_addr.clone(),
                is_encrypted: true,
                cipher_suite: "TLS_AES_256_GCM_SHA384".to_string(),
                used_0rtt: false,
                established_at: Instant::now(),
                last_activity: Instant::now(),
            }
        }

        async fn close(&mut self) -> Result<()> {
            self.is_alive = false;
            Ok(())
        }

        async fn is_alive(&self) -> bool {
            self.is_alive
        }

        async fn measure_quality(&self) -> Result<ConnectionQuality> {
            Ok(ConnectionQuality {
                latency: Duration::from_millis(10),
                throughput_mbps: 1000.0,
                packet_loss: 0.1,
                jitter: Duration::from_millis(2),
                connect_time: Duration::from_millis(50),
            })
        }

        fn local_addr(&self) -> Multiaddr {
            "/ip4/127.0.0.1/tcp/9000".to_string()
        }

        fn remote_addr(&self) -> Multiaddr {
            self.remote_addr.clone()
        }
    }

    fn create_test_transport_manager() -> TransportManager {
        let options = TransportOptions::default();
        TransportManager::new(TransportSelection::Auto, options)
    }

    #[test]
    fn test_transport_type_display() {
        assert_eq!(format!("{}", TransportType::QUIC), "quic");
        assert_eq!(format!("{}", TransportType::TCP), "tcp");
    }

    #[test]
    fn test_transport_type_serialization() {
        let quic_type = TransportType::QUIC;
        let tcp_type = TransportType::TCP;

        assert_eq!(quic_type, TransportType::QUIC);
        assert_eq!(tcp_type, TransportType::TCP);
        assert_ne!(quic_type, tcp_type);
    }

    #[test]
    fn test_transport_selection_variants() {
        let auto = TransportSelection::Auto;
        let prefer_quic = TransportSelection::Prefer(TransportType::QUIC);
        let force_tcp = TransportSelection::Force(TransportType::TCP);

        assert!(matches!(auto, TransportSelection::Auto));
        assert!(matches!(prefer_quic, TransportSelection::Prefer(TransportType::QUIC)));
        assert!(matches!(force_tcp, TransportSelection::Force(TransportType::TCP)));
    }

    #[test]
    fn test_transport_selection_default() {
        let default = TransportSelection::default();
        assert!(matches!(default, TransportSelection::Prefer(TransportType::QUIC)));
    }

    #[test]
    fn test_transport_options_default() {
        let options = TransportOptions::default();
        
        assert!(options.enable_0rtt);
        assert!(options.require_encryption);
        assert_eq!(options.connect_timeout, Duration::from_secs(30));
        assert_eq!(options.keep_alive, Duration::from_secs(60));
        assert_eq!(options.max_message_size, 64 * 1024 * 1024);
    }

    #[test]
    fn test_connection_quality_default() {
        let quality = ConnectionQuality::default();
        
        assert_eq!(quality.latency, Duration::from_millis(50));
        assert_eq!(quality.throughput_mbps, 100.0);
        assert_eq!(quality.packet_loss, 0.0);
        assert_eq!(quality.jitter, Duration::from_millis(5));
        assert_eq!(quality.connect_time, Duration::from_millis(100));
    }

    #[tokio::test]
    async fn test_transport_manager_creation() {
        let manager = create_test_transport_manager();
        assert!(manager.transports.is_empty());
    }

    #[tokio::test]
    async fn test_transport_registration() {
        let mut manager = create_test_transport_manager();
        let quic_transport = Arc::new(MockTransport::new(TransportType::QUIC));
        let tcp_transport = Arc::new(MockTransport::new(TransportType::TCP));

        manager.register_transport(quic_transport.clone());
        manager.register_transport(tcp_transport.clone());

        assert_eq!(manager.transports.len(), 2);
        assert!(manager.transports.contains_key(&TransportType::QUIC));
        assert!(manager.transports.contains_key(&TransportType::TCP));
    }

    #[tokio::test]
    async fn test_connection_establishment() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::QUIC));
        manager.register_transport(transport);

        let peer_id = manager.connect(&"/ip4/127.0.0.1/tcp/9001".to_string()).await?;
        assert_eq!(peer_id, "peer_from_/ip4/127.0.0.1/tcp/9001");

        let connections = manager.connections.read().await;
        assert!(connections.contains_key(&peer_id));

        Ok(())
    }

    #[tokio::test]
    async fn test_connection_with_specific_transport() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::TCP));
        manager.register_transport(transport);

        let peer_id = manager.connect_with_transport(
            &"/ip4/127.0.0.1/tcp/9002".to_string(),
            TransportType::TCP
        ).await?;

        assert_eq!(peer_id, "peer_from_/ip4/127.0.0.1/tcp/9002");
        Ok(())
    }

    #[tokio::test]
    async fn test_connection_failure_handling() {
        let mut manager = create_test_transport_manager();
        let failing_transport = Arc::new(MockTransport::new(TransportType::QUIC).with_failure());
        manager.register_transport(failing_transport);

        let result = manager.connect(&"/ip4/127.0.0.1/tcp/9003".to_string()).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Connection failed"));
    }

    #[tokio::test]
    async fn test_message_sending() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::QUIC));
        manager.register_transport(transport);

        let peer_id = manager.connect(&"/ip4/127.0.0.1/tcp/9004".to_string()).await?;
        let message = b"Hello, transport!".to_vec();
        
        manager.send_message(&peer_id, message.clone()).await?;

        // Verify message was processed
        let pool_info = manager.get_connection_pool_info(&peer_id).await?;
        assert_eq!(pool_info.active_connections, 1);

        Ok(())
    }

    #[tokio::test]
    async fn test_message_sending_no_connection() {
        let manager = create_test_transport_manager();
        let result = manager.send_message(&"nonexistent_peer".to_string(), vec![1, 2, 3]).await;
        
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("No connection to peer"));
    }

    #[tokio::test]
    async fn test_connection_info_retrieval() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::QUIC));
        manager.register_transport(transport);

        let peer_id = manager.connect(&"/ip4/127.0.0.1/tcp/9005".to_string()).await?;
        let info = manager.get_connection_info(&peer_id).await?;

        assert_eq!(info.transport_type, TransportType::QUIC);
        assert_eq!(info.remote_addr, "/ip4/127.0.0.1/tcp/9005");
        assert!(info.is_encrypted);
        assert_eq!(info.cipher_suite, "TLS_AES_256_GCM_SHA384");

        Ok(())
    }

    #[tokio::test]
    async fn test_connection_pool_info() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::QUIC));
        manager.register_transport(transport);

        let peer_id = manager.connect(&"/ip4/127.0.0.1/tcp/9006".to_string()).await?;
        let pool_info = manager.get_connection_pool_info(&peer_id).await?;

        assert_eq!(pool_info.active_connections, 1);
        assert_eq!(pool_info.total_connections, 1);
        assert_eq!(pool_info.bytes_sent, 0);

        Ok(())
    }

    #[tokio::test]
    async fn test_connection_pool_stats() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::QUIC));
        manager.register_transport(transport);

        let peer_id = manager.connect(&"/ip4/127.0.0.1/tcp/9007".to_string()).await?;
        let stats = manager.get_connection_pool_stats(&peer_id).await?;

        assert_eq!(stats.messages_per_connection.len(), 1);
        assert_eq!(stats.bytes_per_connection.len(), 1);
        assert_eq!(stats.latency_per_connection.len(), 1);

        Ok(())
    }

    #[tokio::test]
    async fn test_connection_quality_measurement() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::QUIC));
        manager.register_transport(transport);

        let peer_id = manager.connect(&"/ip4/127.0.0.1/tcp/9008".to_string()).await?;
        let quality = manager.measure_connection_quality(&peer_id).await?;

        assert_eq!(quality.latency, Duration::from_millis(10));
        assert_eq!(quality.throughput_mbps, 1000.0);
        assert_eq!(quality.packet_loss, 0.1);
        assert_eq!(quality.jitter, Duration::from_millis(2));

        Ok(())
    }

    #[tokio::test]
    async fn test_transport_switching() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let transport = Arc::new(MockTransport::new(TransportType::QUIC));
        manager.register_transport(transport);

        let peer_id = manager.connect(&"/ip4/127.0.0.1/tcp/9009".to_string()).await?;
        
        // Transport switching is not fully implemented, but should not error
        let result = manager.switch_transport(&peer_id, TransportType::TCP).await;
        assert!(result.is_ok());

        Ok(())
    }

    #[tokio::test]
    async fn test_auto_transport_selection_prefer_quic() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let quic_transport = Arc::new(MockTransport::new(TransportType::QUIC));
        let tcp_transport = Arc::new(MockTransport::new(TransportType::TCP));
        
        manager.register_transport(quic_transport);
        manager.register_transport(tcp_transport);

        let addr = "/ip4/127.0.0.1/tcp/9010".to_string();
        let selected = manager.auto_select_transport(&addr).await?;
        
        // Should prefer QUIC when available
        assert_eq!(selected, TransportType::QUIC);

        Ok(())
    }

    #[tokio::test]
    async fn test_transport_selection_fallback_to_tcp() -> Result<()> {
        let mut manager = create_test_transport_manager();
        let tcp_transport = Arc::new(MockTransport::new(TransportType::TCP));
        
        manager.register_transport(tcp_transport);

        let addr = "/ip4/127.0.0.1/tcp/9011".to_string();
        let selected = manager.auto_select_transport(&addr).await?;
        
        // Should fall back to TCP when QUIC not available
        assert_eq!(selected, TransportType::TCP);

        Ok(())
    }

    #[tokio::test]
    async fn test_transport_selection_no_suitable_transport() {
        let manager = create_test_transport_manager();
        let addr = "/ip4/127.0.0.1/tcp/9012".to_string();
        
        let result = manager.auto_select_transport(&addr).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("No suitable transport available"));
    }

    #[tokio::test]
    async fn test_forced_transport_selection() -> Result<()> {
        let mut manager = TransportManager::new(
            TransportSelection::Force(TransportType::TCP),
            TransportOptions::default()
        );
        let tcp_transport = Arc::new(MockTransport::new(TransportType::TCP));
        
        manager.register_transport(tcp_transport);

        let addr = "/ip4/127.0.0.1/tcp/9013".to_string();
        let selected = manager.select_transport(&addr).await?;
        
        assert_eq!(selected, TransportType::TCP);

        Ok(())
    }

    #[tokio::test]
    async fn test_forced_transport_unavailable() {
        let manager = TransportManager::new(
            TransportSelection::Force(TransportType::QUIC),
            TransportOptions::default()
        );

        let addr = "/ip4/127.0.0.1/tcp/9014".to_string();
        let result = manager.select_transport(&addr).await;
        
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Forced transport QUIC not available"));
    }

    #[tokio::test]
    async fn test_preferred_transport_with_fallback() -> Result<()> {
        let mut manager = TransportManager::new(
            TransportSelection::Prefer(TransportType::QUIC),
            TransportOptions::default()
        );
        let tcp_transport = Arc::new(MockTransport::new(TransportType::TCP));
        
        manager.register_transport(tcp_transport);

        let addr = "/ip4/127.0.0.1/tcp/9015".to_string();
        let selected = manager.select_transport(&addr).await?;
        
        // Should fall back to TCP when preferred QUIC is not available
        assert_eq!(selected, TransportType::TCP);

        Ok(())
    }

    #[tokio::test]
    async fn test_mock_connection_lifecycle() -> Result<()> {
        let mut conn = MockConnection::new("/ip4/127.0.0.1/tcp/9016".to_string());

        assert!(conn.is_alive().await);

        // Test sending
        conn.send(b"test message").await?;
        assert_eq!(conn.bytes_sent.load(Ordering::Relaxed), 12);

        // Test receiving
        let received = conn.receive().await?;
        assert_eq!(received, b"mock_response");
        assert_eq!(conn.bytes_received.load(Ordering::Relaxed), 13);

        // Test connection info
        let info = conn.info().await;
        assert_eq!(info.transport_type, TransportType::QUIC);
        assert!(info.is_encrypted);

        // Test quality measurement
        let quality = conn.measure_quality().await?;
        assert_eq!(quality.latency, Duration::from_millis(10));

        // Test close
        conn.close().await?;
        assert!(!conn.is_alive().await);

        // Operations should fail after close
        let result = conn.send(b"test").await;
        assert!(result.is_err());

        Ok(())
    }

    #[tokio::test]
    async fn test_connection_pool_max_connections() -> Result<()> {
        let mut pool = ConnectionPool::new(2); // Max 2 connections

        // Add first connection
        let conn1 = Box::new(MockConnection::new("/ip4/127.0.0.1/tcp/9017".to_string()));
        pool.add_connection(conn1).await?;
        assert_eq!(pool.connections.len(), 1);

        // Add second connection
        let conn2 = Box::new(MockConnection::new("/ip4/127.0.0.1/tcp/9018".to_string()));
        pool.add_connection(conn2).await?;
        assert_eq!(pool.connections.len(), 2);

        // Add third connection (should remove first)
        let conn3 = Box::new(MockConnection::new("/ip4/127.0.0.1/tcp/9019".to_string()));
        pool.add_connection(conn3).await?;
        assert_eq!(pool.connections.len(), 2);

        Ok(())
    }

    #[tokio::test]
    async fn test_connection_pool_round_robin() -> Result<()> {
        let mut pool = ConnectionPool::new(3);

        // Add connections
        for i in 0..3 {
            let conn = Box::new(MockConnection::new(format!("/ip4/127.0.0.1/tcp/{}", 9020 + i)));
            pool.add_connection(conn).await?;
        }

        // Test round-robin selection
        let conn1 = pool.get_connection()?;
        let conn2 = pool.get_connection()?;
        let conn3 = pool.get_connection()?;
        let conn4 = pool.get_connection()?; // Should wrap around

        // All connections should be different (until wraparound)
        assert_ne!(Arc::as_ptr(&conn1), Arc::as_ptr(&conn2));
        assert_ne!(Arc::as_ptr(&conn2), Arc::as_ptr(&conn3));
        // Fourth should be same as first (round-robin)
        assert_eq!(Arc::as_ptr(&conn1), Arc::as_ptr(&conn4));

        Ok(())
    }

    #[tokio::test]
    async fn test_connection_pool_empty() {
        let mut pool = ConnectionPool::new(3);
        let result = pool.get_connection();
        
        assert!(result.is_err());
        if let Err(e) = result {
            assert!(e.to_string().contains("No connections available"));
        }
    }

    #[tokio::test]
    async fn test_transport_message_structure() {
        let message = TransportMessage {
            sender: "test_peer".to_string(),
            data: vec![1, 2, 3, 4],
            protocol: "/p2p/test/1.0.0".to_string(),
            received_at: Instant::now(),
        };

        assert_eq!(message.sender, "test_peer");
        assert_eq!(message.data, vec![1, 2, 3, 4]);
        assert_eq!(message.protocol, "/p2p/test/1.0.0");
    }

    #[tokio::test]
    async fn test_mock_transport_address_support() {
        let transport = MockTransport::new(TransportType::QUIC);
        
        assert!(transport.supports_address(&"/ip4/127.0.0.1/tcp/9000".to_string()));
        assert!(transport.supports_address(&"/ip6/::1/tcp/9000".to_string()));
        assert!(!transport.supports_address(&"/ip4/127.0.0.1/udp/9000".to_string()));

        let limited_transport = MockTransport::new(TransportType::QUIC).with_limited_support();
        assert!(limited_transport.supports_address(&"/ip4/127.0.0.1/tcp/9000".to_string()));
        assert!(!limited_transport.supports_address(&"/ip6/::1/tcp/9000".to_string()));
    }

    #[tokio::test]
    async fn test_mock_transport_supported_addresses() {
        let transport = MockTransport::new(TransportType::QUIC);
        let addresses = transport.supported_addresses();
        
        assert_eq!(addresses.len(), 2);
        assert!(addresses.contains(&"/ip4/0.0.0.0/tcp/0".to_string()));
        assert!(addresses.contains(&"/ip6/::/tcp/0".to_string()));

        let limited_transport = MockTransport::new(TransportType::QUIC).with_limited_support();
        let limited_addresses = limited_transport.supported_addresses();
        
        assert_eq!(limited_addresses.len(), 1);
        assert!(limited_addresses.contains(&"/ip4/0.0.0.0/tcp/0".to_string()));
    }

    #[tokio::test]
    async fn test_transport_options_configuration() {
        let options = TransportOptions {
            enable_0rtt: false,
            require_encryption: false,
            connect_timeout: Duration::from_secs(10),
            keep_alive: Duration::from_secs(30),
            max_message_size: 1024,
        };

        assert!(!options.enable_0rtt);
        assert!(!options.require_encryption);
        assert_eq!(options.connect_timeout, Duration::from_secs(10));
        assert_eq!(options.keep_alive, Duration::from_secs(30));
        assert_eq!(options.max_message_size, 1024);
    }
}