Skip to content

Commit 1dcd321

Browse files
committed
Cleanup need_block_found logic to be more clear
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent c0cf545 commit 1dcd321

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

testnet/stacks-node/src/nakamoto_node/relayer.rs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -632,37 +632,45 @@ impl RelayerThread {
632632
return None;
633633
};
634634

635-
if self.need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot) {
636-
info!(
637-
"Relayer: will submit late BlockFound for {}",
635+
let won_last_winning_snapshot =
636+
mining_pkh_opt.is_some() && last_winning_snapshot.miner_pk_hash == mining_pkh_opt;
637+
638+
if won_last_winning_snapshot {
639+
debug!(
640+
"Relayer: we won the last winning sortition {}",
638641
&last_winning_snapshot.consensus_hash
639642
);
640-
// prepare to immediately extend after our BlockFound gets mined.
641-
self.tenure_extend_time = Some(TenureExtendTime::immediate());
642-
return Some(MinerDirective::BeginTenure {
643-
parent_tenure_start: StacksBlockId(
644-
last_winning_snapshot.winning_stacks_block_hash.clone().0,
645-
),
646-
burnchain_tip: sn,
647-
election_block: last_winning_snapshot,
648-
late: true,
649-
});
650-
}
651643

652-
let won_last_winning_snapshot =
653-
mining_pkh_opt.is_some() && last_winning_snapshot.miner_pk_hash == mining_pkh_opt;
654-
let tip_is_last_winning_snapshot = mining_pkh_opt.is_some()
655-
&& canonical_stacks_snapshot.block_height == last_winning_snapshot.block_height
656-
&& canonical_stacks_snapshot.consensus_hash == last_winning_snapshot.consensus_hash;
657-
658-
if won_last_winning_snapshot && tip_is_last_winning_snapshot {
659-
// this is the ongoing tenure snapshot. A BlockFound has already been issued. We
660-
// can instead opt to Extend immediately
661-
info!("Relayer: BlockFound already issued for the last winning sortition. Will extend tenure.");
662-
return Some(MinerDirective::ContinueTenure {
663-
new_burn_view: sn.consensus_hash,
664-
});
644+
if self.need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot) {
645+
info!(
646+
"Relayer: will submit late BlockFound for {}",
647+
&last_winning_snapshot.consensus_hash
648+
);
649+
// prepare to immediately extend after our BlockFound gets mined.
650+
self.tenure_extend_time = Some(TenureExtendTime::immediate());
651+
return Some(MinerDirective::BeginTenure {
652+
parent_tenure_start: StacksBlockId(
653+
last_winning_snapshot.winning_stacks_block_hash.clone().0,
654+
),
655+
burnchain_tip: sn,
656+
election_block: last_winning_snapshot,
657+
late: true,
658+
});
659+
}
660+
let tip_is_last_winning_snapshot = mining_pkh_opt.is_some()
661+
&& canonical_stacks_snapshot.block_height == last_winning_snapshot.block_height
662+
&& canonical_stacks_snapshot.consensus_hash == last_winning_snapshot.consensus_hash;
663+
664+
if tip_is_last_winning_snapshot {
665+
// this is the ongoing tenure snapshot. A BlockFound has already been issued. We
666+
// can instead opt to Extend immediately
667+
info!("Relayer: BlockFound already issued for the last winning sortition. Will extend tenure.");
668+
return Some(MinerDirective::ContinueTenure {
669+
new_burn_view: sn.consensus_hash,
670+
});
671+
}
665672
}
673+
666674
if won_ongoing_tenure_sortition {
667675
info!("Relayer: No sortition, but we produced the canonical Stacks tip. Will extend tenure.");
668676
if !won_last_winning_snapshot {
@@ -684,27 +692,16 @@ impl RelayerThread {
684692
return None;
685693
}
686694

687-
/// Determine if we need to issue a BlockFound.
695+
/// Determine if we the current tenure winner needs to issue a BlockFound.
696+
/// Assumes the caller has already checked that the last-winning snapshot was won by us.
688697
///
689-
/// Returns true if we won the last non-empty sortition, and the stacks tip's snapshot is an ancestor of the last-won sortition
698+
/// Returns true if the stacks tip's snapshot is an ancestor of the last-won sortition
690699
/// Returns false otherwise.
691700
fn need_block_found(
692701
&mut self,
693702
canonical_stacks_snapshot: &BlockSnapshot,
694703
last_winning_snapshot: &BlockSnapshot,
695704
) -> bool {
696-
if last_winning_snapshot.miner_pk_hash != self.get_mining_key_pkh() {
697-
debug!(
698-
"Relayer: we did not win the last winning sortition {}",
699-
&last_winning_snapshot.consensus_hash
700-
);
701-
// We did not win the last winning snapshot, we do not need to issue a block found.
702-
return false;
703-
}
704-
debug!(
705-
"Relayer: we won the last winning sortition {}",
706-
&last_winning_snapshot.consensus_hash
707-
);
708705
// we won the last non-empty sortition. Has there been a BlockFound issued for it?
709706
// This would be true if the stacks tip's tenure is at or descends from this snapshot.
710707
// If there has _not_ been a BlockFound, then we should issue one.
@@ -1854,11 +1851,15 @@ impl RelayerThread {
18541851
return;
18551852
};
18561853

1857-
if self.need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot) {
1854+
let won_last_winning_snapshot = last_winning_snapshot.miner_pk_hash == Some(mining_pk);
1855+
if won_last_winning_snapshot
1856+
&& self.need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot)
1857+
{
18581858
info!("Will not extend tenure -- need to issue a BlockFound first");
18591859
return;
18601860
}
1861-
1861+
// If we reach this code, we have either won the last winning snapshot and have already issued a block found for it and should extend.
1862+
// OR we did not win the last snapshot, but the person who did has failed to produce a block and we should extend our old tenure.
18621863
if let Err(e) = self.stop_tenure() {
18631864
error!("Relayer: Failed to stop tenure: {e:?}");
18641865
return;

0 commit comments

Comments
 (0)