@@ -61,8 +61,9 @@ pub enum P2P {
61
61
/// the node open a p2p port
62
62
Yes ,
63
63
/// The node open a p2p port and also connects to the url given as parameter, it's handy to
64
- /// initialize this with [BitcoinD::p2p_connect] of another node.
65
- Connect ( SocketAddrV4 ) ,
64
+ /// initialize this with [BitcoinD::p2p_connect] of another node. The `bool` parameter indicates
65
+ /// if the node can accept connection too.
66
+ Connect ( SocketAddrV4 , bool ) ,
66
67
}
67
68
68
69
/// All the possible error in this crate
@@ -140,13 +141,15 @@ impl BitcoinD {
140
141
let args = vec ! [ p2p_arg] ;
141
142
( args, Some ( p2p_socket) )
142
143
}
143
- P2P :: Connect ( other_node_url) => {
144
+ P2P :: Connect ( other_node_url, listen ) => {
144
145
let p2p_port = get_available_port ( ) ?;
145
146
let p2p_socket = SocketAddrV4 :: new ( LOCAL_IP , p2p_port) ;
146
147
let p2p_arg = format ! ( "-port={}" , p2p_port) ;
147
148
let connect = format ! ( "-connect={}" , other_node_url) ;
148
- let listen = "-listen=1" . to_string ( ) ; // With connect specified listen is not defaulted to 1
149
- let args = vec ! [ p2p_arg, connect, listen] ;
149
+ let mut args = vec ! [ p2p_arg, connect] ;
150
+ if listen {
151
+ args. push ( "-listen=1" . to_string ( ) )
152
+ }
150
153
( args, Some ( p2p_socket) )
151
154
}
152
155
} ;
@@ -212,8 +215,8 @@ impl BitcoinD {
212
215
}
213
216
214
217
/// Returns the [P2P] enum to connect to this node p2p port
215
- pub fn p2p_connect ( & self ) -> Option < P2P > {
216
- self . params . p2p_socket . map ( P2P :: Connect )
218
+ pub fn p2p_connect ( & self , listen : bool ) -> Option < P2P > {
219
+ self . params . p2p_socket . map ( |s| P2P :: Connect ( s , listen ) )
217
220
}
218
221
219
222
/// Stop the node, waiting correct process termination
@@ -329,7 +332,7 @@ mod test {
329
332
let bitcoind = BitcoinD :: with_conf ( & exe, & conf) . unwrap ( ) ;
330
333
assert_eq ! ( bitcoind. client. get_peer_info( ) . unwrap( ) . len( ) , 0 ) ;
331
334
let other_conf = Conf {
332
- p2p : bitcoind. p2p_connect ( ) . unwrap ( ) ,
335
+ p2p : bitcoind. p2p_connect ( false ) . unwrap ( ) ,
333
336
..Default :: default ( )
334
337
} ;
335
338
let other_bitcoind = BitcoinD :: with_conf ( & exe, & other_conf) . unwrap ( ) ;
@@ -347,14 +350,14 @@ mod test {
347
350
348
351
// Create Node 2 connected Node 1
349
352
let conf_node2 = Conf {
350
- p2p : node1. p2p_connect ( ) . unwrap ( ) ,
353
+ p2p : node1. p2p_connect ( true ) . unwrap ( ) ,
351
354
..Default :: default ( )
352
355
} ;
353
356
let node2 = BitcoinD :: with_conf ( exe_path ( ) , & conf_node2) . unwrap ( ) ;
354
357
355
358
// Create Node 3 Connected To Node 2
356
359
let conf_node3 = Conf {
357
- p2p : node2. p2p_connect ( ) . unwrap ( ) ,
360
+ p2p : node2. p2p_connect ( false ) . unwrap ( ) ,
358
361
..Default :: default ( )
359
362
} ;
360
363
let node3 = BitcoinD :: with_conf ( exe_path ( ) , & conf_node3) . unwrap ( ) ;
@@ -367,7 +370,7 @@ mod test {
367
370
// Peers found
368
371
assert ! ( node1_peers. len( ) >= 1 ) ;
369
372
assert ! ( node2_peers. len( ) >= 1 ) ;
370
- assert ! ( node3_peers. len( ) >= 1 ) ;
373
+ assert_eq ! ( node3_peers. len( ) , 1 , "listen false but more than 1 peer" ) ;
371
374
}
372
375
373
376
fn exe_path ( ) -> String {
0 commit comments