You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR widens the merkle tree library (monad-merkle) to support trees with up to 16,384 leaves (previously limited to 256 by u8 leaf indices). It also includes significant refactoring of raptorcast group management structures (from earlier commits on the branch). The merkle changes are clean and well-tested, but there's a latent safety concern in the packet assembler worth discussing.
Merkle Library Changes (monad-merkle/src/lib.rs) — Looks Good
The core change is well-executed:
leaf_idx widened from u8 to u16 in MerkleProof::new_from_leaf_idx() and MerkleTree::proof() — allows indexing more than 256 leaves.
New constantsMAX_DEPTH = 15 and MAX_NUM_LEAVES = 16384 — clearly defines the library's limits. The depth fits in 4 bits (matching the raptorcast header format).
assert! → debug_assert! for tree length check (line 98) — this is fine since the preconditions are documented and checked upstream, but worth noting this removes a release-mode safety net.
Expanded test coverage — tests now cover 0..=257 leaves exhaustively plus larger values (300, 3000, 16384). The test refactoring into test_proof_with_leaves() is clean.
The comment says 16384 which is correct for depth 15, but the relationship "max leaves = 2^(depth-1)" could use a brief note explaining why it's depth-1 (because the tree has depth levels including the root).
Potential Issue: Wire Format / Assembler Mismatch
monad-raptorcast/src/packet/assembler.rs:296,306,416 — The chunk header format still allocates 1 byte for the merkle leaf index:
// Line 179-183: Wire formatpubconstCHUNK_HEADER_LEN:usize = 0
+ 20// Chunk recipient hash
+ 1// Chunk's merkle leaf idx ← still u8
+ 1// reserved
+ 2;// Chunk idx// Line 296: Writer accepts u8fn write_chunk_header(&mutself,layout:PacketLayout,merkle_leaf_index:u8) -> Result<()>{// Line 416: Cast to u8let leaf_index = leaf_index asu8;
Currently this is safe because MAX_MERKLE_TREE_DEPTH = 9 (line 84 in udp.rs), meaning max 256 leaves which fits in u8. However, the merkle library now advertises support for up to 16,384 leaves, creating a mismatch between what the library supports and what the wire format can handle.
Recommendation: Consider one of:
Adding a compile-time or runtime assertion that MAX_MERKLE_TREE_DEPTH <= 9 alongside a comment explaining the u8 constraint
Using u8::try_from(leaf_index).expect("leaf index must fit in chunk header") instead of as u8 to fail loudly if this invariant breaks
Or just a comment at the as u8 cast documenting this constraint
This isn't blocking since the current limits prevent overflow, but the silent as u8 truncation at line 416 is a footgun if someone increases MAX_MERKLE_TREE_DEPTH later.
Parser Changes — Correct
parser/legacy.rs:143: split_off(1)?[0] as u16 — correctly widens from wire format (u8) to runtime type (u16).
parser/packet_parser.rs:258: self.chunk_header.merkle_leaf_idx as u16 — same pattern, safe widening cast.
These are consistent: the wire format is u8, but the merkle proof API now wants u16.
Raptorcast Refactoring (Earlier Commits)
The group management refactoring across the other commits is well-structured:
New types: SecondaryGroup<PT>, SecondaryGroupAssignment<PT>, SecondaryGroupMap<PT>, FullNodeGroupMap<PT> — clean separation of validator vs. full-node group concerns.
// Line 143-146pubfnempty(at:Round) -> Self{Self{start: at,end: at }}
The RoundSpan::empty() function has a TODO noting it's only for a deprecated Group struct. Good documentation of the tech debt. The removal of Default for RoundSpan and addition of From<RoundSpan> for Range<Round> are clean improvements.
Overall Assessment
The PR is well-structured and ready for merge with one recommended improvement:
Recommended: Add a safety comment or assertion at assembler.rs:416 documenting that the as u8 cast relies on MAX_MERKLE_TREE_DEPTH <= 9. This prevents a future silent truncation bug if the depth limit is raised without updating the wire format.
The merkle library changes are solid, well-tested, and the API widening from u8 to u16 is done consistently across all call sites.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.