Skip to content

Commit 8149e9e

Browse files
committed
test: make regtest run in parallel
1 parent 952877f commit 8149e9e

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
@@ -2839,7 +2839,11 @@ mod tests {
28392839
use crate::Keychain;
28402840

28412841
mod utils {
2842+
use std::net::TcpListener;
2843+
28422844
use super::*;
2845+
use crate::tests::bitcoin_regtest::BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2846+
use crate::util::get_epoch_time_nanos;
28432847

28442848
pub fn create_config() -> Config {
28452849
let mut config = Config::default();
@@ -2848,6 +2852,22 @@ mod tests {
28482852
config.burnchain.password = Some(String::from("12345"));
28492853
// overriding default "0.0.0.0" because doesn't play nicely on Windows.
28502854
config.burnchain.peer_host = String::from("127.0.0.1");
2855+
// avoiding peer port biding to reduce the number of ports to bind to.
2856+
config.burnchain.peer_port = BURNCHAIN_CONFIG_PEER_PORT_DISABLED;
2857+
2858+
//Ask the OS for a free port. Not guaranteed to stay free,
2859+
//after TcpListner is dropped, but good enough for testing
2860+
//and starting bitcoind right after config is created
2861+
let tmp_listener =
2862+
TcpListener::bind("127.0.0.1:0").expect("Failed to bind to get a free port");
2863+
let port = tmp_listener.local_addr().unwrap().port();
2864+
2865+
config.burnchain.rpc_port = port;
2866+
2867+
let now = get_epoch_time_nanos();
2868+
let dir = format!("/tmp/regtest-ctrl-{port}-{now}");
2869+
config.node.working_dir = dir;
2870+
28512871
config
28522872
}
28532873

@@ -2885,7 +2905,7 @@ mod tests {
28852905
key_vtxindex: 1, // 0x0001
28862906
memo: vec![11], // 0x5a >> 3
28872907

2888-
burn_fee: 110_000, //relevant for fee calculation when sending the tx
2908+
burn_fee: 110_000, //relevant for fee calculation when sending the tx
28892909
input: (Txid([0x00; 32]), 0),
28902910
burn_parent_modulus: 2, // 0x5a & 0b111
28912911

@@ -2896,7 +2916,7 @@ mod tests {
28962916
],
28972917

28982918
treatment: vec![],
2899-
sunset_burn: 5_500, //relevant for fee calculation when sending the tx
2919+
sunset_burn: 5_500, //relevant for fee calculation when sending the tx
29002920

29012921
txid: Txid([0x00; 32]),
29022922
vtxindex: 0,
@@ -3130,14 +3150,14 @@ mod tests {
31303150

31313151
let mut config = utils::create_config();
31323152
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3133-
3153+
31343154
let mut btcd_controller = BitcoinCoreController::new(config.clone());
31353155
btcd_controller
31363156
.start_bitcoind()
31373157
.expect("bitcoind should be started!");
31383158

31393159
let btc_controller = BitcoinRegtestController::new(config.clone(), None);
3140-
3160+
31413161
btc_controller.bootstrap_chain(100);
31423162
let utxos = btc_controller.get_all_utxos(&miner_pubkey);
31433163
assert_eq!(0, utxos.len());
@@ -3149,11 +3169,13 @@ mod tests {
31493169
assert_eq!(5_000_000_000, utxos[0].amount);
31503170

31513171
btc_controller.build_next_block(1);
3152-
let utxos = btc_controller.get_all_utxos(&miner_pubkey);
3172+
let mut utxos = btc_controller.get_all_utxos(&miner_pubkey);
3173+
utxos.sort_by(|a, b| b.confirmations.cmp(&a.confirmations));
3174+
31533175
assert_eq!(2, utxos.len());
3154-
assert_eq!(101, utxos[0].confirmations);
3176+
assert_eq!(102, utxos[0].confirmations);
31553177
assert_eq!(5_000_000_000, utxos[0].amount);
3156-
assert_eq!(102, utxos[1].confirmations);
3178+
assert_eq!(101, utxos[1].confirmations);
31573179
assert_eq!(5_000_000_000, utxos[1].amount);
31583180
}
31593181

@@ -3164,15 +3186,15 @@ mod tests {
31643186

31653187
let mut config = utils::create_config();
31663188
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3167-
3189+
31683190
let mut btcd_controller = BitcoinCoreController::new(config.clone());
31693191
btcd_controller
31703192
.start_bitcoind()
31713193
.expect("bitcoind should be started!");
31723194

31733195
let btc_controller = BitcoinRegtestController::new(config.clone(), None);
31743196
btc_controller.bootstrap_chain(101); // one utxo exists
3175-
3197+
31763198
let utxos = btc_controller.get_all_utxos(&other_pubkey);
31773199
assert_eq!(0, utxos.len());
31783200
}
@@ -3192,7 +3214,7 @@ mod tests {
31923214
let btc_controller = BitcoinRegtestController::new(config.clone(), None);
31933215
btc_controller.bootstrap_chain(101);
31943216

3195-
let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 0);
3217+
let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &miner_pubkey, 1, None, 101);
31963218

31973219
let uxto_set = utxos.expect("Shouldn't be None!");
31983220
assert_eq!(1, uxto_set.num_utxos());
@@ -3243,13 +3265,7 @@ mod tests {
32433265
btc_controller.bootstrap_chain(101); // one utxo exists
32443266

32453267
let other_pubkey = utils::create_miner2_pubkey();
3246-
let utxos = btc_controller.get_utxos(
3247-
StacksEpochId::Epoch31,
3248-
&other_pubkey,
3249-
1,
3250-
None,
3251-
0,
3252-
);
3268+
let utxos = btc_controller.get_utxos(StacksEpochId::Epoch31, &other_pubkey, 1, None, 0);
32533269
assert!(
32543270
utxos.is_none(),
32553271
"None because utxos for other pubkey don't exist"
@@ -3295,7 +3311,7 @@ mod tests {
32953311

32963312
let mut config = utils::create_config();
32973313
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3298-
3314+
32993315
let mut btcd_controller = BitcoinCoreController::new(config.clone());
33003316
btcd_controller
33013317
.start_bitcoind()
@@ -3320,14 +3336,15 @@ mod tests {
33203336
}
33213337

33223338
#[test]
3323-
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_not_confirmed() {
3339+
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_not_confirmed(
3340+
) {
33243341
let keychain = utils::create_keychain();
33253342
let miner_pubkey = keychain.get_pub_key();
33263343
let mut signer = keychain.generate_op_signer();
33273344

33283345
let mut config = utils::create_config();
33293346
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3330-
3347+
33313348
let mut btcd_controller = BitcoinCoreController::new(config.clone());
33323349
btcd_controller
33333350
.start_bitcoind()
@@ -3361,14 +3378,15 @@ mod tests {
33613378
}
33623379

33633380
#[test]
3364-
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_is_confirmed() {
3381+
fn test_build_leader_block_commit_tx_fails_resubmitting_same_commit_op_while_prev_is_confirmed()
3382+
{
33653383
let keychain = utils::create_keychain();
33663384
let miner_pubkey = keychain.get_pub_key();
33673385
let mut signer = keychain.generate_op_signer();
33683386

33693387
let mut config = utils::create_config();
33703388
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3371-
3389+
33723390
let mut btcd_controller = BitcoinCoreController::new(config.clone());
33733391
btcd_controller
33743392
.start_bitcoind()
@@ -3387,7 +3405,9 @@ mod tests {
33873405
.expect("At first, building leader block commit should work");
33883406

33893407
let ser = SerializedTx::new(first_tx_ok);
3390-
btc_controller.send_transaction(ser).expect("Tx should be sent to the burnchain!");
3408+
btc_controller
3409+
.send_transaction(ser)
3410+
.expect("Tx should be sent to the burnchain!");
33913411
btc_controller.build_next_block(1); // Now tx is confirmed
33923412

33933413
// re-submitting same commit while previous it is confirmed by the burnchain
@@ -3413,7 +3433,7 @@ mod tests {
34133433

34143434
let mut config = utils::create_config();
34153435
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3416-
3436+
34173437
let mut btcd_controller = BitcoinCoreController::new(config.clone());
34183438
btcd_controller
34193439
.start_bitcoind()
@@ -3432,7 +3452,9 @@ mod tests {
34323452
.expect("At first, building leader block commit should work");
34333453

34343454
let ser = SerializedTx::new(first_tx_ok);
3435-
btc_controller.send_transaction(ser).expect("Tx should be sent to the burnchain!");
3455+
btc_controller
3456+
.send_transaction(ser)
3457+
.expect("Tx should be sent to the burnchain!");
34363458
btc_controller.build_next_block(1); // Now tx is confirmed
34373459

34383460
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3448,9 +3470,6 @@ mod tests {
34483470
assert_eq!(0, rbf_tx.lock_time);
34493471
assert_eq!(1, rbf_tx.input.len());
34503472
assert_eq!(4, rbf_tx.output.len());
3451-
3452-
//let ong= btc_controller.ongoing_block_commit.unwrap();
3453-
//assert_eq!(2, ong.txids.len());
34543473
}
34553474

34563475
#[test]
@@ -3461,7 +3480,7 @@ mod tests {
34613480

34623481
let mut config = utils::create_config();
34633482
config.burnchain.local_mining_public_key = Some(miner_pubkey.to_hex());
3464-
3483+
34653484
let mut btcd_controller = BitcoinCoreController::new(config.clone());
34663485
btcd_controller
34673486
.start_bitcoind()
@@ -3480,7 +3499,9 @@ mod tests {
34803499
.expect("At first, building leader block commit should work");
34813500

34823501
let ser = SerializedTx::new(first_tx_ok);
3483-
btc_controller.send_transaction(ser).expect("Tx should be sent to the burnchain!");
3502+
btc_controller
3503+
.send_transaction(ser)
3504+
.expect("Tx should be sent to the burnchain!");
34843505
btc_controller.build_next_block(1); // Now tx is confirmed
34853506

34863507
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3497,6 +3518,4 @@ mod tests {
34973518
assert_eq!(1, rbf_tx.input.len());
34983519
assert_eq!(4, rbf_tx.output.len());
34993520
}
3500-
3501-
35023521
}

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}")]
@@ -67,9 +70,17 @@ impl BitcoinCoreController {
6770
.arg("-server=1")
6871
.arg("-listenonion=0")
6972
.arg("-rpcbind=127.0.0.1")
70-
.arg(format!("-port={}", self.config.burnchain.peer_port))
73+
//.arg(format!("-port={}", self.config.burnchain.peer_port))
7174
.arg(format!("-datadir={}", self.config.get_burnchain_path_str()));
7275

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

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

0 commit comments

Comments
 (0)