@@ -4,12 +4,13 @@ use crate::sync::block_lookups::common::RequestState;
4
4
use crate :: sync:: block_lookups:: Id ;
5
5
use crate :: sync:: network_context:: { LookupRequestResult , ReqId , SyncNetworkContext } ;
6
6
use beacon_chain:: BeaconChainTypes ;
7
+ use derivative:: Derivative ;
7
8
use itertools:: Itertools ;
8
9
use rand:: seq:: IteratorRandom ;
9
10
use std:: collections:: HashSet ;
10
11
use std:: fmt:: Debug ;
11
12
use std:: sync:: Arc ;
12
- use std:: time:: Duration ;
13
+ use std:: time:: { Duration , Instant } ;
13
14
use store:: Hash256 ;
14
15
use strum:: IntoStaticStr ;
15
16
use types:: blob_sidecar:: FixedBlobSidecarList ;
@@ -53,12 +54,15 @@ pub enum LookupRequestError {
53
54
} ,
54
55
}
55
56
57
+ #[ derive( Derivative ) ]
58
+ #[ derivative( Debug ( bound = "T: BeaconChainTypes" ) ) ]
56
59
pub struct SingleBlockLookup < T : BeaconChainTypes > {
57
60
pub id : Id ,
58
61
pub block_request_state : BlockRequestState < T :: EthSpec > ,
59
62
pub blob_request_state : BlobRequestState < T :: EthSpec > ,
60
63
block_root : Hash256 ,
61
64
awaiting_parent : Option < Hash256 > ,
65
+ created : Instant ,
62
66
}
63
67
64
68
impl < T : BeaconChainTypes > SingleBlockLookup < T > {
@@ -74,6 +78,7 @@ impl<T: BeaconChainTypes> SingleBlockLookup<T> {
74
78
blob_request_state : BlobRequestState :: new ( requested_block_root, peers) ,
75
79
block_root : requested_block_root,
76
80
awaiting_parent,
81
+ created : Instant :: now ( ) ,
77
82
}
78
83
}
79
84
@@ -98,6 +103,11 @@ impl<T: BeaconChainTypes> SingleBlockLookup<T> {
98
103
self . awaiting_parent = None ;
99
104
}
100
105
106
+ /// Returns the time elapsed since this lookup was created
107
+ pub fn elapsed_since_created ( & self ) -> Duration {
108
+ self . created . elapsed ( )
109
+ }
110
+
101
111
/// Maybe insert a verified response into this lookup. Returns true if imported
102
112
pub fn add_child_components ( & mut self , block_component : BlockComponent < T :: EthSpec > ) -> bool {
103
113
match block_component {
@@ -244,7 +254,10 @@ impl<T: BeaconChainTypes> SingleBlockLookup<T> {
244
254
}
245
255
246
256
/// The state of the blob request component of a `SingleBlockLookup`.
257
+ #[ derive( Derivative ) ]
258
+ #[ derivative( Debug ) ]
247
259
pub struct BlobRequestState < E : EthSpec > {
260
+ #[ derivative( Debug = "ignore" ) ]
248
261
pub block_root : Hash256 ,
249
262
pub state : SingleLookupRequestState < FixedBlobSidecarList < E > > ,
250
263
}
@@ -259,7 +272,10 @@ impl<E: EthSpec> BlobRequestState<E> {
259
272
}
260
273
261
274
/// The state of the block request component of a `SingleBlockLookup`.
275
+ #[ derive( Derivative ) ]
276
+ #[ derivative( Debug ) ]
262
277
pub struct BlockRequestState < E : EthSpec > {
278
+ #[ derivative( Debug = "ignore" ) ]
263
279
pub requested_block_root : Hash256 ,
264
280
pub state : SingleLookupRequestState < Arc < SignedBeaconBlock < E > > > ,
265
281
}
@@ -281,7 +297,7 @@ pub struct DownloadResult<T: Clone> {
281
297
pub peer_id : PeerId ,
282
298
}
283
299
284
- #[ derive( Debug , PartialEq , Eq , IntoStaticStr ) ]
300
+ #[ derive( PartialEq , Eq , IntoStaticStr ) ]
285
301
pub enum State < T : Clone > {
286
302
AwaitingDownload ,
287
303
Downloading ( ReqId ) ,
@@ -293,13 +309,16 @@ pub enum State<T: Clone> {
293
309
}
294
310
295
311
/// Object representing the state of a single block or blob lookup request.
296
- #[ derive( PartialEq , Eq , Debug ) ]
312
+ #[ derive( PartialEq , Eq , Derivative ) ]
313
+ #[ derivative( Debug ) ]
297
314
pub struct SingleLookupRequestState < T : Clone > {
298
315
/// State of this request.
299
316
state : State < T > ,
300
317
/// Peers that should have this block or blob.
318
+ #[ derivative( Debug ( format_with = "fmt_peer_set" ) ) ]
301
319
available_peers : HashSet < PeerId > ,
302
320
/// Peers from which we have requested this block.
321
+ #[ derivative( Debug = "ignore" ) ]
303
322
used_peers : HashSet < PeerId > ,
304
323
/// How many times have we attempted to process this block or blob.
305
324
failed_processing : u8 ,
@@ -529,8 +548,30 @@ impl<T: Clone> SingleLookupRequestState<T> {
529
548
}
530
549
}
531
550
551
+ // Display is used in the BadState assertions above
532
552
impl < T : Clone > std:: fmt:: Display for State < T > {
533
553
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
534
554
write ! ( f, "{}" , Into :: <& ' static str >:: into( self ) )
535
555
}
536
556
}
557
+
558
+ // Debug is used in the log_stuck_lookups print to include some more info. Implements custom Debug
559
+ // to not dump an entire block or blob to terminal which don't add valuable data.
560
+ impl < T : Clone > std:: fmt:: Debug for State < T > {
561
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
562
+ match self {
563
+ Self :: AwaitingDownload { .. } => write ! ( f, "AwaitingDownload" ) ,
564
+ Self :: Downloading ( req_id) => write ! ( f, "Downloading({:?})" , req_id) ,
565
+ Self :: AwaitingProcess ( d) => write ! ( f, "AwaitingProcess({:?})" , d. peer_id) ,
566
+ Self :: Processing ( d) => write ! ( f, "Processing({:?})" , d. peer_id) ,
567
+ Self :: Processed { .. } => write ! ( f, "Processed" ) ,
568
+ }
569
+ }
570
+ }
571
+
572
+ fn fmt_peer_set (
573
+ peer_set : & HashSet < PeerId > ,
574
+ f : & mut std:: fmt:: Formatter ,
575
+ ) -> Result < ( ) , std:: fmt:: Error > {
576
+ write ! ( f, "{}" , peer_set. len( ) )
577
+ }
0 commit comments