Skip to content

Commit 721a463

Browse files
authored
Merge pull request #5270 from stacks-network/fix/5267
Fix/5267
2 parents 84a0f9a + 8dc80bd commit 721a463

File tree

4 files changed

+107
-50
lines changed

4 files changed

+107
-50
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: 58 additions & 29 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,10 @@ 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(
172+
tenure_start_block_id.clone(),
173+
get_epoch_time_ms(),
174+
),
170175
tenure_start_block: None,
171176
tenure_end_block: None,
172177
tenure_blocks: None,
@@ -187,7 +192,7 @@ impl NakamotoTenureDownloader {
187192
&mut self,
188193
tenure_start_block: NakamotoBlock,
189194
) -> Result<(), NetError> {
190-
let NakamotoTenureDownloadState::GetTenureStartBlock(_) = &self.state else {
195+
let NakamotoTenureDownloadState::GetTenureStartBlock(..) = &self.state else {
191196
// not the right state for this
192197
warn!("Invalid state for this method";
193198
"state" => %self.state);
@@ -234,8 +239,10 @@ impl NakamotoTenureDownloader {
234239
self.try_accept_tenure_end_block(&tenure_end_block)?;
235240
} else {
236241
// need to get tenure_end_block.
237-
self.state =
238-
NakamotoTenureDownloadState::GetTenureEndBlock(self.tenure_end_block_id.clone());
242+
self.state = NakamotoTenureDownloadState::GetTenureEndBlock(
243+
self.tenure_end_block_id.clone(),
244+
get_epoch_time_ms(),
245+
);
239246
}
240247
Ok(())
241248
}
@@ -252,7 +259,7 @@ impl NakamotoTenureDownloader {
252259
) -> Result<(), NetError> {
253260
if !matches!(
254261
&self.state,
255-
NakamotoTenureDownloadState::GetTenureEndBlock(_)
262+
NakamotoTenureDownloadState::GetTenureEndBlock(..)
256263
) {
257264
warn!("Invalid state for this method";
258265
"state" => %self.state);
@@ -326,6 +333,7 @@ impl NakamotoTenureDownloader {
326333
self.tenure_end_block = Some(tenure_end_block.clone());
327334
self.state = NakamotoTenureDownloadState::GetTenureBlocks(
328335
tenure_end_block.header.parent_block_id.clone(),
336+
get_epoch_time_ms(),
329337
);
330338
Ok(())
331339
}
@@ -361,7 +369,9 @@ impl NakamotoTenureDownloader {
361369
&mut self,
362370
mut tenure_blocks: Vec<NakamotoBlock>,
363371
) -> Result<Option<Vec<NakamotoBlock>>, NetError> {
364-
let NakamotoTenureDownloadState::GetTenureBlocks(block_cursor) = &self.state else {
372+
let NakamotoTenureDownloadState::GetTenureBlocks(block_cursor, start_request_time) =
373+
&self.state
374+
else {
365375
warn!("Invalid state for this method";
366376
"state" => %self.state);
367377
return Err(NetError::InvalidState);
@@ -473,7 +483,8 @@ impl NakamotoTenureDownloader {
473483
&earliest_block.block_id(),
474484
&next_block_id
475485
);
476-
self.state = NakamotoTenureDownloadState::GetTenureBlocks(next_block_id);
486+
self.state =
487+
NakamotoTenureDownloadState::GetTenureBlocks(next_block_id, *start_request_time);
477488
return Ok(None);
478489
}
479490

@@ -498,16 +509,28 @@ impl NakamotoTenureDownloader {
498509
peerhost: PeerHost,
499510
) -> Result<Option<StacksHttpRequest>, ()> {
500511
let request = match self.state {
501-
NakamotoTenureDownloadState::GetTenureStartBlock(start_block_id) => {
502-
debug!("Request tenure-start block {}", &start_block_id);
512+
NakamotoTenureDownloadState::GetTenureStartBlock(
513+
start_block_id,
514+
start_request_time,
515+
) => {
516+
debug!(
517+
"Request tenure-start block {} at {}",
518+
&start_block_id, start_request_time
519+
);
503520
StacksHttpRequest::new_get_nakamoto_block(peerhost, start_block_id.clone())
504521
}
505-
NakamotoTenureDownloadState::GetTenureEndBlock(end_block_id) => {
506-
debug!("Request tenure-end block {}", &end_block_id);
522+
NakamotoTenureDownloadState::GetTenureEndBlock(end_block_id, start_request_time) => {
523+
debug!(
524+
"Request tenure-end block {} at {}",
525+
&end_block_id, start_request_time
526+
);
507527
StacksHttpRequest::new_get_nakamoto_block(peerhost, end_block_id.clone())
508528
}
509-
NakamotoTenureDownloadState::GetTenureBlocks(end_block_id) => {
510-
debug!("Downloading tenure ending at {}", &end_block_id);
529+
NakamotoTenureDownloadState::GetTenureBlocks(end_block_id, start_request_time) => {
530+
debug!(
531+
"Downloading tenure ending at {} at {}",
532+
&end_block_id, start_request_time
533+
);
511534
StacksHttpRequest::new_get_nakamoto_tenure(peerhost, end_block_id.clone(), None)
512535
}
513536
NakamotoTenureDownloadState::Done => {
@@ -570,10 +593,11 @@ impl NakamotoTenureDownloader {
570593
response: StacksHttpResponse,
571594
) -> Result<Option<Vec<NakamotoBlock>>, NetError> {
572595
let handle_result = match self.state {
573-
NakamotoTenureDownloadState::GetTenureStartBlock(_block_id) => {
596+
NakamotoTenureDownloadState::GetTenureStartBlock(block_id, start_request_time) => {
574597
debug!(
575-
"Got download response for tenure-start block {}",
576-
&_block_id
598+
"Got download response for tenure-start block {} in {}ms",
599+
&block_id,
600+
get_epoch_time_ms().saturating_sub(start_request_time)
577601
);
578602
let block = response.decode_nakamoto_block().map_err(|e| {
579603
warn!("Failed to decode response for a Nakamoto block: {:?}", &e);
@@ -582,19 +606,24 @@ impl NakamotoTenureDownloader {
582606
self.try_accept_tenure_start_block(block)?;
583607
Ok(None)
584608
}
585-
NakamotoTenureDownloadState::GetTenureEndBlock(_block_id) => {
586-
debug!("Got download response to tenure-end block {}", &_block_id);
609+
NakamotoTenureDownloadState::GetTenureEndBlock(block_id, start_request_time) => {
610+
debug!(
611+
"Got download response to tenure-end block {} in {}ms",
612+
&block_id,
613+
get_epoch_time_ms().saturating_sub(start_request_time)
614+
);
587615
let block = response.decode_nakamoto_block().map_err(|e| {
588616
warn!("Failed to decode response for a Nakamoto block: {:?}", &e);
589617
e
590618
})?;
591619
self.try_accept_tenure_end_block(&block)?;
592620
Ok(None)
593621
}
594-
NakamotoTenureDownloadState::GetTenureBlocks(_end_block_id) => {
622+
NakamotoTenureDownloadState::GetTenureBlocks(end_block_id, start_request_time) => {
595623
debug!(
596-
"Got download response for tenure blocks ending at {}",
597-
&_end_block_id
624+
"Got download response for tenure blocks ending at {} in {}ms",
625+
&end_block_id,
626+
get_epoch_time_ms().saturating_sub(start_request_time)
598627
);
599628
let blocks = response.decode_nakamoto_tenure().map_err(|e| {
600629
warn!("Failed to decode response for a Nakamoto tenure: {:?}", &e);

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

Lines changed: 12 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,10 @@ impl NakamotoTenureDownloaderSet {
463461
continue;
464462
};
465463
if downloader.is_done() {
466-
debug!("Downloader for {} is done", &naddr);
464+
debug!(
465+
"Downloader for {} on tenure {} is finished",
466+
&naddr, &downloader.tenure_id_consensus_hash
467+
);
467468
finished.push(naddr.clone());
468469
finished_tenures.push(downloader.tenure_id_consensus_hash.clone());
469470
continue;
@@ -534,6 +535,10 @@ impl NakamotoTenureDownloaderSet {
534535
);
535536
new_blocks.insert(downloader.tenure_id_consensus_hash.clone(), blocks);
536537
if downloader.is_done() {
538+
debug!(
539+
"Downloader for {} on tenure {} is finished",
540+
&naddr, &downloader.tenure_id_consensus_hash
541+
);
537542
finished.push(naddr.clone());
538543
finished_tenures.push(downloader.tenure_id_consensus_hash.clone());
539544
continue;

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ use crate::net::{Error as NetError, Hash160, NeighborAddress, SortitionDB};
5050
use crate::stacks_common::types::Address;
5151
use crate::util_lib::db::Error as DBError;
5252

53+
impl NakamotoTenureDownloadState {
54+
pub fn request_time(&self) -> Option<u128> {
55+
match self {
56+
Self::GetTenureStartBlock(_, ts) => Some(*ts),
57+
Self::GetTenureEndBlock(_, ts) => Some(*ts),
58+
Self::GetTenureBlocks(_, ts) => Some(*ts),
59+
Self::Done => None,
60+
}
61+
}
62+
}
63+
5364
impl NakamotoDownloadStateMachine {
5465
/// Find the list of wanted tenures for the given reward cycle. The reward cycle must
5566
/// be complete already. Used for testing.
@@ -240,7 +251,10 @@ fn test_nakamoto_tenure_downloader() {
240251
// must be first block
241252
assert_eq!(
242253
td.state,
243-
NakamotoTenureDownloadState::GetTenureStartBlock(tenure_start_block.header.block_id())
254+
NakamotoTenureDownloadState::GetTenureStartBlock(
255+
tenure_start_block.header.block_id(),
256+
td.state.request_time().unwrap()
257+
)
244258
);
245259
assert!(td
246260
.try_accept_tenure_start_block(blocks.last().unwrap().clone())
@@ -254,7 +268,7 @@ fn test_nakamoto_tenure_downloader() {
254268
.try_accept_tenure_start_block(blocks.first().unwrap().clone())
255269
.is_ok());
256270

257-
let NakamotoTenureDownloadState::GetTenureEndBlock(block_id) = td.state else {
271+
let NakamotoTenureDownloadState::GetTenureEndBlock(block_id, ..) = td.state else {
258272
panic!("wrong state");
259273
};
260274
assert_eq!(block_id, next_tenure_start_block.header.block_id());
@@ -274,7 +288,8 @@ fn test_nakamoto_tenure_downloader() {
274288
assert_eq!(
275289
td.state,
276290
NakamotoTenureDownloadState::GetTenureBlocks(
277-
next_tenure_start_block.header.parent_block_id.clone()
291+
next_tenure_start_block.header.parent_block_id.clone(),
292+
td.state.request_time().unwrap(),
278293
)
279294
);
280295
assert_eq!(td.tenure_end_block, Some(next_tenure_start_block.clone()));
@@ -300,7 +315,10 @@ fn test_nakamoto_tenure_downloader() {
300315
// tail pointer moved
301316
assert_eq!(
302317
td.state,
303-
NakamotoTenureDownloadState::GetTenureBlocks(block.header.parent_block_id.clone())
318+
NakamotoTenureDownloadState::GetTenureBlocks(
319+
block.header.parent_block_id.clone(),
320+
td.state.request_time().unwrap()
321+
)
304322
);
305323
}
306324

@@ -572,7 +590,8 @@ fn test_nakamoto_unconfirmed_tenure_downloader() {
572590
assert_eq!(
573591
ntd.state,
574592
NakamotoTenureDownloadState::GetTenureStartBlock(
575-
unconfirmed_wanted_tenure.winning_block_id.clone()
593+
unconfirmed_wanted_tenure.winning_block_id.clone(),
594+
ntd.state.request_time().unwrap()
576595
)
577596
);
578597
}
@@ -670,7 +689,8 @@ fn test_nakamoto_unconfirmed_tenure_downloader() {
670689
assert_eq!(
671690
ntd.state,
672691
NakamotoTenureDownloadState::GetTenureStartBlock(
673-
unconfirmed_wanted_tenure.winning_block_id.clone()
692+
unconfirmed_wanted_tenure.winning_block_id.clone(),
693+
ntd.state.request_time().unwrap()
674694
)
675695
);
676696
}
@@ -770,7 +790,8 @@ fn test_nakamoto_unconfirmed_tenure_downloader() {
770790
assert_eq!(
771791
ntd.state,
772792
NakamotoTenureDownloadState::GetTenureStartBlock(
773-
unconfirmed_wanted_tenure.winning_block_id.clone()
793+
unconfirmed_wanted_tenure.winning_block_id.clone(),
794+
ntd.state.request_time().unwrap()
774795
)
775796
);
776797
}
@@ -847,7 +868,8 @@ fn test_nakamoto_unconfirmed_tenure_downloader() {
847868
assert_eq!(
848869
ntd.state,
849870
NakamotoTenureDownloadState::GetTenureStartBlock(
850-
unconfirmed_wanted_tenure.winning_block_id.clone()
871+
unconfirmed_wanted_tenure.winning_block_id.clone(),
872+
ntd.state.request_time().unwrap()
851873
)
852874
);
853875
}
@@ -987,7 +1009,8 @@ fn test_nakamoto_unconfirmed_tenure_downloader() {
9871009
assert_eq!(
9881010
ntd.state,
9891011
NakamotoTenureDownloadState::GetTenureStartBlock(
990-
unconfirmed_wanted_tenure.winning_block_id.clone()
1012+
unconfirmed_wanted_tenure.winning_block_id.clone(),
1013+
ntd.state.request_time().unwrap()
9911014
)
9921015
);
9931016
}

0 commit comments

Comments
 (0)