|
1 |
| -use std::sync::Arc; |
2 |
| - |
| 1 | +use crate::rpc::{ |
| 2 | + methods::{ResponseTermination, RpcResponse, RpcSuccessResponse, StatusMessage}, |
| 3 | + SubstreamId, |
| 4 | +}; |
3 | 5 | use libp2p::swarm::ConnectionId;
|
| 6 | +use std::fmt::{Display, Formatter}; |
| 7 | +use std::sync::Arc; |
4 | 8 | use types::{
|
5 | 9 | BlobSidecar, DataColumnSidecar, Epoch, EthSpec, Hash256, LightClientBootstrap,
|
6 | 10 | LightClientFinalityUpdate, LightClientOptimisticUpdate, LightClientUpdate, SignedBeaconBlock,
|
7 | 11 | };
|
8 | 12 |
|
9 |
| -use crate::rpc::{ |
10 |
| - methods::{ResponseTermination, RpcResponse, RpcSuccessResponse, StatusMessage}, |
11 |
| - SubstreamId, |
12 |
| -}; |
13 |
| - |
14 | 13 | /// Identifier of requests sent by a peer.
|
15 | 14 | pub type PeerRequestId = (ConnectionId, SubstreamId);
|
16 | 15 |
|
@@ -235,9 +234,108 @@ impl slog::Value for RequestId {
|
235 | 234 | }
|
236 | 235 | }
|
237 | 236 |
|
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"); |
242 | 340 | }
|
243 | 341 | }
|
0 commit comments