Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Zero pad data shreds on fetch from blockstore (#17147)
Browse files Browse the repository at this point in the history
* Zero pad data shreds on fetch from blockstore

This is a partial backport of #16602 to allow compatibility with that change.

* Remove size check and resize shreds to consistent length
  • Loading branch information
steviez authored May 14, 2021
1 parent 3f90830 commit 9aacd0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
11 changes: 10 additions & 1 deletion ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,16 @@ impl Blockstore {
}

pub fn get_data_shred(&self, slot: Slot, index: u64) -> Result<Option<Vec<u8>>> {
self.data_shred_cf.get_bytes((slot, index))
use crate::shred::SHRED_PAYLOAD_SIZE;
self.data_shred_cf.get_bytes((slot, index)).map(|data| {
data.map(|mut d| {
// For forward compatibility, pad the payload out to
// SHRED_PAYLOAD_SIZE incase the shred was inserted
// with any padding stripped off.
d.resize(cmp::max(d.len(), SHRED_PAYLOAD_SIZE), 0);
d
})
})
}

pub fn get_data_shreds_for_slot(
Expand Down
16 changes: 7 additions & 9 deletions ledger/src/shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,18 @@ impl Shred {
}

pub fn new_from_serialized_shred(mut payload: Vec<u8>) -> Result<Self> {
// A shred can be deserialized in several cases; payload length will vary for these:
// payload.len() <= SHRED_PAYLOAD_SIZE when payload is retrieved from the blockstore
// payload.len() == SHRED_PAYLOAD_SIZE when payload is from a local shred (shred.payload)
// payload.len() > PACKET_DATA_SIZE when payload comes from a packet (window serivce)
// Resize here so the shreds always have the same length
payload.resize(SHRED_PAYLOAD_SIZE, 0);

let mut start = 0;
let common_header: ShredCommonHeader =
Self::deserialize_obj(&mut start, SIZE_OF_COMMON_SHRED_HEADER, &payload)?;

let slot = common_header.slot;
let expected_data_size = SHRED_PAYLOAD_SIZE;
// Safe because any payload from the network must have passed through
// window service, which implies payload wll be of size
// PACKET_DATA_SIZE, and `expected_data_size` <= PACKET_DATA_SIZE.
//
// On the other hand, if this function is called locally, the payload size should match
// the `expected_data_size`.
assert!(payload.len() >= expected_data_size);
payload.truncate(expected_data_size);
let shred = if common_header.shred_type == ShredType(CODING_SHRED) {
let coding_header: CodingShredHeader =
Self::deserialize_obj(&mut start, SIZE_OF_CODING_SHRED_HEADER, &payload)?;
Expand Down

0 comments on commit 9aacd0f

Please sign in to comment.