Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: signerDB index improvements #5811

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stacks-signer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE

## Added

- 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.
- 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.

### Changed

- 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.

- Signers no longer view any block proposal by a miner in their DB as indicative of valid miner activity.
- Various index improvements to the signer's database to improve performance.

## [3.1.0.0.5.0]

Expand Down
38 changes: 36 additions & 2 deletions stacks-signer/src/signerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ static CREATE_INDEXES_6: &str = r#"
CREATE INDEX IF NOT EXISTS block_validations_pending_on_added_time ON block_validations_pending(added_time ASC);
"#;

static CREATE_INDEXES_8: &str = r#"
-- Add new index for get_last_globally_accepted_block query
CREATE INDEX IF NOT EXISTS blocks_consensus_hash_state_height ON blocks (consensus_hash, state, stacks_height DESC);

-- Add new index for get_canonical_tip query
CREATE INDEX IF NOT EXISTS blocks_state_height_signed_group ON blocks (state, stacks_height DESC, signed_group DESC);

-- Index for get_first_signed_block_in_tenure
CREATE INDEX IF NOT EXISTS blocks_consensus_hash_status_height ON blocks (consensus_hash, signed_over, stacks_height ASC);

-- Index for has_unprocessed_blocks
CREATE INDEX IF NOT EXISTS blocks_reward_cycle_state on blocks (reward_cycle, state);
"#;

static CREATE_SIGNER_STATE_TABLE: &str = "
CREATE TABLE IF NOT EXISTS signer_states (
reward_cycle INTEGER PRIMARY KEY,
Expand Down Expand Up @@ -545,9 +559,14 @@ static SCHEMA_7: &[&str] = &[
"INSERT OR REPLACE INTO db_config (version) VALUES (7);",
];

static SCHEMA_8: &[&str] = &[
CREATE_INDEXES_8,
"INSERT INTO db_config (version) VALUES (8);",
];

impl SignerDb {
/// The current schema version used in this build of the signer binary.
pub const SCHEMA_VERSION: u32 = 7;
pub const SCHEMA_VERSION: u32 = 8;

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

/// Migrate from schema 7 to schema 8
fn schema_8_migration(tx: &Transaction) -> Result<(), DBError> {
if Self::get_schema_version(tx)? >= 8 {
// no migration necessary
return Ok(());
}

for statement in SCHEMA_8.iter() {
tx.execute_batch(statement)?;
}

Ok(())
}

/// Register custom scalar functions used by the database
fn register_scalar_functions(&self) -> Result<(), DBError> {
// Register helper function for determining if a block is a tenure change transaction
Expand Down Expand Up @@ -715,7 +748,8 @@ impl SignerDb {
4 => Self::schema_5_migration(&sql_tx)?,
5 => Self::schema_6_migration(&sql_tx)?,
6 => Self::schema_7_migration(&sql_tx)?,
7 => break,
7 => Self::schema_8_migration(&sql_tx)?,
8 => break,
x => return Err(DBError::Other(format!(
"Database schema is newer than supported by this binary. Expected version = {}, Database version = {x}",
Self::SCHEMA_VERSION,
Expand Down
Loading