Skip to content

Commit ae7c822

Browse files
committed
fix: delete and return pending row in one statement
1 parent 0c90997 commit ae7c822

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,11 +1028,11 @@ impl SignerDb {
10281028
pub fn get_and_remove_pending_block_validation(
10291029
&self,
10301030
) -> Result<Option<Sha512Trunc256Sum>, DBError> {
1031-
if let Some(sighash) = self.get_pending_block_validation()? {
1032-
self.remove_pending_block_validation(&sighash)?;
1033-
return Ok(Some(sighash));
1034-
}
1035-
Ok(None)
1031+
let qry = "DELETE FROM block_validations_pending WHERE signer_signature_hash = (SELECT signer_signature_hash FROM block_validations_pending ORDER BY added_time ASC LIMIT 1) RETURNING signer_signature_hash";
1032+
let args = params![];
1033+
let mut stmt = self.db.prepare(qry)?;
1034+
let sighash: Option<String> = stmt.query_row(args, |row| row.get(0)).optional()?;
1035+
Ok(sighash.and_then(|sighash| Sha512Trunc256Sum::from_hex(&sighash).ok()))
10361036
}
10371037

10381038
/// Get a pending block validation, sorted by the time at which it was added to the pending table.
@@ -1975,4 +1975,38 @@ mod tests {
19751975
block_info_3
19761976
);
19771977
}
1978+
1979+
#[test]
1980+
fn test_get_and_remove_pending_block_validation() {
1981+
let db_path = tmp_db_path();
1982+
let db = SignerDb::new(db_path).expect("Failed to create signer db");
1983+
1984+
let pending_hash = db.get_and_remove_pending_block_validation().unwrap();
1985+
assert!(pending_hash.is_none());
1986+
1987+
db.insert_pending_block_validation(&Sha512Trunc256Sum([0x01; 32]), 1000)
1988+
.unwrap();
1989+
db.insert_pending_block_validation(&Sha512Trunc256Sum([0x02; 32]), 2000)
1990+
.unwrap();
1991+
db.insert_pending_block_validation(&Sha512Trunc256Sum([0x03; 32]), 3000)
1992+
.unwrap();
1993+
1994+
let pending_hash = db.get_and_remove_pending_block_validation().unwrap();
1995+
assert_eq!(pending_hash, Some(Sha512Trunc256Sum([0x01; 32])));
1996+
1997+
let pendings = db.get_all_pending_block_validations().unwrap();
1998+
assert_eq!(pendings.len(), 2);
1999+
2000+
let pending_hash = db.get_and_remove_pending_block_validation().unwrap();
2001+
assert_eq!(pending_hash, Some(Sha512Trunc256Sum([0x02; 32])));
2002+
2003+
let pendings = db.get_all_pending_block_validations().unwrap();
2004+
assert_eq!(pendings.len(), 1);
2005+
2006+
let pending_hash = db.get_and_remove_pending_block_validation().unwrap();
2007+
assert_eq!(pending_hash, Some(Sha512Trunc256Sum([0x03; 32])));
2008+
2009+
let pendings = db.get_all_pending_block_validations().unwrap();
2010+
assert_eq!(pendings.len(), 0);
2011+
}
19782012
}

testnet/stacks-node/src/tests/signer/v0.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9924,7 +9924,7 @@ fn no_reorg_due_to_successive_block_validation_ok() {
99249924
debug!("Miner 1 mined block N: {block_n_signature_hash}");
99259925

99269926
info!("------------------------- Pause Block Validation Response of N+1 -------------------------");
9927-
TEST_VALIDATE_STALL.lock().unwrap().replace(true);
9927+
TEST_VALIDATE_STALL.set(true);
99289928
let proposals_before_2 = rl2_proposals.load(Ordering::SeqCst);
99299929
let rejections_before_2 = rl2_rejections.load(Ordering::SeqCst);
99309930
let blocks_before = test_observer::get_blocks().len();
@@ -10059,7 +10059,7 @@ fn no_reorg_due_to_successive_block_validation_ok() {
1005910059

1006010060
info!("------------------------- Unpause Block Validation Response of N+1 -------------------------");
1006110061

10062-
TEST_VALIDATE_STALL.lock().unwrap().replace(false);
10062+
TEST_VALIDATE_STALL.set(false);
1006310063

1006410064
// Verify that the node accepted the proposed N+1, sending back a validate ok response
1006510065
wait_for(30, || {

0 commit comments

Comments
 (0)