Skip to content

Commit

Permalink
feat: use TestFlag for validation delay
Browse files Browse the repository at this point in the history
  • Loading branch information
hstove committed Dec 19, 2024
1 parent 2e30240 commit e522058
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
23 changes: 10 additions & 13 deletions stackslib/src/net/api/postblock_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::io::{Read, Write};
#[cfg(any(test, feature = "testing"))]
use std::sync::LazyLock;
use std::thread::{self, JoinHandle, Thread};
#[cfg(any(test, feature = "testing"))]
use std::time::Duration;
Expand All @@ -35,6 +37,8 @@ use stacks_common::types::net::PeerHost;
use stacks_common::types::StacksPublicKeyBuffer;
use stacks_common::util::hash::{hex_bytes, to_hex, Hash160, Sha256Sum, Sha512Trunc256Sum};
use stacks_common::util::retry::BoundReader;
#[cfg(any(test, feature = "testing"))]
use stacks_common::util::tests::TestFlag;
use stacks_common::util::{get_epoch_time_ms, get_epoch_time_secs};

use crate::burnchains::affirmation::AffirmationMap;
Expand Down Expand Up @@ -67,11 +71,11 @@ use crate::net::{
use crate::util_lib::db::Error as DBError;

#[cfg(any(test, feature = "testing"))]
pub static TEST_VALIDATE_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
pub static TEST_VALIDATE_STALL: LazyLock<TestFlag<bool>> = LazyLock::new(TestFlag::default);
#[cfg(any(test, feature = "testing"))]
/// Artificial delay to add to block validation.
pub static TEST_VALIDATE_DELAY_DURATION_SECS: std::sync::Mutex<Option<u64>> =
std::sync::Mutex::new(None);
pub static TEST_VALIDATE_DELAY_DURATION_SECS: LazyLock<TestFlag<u64>> =
LazyLock::new(TestFlag::default);

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

#[cfg(any(test, feature = "testing"))]
fn get_test_delay() -> Option<u64> {
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().clone()
}

#[cfg(any(test, feature = "testing"))]
fn inject_validation_delay() {
let Some(delay) = get_test_delay() else {
return;
};
let delay = TEST_VALIDATE_DELAY_DURATION_SECS.get();
warn!("Sleeping for {} seconds to simulate slow processing", delay);
thread::sleep(Duration::from_secs(delay));
}
Expand Down Expand Up @@ -379,10 +376,10 @@ impl NakamotoBlockProposal {
) -> Result<BlockValidateOk, BlockValidateRejectReason> {
#[cfg(any(test, feature = "testing"))]
{
if *TEST_VALIDATE_STALL.lock().unwrap() == Some(true) {
if TEST_VALIDATE_STALL.get() {
// Do an extra check just so we don't log EVERY time.
warn!("Block validation is stalled due to testing directive.");
while *TEST_VALIDATE_STALL.lock().unwrap() == Some(true) {
while TEST_VALIDATE_STALL.get() {
std::thread::sleep(std::time::Duration::from_millis(10));
}
info!("Block validation is no longer stalled due to testing directive.");
Expand Down
19 changes: 8 additions & 11 deletions testnet/stacks-node/src/tests/signer/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,7 @@ fn end_of_tenure() {
);

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

let proposals_before = signer_test
.running_nodes
Expand Down Expand Up @@ -2335,7 +2335,7 @@ fn end_of_tenure() {

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

// Add a delay to the block validation process
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().replace(5);
TEST_VALIDATE_DELAY_DURATION_SECS.set(5);

let info_before = signer_test.get_peer_info();
let blocks_before = signer_test.running_nodes.nakamoto_blocks_mined.get();
Expand Down Expand Up @@ -2975,7 +2975,7 @@ fn idle_tenure_extend_active_mining() {
signer_test.boot_to_epoch_3();

// Add a delay to the block validation process
TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap().replace(3);
TEST_VALIDATE_DELAY_DURATION_SECS.set(3);

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

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

let proposals_before = signer_test
Expand Down Expand Up @@ -7700,7 +7700,7 @@ fn block_validation_response_timeout() {
let info_before = info_after;
info!("Unpausing block validation");
// Disable the stall and wait for the block to be processed successfully
TEST_VALIDATE_STALL.lock().unwrap().replace(false);
TEST_VALIDATE_STALL.set(false);
wait_for(30, || {
let info = get_chain_info(&signer_test.running_nodes.conf);
Ok(info.stacks_tip_height > info_before.stacks_tip_height)
Expand Down Expand Up @@ -7770,10 +7770,7 @@ fn block_validation_pending_table() {
"db_path" => db_path.clone().to_str(),
);
signer_test.mine_and_verify_confirmed_naka_block(timeout, num_signers, true);
TEST_VALIDATE_DELAY_DURATION_SECS
.lock()
.unwrap()
.replace(30);
TEST_VALIDATE_DELAY_DURATION_SECS.set(30);

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

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

// Set the delay to 0 so that the block validation finishes quickly
*TEST_VALIDATE_DELAY_DURATION_SECS.lock().unwrap() = None;
TEST_VALIDATE_DELAY_DURATION_SECS.set(0);

wait_for(30, || {
let proposal_responses = test_observer::get_proposal_responses();
Expand Down

0 comments on commit e522058

Please sign in to comment.