Skip to content

Commit e522058

Browse files
committed
feat: use TestFlag for validation delay
1 parent 2e30240 commit e522058

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

stackslib/src/net/api/postblock_proposal.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use std::io::{Read, Write};
18+
#[cfg(any(test, feature = "testing"))]
19+
use std::sync::LazyLock;
1820
use std::thread::{self, JoinHandle, Thread};
1921
#[cfg(any(test, feature = "testing"))]
2022
use std::time::Duration;
@@ -35,6 +37,8 @@ use stacks_common::types::net::PeerHost;
3537
use stacks_common::types::StacksPublicKeyBuffer;
3638
use stacks_common::util::hash::{hex_bytes, to_hex, Hash160, Sha256Sum, Sha512Trunc256Sum};
3739
use stacks_common::util::retry::BoundReader;
40+
#[cfg(any(test, feature = "testing"))]
41+
use stacks_common::util::tests::TestFlag;
3842
use stacks_common::util::{get_epoch_time_ms, get_epoch_time_secs};
3943

4044
use crate::burnchains::affirmation::AffirmationMap;
@@ -67,11 +71,11 @@ use crate::net::{
6771
use crate::util_lib::db::Error as DBError;
6872

6973
#[cfg(any(test, feature = "testing"))]
70-
pub static TEST_VALIDATE_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
74+
pub static TEST_VALIDATE_STALL: LazyLock<TestFlag<bool>> = LazyLock::new(TestFlag::default);
7175
#[cfg(any(test, feature = "testing"))]
7276
/// Artificial delay to add to block validation.
73-
pub static TEST_VALIDATE_DELAY_DURATION_SECS: std::sync::Mutex<Option<u64>> =
74-
std::sync::Mutex::new(None);
77+
pub static TEST_VALIDATE_DELAY_DURATION_SECS: LazyLock<TestFlag<u64>> =
78+
LazyLock::new(TestFlag::default);
7579

7680
// This enum is used to supply a `reason_code` for validation
7781
// rejection responses. This is serialized as an enum with string
@@ -185,16 +189,9 @@ impl BlockValidateResponse {
185189
}
186190
}
187191

188-
#[cfg(any(test, feature = "testing"))]
189-
fn get_test_delay() -> Option<u64> {
190-
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().clone()
191-
}
192-
193192
#[cfg(any(test, feature = "testing"))]
194193
fn inject_validation_delay() {
195-
let Some(delay) = get_test_delay() else {
196-
return;
197-
};
194+
let delay = TEST_VALIDATE_DELAY_DURATION_SECS.get();
198195
warn!("Sleeping for {} seconds to simulate slow processing", delay);
199196
thread::sleep(Duration::from_secs(delay));
200197
}
@@ -379,10 +376,10 @@ impl NakamotoBlockProposal {
379376
) -> Result<BlockValidateOk, BlockValidateRejectReason> {
380377
#[cfg(any(test, feature = "testing"))]
381378
{
382-
if *TEST_VALIDATE_STALL.lock().unwrap() == Some(true) {
379+
if TEST_VALIDATE_STALL.get() {
383380
// Do an extra check just so we don't log EVERY time.
384381
warn!("Block validation is stalled due to testing directive.");
385-
while *TEST_VALIDATE_STALL.lock().unwrap() == Some(true) {
382+
while TEST_VALIDATE_STALL.get() {
386383
std::thread::sleep(std::time::Duration::from_millis(10));
387384
}
388385
info!("Block validation is no longer stalled due to testing directive.");

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ fn end_of_tenure() {
22632263
);
22642264

22652265
info!("------------------------- Test Block Validation Stalled -------------------------");
2266-
TEST_VALIDATE_STALL.lock().unwrap().replace(true);
2266+
TEST_VALIDATE_STALL.set(true);
22672267

22682268
let proposals_before = signer_test
22692269
.running_nodes
@@ -2335,7 +2335,7 @@ fn end_of_tenure() {
23352335

23362336
info!("Unpausing block validation and waiting for block to be processed");
23372337
// Disable the stall and wait for the block to be processed
2338-
TEST_VALIDATE_STALL.lock().unwrap().replace(false);
2338+
TEST_VALIDATE_STALL.set(false);
23392339
wait_for(short_timeout.as_secs(), || {
23402340
let processed_now = get_chain_info(&signer_test.running_nodes.conf).stacks_tip_height;
23412341
Ok(processed_now > blocks_before)
@@ -2831,7 +2831,7 @@ fn stx_transfers_dont_effect_idle_timeout() {
28312831
signer_test.boot_to_epoch_3();
28322832

28332833
// Add a delay to the block validation process
2834-
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().replace(5);
2834+
TEST_VALIDATE_DELAY_DURATION_SECS.set(5);
28352835

28362836
let info_before = signer_test.get_peer_info();
28372837
let blocks_before = signer_test.running_nodes.nakamoto_blocks_mined.get();
@@ -2975,7 +2975,7 @@ fn idle_tenure_extend_active_mining() {
29752975
signer_test.boot_to_epoch_3();
29762976

29772977
// Add a delay to the block validation process
2978-
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().replace(3);
2978+
TEST_VALIDATE_DELAY_DURATION_SECS.set(3);
29792979

29802980
signer_test.mine_nakamoto_block(Duration::from_secs(30), true);
29812981

@@ -7598,7 +7598,7 @@ fn block_validation_response_timeout() {
75987598
info!("------------------------- Test Mine and Verify Confirmed Nakamoto Block -------------------------");
75997599
signer_test.mine_and_verify_confirmed_naka_block(timeout, num_signers, true);
76007600
info!("------------------------- Test Block Validation Stalled -------------------------");
7601-
TEST_VALIDATE_STALL.lock().unwrap().replace(true);
7601+
TEST_VALIDATE_STALL.set(true);
76027602
let validation_stall_start = Instant::now();
76037603

76047604
let proposals_before = signer_test
@@ -7700,7 +7700,7 @@ fn block_validation_response_timeout() {
77007700
let info_before = info_after;
77017701
info!("Unpausing block validation");
77027702
// Disable the stall and wait for the block to be processed successfully
7703-
TEST_VALIDATE_STALL.lock().unwrap().replace(false);
7703+
TEST_VALIDATE_STALL.set(false);
77047704
wait_for(30, || {
77057705
let info = get_chain_info(&signer_test.running_nodes.conf);
77067706
Ok(info.stacks_tip_height > info_before.stacks_tip_height)
@@ -7770,10 +7770,7 @@ fn block_validation_pending_table() {
77707770
"db_path" => db_path.clone().to_str(),
77717771
);
77727772
signer_test.mine_and_verify_confirmed_naka_block(timeout, num_signers, true);
7773-
TEST_VALIDATE_DELAY_DURATION_SECS
7774-
.lock()
7775-
.unwrap()
7776-
.replace(30);
7773+
TEST_VALIDATE_DELAY_DURATION_SECS.set(30);
77777774

77787775
let signer_db = SignerDb::new(db_path).unwrap();
77797776

@@ -7853,7 +7850,7 @@ fn block_validation_pending_table() {
78537850
info!("----- Waiting for pending block validation to be submitted -----");
78547851

78557852
// Set the delay to 0 so that the block validation finishes quickly
7856-
*TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap() = None;
7853+
TEST_VALIDATE_DELAY_DURATION_SECS.set(0);
78577854

78587855
wait_for(30, || {
78597856
let proposal_responses = test_observer::get_proposal_responses();

0 commit comments

Comments
 (0)