diff --git a/stackslib/src/burnchains/db.rs b/stackslib/src/burnchains/db.rs index f7da4a0ae9..3171ec3c98 100644 --- a/stackslib/src/burnchains/db.rs +++ b/stackslib/src/burnchains/db.rs @@ -1123,12 +1123,16 @@ impl BurnchainDB { Ok(res.is_some()) } - pub fn get_burnchain_header( + pub fn get_burnchain_header( conn: &DBConn, + indexer: &B, height: u64, ) -> Result, 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 = query_row(conn, qry, args)?; Ok(res) } diff --git a/stackslib/src/burnchains/tests/db.rs b/stackslib/src/burnchains/tests/db.rs index 9c3b5ee477..5a8d958f12 100644 --- a/stackslib/src/burnchains/tests/db.rs +++ b/stackslib/src/burnchains/tests/db.rs @@ -231,6 +231,15 @@ fn test_store_and_fetch() { } assert_eq!(&header, &non_canonical_block.header()); + // when we get a block header by its height, it's canonical + for (height, header) in headers.iter().enumerate() { + let hdr = BurnchainDB::get_burnchain_header(burnchain_db.conn(), &headers, height as u64) + .unwrap() + .unwrap(); + assert!(headers.iter().find(|h| **h == hdr).is_some()); + assert_ne!(hdr, non_canonical_block.header()); + } + let looked_up_canon = burnchain_db.get_canonical_chain_tip().unwrap(); assert_eq!(&looked_up_canon, &canonical_block.header()); diff --git a/stackslib/src/chainstate/coordinator/mod.rs b/stackslib/src/chainstate/coordinator/mod.rs index e54e9f0205..96eae44641 100644 --- a/stackslib/src/chainstate/coordinator/mod.rs +++ b/stackslib/src/chainstate/coordinator/mod.rs @@ -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, @@ -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; @@ -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") @@ -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") @@ -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,