Skip to content

ledger: Remove internal Merkle Shred enum - #14656

Merged
steviez merged 1 commit into
anza-xyz:masterfrom
steviez:rm_redundant_merkle_shred_enum
Aug 19, 2026
Merged

steviez merged 1 commit into
anza-xyz:masterfrom
steviez:rm_redundant_merkle_shred_enum

Conversation

@steviez

@steviez steviez commented Aug 16, 2026 •

Copy link
Copy Markdown

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

pub enum Shred {
ShredCode(ShredCode),
ShredData(ShredData),
}

Data Impl Enum:

pub enum ShredData {
Legacy(legacy::ShredData),
Merkle(merkle::ShredData),
}

Coding Impl Enum:

pub enum ShredCode {
Legacy(legacy::ShredCode),
Merkle(merkle::ShredCode),
}

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

pub enum Shred {
ShredCode(ShredCode),
ShredData(merkle::ShredData),
}

As do the Merkle shred implementation enum

pub(crate) enum Shred {
ShredCode(ShredCode),
ShredData(ShredData),
}

This second enum leads to some clunkiness like below where we will do a .into_iter().map(...).collect() on a Vec:

Ok(shreds.into_iter().map(Shred::from))

Summary of Changes

Remove the second enum that lives in merkle.rs and update all calling code accordingly. One notable item is that Shredder functions now return Vec<Shred> instead of Iterator<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 in standard_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

@steviez
steviez force-pushed the rm_redundant_merkle_shred_enum branch 2 times, most recently from 1dd3400 to f727b98 Compare August 17, 2026 03:20
@codecov-commenter

codecov-commenter commented Aug 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.05556% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.2%. Comparing base (2947eac) to head (0d3071e).
⚠️ Report is 1 commits behind head on master.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@steviez
steviez force-pushed the rm_redundant_merkle_shred_enum branch from f727b98 to 91adada Compare August 17, 2026 20:20
Comment thread ledger/src/blockstore.rs
Comment on lines +837 to 838
.into_iter()
.filter(Shred::is_data)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread ledger/src/shred.rs
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Shred {
ShredCode(ShredCode),
ShredCode(merkle::ShredCode),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, not necessary but makes it consistent with data below

Comment on lines +247 to +254
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);
});

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@steviez

steviez commented Aug 17, 2026

Copy link
Copy Markdown
Author

@claude review once

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread ledger/src/shred.rs Resolved
@steviez
steviez force-pushed the rm_redundant_merkle_shred_enum branch from f71ce67 to 0e91b50 Compare August 17, 2026 21:14
@steviez
steviez marked this pull request as ready for review August 17, 2026 21:58
@steviez
steviez requested review from a team as code owners August 17, 2026 21:58

@alexpyattaev alexpyattaev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment thread ledger/src/shred.rs Resolved

@steviez steviez left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna force push to squash the commits; reviews should not be cleared

Comment thread ledger/src/shred.rs Resolved
@steviez
steviez force-pushed the rm_redundant_merkle_shred_enum branch from 0e91b50 to 5b62bba Compare August 19, 2026 21:22
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
@steviez
steviez force-pushed the rm_redundant_merkle_shred_enum branch from 5b62bba to 0d3071e Compare August 19, 2026 21:29
@steviez

steviez commented Aug 19, 2026

Copy link
Copy Markdown
Author

Gonna force push to squash the commits; reviews should not be cleared

One more force push; the last one squashed but didn't rebase to tip. As a result, CI failed for h2 RUSTSEC

@steviez
steviez added this pull request to the merge queue Aug 19, 2026
Merged via the queue into anza-xyz:master with commit e6253ef Aug 19, 2026
46 checks passed
@steviez
steviez deleted the rm_redundant_merkle_shred_enum branch August 19, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants