Skip to content

Commit e250a95

Browse files
authored
Merge pull request #5811 from stacks-network/feat/signer-indexes
fix: signerDB index improvements
2 parents d39ff29 + 7a15f92 commit e250a95

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

stacks-signer/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
99

1010
## Added
1111

12-
- Introduced the `reorg_attempts_activity_timeout_ms` configuration option for signers which is used to determine the length of time after the last block of a tenure is confirmed that an incoming miner's attempts to reorg it are considered valid miner activity.
12+
- Introduced the `reorg_attempts_activity_timeout_ms` configuration option for signers which is used to determine the length of time after the last block of a tenure is confirmed that an incoming miner's attempts to reorg it are considered valid miner activity.
1313

1414
### Changed
1515

1616
- Increase default `block_proposal_timeout_ms` from 10 minutes to 4 hours. Until #5729 is implemented, there is no value in rejecting a late block from a miner, since a late block is better than no block at all.
17-
1817
- Signers no longer view any block proposal by a miner in their DB as indicative of valid miner activity.
18+
- Various index improvements to the signer's database to improve performance.
1919

2020
## [3.1.0.0.5.0]
2121

stacks-signer/src/signerdb.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ static CREATE_INDEXES_6: &str = r#"
348348
CREATE INDEX IF NOT EXISTS block_validations_pending_on_added_time ON block_validations_pending(added_time ASC);
349349
"#;
350350

351+
static CREATE_INDEXES_8: &str = r#"
352+
-- Add new index for get_last_globally_accepted_block query
353+
CREATE INDEX IF NOT EXISTS blocks_consensus_hash_state_height ON blocks (consensus_hash, state, stacks_height DESC);
354+
355+
-- Add new index for get_canonical_tip query
356+
CREATE INDEX IF NOT EXISTS blocks_state_height_signed_group ON blocks (state, stacks_height DESC, signed_group DESC);
357+
358+
-- Index for get_first_signed_block_in_tenure
359+
CREATE INDEX IF NOT EXISTS blocks_consensus_hash_status_height ON blocks (consensus_hash, signed_over, stacks_height ASC);
360+
361+
-- Index for has_unprocessed_blocks
362+
CREATE INDEX IF NOT EXISTS blocks_reward_cycle_state on blocks (reward_cycle, state);
363+
"#;
364+
351365
static CREATE_SIGNER_STATE_TABLE: &str = "
352366
CREATE TABLE IF NOT EXISTS signer_states (
353367
reward_cycle INTEGER PRIMARY KEY,
@@ -545,9 +559,14 @@ static SCHEMA_7: &[&str] = &[
545559
"INSERT OR REPLACE INTO db_config (version) VALUES (7);",
546560
];
547561

562+
static SCHEMA_8: &[&str] = &[
563+
CREATE_INDEXES_8,
564+
"INSERT INTO db_config (version) VALUES (8);",
565+
];
566+
548567
impl SignerDb {
549568
/// The current schema version used in this build of the signer binary.
550-
pub const SCHEMA_VERSION: u32 = 7;
569+
pub const SCHEMA_VERSION: u32 = 8;
551570

552571
/// Create a new `SignerState` instance.
553572
/// This will create a new SQLite database at the given path
@@ -675,6 +694,20 @@ impl SignerDb {
675694
Ok(())
676695
}
677696

697+
/// Migrate from schema 7 to schema 8
698+
fn schema_8_migration(tx: &Transaction) -> Result<(), DBError> {
699+
if Self::get_schema_version(tx)? >= 8 {
700+
// no migration necessary
701+
return Ok(());
702+
}
703+
704+
for statement in SCHEMA_8.iter() {
705+
tx.execute_batch(statement)?;
706+
}
707+
708+
Ok(())
709+
}
710+
678711
/// Register custom scalar functions used by the database
679712
fn register_scalar_functions(&self) -> Result<(), DBError> {
680713
// Register helper function for determining if a block is a tenure change transaction
@@ -715,7 +748,8 @@ impl SignerDb {
715748
4 => Self::schema_5_migration(&sql_tx)?,
716749
5 => Self::schema_6_migration(&sql_tx)?,
717750
6 => Self::schema_7_migration(&sql_tx)?,
718-
7 => break,
751+
7 => Self::schema_8_migration(&sql_tx)?,
752+
8 => break,
719753
x => return Err(DBError::Other(format!(
720754
"Database schema is newer than supported by this binary. Expected version = {}, Database version = {x}",
721755
Self::SCHEMA_VERSION,

0 commit comments

Comments
 (0)