1
+ use eyre:: Result ;
1
2
use libp2p:: futures:: StreamExt ;
2
3
use libp2p:: gossipsub:: {
3
4
Message , MessageAcceptance , MessageId , PublishError , SubscriptionError , TopicHash ,
@@ -17,6 +18,7 @@ use super::{DriaBehaviour, DriaBehaviourEvent, P2P_KADEMLIA_PROTOCOL, P2P_PROTOC
17
18
18
19
/// Underlying libp2p client.
19
20
pub struct P2PClient {
21
+ /// `Swarm` instance, everything is accesses through this one.
20
22
swarm : Swarm < DriaBehaviour > ,
21
23
/// Peer count for (All, Mesh).
22
24
peer_count : ( usize , usize ) ,
@@ -36,7 +38,7 @@ impl P2PClient {
36
38
keypair : Keypair ,
37
39
listen_addr : Multiaddr ,
38
40
available_nodes : & AvailableNodes ,
39
- ) -> Result < Self , String > {
41
+ ) -> Result < Self > {
40
42
// this is our peerId
41
43
let node_peerid = keypair. public ( ) . to_peer_id ( ) ;
42
44
log:: info!( "Compute node peer address: {}" , node_peerid) ;
@@ -47,13 +49,10 @@ impl P2PClient {
47
49
tcp:: Config :: default ( ) ,
48
50
noise:: Config :: new,
49
51
yamux:: Config :: default,
50
- )
51
- . map_err ( |e| e. to_string ( ) ) ?
52
+ ) ?
52
53
. with_quic ( )
53
- . with_relay_client ( noise:: Config :: new, yamux:: Config :: default)
54
- . map_err ( |e| e. to_string ( ) ) ?
55
- . with_behaviour ( |key, relay_behavior| Ok ( DriaBehaviour :: new ( key, relay_behavior) ) )
56
- . map_err ( |e| e. to_string ( ) ) ?
54
+ . with_relay_client ( noise:: Config :: new, yamux:: Config :: default) ?
55
+ . with_behaviour ( |key, relay_behavior| Ok ( DriaBehaviour :: new ( key, relay_behavior) ) ) ?
57
56
. with_swarm_config ( |c| {
58
57
c. with_idle_connection_timeout ( Duration :: from_secs ( IDLE_CONNECTION_TIMEOUT_SECS ) )
59
58
} )
@@ -76,7 +75,7 @@ impl P2PClient {
76
75
_ => None ,
77
76
} ) {
78
77
log:: info!( "Dialling peer: {}" , addr) ;
79
- swarm. dial ( addr. clone ( ) ) . map_err ( |e| e . to_string ( ) ) ?;
78
+ swarm. dial ( addr. clone ( ) ) ?;
80
79
log:: info!( "Adding {} to Kademlia routing table" , addr) ;
81
80
swarm
82
81
. behaviour_mut ( )
@@ -94,24 +93,18 @@ impl P2PClient {
94
93
. behaviour_mut ( )
95
94
. kademlia
96
95
. get_closest_peers ( random_peer) ;
97
- swarm
98
- . behaviour_mut ( )
99
- . kademlia
100
- . bootstrap ( )
101
- . map_err ( |e| e. to_string ( ) ) ?;
96
+ swarm. behaviour_mut ( ) . kademlia . bootstrap ( ) ?;
102
97
103
98
// listen on all interfaces for incoming connections
104
99
log:: info!( "Listening p2p network on: {}" , listen_addr) ;
105
- swarm. listen_on ( listen_addr) . map_err ( |e| e . to_string ( ) ) ?;
100
+ swarm. listen_on ( listen_addr) ?;
106
101
107
102
log:: info!(
108
103
"Listening to relay nodes: {:#?}" ,
109
104
available_nodes. relay_nodes
110
105
) ;
111
106
for addr in & available_nodes. relay_nodes {
112
- swarm
113
- . listen_on ( addr. clone ( ) . with ( Protocol :: P2pCircuit ) )
114
- . map_err ( |e| e. to_string ( ) ) ?;
107
+ swarm. listen_on ( addr. clone ( ) . with ( Protocol :: P2pCircuit ) ) ?;
115
108
}
116
109
117
110
Ok ( Self {
@@ -138,6 +131,8 @@ impl P2PClient {
138
131
}
139
132
140
133
/// Publish a message to a topic.
134
+ ///
135
+ /// Returns the message ID.
141
136
pub fn publish (
142
137
& mut self ,
143
138
topic_name : & str ,
@@ -168,7 +163,7 @@ impl P2PClient {
168
163
msg_id : & MessageId ,
169
164
propagation_source : & PeerId ,
170
165
acceptance : MessageAcceptance ,
171
- ) -> Result < ( ) , PublishError > {
166
+ ) -> Result < ( ) > {
172
167
log:: trace!( "Validating message ({}): {:?}" , msg_id, acceptance) ;
173
168
174
169
let msg_was_in_cache = self
@@ -240,6 +235,7 @@ impl P2PClient {
240
235
/// - For Kademlia, we check the kademlia protocol and then add the address to the Kademlia routing table.
241
236
fn handle_identify_event ( & mut self , peer_id : PeerId , info : identify:: Info ) {
242
237
// we only care about the observed address, although there may be other addresses at `info.listen_addrs`
238
+ // TODO: this may be wrong
243
239
let addr = info. observed_addr ;
244
240
245
241
// check protocol string
0 commit comments