Skip to content

Commit

Permalink
Fix crash when indexing a block with no transactions (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Feb 1, 2022
1 parent 460d670 commit 0983b63
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Index {
coinbase_inputs.push_front((start.n(), (start + h.subsidy()).n()));
}

for tx in &block.txdata[1..] {
for tx in block.txdata.iter().skip(1) {
let mut input_ordinal_ranges = VecDeque::new();
for input in &tx.input {
let mut key = Vec::new();
Expand Down Expand Up @@ -100,8 +100,7 @@ impl Index {
coinbase_inputs.extend(&input_ordinal_ranges);
}

{
let tx = &block.txdata[0];
if let Some(tx) = block.txdata.first() {
for (vout, output) in tx.output.iter().enumerate() {
let mut ordinals = Vec::new();
let mut remaining = output.value;
Expand Down
10 changes: 10 additions & 0 deletions tests/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ fn first_satoshi_spent_in_second_block_slot() -> Result {
.transaction(&[(0, 0, 0)], 1)
.run()
}

#[test]
fn regression_empty_block_crash() -> Result {
Test::new()?
.command("find --blocksdir blocks 0 --slot --as-of-height 1")
.block()
.block_without_coinbase()
.expected_stdout("0.0.0.0\n")
.run()
}
19 changes: 19 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ impl Test {
self
}

fn block_without_coinbase(mut self) -> Self {
if self.blocks.is_empty() {
self.blocks.push(genesis_block(Network::Bitcoin));
} else {
self.blocks.push(Block {
header: BlockHeader {
version: 0,
prev_blockhash: self.blocks.last().unwrap().block_hash(),
merkle_root: Default::default(),
time: 0,
bits: 0,
nonce: 0,
},
txdata: Vec::new(),
});
}
self
}

fn transaction(mut self, slots: &[(usize, usize, u32)], output_count: u64) -> Self {
let value = slots
.iter()
Expand Down

0 comments on commit 0983b63

Please sign in to comment.