Skip to content

Commit

Permalink
Remove IndexResult and index into db::WriteBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta authored and romanz committed Jul 28, 2023
1 parent 7852dd3 commit c0b4288
Showing 1 changed file with 15 additions and 41 deletions.
56 changes: 15 additions & 41 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,6 @@ impl Stats {
}
}

struct IndexResult {
header_row: HeaderRow,
funding_rows: Vec<HashPrefixRow>,
spending_rows: Vec<HashPrefixRow>,
txid_rows: Vec<HashPrefixRow>,
}

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,
Expand Down Expand Up @@ -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);
})?;
Expand All @@ -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();
}

0 comments on commit c0b4288

Please sign in to comment.