|
| 1 | +// Copyright (C) 2026 Stacks Open Internet Foundation |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +use std::env; |
| 16 | + |
| 17 | +use libsigner::v0::messages::{BlockResponse, SignerMessage}; |
| 18 | +use stacks::chainstate::burn::db::sortdb::SortitionDB; |
| 19 | +use stacks::chainstate::stacks::TenureChangeCause; |
| 20 | +use stacks::codec::StacksMessageCodec; |
| 21 | +use stacks::net::api::postblock_proposal::TEST_VALIDATE_STALL; |
| 22 | +use tracing_subscriber::layer::SubscriberExt; |
| 23 | +use tracing_subscriber::util::SubscriberInitExt; |
| 24 | +use tracing_subscriber::{fmt, EnvFilter}; |
| 25 | + |
| 26 | +use crate::tests::nakamoto_integrations::wait_for; |
| 27 | +use crate::tests::neon_integrations::test_observer; |
| 28 | +use crate::tests::signer::v0::{ |
| 29 | + wait_for_block_pre_commits_from_signers, wait_for_block_pushed_by_miner_key, MultipleMinerTest, |
| 30 | +}; |
| 31 | + |
| 32 | +#[test] |
| 33 | +#[ignore] |
| 34 | +/// Test that signers don't issue signatures until they have validated the block |
| 35 | +/// |
| 36 | +/// This test verifies a race condition where a signer receives enough pre-commits |
| 37 | +/// to exceed the 70% threshold before receiving its own block validation response. |
| 38 | +/// The signer should NOT issue a signature until it has confirmed the block is valid. |
| 39 | +/// |
| 40 | +/// Test Setup: |
| 41 | +/// - Distribute signers across two miners (4 on miner 1, 1 on miner 2) |
| 42 | +/// - Signers on different miners use different validation endpoints |
| 43 | +/// |
| 44 | +/// Test Execution: |
| 45 | +/// 1. Propose a block to all signers |
| 46 | +/// 2. Pause block validation on miner 2 (the single signer) |
| 47 | +/// 3. 4 signers on miner 1 issue pre-commits, pushing threshold over 70% |
| 48 | +/// 4. The single signer on miner 2 receives all pre-commits but its validation is stalled |
| 49 | +/// 5. Verify the single signer does NOT issue a signature until validation completes |
| 50 | +/// 6. Resume validation and confirm the block is accepted |
| 51 | +/// |
| 52 | +/// Test Assertion: |
| 53 | +/// The signer waits for its own validation before issuing a signature, preventing |
| 54 | +/// race conditions where it could sign before discovering the block is invalid. |
| 55 | +fn signer_waits_for_validation_before_signing() { |
| 56 | + if env::var("BITCOIND_TEST") != Ok("1".into()) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + tracing_subscriber::registry() |
| 61 | + .with(fmt::layer()) |
| 62 | + .with(EnvFilter::from_default_env()) |
| 63 | + .init(); |
| 64 | + |
| 65 | + info!("------------------------- Test Setup -------------------------"); |
| 66 | + |
| 67 | + // Create a multiple miner test with 5 signers |
| 68 | + // They will be distributed: 4 to miner 1, 1 to miner 2 |
| 69 | + let num_signers = 5; |
| 70 | + let node_2_auth = "node_2".to_string(); |
| 71 | + let node_1_auth = "node_1".to_string(); |
| 72 | + let mut miners = MultipleMinerTest::new_with_signer_dist( |
| 73 | + num_signers, |
| 74 | + 0, |
| 75 | + |config| { |
| 76 | + if config.endpoint.port() % 5 == 0 { |
| 77 | + config.auth_password = node_2_auth.clone(); |
| 78 | + } else { |
| 79 | + config.auth_password = node_1_auth.clone(); |
| 80 | + } |
| 81 | + }, |
| 82 | + |config| { |
| 83 | + config.burnchain.pox_reward_length = Some(30); |
| 84 | + config.connection_options.auth_token = Some(node_1_auth.clone()); |
| 85 | + }, |
| 86 | + |config| { |
| 87 | + config.connection_options.auth_token = Some(node_2_auth.clone()); |
| 88 | + }, |
| 89 | + // Distribute signers so first 4 go to node 1, last 1 goes to node 2 |
| 90 | + |port| if port % 5 == 0 { 1 } else { 0 }, |
| 91 | + None, |
| 92 | + ); |
| 93 | + let all_signers = miners.signer_test.signer_test_pks(); |
| 94 | + let signer_configs = &miners.signer_test.signer_configs; |
| 95 | + let (conf_1, conf_2) = miners.get_node_configs(); |
| 96 | + let mut approving_signers = Vec::new(); |
| 97 | + let mut stalled_signer = Vec::new(); |
| 98 | + for (config, signer_pk) in signer_configs.iter().zip(all_signers.iter()) { |
| 99 | + if config.node_host == conf_2.node.rpc_bind { |
| 100 | + stalled_signer.push(signer_pk.clone()); |
| 101 | + } else { |
| 102 | + approving_signers.push(signer_pk.clone()); |
| 103 | + } |
| 104 | + } |
| 105 | + assert_eq!( |
| 106 | + stalled_signer.len(), |
| 107 | + 1, |
| 108 | + "Expected exactly one signer to be assigned to miner 2" |
| 109 | + ); |
| 110 | + let (miner_pk_1, _miner_pk_2) = miners.get_miner_public_keys(); |
| 111 | + |
| 112 | + miners.pause_commits_miner_2(); |
| 113 | + miners.boot_to_epoch_3(); |
| 114 | + |
| 115 | + // Make sure we know which miner will win in the stalled block |
| 116 | + miners.pause_commits_miner_1(); |
| 117 | + info!("------------------------- Mine First Block N -------------------------"); |
| 118 | + |
| 119 | + let sortdb = SortitionDB::open( |
| 120 | + &conf_1.get_burn_db_file_path(), |
| 121 | + false, |
| 122 | + conf_1.get_burnchain().pox_constants, |
| 123 | + ) |
| 124 | + .unwrap(); |
| 125 | + // Mine an initial block to establish state |
| 126 | + miners |
| 127 | + .mine_bitcoin_block_and_tenure_change_tx(&sortdb, TenureChangeCause::BlockFound, 30) |
| 128 | + .expect("Failed to mine BTC block followed by tenure change tx"); |
| 129 | + miners.submit_commit_miner_1(&sortdb); |
| 130 | + miners.signer_test.check_signer_states_normal(); |
| 131 | + |
| 132 | + let info_before = miners.get_peer_info(); |
| 133 | + info!("------------------------- Stall Validation on Miner 2 -------------------------"); |
| 134 | + // Stall block validation submission on the signer connected to miner 2 |
| 135 | + // This prevents that signer from validating the next proposed block |
| 136 | + TEST_VALIDATE_STALL.set(vec![Some(node_2_auth)]); |
| 137 | + |
| 138 | + info!("------------------------- Mine Block N+1 with Stalled Validation -------------------------"); |
| 139 | + // Mine a new tenure which will issue a block proposal to all signers for its tenure change. |
| 140 | + miners.signer_test.mine_bitcoin_block(); |
| 141 | + |
| 142 | + // The 4 signers on miner 1 should have validated and sent pre-commits |
| 143 | + // The 1 signer on miner 2 should be waiting for validation and should NOT have issued a signature |
| 144 | + let block = |
| 145 | + wait_for_block_pushed_by_miner_key(30, info_before.stacks_tip_height + 1, &miner_pk_1) |
| 146 | + .expect("Failed to mine block N+1"); |
| 147 | + let signer_signature_hash = block.header.signer_signature_hash(); |
| 148 | + info!("------------------------- Mined {signer_signature_hash}. Checking for Pre-Commits for {} Signers-------------------------", approving_signers.len()); |
| 149 | + wait_for_block_pre_commits_from_signers(30, &signer_signature_hash, &approving_signers) |
| 150 | + .expect("Failed to receive pre-commits from approving signers"); |
| 151 | + // We only wait a small amount of time for each of these checks since we already received block commits from everyone else. |
| 152 | + let stalled_pk = stalled_signer[0].clone(); |
| 153 | + assert!( |
| 154 | + wait_for(15, || { |
| 155 | + for chunk in test_observer::get_stackerdb_chunks() |
| 156 | + .into_iter() |
| 157 | + .flat_map(|chunk| chunk.modified_slots) |
| 158 | + { |
| 159 | + let message = SignerMessage::consensus_deserialize(&mut chunk.data.as_slice()) |
| 160 | + .expect("Failed to deserialize SignerMessage"); |
| 161 | + |
| 162 | + let pk = chunk.recover_pk().expect("Failed to recover pk"); |
| 163 | + if stalled_pk != pk { |
| 164 | + continue; |
| 165 | + } |
| 166 | + |
| 167 | + match message { |
| 168 | + SignerMessage::BlockPreCommit(pre_commit) => { |
| 169 | + assert_ne!( |
| 170 | + signer_signature_hash, pre_commit, |
| 171 | + "Stalled signer should not have issued a pre-commit yet." |
| 172 | + ); |
| 173 | + } |
| 174 | + SignerMessage::BlockResponse(BlockResponse::Accepted(acceptance)) => { |
| 175 | + assert_ne!( |
| 176 | + signer_signature_hash, acceptance.signer_signature_hash, |
| 177 | + "Stalled signer should not have accepted the block yet" |
| 178 | + ); |
| 179 | + } |
| 180 | + SignerMessage::BlockResponse(BlockResponse::Rejected(rejection)) => { |
| 181 | + assert_ne!( |
| 182 | + signer_signature_hash, rejection.signer_signature_hash, |
| 183 | + "Stalled signer should not have rejected the block yet" |
| 184 | + ); |
| 185 | + } |
| 186 | + _ => {} |
| 187 | + } |
| 188 | + } |
| 189 | + Ok(false) |
| 190 | + }) |
| 191 | + .is_err(), |
| 192 | + "Stalled signer issued a pre-commit or response before validation completed" |
| 193 | + ); |
| 194 | + // At this point, we should NOT have received a signature from the stalled signer |
| 195 | + // because it hasn't yet validated the block. We're checking this happened correctly |
| 196 | + // by the fact that we can now resume validation without issues. |
| 197 | + info!("------------------------- Resume Validation -------------------------"); |
| 198 | + TEST_VALIDATE_STALL.set(vec![]); // Unset the stall condition |
| 199 | + info!("------------------------- Wait for Block Response -------------------------"); |
| 200 | + // Now that validation is resumed, the stalled signer should issue a response |
| 201 | + let mut found_commit = false; |
| 202 | + let mut found_accept = false; |
| 203 | + wait_for(15, || { |
| 204 | + for chunk in test_observer::get_stackerdb_chunks() |
| 205 | + .into_iter() |
| 206 | + .flat_map(|chunk| chunk.modified_slots) |
| 207 | + { |
| 208 | + let message = SignerMessage::consensus_deserialize(&mut chunk.data.as_slice()) |
| 209 | + .expect("Failed to deserialize SignerMessage"); |
| 210 | + |
| 211 | + let pk = chunk.recover_pk().expect("Failed to recover pk"); |
| 212 | + if stalled_pk != pk { |
| 213 | + continue; |
| 214 | + } |
| 215 | + match message { |
| 216 | + SignerMessage::BlockPreCommit(pre_commit) => { |
| 217 | + if signer_signature_hash == pre_commit { |
| 218 | + found_commit = true; |
| 219 | + } |
| 220 | + } |
| 221 | + SignerMessage::BlockResponse(BlockResponse::Accepted(acceptance)) => { |
| 222 | + if signer_signature_hash == acceptance.signer_signature_hash { |
| 223 | + found_accept = true; |
| 224 | + } |
| 225 | + } |
| 226 | + SignerMessage::BlockResponse(BlockResponse::Rejected(rejection)) => { |
| 227 | + assert_ne!( |
| 228 | + signer_signature_hash, rejection.signer_signature_hash, |
| 229 | + "Stalled signer should not have rejected the block" |
| 230 | + ); |
| 231 | + } |
| 232 | + _ => {} |
| 233 | + } |
| 234 | + } |
| 235 | + Ok(found_accept && found_commit) |
| 236 | + }) |
| 237 | + .expect("Stalled signer did not issue a pre-commit and acceptance after validation resumed"); |
| 238 | + miners.shutdown(); |
| 239 | +} |
0 commit comments