@@ -4,7 +4,7 @@ use std::path::Path;
4
4
use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
5
5
use std:: sync:: { mpsc, Arc } ;
6
6
use std:: time:: { Duration , Instant } ;
7
- use std:: { cmp, env, fs, thread} ;
7
+ use std:: { cmp, env, fs, io , thread} ;
8
8
9
9
use clarity:: vm:: ast:: stack_depth_checker:: AST_CALL_STACK_DEPTH_BUFFER ;
10
10
use clarity:: vm:: ast:: ASTRules ;
@@ -9302,7 +9302,11 @@ fn test_problematic_blocks_are_not_relayed_or_stored() {
9302
9302
let tip_info = get_chain_info ( & conf) ;
9303
9303
9304
9304
// all blocks were processed
9305
- assert ! ( tip_info. stacks_tip_height >= old_tip_info. stacks_tip_height + 5 ) ;
9305
+ info ! (
9306
+ "tip_info.stacks_tip_height = {}, old_tip_info.stacks_tip_height = {}" ,
9307
+ tip_info. stacks_tip_height, old_tip_info. stacks_tip_height
9308
+ ) ;
9309
+ assert ! ( tip_info. stacks_tip_height > old_tip_info. stacks_tip_height) ;
9306
9310
// one was problematic -- i.e. the one that included tx_high
9307
9311
assert_eq ! ( all_new_files. len( ) , 1 ) ;
9308
9312
@@ -11174,3 +11178,129 @@ fn filter_txs_by_origin() {
11174
11178
11175
11179
test_observer:: clear ( ) ;
11176
11180
}
11181
+
11182
+ // https://stackoverflow.com/questions/26958489/how-to-copy-a-folder-recursively-in-rust
11183
+ fn copy_dir_all ( src : impl AsRef < Path > , dst : impl AsRef < Path > ) -> io:: Result < ( ) > {
11184
+ fs:: create_dir_all ( & dst) ?;
11185
+ for entry in fs:: read_dir ( src) ? {
11186
+ let entry = entry?;
11187
+ let ty = entry. file_type ( ) ?;
11188
+ if ty. is_dir ( ) {
11189
+ copy_dir_all ( entry. path ( ) , dst. as_ref ( ) . join ( entry. file_name ( ) ) ) ?;
11190
+ } else {
11191
+ fs:: copy ( entry. path ( ) , dst. as_ref ( ) . join ( entry. file_name ( ) ) ) ?;
11192
+ }
11193
+ }
11194
+ Ok ( ( ) )
11195
+ }
11196
+
11197
+ #[ test]
11198
+ #[ ignore]
11199
+ fn bitcoin_reorg_flap ( ) {
11200
+ if env:: var ( "BITCOIND_TEST" ) != Ok ( "1" . into ( ) ) {
11201
+ return ;
11202
+ }
11203
+
11204
+ let ( conf, _miner_account) = neon_integration_test_conf ( ) ;
11205
+
11206
+ let mut btcd_controller = BitcoinCoreController :: new ( conf. clone ( ) ) ;
11207
+ btcd_controller
11208
+ . start_bitcoind ( )
11209
+ . expect ( "Failed starting bitcoind" ) ;
11210
+
11211
+ let mut btc_regtest_controller = BitcoinRegtestController :: new ( conf. clone ( ) , None ) ;
11212
+
11213
+ btc_regtest_controller. bootstrap_chain ( 201 ) ;
11214
+
11215
+ eprintln ! ( "Chain bootstrapped..." ) ;
11216
+
11217
+ let mut run_loop = neon:: RunLoop :: new ( conf. clone ( ) ) ;
11218
+ let blocks_processed = run_loop. get_blocks_processed_arc ( ) ;
11219
+
11220
+ let channel = run_loop. get_coordinator_channel ( ) . unwrap ( ) ;
11221
+
11222
+ thread:: spawn ( move || run_loop. start ( None , 0 ) ) ;
11223
+
11224
+ // give the run loop some time to start up!
11225
+ wait_for_runloop ( & blocks_processed) ;
11226
+
11227
+ // first block wakes up the run loop
11228
+ next_block_and_wait ( & mut btc_regtest_controller, & blocks_processed) ;
11229
+
11230
+ // first block will hold our VRF registration
11231
+ next_block_and_wait ( & mut btc_regtest_controller, & blocks_processed) ;
11232
+
11233
+ let mut sort_height = channel. get_sortitions_processed ( ) ;
11234
+ eprintln ! ( "Sort height: {}" , sort_height) ;
11235
+
11236
+ while sort_height < 210 {
11237
+ next_block_and_wait ( & mut btc_regtest_controller, & blocks_processed) ;
11238
+ sort_height = channel. get_sortitions_processed ( ) ;
11239
+ eprintln ! ( "Sort height: {}" , sort_height) ;
11240
+ }
11241
+
11242
+ // stop bitcoind and copy its DB to simulate a chain flap
11243
+ btcd_controller. stop_bitcoind ( ) . unwrap ( ) ;
11244
+ thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
11245
+
11246
+ let btcd_dir = conf. get_burnchain_path_str ( ) ;
11247
+ let mut new_conf = conf. clone ( ) ;
11248
+ new_conf. node . working_dir = format ! ( "{}.new" , & conf. node. working_dir) ;
11249
+ fs:: create_dir_all ( & new_conf. node . working_dir ) . unwrap ( ) ;
11250
+
11251
+ copy_dir_all ( & btcd_dir, & new_conf. get_burnchain_path_str ( ) ) . unwrap ( ) ;
11252
+
11253
+ // resume
11254
+ let mut btcd_controller = BitcoinCoreController :: new ( conf. clone ( ) ) ;
11255
+ btcd_controller
11256
+ . start_bitcoind ( )
11257
+ . expect ( "Failed starting bitcoind" ) ;
11258
+
11259
+ let btc_regtest_controller = BitcoinRegtestController :: new ( conf. clone ( ) , None ) ;
11260
+ thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
11261
+
11262
+ info ! ( "\n \n Begin fork A\n \n " ) ;
11263
+
11264
+ // make fork A
11265
+ for _i in 0 ..3 {
11266
+ btc_regtest_controller. build_next_block ( 1 ) ;
11267
+ thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
11268
+ }
11269
+
11270
+ btcd_controller. stop_bitcoind ( ) . unwrap ( ) ;
11271
+
11272
+ info ! ( "\n \n Begin reorg flap from A to B\n \n " ) ;
11273
+
11274
+ // carry out the flap to fork B -- new_conf's state was the same as before the reorg
11275
+ let mut btcd_controller = BitcoinCoreController :: new ( new_conf. clone ( ) ) ;
11276
+ let btc_regtest_controller = BitcoinRegtestController :: new ( new_conf. clone ( ) , None ) ;
11277
+
11278
+ btcd_controller
11279
+ . start_bitcoind ( )
11280
+ . expect ( "Failed starting bitcoind" ) ;
11281
+
11282
+ for _i in 0 ..5 {
11283
+ btc_regtest_controller. build_next_block ( 1 ) ;
11284
+ thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
11285
+ }
11286
+
11287
+ btcd_controller. stop_bitcoind ( ) . unwrap ( ) ;
11288
+
11289
+ info ! ( "\n \n Begin reorg flap from B to A\n \n " ) ;
11290
+
11291
+ let mut btcd_controller = BitcoinCoreController :: new ( conf. clone ( ) ) ;
11292
+ let btc_regtest_controller = BitcoinRegtestController :: new ( conf. clone ( ) , None ) ;
11293
+ btcd_controller
11294
+ . start_bitcoind ( )
11295
+ . expect ( "Failed starting bitcoind" ) ;
11296
+
11297
+ // carry out the flap back to fork A
11298
+ for _i in 0 ..7 {
11299
+ btc_regtest_controller. build_next_block ( 1 ) ;
11300
+ thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
11301
+ }
11302
+
11303
+ assert_eq ! ( channel. get_sortitions_processed( ) , 225 ) ;
11304
+ btcd_controller. stop_bitcoind ( ) . unwrap ( ) ;
11305
+ channel. stop_chains_coordinator ( ) ;
11306
+ }
0 commit comments