Repository navigation
ledger: Remove internal Merkle Shred enum - #14656
Conversation
1dd3400 to
f727b98
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #14656 +/- ##
=========================================
- Coverage 82.3% 82.2% -0.1%
=========================================
Files 918 918
Lines 361257 361207 -50
=========================================
- Hits 297365 297238 -127
- Misses 63892 63969 +77 🚀 New features to boost your workflow:
|
f727b98 to
91adada
Compare
| .into_iter() | ||
| .filter(Shred::is_data) |
There was a problem hiding this comment.
This pattern of filtering for only data shreds is pretty common across various tests. I suspect many of the cases that do this don't actually required data shreds only. In any case, I'd like to save evaluation of that and/or creation of a Shredder test helper that does the filtering for follow-on
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub enum Shred { | ||
| ShredCode(ShredCode), | ||
| ShredCode(merkle::ShredCode), |
There was a problem hiding this comment.
Strictly speaking, not necessary but makes it consistent with data below
| shreds.iter().for_each(|shred| { | ||
| process_stats.record_shred(shred); | ||
| let next_index = match shred.shred_type() { | ||
| ShredType::Code => &mut self.next_code_index, | ||
| ShredType::Data => &mut self.next_shred_index, | ||
| }; | ||
| *next_index = (*next_index).max(shred.index() + 1); | ||
| }); |
There was a problem hiding this comment.
Follow-on work but I think it would be possible to do both of these without having to take another iteration over all the shreds. Ie, do the work in Shredder where we know the values without having to reiterate.
Future work regardless
| *dst = crate::shred::Shred::from(src); | ||
| } | ||
| Ok(chained_merkle_root) | ||
| finish_erasure_batch(keypair, shreds, chained_merkle_root, reed_solomon_cache) |
There was a problem hiding this comment.
As follow-on, this function seemingly doesn't need to exist any longer. Rather, we can just adjust the visibility of finish_erasure_batch() based on cfg(test)
|
@claude review once |
There was a problem hiding this comment.
I reviewed this PR and only found a minor nit (duplicated match logic between from_payload and new_from_serialized_shred), noted inline. Because this is a large refactor consolidating the internal Merkle Shred enum into the top-level one and changing Shredder methods to return Vec<Shred> across the shred/shredder critical path (ledger, turbine, core), a human look is still worthwhile.
Extended reasoning...
Overview
This PR removes the internal merkle::Shred enum, folding its methods into the top-level shred::Shred enum, and changes Shredder's shred-generation methods to return Vec<Shred> instead of an iterator (since the from/into conversion between the two enums is no longer needed). It touches 19 files: the core shred/shredder/merkle logic in ledger/src/shred*, plus call-site updates in core, ledger-tool, and turbine to adapt to the Vec return type (mostly removing .collect()/adding .into_iter()).
Security risks
No new security-relevant logic is introduced — this is a structural refactor of existing shred construction/validation/recovery code. The Merkle proof, signature, and erasure-recovery logic itself is unchanged; methods are relocated rather than rewritten. The main risk category for this kind of change is behavioral regression from the enum consolidation (e.g. mismatched dispatch arms), not a new vulnerability class.
Level of scrutiny
Shred handling is on the validator's consensus-critical path (turbine propagation, erasure recovery, blockstore ingestion), so this warrants more than a cursory look despite being a mechanical-looking refactor. The diff is large and cuts across module boundaries (merkle.rs, shred.rs, shredder.rs, filter.rs, wire.rs) in ways that are easy to visually verify function-by-function but harder to fully verify for behavioral equivalence at a glance.
Other factors
The PR author (steviez) is already actively reviewing their own PR with several follow-on comments about future cleanup, and CI/codecov show passing patch coverage (92.75%) with no regressions. The only finding from this pass is a minor duplication nit, not a functional bug. Given the size and criticality of the refactor, plus the fact that steviez explicitly asked for this automated review as a supplement (not a replacement) to human review, deferring is the appropriate call here.
f71ce67 to
0e91b50
Compare
steviez
left a comment
There was a problem hiding this comment.
Gonna force push to squash the commits; reviews should not be cleared
0e91b50 to
5b62bba
Compare
When Merkle shreds were first added to the codebase, support was added for both the existing (Legacy) and new Merkle shreds. Dual support included a Shred enum with 4 types (Legacy|Merkle Data|Coding), and the Merkle shred implementation has an enum for data and coding shreds. Once Legacy shreds were cleaned up, these two separate enums had the same cases and we had extra code to convert back and forth. This PR removes the underlying enum and all conversion between them
5b62bba to
0d3071e
Compare
One more force push; the last one squashed but didn't rebase to tip. As a result, CI failed for h2 RUSTSEC |
Problem
When Merkle shreds were added to the codebase, support was added alongside the existing (Legacy) shreds. In order to support both, the different variants were covered with enum's:
The "Public" Enum:
agave/ledger/src/shred.rs
Lines 237 to 240 in 3dccb3e
Data Impl Enum:
agave/ledger/src/shred/shred_data.rs
Lines 14 to 17 in 3dccb3e
Coding Impl Enum:
agave/ledger/src/shred/shred_code.rs
Lines 19 to 22 in 3dccb3e
Merkle shreds have since become the only supported option and the legacy variant support was removed. The "top level" enum contains merkle shred types as in the inner types:
agave/ledger/src/shred.rs
Lines 299 to 302 in 12b5c7e
As do the Merkle shred implementation enum
agave/ledger/src/shred/merkle.rs
Lines 71 to 74 in 12b5c7e
This second enum leads to some clunkiness like below where we will do a
.into_iter().map(...).collect()on aVec:agave/ledger/src/shredder.rs
Line 141 in 12b5c7e
Summary of Changes
Remove the second enum that lives in
merkle.rsand update all calling code accordingly. One notable item is thatShredderfunctions now returnVec<Shred>instead ofIterator<Shred>since they don't have to convert from the "internal" to "public" enum types.And FWIW, the
.into_iter().map(...).collect()I referenced above occurs instandard_broadcast_run.rs. On the surface, this looks like an extra allocation but I remembered towards the end of this cleanup that Rust might/should be able to reuse the allocation in this instance via https://doc.rust-lang.org/nightly/src/alloc/vec/in_place_collect.rs.html. So, we may not have any perf wins but I think this is still progress towards making the codebase match current reality (Merkle shreds only) vs. the old (Legacy + Merkle)Testing
CI