From 6171dd627c9de18d096f6cbe3927b20f30709c82 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Fri, 28 Jul 2023 19:43:40 +0300 Subject: [PATCH] Remove IndexResult and index into db::WriteBatch (#914) Co-authored-by: Riccardo Casatta --- src/index.rs | 56 ++++++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/src/index.rs b/src/index.rs index 323cb31..a60f621 100644 --- a/src/index.rs +++ b/src/index.rs @@ -73,29 +73,6 @@ impl Stats { } } -struct IndexResult { - header_row: HeaderRow, - funding_rows: Vec, - spending_rows: Vec, - txid_rows: Vec, -} - -impl IndexResult { - fn extend(&self, batch: &mut WriteBatch) { - let funding_rows = self.funding_rows.iter().map(HashPrefixRow::to_db_row); - batch.funding_rows.extend(funding_rows); - - let spending_rows = self.spending_rows.iter().map(HashPrefixRow::to_db_row); - batch.spending_rows.extend(spending_rows); - - let txid_rows = self.txid_rows.iter().map(HashPrefixRow::to_db_row); - batch.txid_rows.extend(txid_rows); - - batch.header_rows.push(self.header_row.to_db_row()); - batch.tip_row = serialize(&self.header_row.header.block_hash()).into_boxed_slice(); - } -} - /// Confirmed transactions' address index pub struct Index { store: DBStore, @@ -221,10 +198,11 @@ impl Index { let mut heights = chunk.iter().map(|h| h.height()); let mut batch = WriteBatch::default(); - daemon.for_blocks(blockhashes, |_blockhash, block| { + + daemon.for_blocks(blockhashes, |blockhash, block| { let height = heights.next().expect("unexpected block"); self.stats.observe_duration("block", || { - index_single_block(block, height).extend(&mut batch) + index_single_block(blockhash, block, height, &mut batch); }); self.stats.height.set("tip", height as f64); })?; @@ -251,37 +229,33 @@ fn db_rows_size(rows: &[Row]) -> usize { rows.iter().map(|key| key.len()).sum() } -fn index_single_block(block: Block, height: usize) -> IndexResult { - let mut funding_rows = Vec::with_capacity(block.txdata.iter().map(|tx| tx.output.len()).sum()); - let mut spending_rows = Vec::with_capacity(block.txdata.iter().map(|tx| tx.input.len()).sum()); - let mut txid_rows = Vec::with_capacity(block.txdata.len()); - +fn index_single_block(block_hash: BlockHash, block: Block, height: usize, batch: &mut WriteBatch) { for tx in &block.txdata { - txid_rows.push(TxidRow::row(tx.txid(), height)); + batch + .txid_rows + .push(TxidRow::row(tx.txid(), height).to_db_row()); - funding_rows.extend( + batch.funding_rows.extend( tx.output .iter() .filter(|txo| !txo.script_pubkey.is_provably_unspendable()) .map(|txo| { let scripthash = ScriptHash::new(&txo.script_pubkey); - ScriptHashRow::row(scripthash, height) + ScriptHashRow::row(scripthash, height).to_db_row() }), ); if tx.is_coin_base() { continue; // coinbase doesn't have inputs } - spending_rows.extend( + batch.spending_rows.extend( tx.input .iter() - .map(|txin| SpendingPrefixRow::row(txin.previous_output, height)), + .map(|txin| SpendingPrefixRow::row(txin.previous_output, height).to_db_row()), ); } - IndexResult { - funding_rows, - spending_rows, - txid_rows, - header_row: HeaderRow::new(block.header), - } + batch + .header_rows + .push(HeaderRow::new(block.header).to_db_row()); + batch.tip_row = serialize(&block_hash).into_boxed_slice(); }