Skip to content

Commit 56ae22d

Browse files
committed
Bind ports should not use the same port numbers
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent c87c0eb commit 56ae22d

File tree

3 files changed

+81
-50
lines changed

3 files changed

+81
-50
lines changed

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +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 rand::RngCore;
26+
use rand::Rng;
2727
use stacks::chainstate::burn::ConsensusHash;
2828
use stacks::chainstate::stacks::db::StacksChainState;
2929
use stacks::chainstate::stacks::events::StacksTransactionEvent;
@@ -295,13 +295,14 @@ pub fn new_test_conf() -> Config {
295295
// publicKey: "03e2ed46873d0db820e8c6001aabc082d72b5b900b53b7a1b9714fe7bde3037b81",
296296
// stacksAddress: "ST2VHM28V9E5QCRD6C73215KAPSBKQGPWTEE5CMQT"
297297
let mut rng = rand::thread_rng();
298-
let mut buf = [0u8; 8];
299-
rng.fill_bytes(&mut buf);
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;
300301

301302
let mut conf = Config::default();
302303
conf.node.working_dir = format!(
303304
"/tmp/stacks-node-tests/integrations-neon/{}-{}",
304-
to_hex(&buf),
305+
to_hex(format!("{rpc_port}{p2p_port}").as_bytes()),
305306
get_epoch_time_secs()
306307
);
307308
conf.node.seed =
@@ -313,14 +314,11 @@ pub fn new_test_conf() -> Config {
313314

314315
conf.burnchain.epochs = Some(StacksEpoch::all(0, 0, 0));
315316

316-
let rpc_port = u16::from_be_bytes(buf[0..2].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
317-
let p2p_port = u16::from_be_bytes(buf[2..4].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
318-
319317
let localhost = "127.0.0.1";
320-
conf.node.rpc_bind = format!("{}:{}", localhost, rpc_port);
321-
conf.node.p2p_bind = format!("{}:{}", localhost, p2p_port);
322-
conf.node.data_url = format!("http://{}:{}", localhost, rpc_port);
323-
conf.node.p2p_address = format!("{}:{}", localhost, p2p_port);
318+
conf.node.rpc_bind = format!("{localhost}:{rpc_port}");
319+
conf.node.p2p_bind = format!("{localhost}:{p2p_port}");
320+
conf.node.data_url = format!("http://{localhost}:{rpc_port}");
321+
conf.node.p2p_address = format!("{localhost}:{p2p_port}");
324322
conf
325323
}
326324

@@ -344,10 +342,9 @@ pub fn set_random_binds(config: &mut Config) {
344342
.unwrap();
345343
let (rpc_port, p2p_port) = loop {
346344
let mut rng = rand::thread_rng();
347-
let mut buf = [0u8; 8];
348-
rng.fill_bytes(&mut buf);
349-
let rpc_port = u16::from_be_bytes(buf[0..2].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
350-
let p2p_port = u16::from_be_bytes(buf[2..4].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
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;
351348
if rpc_port != prior_rpc_port && p2p_port != prior_p2p_port {
352349
break (rpc_port, p2p_port);
353350
}

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use http_types::headers::AUTHORIZATION;
2929
use lazy_static::lazy_static;
3030
use libsigner::v0::messages::SignerMessage as SignerMessageV0;
3131
use libsigner::{SignerSession, StackerDBSession};
32-
use rand::RngCore;
32+
use rand::Rng;
3333
use stacks::burnchains::{MagicBytes, Txid};
3434
use stacks::chainstate::burn::db::sortdb::SortitionDB;
3535
use stacks::chainstate::burn::operations::{
@@ -3459,18 +3459,20 @@ 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";
34623463
let mut rng = rand::thread_rng();
3463-
let mut buf = [0u8; 8];
3464-
rng.fill_bytes(&mut buf);
3465-
3466-
let rpc_port = u16::from_be_bytes(buf[0..2].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
3467-
let p2p_port = u16::from_be_bytes(buf[2..4].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
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;
34683471

3469-
let localhost = "127.0.0.1";
3470-
follower_conf.node.rpc_bind = format!("{}:{}", &localhost, rpc_port);
3471-
follower_conf.node.p2p_bind = format!("{}:{}", &localhost, p2p_port);
3472-
follower_conf.node.data_url = format!("http://{}:{}", &localhost, rpc_port);
3473-
follower_conf.node.p2p_address = format!("{}:{}", &localhost, p2p_port);
3472+
follower_conf.node.rpc_bind = format!("{localhost}:{rpc_port}");
3473+
follower_conf.node.p2p_bind = format!("{localhost}:{p2p_port}");
3474+
follower_conf.node.data_url = format!("http://{localhost}:{rpc_port}");
3475+
follower_conf.node.p2p_address = format!("{localhost}:{p2p_port}");
34743476
follower_conf.node.pox_sync_sample_secs = 30;
34753477

34763478
let node_info = get_chain_info(&naka_conf);
@@ -3813,18 +3815,20 @@ fn follower_bootup_across_multiple_cycles() {
38133815
follower_conf.node.local_peer_seed = vec![0x02; 32];
38143816
follower_conf.node.miner = false;
38153817

3818+
let localhost = "127.0.0.1";
38163819
let mut rng = rand::thread_rng();
3817-
let mut buf = [0u8; 8];
3818-
rng.fill_bytes(&mut buf);
3819-
3820-
let rpc_port = u16::from_be_bytes(buf[0..2].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
3821-
let p2p_port = u16::from_be_bytes(buf[2..4].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
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;
38223827

3823-
let localhost = "127.0.0.1";
3824-
follower_conf.node.rpc_bind = format!("{}:{}", &localhost, rpc_port);
3825-
follower_conf.node.p2p_bind = format!("{}:{}", &localhost, p2p_port);
3826-
follower_conf.node.data_url = format!("http://{}:{}", &localhost, rpc_port);
3827-
follower_conf.node.p2p_address = format!("{}:{}", &localhost, p2p_port);
3828+
follower_conf.node.rpc_bind = format!("{localhost}:{rpc_port}");
3829+
follower_conf.node.p2p_bind = format!("{localhost}:{p2p_port}");
3830+
follower_conf.node.data_url = format!("http://{localhost}:{rpc_port}");
3831+
follower_conf.node.p2p_address = format!("{localhost}:{p2p_port}");
38283832
follower_conf.node.pox_sync_sample_secs = 30;
38293833

38303834
let node_info = get_chain_info(&naka_conf);

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

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use clarity::vm::costs::ExecutionCost;
1212
use clarity::vm::types::serialization::SerializationError;
1313
use clarity::vm::types::PrincipalData;
1414
use clarity::vm::{ClarityName, ClarityVersion, ContractName, Value, MAX_CALL_STACK_DEPTH};
15-
use rand::{Rng, RngCore};
15+
use rand::Rng;
1616
use rusqlite::params;
1717
use serde::Deserialize;
1818
use serde_json::json;
@@ -986,7 +986,16 @@ fn bitcoind_integration_test() {
986986
}
987987

988988
let (mut conf, miner_account) = neon_integration_test_conf();
989-
let prom_bind = format!("{}:{}", "127.0.0.1", 6000);
989+
let localhost = "127.0.0.1";
990+
let mut rng = rand::thread_rng();
991+
// Use a non-privileged port between 1024 and 65534
992+
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 {
995+
// 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}");
998+
}
990999
conf.node.prometheus_bind = Some(prom_bind.clone());
9911000

9921001
conf.burnchain.max_rbf = 1000000;
@@ -12466,18 +12475,21 @@ fn bitcoin_reorg_flap_with_follower() {
1246612475
follower_conf.node.seed = vec![0x01; 32];
1246712476
follower_conf.node.local_peer_seed = vec![0x02; 32];
1246812477

12478+
let localhost = "127.0.0.1";
1246912479
let mut rng = rand::thread_rng();
12470-
let mut buf = [0u8; 8];
12471-
rng.fill_bytes(&mut buf);
12472-
12473-
let rpc_port = u16::from_be_bytes(buf[0..2].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
12474-
let p2p_port = u16::from_be_bytes(buf[2..4].try_into().unwrap()).saturating_add(1025) - 1; // use a non-privileged port between 1024 and 65534
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;
1247512487

12476-
let localhost = "127.0.0.1";
12477-
follower_conf.node.rpc_bind = format!("{}:{}", &localhost, rpc_port);
12478-
follower_conf.node.p2p_bind = format!("{}:{}", &localhost, p2p_port);
12479-
follower_conf.node.data_url = format!("http://{}:{}", &localhost, rpc_port);
12480-
follower_conf.node.p2p_address = format!("{}:{}", &localhost, p2p_port);
12488+
follower_conf.node.rpc_bind = format!("{localhost}:{rpc_port}");
12489+
follower_conf.node.p2p_bind = format!("{localhost}:{p2p_port}");
12490+
follower_conf.node.data_url = format!("http://{localhost}:{rpc_port}");
12491+
follower_conf.node.p2p_address = format!("{localhost}:{p2p_port}");
12492+
follower_conf.node.pox_sync_sample_secs = 30;
1248112493

1248212494
let run_loop_thread = thread::spawn(move || miner_run_loop.start(None, 0));
1248312495
wait_for_runloop(&miner_blocks_processed);
@@ -12800,7 +12812,16 @@ fn listunspent_max_utxos() {
1280012812
}
1280112813

1280212814
let (mut conf, _miner_account) = neon_integration_test_conf();
12803-
let prom_bind = format!("{}:{}", "127.0.0.1", 6000);
12815+
let localhost = "127.0.0.1";
12816+
let mut rng = rand::thread_rng();
12817+
// Use a non-privileged port between 1024 and 65534
12818+
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 {
12821+
// 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}");
12824+
}
1280412825
conf.node.prometheus_bind = Some(prom_bind.clone());
1280512826

1280612827
conf.burnchain.max_rbf = 1000000;
@@ -12846,7 +12867,16 @@ fn start_stop_bitcoind() {
1284612867
}
1284712868

1284812869
let (mut conf, _miner_account) = neon_integration_test_conf();
12849-
let prom_bind = format!("{}:{}", "127.0.0.1", 6000);
12870+
let localhost = "127.0.0.1";
12871+
let mut rng = rand::thread_rng();
12872+
// Use a non-privileged port between 1024 and 65534
12873+
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 {
12876+
// 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}");
12879+
}
1285012880
conf.node.prometheus_bind = Some(prom_bind.clone());
1285112881

1285212882
conf.burnchain.max_rbf = 1000000;

0 commit comments

Comments
 (0)