Skip to content

Commit cc89b14

Browse files
committed
Fix occasional port bind error happening in mock mining by checking if in feature == testing and reserving test observer port
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent a2dcd4c commit cc89b14

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

stackslib/src/net/poll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl NetworkState {
8989
}
9090

9191
fn bind_address(addr: &SocketAddr) -> Result<mio_net::TcpListener, net_error> {
92-
if !cfg!(test) {
92+
if !cfg!(test) && !cfg!(feature = "testing") {
9393
mio_net::TcpListener::bind(addr).map_err(|e| {
9494
error!("Failed to bind to {:?}: {:?}", addr, e);
9595
net_error::BindError

testnet/stacks-node/src/tests/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use clarity::vm::events::STXEventType;
2323
use clarity::vm::types::PrincipalData;
2424
use clarity::vm::{ClarityName, ClarityVersion, ContractName, Value};
2525
use lazy_static::lazy_static;
26+
use neon_integrations::test_observer::EVENT_OBSERVER_PORT;
2627
use rand::Rng;
2728
use stacks::chainstate::burn::ConsensusHash;
2829
use stacks::chainstate::stacks::db::StacksChainState;
@@ -109,11 +110,15 @@ pub fn gen_random_port() -> u16 {
109110
let mut rng = rand::thread_rng();
110111
let range_len = (1024..u16::MAX).len();
111112
loop {
113+
// Note it needs to be +1 because we reserve one port for the event observer
112114
assert!(
113-
USED_PORTS.lock().unwrap().len() < range_len,
115+
USED_PORTS.lock().unwrap().len() + 1 < range_len,
114116
"No more available ports"
115117
);
116118
let port = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
119+
if port == EVENT_OBSERVER_PORT {
120+
continue;
121+
}
117122
if insert_new_port(port) {
118123
return port;
119124
}

testnet/stacks-node/src/tests/nakamoto_integrations.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8132,16 +8132,16 @@ fn mock_mining() {
81328132
let send_amt = 100;
81338133
let send_fee = 180;
81348134

8135-
let node_1_rpc = 51024;
8136-
let node_1_p2p = 51023;
8137-
let node_2_rpc = 51026;
8138-
let node_2_p2p = 51025;
8135+
let node_1_rpc = gen_random_port();
8136+
let node_1_p2p = gen_random_port();
8137+
let node_2_rpc = gen_random_port();
8138+
let node_2_p2p = gen_random_port();
81398139

81408140
let localhost = "127.0.0.1";
8141-
naka_conf.node.rpc_bind = format!("{}:{}", localhost, node_1_rpc);
8142-
naka_conf.node.p2p_bind = format!("{}:{}", localhost, node_1_p2p);
8143-
naka_conf.node.data_url = format!("http://{}:{}", localhost, node_1_rpc);
8144-
naka_conf.node.p2p_address = format!("{}:{}", localhost, node_1_p2p);
8141+
naka_conf.node.rpc_bind = format!("{localhost}:{node_1_rpc}");
8142+
naka_conf.node.p2p_bind = format!("{localhost}:{node_1_p2p}");
8143+
naka_conf.node.data_url = format!("http://{localhost}:{node_1_rpc}");
8144+
naka_conf.node.p2p_address = format!("{localhost}:{node_1_p2p}");
81458145
let http_origin = format!("http://{}", &naka_conf.node.rpc_bind);
81468146

81478147
naka_conf.add_initial_balance(
@@ -8221,10 +8221,10 @@ fn mock_mining() {
82218221
follower_conf.node.seed = vec![0x01; 32];
82228222
follower_conf.node.local_peer_seed = vec![0x02; 32];
82238223

8224-
follower_conf.node.rpc_bind = format!("{}:{}", localhost, node_2_rpc);
8225-
follower_conf.node.p2p_bind = format!("{}:{}", localhost, node_2_p2p);
8226-
follower_conf.node.data_url = format!("http://{}:{}", localhost, node_2_rpc);
8227-
follower_conf.node.p2p_address = format!("{}:{}", localhost, node_2_p2p);
8224+
follower_conf.node.rpc_bind = format!("{localhost}:{node_2_rpc}");
8225+
follower_conf.node.p2p_bind = format!("{localhost}:{node_2_p2p}");
8226+
follower_conf.node.data_url = format!("http://{localhost}:{node_2_rpc}");
8227+
follower_conf.node.p2p_address = format!("{localhost}:{node_2_p2p}");
82288228

82298229
let node_info = get_chain_info(&naka_conf);
82308230
follower_conf.node.add_bootstrap_node(

0 commit comments

Comments
 (0)