Skip to content

Commit 9a66494

Browse files
committed
test: make regtest run in parallel
1 parent 5f0bdaf commit 9a66494

File tree

3 files changed

+72
-33
lines changed

3 files changed

+72
-33
lines changed

stacks-common/src/util/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ pub fn get_epoch_time_ms() -> u128 {
7777
since_the_epoch.as_millis()
7878
}
7979

80+
#[cfg(any(test, feature = "testing"))]
81+
pub fn get_epoch_time_nanos() -> u128 {
82+
let start = SystemTime::now();
83+
let since_the_epoch = start
84+
.duration_since(UNIX_EPOCH)
85+
.expect("Time went backwards");
86+
since_the_epoch.as_nanos()
87+
}
88+
8089
pub fn sleep_ms(millis: u64) {
8190
let t = time::Duration::from_millis(millis);
8291
thread::sleep(t);

testnet/stacks-node/src/burnchains/bitcoin_regtest_controller.rs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,7 +2850,11 @@ mod tests {
28502850
use crate::Keychain;
28512851

28522852
mod utils {
2853+
use std::net::TcpListener;
2854+
28532855
use super::*;
2856+
use crate::tests::bitcoin_regtest::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2857+
use crate::util::get_epoch_time_nanos;
28542858

28552859
pub fn create_config() -> Config {
28562860
let mut config = Config::default();
@@ -2859,6 +2863,22 @@ mod tests {
28592863
config.burnchain.password = Some(String::from("12345"));
28602864
// overriding default "0.0.0.0" because doesn't play nicely on Windows.
28612865
config.burnchain.peer_host = String::from("127.0.0.1");
2866+
// avoiding peer port biding to reduce the number of ports to bind to.
2867+
config.burnchain.peer_port = BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2868+
2869+
//Ask the OS for a free port. Not guaranteed to stay free,
2870+
//after TcpListner is dropped, but good enough for testing
2871+
//and starting bitcoind right after config is created
2872+
let tmp_listener =
2873+
TcpListener::bind("127.0.0.1:0").expect("Failed to bind to get a free port");
2874+
let port = tmp_listener.local_addr().unwrap().port();
2875+
2876+
config.burnchain.rpc_port = port;
2877+
2878+
let now = get_epoch_time_nanos();
2879+
let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
2880+
config.node.working_dir = dir;
2881+
28622882
config
28632883
}
28642884

@@ -2896,7 +2916,7 @@ mod tests {
28962916
key_vtxindex: 1, // 0x0001
28972917
memo: vec![11], // 0x5a >> 3
28982918

2899-
burn_fee: 110_000, //relevant for fee calculation when sending the tx
2919+
burn_fee: 110_000, //relevant for fee calculation when sending the tx
29002920
input: (Txid([0x00; 32]), 0),
29012921
burn_parent_modulus: 2, // 0x5a & 0b111
29022922

@@ -2907,7 +2927,7 @@ mod tests {
29072927
],
29082928

29092929
treatment: vec![],
2910-
sunset_burn: 5_500, //relevant for fee calculation when sending the tx
2930+
sunset_burn: 5_500, //relevant for fee calculation when sending the tx
29112931

29122932
txid: Txid([0x00; 32]),
29132933
vtxindex: 0,
@@ -3141,14 +3161,14 @@ mod tests {
31413161

31423162
let mut config = utils::create_config();
31433163
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3144-
3164+
31453165
let mut btcd_controller = BitcoinCoreController::new(config.clone());
31463166
btcd_controller
31473167
.start_bitcoind()
31483168
.expect("bitcoind should be started!");
31493169

31503170
let btc_controller = BitcoinRegtestController::new(config.clone(), None);
3151-
3171+
31523172
btc_controller.bootstrap_chain(100);
31533173
let utxos = btc_controller.get_all_utxos(&miner_pubkey);
31543174
assert_eq!(0, utxos.len());
@@ -3160,11 +3180,13 @@ mod tests {
31603180
assert_eq!(5_000_000_000, utxos[0].amount);
31613181

31623182
btc_controller.build_next_block(1);
3163-
let utxos = btc_controller.get_all_utxos(&miner_pubkey);
3183+
let mut utxos = btc_controller.get_all_utxos(&miner_pubkey);
3184+
utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
3185+
31643186
assert_eq!(2, utxos.len());
3165-
assert_eq!(101, utxos[0].confirmations);
3187+
assert_eq!(102, utxos[0].confirmations);
31663188
assert_eq!(5_000_000_000, utxos[0].amount);
3167-
assert_eq!(102, utxos[1].confirmations);
3189+
assert_eq!(101, utxos[1].confirmations);
31683190
assert_eq!(5_000_000_000, utxos[1].amount);
31693191
}
31703192

@@ -3175,15 +3197,15 @@ mod tests {
31753197

31763198
let mut config = utils::create_config();
31773199
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3178-
3200+
31793201
let mut btcd_controller = BitcoinCoreController::new(config.clone());
31803202
btcd_controller
31813203
.start_bitcoind()
31823204
.expect("bitcoind should be started!");
31833205

31843206
let btc_controller = BitcoinRegtestController::new(config.clone(), None);
31853207
btc_controller.bootstrap_chain(101); // one utxo exists
3186-
3208+
31873209
let utxos = btc_controller.get_all_utxos(&other_pubkey);
31883210
assert_eq!(0, utxos.len());
31893211
}
@@ -3203,7 +3225,7 @@ mod tests {
32033225
let btc_controller = BitcoinRegtestController::new(config.clone(), None);
32043226
btc_controller.bootstrap_chain(101);
32053227

3206-
let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 0);
3228+
let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 101);
32073229

32083230
let uxto_set = utxos.expect("Shouldn't be None!");
32093231
assert_eq!(1, uxto_set.num_utxos());
@@ -3254,13 +3276,7 @@ mod tests {
32543276
btc_controller.bootstrap_chain(101); // one utxo exists
32553277

32563278
let other_pubkey = utils::create_miner2_pubkey();
3257-
let utxos = btc_controller.get_utxos(
3258-
StacksEpochId::Epoch31,
3259-
&other_pubkey,
3260-
1,
3261-
None,
3262-
0,
3263-
);
3279+
let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &other_pubkey, 1, None, 0);
32643280
assert!(
32653281
utxos.is_none(),
32663282
"None because utxos for other pubkey don't exist"
@@ -3306,7 +3322,7 @@ mod tests {
33063322

33073323
let mut config = utils::create_config();
33083324
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3309-
3325+
33103326
let mut btcd_controller = BitcoinCoreController::new(config.clone());
33113327
btcd_controller
33123328
.start_bitcoind()
@@ -3331,14 +3347,15 @@ mod tests {
33313347
}
33323348

33333349
#[test]
3334-
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_not_confirmed() {
3350+
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_not_confirmed(
3351+
) {
33353352
let keychain = utils::create_keychain();
33363353
let miner_pubkey = keychain.get_pub_key();
33373354
let mut signer = keychain.generate_op_signer();
33383355

33393356
let mut config = utils::create_config();
33403357
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3341-
3358+
33423359
let mut btcd_controller = BitcoinCoreController::new(config.clone());
33433360
btcd_controller
33443361
.start_bitcoind()
@@ -3372,14 +3389,15 @@ mod tests {
33723389
}
33733390

33743391
#[test]
3375-
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_is_confirmed() {
3392+
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_is_confirmed()
3393+
{
33763394
let keychain = utils::create_keychain();
33773395
let miner_pubkey = keychain.get_pub_key();
33783396
let mut signer = keychain.generate_op_signer();
33793397

33803398
let mut config = utils::create_config();
33813399
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3382-
3400+
33833401
let mut btcd_controller = BitcoinCoreController::new(config.clone());
33843402
btcd_controller
33853403
.start_bitcoind()
@@ -3398,7 +3416,9 @@ mod tests {
33983416
.expect("At first, building leader block commit should work");
33993417

34003418
let ser = SerializedTx::new(first_tx_ok);
3401-
btc_controller.send_transaction(ser).expect("Tx should be sent to the burnchain!");
3419+
btc_controller
3420+
.send_transaction(ser)
3421+
.expect("Tx should be sent to the burnchain!");
34023422
btc_controller.build_next_block(1); // Now tx is confirmed
34033423

34043424
// re-submitting same commit while previous it is confirmed by the burnchain
@@ -3424,7 +3444,7 @@ mod tests {
34243444

34253445
let mut config = utils::create_config();
34263446
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3427-
3447+
34283448
let mut btcd_controller = BitcoinCoreController::new(config.clone());
34293449
btcd_controller
34303450
.start_bitcoind()
@@ -3443,7 +3463,9 @@ mod tests {
34433463
.expect("At first, building leader block commit should work");
34443464

34453465
let ser = SerializedTx::new(first_tx_ok);
3446-
btc_controller.send_transaction(ser).expect("Tx should be sent to the burnchain!");
3466+
btc_controller
3467+
.send_transaction(ser)
3468+
.expect("Tx should be sent to the burnchain!");
34473469
btc_controller.build_next_block(1); // Now tx is confirmed
34483470

34493471
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3459,9 +3481,6 @@ mod tests {
34593481
assert_eq!(0, rbf_tx.lock_time);
34603482
assert_eq!(1, rbf_tx.input.len());
34613483
assert_eq!(4, rbf_tx.output.len());
3462-
3463-
//let ong= btc_controller.ongoing_block_commit.unwrap();
3464-
//assert_eq!(2, ong.txids.len());
34653484
}
34663485

34673486
#[test]
@@ -3472,7 +3491,7 @@ mod tests {
34723491

34733492
let mut config = utils::create_config();
34743493
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3475-
3494+
34763495
let mut btcd_controller = BitcoinCoreController::new(config.clone());
34773496
btcd_controller
34783497
.start_bitcoind()
@@ -3491,7 +3510,9 @@ mod tests {
34913510
.expect("At first, building leader block commit should work");
34923511

34933512
let ser = SerializedTx::new(first_tx_ok);
3494-
btc_controller.send_transaction(ser).expect("Tx should be sent to the burnchain!");
3513+
btc_controller
3514+
.send_transaction(ser)
3515+
.expect("Tx should be sent to the burnchain!");
34953516
btc_controller.build_next_block(1); // Now tx is confirmed
34963517

34973518
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3508,6 +3529,4 @@ mod tests {
35083529
assert_eq!(1, rbf_tx.input.len());
35093530
assert_eq!(4, rbf_tx.output.len());
35103531
}
3511-
3512-
35133532
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use crate::helium::RunLoop;
1717
use crate::tests::to_addr;
1818
use crate::Config;
1919

20+
// Value usable as `BurnchainConfig::peer_port` to avoid bitcoind peer port binding
21+
pub const BURNCHAIN_CONFIG_PEER_PORT_DISABLED: u16 = 0;
22+
2023
#[derive(Debug, thiserror::Error)]
2124
pub enum BitcoinCoreError {
2225
#[error("bitcoind spawn failed: {0}")]
@@ -69,9 +72,17 @@ impl BitcoinCoreController {
6972
.arg("-server=1")
7073
.arg("-listenonion=0")
7174
.arg("-rpcbind=127.0.0.1")
72-
.arg(format!("-port={}", self.config.burnchain.peer_port))
75+
//.arg(format!("-port={}", self.config.burnchain.peer_port))
7376
.arg(format!("-datadir={}", self.config.get_burnchain_path_str()));
7477

78+
let peer_port = self.config.burnchain.peer_port;
79+
if peer_port == BURNCHAIN_CONFIG_PEER_PORT_DISABLED {
80+
info!("Peer Port is disabled. So `-listen=0` flag will be used");
81+
command.arg("-listen=0");
82+
} else {
83+
command.arg(format!("-port={}", peer_port));
84+
}
85+
7586
self.add_rpc_cli_args(&mut command);
7687

7788
eprintln!("bitcoind spawn: {command:?}");

0 commit comments

Comments
 (0)