@@ -2850,7 +2850,11 @@ mod tests {
2850
2850
use crate :: Keychain ;
2851
2851
2852
2852
mod utils {
2853
+ use std:: net:: TcpListener ;
2854
+
2853
2855
use super :: * ;
2856
+ use crate :: tests:: bitcoin_regtest:: BURNCHAIN_CONFIG_PEER_PORT_DISABLED ;
2857
+ use crate :: util:: get_epoch_time_nanos;
2854
2858
2855
2859
pub fn create_config ( ) -> Config {
2856
2860
let mut config = Config :: default ( ) ;
@@ -2859,6 +2863,22 @@ mod tests {
2859
2863
config. burnchain . password = Some ( String :: from ( "12345" ) ) ;
2860
2864
// overriding default "0.0.0.0" because doesn't play nicely on Windows.
2861
2865
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
+
2862
2882
config
2863
2883
}
2864
2884
@@ -2896,7 +2916,7 @@ mod tests {
2896
2916
key_vtxindex : 1 , // 0x0001
2897
2917
memo : vec ! [ 11 ] , // 0x5a >> 3
2898
2918
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
2900
2920
input : ( Txid ( [ 0x00 ; 32 ] ) , 0 ) ,
2901
2921
burn_parent_modulus : 2 , // 0x5a & 0b111
2902
2922
@@ -2907,7 +2927,7 @@ mod tests {
2907
2927
] ,
2908
2928
2909
2929
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
2911
2931
2912
2932
txid : Txid ( [ 0x00 ; 32 ] ) ,
2913
2933
vtxindex : 0 ,
@@ -3141,14 +3161,14 @@ mod tests {
3141
3161
3142
3162
let mut config = utils:: create_config ( ) ;
3143
3163
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3144
-
3164
+
3145
3165
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3146
3166
btcd_controller
3147
3167
. start_bitcoind ( )
3148
3168
. expect ( "bitcoind should be started!" ) ;
3149
3169
3150
3170
let btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3151
-
3171
+
3152
3172
btc_controller. bootstrap_chain ( 100 ) ;
3153
3173
let utxos = btc_controller. get_all_utxos ( & miner_pubkey) ;
3154
3174
assert_eq ! ( 0 , utxos. len( ) ) ;
@@ -3160,11 +3180,13 @@ mod tests {
3160
3180
assert_eq ! ( 5_000_000_000 , utxos[ 0 ] . amount) ;
3161
3181
3162
3182
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
+
3164
3186
assert_eq ! ( 2 , utxos. len( ) ) ;
3165
- assert_eq ! ( 101 , utxos[ 0 ] . confirmations) ;
3187
+ assert_eq ! ( 102 , utxos[ 0 ] . confirmations) ;
3166
3188
assert_eq ! ( 5_000_000_000 , utxos[ 0 ] . amount) ;
3167
- assert_eq ! ( 102 , utxos[ 1 ] . confirmations) ;
3189
+ assert_eq ! ( 101 , utxos[ 1 ] . confirmations) ;
3168
3190
assert_eq ! ( 5_000_000_000 , utxos[ 1 ] . amount) ;
3169
3191
}
3170
3192
@@ -3175,15 +3197,15 @@ mod tests {
3175
3197
3176
3198
let mut config = utils:: create_config ( ) ;
3177
3199
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3178
-
3200
+
3179
3201
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3180
3202
btcd_controller
3181
3203
. start_bitcoind ( )
3182
3204
. expect ( "bitcoind should be started!" ) ;
3183
3205
3184
3206
let btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3185
3207
btc_controller. bootstrap_chain ( 101 ) ; // one utxo exists
3186
-
3208
+
3187
3209
let utxos = btc_controller. get_all_utxos ( & other_pubkey) ;
3188
3210
assert_eq ! ( 0 , utxos. len( ) ) ;
3189
3211
}
@@ -3203,7 +3225,7 @@ mod tests {
3203
3225
let btc_controller = BitcoinRegtestController :: new ( config. clone ( ) , None ) ;
3204
3226
btc_controller. bootstrap_chain ( 101 ) ;
3205
3227
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 ) ;
3207
3229
3208
3230
let uxto_set = utxos. expect ( "Shouldn't be None!" ) ;
3209
3231
assert_eq ! ( 1 , uxto_set. num_utxos( ) ) ;
@@ -3254,13 +3276,7 @@ mod tests {
3254
3276
btc_controller. bootstrap_chain ( 101 ) ; // one utxo exists
3255
3277
3256
3278
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 ) ;
3264
3280
assert ! (
3265
3281
utxos. is_none( ) ,
3266
3282
"None because utxos for other pubkey don't exist"
@@ -3306,7 +3322,7 @@ mod tests {
3306
3322
3307
3323
let mut config = utils:: create_config ( ) ;
3308
3324
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3309
-
3325
+
3310
3326
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3311
3327
btcd_controller
3312
3328
. start_bitcoind ( )
@@ -3331,14 +3347,15 @@ mod tests {
3331
3347
}
3332
3348
3333
3349
#[ 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
+ ) {
3335
3352
let keychain = utils:: create_keychain ( ) ;
3336
3353
let miner_pubkey = keychain. get_pub_key ( ) ;
3337
3354
let mut signer = keychain. generate_op_signer ( ) ;
3338
3355
3339
3356
let mut config = utils:: create_config ( ) ;
3340
3357
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3341
-
3358
+
3342
3359
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3343
3360
btcd_controller
3344
3361
. start_bitcoind ( )
@@ -3372,14 +3389,15 @@ mod tests {
3372
3389
}
3373
3390
3374
3391
#[ 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
+ {
3376
3394
let keychain = utils:: create_keychain ( ) ;
3377
3395
let miner_pubkey = keychain. get_pub_key ( ) ;
3378
3396
let mut signer = keychain. generate_op_signer ( ) ;
3379
3397
3380
3398
let mut config = utils:: create_config ( ) ;
3381
3399
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3382
-
3400
+
3383
3401
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3384
3402
btcd_controller
3385
3403
. start_bitcoind ( )
@@ -3398,7 +3416,9 @@ mod tests {
3398
3416
. expect ( "At first, building leader block commit should work" ) ;
3399
3417
3400
3418
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!" ) ;
3402
3422
btc_controller. build_next_block ( 1 ) ; // Now tx is confirmed
3403
3423
3404
3424
// re-submitting same commit while previous it is confirmed by the burnchain
@@ -3424,7 +3444,7 @@ mod tests {
3424
3444
3425
3445
let mut config = utils:: create_config ( ) ;
3426
3446
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3427
-
3447
+
3428
3448
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3429
3449
btcd_controller
3430
3450
. start_bitcoind ( )
@@ -3443,7 +3463,9 @@ mod tests {
3443
3463
. expect ( "At first, building leader block commit should work" ) ;
3444
3464
3445
3465
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!" ) ;
3447
3469
btc_controller. build_next_block ( 1 ) ; // Now tx is confirmed
3448
3470
3449
3471
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3459,9 +3481,6 @@ mod tests {
3459
3481
assert_eq ! ( 0 , rbf_tx. lock_time) ;
3460
3482
assert_eq ! ( 1 , rbf_tx. input. len( ) ) ;
3461
3483
assert_eq ! ( 4 , rbf_tx. output. len( ) ) ;
3462
-
3463
- //let ong= btc_controller.ongoing_block_commit.unwrap();
3464
- //assert_eq!(2, ong.txids.len());
3465
3484
}
3466
3485
3467
3486
#[ test]
@@ -3472,7 +3491,7 @@ mod tests {
3472
3491
3473
3492
let mut config = utils:: create_config ( ) ;
3474
3493
config. burnchain . local_mining_public_key = Some ( miner_pubkey. to_hex ( ) ) ;
3475
-
3494
+
3476
3495
let mut btcd_controller = BitcoinCoreController :: new ( config. clone ( ) ) ;
3477
3496
btcd_controller
3478
3497
. start_bitcoind ( )
@@ -3491,7 +3510,9 @@ mod tests {
3491
3510
. expect ( "At first, building leader block commit should work" ) ;
3492
3511
3493
3512
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!" ) ;
3495
3516
btc_controller. build_next_block ( 1 ) ; // Now tx is confirmed
3496
3517
3497
3518
//re-gen signer othewise fails because it will be disposed during previous commit tx.
@@ -3508,6 +3529,4 @@ mod tests {
3508
3529
assert_eq ! ( 1 , rbf_tx. input. len( ) ) ;
3509
3530
assert_eq ! ( 4 , rbf_tx. output. len( ) ) ;
3510
3531
}
3511
-
3512
-
3513
3532
}
0 commit comments