Skip to content

Commit

Permalink
chore: use the burnchain indexer to find the right block header
Browse files Browse the repository at this point in the history
  • Loading branch information
jcnelson committed May 3, 2024
1 parent 1886013 commit c52e9bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 7 additions & 3 deletions stackslib/src/burnchains/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,12 +1123,16 @@ impl BurnchainDB {
Ok(res.is_some())
}

pub fn get_burnchain_header(
pub fn get_burnchain_header<B: BurnchainHeaderReader>(
conn: &DBConn,
indexer: &B,
height: u64,
) -> Result<Option<BurnchainBlockHeader>, BurnchainError> {
let qry = "SELECT * FROM burnchain_db_block_headers WHERE block_height = ?1";
let args = &[&u64_to_sql(height)?];
let Some(hdr) = indexer.read_burnchain_header(height)? else {
return Ok(None);
};
let qry = "SELECT * FROM burnchain_db_block_headers WHERE block_hash = ?1";
let args = &[&hdr.block_hash];
let res: Option<BurnchainBlockHeader> = query_row(conn, qry, args)?;
Ok(res)
}
Expand Down
11 changes: 9 additions & 2 deletions stackslib/src/chainstate/coordinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,7 @@ impl<
/// block can be re-processed in that event.
fn undo_stacks_block_orphaning(
burnchain_conn: &DBConn,
burnchain_indexer: &B,
ic: &SortitionDBConn,
chainstate_db_tx: &mut DBTx,
first_invalidate_start_block: u64,
Expand All @@ -1613,8 +1614,11 @@ impl<
first_invalidate_start_block, last_invalidate_start_block
);
for burn_height in first_invalidate_start_block..(last_invalidate_start_block + 1) {
let burn_header = match BurnchainDB::get_burnchain_header(burnchain_conn, burn_height)?
{
let burn_header = match BurnchainDB::get_burnchain_header(
burnchain_conn,
burnchain_indexer,
burn_height,
)? {
Some(hdr) => hdr,
None => {
continue;
Expand Down Expand Up @@ -1840,6 +1844,7 @@ impl<
// sortitions
let revalidated_burn_header = BurnchainDB::get_burnchain_header(
self.burnchain_blocks_db.conn(),
&self.burnchain_indexer,
first_invalidate_start_block - 1,
)
.expect("FATAL: failed to read burnchain DB")
Expand All @@ -1854,6 +1859,7 @@ impl<
// invalidate all descendant sortitions, no matter what.
let invalidated_burn_header = BurnchainDB::get_burnchain_header(
self.burnchain_blocks_db.conn(),
&self.burnchain_indexer,
last_invalidate_start_block - 1,
)
.expect("FATAL: failed to read burnchain DB")
Expand Down Expand Up @@ -2045,6 +2051,7 @@ impl<
// un-orphan blocks that had been orphaned but were tied to this now-revalidated sortition history
Self::undo_stacks_block_orphaning(
&self.burnchain_blocks_db.conn(),
&self.burnchain_indexer,
&ic,
&mut chainstate_db_tx,
first_invalidate_start_block,
Expand Down

0 comments on commit c52e9bf

Please sign in to comment.