@@ -9,7 +9,6 @@ use std::{
9
9
time:: Duration ,
10
10
} ;
11
11
12
- use anyhow:: anyhow;
13
12
use base64:: Engine ;
14
13
use bitcoin:: { Block , BlockHash , Txid } ;
15
14
use hex:: FromHexError ;
@@ -34,8 +33,7 @@ pub struct BitcoinRpc {
34
33
}
35
34
36
35
pub struct BlockFetcher {
37
- client : reqwest:: blocking:: Client ,
38
- rpc : Arc < BitcoinRpc > ,
36
+ src : BitcoinBlockSource ,
39
37
job_id : Arc < AtomicUsize > ,
40
38
sender : std:: sync:: mpsc:: SyncSender < BlockEvent > ,
41
39
num_workers : usize ,
@@ -303,15 +301,13 @@ impl BitcoinRpcAuth {
303
301
304
302
impl BlockFetcher {
305
303
pub fn new (
306
- rpc : BitcoinRpc ,
307
- client : reqwest:: blocking:: Client ,
304
+ src : BitcoinBlockSource ,
308
305
num_workers : usize ,
309
306
) -> ( Self , std:: sync:: mpsc:: Receiver < BlockEvent > ) {
310
307
let ( tx, rx) = std:: sync:: mpsc:: sync_channel ( 12 ) ;
311
308
(
312
309
Self {
313
- client,
314
- rpc : Arc :: new ( rpc) ,
310
+ src,
315
311
job_id : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
316
312
sender : tx,
317
313
num_workers,
@@ -328,8 +324,7 @@ impl BlockFetcher {
328
324
self . stop ( ) ;
329
325
330
326
let job_id = self . job_id . load ( Ordering :: SeqCst ) ;
331
- let task_client = self . client . clone ( ) ;
332
- let task_rpc = self . rpc . clone ( ) ;
327
+ let task_src = self . src . clone ( ) ;
333
328
let current_task = self . job_id . clone ( ) ;
334
329
let task_sender = self . sender . clone ( ) ;
335
330
let num_workers = self . num_workers ;
@@ -348,20 +343,19 @@ impl BlockFetcher {
348
343
}
349
344
last_check = Instant :: now ( ) ;
350
345
351
- let tip: u32 =
352
- match task_rpc. send_json_blocking ( & task_client, & task_rpc. get_block_count ( ) ) {
353
- Ok ( t) => t,
354
- Err ( e) => {
355
- _ = task_sender. send ( BlockEvent :: Error ( BlockFetchError :: RpcError ( e) ) ) ;
356
- return ;
357
- }
358
- } ;
346
+ let tip: u32 = match task_src. get_block_count ( ) {
347
+ Ok ( t) => t as _ ,
348
+ Err ( e) => {
349
+ _ = task_sender. send ( BlockEvent :: Error ( BlockFetchError :: RpcError ( e) ) ) ;
350
+ return ;
351
+ }
352
+ } ;
359
353
360
354
if tip > checkpoint. height {
361
355
let res = Self :: run_workers (
362
356
job_id,
363
357
current_task. clone ( ) ,
364
- task_rpc . clone ( ) ,
358
+ task_src . clone ( ) ,
365
359
task_sender. clone ( ) ,
366
360
checkpoint,
367
361
tip,
@@ -386,7 +380,7 @@ impl BlockFetcher {
386
380
fn run_workers (
387
381
job_id : usize ,
388
382
current_job : Arc < AtomicUsize > ,
389
- rpc : Arc < BitcoinRpc > ,
383
+ src : BitcoinBlockSource ,
390
384
sender : std:: sync:: mpsc:: SyncSender < BlockEvent > ,
391
385
start_block : ChainAnchor ,
392
386
end_height : u32 ,
@@ -400,7 +394,7 @@ impl BlockFetcher {
400
394
queued_height : start_block. height + 1 ,
401
395
end_height,
402
396
ordered_sender : sender,
403
- rpc ,
397
+ src ,
404
398
num_workers,
405
399
pool : ThreadPool :: new ( num_workers) ,
406
400
} ;
@@ -409,14 +403,14 @@ impl BlockFetcher {
409
403
}
410
404
411
405
pub fn fetch_block (
412
- rpc : & BitcoinRpc ,
413
- client : & reqwest:: blocking:: Client ,
406
+ source : & BitcoinBlockSource ,
414
407
hash : & BlockHash ,
415
408
) -> Result < Block , BitcoinRpcError > {
416
- let block_req = rpc. get_block ( & hash) ;
409
+ let block_req = source . rpc . get_block ( & hash) ;
417
410
let id = block_req. id ;
418
- let response = rpc
419
- . send_request_blocking ( client, & block_req) ?
411
+ let response = source
412
+ . rpc
413
+ . send_request_blocking ( & source. client , & block_req) ?
420
414
. error_for_status ( ) ?;
421
415
let mut raw = response. bytes ( ) ?. to_vec ( ) ;
422
416
@@ -465,7 +459,7 @@ struct Workers {
465
459
queued_height : u32 ,
466
460
end_height : u32 ,
467
461
ordered_sender : std:: sync:: mpsc:: SyncSender < BlockEvent > ,
468
- rpc : Arc < BitcoinRpc > ,
462
+ src : BitcoinBlockSource ,
469
463
num_workers : usize ,
470
464
pool : ThreadPool ,
471
465
}
@@ -521,7 +515,6 @@ impl Workers {
521
515
}
522
516
523
517
fn run ( & mut self ) -> Result < ChainAnchor , BlockFetchError > {
524
- let client = reqwest:: blocking:: Client :: new ( ) ;
525
518
let ( tx, rx) = std:: sync:: mpsc:: sync_channel ( self . num_workers ) ;
526
519
527
520
' queue_blocks: while !self . queued_all ( ) {
@@ -534,8 +527,7 @@ impl Workers {
534
527
return Err ( BlockFetchError :: ChannelClosed ) ;
535
528
}
536
529
let tx = tx. clone ( ) ;
537
- let rpc = self . rpc . clone ( ) ;
538
- let task_client = client. clone ( ) ;
530
+ let rpc = self . src . clone ( ) ;
539
531
let task_sigterm = self . current_job . clone ( ) ;
540
532
let height = self . queued_height ;
541
533
let job_id = self . job_id ;
@@ -545,9 +537,8 @@ impl Workers {
545
537
return ;
546
538
}
547
539
let result: Result < _ , BitcoinRpcError > = ( move || {
548
- let hash: BlockHash =
549
- rpc. send_json_blocking ( & task_client, & rpc. get_block_hash ( height) ) ?;
550
- let block = BlockFetcher :: fetch_block ( & rpc, & task_client, & hash) ?;
540
+ let hash: BlockHash = rpc. get_block_hash ( height) ?;
541
+ let block = BlockFetcher :: fetch_block ( & rpc, & hash) ?;
551
542
Ok ( ( ChainAnchor { height, hash } , block) )
552
543
} ) ( ) ;
553
544
_ = tx. send ( result) ;
@@ -675,7 +666,7 @@ impl ErrorForRpc for reqwest::Response {
675
666
return Err ( BitcoinRpcError :: Rpc ( e) ) ;
676
667
}
677
668
678
- return Ok ( rpc_res. result . unwrap ( ) ) ;
669
+ Ok ( rpc_res. result . unwrap ( ) )
679
670
}
680
671
}
681
672
@@ -704,29 +695,31 @@ impl BitcoinBlockSource {
704
695
}
705
696
706
697
impl BlockSource for BitcoinBlockSource {
707
- fn get_block_hash ( & self , height : u32 ) -> anyhow :: Result < BlockHash > {
698
+ fn get_block_hash ( & self , height : u32 ) -> Result < BlockHash , BitcoinRpcError > {
708
699
Ok ( self
709
700
. rpc
710
701
. send_json_blocking ( & self . client , & self . rpc . get_block_hash ( height) ) ?)
711
702
}
712
703
713
- fn get_block ( & self , hash : & BlockHash ) -> anyhow :: Result < Block > {
704
+ fn get_block ( & self , hash : & BlockHash ) -> Result < Block , BitcoinRpcError > {
714
705
Ok ( self
715
706
. rpc
716
707
. send_json_blocking ( & self . client , & self . rpc . get_block ( hash) ) ?)
717
708
}
718
709
719
- fn get_median_time ( & self ) -> anyhow :: Result < u64 > {
710
+ fn get_median_time ( & self ) -> Result < u64 , BitcoinRpcError > {
720
711
let info: serde_json:: Value = self
721
712
. rpc
722
713
. send_json_blocking ( & self . client , & self . rpc . get_blockchain_info ( ) ) ?;
723
714
if let Some ( time) = info. get ( "mediantime" ) . and_then ( |t| t. as_u64 ( ) ) {
724
715
return Ok ( time) ;
725
716
}
726
- return Err ( anyhow ! ( "Could not fetch median time" ) ) ;
717
+ Err ( BitcoinRpcError :: Other (
718
+ "Could not fetch median time" . to_string ( ) ,
719
+ ) )
727
720
}
728
721
729
- fn get_block_count ( & self ) -> anyhow :: Result < u64 > {
722
+ fn get_block_count ( & self ) -> Result < u64 , BitcoinRpcError > {
730
723
Ok ( self
731
724
. rpc
732
725
. send_json_blocking ( & self . client , & self . rpc . get_block_count ( ) ) ?)
0 commit comments