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

Commit 98bb8cf

Browse files
committed
shred from_payload
1 parent a6b62a9 commit 98bb8cf

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

ledger/src/shred.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@
5050
//! payload can fit into one coding shred / packet.
5151
5252
pub(crate) use shred_data::ShredData;
53+
pub use {
54+
self::stats::{ProcessShredsStats, ShredFetchStats},
55+
crate::shredder::Shredder,
56+
};
5357
use {
54-
self::shred_code::ShredCode,
58+
self::{shred_code::ShredCode, traits::Shred as _},
5559
crate::blockstore::MAX_DATA_SHREDS_PER_SLOT,
5660
bitflags::bitflags,
5761
num_enum::{IntoPrimitive, TryFromPrimitive},
@@ -69,10 +73,6 @@ use {
6973
std::fmt::Debug,
7074
thiserror::Error,
7175
};
72-
pub use {
73-
self::stats::{ProcessShredsStats, ShredFetchStats},
74-
crate::shredder::Shredder,
75-
};
7676

7777
mod common;
7878
mod legacy;
@@ -335,9 +335,23 @@ impl Shred {
335335
}
336336

337337
pub fn new_from_serialized_shred(shred: Vec<u8>) -> Result<Self, Error> {
338-
Ok(match Self::shred_type_from_payload(&shred)? {
339-
ShredType::Code => Self::from(ShredCode::from_payload(shred)?),
340-
ShredType::Data => Self::from(ShredData::from_payload(shred)?),
338+
Ok(match Shred::shred_variant_from_payload(&shred)? {
339+
ShredVariant::LegacyCode => {
340+
let shred = legacy::ShredCode::from_payload(shred)?;
341+
Self::from(ShredCode::from(shred))
342+
}
343+
ShredVariant::LegacyData => {
344+
let shred = legacy::ShredData::from_payload(shred)?;
345+
Self::from(ShredData::from(shred))
346+
}
347+
ShredVariant::MerkleCode(_) => {
348+
let shred = merkle::ShredCode::from_payload(shred)?;
349+
Self::from(ShredCode::from(shred))
350+
}
351+
ShredVariant::MerkleData(_) => {
352+
let shred = merkle::ShredData::from_payload(shred)?;
353+
Self::from(ShredData::from(shred))
354+
}
341355
})
342356
}
343357

ledger/src/shred/merkle.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl Shred for ShredData {
113113
if payload.len() < Self::SIZE_OF_PAYLOAD {
114114
return Err(Error::InvalidPayloadSize(payload.len()));
115115
}
116+
payload.truncate(Self::SIZE_OF_PAYLOAD);
116117
let mut cursor = Cursor::new(&payload[..]);
117118
let common_header: ShredCommonHeader = deserialize_from_with_limit(&mut cursor)?;
118119
let proof_size = match common_header.shred_variant {
@@ -130,7 +131,6 @@ impl Shred for ShredData {
130131
.take(usize::from(proof_size))
131132
.collect::<Result<_, _>>()?;
132133
let merkle_branch = MerkleBranch { root, proof };
133-
payload.truncate(Self::SIZE_OF_PAYLOAD);
134134
let shred = Self {
135135
common_header,
136136
data_header,
@@ -157,8 +157,6 @@ impl Shred for ShredData {
157157
}
158158

159159
fn erasure_shard_as_slice(&self) -> Result<&[u8], Error> {
160-
// TODO: relax this check
161-
// TODO: add this to sanitize
162160
if self.payload.len() != Self::SIZE_OF_PAYLOAD {
163161
return Err(Error::InvalidPayloadSize(self.payload.len()));
164162
}

ledger/src/shred/shred_code.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ impl ShredCode {
5959
))
6060
}
6161

62-
pub(super) fn from_payload(shred: Vec<u8>) -> Result<Self, Error> {
63-
// TODO: need to inspect variant byte
64-
Ok(Self::from(legacy::ShredCode::from_payload(shred)?))
65-
}
66-
6762
pub(super) fn num_data_shreds(&self) -> u16 {
6863
self.coding_header().num_data_shreds
6964
}

ledger/src/shred/shred_data.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ impl ShredData {
5757
))
5858
}
5959

60-
pub(super) fn from_payload(shred: Vec<u8>) -> Result<Self, Error> {
61-
// TODO: need to inspect variant byte
62-
Ok(Self::from(legacy::ShredData::from_payload(shred)?))
63-
}
64-
6560
pub(super) fn last_in_slot(&self) -> bool {
6661
let flags = self.data_header().flags;
6762
flags.contains(ShredFlags::LAST_SHRED_IN_SLOT)
@@ -122,9 +117,7 @@ pub(super) fn erasure_shard_index<T: ShredDataTrait>(shred: &T) -> Option<usize>
122117
usize::try_from(index).ok()
123118
}
124119

125-
// TODO: do i need to verify shred-variant here?
126120
pub(super) fn sanitize<T: ShredDataTrait>(shred: &T) -> Result<(), Error> {
127-
// TODO: This is unnecessary.
128121
let common_header = shred.common_header();
129122
let data_header = shred.data_header();
130123
if shred.erasure_shard_index().is_none() {

0 commit comments

Comments
 (0)