Skip to content

Commit bce9839

Browse files
committed
fix: disconnect from neighbors serving unconfirmed tenures that are stale (otherwise we would cease to make progress if the node never caught up), and throttle down unconfirmed download checks
1 parent 18be1fe commit bce9839

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

stackslib/src/net/download/nakamoto/download_state_machine.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ use crate::net::server::HttpPeer;
6868
use crate::net::{Error as NetError, Neighbor, NeighborAddress, NeighborKey};
6969
use crate::util_lib::db::{DBConn, Error as DBError};
7070

71+
/// How often to check for unconfirmed tenures
72+
const CHECK_UNCONFIRMED_TENURES_MS: u128 = 1_000;
73+
7174
/// The overall downloader can operate in one of two states:
7275
/// * it's doing IBD, in which case it's downloading tenures using neighbor inventories and
7376
/// the start/end block ID hashes obtained from block-commits. This works up until the last two
@@ -118,6 +121,10 @@ pub struct NakamotoDownloadStateMachine {
118121
pub(super) neighbor_rpc: NeighborRPC,
119122
/// Nakamoto chain tip
120123
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,
121128
/// last time an unconfirmed downloader was run
122129
last_unconfirmed_download_run_ms: u128,
123130
}
@@ -139,6 +146,8 @@ impl NakamotoDownloadStateMachine {
139146
unconfirmed_tenure_downloads: HashMap::new(),
140147
neighbor_rpc: NeighborRPC::new(),
141148
nakamoto_tip,
149+
fetch_unconfirmed_tenures: false,
150+
last_unconfirmed_download_check_ms: 0,
142151
last_unconfirmed_download_run_ms: 0,
143152
}
144153
}
@@ -1218,6 +1227,7 @@ impl NakamotoDownloadStateMachine {
12181227
) {
12191228
Ok(blocks_opt) => blocks_opt,
12201229
Err(NetError::StaleView) => {
1230+
neighbor_rpc.add_dead(network, &naddr);
12211231
continue;
12221232
}
12231233
Err(e) => {
@@ -1426,14 +1436,30 @@ impl NakamotoDownloadStateMachine {
14261436
);
14271437

14281438
// 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+
};
14371463

14381464
match self.state {
14391465
NakamotoDownloadState::Confirmed => {
@@ -1443,7 +1469,7 @@ impl NakamotoDownloadStateMachine {
14431469
.expect("FATAL: max_inflight_blocks exceeds usize::MAX"),
14441470
);
14451471

1446-
if self.tenure_downloads.is_empty() && need_unconfirmed_tenures {
1472+
if self.tenure_downloads.is_empty() && self.fetch_unconfirmed_tenures {
14471473
debug!(
14481474
"Transition from {} to {}",
14491475
&self.state,
@@ -1488,7 +1514,7 @@ impl NakamotoDownloadStateMachine {
14881514
} else if self.unconfirmed_tenure_downloads.is_empty()
14891515
&& self.unconfirmed_tenure_download_schedule.is_empty()
14901516
{
1491-
if need_unconfirmed_tenures {
1517+
if self.fetch_unconfirmed_tenures {
14921518
// do this again
14931519
self.unconfirmed_tenure_download_schedule =
14941520
Self::make_unconfirmed_tenure_download_schedule(

0 commit comments

Comments
 (0)