Skip to content

Commit f35213e

Browse files
authored
Sync active request byrange ids logs (#6914)
- Re-opened PR from #6869 Writing and running tests I noted that the sync RPC requests are very verbose now. `DataColumnsByRootRequestId { id: 123, requester: Custody(CustodyId { requester: CustodyRequester(SingleLookupReqId { req_id: 121, lookup_id: 101 }) }) }` Since this Id is logged rather often I believe there's value in 1. Making them more succinct for log verbosity 2. Make them a string that's easy to copy and work with elastic Write custom `Display` implementations to render Ids in a more DX format _ DataColumnsByRootRequestId with a block lookup_ ``` 123/Custody/121/Lookup/101 ``` _DataColumnsByRangeRequestId_ ``` 123/122/RangeSync/0/5492900659401505034 ``` - This one will be shorter after #6868 Also made the logs format and text consistent across all methods
1 parent afdda83 commit f35213e

File tree

2 files changed

+224
-98
lines changed

2 files changed

+224
-98
lines changed

beacon_node/lighthouse_network/src/service/api_types.rs

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use std::sync::Arc;
2-
1+
use crate::rpc::{
2+
methods::{ResponseTermination, RpcResponse, RpcSuccessResponse, StatusMessage},
3+
SubstreamId,
4+
};
35
use libp2p::swarm::ConnectionId;
6+
use std::fmt::{Display, Formatter};
7+
use std::sync::Arc;
48
use types::{
59
BlobSidecar, DataColumnSidecar, Epoch, EthSpec, Hash256, LightClientBootstrap,
610
LightClientFinalityUpdate, LightClientOptimisticUpdate, LightClientUpdate, SignedBeaconBlock,
711
};
812

9-
use crate::rpc::{
10-
methods::{ResponseTermination, RpcResponse, RpcSuccessResponse, StatusMessage},
11-
SubstreamId,
12-
};
13-
1413
/// Identifier of requests sent by a peer.
1514
pub type PeerRequestId = (ConnectionId, SubstreamId);
1615

@@ -235,9 +234,108 @@ impl slog::Value for RequestId {
235234
}
236235
}
237236

238-
// This custom impl reduces log boilerplate not printing `DataColumnsByRootRequestId` on each id log
239-
impl std::fmt::Display for DataColumnsByRootRequestId {
240-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
241-
write!(f, "{} {:?}", self.id, self.requester)
237+
macro_rules! impl_display {
238+
($structname: ty, $format: literal, $($field:ident),*) => {
239+
impl Display for $structname {
240+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
241+
write!(f, $format, $(self.$field,)*)
242+
}
243+
}
244+
};
245+
}
246+
247+
// Since each request Id is deeply nested with various types, if rendered with Debug on logs they
248+
// take too much visual space. This custom Display implementations make the overall Id short while
249+
// not losing information
250+
impl_display!(BlocksByRangeRequestId, "{}/{}", id, parent_request_id);
251+
impl_display!(BlobsByRangeRequestId, "{}/{}", id, parent_request_id);
252+
impl_display!(DataColumnsByRangeRequestId, "{}/{}", id, parent_request_id);
253+
impl_display!(ComponentsByRangeRequestId, "{}/{}", id, requester);
254+
impl_display!(DataColumnsByRootRequestId, "{}/{}", id, requester);
255+
impl_display!(SingleLookupReqId, "{}/Lookup/{}", req_id, lookup_id);
256+
impl_display!(CustodyId, "{}", requester);
257+
impl_display!(SamplingId, "{}/{}", sampling_request_id, id);
258+
259+
impl Display for DataColumnsByRootRequester {
260+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
261+
match self {
262+
Self::Custody(id) => write!(f, "Custody/{id}"),
263+
Self::Sampling(id) => write!(f, "Sampling/{id}"),
264+
}
265+
}
266+
}
267+
268+
impl Display for CustodyRequester {
269+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
270+
write!(f, "{}", self.0)
271+
}
272+
}
273+
274+
impl Display for RangeRequestId {
275+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
276+
match self {
277+
Self::RangeSync { chain_id, batch_id } => write!(f, "RangeSync/{batch_id}/{chain_id}"),
278+
Self::BackfillSync { batch_id } => write!(f, "BackfillSync/{batch_id}"),
279+
}
280+
}
281+
}
282+
283+
impl Display for SamplingRequestId {
284+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
285+
write!(f, "{}", self.0)
286+
}
287+
}
288+
289+
impl Display for SamplingRequester {
290+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
291+
match self {
292+
Self::ImportedBlock(block) => write!(f, "ImportedBlock/{block}"),
293+
}
294+
}
295+
}
296+
297+
#[cfg(test)]
298+
mod tests {
299+
use super::*;
300+
301+
#[test]
302+
fn display_id_data_columns_by_root_custody() {
303+
let id = DataColumnsByRootRequestId {
304+
id: 123,
305+
requester: DataColumnsByRootRequester::Custody(CustodyId {
306+
requester: CustodyRequester(SingleLookupReqId {
307+
req_id: 121,
308+
lookup_id: 101,
309+
}),
310+
}),
311+
};
312+
assert_eq!(format!("{id}"), "123/Custody/121/Lookup/101");
313+
}
314+
315+
#[test]
316+
fn display_id_data_columns_by_root_sampling() {
317+
let id = DataColumnsByRootRequestId {
318+
id: 123,
319+
requester: DataColumnsByRootRequester::Sampling(SamplingId {
320+
id: SamplingRequester::ImportedBlock(Hash256::ZERO),
321+
sampling_request_id: SamplingRequestId(101),
322+
}),
323+
};
324+
assert_eq!(format!("{id}"), "123/Sampling/101/ImportedBlock/0x0000000000000000000000000000000000000000000000000000000000000000");
325+
}
326+
327+
#[test]
328+
fn display_id_data_columns_by_range() {
329+
let id = DataColumnsByRangeRequestId {
330+
id: 123,
331+
parent_request_id: ComponentsByRangeRequestId {
332+
id: 122,
333+
requester: RangeRequestId::RangeSync {
334+
chain_id: 54,
335+
batch_id: Epoch::new(0),
336+
},
337+
},
338+
};
339+
assert_eq!(format!("{id}"), "123/122/RangeSync/0/54");
242340
}
243341
}

0 commit comments

Comments
 (0)