Skip to content

Commit 90b6fb3

Browse files
committed
fix: dont remove pending validation in tests
1 parent 949088f commit 90b6fb3

File tree

4 files changed

+65
-46
lines changed

4 files changed

+65
-46
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,18 +1025,25 @@ impl SignerDb {
10251025

10261026
/// Get a pending block validation, sorted by the time at which it was added to the pending table.
10271027
/// If found, remove it from the pending table.
1028-
pub fn get_pending_block_validation(&self) -> Result<Option<Sha512Trunc256Sum>, DBError> {
1029-
let qry =
1030-
"SELECT signer_signature_hash FROM block_validations_pending ORDER BY added_time ASC";
1031-
let sighash_opt: Option<String> = query_row(&self.db, qry, params![])?;
1032-
if let Some(sighash) = sighash_opt {
1033-
let sighash = Sha512Trunc256Sum::from_hex(&sighash).map_err(|_| DBError::Corruption)?;
1028+
pub fn get_and_remove_pending_block_validation(
1029+
&self,
1030+
) -> Result<Option<Sha512Trunc256Sum>, DBError> {
1031+
if let Some(sighash) = self.get_pending_block_validation()? {
10341032
self.remove_pending_block_validation(&sighash)?;
10351033
return Ok(Some(sighash));
10361034
}
10371035
Ok(None)
10381036
}
10391037

1038+
/// Get a pending block validation, sorted by the time at which it was added to the pending table.
1039+
pub fn get_pending_block_validation(&self) -> Result<Option<Sha512Trunc256Sum>, DBError> {
1040+
let qry =
1041+
"SELECT signer_signature_hash FROM block_validations_pending ORDER BY added_time ASC";
1042+
let args = params![];
1043+
let sighash: Option<String> = query_row(&self.db, qry, args)?;
1044+
Ok(sighash.and_then(|sighash| Sha512Trunc256Sum::from_hex(&sighash).ok()))
1045+
}
1046+
10401047
/// Remove a pending block validation
10411048
pub fn remove_pending_block_validation(
10421049
&self,
@@ -1067,9 +1074,20 @@ impl SignerDb {
10671074
pub fn get_all_pending_block_validations(
10681075
&self,
10691076
) -> Result<Vec<PendingBlockValidation>, DBError> {
1070-
let qry = "SELECT signer_signature_hash, added_time FROM block_validations_pending";
1071-
let args = params![];
1072-
query_rows(&self.db, qry, args)
1077+
let qry = "SELECT signer_signature_hash, added_time FROM block_validations_pending ORDER BY added_time ASC";
1078+
query_rows(&self.db, qry, params![])
1079+
}
1080+
1081+
/// For tests, check if a pending block validation exists
1082+
#[cfg(any(test, feature = "testing"))]
1083+
pub fn has_pending_block_validation(
1084+
&self,
1085+
sighash: &Sha512Trunc256Sum,
1086+
) -> Result<bool, DBError> {
1087+
let qry = "SELECT signer_signature_hash FROM block_validations_pending WHERE signer_signature_hash = ?1";
1088+
let args = params![sighash.to_string()];
1089+
let sighash_opt: Option<String> = query_row(&self.db, qry, args)?;
1090+
Ok(sighash_opt.is_some())
10731091
}
10741092

10751093
/// Return the start time (epoch time in seconds) and the processing time in milliseconds of the tenure (idenfitied by consensus_hash).

stacks-signer/src/v0/signer.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,14 @@ impl Signer {
533533
// from other signers to push the proposed block into a global rejection/acceptance regardless of our participation.
534534
// However, we will not be able to participate beyond this until our block submission times out or we receive a response
535535
// from our node.
536-
warn!("{self}: cannot submit block proposal for validation as we are already waiting for a response for a prior submission")
536+
warn!("{self}: cannot submit block proposal for validation as we are already waiting for a response for a prior submission. Inserting pending proposal.";
537+
"signer_signature_hash" => signer_signature_hash.to_string(),
538+
);
539+
self.signer_db
540+
.insert_pending_block_validation(&signer_signature_hash, get_epoch_time_secs())
541+
.unwrap_or_else(|e| {
542+
warn!("{self}: Failed to insert pending block validation: {e:?}")
543+
});
537544
}
538545

539546
// Do not store KNOWN invalid blocks as this could DOS the signer. We only store blocks that are valid or unknown.
@@ -726,7 +733,7 @@ impl Signer {
726733
}
727734

728735
// Check if there is a pending block validation that we need to submit to the node
729-
match self.signer_db.get_pending_block_validation() {
736+
match self.signer_db.get_and_remove_pending_block_validation() {
730737
Ok(Some(signer_sig_hash)) => {
731738
info!("{self}: Found a pending block validation: {signer_sig_hash:?}");
732739
match self.signer_db.block_lookup(&signer_sig_hash) {

stackslib/src/net/api/postblock_proposal.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ impl BlockValidateResponse {
185185
}
186186
}
187187

188+
#[cfg(any(test, feature = "testing"))]
189+
fn get_test_delay() -> Option<u64> {
190+
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().clone()
191+
}
192+
193+
#[cfg(any(test, feature = "testing"))]
194+
fn inject_validation_delay() {
195+
if let Some(delay) = get_test_delay() {
196+
warn!("Sleeping for {} seconds to simulate slow processing", delay);
197+
thread::sleep(Duration::from_secs(delay));
198+
}
199+
}
200+
188201
/// Represents a block proposed to the `v3/block_proposal` endpoint for validation
189202
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
190203
pub struct NakamotoBlockProposal {
@@ -377,12 +390,7 @@ impl NakamotoBlockProposal {
377390
let start = Instant::now();
378391

379392
#[cfg(any(test, feature = "testing"))]
380-
{
381-
if let Some(delay) = *TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap() {
382-
warn!("Sleeping for {} seconds to simulate slow processing", delay);
383-
thread::sleep(Duration::from_secs(delay));
384-
}
385-
}
393+
inject_validation_delay();
386394

387395
let mainnet = self.chain_id == CHAIN_ID_MAINNET;
388396
if self.chain_id != chainstate.chain_id || mainnet != chainstate.mainnet {

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

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7796,9 +7796,7 @@ fn block_validation_pending_table() {
77967796
let mut signer_test: SignerTest<SpawnedSigner> = SignerTest::new_with_config_modifications(
77977797
num_signers,
77987798
vec![(sender_addr, send_amt + send_fee)],
7799-
|config| {
7800-
config.block_proposal_validation_timeout = timeout;
7801-
},
7799+
|_| {},
78027800
|_| {},
78037801
None,
78047802
None,
@@ -7869,44 +7867,33 @@ fn block_validation_pending_table() {
78697867
let mut last_log = Instant::now();
78707868
last_log -= Duration::from_secs(5);
78717869
wait_for(120, || {
7872-
let sighash = match signer_db.get_pending_block_validation() {
7873-
Ok(Some(sighash)) => sighash,
7874-
Err(e) => {
7875-
error!("Failed to get pending block validation: {e}");
7876-
panic!("Failed to get pending block validation");
7877-
}
7878-
Ok(None) => {
7879-
if last_log.elapsed() > Duration::from_secs(5) {
7880-
info!("----- No pending block validations found -----");
7881-
last_log = Instant::now();
7882-
}
7883-
return Ok(false);
7884-
}
7885-
};
7886-
if last_log.elapsed() > Duration::from_secs(5) && sighash != block_signer_signature_hash {
7870+
let is_pending = signer_db
7871+
.has_pending_block_validation(&block_signer_signature_hash)
7872+
.expect("Unexpected DBError");
7873+
if last_log.elapsed() > Duration::from_secs(5) && !is_pending {
78877874
let pending_block_validations = signer_db
78887875
.get_all_pending_block_validations()
78897876
.expect("Failed to get pending block validations");
78907877
info!(
7891-
"----- Received a different pending block proposal -----";
7892-
"db_signer_signature_hash" => sighash.to_hex(),
7878+
"----- Waiting for pending block proposal in SignerDB -----";
78937879
"proposed_signer_signature_hash" => block_signer_signature_hash.to_hex(),
7880+
"pending_block_validations_len" => pending_block_validations.len(),
78947881
"pending_block_validations" => pending_block_validations.iter()
78957882
.map(|p| p.signer_signature_hash.to_hex())
78967883
.collect::<Vec<String>>()
78977884
.join(", "),
78987885
);
78997886
last_log = Instant::now();
79007887
}
7901-
Ok(sighash == block_signer_signature_hash)
7888+
Ok(is_pending)
79027889
})
79037890
.expect("Timed out waiting for pending block proposal");
79047891

7905-
// Set the delay to 0 so that the block validation finishes quickly
7906-
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().take();
7907-
79087892
info!("----- Waiting for pending block validation to be submitted -----");
79097893

7894+
// Set the delay to 0 so that the block validation finishes quickly
7895+
*TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap() = None;
7896+
79107897
wait_for(30, || {
79117898
let proposal_responses = test_observer::get_proposal_responses();
79127899
let found_proposal = proposal_responses
@@ -7918,11 +7905,10 @@ fn block_validation_pending_table() {
79187905

79197906
info!("----- Waiting for pending block validation to be removed -----");
79207907
wait_for(30, || {
7921-
let Ok(Some(sighash)) = signer_db.get_pending_block_validation() else {
7922-
// There are no pending block validations
7923-
return Ok(true);
7924-
};
7925-
Ok(sighash != block_signer_signature_hash)
7908+
let is_pending = signer_db
7909+
.has_pending_block_validation(&block_signer_signature_hash)
7910+
.expect("Unexpected DBError");
7911+
Ok(!is_pending)
79267912
})
79277913
.expect("Timed out waiting for pending block validation to be removed");
79287914

0 commit comments

Comments
 (0)