Skip to content

Commit 84af701

Browse files
committed
Allow to specify if the node should listen other connection in Connect mode
1 parent b8c465e commit 84af701

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/lib.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ pub enum P2P {
6161
/// the node open a p2p port
6262
Yes,
6363
/// 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),
6667
}
6768

6869
/// All the possible error in this crate
@@ -140,13 +141,15 @@ impl BitcoinD {
140141
let args = vec![p2p_arg];
141142
(args, Some(p2p_socket))
142143
}
143-
P2P::Connect(other_node_url) => {
144+
P2P::Connect(other_node_url, listen) => {
144145
let p2p_port = get_available_port()?;
145146
let p2p_socket = SocketAddrV4::new(LOCAL_IP, p2p_port);
146147
let p2p_arg = format!("-port={}", p2p_port);
147148
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+
}
150153
(args, Some(p2p_socket))
151154
}
152155
};
@@ -212,8 +215,8 @@ impl BitcoinD {
212215
}
213216

214217
/// 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))
217220
}
218221

219222
/// Stop the node, waiting correct process termination
@@ -329,7 +332,7 @@ mod test {
329332
let bitcoind = BitcoinD::with_conf(&exe, &conf).unwrap();
330333
assert_eq!(bitcoind.client.get_peer_info().unwrap().len(), 0);
331334
let other_conf = Conf {
332-
p2p: bitcoind.p2p_connect().unwrap(),
335+
p2p: bitcoind.p2p_connect(false).unwrap(),
333336
..Default::default()
334337
};
335338
let other_bitcoind = BitcoinD::with_conf(&exe, &other_conf).unwrap();
@@ -347,14 +350,14 @@ mod test {
347350

348351
// Create Node 2 connected Node 1
349352
let conf_node2 = Conf {
350-
p2p: node1.p2p_connect().unwrap(),
353+
p2p: node1.p2p_connect(true).unwrap(),
351354
..Default::default()
352355
};
353356
let node2 = BitcoinD::with_conf(exe_path(), &conf_node2).unwrap();
354357

355358
// Create Node 3 Connected To Node 2
356359
let conf_node3 = Conf {
357-
p2p: node2.p2p_connect().unwrap(),
360+
p2p: node2.p2p_connect(false).unwrap(),
358361
..Default::default()
359362
};
360363
let node3 = BitcoinD::with_conf(exe_path(), &conf_node3).unwrap();
@@ -367,7 +370,7 @@ mod test {
367370
// Peers found
368371
assert!(node1_peers.len() >= 1);
369372
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");
371374
}
372375

373376
fn exe_path() -> String {

0 commit comments

Comments
 (0)