Skip to content

Commit

Permalink
shred: Consolidate and document ShredFlags logic (#4653)
Browse files Browse the repository at this point in the history
Also add a unit test to guarantee reference tick saturation behavior
  • Loading branch information
steviez authored Jan 28, 2025
1 parent 4fe5092 commit 9ca57b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
20 changes: 20 additions & 0 deletions ledger/src/shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ bitflags! {
}
}

impl ShredFlags {
/// Creates a new ShredFlags from the given reference_tick
///
/// SHRED_TICK_REFERENCE_MASK is comprised of only six bits whereas the
/// reference_tick has 8 bits (u8). The reference_tick bits will saturate
/// in the event that reference_tick > SHRED_TICK_REFERENCE_MASK
pub(crate) fn from_reference_tick(reference_tick: u8) -> Self {
Self::from_bits_retain(Self::SHRED_TICK_REFERENCE_MASK.bits().min(reference_tick))
}
}

#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Expand Down Expand Up @@ -1480,6 +1491,15 @@ mod tests {
);
}

#[test]
fn test_shred_flags_reference_tick_saturates() {
const MAX_REFERENCE_TICK: u8 = ShredFlags::SHRED_TICK_REFERENCE_MASK.bits();
for tick in 0..=u8::MAX {
let flags = ShredFlags::from_reference_tick(tick);
assert_eq!(flags.bits(), tick.min(MAX_REFERENCE_TICK));
}
}

#[test]
fn test_version_from_hash() {
let hash = [
Expand Down
7 changes: 1 addition & 6 deletions ledger/src/shred/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,7 @@ impl ShredData {
fec_set_index,
};
let size = (data.len() + Self::SIZE_OF_HEADERS) as u16;
let flags = flags
| ShredFlags::from_bits_retain(
ShredFlags::SHRED_TICK_REFERENCE_MASK
.bits()
.min(reference_tick),
);
let flags = flags | ShredFlags::from_reference_tick(reference_tick);
let data_header = DataShredHeader {
parent_offset,
flags,
Expand Down
6 changes: 1 addition & 5 deletions ledger/src/shred/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,11 +1113,7 @@ pub(super) fn make_shreds_from_data(
.checked_sub(parent_slot)
.and_then(|offset| u16::try_from(offset).ok())
.ok_or(Error::InvalidParentSlot { slot, parent_slot })?;
let flags = ShredFlags::from_bits_retain(
ShredFlags::SHRED_TICK_REFERENCE_MASK
.bits()
.min(reference_tick),
);
let flags = ShredFlags::from_reference_tick(reference_tick);
DataShredHeader {
parent_offset,
flags,
Expand Down

0 comments on commit 9ca57b1

Please sign in to comment.