@@ -16,7 +16,7 @@ use std::thread;
1616
1717use electrs_macros:: trace;
1818
19- use crate :: chain:: { Block , BlockHash } ;
19+ use crate :: chain:: { Block , BlockHash , Txid } ;
2020use crate :: daemon:: Daemon ;
2121use crate :: errors:: * ;
2222use crate :: util:: { spawn_thread, HeaderEntry , SyncChannel } ;
@@ -45,6 +45,7 @@ pub struct BlockEntry {
4545 pub block : Block ,
4646 pub entry : HeaderEntry ,
4747 pub size : u32 ,
48+ pub txids : Vec < Txid > ,
4849}
4950
5051type SizedBlock = ( Block , u32 ) ;
@@ -106,10 +107,14 @@ fn bitcoind_fetcher(
106107 let block_entries: Vec < BlockEntry > = blocks
107108 . into_iter ( )
108109 . zip ( entries)
109- . map ( |( block, entry) | BlockEntry {
110- entry : entry. clone ( ) , // TODO: remove this clone()
111- size : block. total_size ( ) as u32 ,
112- block,
110+ . map ( |( block, entry) | {
111+ let txids = block. txdata . iter ( ) . map ( |tx| tx. compute_txid ( ) ) . collect ( ) ;
112+ BlockEntry {
113+ entry : entry. clone ( ) , // TODO: remove this clone()
114+ size : block. total_size ( ) as u32 ,
115+ txids,
116+ block,
117+ }
113118 } )
114119 . collect ( ) ;
115120 assert_eq ! ( block_entries. len( ) , entries. len( ) ) ;
@@ -156,7 +161,10 @@ fn blkfiles_fetcher(
156161 let blockhash = block. block_hash ( ) ;
157162 entry_map
158163 . remove ( & blockhash)
159- . map ( |entry| BlockEntry { block, entry, size } )
164+ . map ( |entry| {
165+ let txids = block. txdata . iter ( ) . map ( |tx| tx. compute_txid ( ) ) . collect ( ) ;
166+ BlockEntry { block, entry, size, txids }
167+ } )
160168 . or_else ( || {
161169 trace ! ( "skipping block {}" , blockhash) ;
162170 None
@@ -224,9 +232,14 @@ fn blkfiles_parser(blobs: Fetcher<Vec<u8>>, magic: u32) -> Fetcher<Vec<SizedBloc
224232 Fetcher :: from (
225233 chan. into_receiver ( ) ,
226234 spawn_thread ( "blkfiles_parser" , move || {
235+ let pool = rayon:: ThreadPoolBuilder :: new ( )
236+ . num_threads ( 0 ) // CPU-bound
237+ . thread_name ( |i| format ! ( "parse-blocks-{}" , i) )
238+ . build ( )
239+ . unwrap ( ) ;
227240 blobs. map ( |blob| {
228241 trace ! ( "parsing {} bytes" , blob. len( ) ) ;
229- let blocks = parse_blocks ( blob, magic) . expect ( "failed to parse blk*.dat file" ) ;
242+ let blocks = parse_blocks ( & pool , blob, magic) . expect ( "failed to parse blk*.dat file" ) ;
230243 sender
231244 . send ( blocks)
232245 . expect ( "failed to send blocks from blk*.dat file" ) ;
@@ -236,7 +249,7 @@ fn blkfiles_parser(blobs: Fetcher<Vec<u8>>, magic: u32) -> Fetcher<Vec<SizedBloc
236249}
237250
238251#[ trace]
239- fn parse_blocks ( blob : Vec < u8 > , magic : u32 ) -> Result < Vec < SizedBlock > > {
252+ fn parse_blocks ( pool : & rayon :: ThreadPool , blob : Vec < u8 > , magic : u32 ) -> Result < Vec < SizedBlock > > {
240253 let mut cursor = Cursor :: new ( & blob) ;
241254 let mut slices = vec ! [ ] ;
242255 let max_pos = blob. len ( ) as u64 ;
@@ -273,11 +286,6 @@ fn parse_blocks(blob: Vec<u8>, magic: u32) -> Result<Vec<SizedBlock>> {
273286 cursor. set_position ( end as u64 ) ;
274287 }
275288
276- let pool = rayon:: ThreadPoolBuilder :: new ( )
277- . num_threads ( 0 ) // CPU-bound
278- . thread_name ( |i| format ! ( "parse-blocks-{}" , i) )
279- . build ( )
280- . unwrap ( ) ;
281289 Ok ( pool. install ( || {
282290 slices
283291 . into_par_iter ( )
0 commit comments