11use std:: { fs:: File , io:: Write , sync:: Arc } ;
22
3- use bitcoin:: { consensus, OutPoint } ;
43use hintfile:: write_compact_size;
54use kernel:: { ChainType , ChainstateManager , ChainstateManagerOptions , ContextBuilder , KernelError } ;
65
@@ -21,35 +20,31 @@ fn main() {
2120 let _context = Arc :: new ( ctx) ;
2221 let chainman = ChainstateManager :: new ( options) . unwrap ( ) ;
2322 println ! ( "Chain state initialized" ) ;
24- let genesis = chainman . block_index_genesis ( ) ;
23+ // Writing the chain tip allows the client to know where to stop
2524 let tip = chainman. block_index_tip ( ) . block_hash ( ) . hash ;
26- file. write_all ( & tip) . unwrap ( ) ;
25+ file. write_all ( & tip) . expect ( "file cannot be written to" ) ;
26+
27+ let genesis = chainman. block_index_genesis ( ) ;
2728 let mut current = chainman. next_block_index ( genesis) . unwrap ( ) ;
2829 loop {
2930 let block = chainman. read_block_data ( & current) . unwrap ( ) ;
30- let bytes: Vec < u8 > = block. into ( ) ;
31- let block = consensus:: deserialize :: < bitcoin:: Block > ( & bytes) . unwrap ( ) ;
32- let ( _, transactions) = block. into_parts ( ) ;
33- println ! ( "On block {}" , current. height( ) ) ;
34- let mut delta: u64 = 0 ;
35- let mut block_offsets: Vec < u64 > = Vec :: new ( ) ;
36- for tx in transactions {
37- let txid = tx. compute_txid ( ) ;
38- for ( index, _txout) in tx. outputs . iter ( ) . enumerate ( ) {
39- let _outpoint = OutPoint {
40- txid,
41- vout : index as u32 ,
42- } ;
43- // if true
44- block_offsets. push ( delta) ;
45- delta = 0 ;
31+ println ! ( "Block {} ..." , current. height( ) ) ;
32+ let mut block_unspents = Vec :: new ( ) ;
33+ let mut curr = 0 ;
34+ for i in 0 ..block. transaction_count ( ) {
35+ let transaction = block. transaction ( i) . unwrap ( ) ;
36+ for vout in 0 ..transaction. output_count ( ) {
37+ if chainman. have_coin ( & transaction, vout) {
38+ block_unspents. push ( curr) ;
39+ }
40+ curr += 1 ;
4641 }
4742 }
43+
4844 // Overflows 32 bit machines
49- let len_encode = block_offsets. len ( ) as u64 ;
50- println ! ( "Writing block offsets" ) ;
45+ let len_encode = block_unspents. len ( ) as u64 ;
5146 write_compact_size ( len_encode, & mut file) . expect ( "unexpected EOF" ) ;
52- for offset in block_offsets {
47+ for offset in block_unspents {
5348 write_compact_size ( offset, & mut file) . expect ( "unexpected EOF" ) ;
5449 }
5550 match chainman. next_block_index ( current) {
0 commit comments