Repository navigation
fix(batch): refuse a nested element wrapper instead of panicking - #1017
Conversation
`Element` is a public enum, so a caller can hand-build `NonCounted(NonCounted(..))` past the checked constructors. Batch execution and batch reference following dispatch on `underlying()`, which unwraps one level, and their wrapper arms were `unreachable!`, so such an element panicked at every grove version. The direct insert path has refused nesting with an error since #659. `reject_backward_references_elements_in_batch`, which every batch entry point (full, partial, partial add-ons) already runs on each element-carrying op, now refuses a nested wrapper with `InvalidBatchOperation`. Not version-gated: a batch that reached those arms panicked and never committed, and no production caller can build a nested wrapper. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Waiting for bot review — coderabbitai ✓ · thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to No actionable issue is identified; the change is ready for normal merge checks. Pre-merge checks |
|
|
🕓 Review not started yet because the new head is waiting for the 30-minute push debounce.
Commit d7d23ca. Normal review starts when eligible; priority review starts as soon as a slot is available. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1017 +/- ##
========================================
Coverage 93.20% 93.20%
========================================
Files 336 336
Lines 106244 106250 +6
========================================
+ Hits 99024 99030 +6
Misses 7220 7220
🚀 New features to boost your workflow:
|
Issue
A batch carrying a hand-built nested element wrapper panics inside
apply_batchinstead of returning an error. I found it while reviewing #1015; the bug was already ondevelop(297cb88).Elementis a public enum, so a Rust caller can buildElement::NonCounted(Box::new(Element::NonCounted(Box::new(item))))directly. Most entry points refuse a wrapper inside a wrapper:new_non_counted,new_not_summed,new_not_counted_or_summed)InvalidInput("nested element wrappers are not allowed")since feat: add Element::NotSummed wrapper to opt-out of sum propagation #659 (add_element_on_transaction/{v0,v1,v2}.rs)The batch path has no such check:
execute_ops_on_pathmatcheselement.underlying().underlying()unwraps one level, so a nested wrapper lands on the wrapper arm:unreachable!("unwrapped above").follow_reference_get_value_hashdoes the same for a pending element that a batch reference follows into. Two arms are affected: theInsertOrReplacefamily, andInsertWithKnownToNotAlreadyExist/InsertIfNotExists.Reproducer, at every grove version:
A probe ran 504 cases at GROVE_V1–V4: 9 element shapes × 7 op kinds × with and without a stored element at the key.
ReplaceandPatchover a stored element, and anInsertIfNotExiststhat would have skipped.NonCounted(NonCounted(Item))under a SumTree) returns an error by itself. A sibling reference that sorts first still follows into it and panics atmod.rs:2943.Fix
Every batch entry point already runs
reject_backward_references_elements_in_batchon each element-carrying op: full batches, partial batches and partial-batch add-ons. It now also refuseselement.is_wrapped() && element.underlying().is_wrapped()withInvalidBatchOperation("nested element wrappers (...) are not allowed"). Putting the check in that existing pass covers all three entry points without new call sites. The threeunreachable!arms stay, and their comments now name the check that keeps them unreachable.Only nesting is refused here.
validate_wrapper_invariants()also refuses two other shapes, and neither panics:NonCountedaround a backward-references element: this pass already refused it.NotSummedorNotCountedOrSummedaround anything other than a sum tree:Element::serializerefuses these before anything is written. The probe gotElementError(CorruptedData), or anInvalidBatchOperationthat fired earlier, for every op kind at every version.Moving the second group up front would change the error those batches return at V1–V3, so it would need a GROVE_V4 slot through the versioned dispatch pattern. It would not prevent any panic or bad write, so this PR leaves them alone.
Why this is not version-gated
The repo rule is that GROVE_V3 is live, so behavior-changing fixes go behind GROVE_V4+. This fix is not gated, for the same reason as #1016:
InvalidBatchOperation, with a new message.disable_operation_consistency_checkset. This wasOk; it is now refused.packages/rs-drive/src/fees/op.rson platformv5.1-dev) and never builds one by hand.Tests
grovedb/src/tests/batch_unit_tests.rs, Group 17. Every test runs over all ofGROVE_VERSIONS:test_batch_nested_wrapper_element_is_refused: 5 nested shapes × 7 op kinds × with and without a stored element at the key. The op kinds areInsertOrReplace,InsertWithKnownToNotAlreadyExist,InsertIfNotExists,InsertIfNotExistsskip,Replace,Patch, andInsertOrReplacedont-check.test_batch_reference_to_pending_nested_wrapper_is_refused: a sibling reference into a pending nested element, through both follow arms. It includes the shape the parent guard refuses on its own.test_partial_batch_nested_wrapper_is_refused: the nested op sits in the initial ops in one case, and the add-on callback returns it in the other.Each case checks for the new
InvalidBatchOperationand an unchanged root hash. On unmodifieddevelopall three tests panic, atmod.rs:2943andmod.rs:4149.cargo test -p grovedb: 3560 lib tests passed (8 ignored), plus every integration binarycargo fmt --all -- --check: cleancargo clippy -p grovedb --all-targets -- -D warnings: cleandocs/audit-non-issues.mdhas no entry covering this.Breaking Changes
None. A batch that panicked now returns
InvalidBatchOperation.Checklist:
🤖 Generated with Claude Code
PR Hygiene ·
d7d23ca/skip-botsproceeds without the ones not yet reported/self-reviewedWhen every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.Summary by CodeRabbit
InvalidBatchOperationbefore writing, including full batches, partial batches, and add-ons to partial batches.