@@ -15,6 +15,7 @@ use rusqlite::params;
15
15
use serde:: Deserialize ;
16
16
use serde_json:: json;
17
17
use stacks:: burnchains:: bitcoin:: address:: { BitcoinAddress , LegacyBitcoinAddressType } ;
18
+ use stacks:: burnchains:: bitcoin:: spv:: SpvClient ;
18
19
use stacks:: burnchains:: bitcoin:: BitcoinNetworkType ;
19
20
use stacks:: burnchains:: burnchain:: TEST_DOWNLOAD_ERROR_ON_REORG ;
20
21
use stacks:: burnchains:: db:: BurnchainDB ;
@@ -72,6 +73,7 @@ use stacks::util_lib::signed_structured_data::pox4::{
72
73
make_pox_4_signer_key_signature, Pox4SignatureTopic ,
73
74
} ;
74
75
use stacks_common:: address:: AddressHashMode ;
76
+ use stacks_common:: deps_common:: bitcoin:: network:: serialize:: BitcoinHash as _;
75
77
use stacks_common:: types:: chainstate:: {
76
78
BlockHeaderHash , BurnchainHeaderHash , StacksAddress , StacksBlockId ,
77
79
} ;
@@ -3401,6 +3403,60 @@ fn download_err_in_btc_reorg() {
3401
3403
thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
3402
3404
let next_stacks_height = get_chain_info ( & conf) . stacks_tip_height ;
3403
3405
info ! ( "Checking stacks height change" ; "before" => stacks_height, "after" => next_stacks_height) ;
3406
+
3407
+ let burnchain_db = BurnchainDB :: open (
3408
+ & btc_regtest_controller
3409
+ . get_burnchain ( )
3410
+ . get_burnchaindb_path ( ) ,
3411
+ false ,
3412
+ )
3413
+ . unwrap ( ) ;
3414
+
3415
+ let spv_client = SpvClient :: new (
3416
+ & conf. get_spv_headers_file_path ( ) ,
3417
+ 0 ,
3418
+ None ,
3419
+ BitcoinNetworkType :: Regtest ,
3420
+ false ,
3421
+ false ,
3422
+ )
3423
+ . expect ( "Unable to open SPV client DB" ) ;
3424
+
3425
+ let highest_btc_ht = spv_client
3426
+ . get_highest_header_height ( )
3427
+ . expect ( "Failed to query highest header from SPV DB" ) ;
3428
+ let highest_stored_block = burnchain_db
3429
+ . get_canonical_chain_tip ( )
3430
+ . expect ( "Failed to query canonical burn block" ) ;
3431
+ let mut highest_spv_header = spv_client
3432
+ . read_block_header ( highest_btc_ht)
3433
+ . unwrap ( )
3434
+ . unwrap ( )
3435
+ . header
3436
+ . bitcoin_hash ( )
3437
+ . 0 ;
3438
+ highest_spv_header. reverse ( ) ;
3439
+ info ! (
3440
+ "Checking btc coherence" ;
3441
+ "highest_stored_block_hash" => %highest_stored_block. block_hash,
3442
+ "highest_stored_block_height" => highest_stored_block. block_height,
3443
+ "highest_spv_block_hash" => %BurnchainHeaderHash ( highest_spv_header) ,
3444
+ "highest_spv_height" => highest_btc_ht,
3445
+ ) ;
3446
+ for btc_block_ht in reorg_height - 1 ..=highest_btc_ht {
3447
+ let btc_block_header = spv_client
3448
+ . read_block_header ( btc_block_ht)
3449
+ . expect ( "Failed to read header from SPV DB that it claimed to have" )
3450
+ . expect ( "Failed to read header from SPV DB that it claimed to have" ) ;
3451
+ let mut btc_block_hash = btc_block_header. header . bitcoin_hash ( ) . 0 ;
3452
+ btc_block_hash. reverse ( ) ;
3453
+ let btc_block_hash = BurnchainHeaderHash ( btc_block_hash) ;
3454
+ let has_block = burnchain_db
3455
+ . has_burnchain_block ( & btc_block_hash)
3456
+ . expect ( "Failed to query burnchain DB" ) ;
3457
+ assert ! ( has_block, "BurnchainDB should contain block header for BTC block #{btc_block_ht}. burn_hash = {btc_block_hash}" ) ;
3458
+ }
3459
+
3404
3460
assert ! ( next_stacks_height > stacks_height) ;
3405
3461
}
3406
3462
0 commit comments