Skip to content

Commit c3dbc5d

Browse files
committed
Store the rejected block in the database in testing directive case
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 09d920c commit c3dbc5d

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
@@ -428,30 +428,8 @@ impl Signer {
428428
};
429429

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

456434
if let Some(block_response) = block_response {
457435
// We know proposal is invalid. Send rejection message, do not do further validation
@@ -935,6 +913,44 @@ impl Signer {
935913
false
936914
}
937915

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