@@ -66,16 +66,18 @@ use crate::util_lib::db::{DBConn, Error as DBError};
66
66
/// start and end block. This includes all tenures except for the two most recent ones.
67
67
#[ derive( Debug , Clone , PartialEq ) ]
68
68
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 ) ,
71
72
/// 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 ) ,
75
76
/// 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 ) ,
79
81
/// We have gotten all the blocks for this tenure
80
82
Done ,
81
83
}
@@ -166,7 +168,10 @@ impl NakamotoTenureDownloader {
166
168
start_signer_keys,
167
169
end_signer_keys,
168
170
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
+ ) ,
170
175
tenure_start_block : None ,
171
176
tenure_end_block : None ,
172
177
tenure_blocks : None ,
@@ -187,7 +192,7 @@ impl NakamotoTenureDownloader {
187
192
& mut self ,
188
193
tenure_start_block : NakamotoBlock ,
189
194
) -> Result < ( ) , NetError > {
190
- let NakamotoTenureDownloadState :: GetTenureStartBlock ( _ ) = & self . state else {
195
+ let NakamotoTenureDownloadState :: GetTenureStartBlock ( .. ) = & self . state else {
191
196
// not the right state for this
192
197
warn ! ( "Invalid state for this method" ;
193
198
"state" => %self . state) ;
@@ -234,8 +239,10 @@ impl NakamotoTenureDownloader {
234
239
self . try_accept_tenure_end_block ( & tenure_end_block) ?;
235
240
} else {
236
241
// 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
+ ) ;
239
246
}
240
247
Ok ( ( ) )
241
248
}
@@ -252,7 +259,7 @@ impl NakamotoTenureDownloader {
252
259
) -> Result < ( ) , NetError > {
253
260
if !matches ! (
254
261
& self . state,
255
- NakamotoTenureDownloadState :: GetTenureEndBlock ( _ )
262
+ NakamotoTenureDownloadState :: GetTenureEndBlock ( .. )
256
263
) {
257
264
warn ! ( "Invalid state for this method" ;
258
265
"state" => %self . state) ;
@@ -326,6 +333,7 @@ impl NakamotoTenureDownloader {
326
333
self . tenure_end_block = Some ( tenure_end_block. clone ( ) ) ;
327
334
self . state = NakamotoTenureDownloadState :: GetTenureBlocks (
328
335
tenure_end_block. header . parent_block_id . clone ( ) ,
336
+ get_epoch_time_ms ( ) ,
329
337
) ;
330
338
Ok ( ( ) )
331
339
}
@@ -361,7 +369,9 @@ impl NakamotoTenureDownloader {
361
369
& mut self ,
362
370
mut tenure_blocks : Vec < NakamotoBlock > ,
363
371
) -> 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 {
365
375
warn ! ( "Invalid state for this method" ;
366
376
"state" => %self . state) ;
367
377
return Err ( NetError :: InvalidState ) ;
@@ -473,7 +483,8 @@ impl NakamotoTenureDownloader {
473
483
& earliest_block. block_id( ) ,
474
484
& next_block_id
475
485
) ;
476
- self . state = NakamotoTenureDownloadState :: GetTenureBlocks ( next_block_id) ;
486
+ self . state =
487
+ NakamotoTenureDownloadState :: GetTenureBlocks ( next_block_id, * start_request_time) ;
477
488
return Ok ( None ) ;
478
489
}
479
490
@@ -498,16 +509,28 @@ impl NakamotoTenureDownloader {
498
509
peerhost : PeerHost ,
499
510
) -> Result < Option < StacksHttpRequest > , ( ) > {
500
511
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
+ ) ;
503
520
StacksHttpRequest :: new_get_nakamoto_block ( peerhost, start_block_id. clone ( ) )
504
521
}
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
+ ) ;
507
527
StacksHttpRequest :: new_get_nakamoto_block ( peerhost, end_block_id. clone ( ) )
508
528
}
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
+ ) ;
511
534
StacksHttpRequest :: new_get_nakamoto_tenure ( peerhost, end_block_id. clone ( ) , None )
512
535
}
513
536
NakamotoTenureDownloadState :: Done => {
@@ -570,10 +593,11 @@ impl NakamotoTenureDownloader {
570
593
response : StacksHttpResponse ,
571
594
) -> Result < Option < Vec < NakamotoBlock > > , NetError > {
572
595
let handle_result = match self . state {
573
- NakamotoTenureDownloadState :: GetTenureStartBlock ( _block_id ) => {
596
+ NakamotoTenureDownloadState :: GetTenureStartBlock ( block_id , start_request_time ) => {
574
597
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)
577
601
) ;
578
602
let block = response. decode_nakamoto_block ( ) . map_err ( |e| {
579
603
warn ! ( "Failed to decode response for a Nakamoto block: {:?}" , & e) ;
@@ -582,19 +606,24 @@ impl NakamotoTenureDownloader {
582
606
self . try_accept_tenure_start_block ( block) ?;
583
607
Ok ( None )
584
608
}
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
+ ) ;
587
615
let block = response. decode_nakamoto_block ( ) . map_err ( |e| {
588
616
warn ! ( "Failed to decode response for a Nakamoto block: {:?}" , & e) ;
589
617
e
590
618
} ) ?;
591
619
self . try_accept_tenure_end_block ( & block) ?;
592
620
Ok ( None )
593
621
}
594
- NakamotoTenureDownloadState :: GetTenureBlocks ( _end_block_id ) => {
622
+ NakamotoTenureDownloadState :: GetTenureBlocks ( end_block_id , start_request_time ) => {
595
623
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)
598
627
) ;
599
628
let blocks = response. decode_nakamoto_tenure ( ) . map_err ( |e| {
600
629
warn ! ( "Failed to decode response for a Nakamoto tenure: {:?}" , & e) ;
0 commit comments