1- #![ allow( missing_docs) ]
2- #![ allow( dead_code) ]
3- #![ allow( unused) ]
4-
51//! P2P core concepts
62
7- use std:: { sync :: Once , time:: Duration } ;
3+ use std:: time:: Duration ;
84
95use libp2p:: {
10- Swarm , SwarmBuilder , identify,
11- identity:: Keypair ,
12- noise, ping, relay,
13- swarm:: { NetworkBehaviour , SwarmEvent } ,
14- tcp, yamux,
6+ Swarm , SwarmBuilder , identity:: Keypair , noise, relay, swarm:: NetworkBehaviour , tcp, yamux,
157} ;
168
17- use libp2p:: mdns;
18-
199use crate :: { config:: P2PConfig , gater:: ConnGater } ;
2010
11+ /// P2P error.
2112#[ derive( Debug , thiserror:: Error ) ]
2213pub enum P2PError {
2314 /// Failed to build the swarm.
2415 #[ error( "Failed to build the swarm: {0}" ) ]
2516 FailedToBuildSwarm ( Box < dyn std:: error:: Error + Send + Sync > ) ,
2617
18+ /// Failed to convert the secret key to a libp2p keypair.
2719 #[ error( "Failed to convert the secret key to a libp2p keypair: {0}" ) ]
2820 FailedToConvertSecretKeyToLibp2pKeypair ( #[ from] k256:: pkcs8:: der:: Error ) ,
2921
22+ /// Failed to decode the libp2p keypair.
3023 #[ error( "Failed to decode the libp2p keypair: {0}" ) ]
3124 FailedToDecodeLibp2pKeypair ( #[ from] libp2p:: identity:: DecodingError ) ,
3225}
3326
3427impl P2PError {
28+ /// Failed to build the swarm.
3529 pub fn failed_to_build_swarm ( error : impl std:: error:: Error + Send + Sync + ' static ) -> Self {
3630 Self :: FailedToBuildSwarm ( Box :: new ( error) )
3731 }
3832}
3933
4034type Result < T > = std:: result:: Result < T , P2PError > ;
4135
36+ /// Node type.
4237pub enum NodeType {
38+ /// TCP node.
4339 TCP ,
40+ /// QUIC node.
4441 QUIC ,
4542}
4643
44+ /// Node.
4745pub struct Node < B : NetworkBehaviour > {
46+ /// Swarm.
4847 pub swarm : Swarm < B > ,
4948}
5049
5150impl < B : NetworkBehaviour > Node < B > {
51+ /// Creates a new node.
5252 pub fn new < F > (
5353 cfg : P2PConfig ,
5454 key : k256:: SecretKey ,
@@ -78,11 +78,12 @@ impl<B: NetworkBehaviour> Node<B> {
7878 tcp:: Config :: default ( )
7979 }
8080
81- pub fn new_with_quic < F > (
82- cfg : P2PConfig ,
81+ /// Creates a new node with QUIC.
82+ fn new_with_quic < F > (
83+ _cfg : P2PConfig ,
8384 key : k256:: SecretKey ,
84- conn_gater : ConnGater ,
85- filter_private_addrs : bool ,
85+ _conn_gater : ConnGater ,
86+ _filter_private_addrs : bool ,
8687 behaviour_fn : F ,
8788 ) -> Result < Self >
8889 where
@@ -91,7 +92,7 @@ impl<B: NetworkBehaviour> Node<B> {
9192 let mut der = key. to_sec1_der ( ) ?;
9293 let keypair = Keypair :: secp256k1_from_der ( & mut der) ?;
9394
94- let mut swarm = SwarmBuilder :: with_existing_identity ( keypair. clone ( ) )
95+ let swarm = SwarmBuilder :: with_existing_identity ( keypair. clone ( ) )
9596 . with_tokio ( )
9697 . with_tcp (
9798 Self :: default_tcp_config ( ) ,
@@ -112,20 +113,21 @@ impl<B: NetworkBehaviour> Node<B> {
112113 Ok ( Node { swarm } )
113114 }
114115
115- pub fn new_with_tcp < F > (
116- cfg : P2PConfig ,
116+ /// Creates a new node with TCP.
117+ fn new_with_tcp < F > (
118+ _cfg : P2PConfig ,
117119 key : k256:: SecretKey ,
118- conn_gater : ConnGater ,
119- filter_private_addrs : bool ,
120+ _conn_gater : ConnGater ,
121+ _filter_private_addrs : bool ,
120122 behaviour_fn : F ,
121123 ) -> Result < Self >
122124 where
123125 F : Fn ( & Keypair , relay:: client:: Behaviour ) -> B ,
124126 {
125- let mut der = key. to_sec1_der ( ) . unwrap ( ) ;
126- let keypair = Keypair :: secp256k1_from_der ( & mut der) . unwrap ( ) ;
127+ let mut der = key. to_sec1_der ( ) ? ;
128+ let keypair = Keypair :: secp256k1_from_der ( & mut der) ? ;
127129
128- let mut swarm = SwarmBuilder :: with_existing_identity ( keypair. clone ( ) )
130+ let swarm = SwarmBuilder :: with_existing_identity ( keypair. clone ( ) )
129131 . with_tokio ( )
130132 . with_tcp (
131133 Self :: default_tcp_config ( ) ,
0 commit comments