Skip to content

Commit b6feac1

Browse files
authored
shredder: allow creating shreds with raw bytes (#7172)
allow creating shreds with raw bytes
1 parent bff4df9 commit b6feac1

File tree

4 files changed

+61
-62
lines changed

4 files changed

+61
-62
lines changed

core/src/replay_stage.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,7 +4341,7 @@ pub(crate) mod tests {
43414341
create_new_tmp_ledger,
43424342
genesis_utils::{create_genesis_config, create_genesis_config_with_leader},
43434343
get_tmp_ledger_path, get_tmp_ledger_path_auto_delete,
4344-
shred::{Shred, ShredFlags, LEGACY_SHRED_DATA_CAPACITY},
4344+
shred::{ProcessShredsStats, ReedSolomonCache, Shred, Shredder},
43454345
},
43464346
solana_poh_config::PohConfig,
43474347
solana_rpc::{
@@ -4973,19 +4973,25 @@ pub(crate) mod tests {
49734973
fn test_dead_fork_entry_deserialize_failure() {
49744974
// Insert entry that causes deserialization failure
49754975
let res = check_dead_fork(|_, bank| {
4976-
let gibberish = [0xa5u8; LEGACY_SHRED_DATA_CAPACITY];
4977-
let parent_offset = bank.slot() - bank.parent_slot();
4978-
let shred = Shred::new_from_data(
4979-
bank.slot(),
4980-
0, // index,
4981-
parent_offset as u16,
4982-
&gibberish,
4983-
ShredFlags::DATA_COMPLETE_SHRED,
4984-
0, // reference_tick
4985-
0, // version
4986-
0, // fec_set_index
4987-
);
4988-
vec![shred]
4976+
let gibberish = [0xa5u8; 1024];
4977+
4978+
let shredder = Shredder::new(bank.slot(), bank.parent_slot(), 0, 0).unwrap();
4979+
let keypair = Keypair::new();
4980+
let reed_solomon_cache = ReedSolomonCache::default();
4981+
4982+
shredder
4983+
.make_shreds_from_data_slice(
4984+
&keypair,
4985+
&gibberish,
4986+
true,
4987+
Some(Hash::default()),
4988+
0,
4989+
0,
4990+
&reed_solomon_cache,
4991+
&mut ProcessShredsStats::default(),
4992+
)
4993+
.unwrap()
4994+
.collect()
49894995
});
49904996

49914997
assert_matches!(

ledger/src/shred.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ use {
7171
assert_matches::debug_assert_matches,
7272
bitflags::bitflags,
7373
num_enum::{IntoPrimitive, TryFromPrimitive},
74-
rayon::ThreadPool,
7574
serde::{Deserialize, Serialize},
7675
solana_clock::Slot,
7776
solana_entry::entry::{create_ticks, Entry},
@@ -83,13 +82,13 @@ use {
8382
solana_signature::{Signature, SIGNATURE_BYTES},
8483
solana_signer::Signer,
8584
static_assertions::const_assert_eq,
86-
std::{fmt::Debug, time::Instant},
85+
std::fmt::Debug,
8786
thiserror::Error,
8887
};
8988

9089
mod common;
9190
mod legacy;
92-
mod merkle;
91+
pub(crate) mod merkle;
9392
mod merkle_tree;
9493
mod payload;
9594
pub mod shred_code;
@@ -835,43 +834,6 @@ pub fn recover(
835834
Ok(shreds.map(|shred| shred.map(Shred::from)))
836835
}
837836

838-
#[allow(clippy::too_many_arguments)]
839-
pub(crate) fn make_merkle_shreds_from_entries(
840-
thread_pool: &ThreadPool,
841-
keypair: &Keypair,
842-
entries: &[Entry],
843-
slot: Slot,
844-
parent_slot: Slot,
845-
shred_version: u16,
846-
reference_tick: u8,
847-
is_last_in_slot: bool,
848-
chained_merkle_root: Option<Hash>,
849-
next_shred_index: u32,
850-
next_code_index: u32,
851-
reed_solomon_cache: &ReedSolomonCache,
852-
stats: &mut ProcessShredsStats,
853-
) -> Result<impl Iterator<Item = Shred>, Error> {
854-
let now = Instant::now();
855-
let entries = bincode::serialize(entries)?;
856-
stats.serialize_elapsed += now.elapsed().as_micros() as u64;
857-
let shreds = merkle::make_shreds_from_data(
858-
thread_pool,
859-
keypair,
860-
chained_merkle_root,
861-
&entries[..],
862-
slot,
863-
parent_slot,
864-
shred_version,
865-
reference_tick,
866-
is_last_in_slot,
867-
next_shred_index,
868-
next_code_index,
869-
reed_solomon_cache,
870-
stats,
871-
)?;
872-
Ok(shreds.into_iter().map(Shred::from))
873-
}
874-
875837
// Accepts shreds in the slot range [root + 1, max_slot].
876838
#[must_use]
877839
pub fn should_discard_shred<'a, P>(

ledger/src/shred/merkle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct ShredCode {
7070
}
7171

7272
#[derive(Clone, Debug, Eq, PartialEq)]
73-
pub(super) enum Shred {
73+
pub(crate) enum Shred {
7474
ShredCode(ShredCode),
7575
ShredData(ShredData),
7676
}
@@ -1019,7 +1019,7 @@ fn make_shreds_code_header_only(
10191019
}
10201020

10211021
#[allow(clippy::too_many_arguments)]
1022-
pub(super) fn make_shreds_from_data(
1022+
pub(crate) fn make_shreds_from_data(
10231023
thread_pool: &ThreadPool,
10241024
keypair: &Keypair,
10251025
// The Merkle root of the previous erasure batch if chained.

ledger/src/shredder.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use {
1919
borrow::Borrow,
2020
fmt::Debug,
2121
sync::{Arc, OnceLock, RwLock},
22+
time::Instant,
2223
},
2324
};
2425

@@ -89,22 +90,52 @@ impl Shredder {
8990
reed_solomon_cache: &ReedSolomonCache,
9091
stats: &mut ProcessShredsStats,
9192
) -> impl Iterator<Item = Shred> {
92-
shred::make_merkle_shreds_from_entries(
93-
&PAR_THREAD_POOL,
93+
let now = Instant::now();
94+
let entries = bincode::serialize(entries).unwrap();
95+
stats.serialize_elapsed += now.elapsed().as_micros() as u64;
96+
Self::make_shreds_from_data_slice(
97+
self,
9498
keypair,
95-
entries,
99+
&entries,
100+
is_last_in_slot,
101+
chained_merkle_root,
102+
next_shred_index,
103+
next_code_index,
104+
reed_solomon_cache,
105+
stats,
106+
)
107+
.unwrap()
108+
}
109+
110+
#[allow(clippy::too_many_arguments)]
111+
pub fn make_shreds_from_data_slice(
112+
&self,
113+
keypair: &Keypair,
114+
data: &[u8],
115+
is_last_in_slot: bool,
116+
chained_merkle_root: Option<Hash>,
117+
next_shred_index: u32,
118+
next_code_index: u32,
119+
reed_solomon_cache: &ReedSolomonCache,
120+
stats: &mut ProcessShredsStats,
121+
) -> Result<impl Iterator<Item = Shred>, Error> {
122+
let thread_pool: &ThreadPool = &PAR_THREAD_POOL;
123+
let shreds = shred::merkle::make_shreds_from_data(
124+
thread_pool,
125+
keypair,
126+
chained_merkle_root,
127+
data,
96128
self.slot,
97129
self.parent_slot,
98130
self.version,
99131
self.reference_tick,
100132
is_last_in_slot,
101-
chained_merkle_root,
102133
next_shred_index,
103134
next_code_index,
104135
reed_solomon_cache,
105136
stats,
106-
)
107-
.unwrap()
137+
)?;
138+
Ok(shreds.into_iter().map(Shred::from))
108139
}
109140

110141
pub fn entries_to_merkle_shreds_for_tests(

0 commit comments

Comments
 (0)