Skip to content

Commit cf5fd4c

Browse files
committed
Cleanup bind ports
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 56ae22d commit cf5fd4c

File tree

3 files changed

+189
-53
lines changed

3 files changed

+189
-53
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,13 @@ pub fn new_test_conf() -> Config {
295295
// publicKey: "03e2ed46873d0db820e8c6001aabc082d72b5b900b53b7a1b9714fe7bde3037b81",
296296
// stacksAddress: "ST2VHM28V9E5QCRD6C73215KAPSBKQGPWTEE5CMQT"
297297
let mut rng = rand::thread_rng();
298-
// Use a non-privileged port between 1024 and 65534
299-
let rpc_port: u16 = rng.gen_range(1024..65533);
300-
let p2p_port = rpc_port + 1;
298+
let (rpc_port, p2p_port) = loop {
299+
let a = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
300+
let b = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
301+
if a != b {
302+
break (a, b);
303+
}
304+
};
301305

302306
let mut conf = Config::default();
303307
conf.node.working_dir = format!(
@@ -324,6 +328,7 @@ pub fn new_test_conf() -> Config {
324328

325329
/// Randomly change the config's network ports to new ports.
326330
pub fn set_random_binds(config: &mut Config) {
331+
let mut rng = rand::thread_rng();
327332
let prior_rpc_port: u16 = config
328333
.node
329334
.rpc_bind
@@ -341,12 +346,15 @@ pub fn set_random_binds(config: &mut Config) {
341346
.parse()
342347
.unwrap();
343348
let (rpc_port, p2p_port) = loop {
344-
let mut rng = rand::thread_rng();
345-
// Use a non-privileged port between 1024 and 65534
346-
let rpc_port: u16 = rng.gen_range(1024..65533);
347-
let p2p_port = rpc_port + 1;
348-
if rpc_port != prior_rpc_port && p2p_port != prior_p2p_port {
349-
break (rpc_port, p2p_port);
349+
let a = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
350+
let b = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
351+
if a != b
352+
&& a != prior_rpc_port
353+
&& a != prior_p2p_port
354+
&& b != prior_rpc_port
355+
&& b != prior_p2p_port
356+
{
357+
break (a, b);
350358
}
351359
};
352360
let localhost = "127.0.0.1";

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

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,16 +3459,37 @@ fn follower_bootup() {
34593459
follower_conf.node.seed = vec![0x01; 32];
34603460
follower_conf.node.local_peer_seed = vec![0x02; 32];
34613461

3462-
let localhost = "127.0.0.1";
34633462
let mut rng = rand::thread_rng();
3464-
// Use a non-privileged port between 1024 and 65534
3465-
let mut rpc_port: u16 = rng.gen_range(1024..65533);
3466-
while format!("{localhost}:{rpc_port}") == naka_conf.node.rpc_bind {
3467-
// We should NOT match the miner's rpc bind and subsequently p2p port
3468-
rpc_port = rng.gen_range(1024..65533);
3469-
}
3470-
let p2p_port = rpc_port + 1;
3463+
let prior_rpc_port: u16 = naka_conf
3464+
.node
3465+
.rpc_bind
3466+
.split(":")
3467+
.last()
3468+
.unwrap()
3469+
.parse()
3470+
.unwrap();
3471+
let prior_p2p_port: u16 = naka_conf
3472+
.node
3473+
.p2p_bind
3474+
.split(":")
3475+
.last()
3476+
.unwrap()
3477+
.parse()
3478+
.unwrap();
3479+
let (rpc_port, p2p_port) = loop {
3480+
let a = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
3481+
let b = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
3482+
if a != b
3483+
&& a != prior_rpc_port
3484+
&& a != prior_p2p_port
3485+
&& b != prior_rpc_port
3486+
&& b != prior_p2p_port
3487+
{
3488+
break (a, b);
3489+
}
3490+
};
34713491

3492+
let localhost = "127.0.0.1";
34723493
follower_conf.node.rpc_bind = format!("{localhost}:{rpc_port}");
34733494
follower_conf.node.p2p_bind = format!("{localhost}:{p2p_port}");
34743495
follower_conf.node.data_url = format!("http://{localhost}:{rpc_port}");
@@ -3815,16 +3836,37 @@ fn follower_bootup_across_multiple_cycles() {
38153836
follower_conf.node.local_peer_seed = vec![0x02; 32];
38163837
follower_conf.node.miner = false;
38173838

3818-
let localhost = "127.0.0.1";
38193839
let mut rng = rand::thread_rng();
3820-
// Use a non-privileged port between 1024 and 65534
3821-
let mut rpc_port: u16 = rng.gen_range(1024..65533);
3822-
while format!("{localhost}:{rpc_port}") == naka_conf.node.rpc_bind {
3823-
// We should NOT match the miner's rpc bind and subsequently p2p port
3824-
rpc_port = rng.gen_range(1024..65533);
3825-
}
3826-
let p2p_port = rpc_port + 1;
3840+
let prior_rpc_port: u16 = naka_conf
3841+
.node
3842+
.rpc_bind
3843+
.split(":")
3844+
.last()
3845+
.unwrap()
3846+
.parse()
3847+
.unwrap();
3848+
let prior_p2p_port: u16 = naka_conf
3849+
.node
3850+
.p2p_bind
3851+
.split(":")
3852+
.last()
3853+
.unwrap()
3854+
.parse()
3855+
.unwrap();
3856+
let (rpc_port, p2p_port) = loop {
3857+
let a = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
3858+
let b = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
3859+
if a != b
3860+
&& a != prior_rpc_port
3861+
&& a != prior_p2p_port
3862+
&& b != prior_rpc_port
3863+
&& b != prior_p2p_port
3864+
{
3865+
break (a, b);
3866+
}
3867+
};
38273868

3869+
let localhost = "127.0.0.1";
38283870
follower_conf.node.rpc_bind = format!("{localhost}:{rpc_port}");
38293871
follower_conf.node.p2p_bind = format!("{localhost}:{p2p_port}");
38303872
follower_conf.node.data_url = format!("http://{localhost}:{rpc_port}");

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

Lines changed: 114 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -986,16 +986,31 @@ fn bitcoind_integration_test() {
986986
}
987987

988988
let (mut conf, miner_account) = neon_integration_test_conf();
989-
let localhost = "127.0.0.1";
990989
let mut rng = rand::thread_rng();
991-
// Use a non-privileged port between 1024 and 65534
992990
let mut prom_port = 6000;
993-
let mut prom_bind = format!("{localhost}:{prom_port}");
994-
while prom_bind == conf.node.rpc_bind || prom_bind == conf.node.p2p_bind {
991+
let prior_rpc_port: u16 = conf
992+
.node
993+
.rpc_bind
994+
.split(":")
995+
.last()
996+
.unwrap()
997+
.parse()
998+
.unwrap();
999+
let prior_p2p_port: u16 = conf
1000+
.node
1001+
.p2p_bind
1002+
.split(":")
1003+
.last()
1004+
.unwrap()
1005+
.parse()
1006+
.unwrap();
1007+
// Use a non-privileged port between 1024 and 65534
1008+
while prom_port == prior_rpc_port || prom_port == prior_p2p_port {
9951009
// We should NOT match the miner's rpc or p2p binds
996-
prom_port = rng.gen_range(1024..65533);
997-
prom_bind = format!("{localhost}:{prom_port}");
1010+
prom_port = rng.gen_range(1024..u16::MAX);
9981011
}
1012+
let localhost = "127.0.0.1";
1013+
let prom_bind = format!("{localhost}:{prom_port}");
9991014
conf.node.prometheus_bind = Some(prom_bind.clone());
10001015

10011016
conf.burnchain.max_rbf = 1000000;
@@ -12475,16 +12490,37 @@ fn bitcoin_reorg_flap_with_follower() {
1247512490
follower_conf.node.seed = vec![0x01; 32];
1247612491
follower_conf.node.local_peer_seed = vec![0x02; 32];
1247712492

12478-
let localhost = "127.0.0.1";
1247912493
let mut rng = rand::thread_rng();
12480-
// Use a non-privileged port between 1024 and 65534
12481-
let mut rpc_port: u16 = rng.gen_range(1024..65533);
12482-
while format!("{localhost}:{rpc_port}") == conf.node.rpc_bind {
12483-
// We should NOT match the miner's rpc bind and subsequently p2p port
12484-
rpc_port = rng.gen_range(1024..65533);
12485-
}
12486-
let p2p_port = rpc_port + 1;
12494+
let prior_rpc_port: u16 = conf
12495+
.node
12496+
.rpc_bind
12497+
.split(":")
12498+
.last()
12499+
.unwrap()
12500+
.parse()
12501+
.unwrap();
12502+
let prior_p2p_port: u16 = conf
12503+
.node
12504+
.p2p_bind
12505+
.split(":")
12506+
.last()
12507+
.unwrap()
12508+
.parse()
12509+
.unwrap();
12510+
let (rpc_port, p2p_port) = loop {
12511+
let a = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
12512+
let b = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
12513+
if a != b
12514+
&& a != prior_rpc_port
12515+
&& a != prior_p2p_port
12516+
&& b != prior_rpc_port
12517+
&& b != prior_p2p_port
12518+
{
12519+
break (a, b);
12520+
}
12521+
};
1248712522

12523+
let localhost = "127.0.0.1";
1248812524
follower_conf.node.rpc_bind = format!("{localhost}:{rpc_port}");
1248912525
follower_conf.node.p2p_bind = format!("{localhost}:{p2p_port}");
1249012526
follower_conf.node.data_url = format!("http://{localhost}:{rpc_port}");
@@ -12670,11 +12706,31 @@ fn mock_miner_replay() {
1267012706
follower_conf.node.local_peer_seed = vec![0x02; 32];
1267112707

1267212708
let mut rng = rand::thread_rng();
12673-
12709+
let prior_rpc_port: u16 = conf
12710+
.node
12711+
.rpc_bind
12712+
.split(":")
12713+
.last()
12714+
.unwrap()
12715+
.parse()
12716+
.unwrap();
12717+
let prior_p2p_port: u16 = conf
12718+
.node
12719+
.p2p_bind
12720+
.split(":")
12721+
.last()
12722+
.unwrap()
12723+
.parse()
12724+
.unwrap();
1267412725
let (rpc_port, p2p_port) = loop {
1267512726
let a = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
1267612727
let b = rng.gen_range(1024..u16::MAX); // use a non-privileged port between 1024 and 65534
12677-
if a != b {
12728+
if a != b
12729+
&& a != prior_rpc_port
12730+
&& a != prior_p2p_port
12731+
&& b != prior_rpc_port
12732+
&& b != prior_p2p_port
12733+
{
1267812734
break (a, b);
1267912735
}
1268012736
};
@@ -12812,16 +12868,31 @@ fn listunspent_max_utxos() {
1281212868
}
1281312869

1281412870
let (mut conf, _miner_account) = neon_integration_test_conf();
12815-
let localhost = "127.0.0.1";
1281612871
let mut rng = rand::thread_rng();
12817-
// Use a non-privileged port between 1024 and 65534
1281812872
let mut prom_port = 6000;
12819-
let mut prom_bind = format!("{localhost}:{prom_port}");
12820-
while prom_bind == conf.node.rpc_bind || prom_bind == conf.node.p2p_bind {
12873+
let prior_rpc_port: u16 = conf
12874+
.node
12875+
.rpc_bind
12876+
.split(":")
12877+
.last()
12878+
.unwrap()
12879+
.parse()
12880+
.unwrap();
12881+
let prior_p2p_port: u16 = conf
12882+
.node
12883+
.p2p_bind
12884+
.split(":")
12885+
.last()
12886+
.unwrap()
12887+
.parse()
12888+
.unwrap();
12889+
// Use a non-privileged port between 1024 and 65534
12890+
while prom_port == prior_rpc_port || prom_port == prior_p2p_port {
1282112891
// We should NOT match the miner's rpc or p2p binds
12822-
prom_port = rng.gen_range(1024..65533);
12823-
prom_bind = format!("{localhost}:{prom_port}");
12892+
prom_port = rng.gen_range(1024..u16::MAX);
1282412893
}
12894+
let localhost = "127.0.0.1";
12895+
let prom_bind = format!("{localhost}:{prom_port}");
1282512896
conf.node.prometheus_bind = Some(prom_bind.clone());
1282612897

1282712898
conf.burnchain.max_rbf = 1000000;
@@ -12867,16 +12938,31 @@ fn start_stop_bitcoind() {
1286712938
}
1286812939

1286912940
let (mut conf, _miner_account) = neon_integration_test_conf();
12870-
let localhost = "127.0.0.1";
1287112941
let mut rng = rand::thread_rng();
12872-
// Use a non-privileged port between 1024 and 65534
1287312942
let mut prom_port = 6000;
12874-
let mut prom_bind = format!("{localhost}:{prom_port}");
12875-
while prom_bind == conf.node.rpc_bind || prom_bind == conf.node.p2p_bind {
12943+
let prior_rpc_port: u16 = conf
12944+
.node
12945+
.rpc_bind
12946+
.split(":")
12947+
.last()
12948+
.unwrap()
12949+
.parse()
12950+
.unwrap();
12951+
let prior_p2p_port: u16 = conf
12952+
.node
12953+
.p2p_bind
12954+
.split(":")
12955+
.last()
12956+
.unwrap()
12957+
.parse()
12958+
.unwrap();
12959+
// Use a non-privileged port between 1024 and 65534
12960+
while prom_port == prior_rpc_port || prom_port == prior_p2p_port {
1287612961
// We should NOT match the miner's rpc or p2p binds
12877-
prom_port = rng.gen_range(1024..65533);
12878-
prom_bind = format!("{localhost}:{prom_port}");
12962+
prom_port = rng.gen_range(1024..u16::MAX);
1287912963
}
12964+
let localhost = "127.0.0.1";
12965+
let prom_bind = format!("{localhost}:{prom_port}");
1288012966
conf.node.prometheus_bind = Some(prom_bind.clone());
1288112967

1288212968
conf.burnchain.max_rbf = 1000000;

0 commit comments

Comments
 (0)