Skip to content

Commit 8d6ffe3

Browse files
committed
feat: various sql improvements from code review
Thanks @Jiloc!
1 parent 67560d8 commit 8d6ffe3

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -660,17 +660,13 @@ CREATE TABLE IF NOT EXISTS block_signatures (
660660
-- the sighash is sufficient to uniquely identify the block across all burnchain, PoX,
661661
-- and stacks forks.
662662
signer_signature_hash TEXT NOT NULL,
663-
-- the signer address that rejected the block
663+
-- the signer address that signed the block
664664
signer_addr TEXT NOT NULL,
665665
-- signature itself
666666
signature TEXT NOT NULL,
667-
PRIMARY KEY (signer_addr, signer_signature_hash)
667+
PRIMARY KEY (signer_signature_hash, signer_addr)
668668
) STRICT;"#;
669669

670-
static CREATE_BLOCK_SIGNATURES_INDEX: &str = r#"
671-
CREATE INDEX IF NOT EXISTS idx_block_signatures_by_sighash ON block_signatures(signer_signature_hash);
672-
"#;
673-
674670
static DROP_BLOCK_REJECTION_SIGNER_ADDRS: &str = r#"
675671
DROP TABLE IF EXISTS block_rejection_signer_addrs;
676672
"#;
@@ -686,13 +682,9 @@ CREATE TABLE IF NOT EXISTS block_rejection_signer_addrs (
686682
signer_addr TEXT NOT NULL,
687683
-- the reject reason code
688684
reject_code INTEGER NOT NULL,
689-
PRIMARY KEY (signer_addr, signer_signature_hash)
685+
PRIMARY KEY (signer_signature_hash, signer_addr)
690686
) STRICT;"#;
691687

692-
static CREATE_BLOCK_REJECTION_SIGNER_ADDRS_INDEX: &str = r#"
693-
CREATE INDEX IF NOT EXISTS idx_block_rejection_signer_addrs_by_sighash ON block_rejection_signer_addrs(signer_signature_hash);
694-
"#;
695-
696688
static SCHEMA_1: &[&str] = &[
697689
DROP_SCHEMA_0,
698690
CREATE_DB_CONFIG,
@@ -806,10 +798,8 @@ static SCHEMA_16: &[&str] = &[
806798
static SCHEMA_17: &[&str] = &[
807799
DROP_BLOCK_SIGNATURES_TABLE,
808800
CREATE_BLOCK_SIGNATURES_TABLE_V17,
809-
CREATE_BLOCK_SIGNATURES_INDEX,
810801
DROP_BLOCK_REJECTION_SIGNER_ADDRS,
811802
CREATE_BLOCK_REJECTION_SIGNER_ADDRS_V17,
812-
CREATE_BLOCK_REJECTION_SIGNER_ADDRS_INDEX,
813803
"INSERT INTO db_config (version) VALUES (17);",
814804
];
815805

@@ -1360,13 +1350,23 @@ impl SignerDb {
13601350
signer_addr.to_string(),
13611351
serde_json::to_string(signature).map_err(DBError::SerializationError)?
13621352
];
1363-
1364-
debug!("Inserting block signature.";
1365-
"signer_signature_hash" => %block_sighash,
1366-
"signature" => %signature);
1367-
13681353
let rows_added = self.db.execute(qry, args)?;
1369-
Ok(rows_added > 0)
1354+
1355+
let is_new_signature = rows_added > 0;
1356+
if is_new_signature {
1357+
debug!("Added block signature.";
1358+
"signer_signature_hash" => %block_sighash,
1359+
"signer_address" => %signer_addr,
1360+
"signature" => %signature
1361+
);
1362+
} else {
1363+
debug!("Duplicate block signature.";
1364+
"signer_signature_hash" => %block_sighash,
1365+
"signer_address" => %signer_addr,
1366+
"signature" => %signature
1367+
);
1368+
}
1369+
Ok(is_new_signature)
13701370
}
13711371

13721372
/// Get all signatures for a block
@@ -1391,15 +1391,15 @@ impl SignerDb {
13911391
reject_reason: &RejectReason,
13921392
) -> Result<bool, DBError> {
13931393
// If this signer/block already has a signature, do not allow a rejection
1394-
let sig_qry = "SELECT 1 FROM block_signatures WHERE signer_signature_hash = ?1 AND signer_addr = ?2 LIMIT 1";
1394+
let sig_qry = "SELECT EXISTS(SELECT 1 FROM block_signatures WHERE signer_signature_hash = ?1 AND signer_addr = ?2)";
13951395
let sig_args = params![block_sighash, addr.to_string()];
1396-
let has_signature: Option<i64> = self
1397-
.db
1398-
.query_row(sig_qry, sig_args, |row| row.get(0))
1399-
.optional()?;
1400-
if has_signature.is_some() {
1401-
warn!("Cannot add block rejection for signer {} and block {} because a signature already exists.",
1402-
addr, block_sighash);
1396+
let exists = self.db.query_row(sig_qry, sig_args, |row| row.get(0))?;
1397+
if exists {
1398+
warn!("Cannot add block rejection because a signature already exists.";
1399+
"signer_signature_hash" => %block_sighash,
1400+
"signer_address" => %addr,
1401+
"reject_reason" => %reject_reason
1402+
);
14031403
return Ok(false);
14041404
}
14051405

@@ -1414,6 +1414,11 @@ impl SignerDb {
14141414
match existing_code {
14151415
Some(code) if code == reject_code => {
14161416
// Row exists with same reject_reason, do nothing
1417+
debug!("Duplicate block rejection.";
1418+
"signer_signature_hash" => %block_sighash,
1419+
"signer_address" => %addr,
1420+
"reject_reason" => %reject_reason
1421+
);
14171422
Ok(false)
14181423
}
14191424
Some(_) => {

0 commit comments

Comments
 (0)