@@ -1289,10 +1289,9 @@ impl RelayerThread {
1289
1289
Ok ( ih. get_last_snapshot_with_sortition ( sort_tip. block_height ) ?)
1290
1290
}
1291
1291
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 (
1296
1295
chain_state : & mut StacksChainState ,
1297
1296
stacks_tip_id : & StacksBlockId ,
1298
1297
stacks_tip_sn : & BlockSnapshot ,
@@ -1303,22 +1302,9 @@ impl RelayerThread {
1303
1302
debug ! ( "Relayer: Sortition {} is empty" , & sn. consensus_hash) ;
1304
1303
return Ok ( false ) ;
1305
1304
}
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.
1309
1306
let mut ic = chain_state. index_conn ( ) ;
1310
1307
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.
1322
1308
let highest_tenure_start_block_header = NakamotoChainState :: get_tenure_start_block_header (
1323
1309
& mut ic,
1324
1310
stacks_tip_id,
@@ -1382,8 +1368,7 @@ impl RelayerThread {
1382
1368
& cursor. consensus_hash
1383
1369
) ;
1384
1370
1385
- // is this a valid sortiton?
1386
- if Self :: is_valid_sortition (
1371
+ if Self :: sortition_commits_to_stacks_tip_tenure (
1387
1372
chain_state,
1388
1373
& canonical_stacks_tip,
1389
1374
& canonical_stacks_tip_sn,
@@ -1803,21 +1788,12 @@ impl RelayerThread {
1803
1788
return ;
1804
1789
} ;
1805
1790
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
-
1816
1791
if let Some ( miner_thread_burn_view) = self . miner_thread_burn_view . as_ref ( ) {
1817
1792
// a miner thread is already running. If its burn view is the same as the canonical
1818
1793
// tip, then do nothing for now
1819
1794
if burn_tip. consensus_hash == miner_thread_burn_view. consensus_hash {
1820
1795
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.
1821
1797
return ;
1822
1798
}
1823
1799
}
@@ -1831,35 +1807,38 @@ impl RelayerThread {
1831
1807
SortitionDB :: get_block_snapshot_consensus ( self . sortdb . conn ( ) , & canonical_stacks_tip_ch)
1832
1808
. expect ( "FATAL: failed to query sortiiton DB for epoch" )
1833
1809
. 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
- }
1848
1810
let Ok ( last_winning_snapshot) = Self :: get_last_winning_snapshot ( & self . sortdb , & burn_tip)
1849
1811
. inspect_err ( |e| {
1850
1812
warn ! ( "Failed to load last winning snapshot: {e:?}" ) ;
1851
1813
} )
1852
1814
else {
1853
1815
// this should be unreachable, but don't tempt fate.
1854
1816
info ! ( "No prior snapshots have a winning sortition. Will not try to mine." ) ;
1817
+ self . tenure_extend_time = None ;
1855
1818
return ;
1856
1819
} ;
1857
-
1858
1820
let won_last_winning_snapshot = last_winning_snapshot. miner_pk_hash == Some ( mining_pk) ;
1859
1821
if won_last_winning_snapshot
1860
1822
&& self . need_block_found ( & canonical_stacks_snapshot, & last_winning_snapshot)
1861
1823
{
1862
1824
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 ;
1863
1842
return ;
1864
1843
}
1865
1844
// 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 {
1887
1866
"burn_view_snapshot" => %burn_tip. consensus_hash,
1888
1867
"block_election_snapshot" => %canonical_stacks_snapshot. consensus_hash,
1889
1868
"reason" => %reason) ;
1869
+ self . tenure_extend_time = None ;
1890
1870
}
1891
- self . tenure_extend_time = None ;
1892
1871
}
1893
1872
1894
1873
/// Main loop of the relayer.
0 commit comments