Skip to content

Commit b3f203d

Browse files
fix(count_offset): address CodeRabbit review on PR #669
Six follow-ups from the review: **Actionable (3)** 1. `check_count_offset_target_tree_type` in `grovedb/src/operations/proof/generate.rs` now normalizes `open_transactional_merk_at_path` errors to `Error::InvalidQuery`. Previously a missing path or other storage-layer error leaked through verbatim while a wrong-tree-type case got the intended message — callers got two different error contracts for the same actionable meaning ("you can't run count-offset against this path"). Captured comment explains why we don't propagate the raw open error. 2. Removed a stale rustdoc block describing `prove_aggregate_sum_on_range` that ended up sitting above `prove_count_offset_on_range` after the previous insertion in `merk/src/merk/prove.rs`. The function's own docstring is unchanged. 3. Phase-2 verifier call in `merk/src/proofs/query/count_offset/verify.rs` now uses `cost_return_on_error_no_add!` instead of an open-coded `match` early-return, matching the project standard. The `_no_add` variant is right here because `verify_count_offset_shape` returns a plain `Result<u64, Error>` without accumulating internal cost. **Nitpicks (3)** 4. Two end-to-end rejection tests now assert `Err(Error::InvalidQuery(_))` via `matches!` instead of bare `is_err()`. Catches regressions where some unrelated error (storage I/O, etc.) accidentally satisfies the assertion. 5. Both `subtree.prove_count_offset_on_range(...)` call sites in `generate.rs` (v0 and v1 short-circuits) now wrap downstream merk failures via `Error::CorruptedData(format!("prove_count_offset_on_range failed: {}", e))` instead of bare `Error::MerkError(_)`, matching the wrapping the existing `prove_aggregate_sum_on_range` calls use a few hundred lines up. 6. `verify_query_with_options`'s entry-level offset check switched from the inline `query.query.offset.is_some() && query.query.offset != Some(0)` to `query.has_non_zero_offset()`, matching the leaf-level dispatches in the same file. Test totals unchanged: merk count_offset 17/17, grovedb count_offset 18/18, full merk 510/510, full grovedb 1731/1731. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent cc0ece0 commit b3f203d

5 files changed

Lines changed: 76 additions & 23 deletions

File tree

