Skip to content

Commit

Permalink
Add rejected_blocks_count_towards_miner_validity test
Browse files Browse the repository at this point in the history
Signed-off-by: Jacinta Ferrant <[email protected]>
  • Loading branch information
jferrant committed Jan 13, 2025
1 parent bb48447 commit 2e1b832
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 41 deletions.
10 changes: 7 additions & 3 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,7 +71,7 @@ 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>> =
Expand Down Expand Up @@ -353,10 +357,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!(
Expand Down
18 changes: 12 additions & 6 deletions testnet/stacks-node/src/nakamoto_node/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#[cfg(test)]
use std::sync::LazyLock;
use std::thread;
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -42,6 +44,8 @@ use stacks::net::stackerdb::StackerDBs;
use stacks::net::{NakamotoBlocksData, StacksMessageType};
use stacks::util::get_epoch_time_secs;
use stacks::util::secp256k1::MessageSignature;
#[cfg(test)]
use stacks::util::tests::TestFlag;
use stacks_common::types::chainstate::{StacksAddress, StacksBlockId};
use stacks_common::types::{PrivateKey, StacksEpochId};
use stacks_common::util::vrf::VRFProof;
Expand All @@ -55,9 +59,11 @@ use crate::run_loop::nakamoto::Globals;
use crate::run_loop::RegisteredKey;

#[cfg(test)]
pub static TEST_MINE_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
/// Test flag to stall the miner thread
pub static TEST_MINE_STALL: LazyLock<TestFlag<bool>> = LazyLock::new(TestFlag::default);
#[cfg(test)]
pub static TEST_BROADCAST_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
/// Test flag to stall block proposal broadcasting
pub static TEST_BROADCAST_STALL: LazyLock<TestFlag<bool>> = LazyLock::new(TestFlag::default);
#[cfg(test)]
pub static TEST_BLOCK_ANNOUNCE_STALL: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
#[cfg(test)]
Expand Down Expand Up @@ -195,15 +201,15 @@ impl BlockMinerThread {

#[cfg(test)]
fn fault_injection_block_broadcast_stall(new_block: &NakamotoBlock) {
if *TEST_BROADCAST_STALL.lock().unwrap() == Some(true) {
if TEST_BROADCAST_STALL.get() {
// Do an extra check just so we don't log EVERY time.
warn!("Fault injection: Broadcasting is stalled due to testing directive.";
"stacks_block_id" => %new_block.block_id(),
"stacks_block_hash" => %new_block.header.block_hash(),
"height" => new_block.header.chain_length,
"consensus_hash" => %new_block.header.consensus_hash
);
while *TEST_BROADCAST_STALL.lock().unwrap() == Some(true) {
while TEST_BROADCAST_STALL.get() {
std::thread::sleep(std::time::Duration::from_millis(10));
}
info!("Fault injection: Broadcasting is no longer stalled due to testing directive.";
Expand Down Expand Up @@ -356,10 +362,10 @@ impl BlockMinerThread {
reward_set: &RewardSet,
) -> Result<(), NakamotoNodeError> {
#[cfg(test)]
if *TEST_MINE_STALL.lock().unwrap() == Some(true) {
if TEST_MINE_STALL.get() {
// Do an extra check just so we don't log EVERY time.
warn!("Mining is stalled due to testing directive");
while *TEST_MINE_STALL.lock().unwrap() == Some(true) {
while TEST_MINE_STALL.get() {
std::thread::sleep(std::time::Duration::from_millis(10));
}
warn!("Mining is no longer stalled due to testing directive. Continuing...");
Expand Down
12 changes: 6 additions & 6 deletions testnet/stacks-node/src/tests/nakamoto_integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4990,7 +4990,7 @@ fn forked_tenure_is_ignored() {

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

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

// Pause mining to prevent the stacks block from being mined before the tenure change is processed
TEST_MINE_STALL.lock().unwrap().replace(true);
TEST_MINE_STALL.set(true);
// Submit a tx for the next block (the next block will be a new tenure, so the burn block height will increment)
let call_tx = tests::make_contract_call(
&sender_sk,
Expand All @@ -6124,7 +6124,7 @@ fn clarity_burn_state() {
Ok(commits_submitted.load(Ordering::SeqCst) > commits_before)
})
.unwrap();
TEST_MINE_STALL.lock().unwrap().replace(false);
TEST_MINE_STALL.set(false);
wait_for(20, || {
Ok(coord_channel
.lock()
Expand Down Expand Up @@ -10407,7 +10407,7 @@ fn clarity_cost_spend_down() {
.expect("Mutex poisoned")
.get_stacks_blocks_processed();
// Pause mining so we can add all our transactions to the mempool at once.
TEST_MINE_STALL.lock().unwrap().replace(true);
TEST_MINE_STALL.set(true);
let mut submitted_txs = vec![];
for _nmb_tx in 0..nmb_txs_per_signer {
for sender_sk in sender_sks.iter() {
Expand Down Expand Up @@ -10436,7 +10436,7 @@ fn clarity_cost_spend_down() {
}
}
}
TEST_MINE_STALL.lock().unwrap().replace(false);
TEST_MINE_STALL.set(false);
wait_for(120, || {
let blocks_processed = coord_channel
.lock()
Expand Down
Loading

0 comments on commit 2e1b832

Please sign in to comment.