Skip to content

Commit 8fbd9b8

Browse files
committed
cleanup
1 parent 2751133 commit 8fbd9b8

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

ledger/src/blockstore.rs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,18 +4099,23 @@ impl Blockstore {
40994099
.collect()
41004100
}
41014101

4102-
/// Fetch the components corresponding to all of the shred indices in `completed_ranges`
4102+
/// Helper function to process shreds in `completed_ranges` and apply a transformation
4103+
/// to the resulting block components.
41034104
/// This function takes advantage of the fact that `completed_ranges` are both
41044105
/// contiguous and in sorted order. To clarify, suppose completed_ranges is as follows:
41054106
/// completed_ranges = [..., (s_i..e_i), (s_i+1..e_i+1), ...]
41064107
/// Then, the following statements are true:
41074108
/// s_i < e_i == s_i+1 < e_i+1
4108-
fn get_slot_components_in_block(
4109+
fn process_slot_data_in_block<T, F>(
41094110
&self,
41104111
slot: Slot,
41114112
completed_ranges: CompletedRanges,
41124113
slot_meta: Option<&SlotMeta>,
4113-
) -> Result<Vec<BlockComponent>> {
4114+
transform: F,
4115+
) -> Result<Vec<T>>
4116+
where
4117+
F: Fn(Vec<BlockComponent>) -> Result<Vec<T>>,
4118+
{
41144119
debug_assert!(completed_ranges
41154120
.iter()
41164121
.tuple_windows()
@@ -4143,7 +4148,7 @@ impl Blockstore {
41434148
completed_ranges
41444149
.into_iter()
41454150
.map(|Range { start, end }| end - start)
4146-
.map(move |num_shreds| {
4151+
.map(|num_shreds| {
41474152
shreds
41484153
.by_ref()
41494154
.take(num_shreds as usize)
@@ -4156,9 +4161,18 @@ impl Blockstore {
41564161
.and_then(|payload| {
41574162
// TODO(karthik): if Alpenglow flag is disabled, return an error on special
41584163
// EntryBatches.
4159-
BlockComponent::from_bytes_multiple(&payload).map_err(|e| {
4164+
let components = BlockComponent::from_bytes_multiple(&payload).map_err(
4165+
|e| {
4166+
BlockstoreError::InvalidShredData(Box::new(
4167+
bincode::ErrorKind::Custom(format!(
4168+
"could not reconstruct block components: {e:?}"
4169+
)),
4170+
))
4171+
},
4172+
)?;
4173+
transform(components).map_err(|e| {
41604174
BlockstoreError::InvalidShredData(Box::new(bincode::ErrorKind::Custom(
4161-
format!("could not reconstruct entries: {e:?}"),
4175+
format!("could not transform block components: {e:?}"),
41624176
)))
41634177
})
41644178
})
@@ -4167,6 +4181,21 @@ impl Blockstore {
41674181
.collect()
41684182
}
41694183

4184+
/// Fetch the components corresponding to all of the shred indices in `completed_ranges`
4185+
/// This function takes advantage of the fact that `completed_ranges` are both
4186+
/// contiguous and in sorted order. To clarify, suppose completed_ranges is as follows:
4187+
/// completed_ranges = [..., (s_i..e_i), (s_i+1..e_i+1), ...]
4188+
/// Then, the following statements are true:
4189+
/// s_i < e_i == s_i+1 < e_i+1
4190+
fn get_slot_components_in_block(
4191+
&self,
4192+
slot: Slot,
4193+
completed_ranges: CompletedRanges,
4194+
slot_meta: Option<&SlotMeta>,
4195+
) -> Result<Vec<BlockComponent>> {
4196+
self.process_slot_data_in_block(slot, completed_ranges, slot_meta, Ok)
4197+
}
4198+
41704199
/// Fetch the entries corresponding to all of the shred indices in `completed_ranges`
41714200
/// This function takes advantage of the fact that `completed_ranges` are both
41724201
/// contiguous and in sorted order. To clarify, suppose completed_ranges is as follows:
@@ -4179,12 +4208,13 @@ impl Blockstore {
41794208
completed_ranges: CompletedRanges,
41804209
slot_meta: Option<&SlotMeta>,
41814210
) -> Result<Vec<Entry>> {
4182-
Ok(self
4183-
.get_slot_components_in_block(slot, completed_ranges, slot_meta)?
4184-
.into_iter()
4185-
.filter_map(|c| c.as_entry_batch_owned())
4186-
.flatten()
4187-
.collect_vec())
4211+
self.process_slot_data_in_block(slot, completed_ranges, slot_meta, |cs| {
4212+
Ok(cs
4213+
.into_iter()
4214+
.filter_map(|bc| bc.as_entry_batch_owned())
4215+
.flatten()
4216+
.collect_vec())
4217+
})
41884218
}
41894219

41904220
pub fn get_entries_in_data_block(

0 commit comments

Comments
 (0)