‎grovedb/src/operations/proof/generate.rs‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ impl GroveDb {
194194
/// hits the empty-tree arm and *doesn't* recurse into the leaf
195195
/// merk, so the leaf-level tree-type check never fires. Doing it
196196
/// here gives callers a clear up-front error in that case.
197+
///
198+
/// Error contract: any failure to resolve `path_query.path` to an
199+
/// eligible merk surfaces as `Error::InvalidQuery`. We don't
200+
/// forward the raw `open_transactional_merk_at_path` error because
201+
/// it can leak storage-layer specifics (missing-path,
202+
/// path-not-a-tree, corrupted-link, etc.) — from the caller's
203+
/// point of view all of those have the same actionable meaning
204+
/// here: "you can't run a count-offset query against this path",
205+
/// and the single `InvalidQuery` covers all of them uniformly.
206+
/// Storage-layer or hardware-IO errors still flow through but get
207+
/// classified the same way; that's acceptable because the
208+
/// alternative — surfacing them as `MerkError` / `CorruptedData`
209+
/// from a purely syntactic gate — gives callers an unstable
210+
/// error contract that depends on whether the merk happens to
211+
/// exist.
197212
fn check_count_offset_target_tree_type(
198213
&self,
199214
path_query: &PathQuery,
@@ -203,15 +218,25 @@ impl GroveDb {
203218
let mut cost = OperationCost::default();
204219
let tx = self.start_transaction();
205220
let path_slices: Vec<&[u8]> = path_query.path.iter().map(|p| p.as_slice()).collect();
206-
let target = cost_return_on_error!(
207-
&mut cost,
208-
self.open_transactional_merk_at_path(
221+
let open_result = self
222+
.open_transactional_merk_at_path(
209223
path_slices.as_slice().into(),
210224
&tx,
211225
None,
212226
grove_version,
213227
)
214-
);
228+
.unwrap_add_cost(&mut cost);
229+
let target = match open_result {
230+
Ok(t) => t,
231+
Err(_e) => {
232+
return Err(Error::InvalidQuery(
233+
"count-offset paginated queries are only valid against \
234+
ProvableCountTree / ProvableCountSumTree merks; the target path \
235+
could not be resolved to an eligible merk",
236+
))
237+
.wrap_with_cost(cost);
238+
}
239+
};
215240
if !matches!(
216241
target.tree_type,
217242
MerkTreeType::ProvableCountTree | MerkTreeType::ProvableCountSumTree
@@ -465,7 +490,17 @@ impl GroveDb {
465490
query.left_to_right,
466491
grove_version,
467492
)
468-
.map_err(Error::MerkError)
493+
// Wrap with operational context so a downstream
494+
// proof failure (corrupted merk, invariant
495+
// violation in the prover, etc.) is identifiable
496+
// as a count-offset-specific failure rather than
497+
// an opaque `MerkError`. Mirrors the
498+
// `prove_aggregate_sum_on_range` wrapping a few
499+
// hundred lines up.
500+
.map_err(|e| Error::CorruptedData(format!(
501+
"prove_count_offset_on_range failed: {}",
502+
e
503+
)))
469504
);
470505
let mut serialized = Vec::with_capacity(128);
471506
encode_into(prove_result.ops.iter(), &mut serialized);
@@ -1390,7 +1425,17 @@ impl GroveDb {
13901425
query.left_to_right,
13911426
grove_version,
13921427
)
1393-
.map_err(Error::MerkError)
1428+
// Wrap with operational context so a downstream
1429+
// proof failure (corrupted merk, invariant
1430+
// violation in the prover, etc.) is identifiable
1431+
// as a count-offset-specific failure rather than
1432+
// an opaque `MerkError`. Mirrors the
1433+
// `prove_aggregate_sum_on_range` wrapping a few
1434+
// hundred lines up.
1435+
.map_err(|e| Error::CorruptedData(format!(
1436+
"prove_count_offset_on_range failed: {}",
1437+
e
1438+
)))
13941439
);
13951440
let mut serialized = Vec::with_capacity(128);
13961441
encode_into(prove_result.ops.iter(), &mut serialized);

‎grovedb/src/operations/proof/verify.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl GroveDb {
5454
))?;
5555
}
5656

57-
if query.query.offset.is_some() && query.query.offset != Some(0) {
57+
if query.has_non_zero_offset() {
5858
// Mirror of the prover-side relaxation: a non-zero offset
5959
// is only honored when the query validates as offset-
6060
// paginated against a ProvableCountTree / ProvableCountSumTree

‎grovedb/src/tests/count_offset_paginated_tests.rs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,14 @@ mod tests {
404404
SizedQuery::new(q, Some(3), Some(1)),
405405
);
406406
let result = db.prove_query(&path_query, None, v).unwrap();
407+
// The rejection MUST be `InvalidQuery` specifically — `is_err()`
408+
// alone would mask a regression where some unrelated error
409+
// (e.g. storage I/O) accidentally satisfies the test.
407410
assert!(
408-
result.is_err(),
409-
"prover must reject offset on a query with a default subquery branch"
411+
matches!(result, Err(crate::Error::InvalidQuery(_))),
412+
"prover must reject offset on a query with a default subquery branch \
413+
with InvalidQuery; got {:?}",
414+
result
410415
);
411416
}
412417

@@ -495,9 +500,14 @@ mod tests {
495500
SizedQuery::new(q, Some(3), Some(1)),
496501
);
497502
let result = db.prove_query(&path_query, None, v).unwrap();
503+
// Same rationale as the subquery rejection test: pin the
504+
// exact error variant to detect regressions in the
505+
// tree-type gate's error normalization.
498506
assert!(
499-
result.is_err(),
500-
"prover must reject offset against a NormalTree at leaf-open time"
507+
matches!(result, Err(crate::Error::InvalidQuery(_))),
508+
"prover must reject offset against a NormalTree at leaf-open time \
509+
with InvalidQuery; got {:?}",
510+
result
501511
);
502512
}
503513
}

‎merk/src/merk/prove.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,6 @@ where
189189
})
190190
}
191191

192-
/// Generate a sum-only proof for an `AggregateSumOnRange` query.
193-
/// Mirror of [`Self::prove_aggregate_count_on_range`] for the
194-
/// `ProvableSumTree` flavor.
195-
///
196-
/// The merk's `tree_type` must be `ProvableSumTree`; any other tree type
197-
/// is rejected with `Error::InvalidProofError` before any walking
198-
/// happens. Empty merk: returns `(empty proof, sum = 0)`.
199192
/// Generate an offset-paginated proof for a single-range query
200193
/// against a `ProvableCountTree` or `ProvableCountSumTree`.
201194
///

‎merk/src/proofs/query/count_offset/verify.rs‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
//! `CountOffsetProofResult`; the caller can choose to treat it as an
4646
//! error if their semantics require offset to be exactly satisfied.
4747
48-
use grovedb_costs::{cost_return_on_error, CostResult, CostsExt, OperationCost};
48+
use grovedb_costs::{
49+
cost_return_on_error, cost_return_on_error_no_add, CostResult, CostsExt, OperationCost,
50+
};
4951

5052
use crate::{
5153
proofs::{
@@ -155,10 +157,13 @@ pub fn verify_count_offset_on_range_proof(
155157
returned: Vec::new(),
156158
left_to_right,
157159
};
158-
match verify_count_offset_shape(&tree, inner_range, None, None, &mut state) {
159-
Ok(_struct_count) => {}
160-
Err(e) => return Err(e).wrap_with_cost(cost),
161-
}
160+
// `verify_count_offset_shape` returns a plain `Result<u64, Error>`
161+
// (no internal cost accumulation), so we use the no-add variant of
162+
// the project-standard cost-return macro.
163+
cost_return_on_error_no_add!(
164+
cost,
165+
verify_count_offset_shape(&tree, inner_range, None, None, &mut state)
166+
);
162167

163168
let root_hash = tree.hash().unwrap_add_cost(&mut cost);
164169
Ok(CountOffsetProofResult {

0 commit comments

Comments
 (0)