Skip to content

fix: descending limited append-layer reads keep the highest positions (#855) - #866

Merged
QuantumExplorer merged 1 commit into
developfrom
claude/issue-855-d40073
Sep 6, 2026
Merged

QuantumExplorer merged 1 commit into
developfrom
claude/issue-855-d40073

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Closes #855.

What the audit found

Bulk range extraction sorts positions ascending, and a limit applied to that order returns the FIRST positions even when the query is descending — the opposite end of the requested range.

State on develop

The audited snapshot (2fa0f133) predates #847, which made the GroveDB V1 verifier order the MMR / BulkAppend / Dense / CommitmentTree rows by the query direction before consuming the shared limit. The GroveDB proof path was already correct; what was missing was the runtime validation the issue lists, plus the crate-level helper the issue names as root control.

Changes

  • BulkAppendTreeProof::verify_against_query (grovedb-bulk-append-tree): the collected values now follow query.left_to_right and honour query.limit, so a descending limited query yields the highest positions — the same window the GroveDB verifier and a direct read return. Completeness is still checked against the full ranges before the cap, so a limit cannot hide a missing tail (pinned by a test).
  • Crate tests built queries with Query::default(), whose derived left_to_right is false; they now use Query::new() so the "ascending" fixtures really are ascending.
  • New grovedb/src/tests/append_layer_direction_tests.rs — the validation the audit left outstanding. For BulkAppendTree and CommitmentTree with entries spanning two completed chunks plus the dense buffer: every window × direction × cap kind (SizedQuery::limit and per-instance Query::limit) is proved, verified against the live root, and compared to the direct range read (bulk_get_range / commitment_tree_get_range). The same proof is then re-verified uncapped in both directions to show it carries the whole selected span. A separate test pins that a descending cap is the reverse tail of the ascending read, never its prefix.

Noted, not changed

The trusted query_raw engine does not descend into append-only layers (it yields no rows there in either direction). That is a pre-existing read-engine gap unrelated to ordering; the new trusted-read test only pins that it never serves a different window from the proof.

Verification

  • cargo test -p grovedb-bulk-append-tree: 120 passed.
  • cargo test -p grovedb --all-features for the new module plus the per-instance-limit, bulk, commitment, dense and MMR suites: 251 passed.
  • Clippy (--all-features --all-targets -D warnings) reports nothing in the changed files; the remaining hits are pre-existing in untouched test files under the local clippy 1.97.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Corrected descending queries with limits so they return the highest matching results first.
    • Ensured query limits only restrict returned results and do not weaken completeness validation.
    • Improved consistency between direct reads, generated proofs, and verified results across storage regions.
  • Tests

    • Added coverage for descending, limited reads across append and commitment tree layers.

…#855)

`BulkAppendTreeProof::verify_against_query` collected matched values in
ascending position order and ignored the query's `left_to_right` and
`limit`, so a descending limited query handed back the opposite end of
the range. It now orders by the trusted direction BEFORE applying the
cap, and completeness is still checked against the full ranges so the
limit cannot hide a missing tail.

The GroveDB V1 verifier already orders the MMR / BulkAppend / Dense /
CommitmentTree rows by direction before consuming the shared limit
(#847). This adds the runtime validation the audit left outstanding:
for bulk and commitment trees, windows spanning chunk blobs and the
dense buffer are proved, verified, and compared against the direct
range read in both directions under both global and per-instance caps,
and the same proof is re-verified uncapped to show it carries the whole
selected span.

The bulk crate's proof tests built queries with `Query::default()`,
whose derived `left_to_right` is `false`; they now use `Query::new()`
so the "ascending" fixtures really are ascending.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 0391e7e5-e6fe-495a-8465-964d0ee4daf2

📥 Commits

Reviewing files that changed from the base of the PR and between 6fc7e1e and d8261db.

📒 Files selected for processing (4)
  • grovedb-bulk-append-tree/src/proof/mod.rs
  • grovedb-bulk-append-tree/src/proof/tests.rs
  • grovedb/src/tests/append_layer_direction_tests.rs
  • grovedb/src/tests/mod.rs

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

verify_against_query now orders results according to query direction before applying limits. New tests validate descending limited reads, proof completeness, and consistency across bulk and commitment tree layers.

Changes

Direction-aware proof reads

Layer / File(s) Summary
Direction-aware verification
grovedb-bulk-append-tree/src/proof/mod.rs
Verification reverses results for descending queries before applying query.limit. Completeness validation still covers the full query ranges.
Proof ordering and completeness tests
grovedb-bulk-append-tree/src/proof/tests.rs
Tests cover descending ordering, direction-aware limits, oversized limits, and rejection of incomplete proofs. Existing query construction uses Query::new().
Append-layer integration validation
grovedb/src/tests/append_layer_direction_tests.rs, grovedb/src/tests/mod.rs
Tests compare direct, proved, verified, and trusted reads for bulk and commitment trees across directions, windows, and global or per-instance limits.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to d8261

Direction-aware proof ordering and limiting are covered across bulk and commitment tree reads, with completeness retained before truncation.

Sequence Diagram(s)

sequenceDiagram
  participant DirectRead
  participant ProofGeneration
  participant verify_against_query
  DirectRead->>ProofGeneration: select requested append-layer window
  ProofGeneration->>verify_against_query: provide proof and query
  verify_against_query->>verify_against_query: order results by query direction
  verify_against_query->>verify_against_query: apply query.limit
  verify_against_query-->>DirectRead: return verified window
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the primary fix: descending limited append-layer reads now keep the highest positions. It matches issue #855 and the implementation scope.
Linked Issues check ✅ Passed The changes satisfy issue #855. They apply query direction before limiting, preserve completeness checks over full ranges, and add validation for direct reads, generated proofs, and verified results a…
Out of Scope Changes check ✅ Passed The changes remain within issue #855. The implementation fix, Query::new() test updates, test registration, and expanded direction and limit tests all support the reported correctness issue and its va…
Docstring Coverage ✅ Passed Docstring coverage is 84.38% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 32 functions across 4 files.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/issue-855-d40073

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.58%. Comparing base (6fc7e1e) to head (d8261db).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #866   +/-   ##
========================================
  Coverage    92.58%   92.58%           
========================================
  Files          297      297           
  Lines        92293    92299    +6     
========================================
+ Hits         85448    85454    +6     
  Misses        6845     6845           
Components Coverage Δ
grovedb-core 90.80% <ø> (ø)
merk 93.27% <ø> (ø)
storage 91.70% <ø> (ø)
commitment-tree 96.38% <ø> (ø)
mmr 95.12% <ø> (ø)
bulk-append-tree 92.78% <100.00%> (+0.02%) ⬆️
element 97.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@QuantumExplorer QuantumExplorer left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reviewed

@QuantumExplorer
QuantumExplorer merged commit 3be2eb6 into develop Sep 6, 2026
11 checks passed
@QuantumExplorer
QuantumExplorer deleted the claude/issue-855-d40073 branch September 6, 2026 17:52
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.

[audit][P04] Descending bulk and commitment proof reads return the opposite end of a range

1 participant