From 6aa2b3add9cb7b1571e43af96afa6909d7bf37d7 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Thu, 16 Jan 2025 15:29:10 +0100 Subject: [PATCH] count tx on first visit --- lib/src/block_extra.rs | 2 +- lib/src/lib.rs | 3 +++ lib/src/stages/read_detect.rs | 18 ++++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/src/block_extra.rs b/lib/src/block_extra.rs index 9ed3a79..a7380e9 100644 --- a/lib/src/block_extra.rs +++ b/lib/src/block_extra.rs @@ -87,7 +87,7 @@ impl TryFrom for BlockExtra { block_total_inputs: fs_block.block_total_inputs, block_total_outputs: fs_block.block_total_outputs, txids: vec![], - block_total_txs: 0, + block_total_txs: fs_block.block_total_txs as usize, }) } } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index ee2a7ed..6972edd 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -83,6 +83,9 @@ pub struct FsBlock { /// Total number of transaction outputs in this block pub(crate) block_total_outputs: u32, + + /// Total number of transactions in this block + pub(crate) block_total_txs: u32, } fn iterate(config: Config, channel: SyncSender>) -> JoinHandle<()> { diff --git a/lib/src/stages/read_detect.rs b/lib/src/stages/read_detect.rs index 134743a..5b69b3a 100644 --- a/lib/src/stages/read_detect.rs +++ b/lib/src/stages/read_detect.rs @@ -48,6 +48,7 @@ pub struct DetectedBlock { prev: BlockHash, inputs: u32, outputs: u32, + txs: u32, } impl DetectedBlock { @@ -62,6 +63,7 @@ impl DetectedBlock { serialization_version, block_total_inputs: self.inputs, block_total_outputs: self.outputs, + block_total_txs: self.txs, } } } @@ -159,7 +161,7 @@ pub fn detect(buffer: &[u8], magic: Magic) -> Result, bitcoin let size: u32 = size.parsed().into(); pointer += 4; let start = pointer; - let mut visitor = InputsOutputsCounter::new(); + let mut visitor = InputsOutputsTxsCounter::new(); match bsl::Block::visit(remaining, &mut visitor) { Ok(block) => { pointer += block.consumed(); @@ -178,6 +180,7 @@ pub fn detect(buffer: &[u8], magic: Magic) -> Result, bitcoin prev, inputs: visitor.inputs, outputs: visitor.outputs, + txs: visitor.txs, }; detected_blocks.push(detected_block); } @@ -187,21 +190,23 @@ pub fn detect(buffer: &[u8], magic: Magic) -> Result, bitcoin Ok(detected_blocks) } -struct InputsOutputsCounter { +struct InputsOutputsTxsCounter { inputs: u32, outputs: u32, + txs: u32, } -impl InputsOutputsCounter { +impl InputsOutputsTxsCounter { fn new() -> Self { Self { inputs: 0, outputs: 0, + txs: 0, } } } -impl bitcoin_slices::Visitor for InputsOutputsCounter { +impl bitcoin_slices::Visitor for InputsOutputsTxsCounter { fn visit_tx_ins(&mut self, total_inputs: usize) { self.inputs += total_inputs as u32; } @@ -209,6 +214,11 @@ impl bitcoin_slices::Visitor for InputsOutputsCounter { fn visit_tx_outs(&mut self, total_outputs: usize) { self.outputs += total_outputs as u32; } + + fn visit_transaction(&mut self, _tx: &bsl::Transaction) -> core::ops::ControlFlow<()> { + self.txs += 1; + core::ops::ControlFlow::Continue(()) + } } /// Implements a rolling u32, every time a new u8 is `push`ed the old value is shifted by 1 byte