Skip to content

Commit

Permalink
Merge pull request #4748 from stacks-network/fix/4721
Browse files Browse the repository at this point in the history
Fix/4721
  • Loading branch information
jcnelson authored May 8, 2024
2 parents 28ee6dc + e59f9f5 commit 329c27c
Show file tree
Hide file tree
Showing 3 changed files with 25 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
9 changes: 9 additions & 0 deletions stackslib/src/burnchains/tests/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

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 329c27c

Please sign in to comment.