Skip to content

Commit 064a223

Browse files
committed
fix: fix 5267 by making it so that any peer can be re-assigned to an idle downloader
1 parent a4597f5 commit 064a223

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl NakamotoDownloadStateMachine {
384384
&new_wanted_tenures
385385
);
386386
self.wanted_tenures.append(&mut new_wanted_tenures);
387-
debug!("extended wanted_tenures is now {:?}", &self.wanted_tenures);
387+
test_debug!("extended wanted_tenures is now {:?}", &self.wanted_tenures);
388388

389389
Ok(())
390390
}
@@ -983,9 +983,9 @@ impl NakamotoDownloadStateMachine {
983983
prev_schedule
984984
};
985985

986-
debug!("new schedule: {:?}", schedule);
987-
debug!("new available: {:?}", &available);
988-
debug!("new tenure_block_ids: {:?}", &tenure_block_ids);
986+
test_debug!("new schedule: {:?}", schedule);
987+
test_debug!("new available: {:?}", &available);
988+
test_debug!("new tenure_block_ids: {:?}", &tenure_block_ids);
989989

990990
self.tenure_download_schedule = schedule;
991991
self.tenure_block_ids = tenure_block_ids;
@@ -1023,7 +1023,7 @@ impl NakamotoDownloadStateMachine {
10231023
.map(|wt| (wt.burn_height, &wt.tenure_id_consensus_hash))
10241024
.collect();
10251025

1026-
debug!("Check availability {:?}", available);
1026+
test_debug!("Check availability {:?}", available);
10271027
let mut highest_available = Vec::with_capacity(2);
10281028
for (_, ch) in tenure_block_heights.iter().rev() {
10291029
let available_count = available

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,18 @@ use crate::util_lib::db::{DBConn, Error as DBError};
6666
/// start and end block. This includes all tenures except for the two most recent ones.
6767
#[derive(Debug, Clone, PartialEq)]
6868
pub enum NakamotoTenureDownloadState {
69-
/// Getting the tenure-start block (the given StacksBlockId is it's block ID).
70-
GetTenureStartBlock(StacksBlockId),
69+
/// Getting the tenure-start block (the given StacksBlockId is it's block ID), as well as the
70+
/// millisecond epoch timestamp at which the request began
71+
GetTenureStartBlock(StacksBlockId, u128),
7172
/// Getting the tenure-end block.
72-
///
73-
/// The field here is the block ID of the tenure end block.
74-
GetTenureEndBlock(StacksBlockId),
73+
/// The fields here are the block ID of the tenure end block, as well as the millisecond epoch
74+
/// timestamp at which the request begahn
75+
GetTenureEndBlock(StacksBlockId, u128),
7576
/// Receiving tenure blocks.
76-
/// The field here is the hash of the _last_ block in the tenure that must be downloaded. This
77-
/// is because a tenure is fetched in order from highest block to lowest block.
78-
GetTenureBlocks(StacksBlockId),
77+
/// The fields here are the hash of the _last_ block in the tenure that must be downloaded, as well
78+
/// as the millisecond epoch timestamp at which the request began. The first field is needed
79+
/// because a tenure is fetched in order from highest block to lowest block.
80+
GetTenureBlocks(StacksBlockId, u128),
7981
/// We have gotten all the blocks for this tenure
8082
Done,
8183
}
@@ -166,7 +168,7 @@ impl NakamotoTenureDownloader {
166168
start_signer_keys,
167169
end_signer_keys,
168170
idle: false,
169-
state: NakamotoTenureDownloadState::GetTenureStartBlock(tenure_start_block_id.clone()),
171+
state: NakamotoTenureDownloadState::GetTenureStartBlock(tenure_start_block_id.clone(), get_epoch_time_ms()),
170172
tenure_start_block: None,
171173
tenure_end_block: None,
172174
tenure_blocks: None,
@@ -187,7 +189,7 @@ impl NakamotoTenureDownloader {
187189
&mut self,
188190
tenure_start_block: NakamotoBlock,
189191
) -> Result<(), NetError> {
190-
let NakamotoTenureDownloadState::GetTenureStartBlock(_) = &self.state else {
192+
let NakamotoTenureDownloadState::GetTenureStartBlock(..) = &self.state else {
191193
// not the right state for this
192194
warn!("Invalid state for this method";
193195
"state" => %self.state);
@@ -235,7 +237,7 @@ impl NakamotoTenureDownloader {
235237
} else {
236238
// need to get tenure_end_block.
237239
self.state =
238-
NakamotoTenureDownloadState::GetTenureEndBlock(self.tenure_end_block_id.clone());
240+
NakamotoTenureDownloadState::GetTenureEndBlock(self.tenure_end_block_id.clone(), get_epoch_time_ms());
239241
}
240242
Ok(())
241243
}
@@ -252,7 +254,7 @@ impl NakamotoTenureDownloader {
252254
) -> Result<(), NetError> {
253255
if !matches!(
254256
&self.state,
255-
NakamotoTenureDownloadState::GetTenureEndBlock(_)
257+
NakamotoTenureDownloadState::GetTenureEndBlock(..)
256258
) {
257259
warn!("Invalid state for this method";
258260
"state" => %self.state);
@@ -326,6 +328,7 @@ impl NakamotoTenureDownloader {
326328
self.tenure_end_block = Some(tenure_end_block.clone());
327329
self.state = NakamotoTenureDownloadState::GetTenureBlocks(
328330
tenure_end_block.header.parent_block_id.clone(),
331+
get_epoch_time_ms()
329332
);
330333
Ok(())
331334
}
@@ -361,7 +364,7 @@ impl NakamotoTenureDownloader {
361364
&mut self,
362365
mut tenure_blocks: Vec<NakamotoBlock>,
363366
) -> Result<Option<Vec<NakamotoBlock>>, NetError> {
364-
let NakamotoTenureDownloadState::GetTenureBlocks(block_cursor) = &self.state else {
367+
let NakamotoTenureDownloadState::GetTenureBlocks(block_cursor, start_request_time) = &self.state else {
365368
warn!("Invalid state for this method";
366369
"state" => %self.state);
367370
return Err(NetError::InvalidState);
@@ -461,7 +464,7 @@ impl NakamotoTenureDownloader {
461464
&earliest_block.block_id(),
462465
&next_block_id
463466
);
464-
self.state = NakamotoTenureDownloadState::GetTenureBlocks(next_block_id);
467+
self.state = NakamotoTenureDownloadState::GetTenureBlocks(next_block_id, *start_request_time);
465468
return Ok(None);
466469
}
467470

@@ -486,16 +489,16 @@ impl NakamotoTenureDownloader {
486489
peerhost: PeerHost,
487490
) -> Result<Option<StacksHttpRequest>, ()> {
488491
let request = match self.state {
489-
NakamotoTenureDownloadState::GetTenureStartBlock(start_block_id) => {
490-
debug!("Request tenure-start block {}", &start_block_id);
492+
NakamotoTenureDownloadState::GetTenureStartBlock(start_block_id, start_request_time) => {
493+
debug!("Request tenure-start block {} at {}", &start_block_id, start_request_time);
491494
StacksHttpRequest::new_get_nakamoto_block(peerhost, start_block_id.clone())
492495
}
493-
NakamotoTenureDownloadState::GetTenureEndBlock(end_block_id) => {
494-
debug!("Request tenure-end block {}", &end_block_id);
496+
NakamotoTenureDownloadState::GetTenureEndBlock(end_block_id, start_request_time) => {
497+
debug!("Request tenure-end block {} at {}", &end_block_id, start_request_time);
495498
StacksHttpRequest::new_get_nakamoto_block(peerhost, end_block_id.clone())
496499
}
497-
NakamotoTenureDownloadState::GetTenureBlocks(end_block_id) => {
498-
debug!("Downloading tenure ending at {}", &end_block_id);
500+
NakamotoTenureDownloadState::GetTenureBlocks(end_block_id, start_request_time) => {
501+
debug!("Downloading tenure ending at {} at {}", &end_block_id, start_request_time);
499502
StacksHttpRequest::new_get_nakamoto_tenure(peerhost, end_block_id.clone(), None)
500503
}
501504
NakamotoTenureDownloadState::Done => {
@@ -558,10 +561,10 @@ impl NakamotoTenureDownloader {
558561
response: StacksHttpResponse,
559562
) -> Result<Option<Vec<NakamotoBlock>>, NetError> {
560563
let handle_result = match self.state {
561-
NakamotoTenureDownloadState::GetTenureStartBlock(_block_id) => {
564+
NakamotoTenureDownloadState::GetTenureStartBlock(_block_id, _start_request_time) => {
562565
debug!(
563-
"Got download response for tenure-start block {}",
564-
&_block_id
566+
"Got download response for tenure-start block {} in {}ms",
567+
&_block_id, get_epoch_time_ms().saturating_sub(_start_request_time)
565568
);
566569
let block = response.decode_nakamoto_block().map_err(|e| {
567570
warn!("Failed to decode response for a Nakamoto block: {:?}", &e);
@@ -570,19 +573,19 @@ impl NakamotoTenureDownloader {
570573
self.try_accept_tenure_start_block(block)?;
571574
Ok(None)
572575
}
573-
NakamotoTenureDownloadState::GetTenureEndBlock(_block_id) => {
574-
debug!("Got download response to tenure-end block {}", &_block_id);
576+
NakamotoTenureDownloadState::GetTenureEndBlock(_block_id, _start_request_time) => {
577+
debug!("Got download response to tenure-end block {} in {}ms", &_block_id, get_epoch_time_ms().saturating_sub(_start_request_time));
575578
let block = response.decode_nakamoto_block().map_err(|e| {
576579
warn!("Failed to decode response for a Nakamoto block: {:?}", &e);
577580
e
578581
})?;
579582
self.try_accept_tenure_end_block(&block)?;
580583
Ok(None)
581584
}
582-
NakamotoTenureDownloadState::GetTenureBlocks(_end_block_id) => {
585+
NakamotoTenureDownloadState::GetTenureBlocks(_end_block_id, _start_request_time) => {
583586
debug!(
584-
"Got download response for tenure blocks ending at {}",
585-
&_end_block_id
587+
"Got download response for tenure blocks ending at {} in {}ms",
588+
&_end_block_id, get_epoch_time_ms().saturating_sub(_start_request_time)
586589
);
587590
let blocks = response.decode_nakamoto_tenure().map_err(|e| {
588591
warn!("Failed to decode response for a Nakamoto tenure: {:?}", &e);

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,11 @@ impl NakamotoTenureDownloaderSet {
230230
if !downloader.idle {
231231
continue;
232232
}
233-
if downloader.naddr != naddr {
234-
continue;
235-
}
236233
debug!(
237234
"Assign peer {} to work on downloader for {} in state {}",
238235
&naddr, &downloader.tenure_id_consensus_hash, &downloader.state
239236
);
237+
downloader.naddr = naddr.clone();
240238
self.peers.insert(naddr, i);
241239
return true;
242240
}
@@ -308,8 +306,8 @@ impl NakamotoTenureDownloaderSet {
308306
};
309307
if &downloader.tenure_id_consensus_hash == tenure_id {
310308
debug!(
311-
"Have downloader for tenure {} already (idle={}, state={})",
312-
tenure_id, downloader.idle, &downloader.state
309+
"Have downloader for tenure {} already (idle={}, state={}, naddr={})",
310+
tenure_id, downloader.idle, &downloader.state, &downloader.naddr
313311
);
314312
return true;
315313
}
@@ -328,7 +326,7 @@ impl NakamotoTenureDownloaderSet {
328326
count: usize,
329327
current_reward_cycles: &BTreeMap<u64, CurrentRewardSet>,
330328
) {
331-
debug!("make_tenure_downloaders";
329+
test_debug!("make_tenure_downloaders";
332330
"schedule" => ?schedule,
333331
"available" => ?available,
334332
"tenure_block_ids" => ?tenure_block_ids,
@@ -463,7 +461,7 @@ impl NakamotoTenureDownloaderSet {
463461
continue;
464462
};
465463
if downloader.is_done() {
466-
debug!("Downloader for {} is done", &naddr);
464+
debug!("Downloader for {} on tenure {} is finished", &naddr, &downloader.tenure_id_consensus_hash);
467465
finished.push(naddr.clone());
468466
finished_tenures.push(downloader.tenure_id_consensus_hash.clone());
469467
continue;
@@ -534,6 +532,7 @@ impl NakamotoTenureDownloaderSet {
534532
);
535533
new_blocks.insert(downloader.tenure_id_consensus_hash.clone(), blocks);
536534
if downloader.is_done() {
535+
debug!("Downloader for {} on tenure {} is finished", &naddr, &downloader.tenure_id_consensus_hash);
537536
finished.push(naddr.clone());
538537
finished_tenures.push(downloader.tenure_id_consensus_hash.clone());
539538
continue;

0 commit comments

Comments
 (0)