Skip to content

Commit 8f821f2

Browse files
committed
Cleanup: do not prematurely wrap a BlockRejection into a BlockResponse throughout
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 42b10da commit 8f821f2

File tree

3 files changed

+60
-50
lines changed

3 files changed

+60
-50
lines changed

libsigner/src/v0/messages.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,18 @@ pub enum BlockResponse {
11601160
Rejected(BlockRejection),
11611161
}
11621162

1163+
impl From<BlockRejection> for BlockResponse {
1164+
fn from(rejection: BlockRejection) -> Self {
1165+
BlockResponse::Rejected(rejection)
1166+
}
1167+
}
1168+
1169+
impl From<BlockAccepted> for BlockResponse {
1170+
fn from(accepted: BlockAccepted) -> Self {
1171+
BlockResponse::Accepted(accepted)
1172+
}
1173+
}
1174+
11631175
#[cfg_attr(test, mutants::skip)]
11641176
impl std::fmt::Display for BlockResponse {
11651177
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

stacks-signer/src/v0/signer.rs

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ use clarity::util::sleep_ms;
3535
#[cfg(any(test, feature = "testing"))]
3636
use clarity::util::tests::TestFlag;
3737
use libsigner::v0::messages::{
38-
BlockAccepted, BlockRejection, BlockResponse, MessageSlotID, MockProposal, MockSignature,
39-
RejectReason, RejectReasonPrefix, SignerMessage, StateMachineUpdate,
38+
BlockAccepted, BlockRejection, BlockResponse, BlockResponseData, MessageSlotID, MockProposal,
39+
MockSignature, RejectReason, RejectReasonPrefix, SignerMessage, SignerMessageMetadata,
40+
StateMachineUpdate,
4041
};
4142
use libsigner::v0::signer_state::GlobalStateEvaluator;
4243
use libsigner::{BlockProposal, SignerEvent, SignerSession};
@@ -452,31 +453,36 @@ impl Signer {
452453
let valid = block_info.valid?;
453454
let response = if valid {
454455
debug!("{self}: Accepting block {}", block_info.block.block_id());
455-
self.create_block_acceptance(&block_info.block)
456+
self.create_block_acceptance(&block_info.block).into()
456457
} else {
457458
debug!("{self}: Rejecting block {}", block_info.block.block_id());
458459
self.create_block_rejection(RejectReason::RejectedInPriorRound, &block_info.block)
460+
.into()
459461
};
460462
Some(response)
461463
}
462464

463-
/// Create a block acceptance response for a block
464-
pub fn create_block_acceptance(&self, block: &NakamotoBlock) -> BlockResponse {
465+
/// Create a block acceptance for a block
466+
pub fn create_block_acceptance(&self, block: &NakamotoBlock) -> BlockAccepted {
465467
let signature = self
466468
.private_key
467469
.sign(block.header.signer_signature_hash().bits())
468470
.expect("Failed to sign block");
469-
BlockResponse::accepted(
470-
block.header.signer_signature_hash(),
471+
BlockAccepted {
472+
signer_signature_hash: block.header.signer_signature_hash(),
471473
signature,
472-
self.signer_db.calculate_tenure_extend_timestamp(
473-
self.proposal_config
474-
.tenure_idle_timeout
475-
.saturating_add(self.proposal_config.tenure_idle_timeout_buffer),
476-
block,
477-
true,
474+
metadata: SignerMessageMetadata::default(),
475+
response_data: BlockResponseData::new(
476+
self.signer_db.calculate_tenure_extend_timestamp(
477+
self.proposal_config
478+
.tenure_idle_timeout
479+
.saturating_add(self.proposal_config.tenure_idle_timeout_buffer),
480+
block,
481+
true,
482+
),
483+
RejectReason::NotRejected,
478484
),
479-
)
485+
}
480486
}
481487

482488
/// The actual switch-on-event processing of an event.
@@ -676,8 +682,8 @@ impl Signer {
676682
&self,
677683
reject_reason: RejectReason,
678684
block: &NakamotoBlock,
679-
) -> BlockResponse {
680-
BlockResponse::rejected(
685+
) -> BlockRejection {
686+
BlockRejection::new(
681687
block.header.signer_signature_hash(),
682688
reject_reason,
683689
&self.private_key,
@@ -716,13 +722,13 @@ impl Signer {
716722
}
717723

718724
/// Check if block should be rejected based on the appropriate state (either local or global)
719-
/// Will return a BlockResponse::Rejection if the block is invalid, none otherwise.
725+
/// Will return a BlockRejection if the block is invalid, none otherwise.
720726
fn check_block_against_state(
721727
&mut self,
722728
stacks_client: &StacksClient,
723729
sortition_state: &mut Option<SortitionsView>,
724730
block: &NakamotoBlock,
725-
) -> Option<BlockResponse> {
731+
) -> Option<BlockRejection> {
726732
let Some(latest_version) = self
727733
.global_state_evaluator
728734
.determine_latest_supported_signer_protocol_version()
@@ -743,14 +749,14 @@ impl Signer {
743749
}
744750

745751
/// Check if block should be rejected based on the local view of the sortition state
746-
/// Will return a BlockResponse::Rejection if the block is invalid, none otherwise.
752+
/// Will return a BlockRejection if the block is invalid, none otherwise.
747753
/// This is the pre-global signer state activation path.
748754
fn check_block_against_local_state(
749755
&mut self,
750756
stacks_client: &StacksClient,
751757
sortition_state: &mut Option<SortitionsView>,
752758
block: &NakamotoBlock,
753-
) -> Option<BlockResponse> {
759+
) -> Option<BlockRejection> {
754760
let signer_signature_hash = block.header.signer_signature_hash();
755761
let block_id = block.block_id();
756762
// Get sortition view if we don't have it
@@ -804,13 +810,13 @@ impl Signer {
804810
}
805811

806812
/// Check if block should be rejected based on global signer state
807-
/// Will return a BlockResponse::Rejection if the block is invalid, none otherwise.
813+
/// Will return a BlockRejection if the block is invalid, none otherwise.
808814
/// This is the Post-global signer state activation path
809815
fn check_block_against_global_state(
810816
&mut self,
811817
stacks_client: &StacksClient,
812818
block: &NakamotoBlock,
813-
) -> Option<BlockResponse> {
819+
) -> Option<BlockRejection> {
814820
let signer_signature_hash = block.header.signer_signature_hash();
815821
let block_id = block.block_id();
816822
// First update our global state evaluator with our local state if we have one
@@ -1053,12 +1059,10 @@ impl Signer {
10531059
self.signer_db
10541060
.insert_block(&block_info)
10551061
.unwrap_or_else(|e| self.handle_insert_block_error(e));
1056-
let block_response = self.create_block_acceptance(&block_info.block);
1057-
if let Some(accepted) = block_response.as_block_accepted() {
1058-
// have to save the signature _after_ the block info
1059-
self.handle_block_signature(stacks_client, accepted);
1060-
}
1061-
self.impl_send_block_response(&block_info.block, block_response);
1062+
let accepted = self.create_block_acceptance(&block_info.block);
1063+
// have to save the signature _after_ the block info
1064+
self.handle_block_signature(stacks_client, &accepted);
1065+
self.impl_send_block_response(&block_info.block, accepted.into());
10621066
}
10631067

10641068
/// Handle block proposal messages submitted to signers stackerdb
@@ -1149,16 +1153,16 @@ impl Signer {
11491153
}
11501154

11511155
// Check if proposal can be rejected now if not valid against sortition view
1152-
let block_response =
1156+
let block_rejection =
11531157
self.check_block_against_state(stacks_client, sortition_state, &block_proposal.block);
11541158

11551159
#[cfg(any(test, feature = "testing"))]
1156-
let block_response =
1157-
self.test_reject_block_proposal(block_proposal, &mut block_info, block_response);
1160+
let block_rejection =
1161+
self.test_reject_block_proposal(block_proposal, &mut block_info, block_rejection);
11581162

1159-
if let Some(block_response) = block_response {
1163+
if let Some(block_rejection) = block_rejection {
11601164
// We know proposal is invalid. Send rejection message, do not do further validation and do not store it.
1161-
self.send_block_response(&block_info.block, block_response);
1165+
self.send_block_response(&block_info.block, block_rejection.into());
11621166
} else {
11631167
// Just in case check if the last block validation submission timed out.
11641168
self.check_submitted_block_proposal();
@@ -1240,7 +1244,7 @@ impl Signer {
12401244
&mut self,
12411245
stacks_client: &StacksClient,
12421246
proposed_block: &NakamotoBlock,
1243-
) -> Option<BlockResponse> {
1247+
) -> Option<BlockRejection> {
12441248
let signer_signature_hash = proposed_block.header.signer_signature_hash();
12451249
// If this is a tenure change block, ensure that it confirms the correct number of blocks from the parent tenure.
12461250
if let Some(tenure_change) = proposed_block.get_tenure_change_tx_payload() {
@@ -1357,12 +1361,9 @@ impl Signer {
13571361
return;
13581362
}
13591363

1360-
if let Some(block_response) =
1364+
if let Some(block_rejection) =
13611365
self.check_block_against_signer_db_state(stacks_client, &block_info.block)
13621366
{
1363-
let Some(block_rejection) = block_response.as_block_rejection() else {
1364-
return;
1365-
};
13661367
// The signer db state has changed. We no longer view this block as valid. Override the validation response.
13671368
if let Err(e) = block_info.mark_locally_rejected() {
13681369
if !block_info.has_reached_consensus() {
@@ -1372,11 +1373,8 @@ impl Signer {
13721373
self.signer_db
13731374
.insert_block(&block_info)
13741375
.unwrap_or_else(|e| self.handle_insert_block_error(e));
1375-
self.handle_block_rejection(block_rejection, sortition_state);
1376-
self.impl_send_block_response(
1377-
&block_info.block,
1378-
BlockResponse::Rejected(block_rejection.clone()),
1379-
);
1376+
self.handle_block_rejection(&block_rejection, sortition_state);
1377+
self.impl_send_block_response(&block_info.block, block_rejection.into());
13801378
} else {
13811379
if let Err(e) = block_info.mark_locally_accepted(false) {
13821380
if !block_info.has_reached_consensus() {
@@ -1450,7 +1448,7 @@ impl Signer {
14501448
.insert_block(&block_info)
14511449
.unwrap_or_else(|e| self.handle_insert_block_error(e));
14521450
self.handle_block_rejection(&block_rejection, sortition_state);
1453-
self.impl_send_block_response(&block_info.block, BlockResponse::Rejected(block_rejection));
1451+
self.impl_send_block_response(&block_info.block, block_rejection.into());
14541452
}
14551453

14561454
/// Handle the block validate response returned from our prior calls to submit a block for validation
@@ -1562,13 +1560,13 @@ impl Signer {
15621560
),
15631561
&block_info.block,
15641562
);
1565-
block_info.reject_reason = Some(rejection.get_response_data().reject_reason.clone());
1563+
block_info.reject_reason = Some(rejection.response_data.reject_reason.clone());
15661564
if let Err(e) = block_info.mark_locally_rejected() {
15671565
if !block_info.has_reached_consensus() {
15681566
warn!("{self}: Failed to mark block as locally rejected: {e:?}");
15691567
}
15701568
};
1571-
self.impl_send_block_response(&block_info.block, rejection);
1569+
self.impl_send_block_response(&block_info.block, rejection.into());
15721570

15731571
self.signer_db
15741572
.insert_block(&block_info)

stacks-signer/src/v0/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::collections::HashMap;
1717
use std::sync::LazyLock;
1818

1919
use blockstack_lib::chainstate::nakamoto::NakamotoBlock;
20-
use libsigner::v0::messages::{BlockResponse, RejectReason};
20+
use libsigner::v0::messages::{BlockRejection, RejectReason};
2121
use libsigner::BlockProposal;
2222
use stacks_common::types::chainstate::StacksPublicKey;
2323
use stacks_common::util::get_epoch_time_secs;
@@ -81,8 +81,8 @@ impl Signer {
8181
&mut self,
8282
block_proposal: &BlockProposal,
8383
block_info: &mut BlockInfo,
84-
block_response: Option<BlockResponse>,
85-
) -> Option<BlockResponse> {
84+
block_rejection: Option<BlockRejection>,
85+
) -> Option<BlockRejection> {
8686
let public_keys = TEST_REJECT_ALL_BLOCK_PROPOSAL.get();
8787
if public_keys.contains(
8888
&stacks_common::types::chainstate::StacksPublicKey::from_private(&self.private_key),
@@ -108,7 +108,7 @@ impl Signer {
108108
.unwrap_or_else(|e| self.handle_insert_block_error(e));
109109
Some(self.create_block_rejection(RejectReason::TestingDirective, &block_proposal.block))
110110
} else {
111-
block_response
111+
block_rejection
112112
}
113113
}
114114

0 commit comments

Comments
 (0)