Skip to content

Commit 3296f98

Browse files
committed
fix: check if db_version exists to determine staging_blocks schema
1 parent 4152da0 commit 3296f98

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

stackslib/src/chainstate/nakamoto/staging_blocks.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::chainstate::stacks::{Error as ChainstateError, StacksBlock, StacksBlo
3535
use crate::stacks_common::codec::StacksMessageCodec;
3636
use crate::util_lib::db::{
3737
query_int, query_row, query_row_columns, query_row_panic, query_rows, sqlite_open,
38-
tx_begin_immediate, u64_to_sql, DBConn, Error as DBError, FromRow,
38+
table_exists, tx_begin_immediate, u64_to_sql, DBConn, Error as DBError, FromRow,
3939
};
4040

4141
/// The means by which a block is obtained.
@@ -666,13 +666,17 @@ impl StacksChainState {
666666
pub fn get_nakamoto_staging_blocks_db_version(
667667
conn: &Connection,
668668
) -> Result<u32, ChainstateError> {
669+
let db_version_exists = table_exists(&conn, "db_version")?;
670+
if !db_version_exists {
671+
return Ok(1);
672+
}
669673
let qry = "SELECT version FROM db_version ORDER BY version DESC LIMIT 1";
670674
let args = NO_PARAMS;
671675
let version: Option<i64> = match query_row(&conn, qry, args) {
672676
Ok(x) => x,
673677
Err(e) => {
674678
debug!("Failed to get Nakamoto staging blocks DB version: {:?}", &e);
675-
return Ok(1);
679+
return Err(ChainstateError::DBError(DBError::Corruption));
676680
}
677681
};
678682

@@ -684,7 +688,7 @@ impl StacksChainState {
684688
}
685689
None => {
686690
debug!("No version present in Nakamoto staging blocks DB; defaulting to 1");
687-
Ok(1)
691+
Err(ChainstateError::DBError(DBError::Corruption))
688692
}
689693
}
690694
}

0 commit comments

Comments
 (0)