1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

Remove IndexResult and index into db::WriteBatch (#914)

Co-authored-by: Riccardo Casatta <riccardo@casatta.it>
This commit is contained in:
Roman Zeyde 2023-07-28 19:43:40 +03:00 committed by GitHub
parent 7852dd3697
commit 6171dd627c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,
@ -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();
}