Skip to content

Commit 701ae1a

Browse files
committed
Use check_tenure_confirms_parent in check_block_against_signer_db_state to accomodate for unknown consensus hashes
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent e2f0e25 commit 701ae1a

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

stacks-signer/src/chainstate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl SortitionsView {
539539
///
540540
/// The rationale here is that the signer DB can be out-of-sync with the node. For example,
541541
/// the signer may have been added to an already-running node.
542-
fn check_tenure_change_confirms_parent(
542+
pub fn check_tenure_change_confirms_parent(
543543
tenure_change: &TenureChangePayload,
544544
block: &NakamotoBlock,
545545
signer_db: &mut SignerDb,

stacks-signer/src/v0/signer.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -582,45 +582,38 @@ impl Signer {
582582
}
583583
}
584584

585-
/// WARNING: Do NOT call this function PRIOR to check_proposal or block_proposal validation succeeds.
585+
/// WARNING: This is an incomplete check. Do NOT call this function PRIOR to check_proposal or block_proposal validation succeeds.
586586
///
587587
/// Re-verify a block's chain length against the last signed block within signerdb.
588588
/// This is required in case a block has been approved since the initial checks of the block validation endpoint.
589589
fn check_block_against_signer_db_state(
590-
&self,
590+
&mut self,
591+
stacks_client: &StacksClient,
591592
proposed_block: &NakamotoBlock,
592593
) -> Option<BlockResponse> {
593594
let signer_signature_hash = proposed_block.header.signer_signature_hash();
594595
let proposed_block_consensus_hash = proposed_block.header.consensus_hash;
595596
// If the tenure change block confirms the expected parent block, it should confirm at least one more block than the last accepted block in the parent tenure.
596597
if let Some(tenure_change) = proposed_block.get_tenure_change_tx_payload() {
597-
match SortitionsView::get_tenure_last_block_info(
598-
&tenure_change.prev_tenure_consensus_hash,
599-
&self.signer_db,
598+
// Ensure that the tenure change block confirms the expected parent block
599+
match SortitionsView::check_tenure_change_confirms_parent(
600+
tenure_change,
601+
proposed_block,
602+
&mut self.signer_db,
603+
stacks_client,
600604
self.proposal_config.tenure_last_block_proposal_timeout,
601605
) {
602-
Ok(Some(last_block_info)) => {
603-
if proposed_block.header.chain_length
604-
<= last_block_info.block.header.chain_length
605-
{
606-
warn!(
607-
"Miner's block proposal does not confirm as many blocks as we expect";
608-
"proposed_block_consensus_hash" => %proposed_block_consensus_hash,
609-
"proposed_block_signer_sighash" => %signer_signature_hash,
610-
"proposed_chain_length" => proposed_block.header.chain_length,
611-
"expected_at_least" => last_block_info.block.header.chain_length + 1,
612-
);
613-
return Some(self.create_block_rejection(
606+
Ok(true) => {}
607+
Ok(false) => {
608+
return Some(
609+
self.create_block_rejection(
614610
RejectCode::SortitionViewMismatch,
615611
proposed_block,
616-
));
617-
}
618-
}
619-
Ok(_) => {
620-
// We have no information about the parent consensus hash. Just assume its valid.
612+
),
613+
)
621614
}
622615
Err(e) => {
623-
warn!("{self}: Failed to check block against signer db: {e}";
616+
warn!("{self}: Error checking block proposal: {e}";
624617
"signer_sighash" => %signer_signature_hash,
625618
"block_id" => %proposed_block.block_id()
626619
);
@@ -631,8 +624,7 @@ impl Signer {
631624
}
632625
}
633626

634-
// Ensure that the block proposal confirms the expected number of blocks in the current tenure
635-
// (This may be redundant for a tenure change block, but we could have had two valid tenure change blocks in a row)
627+
// Ensure that the block is the last block in the chain of its current tenure.
636628
match self
637629
.signer_db
638630
.get_last_accepted_block(&proposed_block_consensus_hash)
@@ -712,7 +704,9 @@ impl Signer {
712704
}
713705
};
714706

715-
if let Some(block_response) = self.check_block_against_signer_db_state(&block_info.block) {
707+
if let Some(block_response) =
708+
self.check_block_against_signer_db_state(stacks_client, &block_info.block)
709+
{
716710
// The signer db state has changed. We no longer view this block as valid. Override the validation response.
717711
if let Err(e) = block_info.mark_locally_rejected() {
718712
if !block_info.has_reached_consensus() {

0 commit comments

Comments
 (0)