Skip to content

Commit 2e1b832

Browse files
committed
Add rejected_blocks_count_towards_miner_validity test
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent bb48447 commit 2e1b832

File tree

4 files changed

+239
-41
lines changed

4 files changed

+239
-41
lines changed

stackslib/src/net/api/postblock_proposal.rs

Lines changed: 7 additions & 3 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,7 +71,7 @@ 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.
7377
pub static TEST_VALIDATE_DELAY_DURATION_SECS: std::sync::Mutex<Option<u64>> =
@@ -353,10 +357,10 @@ impl NakamotoBlockProposal {
353357
) -> Result<BlockValidateOk, BlockValidateRejectReason> {
354358
#[cfg(any(test, feature = "testing"))]
355359
{
356-
if *TEST_VALIDATE_STALL.lock().unwrap() == Some(true) {
360+
if TEST_VALIDATE_STALL.get() {
357361
// Do an extra check just so we don't log EVERY time.
358362
warn!("Block validation is stalled due to testing directive.");
359-
while *TEST_VALIDATE_STALL.lock().unwrap() == Some(true) {
363+
while TEST_VALIDATE_STALL.get() {
360364
std::thread::sleep(std::time::Duration::from_millis(10));
361365
}
362366
info!(

testnet/stacks-node/src/nakamoto_node/miner.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
//
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
#[cfg(test)]
17+
use std::sync::LazyLock;
1618
use std::thread;
1719
use std::thread::JoinHandle;
1820
use std::time::{Duration, Instant};
@@ -42,6 +44,8 @@ use stacks::net::stackerdb::StackerDBs;
4244
use stacks::net::{NakamotoBlocksData, StacksMessageType};
4345
use stacks::util::get_epoch_time_secs;
4446
use stacks::util::secp256k1::MessageSignature;
47+
#[cfg(test)]
48+
use stacks::util::tests::TestFlag;
4549
use stacks_common::types::chainstate::{StacksAddress, StacksBlockId};
4650
use stacks_common::types::{PrivateKey, StacksEpochId};
4751
use stacks_common::util::vrf::VRFProof;
@@ -55,9 +59,11 @@ use crate::run_loop::nakamoto::Globals;
5559
use crate::run_loop::RegisteredKey;
5660

5761
#[cfg(test)]
58-
pub static TEST_MINE_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
62+
/// Test flag to stall the miner thread
63+
pub static TEST_MINE_STALL: LazyLock<TestFlag<bool>> = LazyLock::new(TestFlag::default);
5964
#[cfg(test)]
60-
pub static TEST_BROADCAST_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
65+
/// Test flag to stall block proposal broadcasting
66+
pub static TEST_BROADCAST_STALL: LazyLock<TestFlag<bool>> = LazyLock::new(TestFlag::default);
6167
#[cfg(test)]
6268
pub static TEST_BLOCK_ANNOUNCE_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
6369
#[cfg(test)]
@@ -195,15 +201,15 @@ impl BlockMinerThread {
195201

196202
#[cfg(test)]
197203
fn fault_injection_block_broadcast_stall(new_block: &NakamotoBlock) {
198-
if *TEST_BROADCAST_STALL.lock().unwrap() == Some(true) {
204+
if TEST_BROADCAST_STALL.get() {
199205
// Do an extra check just so we don't log EVERY time.
200206
warn!("Fault injection: Broadcasting is stalled due to testing directive.";
201207
"stacks_block_id" => %new_block.block_id(),
202208
"stacks_block_hash" => %new_block.header.block_hash(),
203209
"height" => new_block.header.chain_length,
204210
"consensus_hash" => %new_block.header.consensus_hash
205211
);
206-
while *TEST_BROADCAST_STALL.lock().unwrap() == Some(true) {
212+
while TEST_BROADCAST_STALL.get() {
207213
std::thread::sleep(std::time::Duration::from_millis(10));
208214
}
209215
info!("Fault injection: Broadcasting is no longer stalled due to testing directive.";
@@ -356,10 +362,10 @@ impl BlockMinerThread {
356362
reward_set: &RewardSet,
357363
) -> Result<(), NakamotoNodeError> {
358364
#[cfg(test)]
359-
if *TEST_MINE_STALL.lock().unwrap() == Some(true) {
365+
if TEST_MINE_STALL.get() {
360366
// Do an extra check just so we don't log EVERY time.
361367
warn!("Mining is stalled due to testing directive");
362-
while *TEST_MINE_STALL.lock().unwrap() == Some(true) {
368+
while TEST_MINE_STALL.get() {
363369
std::thread::sleep(std::time::Duration::from_millis(10));
364370
}
365371
warn!("Mining is no longer stalled due to testing directive. Continuing...");

testnet/stacks-node/src/tests/nakamoto_integrations.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4990,7 +4990,7 @@ fn forked_tenure_is_ignored() {
49904990

49914991
// For the next tenure, submit the commit op but do not allow any stacks blocks to be broadcasted.
49924992
// Stall the miner thread; only wait until the number of submitted commits increases.
4993-
TEST_BROADCAST_STALL.lock().unwrap().replace(true);
4993+
TEST_BROADCAST_STALL.set(true);
49944994
TEST_BLOCK_ANNOUNCE_STALL.lock().unwrap().replace(true);
49954995
let blocks_before = mined_blocks.load(Ordering::SeqCst);
49964996
let commits_before = commits_submitted.load(Ordering::SeqCst);
@@ -5008,7 +5008,7 @@ fn forked_tenure_is_ignored() {
50085008
// Unpause the broadcast of Tenure B's block, do not submit commits, and do not allow blocks to
50095009
// be processed
50105010
test_skip_commit_op.set(true);
5011-
TEST_BROADCAST_STALL.lock().unwrap().replace(false);
5011+
TEST_BROADCAST_STALL.set(false);
50125012

50135013
// Wait for a stacks block to be broadcasted.
50145014
// However, it will not be processed.
@@ -6099,7 +6099,7 @@ fn clarity_burn_state() {
60996099
result.expect_result_ok().expect("Read-only call failed");
61006100

61016101
// Pause mining to prevent the stacks block from being mined before the tenure change is processed
6102-
TEST_MINE_STALL.lock().unwrap().replace(true);
6102+
TEST_MINE_STALL.set(true);
61036103
// Submit a tx for the next block (the next block will be a new tenure, so the burn block height will increment)
61046104
let call_tx = tests::make_contract_call(
61056105
&sender_sk,
@@ -6124,7 +6124,7 @@ fn clarity_burn_state() {
61246124
Ok(commits_submitted.load(Ordering::SeqCst) > commits_before)
61256125
})
61266126
.unwrap();
6127-
TEST_MINE_STALL.lock().unwrap().replace(false);
6127+
TEST_MINE_STALL.set(false);
61286128
wait_for(20, || {
61296129
Ok(coord_channel
61306130
.lock()
@@ -10407,7 +10407,7 @@ fn clarity_cost_spend_down() {
1040710407
.expect("Mutex poisoned")
1040810408
.get_stacks_blocks_processed();
1040910409
// Pause mining so we can add all our transactions to the mempool at once.
10410-
TEST_MINE_STALL.lock().unwrap().replace(true);
10410+
TEST_MINE_STALL.set(true);
1041110411
let mut submitted_txs = vec![];
1041210412
for _nmb_tx in 0..nmb_txs_per_signer {
1041310413
for sender_sk in sender_sks.iter() {
@@ -10436,7 +10436,7 @@ fn clarity_cost_spend_down() {
1043610436
}
1043710437
}
1043810438
}
10439-
TEST_MINE_STALL.lock().unwrap().replace(false);
10439+
TEST_MINE_STALL.set(false);
1044010440
wait_for(120, || {
1044110441
let blocks_processed = coord_channel
1044210442
.lock()

0 commit comments

Comments
 (0)