Skip to content

Commit 9eb307b

Browse files
committed
Cleanup try_continue_tenure to overwrite tenure_extend_time to None where appropriate
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 3469c50 commit 9eb307b

File tree

1 file changed

+25
-46
lines changed

1 file changed

+25
-46
lines changed

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

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,10 +1289,9 @@ impl RelayerThread {
12891289
Ok(ih.get_last_snapshot_with_sortition(sort_tip.block_height)?)
12901290
}
12911291

1292-
/// Is the given sortition a valid sortition?
1293-
/// I.e. whose winning commit's parent tenure ID is on the canonical Stacks history,
1294-
/// and whose consensus hash corresponds to the ongoing tenure or a confirmed tenure?
1295-
fn is_valid_sortition(
1292+
/// Returns true if the sortition `sn` commits to the tenure start block of the ongoing Stacks tenure `stacks_tip_sn`.
1293+
/// Returns false otherwise.
1294+
fn sortition_commits_to_stacks_tip_tenure(
12961295
chain_state: &mut StacksChainState,
12971296
stacks_tip_id: &StacksBlockId,
12981297
stacks_tip_sn: &BlockSnapshot,
@@ -1303,22 +1302,9 @@ impl RelayerThread {
13031302
debug!("Relayer: Sortition {} is empty", &sn.consensus_hash);
13041303
return Ok(false);
13051304
}
1306-
1307-
// check that this commit's parent tenure ID is on the history tipped at
1308-
// `stacks_tip_id`
1305+
// The sortition must commit to the tenure start block of the ongoing Stacks tenure.
13091306
let mut ic = chain_state.index_conn();
13101307
let parent_tenure_id = StacksBlockId(sn.winning_stacks_block_hash.clone().0);
1311-
let height_opt = ic.get_ancestor_block_height(&parent_tenure_id, stacks_tip_id)?;
1312-
if height_opt.is_none() {
1313-
// parent_tenure_id is not an ancestor of stacks_tip_id
1314-
debug!(
1315-
"Relayer: Sortition {} has winning commit hash {parent_tenure_id}, which is not canonical",
1316-
&sn.consensus_hash
1317-
);
1318-
return Ok(false);
1319-
}
1320-
1321-
// The sortition must commit to the tenure start block of the ongoing Stacks tenure.
13221308
let highest_tenure_start_block_header = NakamotoChainState::get_tenure_start_block_header(
13231309
&mut ic,
13241310
stacks_tip_id,
@@ -1382,8 +1368,7 @@ impl RelayerThread {
13821368
&cursor.consensus_hash
13831369
);
13841370

1385-
// is this a valid sortiton?
1386-
if Self::is_valid_sortition(
1371+
if Self::sortition_commits_to_stacks_tip_tenure(
13871372
chain_state,
13881373
&canonical_stacks_tip,
13891374
&canonical_stacks_tip_sn,
@@ -1803,21 +1788,12 @@ impl RelayerThread {
18031788
return;
18041789
};
18051790

1806-
let won_sortition = burn_tip.sortition && burn_tip.miner_pk_hash == Some(mining_pk);
1807-
1808-
if won_sortition {
1809-
debug!("Will not tenure extend. Won current sortition";
1810-
"burn_chain_sortition_tip_ch" => %burn_tip.consensus_hash
1811-
);
1812-
self.tenure_extend_time = None;
1813-
return;
1814-
}
1815-
18161791
if let Some(miner_thread_burn_view) = self.miner_thread_burn_view.as_ref() {
18171792
// a miner thread is already running. If its burn view is the same as the canonical
18181793
// tip, then do nothing for now
18191794
if burn_tip.consensus_hash == miner_thread_burn_view.consensus_hash {
18201795
info!("Will not try to start a tenure extend -- the current miner thread's burn view matches the sortition tip"; "sortition tip" => %burn_tip.consensus_hash);
1796+
// Do not reset the timer, as we may be able to extend later.
18211797
return;
18221798
}
18231799
}
@@ -1831,35 +1807,38 @@ impl RelayerThread {
18311807
SortitionDB::get_block_snapshot_consensus(self.sortdb.conn(), &canonical_stacks_tip_ch)
18321808
.expect("FATAL: failed to query sortiiton DB for epoch")
18331809
.expect("FATAL: no sortition for canonical stacks tip");
1834-
1835-
let won_ongoing_tenure_sortition =
1836-
canonical_stacks_snapshot.miner_pk_hash == Some(mining_pk);
1837-
1838-
if !won_ongoing_tenure_sortition {
1839-
// We did not win the ongoing tenure sortition, so nothing we can even do.
1840-
debug!("Will not tenure extend. Did not win ongoing tenure sortition";
1841-
"burn_chain_sortition_tip_ch" => %burn_tip.consensus_hash,
1842-
"canonical_stacks_tip_ch" => %canonical_stacks_tip_ch,
1843-
"burn_chain_sortition_tip_mining_pk" => ?burn_tip.miner_pk_hash,
1844-
"mining_pk" => %mining_pk,
1845-
);
1846-
return;
1847-
}
18481810
let Ok(last_winning_snapshot) = Self::get_last_winning_snapshot(&self.sortdb, &burn_tip)
18491811
.inspect_err(|e| {
18501812
warn!("Failed to load last winning snapshot: {e:?}");
18511813
})
18521814
else {
18531815
// this should be unreachable, but don't tempt fate.
18541816
info!("No prior snapshots have a winning sortition. Will not try to mine.");
1817+
self.tenure_extend_time = None;
18551818
return;
18561819
};
1857-
18581820
let won_last_winning_snapshot = last_winning_snapshot.miner_pk_hash == Some(mining_pk);
18591821
if won_last_winning_snapshot
18601822
&& self.need_block_found(&canonical_stacks_snapshot, &last_winning_snapshot)
18611823
{
18621824
info!("Will not extend tenure -- need to issue a BlockFound first");
1825+
// We may manage to extend later, so don't set the timer to None.
1826+
return;
1827+
}
1828+
let won_ongoing_tenure_sortition =
1829+
canonical_stacks_snapshot.miner_pk_hash == Some(mining_pk);
1830+
if !won_ongoing_tenure_sortition {
1831+
// We did not win the ongoing tenure sortition, so nothing we can even do.
1832+
// Make sure this check is done AFTER checking for the BlockFound so that
1833+
// we can set tenure_extend_time to None in this case without causing problems
1834+
// (If we need to issue a block found, we may not have won_ongoing_tenure_sortition)
1835+
debug!("Will not tenure extend. Did not win ongoing tenure sortition";
1836+
"burn_chain_sortition_tip_ch" => %burn_tip.consensus_hash,
1837+
"canonical_stacks_tip_ch" => %canonical_stacks_tip_ch,
1838+
"burn_chain_sortition_tip_mining_pk" => ?burn_tip.miner_pk_hash,
1839+
"mining_pk" => %mining_pk,
1840+
);
1841+
self.tenure_extend_time = None;
18631842
return;
18641843
}
18651844
// 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.
@@ -1887,8 +1866,8 @@ impl RelayerThread {
18871866
"burn_view_snapshot" => %burn_tip.consensus_hash,
18881867
"block_election_snapshot" => %canonical_stacks_snapshot.consensus_hash,
18891868
"reason" => %reason);
1869+
self.tenure_extend_time = None;
18901870
}
1891-
self.tenure_extend_time = None;
18921871
}
18931872

18941873
/// Main loop of the relayer.

0 commit comments

Comments
 (0)