Skip to content

Commit cbc378a

Browse files
committed
De-duplicate code
1 parent 4583116 commit cbc378a

File tree

2 files changed

+26
-55
lines changed
  • beacon_node

2 files changed

+26
-55
lines changed

beacon_node/lighthouse_network/src/service/api_types.rs

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -234,45 +234,27 @@ impl slog::Value for RequestId {
234234
}
235235
}
236236

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+
237247
// Since each request Id is deeply nested with various types, if rendered with Debug on logs they
238248
// take too much visual space. This custom Display implementations make the overall Id short while
239249
// not losing information
240-
impl Display for BlocksByRangeRequestId {
241-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
242-
write!(f, "{}/{}", self.id, self.parent_request_id)
243-
}
244-
}
245-
246-
impl Display for BlobsByRangeRequestId {
247-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
248-
write!(f, "{}/{}", self.id, self.parent_request_id)
249-
}
250-
}
251-
252-
impl Display for DataColumnsByRangeRequestId {
253-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
254-
write!(f, "{}/{}", self.id, self.parent_request_id)
255-
}
256-
}
257-
258-
impl Display for ComponentsByRangeRequestId {
259-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
260-
write!(f, "{}/{}", self.id, self.requester)
261-
}
262-
}
263-
264-
impl Display for SingleLookupReqId {
265-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
266-
write!(f, "{}/Lookup/{}", self.req_id, self.lookup_id)
267-
}
268-
}
269-
270-
// This custom impl reduces log boilerplate not printing `DataColumnsByRootRequestId` on each id log
271-
impl Display for DataColumnsByRootRequestId {
272-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
273-
write!(f, "{}/{}", self.id, self.requester)
274-
}
275-
}
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);
276258

277259
impl Display for DataColumnsByRootRequester {
278260
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
@@ -283,12 +265,6 @@ impl Display for DataColumnsByRootRequester {
283265
}
284266
}
285267

286-
impl Display for CustodyId {
287-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
288-
write!(f, "{}", self.requester)
289-
}
290-
}
291-
292268
impl Display for CustodyRequester {
293269
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
294270
write!(f, "{}", self.0)
@@ -304,12 +280,6 @@ impl Display for RangeRequestId {
304280
}
305281
}
306282

307-
impl Display for SamplingId {
308-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
309-
write!(f, "{}/{}", self.sampling_request_id, self.id)
310-
}
311-
}
312-
313283
impl Display for SamplingRequestId {
314284
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
315285
write!(f, "{}", self.0)
@@ -361,11 +331,11 @@ mod tests {
361331
parent_request_id: ComponentsByRangeRequestId {
362332
id: 122,
363333
requester: RangeRequestId::RangeSync {
364-
chain_id: 5492900659401505034,
334+
chain_id: 54,
365335
batch_id: Epoch::new(0),
366336
},
367337
},
368338
};
369-
assert_eq!(format!("{id}"), "123/122/RangeSync/0/5492900659401505034");
339+
assert_eq!(format!("{id}"), "123/122/RangeSync/0/54");
370340
}
371341
}

beacon_node/network/src/network_beacon_processor/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,13 +612,13 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
612612
process_id: ChainSegmentProcessId,
613613
blocks: Vec<RpcBlock<T::EthSpec>>,
614614
) -> Result<(), Error<T::EthSpec>> {
615+
let is_backfill = matches!(&process_id, ChainSegmentProcessId::BackSyncBatchId { .. });
615616
debug!(self.log, "Batch sending for process";
616617
"blocks" => blocks.len(),
617618
"id" => ?process_id,
618619
);
619620

620621
let processor = self.clone();
621-
let id = process_id.clone();
622622
let process_fn = async move {
623623
let notify_execution_layer = if processor
624624
.network_globals
@@ -631,16 +631,17 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
631631
NotifyExecutionLayer::Yes
632632
};
633633
processor
634-
.process_chain_segment(id, blocks, notify_execution_layer)
634+
.process_chain_segment(process_id, blocks, notify_execution_layer)
635635
.await;
636636
};
637637
let process_fn = Box::pin(process_fn);
638638

639639
// Back-sync batches are dispatched with a different `Work` variant so
640640
// they can be rate-limited.
641-
let work = match process_id {
642-
ChainSegmentProcessId::RangeBatchId { .. } => Work::ChainSegment(process_fn),
643-
ChainSegmentProcessId::BackSyncBatchId { .. } => Work::ChainSegmentBackfill(process_fn),
641+
let work = if is_backfill {
642+
Work::ChainSegmentBackfill(process_fn)
643+
} else {
644+
Work::ChainSegment(process_fn)
644645
};
645646

646647
self.try_send(BeaconWorkEvent {

0 commit comments

Comments
 (0)