@@ -2839,7 +2839,11 @@ mod tests {
2839
2839
use crate :: Keychain ;
2840
2840
2841
2841
mod utils {
2842
+ use std:: net:: TcpListener ;
2843
+
2842
2844
use super :: * ;
2845
+ use crate :: tests:: bitcoin_regtest:: BURNCHAIN_CONFIG_PEER_PORT_DISABLED ;
2846
+ use crate :: util:: get_epoch_time_nanos;
2843
2847
2844
2848
pub fn create_config ( ) -> Config {
2845
2849
let mut config = Config :: default ( ) ;
@@ -2848,6 +2852,22 @@ mod tests {
2848
2852
config. burnchain . password = Some ( String :: from ( "12345" ) ) ;
2849
2853
// overriding default "0.0.0.0" because doesn't play nicely on Windows.
2850
2854
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
+
2851
2871
config
2852
2872
}
2853
2873
@@ -2885,7 +2905,7 @@ mod tests {
2885
2905
key_vtxindex : 1 , // 0x0001
2886
2906
memo : vec ! [ 11 ] , // 0x5a >> 3
2887
2907
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
2889
2909
input : ( Txid ( [ 0x00 ; 32 ] ) , 0 ) ,
2890
2910
burn_parent_modulus : 2 , // 0x5a & 0b111
2891
2911
@@ -2896,7 +2916,7 @@ mod tests {
2896
2916
] ,
2897
2917
2898
2918
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
2900
2920
2901
2921
txid : Txid ( [ 0x00 ; 32 ] ) ,
2902
2922
vtxindex : 0 ,
@@ -3130,14 +3150,14 @@ mod tests {
3130
3150
3131
3151
let mut config = utils:: create_config ( ) ;
3132
3152
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3133
-
3153
+
3134
3154
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3135
3155
btcd_controller
3136
3156
. start_bitcoind ( )
3137
3157
. expect ( "bitcoind should be started!" ) ;
3138
3158
3139
3159
let btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3140
-
3160
+
3141
3161
btc_controller. bootstrap_chain ( 100 ) ;
3142
3162
let utxos = btc_controller. get_all_utxos ( & miner_pubkey) ;
3143
3163
assert_eq ! ( 0 , utxos. len( ) ) ;
@@ -3149,11 +3169,13 @@ mod tests {
3149
3169
assert_eq ! ( 5_000_000_000 , utxos[ 0 ] . amount) ;
3150
3170
3151
3171
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
+
3153
3175
assert_eq ! ( 2 , utxos. len( ) ) ;
3154
- assert_eq ! ( 101 , utxos[ 0 ] . confirmations) ;
3176
+ assert_eq ! ( 102 , utxos[ 0 ] . confirmations) ;
3155
3177
assert_eq ! ( 5_000_000_000 , utxos[ 0 ] . amount) ;
3156
- assert_eq ! ( 102 , utxos[ 1 ] . confirmations) ;
3178
+ assert_eq ! ( 101 , utxos[ 1 ] . confirmations) ;
3157
3179
assert_eq ! ( 5_000_000_000 , utxos[ 1 ] . amount) ;
3158
3180
}
3159
3181
@@ -3164,15 +3186,15 @@ mod tests {
3164
3186
3165
3187
let mut config = utils:: create_config ( ) ;
3166
3188
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3167
-
3189
+
3168
3190
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3169
3191
btcd_controller
3170
3192
. start_bitcoind ( )
3171
3193
. expect ( "bitcoind should be started!" ) ;
3172
3194
3173
3195
let btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3174
3196
btc_controller. bootstrap_chain ( 101 ) ; // one utxo exists
3175
-
3197
+
3176
3198
let utxos = btc_controller. get_all_utxos ( & other_pubkey) ;
3177
3199
assert_eq ! ( 0 , utxos. len( ) ) ;
3178
3200
}
@@ -3192,7 +3214,7 @@ mod tests {
3192
3214
let btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3193
3215
btc_controller. bootstrap_chain ( 101 ) ;
3194
3216
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 ) ;
3196
3218
3197
3219
let uxto_set = utxos. expect ( "Shouldn't be None!" ) ;
3198
3220
assert_eq ! ( 1 , uxto_set. num_utxos( ) ) ;
@@ -3243,13 +3265,7 @@ mod tests {
3243
3265
btc_controller. bootstrap_chain ( 101 ) ; // one utxo exists
3244
3266
3245
3267
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 ) ;
3253
3269
assert ! (
3254
3270
utxos. is_none( ) ,
3255
3271
"None because utxos for other pubkey don't exist"
@@ -3295,7 +3311,7 @@ mod tests {
3295
3311
3296
3312
let mut config = utils:: create_config ( ) ;
3297
3313
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3298
-
3314
+
3299
3315
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3300
3316
btcd_controller
3301
3317
. start_bitcoind ( )
@@ -3320,14 +3336,15 @@ mod tests {
3320
3336
}
3321
3337
3322
3338
#[ 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
+ ) {
3324
3341
let keychain = utils:: create_keychain ( ) ;
3325
3342
let miner_pubkey = keychain. get_pub_key ( ) ;
3326
3343
let mut signer = keychain. generate_op_signer ( ) ;
3327
3344
3328
3345
let mut config = utils:: create_config ( ) ;
3329
3346
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3330
-
3347
+
3331
3348
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3332
3349
btcd_controller
3333
3350
. start_bitcoind ( )
@@ -3361,14 +3378,15 @@ mod tests {
3361
3378
}
3362
3379
3363
3380
#[ 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
+ {
3365
3383
let keychain = utils:: create_keychain ( ) ;
3366
3384
let miner_pubkey = keychain. get_pub_key ( ) ;
3367
3385
let mut signer = keychain. generate_op_signer ( ) ;
3368
3386
3369
3387
let mut config = utils:: create_config ( ) ;
3370
3388
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3371
-
3389
+
3372
3390
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3373
3391
btcd_controller
3374
3392
. start_bitcoind ( )
@@ -3387,7 +3405,9 @@ mod tests {
3387
3405
. expect ( "At first, building leader block commit should work" ) ;
3388
3406
3389
3407
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!" ) ;
3391
3411
btc_controller. build_next_block ( 1 ) ; // Now tx is confirmed
3392
3412
3393
3413
// re-submitting same commit while previous it is confirmed by the burnchain
@@ -3413,7 +3433,7 @@ mod tests {
3413
3433
3414
3434
let mut config = utils:: create_config ( ) ;
3415
3435
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3416
-
3436
+
3417
3437
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3418
3438
btcd_controller
3419
3439
. start_bitcoind ( )
@@ -3432,7 +3452,9 @@ mod tests {
3432
3452
. expect ( "At first, building leader block commit should work" ) ;
3433
3453
3434
3454
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!" ) ;
3436
3458
btc_controller. build_next_block ( 1 ) ; // Now tx is confirmed
3437
3459
3438
3460
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3448,9 +3470,6 @@ mod tests {
3448
3470
assert_eq ! ( 0 , rbf_tx. lock_time) ;
3449
3471
assert_eq ! ( 1 , rbf_tx. input. len( ) ) ;
3450
3472
assert_eq ! ( 4 , rbf_tx. output. len( ) ) ;
3451
-
3452
- //let ong= btc_controller.ongoing_block_commit.unwrap();
3453
- //assert_eq!(2, ong.txids.len());
3454
3473
}
3455
3474
3456
3475
#[ test]
@@ -3461,7 +3480,7 @@ mod tests {
3461
3480
3462
3481
let mut config = utils:: create_config ( ) ;
3463
3482
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3464
-
3483
+
3465
3484
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3466
3485
btcd_controller
3467
3486
. start_bitcoind ( )
@@ -3480,7 +3499,9 @@ mod tests {
3480
3499
. expect ( "At first, building leader block commit should work" ) ;
3481
3500
3482
3501
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!" ) ;
3484
3505
btc_controller. build_next_block ( 1 ) ; // Now tx is confirmed
3485
3506
3486
3507
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3497,6 +3518,4 @@ mod tests {
3497
3518
assert_eq ! ( 1 , rbf_tx. input. len( ) ) ;
3498
3519
assert_eq ! ( 4 , rbf_tx. output. len( ) ) ;
3499
3520
}
3500
-
3501
-
3502
3521
}
0 commit comments