Skip to content

Commit c8f1d99

Browse files
committed
fix: use signed_group as tiebreaker for canonical tip
1 parent 2e963e7 commit c8f1d99

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ static CREATE_INDEXES_4: &str = r#"
328328
CREATE INDEX IF NOT EXISTS blocks_state ON blocks ((json_extract(block_info, '$.state')));
329329
"#;
330330

331+
static CREATE_INDEXES_5: &str = r#"
332+
CREATE INDEX IF NOT EXISTS blocks_signed_group ON blocks ((json_extract(block_info, '$.signed_group')));
333+
"#;
334+
331335
static CREATE_SIGNER_STATE_TABLE: &str = "
332336
CREATE TABLE IF NOT EXISTS signer_states (
333337
reward_cycle INTEGER PRIMARY KEY,
@@ -430,6 +434,11 @@ static SCHEMA_4: &[&str] = &[
430434
"INSERT OR REPLACE INTO db_config (version) VALUES (4);",
431435
];
432436

437+
static SCHEMA_5: &[&str] = &[
438+
CREATE_INDEXES_5,
439+
"INSERT OR REPLACE INTO db_config (version) VALUES (5);",
440+
];
441+
433442
impl SignerDb {
434443
/// The current schema version used in this build of the signer binary.
435444
pub const SCHEMA_VERSION: u32 = 4;
@@ -518,6 +527,20 @@ impl SignerDb {
518527
Ok(())
519528
}
520529

530+
/// Migrate from schema 4 to schema 5
531+
fn schema_5_migration(tx: &Transaction) -> Result<(), DBError> {
532+
if Self::get_schema_version(tx)? >= 5 {
533+
// no migration necessary
534+
return Ok(());
535+
}
536+
537+
for statement in SCHEMA_5.iter() {
538+
tx.execute_batch(statement)?;
539+
}
540+
541+
Ok(())
542+
}
543+
521544
/// Either instantiate a new database, or migrate an existing one
522545
/// If the detected version of the existing database is 0 (i.e., a pre-migration
523546
/// logic DB, the DB will be dropped).
@@ -530,7 +553,8 @@ impl SignerDb {
530553
1 => Self::schema_2_migration(&sql_tx)?,
531554
2 => Self::schema_3_migration(&sql_tx)?,
532555
3 => Self::schema_4_migration(&sql_tx)?,
533-
4 => break,
556+
4 => Self::schema_5_migration(&sql_tx)?,
557+
5 => break,
534558
x => return Err(DBError::Other(format!(
535559
"Database schema is newer than supported by this binary. Expected version = {}, Database version = {x}",
536560
Self::SCHEMA_VERSION,
@@ -642,7 +666,7 @@ impl SignerDb {
642666

643667
/// Return the canonical tip -- the last globally accepted block.
644668
pub fn get_canonical_tip(&self) -> Result<Option<BlockInfo>, DBError> {
645-
let query = "SELECT block_info FROM blocks WHERE json_extract(block_info, '$.state') = ?1 ORDER BY stacks_height DESC LIMIT 1";
669+
let query = "SELECT block_info FROM blocks WHERE json_extract(block_info, '$.state') = ?1 ORDER BY stacks_height DESC, json_extract(block_info, '$.signed_group') DESC LIMIT 1";
646670
let args = params![&BlockState::GloballyAccepted.to_string()];
647671
let result: Option<String> = query_row(&self.db, query, args)?;
648672

0 commit comments

Comments
 (0)