Skip to content

Commit

Permalink
fix: prevent panic when scanning from genesis block (#408)
Browse files Browse the repository at this point in the history
Parent block height (`u64`) is always set to `block - 1` which will
result on a panic when trying to construct genesis parent's block
height.

Co-authored-by: Ludo Galabru <ludo@hiro.so>
  • Loading branch information
vabanaerytk and Ludo Galabru authored Sep 15, 2023
1 parent d89bd1a commit 4ab8dd8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
5 changes: 4 additions & 1 deletion components/chainhook-sdk/src/indexer/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,10 @@ pub fn standardize_bitcoin_block(
.previousblockhash
.unwrap_or(BlockHash::all_zeros().to_string())
),
index: block_height - 1,
index: match block_height {
0 => 0,
_ => block_height - 1,
},
},
timestamp: block.time as u32,
metadata: BitcoinBlockMetadata {
Expand Down
5 changes: 4 additions & 1 deletion components/chainhook-sdk/src/indexer/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,10 @@ pub fn standardize_stacks_block(
},
parent_block_identifier: BlockIdentifier {
hash: block.parent_index_block_hash.clone(),
index: block.block_height - 1,
index: match block.block_height {
0 => 0,
_ => block.block_height - 1,
}
},
timestamp: block.parent_burn_block_timestamp,
metadata: StacksBlockMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ pub fn generate_test_bitcoin_block(
let mut hash = vec![
fork_id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

let parent_height = match block_height {
0 => 0,
_ => block_height - 1,
};

let parent_block_identifier = match parent {
Some(parent) => {
assert_eq!(parent.block_identifier.index, block_height - 1);
assert_eq!(parent.block_identifier.index, parent_height);
parent.block_identifier.clone()
}
_ => {
let mut parent_hash = if (block_height - 1) == 1 {
let mut parent_hash = if parent_height == 1 {
vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
Expand All @@ -26,9 +32,10 @@ pub fn generate_test_bitcoin_block(
fork_id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
};
parent_hash.append(&mut (block_height - 1).to_be_bytes().to_vec());

parent_hash.append(&mut parent_height.to_be_bytes().to_vec());
BlockIdentifier {
index: block_height - 1,
index: parent_height,
hash: format!("0x{}", hex::encode(&parent_hash[..])),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@ pub fn generate_test_stacks_block(
let mut hash = vec![
fork_id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

let parent_height = match block_height {
0 => 0,
_ => block_height - 1,
};

let (parent_block_identifier, confirm_microblock_identifier) = match parent {
Some(BlockEvent::Block(parent)) => {
assert_eq!(parent.block_identifier.index, block_height - 1);
assert_eq!(parent.block_identifier.index, parent_height);
(parent.block_identifier.clone(), None)
}
Some(BlockEvent::Microblock(microblock_parent)) => {
assert_eq!(
microblock_parent.metadata.anchor_block_identifier.index,
block_height - 1
parent_height,
);
(
microblock_parent.metadata.anchor_block_identifier.clone(),
Some(microblock_parent.block_identifier.clone()),
)
}
_ => {
let mut parent_hash = if (block_height - 1) == 1 {
let mut parent_hash = if parent_height == 1 {
vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
Expand All @@ -37,10 +43,10 @@ pub fn generate_test_stacks_block(
fork_id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]
};
parent_hash.append(&mut (block_height - 1).to_be_bytes().to_vec());
parent_hash.append(&mut parent_height.to_be_bytes().to_vec());
(
BlockIdentifier {
index: block_height - 1,
index: parent_height,
hash: hex::encode(&parent_hash[..]),
},
None,
Expand All @@ -58,7 +64,7 @@ pub fn generate_test_stacks_block(
transactions,
metadata: StacksBlockMetadata {
bitcoin_anchor_block_identifier: BlockIdentifier {
index: block_height - 1,
index: parent_height,
hash: format!(""),
},
pox_cycle_index: 1,
Expand Down

0 comments on commit 4ab8dd8

Please sign in to comment.