Skip to content

Commit 2aae806

Browse files
authored
fix: handle deep re-orgs correctly (#735)
Handles potential forks in the Stacks chain by properly looking for existing blocks in the DB using both the height and hash instead of just the height. With this fix, a deep re-org fork will be processed correctly from the right block ancestor. Fixes #734
1 parent 6078351 commit 2aae806

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

components/chainhook-cli/src/storage/database_access.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use chainhook_sdk::types::BlockIdentifier;
33
use chainhook_sdk::utils::Context;
44
use std::path::PathBuf;
55

6-
use crate::storage::{is_stacks_block_present, open_readonly_stacks_db_conn_with_retry};
6+
use crate::storage::{get_stacks_block_at_block_height, open_readonly_stacks_db_conn_with_retry};
77

88
/// Implementation of DatabaseAccess trait for chainhook-cli
99
/// This provides database access to the SDK without creating circular dependencies
@@ -24,6 +24,15 @@ impl BlocksDatabaseAccess for StacksDatabaseAccess {
2424
ctx: &Context,
2525
) -> Result<bool, String> {
2626
let stacks_db = open_readonly_stacks_db_conn_with_retry(&self.db_path, 0, ctx)?;
27-
Ok(is_stacks_block_present(block_identifier, 0, &stacks_db))
27+
// We have to retrieve the full block to check and compare both its hash and index. There's a function called
28+
// `is_stacks_block_present` that sounds like could be used here but it only checks the index.
29+
if let Some(block) =
30+
get_stacks_block_at_block_height(block_identifier.index, true, 0, &stacks_db)?
31+
{
32+
if block.block_identifier.hash == block_identifier.hash {
33+
return Ok(true);
34+
}
35+
}
36+
return Ok(false);
2837
}
2938
}

0 commit comments

Comments
 (0)