Skip to content

Commit 68fe720

Browse files
authored
Merge pull request #5390 from stacks-network/fix/locally-accepted-blocks
locally_accepted_blocks_overriden_by_global_rejection fix: Store the rejected block in the database in testing directive case
2 parents a63b1e5 + c3dbc5d commit 68fe720

File tree

2 files changed

+42
-24
lines changed
  • stacks-signer/src/v0
  • testnet/stacks-node/src/tests/signer

2 files changed

+42
-24
lines changed

stacks-signer/src/v0/signer.rs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -434,30 +434,8 @@ impl Signer {
434434
};
435435

436436
#[cfg(any(test, feature = "testing"))]
437-
let block_response = match &*TEST_REJECT_ALL_BLOCK_PROPOSAL.lock().unwrap() {
438-
Some(public_keys) => {
439-
if public_keys.contains(
440-
&stacks_common::types::chainstate::StacksPublicKey::from_private(
441-
&self.private_key,
442-
),
443-
) {
444-
warn!("{self}: Rejecting block proposal automatically due to testing directive";
445-
"block_id" => %block_proposal.block.block_id(),
446-
"height" => block_proposal.block.header.chain_length,
447-
"consensus_hash" => %block_proposal.block.header.consensus_hash
448-
);
449-
Some(BlockResponse::rejected(
450-
block_proposal.block.header.signer_signature_hash(),
451-
RejectCode::TestingDirective,
452-
&self.private_key,
453-
self.mainnet,
454-
))
455-
} else {
456-
None
457-
}
458-
}
459-
None => block_response,
460-
};
437+
let block_response =
438+
self.test_reject_block_proposal(block_proposal, &mut block_info, block_response);
461439

462440
if let Some(block_response) = block_response {
463441
// We know proposal is invalid. Send rejection message, do not do further validation
@@ -948,6 +926,44 @@ impl Signer {
948926
false
949927
}
950928

929+
#[cfg(any(test, feature = "testing"))]
930+
fn test_reject_block_proposal(
931+
&mut self,
932+
block_proposal: &BlockProposal,
933+
block_info: &mut BlockInfo,
934+
block_response: Option<BlockResponse>,
935+
) -> Option<BlockResponse> {
936+
let Some(public_keys) = &*TEST_REJECT_ALL_BLOCK_PROPOSAL.lock().unwrap() else {
937+
return block_response;
938+
};
939+
if public_keys.contains(
940+
&stacks_common::types::chainstate::StacksPublicKey::from_private(&self.private_key),
941+
) {
942+
warn!("{self}: Rejecting block proposal automatically due to testing directive";
943+
"block_id" => %block_proposal.block.block_id(),
944+
"height" => block_proposal.block.header.chain_length,
945+
"consensus_hash" => %block_proposal.block.header.consensus_hash
946+
);
947+
if let Err(e) = block_info.mark_locally_rejected() {
948+
warn!("{self}: Failed to mark block as locally rejected: {e:?}",);
949+
};
950+
// We must insert the block into the DB to prevent subsequent repeat proposals being accepted (should reject
951+
// as invalid since we rejected in a prior round if this crops up again)
952+
// in case this is the first time we saw this block. Safe to do since this is testing case only.
953+
self.signer_db
954+
.insert_block(block_info)
955+
.unwrap_or_else(|_| panic!("{self}: Failed to insert block in DB"));
956+
Some(BlockResponse::rejected(
957+
block_proposal.block.header.signer_signature_hash(),
958+
RejectCode::TestingDirective,
959+
&self.private_key,
960+
self.mainnet,
961+
))
962+
} else {
963+
None
964+
}
965+
}
966+
951967
/// Send a mock signature to stackerdb to prove we are still alive
952968
fn mock_sign(&mut self, mock_proposal: MockProposal) {
953969
info!("{self}: Mock signing mock proposal: {mock_proposal:?}");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,6 +4246,8 @@ fn locally_accepted_blocks_overriden_by_global_rejection() {
42464246
.unwrap()
42474247
.replace(rejecting_signers.clone());
42484248
test_observer::clear();
4249+
// Make a new stacks transaction to create a different block signature, but make sure to propose it
4250+
// AFTER the signers are unfrozen so they don't inadvertently prevent the new block being accepted
42494251
let transfer_tx = make_stacks_transfer(
42504252
&sender_sk,
42514253
sender_nonce,

0 commit comments

Comments
 (0)