Skip to content

Commit

Permalink
count tx on first visit
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Jan 16, 2025
1 parent c8c23fb commit 6aa2b3a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/src/block_extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl TryFrom<FsBlock> 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,
})
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<BlockExtra>>) -> JoinHandle<()> {
Expand Down
18 changes: 14 additions & 4 deletions lib/src/stages/read_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct DetectedBlock {
prev: BlockHash,
inputs: u32,
outputs: u32,
txs: u32,
}

impl DetectedBlock {
Expand All @@ -62,6 +63,7 @@ impl DetectedBlock {
serialization_version,
block_total_inputs: self.inputs,
block_total_outputs: self.outputs,
block_total_txs: self.txs,
}
}
}
Expand Down Expand Up @@ -159,7 +161,7 @@ pub fn detect(buffer: &[u8], magic: Magic) -> Result<Vec<DetectedBlock>, 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();
Expand All @@ -178,6 +180,7 @@ pub fn detect(buffer: &[u8], magic: Magic) -> Result<Vec<DetectedBlock>, bitcoin
prev,
inputs: visitor.inputs,
outputs: visitor.outputs,
txs: visitor.txs,
};
detected_blocks.push(detected_block);
}
Expand All @@ -187,28 +190,35 @@ pub fn detect(buffer: &[u8], magic: Magic) -> Result<Vec<DetectedBlock>, 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;
}

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
Expand Down

0 comments on commit 6aa2b3a

Please sign in to comment.