@@ -68,6 +68,9 @@ use crate::net::server::HttpPeer;
68
68
use crate :: net:: { Error as NetError , Neighbor , NeighborAddress , NeighborKey } ;
69
69
use crate :: util_lib:: db:: { DBConn , Error as DBError } ;
70
70
71
+ /// How often to check for unconfirmed tenures
72
+ const CHECK_UNCONFIRMED_TENURES_MS : u128 = 1_000 ;
73
+
71
74
/// The overall downloader can operate in one of two states:
72
75
/// * it's doing IBD, in which case it's downloading tenures using neighbor inventories and
73
76
/// the start/end block ID hashes obtained from block-commits. This works up until the last two
@@ -118,6 +121,10 @@ pub struct NakamotoDownloadStateMachine {
118
121
pub ( super ) neighbor_rpc : NeighborRPC ,
119
122
/// Nakamoto chain tip
120
123
nakamoto_tip : StacksBlockId ,
124
+ /// do we need to fetch unconfirmed tenures?
125
+ fetch_unconfirmed_tenures : bool ,
126
+ /// last time an unconfirmed tenures was checked
127
+ last_unconfirmed_download_check_ms : u128 ,
121
128
/// last time an unconfirmed downloader was run
122
129
last_unconfirmed_download_run_ms : u128 ,
123
130
}
@@ -139,6 +146,8 @@ impl NakamotoDownloadStateMachine {
139
146
unconfirmed_tenure_downloads : HashMap :: new ( ) ,
140
147
neighbor_rpc : NeighborRPC :: new ( ) ,
141
148
nakamoto_tip,
149
+ fetch_unconfirmed_tenures : false ,
150
+ last_unconfirmed_download_check_ms : 0 ,
142
151
last_unconfirmed_download_run_ms : 0 ,
143
152
}
144
153
}
@@ -1218,6 +1227,7 @@ impl NakamotoDownloadStateMachine {
1218
1227
) {
1219
1228
Ok ( blocks_opt) => blocks_opt,
1220
1229
Err ( NetError :: StaleView ) => {
1230
+ neighbor_rpc. add_dead ( network, & naddr) ;
1221
1231
continue ;
1222
1232
}
1223
1233
Err ( e) => {
@@ -1426,14 +1436,30 @@ impl NakamotoDownloadStateMachine {
1426
1436
) ;
1427
1437
1428
1438
// check this now, since we mutate self.available
1429
- let need_unconfirmed_tenures = Self :: need_unconfirmed_tenures (
1430
- burnchain_height,
1431
- & network. burnchain_tip ,
1432
- & self . wanted_tenures ,
1433
- self . prev_wanted_tenures . as_ref ( ) . unwrap_or ( & vec ! [ ] ) ,
1434
- & self . tenure_block_ids ,
1435
- & self . available_tenures ,
1436
- ) ;
1439
+ self . fetch_unconfirmed_tenures = if self
1440
+ . last_unconfirmed_download_check_ms
1441
+ . saturating_add ( CHECK_UNCONFIRMED_TENURES_MS )
1442
+ > get_epoch_time_ms ( )
1443
+ {
1444
+ debug ! (
1445
+ "Throttle checking for unconfirmed tenures until {}" ,
1446
+ self . last_unconfirmed_download_check_ms
1447
+ . saturating_add( CHECK_UNCONFIRMED_TENURES_MS )
1448
+ / 1000
1449
+ ) ;
1450
+ false
1451
+ } else {
1452
+ let do_fetch = Self :: need_unconfirmed_tenures (
1453
+ burnchain_height,
1454
+ & network. burnchain_tip ,
1455
+ & self . wanted_tenures ,
1456
+ self . prev_wanted_tenures . as_ref ( ) . unwrap_or ( & vec ! [ ] ) ,
1457
+ & self . tenure_block_ids ,
1458
+ & self . available_tenures ,
1459
+ ) ;
1460
+ self . last_unconfirmed_download_check_ms = get_epoch_time_ms ( ) ;
1461
+ do_fetch
1462
+ } ;
1437
1463
1438
1464
match self . state {
1439
1465
NakamotoDownloadState :: Confirmed => {
@@ -1443,7 +1469,7 @@ impl NakamotoDownloadStateMachine {
1443
1469
. expect ( "FATAL: max_inflight_blocks exceeds usize::MAX" ) ,
1444
1470
) ;
1445
1471
1446
- if self . tenure_downloads . is_empty ( ) && need_unconfirmed_tenures {
1472
+ if self . tenure_downloads . is_empty ( ) && self . fetch_unconfirmed_tenures {
1447
1473
debug ! (
1448
1474
"Transition from {} to {}" ,
1449
1475
& self . state,
@@ -1488,7 +1514,7 @@ impl NakamotoDownloadStateMachine {
1488
1514
} else if self . unconfirmed_tenure_downloads . is_empty ( )
1489
1515
&& self . unconfirmed_tenure_download_schedule . is_empty ( )
1490
1516
{
1491
- if need_unconfirmed_tenures {
1517
+ if self . fetch_unconfirmed_tenures {
1492
1518
// do this again
1493
1519
self . unconfirmed_tenure_download_schedule =
1494
1520
Self :: make_unconfirmed_tenure_download_schedule (
0 commit comments