11use std:: {
22 fs:: File ,
33 path:: PathBuf ,
4+ str:: FromStr ,
45 sync:: { mpsc:: channel, Arc , Mutex } ,
56 time:: { Duration , Instant } ,
67} ;
@@ -10,8 +11,8 @@ use hintfile::Hints;
1011use kernel:: { ChainstateManager , ChainstateManagerOptions , ContextBuilder } ;
1112
1213use node:: {
13- bootstrap_dns, elapsed_time, get_blocks_for_range , hashes_from_chain , sync_block_headers ,
14- AccumulatorState , ChainExt ,
14+ bootstrap_dns, elapsed_time, emit_hashes_in_order , get_blocks_for_range , hashes_from_chain ,
15+ migrate_blocks , setup_validation_interface , sync_block_headers , AccumulatorState , ChainExt ,
1516} ;
1617use p2p:: net:: TimeoutParams ;
1718
@@ -20,9 +21,14 @@ const PING_INTERVAL: Duration = Duration::from_secs(10 * 60);
2021configure_me:: include_config!( ) ;
2122
2223fn main ( ) {
24+ let subscriber = tracing_subscriber:: FmtSubscriber :: new ( ) ;
25+ tracing:: subscriber:: set_global_default ( subscriber) . unwrap ( ) ;
2326 let ( config, _) = Config :: including_optional_config_files :: < & [ & str ] > ( & [ ] ) . unwrap_or_exit ( ) ;
2427 let hint_path = config. hintfile ;
2528 let blocks_dir = config. blocks_dir ;
29+ let home_var = std:: env:: var ( "HOME" ) . unwrap ( ) ;
30+ let home_dir = PathBuf :: from_str ( & home_var) . unwrap ( ) ;
31+ let bitcoind_dir = home_dir. join ( ".bitcoin" ) ;
2632 let network = config
2733 . network
2834 . parse :: < Network > ( )
@@ -38,8 +44,6 @@ fn main() {
3844 timeout_conf. write_timeout ( write_timeout) ;
3945 timeout_conf. tcp_handshake_timeout ( tcp_timeout) ;
4046 timeout_conf. ping_interval ( PING_INTERVAL ) ;
41- let subscriber = tracing_subscriber:: FmtSubscriber :: new ( ) ;
42- tracing:: subscriber:: set_global_default ( subscriber) . unwrap ( ) ;
4347 let hintfile_start_time = Instant :: now ( ) ;
4448 tracing:: info!( "Reading in {hint_path}" ) ;
4549 let mut hintfile = File :: open ( hint_path) . expect ( "invalid hintfile path" ) ;
@@ -58,9 +62,16 @@ fn main() {
5862 let kernel_start_time = Instant :: now ( ) ;
5963 let ctx = ContextBuilder :: new ( )
6064 . chain_type ( network. chain_type ( ) )
65+ . validation_interface ( setup_validation_interface ( ) )
6166 . build ( )
6267 . unwrap ( ) ;
63- let options = ChainstateManagerOptions :: new ( & ctx, "." , "./blocks" ) . unwrap ( ) ;
68+ let bitcoind_block_dir = bitcoind_dir. join ( "blocks" ) ;
69+ let options = ChainstateManagerOptions :: new (
70+ & ctx,
71+ bitcoind_dir. to_str ( ) . unwrap ( ) ,
72+ bitcoind_block_dir. to_str ( ) . unwrap ( ) ,
73+ )
74+ . unwrap ( ) ;
6475 let chainman = ChainstateManager :: new ( options) . unwrap ( ) ;
6576 elapsed_time ( kernel_start_time) ;
6677 let tip = chainman. best_header ( ) . height ( ) ;
@@ -74,7 +85,12 @@ fn main() {
7485 let acc_task = std:: thread:: spawn ( move || accumulator_state. verify ( ) ) ;
7586 let peers = Arc :: new ( Mutex :: new ( peers) ) ;
7687 let mut tasks = Vec :: new ( ) ;
77- let hashes = hashes_from_chain ( Arc :: clone ( & chain) , task_num) ;
88+ let hashes = if matches ! ( network, Network :: Bitcoin ) {
89+ hashes_from_chain ( Arc :: clone ( & chain) , task_num)
90+ } else {
91+ let hashes = emit_hashes_in_order ( Arc :: clone ( & chain) ) . collect :: < Vec < BlockHash > > ( ) ;
92+ hashes. chunks ( 10_000 ) . map ( |slice| slice. to_vec ( ) ) . collect ( )
93+ } ;
7894 for ( task_id, chunk) in hashes. into_iter ( ) . enumerate ( ) {
7995 let chain = Arc :: clone ( & chain) ;
8096 let tx = tx. clone ( ) ;
@@ -107,4 +123,6 @@ fn main() {
107123 let acc_result = acc_task. join ( ) . unwrap ( ) ;
108124 tracing:: info!( "Verified: {acc_result}" ) ;
109125 elapsed_time ( main_routine_time) ;
126+ tracing:: info!( "Migrating blocks to Bitcoin Core" ) ;
127+ migrate_blocks ( chain, & block_file_path) ;
110128}
0 commit comments