From 8c317ae6bb41b3a7f149f79932480c918fb1e45d Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Thu, 1 Oct 2026 23:42:13 +0700 Subject: [PATCH 01/10] feat(batch)!: opt-in settlement of storage owner changes An update in place whose new storage flags name a different owner than the stored flags can now be accounted as a deletion plus an insertion: the old element's key and value bytes are removed and sectioned to the old owner by split_removal_bytes, as a deletion sections them, and the new element's key and value bytes are added and charged to the writer, who keeps the flags they wrote. The flags callback may answer the new grovedb_costs ElementFlagsUpdate (Unchanged, Changed, SettleOwnerChange) in place of a bool, and StorageFlags::update_element_flags_settling_owner_changes answers SettleOwnerChange on an owner change. A batch accepts that answer only with the new BatchApplyOptions::settle_owner_changes, which also makes the average-case and worst-case estimates charge every write that may settle at least the bytes its node adds when inserted, referrer entries a backward-references element carries over included. Same-batch references to a settled item or sum item commit to the bytes that land. With the option off, every cost, estimate, stored element and hash is unchanged. BREAKING CHANGE: BatchApplyOptions has a new public field; Merk's just-in-time value update callback answers (ElementFlagsUpdate, Option) instead of (bool, ...); a GroveDB flags callback that never returns Ok must name its result type. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 36 + costs/src/storage_cost/transition.rs | 47 + docs/crates/auxiliary.md | 1 + .../src/update_element_flags.rs | 229 +++- grovedb/src/batch/backward_references.rs | 14 +- grovedb/src/batch/batch_structure.rs | 8 +- .../estimated_costs/average_case_costs.rs | 120 +- grovedb/src/batch/estimated_costs/mod.rs | 108 ++ .../batch/estimated_costs/worst_case_costs.rs | 89 +- .../batch/just_in_time_reference_update.rs | 105 +- .../src/batch/just_in_time_value_update.rs | 72 +- grovedb/src/batch/mod.rs | 95 +- grovedb/src/batch/options.rs | 23 + .../batch/settle_owner_change_cost_tests.rs | 1064 +++++++++++++++++ .../backward_references_flags_update_tests.rs | 89 +- merk/benches/merk.rs | 6 +- merk/src/element/delete.rs | 7 +- merk/src/merk/apply.rs | 21 +- merk/src/test_utils/mod.rs | 8 +- merk/src/tree/just_in_time_value_update.rs | 179 ++- merk/src/tree/mod.rs | 9 +- merk/src/tree/ops.rs | 13 +- merk/src/tree/put_value/mod.rs | 9 +- merk/src/tree/put_value/v0.rs | 6 +- merk/src/tree/put_value/v1.rs | 6 +- merk/src/tree/walk/mod.rs | 14 +- 26 files changed, 2222 insertions(+), 156 deletions(-) create mode 100644 grovedb/src/batch/settle_owner_change_cost_tests.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index f535199ef..31fa40b23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,8 +14,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 prefix in key order, including a transaction's own uncommitted writes when read through it. Aux storage was reachable by exact key only, so a collection kept as one aux entry per member could not be read back. (#968) +- An opt-in mode that settles storage owner changes: + `BatchApplyOptions::settle_owner_changes` together with + `StorageFlags::update_element_flags_settling_owner_changes` as the flags + callback. An update in place whose new storage flags name a different owner + than the stored flags is accounted as a deletion plus an insertion: every + byte of the old element, key included, counts as removed and is sectioned + to the old owner through its epoch map by `split_removal_bytes`, and every + byte of the new element, key included, counts as added and is charged to + the writer, who keeps the flags they wrote. Without the mode the new owner + took the whole element (and its refund) when the size changed, or paid for + an element that kept naming the old owner when it did not. Updates that + keep the owner, or that have no owner on either side, are unchanged. The + flags callback can now answer an `ElementFlagsUpdate` (`Unchanged`, + `Changed`, `SettleOwnerChange`, new in `grovedb_costs`) in place of a + `bool`; a batch refuses `SettleOwnerChange` unless the option is set. With + the option, the average-case and worst-case batch estimates charge every + `InsertOrReplace`, `Replace`, `Patch` and trusted `RefreshReference` of an + element with flags at least the bytes its node adds when inserted (for a + backward-references element, with the referrers it carries over counted at + its declared capacity), so an estimate is never below a settled apply. With the option off, every cost, + estimate, stored element and hash is what it was before. ### Changed +- **BREAKING**: `BatchApplyOptions` has a new public field, + `settle_owner_changes` (`false` by default), so a struct literal that names + every field without `..Default::default()` must add it. +- **BREAKING**: Merk's just-in-time value update callback (the + `update_tree_value_based_on_costs` argument of + `Merk::apply_with_costs_just_in_time_value_update`, `Merk::apply_unchecked` + and the `TreeNode` put functions) answers + `(ElementFlagsUpdate, Option)` in place of + `(bool, Option)`. GroveDB's own flags callback (in + `apply_batch_with_element_flags_update`, + `apply_partial_batch_with_element_flags_update` and + `estimated_case_operations_for_batch`) takes any answer that converts into + an `ElementFlagsUpdate`, so a callback answering a `bool` compiles as before, + except one that never returns `Ok`, which now has to name its result type + (for example `-> Result`). - **BREAKING**: `delete_operation_for_delete_internal` and `delete_operations_for_delete_up_tree_while_empty` take the operations already pending in the batch as any `impl PendingOperations` in place of a diff --git a/costs/src/storage_cost/transition.rs b/costs/src/storage_cost/transition.rs index 1ac154033..5d02cddee 100644 --- a/costs/src/storage_cost/transition.rs +++ b/costs/src/storage_cost/transition.rs @@ -55,6 +55,39 @@ pub enum OperationStorageTransitionType { OperationNone, } +/// What a just-in-time flags update decided for an element that an update +/// rewrites in place +/// +/// A flags-update callback that only rewrites flags answers `Changed` or +/// `Unchanged`, and a `bool` converts to those. `SettleOwnerChange` also +/// changes how the update is accounted, and a batch only accepts it when its +/// options ask for that. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ElementFlagsUpdate { + /// The new element's flags were left as they were + Unchanged, + /// The new element's flags were rewritten + Changed, + /// The update hands the element to a different owner. It is accounted + /// as the removal of the old element plus the insertion of the new one + /// (an [`OperationStorageTransitionType::OperationReplace`]): every byte + /// of the old element, key included, counts as removed and is sectioned + /// through the old element's flags as a deletion sections it, and every + /// byte of the new element, key included, counts as added. The new + /// element keeps the flags the callback leaves it with. + SettleOwnerChange, +} + +impl From for ElementFlagsUpdate { + fn from(flags_changed: bool) -> Self { + if flags_changed { + ElementFlagsUpdate::Changed + } else { + ElementFlagsUpdate::Unchanged + } + } +} + impl StorageCost { /// the type of transition that the costs represent pub fn transition_type(&self) -> OperationStorageTransitionType { @@ -79,3 +112,17 @@ impl StorageCost { } } } + +#[cfg(test)] +mod tests { + use super::ElementFlagsUpdate; + + #[test] + fn a_bool_flags_update_converts_to_changed_or_unchanged() { + assert_eq!(ElementFlagsUpdate::from(true), ElementFlagsUpdate::Changed); + assert_eq!( + ElementFlagsUpdate::from(false), + ElementFlagsUpdate::Unchanged + ); + } +} diff --git a/docs/crates/auxiliary.md b/docs/crates/auxiliary.md index 0b41f3e29..904a74108 100644 --- a/docs/crates/auxiliary.md +++ b/docs/crates/auxiliary.md @@ -214,6 +214,7 @@ These work on raw `ElementFlags` and match the callback signatures GroveDB expec - **Anything else, or no old flags**: leaves `new_flags` alone and returns `false`. It always merges with `MergingOwnersStrategy::UseTheirs`. When `old_flags` is `Some`, both the old and the new bytes must decode to flags, or it returns `RemovingFlagsError`. +- `StorageFlags::update_element_flags_settling_owner_changes(cost, old_flags, new_flags) -> Result` answers `ElementFlagsUpdate::SettleOwnerChange` for an update in place (bigger, smaller or same size) whose old and new flags both name an owner and the owners differ, and leaves `new_flags` as written. Every other update gets what `update_element_flags` gives it, as `Changed` or `Unchanged`. A batch accepts the settling answer only with `BatchApplyOptions::settle_owner_changes` set, and then accounts the update as a deletion plus an insertion: the old element's key and value bytes are removed and sectioned to the old owner by `split_removal_bytes`, and the new element's key and value bytes are added. - `StorageFlags::split_removal_bytes(flags: &mut ElementFlags, removed_key_bytes, removed_value_bytes)` decodes the flags and calls `split_storage_removed_bytes`. It returns `BasicStorageRemoval` for both parts if the element has no flags. Other helpers include: diff --git a/grovedb-epoch-based-storage-flags/src/update_element_flags.rs b/grovedb-epoch-based-storage-flags/src/update_element_flags.rs index 77f8e4b43..8982cce46 100644 --- a/grovedb-epoch-based-storage-flags/src/update_element_flags.rs +++ b/grovedb-epoch-based-storage-flags/src/update_element_flags.rs @@ -1,8 +1,62 @@ -use grovedb_costs::storage_cost::{transition::OperationStorageTransitionType, StorageCost}; +use grovedb_costs::storage_cost::{ + transition::{ElementFlagsUpdate, OperationStorageTransitionType}, + StorageCost, +}; use crate::{error::StorageFlagsError, ElementFlags, MergingOwnersStrategy, StorageFlags}; impl StorageFlags { + /// [`Self::update_element_flags`], except that an update whose new flags + /// name a different owner than the old flags settles the owner change + /// + /// When both the old and the new flags name an owner and the owners + /// differ, the new flags are left as the writer wrote them and the update + /// answers [`ElementFlagsUpdate::SettleOwnerChange`]: the batch then + /// accounts it as the removal of the old element, sectioned to the old + /// owner through its epoch map by [`Self::split_removal_bytes`], plus the + /// insertion of the new element, charged to the writer. Every other update + /// (same owner, or no owner on either side) is answered by + /// [`Self::update_element_flags`], unchanged. + /// + /// The batch accepts the answer only when its options set + /// `settle_owner_changes`. + pub fn update_element_flags_settling_owner_changes( + cost: &StorageCost, + old_flags: Option, + new_flags: &mut ElementFlags, + ) -> Result { + if Self::update_changes_owner(cost, old_flags.as_ref(), new_flags) { + return Ok(ElementFlagsUpdate::SettleOwnerChange); + } + Self::update_element_flags(cost, old_flags, new_flags).map(ElementFlagsUpdate::from) + } + + /// Whether `cost` updates an element in place and the old and new flags + /// both parse and name different owners. Flags that do not parse are left + /// to [`Self::update_element_flags`], which reports them. + fn update_changes_owner( + cost: &StorageCost, + old_flags: Option<&ElementFlags>, + new_flags: &ElementFlags, + ) -> bool { + let is_update_in_place = matches!( + cost.transition_type(), + OperationStorageTransitionType::OperationUpdateBiggerSize + | OperationStorageTransitionType::OperationUpdateSmallerSize + | OperationStorageTransitionType::OperationUpdateSameSize + ); + let owner_of = |flags: &ElementFlags| { + StorageFlags::from_element_flags_ref(flags) + .ok() + .flatten() + .and_then(|storage_flags| storage_flags.owner_id().copied()) + }; + match (old_flags.and_then(owner_of), owner_of(new_flags)) { + (Some(old_owner), Some(new_owner)) => is_update_in_place && old_owner != new_owner, + _ => false, + } + } + pub fn update_element_flags( cost: &StorageCost, old_flags: Option, @@ -146,7 +200,9 @@ impl StorageFlags { mod tests { use std::collections::BTreeMap; - use grovedb_costs::storage_cost::{removal::StorageRemovedBytes, StorageCost}; + use grovedb_costs::storage_cost::{ + removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost, + }; use crate::StorageFlags; @@ -310,4 +366,173 @@ mod tests { .to_string() .contains("drive did not understand updated item flag information")); } + + fn update_cost(added_bytes: u32, replaced_bytes: u32, removed_bytes: u32) -> StorageCost { + StorageCost { + added_bytes, + replaced_bytes, + removed_bytes: if removed_bytes > 0 { + StorageRemovedBytes::BasicStorageRemoval(removed_bytes) + } else { + StorageRemovedBytes::NoStorageRemoval + }, + } + } + + #[test] + fn settling_update_settles_an_owner_change_of_any_size() { + let old_owner = [1u8; 32]; + let new_owner = [2u8; 32]; + let old = StorageFlags::SingleEpochOwned(1, old_owner).to_element_flags(); + let written = StorageFlags::SingleEpochOwned(4, new_owner).to_element_flags(); + for cost in [ + update_cost(10, 50, 0), + update_cost(0, 50, 10), + update_cost(0, 50, 0), + ] { + let mut new_flags = written.clone(); + let update = StorageFlags::update_element_flags_settling_owner_changes( + &cost, + Some(old.clone()), + &mut new_flags, + ) + .expect("expected success"); + assert_eq!(update, ElementFlagsUpdate::SettleOwnerChange); + // the writer's flags are kept as written, never merged with the + // old owner's epochs + assert_eq!(new_flags, written); + } + } + + #[test] + fn settling_update_settles_an_owner_change_of_multi_epoch_flags() { + let old = StorageFlags::MultiEpochOwned(1, BTreeMap::from([(2, 20), (3, 7)]), [1u8; 32]) + .to_element_flags(); + let written = StorageFlags::SingleEpochOwned(5, [2u8; 32]).to_element_flags(); + let mut new_flags = written.clone(); + let update = StorageFlags::update_element_flags_settling_owner_changes( + &update_cost(4, 60, 0), + Some(old), + &mut new_flags, + ) + .expect("expected success"); + assert_eq!(update, ElementFlagsUpdate::SettleOwnerChange); + assert_eq!(new_flags, written); + } + + /// Every update that does not change the owner gets exactly what + /// `update_element_flags` gives it. + #[test] + fn settling_update_matches_update_element_flags_without_an_owner_change() { + let owner = [1u8; 32]; + let cases = [ + // same owner, bigger, smaller and same size + ( + Some(StorageFlags::SingleEpochOwned(1, owner)), + StorageFlags::SingleEpochOwned(2, owner), + update_cost(10, 50, 0), + ), + ( + Some(StorageFlags::MultiEpochOwned( + 1, + BTreeMap::from([(2, 20)]), + owner, + )), + StorageFlags::SingleEpochOwned(2, owner), + StorageCost { + added_bytes: 0, + replaced_bytes: 50, + removed_bytes: StorageRemovedBytes::SectionedStorageRemoval(BTreeMap::from([ + (owner, intmap::IntMap::from_iter([(2u16, 5u32)])), + ])), + }, + ), + ( + Some(StorageFlags::SingleEpochOwned(1, owner)), + StorageFlags::SingleEpochOwned(2, owner), + update_cost(0, 50, 0), + ), + // no owner on the old side, on the new side, or on either + ( + Some(StorageFlags::SingleEpoch(1)), + StorageFlags::SingleEpochOwned(2, owner), + update_cost(10, 50, 0), + ), + ( + Some(StorageFlags::SingleEpochOwned(1, owner)), + StorageFlags::SingleEpoch(2), + update_cost(0, 50, 0), + ), + ( + Some(StorageFlags::SingleEpoch(1)), + StorageFlags::SingleEpoch(2), + update_cost(10, 50, 0), + ), + // no old flags at all + ( + None, + StorageFlags::SingleEpochOwned(2, owner), + update_cost(10, 50, 0), + ), + ]; + for (old, new, cost) in cases { + let old = old.map(|old| old.to_element_flags()); + let mut expected_flags = new.to_element_flags(); + let expected = + StorageFlags::update_element_flags(&cost, old.clone(), &mut expected_flags) + .expect("expected success"); + let mut new_flags = new.to_element_flags(); + let update = StorageFlags::update_element_flags_settling_owner_changes( + &cost, + old, + &mut new_flags, + ) + .expect("expected success"); + assert_eq!(update, ElementFlagsUpdate::from(expected)); + assert_eq!(new_flags, expected_flags); + } + } + + #[test] + fn settling_update_does_not_settle_outside_an_update_in_place() { + let old = StorageFlags::SingleEpochOwned(1, [1u8; 32]).to_element_flags(); + let written = StorageFlags::SingleEpochOwned(2, [2u8; 32]).to_element_flags(); + let mut new_flags = written.clone(); + let update = StorageFlags::update_element_flags_settling_owner_changes( + &update_cost(10, 0, 0), + Some(old), + &mut new_flags, + ) + .expect("expected success"); + assert_eq!(update, ElementFlagsUpdate::Unchanged); + assert_eq!(new_flags, written); + } + + #[test] + fn settling_update_reports_unreadable_flags_as_update_element_flags_does() { + let cost = update_cost(0, 50, 0); + let owned = StorageFlags::SingleEpochOwned(1, [1u8; 32]).to_element_flags(); + + let mut new_flags = owned.clone(); + let old_error = StorageFlags::update_element_flags_settling_owner_changes( + &cost, + Some(vec![255]), + &mut new_flags, + ) + .expect_err("expected old flag parse error"); + assert!(old_error + .to_string() + .contains("drive did not understand flags of old item being updated")); + + let mut removed_flags = vec![]; + let removed_error = StorageFlags::update_element_flags_settling_owner_changes( + &cost, + Some(owned), + &mut removed_flags, + ) + .expect_err("expected an error for removed flags"); + assert!(removed_error + .to_string() + .contains("removing flags from an item with flags is not allowed")); + } } diff --git a/grovedb/src/batch/backward_references.rs b/grovedb/src/batch/backward_references.rs index 471b20c3d..97c656d14 100644 --- a/grovedb/src/batch/backward_references.rs +++ b/grovedb/src/batch/backward_references.rs @@ -73,7 +73,7 @@ use std::{ use grovedb_costs::{ cost_return_on_error, cost_return_on_error_no_add, - storage_cost::{removal::StorageRemovedBytes, StorageCost}, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; use grovedb_merk::{ @@ -729,7 +729,11 @@ impl<'db, 'g> Expansion<'db, 'g> { split_removal_bytes: &mut SR, ) -> CostResult where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -877,7 +881,11 @@ pub(super) fn expand_backward_references_ops<'db, G, SR>( grove_version: &GroveVersion, ) -> CostResult<(Vec, PreparedMerks<'db>, LandedItems), Error> where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, diff --git a/grovedb/src/batch/batch_structure.rs b/grovedb/src/batch/batch_structure.rs index 213d6c7f6..0b5c8b0eb 100644 --- a/grovedb/src/batch/batch_structure.rs +++ b/grovedb/src/batch/batch_structure.rs @@ -9,7 +9,7 @@ use std::{ #[cfg(feature = "minimal")] use grovedb_costs::{ cost_return_on_error, cost_return_on_error_no_add, - storage_cost::{removal::StorageRemovedBytes, StorageCost}, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; use grovedb_merk::element::tree_type::ElementTreeTypeExtensions; @@ -113,7 +113,11 @@ impl fmt::Debug for BatchStructure { impl BatchStructure where C: TreeCache, - F: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + F: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, diff --git a/grovedb/src/batch/estimated_costs/average_case_costs.rs b/grovedb/src/batch/estimated_costs/average_case_costs.rs index 850e8d4d1..f4c34d3c1 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -69,9 +69,37 @@ impl GroveOp { // it declares `Check`. backwards_references: BackwardsReferences, propagate: bool, + // The batch's `settle_owner_changes` option: a write that may settle + // an owner change is charged as the insertion it then records too. + settle_owner_changes: bool, grove_version: &GroveVersion, ) -> CostResult<(), Error> { let in_tree_type = layer_element_estimates.tree_type; + // A referrer entry of the typical shape: an inverted path built from + // a qualified origin like this op's own (its path segments plus its + // key; an absolute inversion serializes them all), the cascade flag, + // and framing. + let referrer_entry_bound = path + .0 + .iter() + .map(|segment| 4 + segment.max_length() as u32) + .sum::() + .saturating_add(4 + key.max_length() as u32) + .saturating_add(16); + let settling = |base: CostResult<(), Error>, element: &Element| { + if settle_owner_changes { + super::with_settled_owner_change( + base, + key, + element, + in_tree_type.inner_node_type(), + referrer_entry_bound, + grove_version, + ) + } else { + base + } + }; let propagate_if_input = || { if propagate { Some(layer_element_estimates) @@ -93,20 +121,10 @@ impl GroveOp { } match element { Some(Element::BidirectionalReference(..)) => { - // The registration entry appended to the target: an - // inverted path built from the referrer's qualified - // origin (this op's path segments plus its key — an - // absolute inversion serializes them all), the cascade - // flag, and framing. - let origin_bytes: u32 = path - .0 - .iter() - .map(|segment| 4 + segment.max_length() as u32) - .sum::() - .saturating_add(4 + key.max_length() as u32); - let entry_bound = origin_bytes.saturating_add(16); + // The registration entry appended to the target, whose + // referrer is this op itself. Some(super::BackwardReferencesFanOut::average_reference( - entry_bound, + referrer_entry_bound, )) } // A backward-references ITEM write carries its own referrer @@ -225,8 +243,20 @@ impl GroveOp { grove_version, ), GroveOp::InsertOrReplace { element } - | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } - | GroveOp::InsertWithKnownToNotAlreadyExist { element } => with_fan_out( + | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( + settling( + GroveDb::average_case_merk_insert_element( + key, + element, + in_tree_type, + propagate_if_input(), + grove_version, + ), + element, + ), + backward_references_fan_out(Some(element)), + ), + GroveOp::InsertWithKnownToNotAlreadyExist { element } => with_fan_out( GroveDb::average_case_merk_insert_element( key, element, @@ -310,22 +340,32 @@ impl GroveOp { } else { inner }; - GroveDb::average_case_merk_replace_element( + let replace_cost = GroveDb::average_case_merk_replace_element( key, &element, in_tree_type, propagate_if_input(), grove_version, - ) + ); + // An untrusted refresh writes the stored flags back, so only + // a trusted one can change the owner. + if mode.is_trusted() { + settling(replace_cost, &element) + } else { + replace_cost + } } GroveOp::Replace { element } | GroveOp::ReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - GroveDb::average_case_merk_replace_element( - key, + settling( + GroveDb::average_case_merk_replace_element( + key, + element, + in_tree_type, + propagate_if_input(), + grove_version, + ), element, - in_tree_type, - propagate_if_input(), - grove_version, ), backward_references_fan_out(Some(element)), ), @@ -337,13 +377,16 @@ impl GroveOp { element, change_in_bytes, } => with_fan_out( - GroveDb::average_case_merk_patch_element( - key, + settling( + GroveDb::average_case_merk_patch_element( + key, + element, + *change_in_bytes, + in_tree_type, + propagate_if_input(), + grove_version, + ), element, - *change_in_bytes, - in_tree_type, - propagate_if_input(), - grove_version, ), backward_references_fan_out(Some(element)), ), @@ -979,7 +1022,7 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { path: &KeyInfoPath, ops_at_path_by_key: BTreeMap, _ops_by_qualified_paths: &BTreeMap>, GroveOp>, - _batch_apply_options: &BatchApplyOptions, + batch_apply_options: &BatchApplyOptions, _flags_update: &mut G, _split_removal_bytes: &mut SR, grove_version: &GroveVersion, @@ -1151,6 +1194,7 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { append_tree_chunk_power, op.backwards_references(), false, + batch_apply_options.settle_owner_changes, grove_version ) ); @@ -2265,6 +2309,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version ) .cost_as_result() @@ -2277,6 +2322,7 @@ mod tests { Some(10), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2332,6 +2378,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2381,6 +2428,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2425,6 +2473,7 @@ mod tests { Some(4), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2461,6 +2510,7 @@ mod tests { Some(4), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2476,6 +2526,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version ) .cost_as_result() @@ -2495,6 +2546,7 @@ mod tests { Some(2), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2507,6 +2559,7 @@ mod tests { Some(10), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2562,6 +2615,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2614,6 +2668,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2657,6 +2712,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2714,6 +2770,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2776,6 +2833,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2823,6 +2881,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2843,6 +2902,7 @@ mod tests { None, BackwardsReferences::DontCheck, true, + false, grove_version, ) .cost_as_result() @@ -3506,6 +3566,7 @@ mod tests { None, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -3520,6 +3581,7 @@ mod tests { Some(4), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() diff --git a/grovedb/src/batch/estimated_costs/mod.rs b/grovedb/src/batch/estimated_costs/mod.rs index bc1fa4c9a..2fcb9298d 100644 --- a/grovedb/src/batch/estimated_costs/mod.rs +++ b/grovedb/src/batch/estimated_costs/mod.rs @@ -41,6 +41,114 @@ pub(in crate::batch) fn wrapper_overhead_for( } } +/// The node type a worst-case estimate sizes a settled owner change with: +/// the worst-case layers do not say which tree a write lands in, and no node +/// carries more feature bytes than a big sum tree's. +#[cfg(feature = "minimal")] +pub(in crate::batch) const WORST_CASE_SETTLED_NODE_TYPE: grovedb_merk::merk::NodeType = + grovedb_merk::merk::NodeType::BigSumNode; + +/// An upper bound on one registered referrer entry of a backward-references +/// element whose referrers may sit anywhere registration admits them: the +/// inverted path of a referrer at the deepest such position +/// (`MAX_BACKWARD_REFERENCES_GROVE_DEPTH` segments plus its key, each of the +/// biggest key size), with the per-segment and framing allowance the +/// registration bound uses. +#[cfg(feature = "minimal")] +pub(in crate::batch) const WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND: u32 = + (crate::bidirectional_references::MAX_BACKWARD_REFERENCES_GROVE_DEPTH as u32 + 1) + * (4 + grovedb_merk::estimated_costs::worst_case_costs::MERK_BIGGEST_KEY_SIZE) + + 16; + +/// The bytes a write of `element` at a key of `key_len` bytes adds when it +/// settles an owner change: the whole node, key and value, sized exactly as +/// the apply sizes the node it stores in a tree of `node_type` nodes. +/// +/// A backward-references element lands with the referrers registered on the +/// element it replaces in place of the ones it was written with, and the +/// settled apply adds their entries too: they are charged at the element's +/// declared capacity, each at `referrer_entry_bound` bytes. +#[cfg(feature = "minimal")] +fn settled_owner_change_added_bytes( + key_len: u32, + element: &crate::Element, + node_type: grovedb_merk::merk::NodeType, + referrer_entry_bound: u32, + grove_version: &grovedb_version::version::GroveVersion, +) -> Result { + use grovedb_merk::{ + element::costs::ElementCostExtensions, + tree::kv::{ + ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, + KV, + }, + }; + + let carried_referrer_bytes = element.max_incoming_references().map_or(0, |capacity| { + (capacity as u32).saturating_mul(referrer_entry_bound) + }); + Ok(match element.value_defined_cost(grove_version) { + Some(LayeredValueDefinedCost(value_cost)) => { + KV::layered_node_byte_cost_size_for_key_and_value_lengths( + key_len, + value_cost.saturating_add(carried_referrer_bytes), + node_type, + ) + } + Some(SpecializedValueDefinedCost(value_cost)) => { + KV::node_byte_cost_size_for_key_and_raw_value_lengths( + key_len, + value_cost.saturating_add(carried_referrer_bytes), + node_type, + ) + } + None => KV::node_byte_cost_size_for_key_and_raw_value_lengths( + key_len, + (element.serialized_size(grove_version)? as u32).saturating_add(carried_referrer_bytes), + node_type, + ), + }) +} + +/// `base`, the estimate of a write of `element` over a stored element, +/// charged for the case that the write settles an owner change +/// ([`BatchApplyOptions::settle_owner_changes`](crate::batch::BatchApplyOptions::settle_owner_changes)): +/// its added bytes are raised to every byte of the element's node, key +/// included, sized for a tree of `node_type` nodes with each referrer entry +/// a backward-references element may carry over at `referrer_entry_bound` +/// bytes, so the estimate covers both the replacement it models and the +/// insertion a settled owner change records. An element without flags names +/// no owner and never settles, so its estimate is left as it is. +#[cfg(feature = "minimal")] +pub(in crate::batch) fn with_settled_owner_change( + base: grovedb_costs::CostResult<(), crate::Error>, + key: &crate::batch::KeyInfo, + element: &crate::Element, + node_type: grovedb_merk::merk::NodeType, + referrer_entry_bound: u32, + grove_version: &grovedb_version::version::GroveVersion, +) -> grovedb_costs::CostResult<(), crate::Error> { + use grovedb_costs::CostsExt; + use grovedb_storage::worst_case_costs::WorstKeyLength; + + let grovedb_costs::CostContext { value, mut cost } = base; + if value.is_ok() && element.get_flags().is_some() { + match settled_owner_change_added_bytes( + key.max_length() as u32, + element, + node_type, + referrer_entry_bound, + grove_version, + ) { + Ok(settled_bytes) => { + cost.storage_cost.added_bytes = cost.storage_cost.added_bytes.max(settled_bytes) + } + Err(e) => return Err(e).wrap_with_cost(cost), + } + } + value.wrap_with_cost(cost) +} + // ── Backward-references fan-out estimation model ──────────────────────── // // Under `BatchApplyOptions::backwards_references` (GROVE_V4+), a diff --git a/grovedb/src/batch/estimated_costs/worst_case_costs.rs b/grovedb/src/batch/estimated_costs/worst_case_costs.rs index 45a18aa8d..75c6636c3 100644 --- a/grovedb/src/batch/estimated_costs/worst_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/worst_case_costs.rs @@ -58,6 +58,9 @@ impl GroveOp { // it declares `Check`. backwards_references: BackwardsReferences, propagate: bool, + // The batch's `settle_owner_changes` option: a write that may settle + // an owner change is charged as the insertion it then records too. + settle_owner_changes: bool, grove_version: &GroveVersion, ) -> CostResult<(), Error> { let propagate_if_input = || { @@ -67,6 +70,20 @@ impl GroveOp { None } }; + let settling = |base: CostResult<(), Error>, element: &Element| { + if settle_owner_changes { + super::with_settled_owner_change( + base, + key, + element, + super::WORST_CASE_SETTLED_NODE_TYPE, + super::WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND, + grove_version, + ) + } else { + base + } + }; let fan_out_version = grove_version .grovedb_versions .operations @@ -207,8 +224,20 @@ impl GroveOp { grove_version, ), GroveOp::InsertOrReplace { element } - | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } - | GroveOp::InsertWithKnownToNotAlreadyExist { element } => with_fan_out( + | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( + settling( + GroveDb::worst_case_merk_insert_element( + key, + element, + in_parent_tree_type, + propagate_if_input(), + grove_version, + ), + element, + ), + backward_references_fan_out(Some(element)), + ), + GroveOp::InsertWithKnownToNotAlreadyExist { element } => with_fan_out( GroveDb::worst_case_merk_insert_element( key, element, @@ -283,22 +312,32 @@ impl GroveOp { } else { inner }; - GroveDb::worst_case_merk_replace_element( + let replace_cost = GroveDb::worst_case_merk_replace_element( key, &element, in_parent_tree_type, propagate_if_input(), grove_version, - ) + ); + // An untrusted refresh writes the stored flags back, so only + // a trusted one can change the owner. + if mode.is_trusted() { + settling(replace_cost, &element) + } else { + replace_cost + } } GroveOp::Replace { element } | GroveOp::ReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - GroveDb::worst_case_merk_replace_element( - key, + settling( + GroveDb::worst_case_merk_replace_element( + key, + element, + in_parent_tree_type, + propagate_if_input(), + grove_version, + ), element, - in_parent_tree_type, - propagate_if_input(), - grove_version, ), backward_references_fan_out(Some(element)), ), @@ -310,12 +349,15 @@ impl GroveOp { element, change_in_bytes: _, } => with_fan_out( - GroveDb::worst_case_merk_replace_element( - key, + settling( + GroveDb::worst_case_merk_replace_element( + key, + element, + in_parent_tree_type, + propagate_if_input(), + grove_version, + ), element, - in_parent_tree_type, - propagate_if_input(), - grove_version, ), backward_references_fan_out(Some(element)), ), @@ -852,7 +894,7 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { path: &KeyInfoPath, ops_at_path_by_key: BTreeMap, _ops_by_qualified_paths: &BTreeMap>, GroveOp>, - _batch_apply_options: &BatchApplyOptions, + batch_apply_options: &BatchApplyOptions, _flags_update: &mut G, _split_removal_bytes: &mut SR, grove_version: &GroveVersion, @@ -909,6 +951,7 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { worst_case_layer_element_estimates, op.backwards_references(), false, + batch_apply_options.settle_owner_changes, grove_version ) ); @@ -1583,6 +1626,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1609,6 +1653,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, + false, grove_version, ) .cost_as_result() @@ -1634,6 +1679,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1659,6 +1705,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1683,6 +1730,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1719,6 +1767,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1747,6 +1796,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, + false, grove_version, ) .cost_as_result() @@ -1771,6 +1821,7 @@ mod tests { &MaxElementsNumber(50), BackwardsReferences::DontCheck, true, + false, grove_version, ) .cost_as_result() @@ -1803,6 +1854,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1835,6 +1887,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, + false, grove_version, ) .cost_as_result() @@ -1871,6 +1924,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1927,6 +1981,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -1998,6 +2053,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2025,6 +2081,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, + false, grove_version, ) .cost_as_result() @@ -2075,6 +2132,7 @@ mod tests { &layer_info, BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() @@ -2135,6 +2193,7 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, + false, grove_version, ) .cost_as_result() diff --git a/grovedb/src/batch/just_in_time_reference_update.rs b/grovedb/src/batch/just_in_time_reference_update.rs index 78cb715fb..107599f90 100644 --- a/grovedb/src/batch/just_in_time_reference_update.rs +++ b/grovedb/src/batch/just_in_time_reference_update.rs @@ -4,11 +4,13 @@ use grovedb_costs::{ cost_return_on_error_into_no_add, cost_return_on_error_no_add, storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }, CostResult, CostsExt, OperationCost, }; use grovedb_merk::{ + element::costs::ElementCostExtensions, tree::{kv::KV, value_hash, TreeNode}, tree_type::TreeType, CryptoHash, Merk, @@ -56,6 +58,17 @@ where Ok(val_hash).wrap_with_cost(cost) } + /// The value hash of the bytes a pending write of `new_element` over the + /// stored `old_element` finally stores, after the apply has run the + /// caller's flags update on it, so that a reference written in the same + /// batch commits to them. + /// + /// With `settle_owner_changes` a flags update may settle an owner change, + /// and the apply then stores the new element with the flags the callback + /// leaves it, whatever its size. A sum item is otherwise taken to keep + /// its stored flags without asking the flags update, so in that mode it + /// is asked first. + #[allow(clippy::too_many_arguments)] pub(crate) fn process_old_element_flags( key: &[u8], serialized: &[u8], @@ -63,12 +76,17 @@ where old_element: Element, old_serialized_element: &[u8], in_tree_type: TreeType, + settle_owner_changes: bool, flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, ) -> CostResult where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -76,9 +94,71 @@ where ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { let mut cost = OperationCost::default(); + let client_error = |e: Error| -> Error { + match e { + Error::JustInTimeElementFlagsClientError(_) => { + MerkError::ClientCorruptionError(e.to_string()).into() + } + _ => MerkError::ClientCorruptionError("non client error".to_string()).into(), + } + }; if old_element.is_sum_item() { return if new_element.is_sum_item() { let maybe_old_flags = old_element.get_flags_owned(); + if settle_owner_changes + && maybe_old_flags.is_some() + && new_element.get_flags().is_some() + { + // Merk measures a sum item by its own specialized cost, + // new flags included, against the stored one's. + let node_type = in_tree_type.inner_node_type(); + let old_storage_cost = cost_return_on_error_no_add!( + cost, + Element::specialized_costs_for_key_value( + key, + old_serialized_element, + node_type, + grove_version, + ) + .map_err(Error::MerkError) + ); + let new_storage_cost = cost_return_on_error_no_add!( + cost, + Element::specialized_costs_for_key_value( + key, + serialized, + node_type, + grove_version, + ) + .map_err(Error::MerkError) + ); + let mut storage_costs = + TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); + if let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes { + let mut old_flags = maybe_old_flags.clone().unwrap_or_default(); + let (_, value_removed_bytes) = cost_return_on_error_no_add!( + cost, + split_removal_bytes(&mut old_flags, 0, removed_bytes) + ); + storage_costs.removed_bytes = value_removed_bytes; + } + let mut settled_element = new_element.clone(); + if let Some(new_flags) = settled_element.get_flags_mut().as_mut() { + let update = cost_return_on_error_no_add!( + cost, + (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) + .map_err(client_error) + ); + if update == ElementFlagsUpdate::SettleOwnerChange { + let settled_bytes = cost_return_on_error_into_no_add!( + cost, + settled_element.serialize(grove_version) + ); + let val_hash = value_hash(&settled_bytes).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + } + } if maybe_old_flags.is_some() { let mut updated_new_element_with_old_flags = new_element.clone(); updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); @@ -168,20 +248,12 @@ where "element has no flags for just-in-time update", )) ); - let changed = cost_return_on_error_no_add!( + let update = cost_return_on_error_no_add!( cost, - (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags).map_err(|e| { - match e { - Error::JustInTimeElementFlagsClientError(_) => { - MerkError::ClientCorruptionError(e.to_string()).into() - } - _ => { - MerkError::ClientCorruptionError("non client error".to_string()).into() - } - } - }) + (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) + .map_err(client_error) ); - if !changed { + if update == ElementFlagsUpdate::Unchanged { // There are no storage flags, we can just hash new element let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); @@ -193,6 +265,13 @@ where new_element_cloned.serialize(grove_version) ); + if update == ElementFlagsUpdate::SettleOwnerChange { + // The apply stores the new element with the flags the + // callback left it, whatever its size. + let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + new_storage_cost = KV::node_value_byte_cost_size( key.len() as u32, new_serialized_bytes.len() as u32, diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index f55a6287e..ebcc08fb6 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -13,6 +13,7 @@ use grovedb_costs::storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }; use grovedb_merk::{ @@ -32,6 +33,36 @@ use integer_encoding::VarInt; use crate::{Element, ElementFlags, Error}; +/// The caller's flags-update callback as a batch runs it: its answer as an +/// [`ElementFlagsUpdate`] (a `bool` answer is `Changed` or `Unchanged`), and +/// `SettleOwnerChange` refused unless the batch's options set +/// `settle_owner_changes`. +pub(crate) fn batch_flags_update( + mut flags_update: impl FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, + settle_owner_changes: bool, +) -> impl FnMut(&StorageCost, Option, &mut ElementFlags) -> Result +where + U: Into, +{ + move |storage_cost: &StorageCost, + old_flags: Option, + new_flags: &mut ElementFlags| { + let update = flags_update(storage_cost, old_flags, new_flags)?.into(); + if update == ElementFlagsUpdate::SettleOwnerChange && !settle_owner_changes { + return Err(Error::JustInTimeElementFlagsClientError( + "the flags update settled an owner change, but the batch options do not set \ + settle_owner_changes" + .to_owned(), + )); + } + Ok(update) + } +} + /// The storage cost of the stored value a write replaces. pub(super) fn old_specialized_cost( key: &[u8], @@ -72,17 +103,22 @@ pub(super) fn new_value_with_old_flags( } /// Run the caller's flags-update callback on the new value's flags; when it -/// changes them, rewrite `new_value` and return the value-defined cost the -/// rewritten element carries. +/// changes them or settles an owner change, rewrite `new_value` with the +/// flags it leaves and return the value-defined cost the rewritten element +/// carries. pub(super) fn update_value_flags_based_on_costs( flags_update: &mut G, storage_costs: &StorageCost, old_value: &[u8], new_value: &mut Vec, grove_version: &GroveVersion, -) -> Result<(bool, Option), MerkError> +) -> Result<(ElementFlagsUpdate, Option), MerkError> where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, { // todo: change the flags without full deserialization let old_element = Element::deserialize(old_value, grove_version) @@ -93,16 +129,16 @@ where .map_err(|e| MerkError::ClientCorruptionError(e.to_string()))?; let maybe_new_flags = new_element.get_flags_mut(); match maybe_new_flags { - None => Ok((false, None)), + None => Ok((ElementFlagsUpdate::Unchanged, None)), Some(new_flags) => { - let changed = + let update = (flags_update)(storage_costs, maybe_old_flags, new_flags).map_err(|e| match e { Error::JustInTimeElementFlagsClientError(_) => { MerkError::ClientCorruptionError(e.to_string()) } _ => MerkError::ClientCorruptionError("non client error".to_string()), })?; - if changed { + if update != ElementFlagsUpdate::Unchanged { let flags_len = new_flags.len() as u32; new_value.clone_from( &new_element @@ -143,14 +179,17 @@ where + flags_len + flags_len.required_space() as u32 + wrapper_overhead; - Ok((true, Some(LayeredValueDefinedCost(tree_value_cost)))) + Ok((update, Some(LayeredValueDefinedCost(tree_value_cost)))) } Element::SumItem(..) => { let sum_item_value_cost = SUM_ITEM_COST_SIZE + flags_len + flags_len.required_space() as u32 + wrapper_overhead; - Ok((true, Some(SpecializedValueDefinedCost(sum_item_value_cost)))) + Ok(( + update, + Some(SpecializedValueDefinedCost(sum_item_value_cost)), + )) } Element::ItemWithSumItem(item_value, ..) => { let item_len = item_value.len() as u32; @@ -160,12 +199,15 @@ where + item_len + item_len.required_space() as u32 + wrapper_overhead; - Ok((true, Some(SpecializedValueDefinedCost(sum_item_value_cost)))) + Ok(( + update, + Some(SpecializedValueDefinedCost(sum_item_value_cost)), + )) } - _ => Ok((true, None)), + _ => Ok((update, None)), } } else { - Ok((false, None)) + Ok((ElementFlagsUpdate::Unchanged, None)) } } } @@ -217,7 +259,11 @@ pub(crate) fn predict_provided_value_hash_put( grove_version: &GroveVersion, ) -> Result where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index e5ef4eaf5..78108063a 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -32,6 +32,8 @@ mod just_in_time_value_update; mod options; mod refresh_reference_mode; #[cfg(test)] +mod settle_owner_change_cost_tests; +#[cfg(test)] mod single_deletion_cost_tests; #[cfg(test)] mod single_insert_cost_tests; @@ -61,6 +63,7 @@ use grovedb_costs::{ cost_return_on_error_no_add, storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }, CostResult, CostsExt, OperationCost, @@ -1962,6 +1965,10 @@ struct TreeCacheMerkByPath { /// added and unattributed-removal lanes into `replaced_bytes` — a row /// move is physically a delete plus an insert but logically an update. indexed_mirror_rekey_churn_bytes: u32, + /// The batch's `settle_owner_changes` option: a flags update may settle + /// an owner change, so the hash a same-batch reference commits to must + /// ask the flags update about a sum item too. + settle_owner_changes: bool, } impl fmt::Debug for TreeCacheMerkByPath { @@ -2281,7 +2288,11 @@ where grove_version: &GroveVersion, ) -> CostResult where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -2580,7 +2591,11 @@ where grove_version: &GroveVersion, ) -> CostResult where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -2710,7 +2725,11 @@ where grove_version: &GroveVersion, ) -> CostResult where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -2832,6 +2851,7 @@ where old_element, &old_serialized_element, is_in_sum_tree, + self.settle_owner_changes, flags_update, split_removal_bytes, grove_version, @@ -3082,7 +3102,11 @@ where impl<'db, S, F, F2, G, SR> TreeCache for TreeCacheMerkByPath where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -5210,7 +5234,11 @@ impl GroveDb { Error, > where - F: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + F: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -5551,7 +5579,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, split_removed_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -5607,6 +5635,9 @@ impl GroveDb { cidx_overwrite_cleanup_paths: Default::default(), deleted_tree_actual_types: Default::default(), indexed_mirror_rekey_churn_bytes: 0, + settle_owner_changes: batch_apply_options + .as_ref() + .is_some_and(|options| options.settle_owner_changes), }, grove_version ) @@ -5638,7 +5669,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, split_removed_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -6719,15 +6750,20 @@ impl GroveDb { /// predicts the bytes a flagged item will store (its referrers commit to /// them) by consulting the same callbacks before the apply, and refuses /// the batch if the apply stores anything else. - pub fn apply_batch_with_element_flags_update( + /// + /// The flags callback answers a `bool` (whether it rewrote the flags) or + /// an [`ElementFlagsUpdate`]; `SettleOwnerChange` is accepted only when + /// the options set + /// [`settle_owner_changes`](BatchApplyOptions::settle_owner_changes). + pub fn apply_batch_with_element_flags_update>( &self, ops: Vec, batch_apply_options: Option, - mut update_element_flags_function: impl FnMut( + update_element_flags_function: impl FnMut( &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, mut split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -6746,6 +6782,12 @@ impl GroveDb { .apply_batch .apply_batch_with_element_flags_update ); + let mut update_element_flags_function = just_in_time_value_update::batch_flags_update( + update_element_flags_function, + batch_apply_options + .as_ref() + .is_some_and(|options| options.settle_owner_changes), + ); let _storage_removal_version_guard = grovedb_costs::storage_cost::removal::use_basic_sectioned_removal_addition_version( grove_version @@ -7207,15 +7249,18 @@ impl GroveDb { /// The initial segment pauses at `batch_pause_height` (default: 1). /// Cross-segment safety checks are always enforced, even when /// `disable_operation_consistency_check` skips per-segment validation. - pub fn apply_partial_batch_with_element_flags_update( + /// + /// The flags callback is answered as in + /// [`Self::apply_batch_with_element_flags_update`]. + pub fn apply_partial_batch_with_element_flags_update>( &self, ops: Vec, batch_apply_options: Option, - mut update_element_flags_function: impl FnMut( + update_element_flags_function: impl FnMut( &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, mut split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -7238,6 +7283,12 @@ impl GroveDb { .apply_batch .apply_partial_batch_with_element_flags_update ); + let mut update_element_flags_function = just_in_time_value_update::batch_flags_update( + update_element_flags_function, + batch_apply_options + .as_ref() + .is_some_and(|options| options.settle_owner_changes), + ); let _storage_removal_version_guard = grovedb_costs::storage_cost::removal::use_basic_sectioned_removal_addition_version( grove_version @@ -7973,7 +8024,11 @@ impl GroveDb { #[cfg(feature = "estimated_costs")] /// Returns the estimated average or worst case cost for an entire batch of /// ops - pub fn estimated_case_operations_for_batch( + /// + /// With [`settle_owner_changes`](BatchApplyOptions::settle_owner_changes) + /// set, every write that may settle an owner change is charged at least + /// the bytes its element adds when inserted. + pub fn estimated_case_operations_for_batch>( estimated_costs_type: EstimatedCostsType, ops: Vec, batch_apply_options: Option, @@ -7981,7 +8036,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -7999,6 +8054,12 @@ impl GroveDb { .apply_batch .estimated_case_operations_for_batch ); + let update_element_flags_function = just_in_time_value_update::batch_flags_update( + update_element_flags_function, + batch_apply_options + .as_ref() + .is_some_and(|options| options.settle_owner_changes), + ); let mut cost = OperationCost::default(); if ops.is_empty() { @@ -8239,6 +8300,7 @@ mod tests { disable_operation_consistency_check: true, base_root_storage_is_free: true, batch_pause_height: None, + settle_owner_changes: false, }), None, grove_version @@ -8844,6 +8906,7 @@ mod tests { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, + settle_owner_changes: false, }), None, grove_version @@ -8885,6 +8948,7 @@ mod tests { validate_insertion_does_not_override: true, base_root_storage_is_free: true, batch_pause_height: None, + settle_owner_changes: false, }), None, grove_version @@ -8918,6 +8982,7 @@ mod tests { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, + settle_owner_changes: false, }), None, grove_version diff --git a/grovedb/src/batch/options.rs b/grovedb/src/batch/options.rs index fb88e97f4..51b6ef177 100644 --- a/grovedb/src/batch/options.rs +++ b/grovedb/src/batch/options.rs @@ -78,6 +78,28 @@ pub struct BatchApplyOptions { /// At what height do we want to pause applying batch operations /// Most of the time this should be not set pub batch_pause_height: Option, + /// Settle owner changes: account an update in place whose flags update + /// answers + /// [`ElementFlagsUpdate::SettleOwnerChange`](grovedb_costs::storage_cost::transition::ElementFlagsUpdate::SettleOwnerChange) + /// as the removal of the old element plus the insertion of the new one. + /// + /// The old element's bytes, key included, count as removed and are + /// sectioned by the removal-bytes callback as a deletion sections them; + /// the new element's bytes, key included, count as added, and it keeps + /// the flags the callback leaves it with. A batch that does not set this + /// refuses a flags update that answers `SettleOwnerChange`. + /// + /// The estimators read it too: an estimate with it set charges every + /// write that may settle an owner change (`InsertOrReplace`, `Replace`, + /// `Patch` and a trusted `RefreshReference` of an element with flags) at + /// least the bytes its element adds when inserted, with the referrer + /// entries a backward-references element carries over counted at its + /// declared capacity, so the estimate is never below what the apply + /// records. + /// + /// Off by default, and every cost is then what it was before the option + /// existed. + pub settle_owner_changes: bool, } #[cfg(feature = "minimal")] @@ -89,6 +111,7 @@ impl Default for BatchApplyOptions { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, + settle_owner_changes: false, } } } diff --git a/grovedb/src/batch/settle_owner_change_cost_tests.rs b/grovedb/src/batch/settle_owner_change_cost_tests.rs new file mode 100644 index 000000000..b7bcba881 --- /dev/null +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -0,0 +1,1064 @@ +//! Costs of an update in place that settles an owner change +//! (`BatchApplyOptions::settle_owner_changes`): the old element is removed +//! and its bytes sectioned to its owner as a deletion sections them, and the +//! new element is inserted and charged to the writer. + +#[cfg(feature = "minimal")] +mod tests { + use std::collections::{BTreeMap, HashMap}; + + use grovedb_costs::{ + storage_cost::{ + removal::StorageRemovedBytes::{NoStorageRemoval, SectionedStorageRemoval}, + transition::ElementFlagsUpdate, + }, + OperationCost, + }; + use grovedb_epoch_based_storage_flags::StorageFlags; + use grovedb_merk::{ + element::{ + costs::ElementCostExtensions, get::ElementFetchFromStorageExtensions, + tree_type::ElementTreeTypeExtensions, + }, + estimated_costs::{ + average_case_costs::{ + EstimatedLayerCount::ApproximateElements, + EstimatedLayerInformation, + EstimatedLayerSizes::{AllItems, AllSubtrees}, + EstimatedSumTrees::NoSumTrees, + }, + worst_case_costs::WorstCaseLayerInformation::MaxElementsNumber, + }, + tree::kv::KV, + tree_type::TreeType, + }; + use grovedb_version::version::GroveVersion; + use intmap::IntMap; + + use crate::{ + batch::{ + estimated_costs::EstimatedCostsType::{AverageCaseCostsType, WorstCaseCostsType}, + BatchApplyOptions, KeyInfoPath, QualifiedGroveDbOp, SubelementsDeletionBehavior, + }, + reference_path::ReferencePathType, + tests::{common::EMPTY_PATH, make_empty_grovedb, TempGroveDb}, + BackwardReferences, Element, Error, GroveDb, + }; + + const OLD_OWNER: [u8; 32] = [1; 32]; + const NEW_OWNER: [u8; 32] = [2; 32]; + const KEY: &[u8] = b"key1"; + + fn owned_flags(epoch: u16, owner: [u8; 32]) -> Option> { + Some(StorageFlags::SingleEpochOwned(epoch, owner).to_element_flags()) + } + + fn unowned_flags(epoch: u16) -> Option> { + Some(StorageFlags::SingleEpoch(epoch).to_element_flags()) + } + + fn target_path() -> ReferencePathType { + ReferencePathType::AbsolutePathReference(vec![b"targets".to_vec(), b"target".to_vec()]) + } + + fn long_target_path() -> ReferencePathType { + ReferencePathType::AbsolutePathReference(vec![ + b"targets".to_vec(), + b"target with a much longer key".to_vec(), + ]) + } + + /// How a batch is applied. + #[derive(Clone, Copy, PartialEq)] + enum Mode { + /// Today's flags update, without the option. + Merging, + /// The settling flags update, with the option. + Settling, + } + + fn options(mode: Mode) -> BatchApplyOptions { + BatchApplyOptions { + settle_owner_changes: mode == Mode::Settling, + ..Default::default() + } + } + + fn apply( + db: &TempGroveDb, + ops: Vec, + mode: Mode, + grove_version: &GroveVersion, + ) -> Result { + db.apply_batch_with_element_flags_update( + ops, + Some(options(mode)), + |cost, old_flags, new_flags| { + match mode { + Mode::Merging => StorageFlags::update_element_flags(cost, old_flags, new_flags) + .map(ElementFlagsUpdate::from), + Mode::Settling => StorageFlags::update_element_flags_settling_owner_changes( + cost, old_flags, new_flags, + ), + } + .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) + }, + |flags, removed_key_bytes, removed_value_bytes| { + StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) + .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) + }, + None, + grove_version, + ) + .cost_as_result() + } + + /// A grove holding `old` at `[tree]/key1` next to an unflagged sibling, + /// in a tree of `tree_type`, and a `targets` tree whose items references + /// can point at. + fn grove_with(old: &Element, tree_type: TreeType, grove_version: &GroveVersion) -> TempGroveDb { + let db = make_empty_grovedb(); + db.insert( + EMPTY_PATH, + b"targets", + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the targets tree"); + for target in [b"target".as_slice(), b"target with a much longer key"] { + db.insert( + [b"targets".as_slice()].as_ref(), + target, + Element::new_item(b"target value".to_vec()), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert a target"); + } + let tree = match tree_type { + TreeType::SumTree => Element::empty_sum_tree(), + _ => Element::empty_tree(), + }; + db.insert(EMPTY_PATH, b"tree", tree, None, None, grove_version) + .unwrap() + .expect("expected to insert the tree"); + db.insert( + [b"tree".as_slice()].as_ref(), + b"key0", + Element::new_item(b"sibling".to_vec()), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the sibling"); + db.insert( + [b"tree".as_slice()].as_ref(), + KEY, + old.clone(), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the old element"); + db + } + + fn write_op(element: &Element) -> QualifiedGroveDbOp { + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"tree".to_vec()], + KEY.to_vec(), + element.clone(), + ) + } + + fn delete_op(element: &Element) -> QualifiedGroveDbOp { + match element.tree_type() { + Some(tree_type) => QualifiedGroveDbOp::delete_tree_op( + vec![b"tree".to_vec()], + KEY.to_vec(), + tree_type, + SubelementsDeletionBehavior::Error, + ), + None => QualifiedGroveDbOp::delete_op(vec![b"tree".to_vec()], KEY.to_vec()), + } + } + + fn stored(db: &TempGroveDb, grove_version: &GroveVersion) -> Element { + db.get_raw( + [b"tree".as_slice()].as_ref().into(), + KEY, + None, + grove_version, + ) + .unwrap() + .expect("expected the element") + } + + /// The element at `key1` as its Merk stores it, a backward-references + /// element's referrer list included (public reads strip it). + fn stored_in_merk(db: &TempGroveDb, grove_version: &GroveVersion) -> Element { + let tx = db.start_transaction(); + let merk = db + .open_transactional_merk_at_path( + [b"tree".as_slice()].as_ref().into(), + &tx, + None, + grove_version, + ) + .unwrap() + .expect("expected to open the tree"); + Element::get(&merk, KEY, true, grove_version) + .unwrap() + .expect("expected the element") + } + + fn storage_flags_of(element: &Element) -> StorageFlags { + StorageFlags::from_element_flags_ref(element.get_flags().as_ref().expect("expected flags")) + .expect("expected valid flags") + .expect("expected storage flags") + } + + /// The bytes the node of `element` at `key1` holds besides its key, as + /// the apply sizes them. + fn value_bytes(element: &Element, tree_type: TreeType, grove_version: &GroveVersion) -> u32 { + Element::specialized_costs_for_key_value( + KEY, + &element + .serialize(grove_version) + .expect("expected to serialize"), + tree_type.inner_node_type(), + grove_version, + ) + .expect("expected value bytes") + } + + fn key_bytes() -> u32 { + KV::node_key_byte_cost_size(KEY.len() as u32) + } + + /// The bytes `cost` removes from `owner`, by epoch. + fn removed_from(cost: &OperationCost, owner: [u8; 32]) -> BTreeMap { + match &cost.storage_cost.removed_bytes { + SectionedStorageRemoval(by_owner) => by_owner + .get(&owner) + .map(|by_epoch| { + by_epoch + .iter() + .map(|(epoch, bytes)| (epoch, *bytes)) + .collect() + }) + .unwrap_or_default(), + _ => BTreeMap::new(), + } + } + + /// What a deletion of an element holding `flags` removes from its owner: + /// its key and value bytes, sectioned through its epoch map. + fn deletion_sections(flags: &StorageFlags, value_bytes: u32) -> BTreeMap { + let (key_removal, value_removal) = + flags.split_storage_removed_bytes(key_bytes(), value_bytes); + let mut sections = BTreeMap::new(); + for removal in [key_removal, value_removal] { + if let SectionedStorageRemoval(by_owner) = removal { + let by_epoch: &IntMap = by_owner + .get(flags.owner_id().expect("expected an owner")) + .expect("expected the owner's section"); + for (epoch, bytes) in by_epoch.iter() { + *sections.entry(epoch).or_insert(0) += *bytes; + } + } + } + sections + } + + /// Writes `new` over `old` with the settling update, checks the update + /// was accounted as the deletion of `old` plus the insertion of `new`, + /// and returns the grove and the cost. + fn assert_settles( + old: &Element, + new: &Element, + tree_type: TreeType, + grove_version: &GroveVersion, + ) -> (TempGroveDb, OperationCost) { + // Rewriting `old` over itself replaces its own bytes plus whatever + // the update rewrites around it (its parent nodes and tree element). + let rewrite_db = grove_with(old, tree_type, grove_version); + let rewrite = apply( + &rewrite_db, + vec![write_op(old)], + Mode::Merging, + grove_version, + ) + .expect("expected to rewrite the old element"); + let replaced_around_it = + rewrite.storage_cost.replaced_bytes - value_bytes(old, tree_type, grove_version); + assert_eq!(rewrite.storage_cost.added_bytes, 0); + + let delete_db = grove_with(old, tree_type, grove_version); + let deletion = apply( + &delete_db, + vec![delete_op(old)], + Mode::Merging, + grove_version, + ) + .expect("expected to delete the old element"); + + let db = grove_with(old, tree_type, grove_version); + let cost = apply(&db, vec![write_op(new)], Mode::Settling, grove_version) + .expect("expected the settling update to apply"); + + // the new element is inserted: key and value added, charged to the + // writer + assert_eq!( + cost.storage_cost.added_bytes, + key_bytes() + value_bytes(new, tree_type, grove_version) + ); + // nothing of the element is replaced + assert_eq!(cost.storage_cost.replaced_bytes, replaced_around_it); + // the old element is removed from its owner exactly as a deletion + // removes it + let old_flags = storage_flags_of(old); + assert_eq!( + removed_from(&cost, OLD_OWNER), + deletion_sections(&old_flags, value_bytes(old, tree_type, grove_version)) + ); + assert_eq!( + removed_from(&cost, OLD_OWNER), + removed_from(&deletion, OLD_OWNER) + ); + assert!(removed_from(&cost, NEW_OWNER).is_empty()); + // the new element is stored as written, with fresh flags + assert_eq!(&stored(&db, grove_version), new); + let issues = db + .visualize_verify_grovedb(None, true, false, grove_version) + .expect("expected to verify the grove"); + assert!(issues.is_empty(), "grove issues: {issues:?}"); + (db, cost) + } + + #[test] + fn owner_change_on_growth_settles() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 60], owned_flags(2, NEW_OWNER)); + assert_settles(&old, &new, TreeType::NormalTree, grove_version); + } + + #[test] + fn owner_change_on_shrink_settles() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 60], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 5], owned_flags(2, NEW_OWNER)); + assert_settles(&old, &new, TreeType::NormalTree, grove_version); + } + + #[test] + fn owner_change_at_the_same_size_settles() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 20], owned_flags(2, NEW_OWNER)); + assert_eq!( + value_bytes(&old, TreeType::NormalTree, grove_version), + value_bytes(&new, TreeType::NormalTree, grove_version) + ); + assert_settles(&old, &new, TreeType::NormalTree, grove_version); + } + + /// An owner change in the old element's own epoch settles too: the old + /// owner gets back every byte they paid for. + #[test] + fn owner_change_in_the_same_epoch_settles() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 30], owned_flags(0, NEW_OWNER)); + assert_settles(&old, &new, TreeType::NormalTree, grove_version); + } + + #[test] + fn owner_change_of_multi_epoch_flags_refunds_every_epoch() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let db = grove_with(&old, TreeType::NormalTree, grove_version); + // the old owner grows it in epoch 1 and again in epoch 2: the same + // owner merges as before + for (epoch, len) in [(1, 50), (2, 90)] { + apply( + &db, + vec![write_op(&Element::new_item_with_flags( + vec![7; len], + owned_flags(epoch, OLD_OWNER), + ))], + Mode::Settling, + grove_version, + ) + .expect("expected the old owner to grow the element"); + } + let grown = stored(&db, grove_version); + let StorageFlags::MultiEpochOwned(0, epochs, OLD_OWNER) = storage_flags_of(&grown) else { + panic!("expected multi epoch flags of the old owner"); + }; + assert_eq!(epochs.keys().copied().collect::>(), vec![1, 2]); + + let new = Element::new_item_with_flags(vec![8; 40], owned_flags(4, NEW_OWNER)); + let (_, cost) = assert_settles(&grown, &new, TreeType::NormalTree, grove_version); + // every epoch the old owner paid in is refunded + assert_eq!( + removed_from(&cost, OLD_OWNER) + .keys() + .copied() + .collect::>(), + vec![0, 1, 2] + ); + } + + /// Owner changes of the other element types: an item with a sum item, + /// a sum item, a reference, a reference with a sum item and a tree. + #[test] + fn owner_changes_of_every_flagged_element_type_settle() { + let grove_version = GroveVersion::latest(); + let cases = [ + ( + Element::new_item_with_sum_item_with_flags( + vec![7; 20], + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_item_with_sum_item_with_flags( + vec![8; 45], + 9, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ( + Element::new_item_with_sum_item_with_flags( + vec![7; 45], + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_item_with_sum_item_with_flags( + vec![8; 3], + -4, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ( + Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), + Element::new_sum_item_with_flags(900, owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ( + Element::new_reference_with_flags(target_path(), owned_flags(0, OLD_OWNER)), + Element::new_reference_with_flags(long_target_path(), owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::new_reference_with_flags(long_target_path(), owned_flags(0, OLD_OWNER)), + Element::new_reference_with_flags(target_path(), owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::new_reference_with_sum_item_with_flags( + target_path(), + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_reference_with_sum_item_with_flags( + long_target_path(), + 7, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ( + Element::empty_tree_with_flags(owned_flags(0, OLD_OWNER)), + Element::empty_tree_with_flags(owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::empty_sum_tree_with_flags(owned_flags(0, OLD_OWNER)), + Element::empty_sum_tree_with_flags(owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ]; + for (old, new, tree_type) in cases { + assert_settles(&old, &new, tree_type, grove_version); + } + } + + /// A backward-references item keeps the referrers registered on the item + /// it replaces, and a settled write adds their entries too. Both + /// estimates cover them through the item's declared capacity. + #[test] + fn estimates_cover_the_referrers_a_settled_item_carries_over() { + let grove_version = GroveVersion::latest(); + let family_item = |value: Vec, flags| { + Element::ItemWithBackwardsReferences( + value, + BackwardReferences::with_max_incoming(4), + flags, + ) + }; + let old = family_item(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = family_item(vec![8; 20], owned_flags(2, NEW_OWNER)); + let db = grove_with(&old, TreeType::NormalTree, grove_version); + for referrer in [b"r1".as_slice(), b"r2", b"r3"] { + db.insert( + [b"tree".as_slice()].as_ref(), + referrer, + Element::new_bidirectional_reference(ReferencePathType::SiblingReference( + KEY.to_vec(), + )), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to register a referrer"); + } + let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) + .expect("expected the settling update to apply"); + let landed = stored_in_merk(&db, grove_version); + assert_eq!( + landed.backward_references().map(|refs| refs.len()), + Some(3), + "the item keeps its referrers" + ); + assert_eq!( + storage_flags_of(&landed), + StorageFlags::SingleEpochOwned(2, NEW_OWNER) + ); + // the referrer entries are part of what the settled apply adds + assert_eq!( + applied.storage_cost.added_bytes, + key_bytes() + value_bytes(&landed, TreeType::NormalTree, grove_version) + ); + assert!( + applied.storage_cost.added_bytes + > key_bytes() + value_bytes(&new, TreeType::NormalTree, grove_version) + ); + for average_case_tree_type in [Some(TreeType::NormalTree), None] { + let estimate = estimate( + vec![write_op(&new)], + Some(options(Mode::Settling)), + average_case_tree_type, + grove_version, + ); + assert!( + estimate.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "estimate {estimate:?} adds less than the settled apply {applied:?}" + ); + } + } + + /// A partial batch settles an owner change as a full batch does, and + /// hands its add-on callback the settled cost. + #[test] + fn a_partial_batch_settles_an_owner_change() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 60], owned_flags(2, NEW_OWNER)); + let (full_db, full_cost) = assert_settles(&old, &new, TreeType::NormalTree, grove_version); + + let db = grove_with(&old, TreeType::NormalTree, grove_version); + let mut initial_cost = None; + let cost = db + .apply_partial_batch_with_element_flags_update( + vec![write_op(&new)], + Some(options(Mode::Settling)), + |cost, old_flags, new_flags| { + StorageFlags::update_element_flags_settling_owner_changes( + cost, old_flags, new_flags, + ) + .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) + }, + |flags, removed_key_bytes, removed_value_bytes| { + StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) + .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) + }, + |cost, _leftover_operations| { + initial_cost = Some(cost.clone()); + Ok(vec![]) + }, + None, + grove_version, + ) + .cost_as_result() + .expect("expected the partial batch to apply"); + let initial_cost = initial_cost.expect("expected the add-on callback to run"); + assert_eq!( + removed_from(&initial_cost, OLD_OWNER), + removed_from(&full_cost, OLD_OWNER) + ); + assert_eq!(cost.storage_cost, full_cost.storage_cost); + assert_eq!(stored(&db, grove_version), new); + assert_eq!( + db.root_hash(None, grove_version).unwrap().unwrap(), + full_db.root_hash(None, grove_version).unwrap().unwrap() + ); + } + + /// Applies `new` over `old` in both modes and checks they agree on + /// everything: cost, stored element and root hash. + fn assert_same_in_both_modes( + old: &Element, + new: &Element, + tree_type: TreeType, + grove_version: &GroveVersion, + ) { + let merging_db = grove_with(old, tree_type, grove_version); + let merging = apply( + &merging_db, + vec![write_op(new)], + Mode::Merging, + grove_version, + ) + .expect("expected the merging update to apply"); + let settling_db = grove_with(old, tree_type, grove_version); + let settling = apply( + &settling_db, + vec![write_op(new)], + Mode::Settling, + grove_version, + ) + .expect("expected the settling update to apply"); + assert_eq!(settling, merging); + assert_eq!( + stored(&settling_db, grove_version), + stored(&merging_db, grove_version) + ); + assert_eq!( + settling_db.root_hash(None, grove_version).unwrap().unwrap(), + merging_db.root_hash(None, grove_version).unwrap().unwrap() + ); + } + + #[test] + fn same_owner_updates_are_unchanged() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + for (len, epoch) in [(60, 2), (5, 2), (20, 2), (60, 0), (5, 0)] { + let new = Element::new_item_with_flags(vec![8; len], owned_flags(epoch, OLD_OWNER)); + assert_same_in_both_modes(&old, &new, TreeType::NormalTree, grove_version); + } + let old = Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)); + let new = Element::new_sum_item_with_flags(900, owned_flags(2, OLD_OWNER)); + assert_same_in_both_modes(&old, &new, TreeType::SumTree, grove_version); + } + + #[test] + fn updates_without_an_owner_on_either_side_are_unchanged() { + let grove_version = GroveVersion::latest(); + let cases = [ + (unowned_flags(0), owned_flags(2, NEW_OWNER)), + (owned_flags(0, OLD_OWNER), unowned_flags(2)), + (unowned_flags(0), unowned_flags(2)), + (None, owned_flags(2, NEW_OWNER)), + ]; + for (old_flags, new_flags) in cases { + for len in [60, 5, 20] { + let old = Element::new_item_with_flags(vec![7; 20], old_flags.clone()); + let new = Element::new_item_with_flags(vec![8; len], new_flags.clone()); + assert_same_in_both_modes(&old, &new, TreeType::NormalTree, grove_version); + } + } + } + + /// Without the option, an owner change is merged into the new owner's + /// flags as it always was: the new owner takes the old epochs and pays + /// only the growth, and the old owner is refunded nothing. + #[test] + fn owner_change_without_the_option_merges_as_before() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 60], owned_flags(2, NEW_OWNER)); + let db = grove_with(&old, TreeType::NormalTree, grove_version); + let cost = apply(&db, vec![write_op(&new)], Mode::Merging, grove_version) + .expect("expected the merging update to apply"); + assert_eq!(cost.storage_cost.removed_bytes, NoStorageRemoval); + let StorageFlags::MultiEpochOwned(0, epochs, NEW_OWNER) = + storage_flags_of(&stored(&db, grove_version)) + else { + panic!("expected the new owner to take the old owner's epochs"); + }; + assert_eq!(epochs.keys().copied().collect::>(), vec![2]); + let stored_element = stored(&db, grove_version); + assert_eq!( + cost.storage_cost.added_bytes, + value_bytes(&stored_element, TreeType::NormalTree, grove_version) + - value_bytes(&old, TreeType::NormalTree, grove_version) + ); + } + + /// The option alone changes nothing: a flags update that never settles + /// is applied exactly as without it. + #[test] + fn the_option_alone_changes_nothing() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + for len in [60, 5, 20] { + let new = Element::new_item_with_flags(vec![8; len], owned_flags(2, NEW_OWNER)); + let mut costs = vec![]; + let mut hashes = vec![]; + for settle_owner_changes in [false, true] { + let db = grove_with(&old, TreeType::NormalTree, grove_version); + costs.push( + db.apply_batch_with_element_flags_update( + vec![write_op(&new)], + Some(BatchApplyOptions { + settle_owner_changes, + ..Default::default() + }), + |cost, old_flags, new_flags| { + StorageFlags::update_element_flags(cost, old_flags, new_flags).map_err( + |e| Error::JustInTimeElementFlagsClientError(e.to_string()), + ) + }, + |flags, removed_key_bytes, removed_value_bytes| { + StorageFlags::split_removal_bytes( + flags, + removed_key_bytes, + removed_value_bytes, + ) + .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) + }, + None, + grove_version, + ) + .cost_as_result() + .expect("expected the update to apply"), + ); + hashes.push(db.root_hash(None, grove_version).unwrap().unwrap()); + } + assert_eq!(costs[0], costs[1]); + assert_eq!(hashes[0], hashes[1]); + } + } + + #[test] + fn a_settling_flags_update_is_refused_without_the_option() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 60], owned_flags(2, NEW_OWNER)); + let db = grove_with(&old, TreeType::NormalTree, grove_version); + let root_hash = db.root_hash(None, grove_version).unwrap().unwrap(); + let error = db + .apply_batch_with_element_flags_update( + vec![write_op(&new)], + None, + |cost, old_flags, new_flags| { + StorageFlags::update_element_flags_settling_owner_changes( + cost, old_flags, new_flags, + ) + .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) + }, + |flags, removed_key_bytes, removed_value_bytes| { + StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) + .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) + }, + None, + grove_version, + ) + .unwrap() + .expect_err("expected the settling answer to be refused"); + assert!( + error.to_string().contains("settle_owner_changes"), + "unexpected error: {error}" + ); + assert_eq!( + db.root_hash(None, grove_version).unwrap().unwrap(), + root_hash + ); + } + + /// A reference written in the same batch commits to the bytes the + /// settled element stores: its new flags, not the old owner's. + #[test] + fn a_reference_in_the_same_batch_commits_to_the_settled_element() { + let grove_version = GroveVersion::latest(); + let cases = [ + ( + Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 20], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 70], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), + Element::new_sum_item_with_flags(900, owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ( + Element::new_item_with_sum_item_with_flags( + vec![7; 20], + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_item_with_sum_item_with_flags( + vec![8; 20], + 9, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ]; + for (old, new, tree_type) in cases { + let db = grove_with(&old, tree_type, grove_version); + apply( + &db, + vec![ + write_op(&new), + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"targets".to_vec()], + b"reference".to_vec(), + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + b"tree".to_vec(), + KEY.to_vec(), + ])), + ), + ], + Mode::Settling, + grove_version, + ) + .expect("expected the batch to apply"); + assert_eq!(stored(&db, grove_version), new); + let issues = db + .visualize_verify_grovedb(None, true, false, grove_version) + .expect("expected to verify the grove"); + assert!(issues.is_empty(), "reference issues: {issues:?}"); + } + } + + fn average_case_layers(tree_type: TreeType) -> HashMap { + let mut paths = HashMap::new(); + paths.insert( + KeyInfoPath(vec![]), + EstimatedLayerInformation { + tree_type: TreeType::NormalTree, + estimated_layer_count: ApproximateElements(2), + estimated_layer_sizes: AllSubtrees(7, NoSumTrees, None), + }, + ); + paths.insert( + KeyInfoPath::from_known_path([b"tree".as_slice()]), + EstimatedLayerInformation { + tree_type, + estimated_layer_count: ApproximateElements(2), + estimated_layer_sizes: AllItems(4, 100, Some(35)), + }, + ); + paths + } + + fn worst_case_layers() -> HashMap< + KeyInfoPath, + grovedb_merk::estimated_costs::worst_case_costs::WorstCaseLayerInformation, + > { + let mut paths = HashMap::new(); + paths.insert(KeyInfoPath(vec![]), MaxElementsNumber(2)); + paths.insert( + KeyInfoPath::from_known_path([b"tree".as_slice()]), + MaxElementsNumber(2), + ); + paths + } + + fn estimate( + ops: Vec, + batch_apply_options: Option, + average_case_tree_type: Option, + grove_version: &GroveVersion, + ) -> OperationCost { + let estimated_costs_type = match average_case_tree_type { + Some(tree_type) => AverageCaseCostsType(average_case_layers(tree_type)), + None => WorstCaseCostsType(worst_case_layers()), + }; + GroveDb::estimated_case_operations_for_batch( + estimated_costs_type, + ops, + batch_apply_options, + |_cost, _old_flags, _new_flags| Ok(false), + |_flags, _removed_key_bytes, _removed_value_bytes| { + Ok((NoStorageRemoval, NoStorageRemoval)) + }, + grove_version, + ) + .cost_as_result() + .expect("expected an estimate") + } + + /// With the option, the average-case and worst-case estimates of every + /// write that may settle an owner change are never below the settled + /// apply; without it they are what they always were. + #[test] + fn estimates_with_the_option_cover_a_settled_owner_change() { + let grove_version = GroveVersion::latest(); + let cases = [ + ( + Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 60], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::new_item_with_flags(vec![7; 60], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 5], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::new_item_with_sum_item_with_flags( + vec![7; 20], + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_item_with_sum_item_with_flags( + vec![8; 45], + 1, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ( + Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), + Element::new_sum_item_with_flags(1, owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ( + Element::new_reference_with_flags(target_path(), owned_flags(0, OLD_OWNER)), + Element::new_reference_with_flags(long_target_path(), owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ( + Element::new_reference_with_sum_item_with_flags( + target_path(), + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_reference_with_sum_item_with_flags( + long_target_path(), + 1, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ( + Element::empty_tree_with_flags(owned_flags(0, OLD_OWNER)), + Element::empty_tree_with_flags(owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ]; + for (old, new, tree_type) in cases { + let db = grove_with(&old, tree_type, grove_version); + let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) + .expect("expected the settling update to apply"); + + let replace = + QualifiedGroveDbOp::replace_op(vec![b"tree".to_vec()], KEY.to_vec(), new.clone()); + let mut ops = vec![write_op(&new), replace]; + if let Element::Item(..) = new { + ops.push(QualifiedGroveDbOp::patch_op( + vec![b"tree".to_vec()], + KEY.to_vec(), + new.clone(), + 0, + )); + } + if let Element::Reference(path, max_hop, flags) = &new { + ops.push(QualifiedGroveDbOp::refresh_reference_op( + vec![b"tree".to_vec()], + KEY.to_vec(), + path.clone(), + *max_hop, + flags.clone(), + false, + true, + )); + } + for op in ops { + for average_case_tree_type in [Some(tree_type), None] { + let settling = estimate( + vec![op.clone()], + Some(options(Mode::Settling)), + average_case_tree_type, + grove_version, + ); + assert!( + settling + .storage_cost + .worse_or_eq_than(&applied.storage_cost), + "estimate {settling:?} of {op:?} is below the settled apply {applied:?}" + ); + assert!( + settling.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "estimate {settling:?} of {op:?} adds less than the settled apply \ + {applied:?}" + ); + // without the option the estimate is what it always was + let merging = estimate( + vec![op.clone()], + Some(options(Mode::Merging)), + average_case_tree_type, + grove_version, + ); + assert_eq!( + merging, + estimate( + vec![op.clone()], + None, + average_case_tree_type, + grove_version + ) + ); + } + } + } + } + + /// A replacement estimated without the option charges no added bytes, + /// so it would be below a settled owner change; the option raises it to + /// the insertion the settled apply records. + #[test] + fn a_replace_estimate_without_the_option_is_below_a_settled_owner_change() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 20], owned_flags(2, NEW_OWNER)); + let db = grove_with(&old, TreeType::NormalTree, grove_version); + let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) + .expect("expected the settling update to apply"); + let replace = + QualifiedGroveDbOp::replace_op(vec![b"tree".to_vec()], KEY.to_vec(), new.clone()); + let merging = estimate( + vec![replace.clone()], + None, + Some(TreeType::NormalTree), + grove_version, + ); + assert_eq!(merging.storage_cost.added_bytes, 0); + assert!(merging.storage_cost.added_bytes < applied.storage_cost.added_bytes); + let settling = estimate( + vec![replace], + Some(options(Mode::Settling)), + Some(TreeType::NormalTree), + grove_version, + ); + assert_eq!( + settling.storage_cost.added_bytes, + applied.storage_cost.added_bytes + ); + assert_eq!( + settling.storage_cost.replaced_bytes, + merging.storage_cost.replaced_bytes + ); + } +} diff --git a/grovedb/src/tests/backward_references_flags_update_tests.rs b/grovedb/src/tests/backward_references_flags_update_tests.rs index 4da6a09d3..9ac0ebca2 100644 --- a/grovedb/src/tests/backward_references_flags_update_tests.rs +++ b/grovedb/src/tests/backward_references_flags_update_tests.rs @@ -317,6 +317,93 @@ fn same_batch_referrers_of_an_updated_item_commit_to_the_landed_bytes() { assert_referrers_bound(&db, &[TEST_LEAF], &[b"n1"], b"world", grove_version); } +/// An update that hands a flagged item to a new owner settles under +/// `settle_owner_changes`: the item lands with the new owner's flags as +/// written, and its referrers, old and same-batch, commit to those bytes. +#[test] +fn settled_owner_change_keeps_referrers_bound() { + let grove_version = GroveVersion::latest(); + let owned_item = |value: &[u8], epoch: u16, owner: [u8; 32]| { + Element::ItemWithBackwardsReferences( + value.to_vec(), + BackwardReferences::with_max_incoming(4), + Some(StorageFlags::SingleEpochOwned(epoch, owner).to_element_flags()), + ) + }; + for value in [ + b"world".as_slice(), + b"hello world, larger".as_slice(), + b"hi".as_slice(), + ] { + let db = make_test_grovedb(grove_version); + db.insert( + &[TEST_LEAF], + b"value", + owned_item(b"hello", 1, [1; 32]), + None, + None, + grove_version, + ) + .unwrap() + .unwrap(); + for (key, target) in [(b"r1", b"value".as_slice()), (b"r2", b"r1".as_slice())] { + db.insert( + &[TEST_LEAF], + key, + sibling_bidi(target), + None, + None, + grove_version, + ) + .unwrap() + .unwrap(); + } + db.apply_batch_with_element_flags_update( + vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec()], + b"value".to_vec(), + owned_item(value, 3, [2; 32]), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![TEST_LEAF.to_vec()], + b"n1".to_vec(), + sibling_bidi(b"value"), + ), + ], + Some(crate::batch::BatchApplyOptions { + settle_owner_changes: true, + ..Default::default() + }), + |cost, old_flags, new_flags| { + StorageFlags::update_element_flags_settling_owner_changes( + cost, old_flags, new_flags, + ) + .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) + }, + |flags, removed_key_bytes, removed_value_bytes| { + StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) + .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) + }, + None, + grove_version, + ) + .unwrap() + .expect("expected the settled owner change to apply"); + assert_eq!( + stored_storage_flags(&db, &[TEST_LEAF], b"value", grove_version), + Some(StorageFlags::SingleEpochOwned(3, [2; 32])) + ); + assert_referrers_bound( + &db, + &[TEST_LEAF], + &[b"r1", b"r2", b"n1"], + value, + grove_version, + ); + } +} + /// Replacing a flagged bidirectional reference with a flagged item keeps /// the reference's referrer and moves it onto the item: the referrer must /// commit to the item as it lands. @@ -983,7 +1070,7 @@ fn failing_callback_rejects_the_batch() { .apply_batch_with_element_flags_update( vec![replace_value_op(b"updated", 3)], None, - |_cost, _old_flags, _new_flags| { + |_cost, _old_flags, _new_flags| -> Result { Err(Error::JustInTimeElementFlagsClientError( "refused".to_owned(), )) diff --git a/merk/benches/merk.rs b/merk/benches/merk.rs index ee83ad3a3..bfc2aee8b 100644 --- a/merk/benches/merk.rs +++ b/merk/benches/merk.rs @@ -29,7 +29,9 @@ //! Merk benches use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; -use grovedb_costs::storage_cost::{removal::StorageRemovedBytes::BasicStorageRemoval, StorageCost}; +use grovedb_costs::storage_cost::{ + removal::StorageRemovedBytes::BasicStorageRemoval, transition::ElementFlagsUpdate, StorageCost, +}; use grovedb_merk::{ proofs, test_utils::{make_batch_rand, make_batch_seq, make_del_batch_rand, TempMerk}, @@ -55,7 +57,7 @@ fn apply_batch_default>( None::<&fn(&[u8], &GroveVersion) -> Option>, &|_old_value, _new_value| Ok(None), &mut |_costs: &StorageCost, _old_value: &Vec, _new_value: &mut Vec| { - Ok((false, None)) + Ok((ElementFlagsUpdate::Unchanged, None)) }, &mut |_key: &Vec, key_bytes_to_remove: u32, value_bytes_to_remove: u32| { Ok(( diff --git a/merk/src/element/delete.rs b/merk/src/element/delete.rs index f43bf8166..fd4d7691a 100644 --- a/merk/src/element/delete.rs +++ b/merk/src/element/delete.rs @@ -1,7 +1,10 @@ //! Delete //! Implements functions in Element for deleting -use grovedb_costs::{storage_cost::removal::StorageRemovedBytes, CostResult, CostsExt}; +use grovedb_costs::{ + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate}, + CostResult, CostsExt, +}; use grovedb_element::Element; use grovedb_storage::StorageContext; use grovedb_version::{check_grovedb_v0_with_cost, version::GroveVersion}; @@ -193,7 +196,7 @@ impl ElementDeleteFromStorageExtensions for Element { }, Some(&Element::value_defined_cost_for_serialized_value), &|_, _| Ok(None), - &mut |_costs, _old_value, _value| Ok((false, None)), + &mut |_costs, _old_value, _value| Ok((ElementFlagsUpdate::Unchanged, None)), sectioned_removal, grove_version, ) diff --git a/merk/src/merk/apply.rs b/merk/src/merk/apply.rs index d0b68b0b9..3bd271c72 100644 --- a/merk/src/merk/apply.rs +++ b/merk/src/merk/apply.rs @@ -3,6 +3,7 @@ use std::cmp::Ordering; use grovedb_costs::{ storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }, CostResult, CostsExt, @@ -81,7 +82,7 @@ where }, None::<&fn(&[u8], &GroveVersion) -> Option>, &|_old_value, _value| Ok(None), - &mut |_costs, _old_value, _value| Ok((false, None)), + &mut |_costs, _old_value, _value| Ok((ElementFlagsUpdate::Unchanged, None)), &mut |_a, key_bytes_to_remove, value_bytes_to_remove| { Ok(( BasicStorageRemoval(key_bytes_to_remove), @@ -145,7 +146,7 @@ where old_specialized_cost, value_defined_cost_fn, &|_, _| Ok(None), - &mut |_costs, _old_value, _value| Ok((false, None)), + &mut |_costs, _old_value, _value| Ok((ElementFlagsUpdate::Unchanged, None)), &mut |_a, key_bytes_to_remove, value_bytes_to_remove| { Ok(( BasicStorageRemoval(key_bytes_to_remove), @@ -175,12 +176,13 @@ where /// &|k, v| Ok(0), /// None::<&fn(&[u8], &GroveVersion) -> Option>, /// &|k, v| Ok(None), - /// &mut |s, v, o| Ok((false, None)), + /// &mut |s, v, o| Ok((ElementFlagsUpdate::Unchanged, None)), /// &mut |s, k, v| Ok((NoStorageRemoval, NoStorageRemoval)), /// grove_version, /// ).unwrap().expect(""); /// /// use grovedb_costs::storage_cost::removal::StorageRemovedBytes::NoStorageRemoval; + /// use grovedb_costs::storage_cost::transition::ElementFlagsUpdate; /// use grovedb_merk::Op; /// use grovedb_merk::tree::kv::ValueDefinedCostType; /// use grovedb_merk::TreeFeatureType::BasicMerkNode; @@ -200,7 +202,7 @@ where /// &|k, v| Ok(0), /// None::<&fn(&[u8], &GroveVersion) -> Option>, /// &|k, v| Ok(None), - /// &mut |s, v, o| Ok((false, None)), + /// &mut |s, v, o| Ok((ElementFlagsUpdate::Unchanged, None)), /// &mut |s, k, v| Ok((NoStorageRemoval, NoStorageRemoval)), /// grove_version, /// ).unwrap().expect(""); @@ -223,7 +225,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -291,12 +293,13 @@ where /// &|k, v| Ok(0), /// None::<&fn(&[u8], &GroveVersion) -> Option>, /// &|k, v| Ok(None), - /// &mut |s, o, v| Ok((false, None)), + /// &mut |s, o, v| Ok((ElementFlagsUpdate::Unchanged, None)), /// &mut |s, k, v| Ok((NoStorageRemoval, NoStorageRemoval)), /// grove_version, /// ).unwrap().expect(""); /// /// use grovedb_costs::storage_cost::removal::StorageRemovedBytes::NoStorageRemoval; + /// use grovedb_costs::storage_cost::transition::ElementFlagsUpdate; /// use grovedb_merk::Op; /// use grovedb_merk::tree::kv::ValueDefinedCostType; /// use grovedb_merk::TreeFeatureType::BasicMerkNode; @@ -315,7 +318,7 @@ where /// &|k, v| Ok(0), /// None::<&fn(&[u8], &GroveVersion) -> Option>, /// &|o, v| Ok(None), - /// &mut |s, o, v| Ok((false, None)), + /// &mut |s, o, v| Ok((ElementFlagsUpdate::Unchanged, None)), /// &mut |s, k, v| Ok((NoStorageRemoval, NoStorageRemoval)), /// grove_version, /// ).unwrap().expect(""); @@ -343,7 +346,7 @@ where &StorageCost, &Vec, &mut Vec, - ) -> Result<(bool, Option), Error>, + ) -> Result<(ElementFlagsUpdate, Option), Error>, R: FnMut(&Vec, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { self.apply_unchecked_with_old_value_observer( @@ -400,7 +403,7 @@ where &StorageCost, &Vec, &mut Vec, - ) -> Result<(bool, Option), Error>, + ) -> Result<(ElementFlagsUpdate, Option), Error>, R: FnMut(&Vec, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, O: FnMut(&[u8], &[u8], OldValueDisposition), { diff --git a/merk/src/test_utils/mod.rs b/merk/src/test_utils/mod.rs index 07e99821c..f42657457 100644 --- a/merk/src/test_utils/mod.rs +++ b/merk/src/test_utils/mod.rs @@ -32,7 +32,9 @@ mod temp_merk; use std::{convert::TryInto, ops::Range}; -use grovedb_costs::storage_cost::removal::StorageRemovedBytes::BasicStorageRemoval; +use grovedb_costs::storage_cost::{ + removal::StorageRemovedBytes::BasicStorageRemoval, transition::ElementFlagsUpdate, +}; use grovedb_path::SubtreePath; use grovedb_storage::{Storage, StorageBatch}; use grovedb_version::version::GroveVersion; @@ -96,7 +98,7 @@ pub fn apply_memonly_unchecked( }, None::<&fn(&[u8], &GroveVersion) -> Option>, &|_, _| Ok(None), - &mut |_, _, _| Ok((false, None)), + &mut |_, _, _| Ok((ElementFlagsUpdate::Unchanged, None)), &mut |_flags, key_bytes_to_remove, value_bytes_to_remove| { Ok(( BasicStorageRemoval(key_bytes_to_remove), @@ -157,7 +159,7 @@ pub fn apply_to_memonly( }, None::<&fn(&[u8], &GroveVersion) -> Option>, &|_, _| Ok(None), - &mut |_, _, _| Ok((false, None)), + &mut |_, _, _| Ok((ElementFlagsUpdate::Unchanged, None)), &mut |_flags, key_bytes_to_remove, value_bytes_to_remove| { Ok(( BasicStorageRemoval(key_bytes_to_remove), diff --git a/merk/src/tree/just_in_time_value_update.rs b/merk/src/tree/just_in_time_value_update.rs index 7e5514328..1c63994fa 100644 --- a/merk/src/tree/just_in_time_value_update.rs +++ b/merk/src/tree/just_in_time_value_update.rs @@ -1,5 +1,7 @@ use grovedb_costs::storage_cost::{ + key_value_cost::KeyValueStorageCost, removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }; @@ -44,7 +46,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -91,7 +93,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -134,27 +136,41 @@ impl TreeNode { storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; } - let (flags_changed, value_defined_cost) = update_tree_value_based_on_costs( + let (flags_update, value_defined_cost) = update_tree_value_based_on_costs( &storage_costs.value_storage_cost, &old_value, self.value_mut_ref(), )?; - if !flags_changed { - break; - } else { - self.inner.kv.value_defined_cost = value_defined_cost; - let after_update_tree_plus_hook_size = - self.value_encoding_length_with_parent_to_child_reference(); - if after_update_tree_plus_hook_size == current_tree_plus_hook_size { - break; + match flags_update { + ElementFlagsUpdate::Unchanged => break, + ElementFlagsUpdate::SettleOwnerChange => { + // The value now holds the new element as the client + // left it, and the update is accounted as the removal + // of the old element plus the insertion of this one. + self.inner.kv.value_defined_cost = value_defined_cost; + self.known_storage_cost = Some(self.settled_owner_change_storage_cost( + &old_value, + old_specialized_cost, + section_removal_bytes, + )?); + self.old_value = Some(self.value_ref().clone()); + return Ok(()); + } + ElementFlagsUpdate::Changed => { + self.inner.kv.value_defined_cost = value_defined_cost; + let after_update_tree_plus_hook_size = + self.value_encoding_length_with_parent_to_child_reference(); + if after_update_tree_plus_hook_size == current_tree_plus_hook_size { + break; + } + // we are calling this with merged flags that are were put in through value + // mut ref + let new_size_and_storage_costs = + self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; + current_tree_plus_hook_size = new_size_and_storage_costs.0; + storage_costs = new_size_and_storage_costs.1; + self.set_value(original_new_value.clone()) } - // we are calling this with merged flags that are were put in through value mut - // ref - let new_size_and_storage_costs = - self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; - current_tree_plus_hook_size = new_size_and_storage_costs.0; - storage_costs = new_size_and_storage_costs.1; - self.set_value(original_new_value.clone()) } if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { return Err(Error::CyclicError( @@ -181,12 +197,51 @@ impl TreeNode { Ok(()) } + + /// The storage an update that settles an owner change records: the old + /// element removed, key included, with the removal sectioned through its + /// flags exactly as a deletion sections it, and the new element as this + /// node now holds it added, key included. + fn settled_owner_change_storage_cost( + &self, + old_value: &Vec, + old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + section_removal_bytes: &mut impl FnMut( + &Vec, + u32, + u32, + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + ) -> Result { + let key_bytes = KV::node_key_byte_cost_size(self.key().len() as u32); + let old_value_bytes = old_specialized_cost(self.key_as_ref(), old_value)?; + let (removed_key_bytes, removed_value_bytes) = + section_removal_bytes(old_value, key_bytes, old_value_bytes)?; + Ok(KeyValueStorageCost { + key_storage_cost: StorageCost { + added_bytes: key_bytes, + replaced_bytes: 0, + removed_bytes: removed_key_bytes, + }, + value_storage_cost: StorageCost { + added_bytes: self.value_encoding_length_with_parent_to_child_reference(), + replaced_bytes: 0, + removed_bytes: removed_value_bytes, + }, + new_node: false, + needs_value_verification: self.inner.kv.value_defined_cost.is_none(), + prepaid: false, + }) + } } #[cfg(all(test, feature = "full"))] mod tests { use grovedb_costs::storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }; use grovedb_element::{BackwardReferences, Element}; @@ -195,7 +250,10 @@ mod tests { use crate::{ merk::NodeType, test_utils::TempMerk, - tree::{kv::ValueDefinedCostType, kv::KV, TreeFeatureType::BasicMerkNode, TreeNode}, + tree::{ + kv::ValueDefinedCostType, kv::KV, TreeFeatureType::BasicMerkNode, TreeNode, + TreeNodeInner, NULL_HASH, + }, Error, Op, }; @@ -220,7 +278,7 @@ mod tests { cost: &StorageCost, _old: &Vec, new: &mut Vec, - ) -> Result<(bool, Option), Error> { + ) -> Result<(ElementFlagsUpdate, Option), Error> { let grove_version = GroveVersion::latest(); let mut element = Element::deserialize(new, grove_version) .map_err(|e| Error::ClientCorruptionError(e.to_string()))?; @@ -235,7 +293,7 @@ mod tests { *new = element .serialize(grove_version) .map_err(|e| Error::ClientCorruptionError(e.to_string()))?; - Ok((true, None)) + Ok((ElementFlagsUpdate::Changed, None)) } fn basic_removal( @@ -322,4 +380,83 @@ mod tests { assert_eq!(predicted, stored); } } + + /// An update whose flags update settles an owner change is accounted as + /// the removal of the old value plus the insertion of the new one: every + /// byte of both, key included, with the old value sectioned by the + /// removal callback exactly as a deletion sections it. + #[test] + fn a_settled_owner_change_removes_the_old_value_and_inserts_the_new_one() { + let grove_version = GroveVersion::latest(); + let key = b"key".to_vec(); + let item = |value: &[u8], flags: u8| { + Element::new_item_with_flags(value.to_vec(), Some(vec![flags])) + .serialize(grove_version) + .unwrap() + }; + for (old, new) in [ + (item(b"old", 1), item(b"a much longer new value", 2)), + (item(b"a much longer old value", 1), item(b"new", 2)), + (item(b"same", 1), item(b"size", 2)), + ] { + let kv = KV::from_fields( + key.clone(), + old.clone(), + NULL_HASH, + NULL_HASH, + BasicMerkNode, + ); + let mut node = TreeNode::new_with_tree_inner(TreeNodeInner { + left: None, + right: None, + kv, + }); + node.inner.kv = node + .inner + .kv + .put_ordinary_value_no_update_of_hashes(new.clone()); + + let mut sectioned = vec![]; + node.just_in_time_tree_node_value_update( + &old_cost, + &no_temp_value, + &mut |_cost, _old, _new| Ok((ElementFlagsUpdate::SettleOwnerChange, None)), + &mut |value, key_bytes, value_bytes| { + sectioned.push((value.clone(), key_bytes, value_bytes)); + basic_removal(value, key_bytes, value_bytes) + }, + ) + .expect("expected the update to settle"); + + let key_bytes = KV::node_key_byte_cost_size(key.len() as u32); + let old_value_bytes = old_cost(&key, &old).unwrap(); + let new_value_bytes = old_cost(&key, &new).unwrap(); + // (a shrink also sections its freed bytes before the flags + // update is asked, as it always did) + assert_eq!( + sectioned.last(), + Some(&(old.clone(), key_bytes, old_value_bytes)) + ); + let storage_cost = node.known_storage_cost.expect("expected a storage cost"); + assert_eq!( + storage_cost.key_storage_cost, + StorageCost { + added_bytes: key_bytes, + replaced_bytes: 0, + removed_bytes: BasicStorageRemoval(key_bytes), + } + ); + assert_eq!( + storage_cost.value_storage_cost, + StorageCost { + added_bytes: new_value_bytes, + replaced_bytes: 0, + removed_bytes: BasicStorageRemoval(old_value_bytes), + } + ); + assert!(!storage_cost.new_node); + assert_eq!(node.inner.kv.value, new); + assert_eq!(node.old_value, Some(new)); + } + } } diff --git a/merk/src/tree/mod.rs b/merk/src/tree/mod.rs index c8d1cab0a..fda8addf1 100644 --- a/merk/src/tree/mod.rs +++ b/merk/src/tree/mod.rs @@ -40,6 +40,7 @@ use grovedb_costs::{ storage_cost::{ key_value_cost::KeyValueStorageCost, removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }, CostContext, CostResult, CostsExt, OperationCost, @@ -1145,7 +1146,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -1209,7 +1210,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -1368,7 +1369,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -1427,7 +1428,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( diff --git a/merk/src/tree/ops.rs b/merk/src/tree/ops.rs index 3c6da105f..3519e6f7f 100644 --- a/merk/src/tree/ops.rs +++ b/merk/src/tree/ops.rs @@ -12,6 +12,7 @@ use grovedb_costs::{ storage_cost::{ key_value_cost::KeyValueStorageCost, removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }, CostContext, CostResult, CostsExt, OperationCost, @@ -221,7 +222,7 @@ where &StorageCost, &Vec, &mut Vec, - ) -> Result<(bool, Option), Error>, + ) -> Result<(ElementFlagsUpdate, Option), Error>, R: FnMut(&Vec, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, O: FnMut(&[u8], &[u8], OldValueDisposition), { @@ -311,7 +312,7 @@ where &StorageCost, &Vec, &mut Vec, - ) -> Result<(bool, Option), Error>, + ) -> Result<(ElementFlagsUpdate, Option), Error>, R: FnMut(&Vec, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, O: FnMut(&[u8], &[u8], OldValueDisposition), { @@ -498,7 +499,7 @@ where &|_, _| Ok(0), None::<&fn(&[u8], &GroveVersion) -> Option>, &|_, _| Ok(None), - &mut |_, _, _| Ok((false, None)), + &mut |_, _, _| Ok((ElementFlagsUpdate::Unchanged, None)), &mut |_flags, key_bytes_to_remove, value_bytes_to_remove| { Ok(( BasicStorageRemoval(key_bytes_to_remove), @@ -533,7 +534,7 @@ where &StorageCost, &Vec, &mut Vec, - ) -> Result<(bool, Option), Error>, + ) -> Result<(ElementFlagsUpdate, Option), Error>, R: FnMut(&Vec, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, O: FnMut(&[u8], &[u8], OldValueDisposition), { @@ -925,7 +926,7 @@ where &StorageCost, &Vec, &mut Vec, - ) -> Result<(bool, Option), Error>, + ) -> Result<(ElementFlagsUpdate, Option), Error>, V: Fn(&[u8], &GroveVersion) -> Option, R: FnMut(&Vec, u32, u32) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, O: FnMut(&[u8], &[u8], OldValueDisposition), @@ -1252,7 +1253,7 @@ mod test { &|_, _| Ok(0), None::<&fn(&[u8], &GroveVersion) -> Option>, &|_, _| Ok(None), - &mut |_, _, _| Ok((false, None)), + &mut |_, _, _| Ok((ElementFlagsUpdate::Unchanged, None)), &mut |_flags, key_bytes_to_remove, value_bytes_to_remove| { Ok(( BasicStorageRemoval(key_bytes_to_remove), diff --git a/merk/src/tree/put_value/mod.rs b/merk/src/tree/put_value/mod.rs index 1f81e3377..38a681cb3 100644 --- a/merk/src/tree/put_value/mod.rs +++ b/merk/src/tree/put_value/mod.rs @@ -22,7 +22,10 @@ mod v0; mod v1; -use grovedb_costs::{storage_cost::removal::StorageRemovedBytes, CostResult, CostsExt}; +use grovedb_costs::{ + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate}, + CostResult, CostsExt, +}; use grovedb_version::{error::GroveVersionError, version::GroveVersion}; use crate::{ @@ -49,7 +52,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -109,7 +112,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( diff --git a/merk/src/tree/put_value/v0.rs b/merk/src/tree/put_value/v0.rs index a77491231..3eeb16531 100644 --- a/merk/src/tree/put_value/v0.rs +++ b/merk/src/tree/put_value/v0.rs @@ -5,7 +5,7 @@ use grovedb_costs::{ cost_return_on_error_no_add, - storage_cost::{removal::StorageRemovedBytes, StorageCost}, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; @@ -31,7 +31,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -84,7 +84,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( diff --git a/merk/src/tree/put_value/v1.rs b/merk/src/tree/put_value/v1.rs index 90c431d34..e3834d258 100644 --- a/merk/src/tree/put_value/v1.rs +++ b/merk/src/tree/put_value/v1.rs @@ -6,7 +6,7 @@ use grovedb_costs::{ cost_return_on_error_no_add, - storage_cost::{removal::StorageRemovedBytes, StorageCost}, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; @@ -32,7 +32,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -85,7 +85,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( diff --git a/merk/src/tree/walk/mod.rs b/merk/src/tree/walk/mod.rs index e125ed48e..5f44fba3b 100644 --- a/merk/src/tree/walk/mod.rs +++ b/merk/src/tree/walk/mod.rs @@ -11,7 +11,7 @@ pub use fetch::Fetch; use grovedb_costs::{cost_return_on_error, CostResult, CostsExt, OperationCost}; use grovedb_costs::{ cost_return_on_error_no_add, - storage_cost::{removal::StorageRemovedBytes, StorageCost}, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, }; use grovedb_version::version::GroveVersion; #[cfg(feature = "minimal")] @@ -234,7 +234,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -281,7 +281,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -327,7 +327,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -377,7 +377,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -427,7 +427,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -478,7 +478,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( From d1c771eb15c2db5aaa2000cd22c95cc09997007d Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 2 Oct 2026 02:55:12 +0700 Subject: [PATCH 02/10] fix(batch): review fixes for settling storage owner changes Same-batch reference to a sum item (GROVE_V4, apply_batch.same_batch_reference_target_prediction): a reference written or refreshed in the batch that updates its sum-item target now predicts the stored bytes through Merk's own just-in-time value update, with the put the apply performs. The legacy prediction assumed a sum item keeps its stored flags, so a Drive index reference refreshed alongside an ItemWithSumItem updated in a later epoch went stale. GROVE_V1..V3 keep it. Unchanged flags of another length (GROVE_V4, merk_versions.tree.just_in_time_value_update): a replacement whose flags update answers Unchanged is re-measured from the bytes it stores when their size differs from the old-flags measurement, instead of failing the commit with a storage cost mismatch. The stored value's cost is measured once. GROVE_V1..V3 keep the old measurement. Settle estimates: - carried referrer entries are charged at the largest entry registration admits in the average case too - the InsertTreeWithRootHash a flagged tree write becomes when the batch also writes under it is raised in both estimators - the referrer-entry allowance lives in one helper - the dead carried additions are removed - estimator entry points take the batch options instead of a second bare bool, so their tests are as on develop - the estimate path drops its refusal wrapper, which could never fire The settling flags callback decodes old and new flags once. Tests: - V4 and pinned-V3 coverage for both fixes - the settled-tree and long-path referrer estimates - option-off estimates pinned to 66 figures taken from develop a720f7c1 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 31 +- .../src/update_element_flags.rs | 75 +- .../src/version/grovedb_versions.rs | 15 + grovedb-version/src/version/merk_versions.rs | 16 + grovedb-version/src/version/v1.rs | 6 +- grovedb-version/src/version/v2.rs | 6 +- grovedb-version/src/version/v3.rs | 6 +- grovedb-version/src/version/v4.rs | 9 +- .../estimated_costs/average_case_costs.rs | 110 +-- grovedb/src/batch/estimated_costs/mod.rs | 138 +++- .../batch/estimated_costs/worst_case_costs.rs | 115 +-- .../batch/just_in_time_reference_update.rs | 300 -------- .../just_in_time_reference_update/mod.rs | 136 ++++ .../batch/just_in_time_reference_update/v0.rs | 260 +++++++ .../batch/just_in_time_reference_update/v1.rs | 100 +++ .../src/batch/just_in_time_value_update.rs | 94 ++- grovedb/src/batch/mod.rs | 27 +- grovedb/src/batch/options.rs | 19 +- .../same_batch_reference_prediction_tests.rs | 413 ++++++++++ .../batch/settle_owner_change_cost_tests.rs | 720 +++++++++++++++--- .../mod.rs} | 198 ++--- merk/src/tree/just_in_time_value_update/v0.rs | 137 ++++ merk/src/tree/just_in_time_value_update/v1.rs | 157 ++++ merk/src/tree/mod.rs | 15 +- merk/src/tree/ops.rs | 5 +- merk/src/tree/put_value/mod.rs | 4 + merk/src/tree/put_value/v0.rs | 10 +- merk/src/tree/put_value/v1.rs | 10 +- merk/src/tree/walk/mod.rs | 12 +- 29 files changed, 2433 insertions(+), 711 deletions(-) delete mode 100644 grovedb/src/batch/just_in_time_reference_update.rs create mode 100644 grovedb/src/batch/just_in_time_reference_update/mod.rs create mode 100644 grovedb/src/batch/just_in_time_reference_update/v0.rs create mode 100644 grovedb/src/batch/just_in_time_reference_update/v1.rs create mode 100644 grovedb/src/batch/same_batch_reference_prediction_tests.rs rename merk/src/tree/{just_in_time_value_update.rs => just_in_time_value_update/mod.rs} (68%) create mode 100644 merk/src/tree/just_in_time_value_update/v0.rs create mode 100644 merk/src/tree/just_in_time_value_update/v1.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 31fa40b23..9ffab75ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,10 +31,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `bool`; a batch refuses `SettleOwnerChange` unless the option is set. With the option, the average-case and worst-case batch estimates charge every `InsertOrReplace`, `Replace`, `Patch` and trusted `RefreshReference` of an - element with flags at least the bytes its node adds when inserted (for a + element with flags, and the write of a flagged tree the batch also writes + under, at least the bytes its node adds when inserted (for a backward-references element, with the referrers it carries over counted at - its declared capacity), so an estimate is never below a settled apply. With the option off, every cost, - estimate, stored element and hash is what it was before. + its declared capacity, each at the largest entry registration admits), so + an estimate is never below a settled apply. With the option off, every + cost, estimate, stored element and hash is what it was before. ### Changed - **BREAKING**: `BatchApplyOptions` has a new public field, @@ -52,6 +54,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 an `ElementFlagsUpdate`, so a callback answering a `bool` compiles as before, except one that never returns `Ok`, which now has to name its result type (for example `-> Result`). +- **BREAKING**: `TreeNode::put_value_with_fixed_cost`, + `TreeNode::put_value_with_reference_value_hash_and_value_cost`, + `TreeNode::put_value_with_two_reference_value_hashes_and_value_cost` and + their `Walker` counterparts take a `&GroveVersion`, and + `TreeNode::provided_value_hash_put_final_value` takes the value-defined + cost the put stamps on the node and a `&GroveVersion`. - **BREAKING**: `delete_operation_for_delete_internal` and `delete_operations_for_delete_up_tree_while_empty` take the operations already pending in the batch as any `impl PendingOperations` in place of a @@ -92,6 +100,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 an absent path. `AxisKeys::empty_for_axis` is new. (#965) ### Fixed +- A reference written in a batch that also updates its target item, such as + a Drive index reference refreshed alongside its document, committed to + bytes the update never stores when the target was a sum item (`SumItem`, + `ItemWithSumItem`) whose storage flags the flags update merges: an owned + item updated in a later epoch, or an unowned one gaining an owner. The + prediction assumed a sum item keeps its stored flags, while the apply + stores the merged ones, so the grove no longer verified. From GROVE_V4 + (`apply_batch.same_batch_reference_target_prediction`) the prediction runs + Merk's own just-in-time value update, so the reference commits to exactly + the stored bytes; GROVE_V1..V3 keep the old prediction. +- A replacement whose flags update answered `Unchanged` was charged as if the + new value carried the old value's flags. When its own flags had a different + length (a write with longer, shorter or no flags without a flags update, + or an unowned item gaining an owner as it grows) the commit failed with a + storage cost mismatch. From GROVE_V4 + (`merk_versions.tree.just_in_time_value_update`) the replacement is + measured from the bytes it stores; GROVE_V1..V3 keep the old measurement. - From `GROVE_V4`, `delete_operation_for_delete_internal`, and the up-tree builders through it, no longer count a pending `DeleteTree` with `SubelementsDeletionBehavior::Skip` as removing its child when deciding diff --git a/grovedb-epoch-based-storage-flags/src/update_element_flags.rs b/grovedb-epoch-based-storage-flags/src/update_element_flags.rs index 8982cce46..d4dc4c934 100644 --- a/grovedb-epoch-based-storage-flags/src/update_element_flags.rs +++ b/grovedb-epoch-based-storage-flags/src/update_element_flags.rs @@ -25,36 +25,37 @@ impl StorageFlags { old_flags: Option, new_flags: &mut ElementFlags, ) -> Result { - if Self::update_changes_owner(cost, old_flags.as_ref(), new_flags) { - return Ok(ElementFlagsUpdate::SettleOwnerChange); - } - Self::update_element_flags(cost, old_flags, new_flags).map(ElementFlagsUpdate::from) - } - - /// Whether `cost` updates an element in place and the old and new flags - /// both parse and name different owners. Flags that do not parse are left - /// to [`Self::update_element_flags`], which reports them. - fn update_changes_owner( - cost: &StorageCost, - old_flags: Option<&ElementFlags>, - new_flags: &ElementFlags, - ) -> bool { + // Without old flags there is no old owner, and + // `update_element_flags` leaves the new flags as written. + let Some(old_flags) = old_flags else { + return Ok(ElementFlagsUpdate::Unchanged); + }; + let (maybe_old_storage_flags, new_storage_flags) = + Self::decode_update_flags(&old_flags, new_flags)?; let is_update_in_place = matches!( cost.transition_type(), OperationStorageTransitionType::OperationUpdateBiggerSize | OperationStorageTransitionType::OperationUpdateSmallerSize | OperationStorageTransitionType::OperationUpdateSameSize ); - let owner_of = |flags: &ElementFlags| { - StorageFlags::from_element_flags_ref(flags) - .ok() - .flatten() - .and_then(|storage_flags| storage_flags.owner_id().copied()) - }; - match (old_flags.and_then(owner_of), owner_of(new_flags)) { - (Some(old_owner), Some(new_owner)) => is_update_in_place && old_owner != new_owner, - _ => false, + if is_update_in_place + && let (Some(old_owner), Some(new_owner)) = ( + maybe_old_storage_flags + .as_ref() + .and_then(|flags| flags.owner_id()), + new_storage_flags.owner_id(), + ) + && old_owner != new_owner + { + return Ok(ElementFlagsUpdate::SettleOwnerChange); } + Self::update_decoded_element_flags( + cost, + maybe_old_storage_flags, + new_storage_flags, + new_flags, + ) + .map(ElementFlagsUpdate::from) } pub fn update_element_flags( @@ -66,11 +67,26 @@ impl StorageFlags { let Some(old_flags) = old_flags else { return Ok(false); }; + let (maybe_old_storage_flags, new_storage_flags) = + Self::decode_update_flags(&old_flags, new_flags)?; + Self::update_decoded_element_flags( + cost, + maybe_old_storage_flags, + new_storage_flags, + new_flags, + ) + } + /// The old and new flags of an update, decoded as + /// [`Self::update_element_flags`] requires them. + fn decode_update_flags( + old_flags: &ElementFlags, + new_flags: &ElementFlags, + ) -> Result<(Option, StorageFlags), StorageFlagsError> { // This could be none only because the old element didn't exist // If they were empty we get an error let maybe_old_storage_flags = - StorageFlags::from_element_flags_ref(&old_flags).map_err(|mut e| { + StorageFlags::from_element_flags_ref(old_flags).map_err(|mut e| { e.add_info("drive did not understand flags of old item being updated"); e })?; @@ -82,6 +98,17 @@ impl StorageFlags { .ok_or(StorageFlagsError::RemovingFlagsError( "removing flags from an item with flags is not allowed".to_string(), ))?; + Ok((maybe_old_storage_flags, new_storage_flags)) + } + + /// [`Self::update_element_flags`] on flags [`Self::decode_update_flags`] + /// decoded. + fn update_decoded_element_flags( + cost: &StorageCost, + maybe_old_storage_flags: Option, + new_storage_flags: StorageFlags, + new_flags: &mut ElementFlags, + ) -> Result { let old_storage_flags = maybe_old_storage_flags .clone() diff --git a/grovedb-version/src/version/grovedb_versions.rs b/grovedb-version/src/version/grovedb_versions.rs index 5b3a8ca3d..fbafce562 100644 --- a/grovedb-version/src/version/grovedb_versions.rs +++ b/grovedb-version/src/version/grovedb_versions.rs @@ -249,6 +249,21 @@ pub struct GroveDBApplyBatchVersions { /// are versioned by their own `insert_on_transaction` and /// `delete_internal_on_transaction` slots. pub backward_references_maintenance: FeatureVersion, + /// How a reference written in a batch predicts the value hash of a + /// target item the same batch is still to update. + /// + /// - `0` (V1..V3): a hand-written copy of Merk's just-in-time value + /// update. For a sum item it assumes the new element keeps the stored + /// flags and does not consult the flags update, while the apply stores + /// the flags the flags update leaves, so when those differ (an owned + /// item updated in a later epoch, an unowned one gaining an owner) the + /// reference commits to bytes that are never stored. + /// - `1` (V4+): the prediction runs Merk's own just-in-time value update + /// on the stored and new bytes, with the value-defined cost the apply + /// puts the element with, so the reference commits to exactly the + /// bytes the apply stores, for every item kind and every answer of the + /// flags update. + pub same_batch_reference_target_prediction: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/grovedb-version/src/version/merk_versions.rs b/grovedb-version/src/version/merk_versions.rs index 658711975..93f1bed7a 100644 --- a/grovedb-version/src/version/merk_versions.rs +++ b/grovedb-version/src/version/merk_versions.rs @@ -29,6 +29,22 @@ pub struct MerkTreeVersions { /// accounting is unchanged: it is always computed from the stored /// predecessor bytes. pub put_value: FeatureVersion, + /// `TreeNode::just_in_time_tree_node_value_update` — the client-driven + /// update a replacement of a stored value runs (flags carry-over, + /// flags-update callback, removal sectioning). + /// + /// Version 0 measures the replacement with the new value carrying the + /// OLD value's flags, and keeps that measurement when the flags-update + /// callback answers `Unchanged`. The new value is then stored with its + /// own flags, so when those differ in length from the old ones the + /// recorded cost does not match the bytes written and the commit fails + /// with a storage cost mismatch. Consensus-locked for grove v1..v3. + /// + /// Version 1 re-measures the replacement from the bytes it stores when + /// the callback answers `Unchanged` and their size differs from the + /// first measurement. Every update that version 0 accepts is charged + /// the same. + pub just_in_time_value_update: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/grovedb-version/src/version/v1.rs b/grovedb-version/src/version/v1.rs index da3fd8741..86b9a3339 100644 --- a/grovedb-version/src/version/v1.rs +++ b/grovedb-version/src/version/v1.rs @@ -44,6 +44,7 @@ pub const GROVE_V1: GroveVersion = GroveVersion { add_on_op_collision: 0, non_merk_parent_keyed_ops_rejection: 0, backward_references_maintenance: 0, + same_batch_reference_target_prediction: 0, }, element: GroveDBElementMethodVersions { delete: 0, @@ -288,7 +289,10 @@ pub const GROVE_V1: GroveVersion = GroveVersion { }, // Ordinary replacements keep the loaded `value_defined_cost` // metadata (issue #908); consensus-locked, bumped in v4. - tree: MerkTreeVersions { put_value: 0 }, + tree: MerkTreeVersions { + put_value: 0, + just_in_time_value_update: 0, + }, }, // MMR hash charges: the shipped accounting, which billed the // storage reads each operation performed but not the blake3 merges diff --git a/grovedb-version/src/version/v2.rs b/grovedb-version/src/version/v2.rs index 19939e144..ad9d30d50 100644 --- a/grovedb-version/src/version/v2.rs +++ b/grovedb-version/src/version/v2.rs @@ -44,6 +44,7 @@ pub const GROVE_V2: GroveVersion = GroveVersion { add_on_op_collision: 0, non_merk_parent_keyed_ops_rejection: 0, backward_references_maintenance: 0, + same_batch_reference_target_prediction: 0, }, element: GroveDBElementMethodVersions { delete: 0, @@ -287,7 +288,10 @@ pub const GROVE_V2: GroveVersion = GroveVersion { }, // Ordinary replacements keep the loaded `value_defined_cost` // metadata (issue #908); consensus-locked, bumped in v4. - tree: MerkTreeVersions { put_value: 0 }, + tree: MerkTreeVersions { + put_value: 0, + just_in_time_value_update: 0, + }, }, // MMR hash charges: the shipped accounting, which billed the // storage reads each operation performed but not the blake3 merges diff --git a/grovedb-version/src/version/v3.rs b/grovedb-version/src/version/v3.rs index 63aabb6f6..7445c4c31 100644 --- a/grovedb-version/src/version/v3.rs +++ b/grovedb-version/src/version/v3.rs @@ -44,6 +44,7 @@ pub const GROVE_V3: GroveVersion = GroveVersion { add_on_op_collision: 0, non_merk_parent_keyed_ops_rejection: 0, backward_references_maintenance: 0, + same_batch_reference_target_prediction: 0, }, element: GroveDBElementMethodVersions { delete: 0, @@ -305,7 +306,10 @@ pub const GROVE_V3: GroveVersion = GroveVersion { }, // Ordinary replacements keep the loaded `value_defined_cost` // metadata (issue #908); consensus-locked, bumped in v4. - tree: MerkTreeVersions { put_value: 0 }, + tree: MerkTreeVersions { + put_value: 0, + just_in_time_value_update: 0, + }, }, // MMR hash charges: the shipped accounting, which billed the // storage reads each operation performed but not the blake3 merges diff --git a/grovedb-version/src/version/v4.rs b/grovedb-version/src/version/v4.rs index 162ad6db6..9cbaae516 100644 --- a/grovedb-version/src/version/v4.rs +++ b/grovedb-version/src/version/v4.rs @@ -366,6 +366,10 @@ pub const GROVE_V4: GroveVersion = GroveVersion { // participant gate, per-op BackwardsReferences declarations, and // DontCheck required for DropFlat. backward_references_maintenance: 1, + // v1: a reference written in the same batch as its target's + // update predicts the target's stored bytes through Merk's own + // just-in-time value update. + same_batch_reference_target_prediction: 1, }, element: GroveDBElementMethodVersions { delete: 0, @@ -665,7 +669,10 @@ pub const GROVE_V4: GroveVersion = GroveVersion { }, // Bumped 0 -> 1: ordinary (Item / Reference) replacements of a // specialized value are charged from their own bytes (issue #908). - tree: MerkTreeVersions { put_value: 1 }, + tree: MerkTreeVersions { + put_value: 1, + just_in_time_value_update: 1, + }, }, // MMR hash charges: one hash per blake3 merge actually computed — // `push` per collapsed peak, `get_root` and `gen_proof` per peak diff --git a/grovedb/src/batch/estimated_costs/average_case_costs.rs b/grovedb/src/batch/estimated_costs/average_case_costs.rs index f4c34d3c1..639e68db7 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -45,10 +45,37 @@ use crate::{BackwardsReferences, Element}; #[cfg(feature = "minimal")] impl GroveOp { + /// [`Self::average_case_cost_with_options`] with the default batch + /// options. + #[cfg(test)] + #[allow(clippy::too_many_arguments)] + fn average_case_cost( + &self, + path: &KeyInfoPath, + key: &KeyInfo, + layer_element_estimates: &EstimatedLayerInformation, + append_tree_chunk_power: Option, + backwards_references: BackwardsReferences, + propagate: bool, + grove_version: &GroveVersion, + ) -> CostResult<(), Error> { + self.average_case_cost_with_options( + path, + key, + layer_element_estimates, + append_tree_chunk_power, + backwards_references, + propagate, + &BatchApplyOptions::default(), + grove_version, + ) + } + /// Get the estimated average case cost of the op. Calls a lower level /// function to calculate the estimate based on the type of op. Returns /// CostResult. - fn average_case_cost( + #[allow(clippy::too_many_arguments)] + fn average_case_cost_with_options( &self, // The op's own path: sizes the inverted-registration growth bound // (every `invert()` output is built from the origin's qualified @@ -69,31 +96,20 @@ impl GroveOp { // it declares `Check`. backwards_references: BackwardsReferences, propagate: bool, - // The batch's `settle_owner_changes` option: a write that may settle - // an owner change is charged as the insertion it then records too. - settle_owner_changes: bool, + // The batch's options: with `settle_owner_changes`, a write that may + // settle an owner change is charged as the insertion it then records + // too. + batch_apply_options: &BatchApplyOptions, grove_version: &GroveVersion, ) -> CostResult<(), Error> { let in_tree_type = layer_element_estimates.tree_type; - // A referrer entry of the typical shape: an inverted path built from - // a qualified origin like this op's own (its path segments plus its - // key; an absolute inversion serializes them all), the cascade flag, - // and framing. - let referrer_entry_bound = path - .0 - .iter() - .map(|segment| 4 + segment.max_length() as u32) - .sum::() - .saturating_add(4 + key.max_length() as u32) - .saturating_add(16); let settling = |base: CostResult<(), Error>, element: &Element| { - if settle_owner_changes { + if batch_apply_options.settle_owner_changes { super::with_settled_owner_change( base, key, element, in_tree_type.inner_node_type(), - referrer_entry_bound, grove_version, ) } else { @@ -124,7 +140,7 @@ impl GroveOp { // The registration entry appended to the target, whose // referrer is this op itself. Some(super::BackwardReferencesFanOut::average_reference( - referrer_entry_bound, + super::backward_reference_entry_bound(path, key), )) } // A backward-references ITEM write carries its own referrer @@ -228,20 +244,38 @@ impl GroveOp { not_summed, not_counted_or_summed, .. - } => GroveDb::average_case_merk_insert_tree( - key, - flags, - aggregate_data.parent_tree_type(), - in_tree_type, + } => { // Account for the wrapper byte if the op rebuilds the // tree as `NonCounted(...)`, `NotSummed(...)`, or // `NotCountedOrSummed(...)`. They share the same +1 // discriminant overhead and are mutually exclusive on // the rebuilt element. - super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed), - propagate_if_input(), - grove_version, - ), + let wrapper_overhead = + super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed); + let insert_cost = GroveDb::average_case_merk_insert_tree( + key, + flags, + aggregate_data.parent_tree_type(), + in_tree_type, + wrapper_overhead, + propagate_if_input(), + grove_version, + ); + // The write of a tree the batch also writes under: it may + // settle an owner change as a write of its element does. + if batch_apply_options.settle_owner_changes { + super::with_settled_tree_owner_change( + insert_cost, + key, + flags, + aggregate_data.parent_tree_type(), + wrapper_overhead, + in_tree_type.inner_node_type(), + ) + } else { + insert_cost + } + } GroveOp::InsertOrReplace { element } | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( settling( @@ -1187,14 +1221,14 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { } cost_return_on_error!( &mut cost, - op.average_case_cost( + op.average_case_cost_with_options( path, &key, layer_element_estimates, append_tree_chunk_power, op.backwards_references(), false, - batch_apply_options.settle_owner_changes, + batch_apply_options, grove_version ) ); @@ -2309,7 +2343,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version ) .cost_as_result() @@ -2322,7 +2355,6 @@ mod tests { Some(10), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2378,7 +2410,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2428,7 +2459,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2473,7 +2503,6 @@ mod tests { Some(4), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2510,7 +2539,6 @@ mod tests { Some(4), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2526,7 +2554,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version ) .cost_as_result() @@ -2546,7 +2573,6 @@ mod tests { Some(2), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2559,7 +2585,6 @@ mod tests { Some(10), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2615,7 +2640,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2668,7 +2692,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2712,7 +2735,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2770,7 +2792,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2833,7 +2854,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2881,7 +2901,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2902,7 +2921,6 @@ mod tests { None, BackwardsReferences::DontCheck, true, - false, grove_version, ) .cost_as_result() @@ -3566,7 +3584,6 @@ mod tests { None, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -3581,7 +3598,6 @@ mod tests { Some(4), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() diff --git a/grovedb/src/batch/estimated_costs/mod.rs b/grovedb/src/batch/estimated_costs/mod.rs index 2fcb9298d..ecb054c9e 100644 --- a/grovedb/src/batch/estimated_costs/mod.rs +++ b/grovedb/src/batch/estimated_costs/mod.rs @@ -48,17 +48,46 @@ pub(in crate::batch) fn wrapper_overhead_for( pub(in crate::batch) const WORST_CASE_SETTLED_NODE_TYPE: grovedb_merk::merk::NodeType = grovedb_merk::merk::NodeType::BigSumNode; +/// The allowance a registered referrer entry takes per segment of the +/// referrer's inverted path (and for its key). +#[cfg(feature = "minimal")] +const REFERRER_ENTRY_SEGMENT_BYTES: u32 = 4; + +/// The allowance a registered referrer entry takes for its cascade flag and +/// framing. +#[cfg(feature = "minimal")] +const REFERRER_ENTRY_FRAMING_BYTES: u32 = 16; + +/// An upper bound on the registered referrer entry of a referrer at `path` +/// under `key`: an inverted path built from the referrer's qualified origin +/// (its path segments plus its key; an absolute inversion serializes them +/// all), the cascade flag, and framing. +#[cfg(feature = "minimal")] +pub(in crate::batch) fn backward_reference_entry_bound( + path: &crate::batch::KeyInfoPath, + key: &crate::batch::KeyInfo, +) -> u32 { + use grovedb_storage::worst_case_costs::WorstKeyLength; + + path.0 + .iter() + .map(|segment| REFERRER_ENTRY_SEGMENT_BYTES + segment.max_length() as u32) + .sum::() + .saturating_add(REFERRER_ENTRY_SEGMENT_BYTES + key.max_length() as u32) + .saturating_add(REFERRER_ENTRY_FRAMING_BYTES) +} + /// An upper bound on one registered referrer entry of a backward-references -/// element whose referrers may sit anywhere registration admits them: the -/// inverted path of a referrer at the deepest such position -/// (`MAX_BACKWARD_REFERENCES_GROVE_DEPTH` segments plus its key, each of the -/// biggest key size), with the per-segment and framing allowance the -/// registration bound uses. +/// element whose referrers may sit anywhere registration admits them: +/// [`backward_reference_entry_bound`] of a referrer at the deepest such +/// position (`MAX_BACKWARD_REFERENCES_GROVE_DEPTH` segments plus its key, +/// each of the biggest key size). #[cfg(feature = "minimal")] pub(in crate::batch) const WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND: u32 = (crate::bidirectional_references::MAX_BACKWARD_REFERENCES_GROVE_DEPTH as u32 + 1) - * (4 + grovedb_merk::estimated_costs::worst_case_costs::MERK_BIGGEST_KEY_SIZE) - + 16; + * (REFERRER_ENTRY_SEGMENT_BYTES + + grovedb_merk::estimated_costs::worst_case_costs::MERK_BIGGEST_KEY_SIZE) + + REFERRER_ENTRY_FRAMING_BYTES; /// The bytes a write of `element` at a key of `key_len` bytes adds when it /// settles an owner change: the whole node, key and value, sized exactly as @@ -66,14 +95,16 @@ pub(in crate::batch) const WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND: u32 = /// /// A backward-references element lands with the referrers registered on the /// element it replaces in place of the ones it was written with, and the -/// settled apply adds their entries too: they are charged at the element's -/// declared capacity, each at `referrer_entry_bound` bytes. +/// settled apply adds their entries too. Those referrers can sit anywhere +/// registration admits them, so they are charged at the element's declared +/// capacity, each at [`WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND`]. (Only +/// backward-references elements declare a capacity, and none of them has a +/// value-defined cost.) #[cfg(feature = "minimal")] fn settled_owner_change_added_bytes( key_len: u32, element: &crate::Element, node_type: grovedb_merk::merk::NodeType, - referrer_entry_bound: u32, grove_version: &grovedb_version::version::GroveVersion, ) -> Result { use grovedb_merk::{ @@ -84,48 +115,42 @@ fn settled_owner_change_added_bytes( }, }; - let carried_referrer_bytes = element.max_incoming_references().map_or(0, |capacity| { - (capacity as u32).saturating_mul(referrer_entry_bound) - }); - Ok(match element.value_defined_cost(grove_version) { + let raw_value_len = match element.value_defined_cost(grove_version) { Some(LayeredValueDefinedCost(value_cost)) => { - KV::layered_node_byte_cost_size_for_key_and_value_lengths( - key_len, - value_cost.saturating_add(carried_referrer_bytes), - node_type, - ) + return Ok(KV::layered_node_byte_cost_size_for_key_and_value_lengths( + key_len, value_cost, node_type, + )) } - Some(SpecializedValueDefinedCost(value_cost)) => { - KV::node_byte_cost_size_for_key_and_raw_value_lengths( - key_len, - value_cost.saturating_add(carried_referrer_bytes), - node_type, - ) + Some(SpecializedValueDefinedCost(value_cost)) => value_cost, + None => { + let carried_referrer_bytes = element.max_incoming_references().map_or(0, |capacity| { + (capacity as u32).saturating_mul(WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND) + }); + (element.serialized_size(grove_version)? as u32).saturating_add(carried_referrer_bytes) } - None => KV::node_byte_cost_size_for_key_and_raw_value_lengths( - key_len, - (element.serialized_size(grove_version)? as u32).saturating_add(carried_referrer_bytes), - node_type, - ), - }) + }; + Ok(KV::node_byte_cost_size_for_key_and_raw_value_lengths( + key_len, + raw_value_len, + node_type, + )) } /// `base`, the estimate of a write of `element` over a stored element, /// charged for the case that the write settles an owner change /// ([`BatchApplyOptions::settle_owner_changes`](crate::batch::BatchApplyOptions::settle_owner_changes)): /// its added bytes are raised to every byte of the element's node, key -/// included, sized for a tree of `node_type` nodes with each referrer entry -/// a backward-references element may carry over at `referrer_entry_bound` -/// bytes, so the estimate covers both the replacement it models and the -/// insertion a settled owner change records. An element without flags names -/// no owner and never settles, so its estimate is left as it is. +/// included, sized for a tree of `node_type` nodes with the referrer entries +/// a backward-references element may carry over, so the estimate covers both +/// the replacement it models and the insertion a settled owner change +/// records. An element without flags names no owner and never settles, so +/// its estimate is left as it is. #[cfg(feature = "minimal")] pub(in crate::batch) fn with_settled_owner_change( base: grovedb_costs::CostResult<(), crate::Error>, key: &crate::batch::KeyInfo, element: &crate::Element, node_type: grovedb_merk::merk::NodeType, - referrer_entry_bound: u32, grove_version: &grovedb_version::version::GroveVersion, ) -> grovedb_costs::CostResult<(), crate::Error> { use grovedb_costs::CostsExt; @@ -137,7 +162,6 @@ pub(in crate::batch) fn with_settled_owner_change( key.max_length() as u32, element, node_type, - referrer_entry_bound, grove_version, ) { Ok(settled_bytes) => { @@ -149,6 +173,44 @@ pub(in crate::batch) fn with_settled_owner_change( value.wrap_with_cost(cost) } +/// [`with_settled_owner_change`] for the `InsertTreeWithRootHash` that a +/// write of a tree becomes when the batch also writes under it: `base` is +/// raised by the tree element that op writes — a tree of `tree_type` with +/// `flags`, and `wrapper_overhead` wrapper bytes — sized as the apply sizes +/// it in a tree of `node_type` nodes. +#[cfg(feature = "minimal")] +pub(in crate::batch) fn with_settled_tree_owner_change( + base: grovedb_costs::CostResult<(), crate::Error>, + key: &crate::batch::KeyInfo, + flags: &Option, + tree_type: grovedb_merk::tree_type::TreeType, + wrapper_overhead: u32, + node_type: grovedb_merk::merk::NodeType, +) -> grovedb_costs::CostResult<(), crate::Error> { + use grovedb_costs::CostsExt; + use grovedb_merk::{tree::kv::KV, tree_type::CostSize}; + use grovedb_storage::worst_case_costs::WorstKeyLength; + use integer_encoding::VarInt; + + let grovedb_costs::CostContext { value, mut cost } = base; + if value.is_ok() + && let Some(flags) = flags + { + let flags_len = flags.len() as u32; + let value_cost = tree_type.cost_size() + + flags_len + + flags_len.required_space() as u32 + + wrapper_overhead; + let settled_bytes = KV::layered_node_byte_cost_size_for_key_and_value_lengths( + key.max_length() as u32, + value_cost, + node_type, + ); + cost.storage_cost.added_bytes = cost.storage_cost.added_bytes.max(settled_bytes); + } + value.wrap_with_cost(cost) +} + // ── Backward-references fan-out estimation model ──────────────────────── // // Under `BatchApplyOptions::backwards_references` (GROVE_V4+), a diff --git a/grovedb/src/batch/estimated_costs/worst_case_costs.rs b/grovedb/src/batch/estimated_costs/worst_case_costs.rs index 75c6636c3..3d1f68792 100644 --- a/grovedb/src/batch/estimated_costs/worst_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/worst_case_costs.rs @@ -43,7 +43,34 @@ use crate::{BackwardsReferences, Element}; #[cfg(feature = "minimal")] impl GroveOp { + /// [`Self::worst_case_cost_with_options`] with the default batch + /// options. + #[cfg(test)] + #[allow(clippy::too_many_arguments)] fn worst_case_cost( + &self, + path: &KeyInfoPath, + key: &KeyInfo, + in_parent_tree_type: TreeType, + worst_case_layer_element_estimates: &WorstCaseLayerInformation, + backwards_references: BackwardsReferences, + propagate: bool, + grove_version: &GroveVersion, + ) -> CostResult<(), Error> { + self.worst_case_cost_with_options( + path, + key, + in_parent_tree_type, + worst_case_layer_element_estimates, + backwards_references, + propagate, + &BatchApplyOptions::default(), + grove_version, + ) + } + + #[allow(clippy::too_many_arguments)] + fn worst_case_cost_with_options( &self, // The op's own path: sizes the inverted-registration growth bound // (every `invert()` output is built from the origin's qualified @@ -58,9 +85,10 @@ impl GroveOp { // it declares `Check`. backwards_references: BackwardsReferences, propagate: bool, - // The batch's `settle_owner_changes` option: a write that may settle - // an owner change is charged as the insertion it then records too. - settle_owner_changes: bool, + // The batch's options: with `settle_owner_changes`, a write that may + // settle an owner change is charged as the insertion it then records + // too. + batch_apply_options: &BatchApplyOptions, grove_version: &GroveVersion, ) -> CostResult<(), Error> { let propagate_if_input = || { @@ -71,13 +99,12 @@ impl GroveOp { } }; let settling = |base: CostResult<(), Error>, element: &Element| { - if settle_owner_changes { + if batch_apply_options.settle_owner_changes { super::with_settled_owner_change( base, key, element, super::WORST_CASE_SETTLED_NODE_TYPE, - super::WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND, grove_version, ) } else { @@ -97,20 +124,10 @@ impl GroveOp { } match element { Some(Element::BidirectionalReference(..)) => { - // The registration entry appended to the target: an - // inverted path built from the referrer's qualified - // origin (this op's path segments plus its key — an - // absolute inversion serializes them all), the cascade - // flag, and framing. - let origin_bytes: u32 = path - .0 - .iter() - .map(|segment| 4 + segment.max_length() as u32) - .sum::() - .saturating_add(4 + key.max_length() as u32); - let entry_bound = origin_bytes.saturating_add(16); + // The registration entry appended to the target, whose + // referrer is this op itself. Some(super::BackwardReferencesFanOut::worst_reference( - entry_bound, + super::backward_reference_entry_bound(path, key), )) } // A backward-references ITEM write carries its own referrer @@ -213,16 +230,38 @@ impl GroveOp { not_summed, not_counted_or_summed, .. - } => GroveDb::worst_case_merk_insert_tree( - key, - flags, - aggregate_data.parent_tree_type(), - in_parent_tree_type, - // See the comment in the corresponding average-case arm. - super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed), - propagate_if_input(), - grove_version, - ), + } => { + // Account for the wrapper byte if the op rebuilds the + // tree as `NonCounted(...)`, `NotSummed(...)`, or + // `NotCountedOrSummed(...)`. They share the same +1 + // discriminant overhead and are mutually exclusive on + // the rebuilt element. + let wrapper_overhead = + super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed); + let insert_cost = GroveDb::worst_case_merk_insert_tree( + key, + flags, + aggregate_data.parent_tree_type(), + in_parent_tree_type, + wrapper_overhead, + propagate_if_input(), + grove_version, + ); + // The write of a tree the batch also writes under: it may + // settle an owner change as a write of its element does. + if batch_apply_options.settle_owner_changes { + super::with_settled_tree_owner_change( + insert_cost, + key, + flags, + aggregate_data.parent_tree_type(), + wrapper_overhead, + super::WORST_CASE_SETTLED_NODE_TYPE, + ) + } else { + insert_cost + } + } GroveOp::InsertOrReplace { element } | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( settling( @@ -944,14 +983,14 @@ impl TreeCache for WorstCaseTreeCacheKnownPaths { } cost_return_on_error!( &mut cost, - op.worst_case_cost( + op.worst_case_cost_with_options( path, &key, TreeType::NormalTree, worst_case_layer_element_estimates, op.backwards_references(), false, - batch_apply_options.settle_owner_changes, + batch_apply_options, grove_version ) ); @@ -1626,7 +1665,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -1653,7 +1691,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, - false, grove_version, ) .cost_as_result() @@ -1679,7 +1716,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -1705,7 +1741,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -1730,7 +1765,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -1767,7 +1801,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -1796,7 +1829,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, - false, grove_version, ) .cost_as_result() @@ -1821,7 +1853,6 @@ mod tests { &MaxElementsNumber(50), BackwardsReferences::DontCheck, true, - false, grove_version, ) .cost_as_result() @@ -1854,7 +1885,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -1887,7 +1917,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, - false, grove_version, ) .cost_as_result() @@ -1924,7 +1953,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -1981,7 +2009,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2053,7 +2080,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2081,7 +2107,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, true, - false, grove_version, ) .cost_as_result() @@ -2132,7 +2157,6 @@ mod tests { &layer_info, BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() @@ -2193,7 +2217,6 @@ mod tests { &MaxElementsNumber(100), BackwardsReferences::DontCheck, false, - false, grove_version, ) .cost_as_result() diff --git a/grovedb/src/batch/just_in_time_reference_update.rs b/grovedb/src/batch/just_in_time_reference_update.rs deleted file mode 100644 index 107599f90..000000000 --- a/grovedb/src/batch/just_in_time_reference_update.rs +++ /dev/null @@ -1,300 +0,0 @@ -use std::borrow::Cow; - -use grovedb_costs::{ - cost_return_on_error_into_no_add, cost_return_on_error_no_add, - storage_cost::{ - removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, - transition::ElementFlagsUpdate, - StorageCost, - }, - CostResult, CostsExt, OperationCost, -}; -use grovedb_merk::{ - element::costs::ElementCostExtensions, - tree::{kv::KV, value_hash, TreeNode}, - tree_type::TreeType, - CryptoHash, Merk, -}; -use grovedb_storage::StorageContext; -use grovedb_version::version::GroveVersion; - -use crate::{ - batch::{MerkError, TreeCacheMerkByPath}, - Element, ElementFlags, Error, -}; - -impl<'db, S, F, F2> TreeCacheMerkByPath -where - F: FnMut(&[Vec], bool) -> CostResult, Error>, - F2: FnMut( - &[Vec], - Option<&Element>, - ) -> CostResult)>, Error>, - S: StorageContext<'db>, -{ - /// The hash a reference written in this batch commits to for a pending - /// write of a backward-references ITEM at `qualified_path`: the logical - /// (stripped) hash of the bytes the apply finally stores there — the - /// preprocessor's prediction when the caller's flags update may rewrite - /// them, the op's own element otherwise. - pub(crate) fn landed_backward_references_item_value_hash( - &self, - qualified_path: &[Vec], - element: &Element, - grove_version: &GroveVersion, - ) -> CostResult { - let mut cost = OperationCost::default(); - let landed = self - .landed_backward_references_items - .get(qualified_path) - .unwrap_or(element); - let serialized = cost_return_on_error_into_no_add!( - cost, - landed - .stripped_of_backward_references() - .serialize(grove_version) - ); - let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); - Ok(val_hash).wrap_with_cost(cost) - } - - /// The value hash of the bytes a pending write of `new_element` over the - /// stored `old_element` finally stores, after the apply has run the - /// caller's flags update on it, so that a reference written in the same - /// batch commits to them. - /// - /// With `settle_owner_changes` a flags update may settle an owner change, - /// and the apply then stores the new element with the flags the callback - /// leaves it, whatever its size. A sum item is otherwise taken to keep - /// its stored flags without asking the flags update, so in that mode it - /// is asked first. - #[allow(clippy::too_many_arguments)] - pub(crate) fn process_old_element_flags( - key: &[u8], - serialized: &[u8], - new_element: &mut Element, - old_element: Element, - old_serialized_element: &[u8], - in_tree_type: TreeType, - settle_owner_changes: bool, - flags_update: &mut G, - split_removal_bytes: &mut SR, - grove_version: &GroveVersion, - ) -> CostResult - where - G: FnMut( - &StorageCost, - Option, - &mut ElementFlags, - ) -> Result, - SR: FnMut( - &mut ElementFlags, - u32, - u32, - ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, - { - let mut cost = OperationCost::default(); - let client_error = |e: Error| -> Error { - match e { - Error::JustInTimeElementFlagsClientError(_) => { - MerkError::ClientCorruptionError(e.to_string()).into() - } - _ => MerkError::ClientCorruptionError("non client error".to_string()).into(), - } - }; - if old_element.is_sum_item() { - return if new_element.is_sum_item() { - let maybe_old_flags = old_element.get_flags_owned(); - if settle_owner_changes - && maybe_old_flags.is_some() - && new_element.get_flags().is_some() - { - // Merk measures a sum item by its own specialized cost, - // new flags included, against the stored one's. - let node_type = in_tree_type.inner_node_type(); - let old_storage_cost = cost_return_on_error_no_add!( - cost, - Element::specialized_costs_for_key_value( - key, - old_serialized_element, - node_type, - grove_version, - ) - .map_err(Error::MerkError) - ); - let new_storage_cost = cost_return_on_error_no_add!( - cost, - Element::specialized_costs_for_key_value( - key, - serialized, - node_type, - grove_version, - ) - .map_err(Error::MerkError) - ); - let mut storage_costs = - TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); - if let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes { - let mut old_flags = maybe_old_flags.clone().unwrap_or_default(); - let (_, value_removed_bytes) = cost_return_on_error_no_add!( - cost, - split_removal_bytes(&mut old_flags, 0, removed_bytes) - ); - storage_costs.removed_bytes = value_removed_bytes; - } - let mut settled_element = new_element.clone(); - if let Some(new_flags) = settled_element.get_flags_mut().as_mut() { - let update = cost_return_on_error_no_add!( - cost, - (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) - .map_err(client_error) - ); - if update == ElementFlagsUpdate::SettleOwnerChange { - let settled_bytes = cost_return_on_error_into_no_add!( - cost, - settled_element.serialize(grove_version) - ); - let val_hash = value_hash(&settled_bytes).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } - } - } - if maybe_old_flags.is_some() { - let mut updated_new_element_with_old_flags = new_element.clone(); - updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); - // There are no storage flags, we can just hash new element - let new_serialized_bytes = cost_return_on_error_into_no_add!( - cost, - updated_new_element_with_old_flags.serialize(grove_version) - ); - let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); - Ok(val_hash).wrap_with_cost(cost) - } else { - let val_hash = value_hash(serialized).unwrap_add_cost(&mut cost); - Ok(val_hash).wrap_with_cost(cost) - } - } else { - Err(Error::NotSupported( - "going from a sum item to a not sum item is not supported".to_string(), - )) - .wrap_with_cost(cost) - }; - } else if new_element.is_sum_item() { - return Err(Error::NotSupported( - "going from an item to a sum item is not supported".to_string(), - )) - .wrap_with_cost(cost); - } - let mut maybe_old_flags = old_element.get_flags_owned(); - - let old_storage_cost = KV::node_value_byte_cost_size( - key.len() as u32, - old_serialized_element.len() as u32, - in_tree_type.inner_node_type(), - ); - - let original_new_element = new_element.clone(); - - let mut serialization_to_use = Cow::Borrowed(serialized); - - let mut new_storage_cost = if maybe_old_flags.is_some() { - // we need to get the new storage_cost as if it had the same storage flags as - // before - let mut updated_new_element_with_old_flags = original_new_element.clone(); - updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); - - let serialized_with_old_flags = cost_return_on_error_into_no_add!( - cost, - updated_new_element_with_old_flags.serialize(grove_version) - ); - KV::node_value_byte_cost_size( - key.len() as u32, - serialized_with_old_flags.len() as u32, - in_tree_type.inner_node_type(), - ) - } else { - KV::node_value_byte_cost_size( - key.len() as u32, - serialized.len() as u32, - in_tree_type.inner_node_type(), - ) - }; - - let mut i = 0; - - loop { - // Calculate storage costs - let mut storage_costs = - TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); - - if let Some(old_element_flags) = maybe_old_flags.as_mut() - && let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes - { - let (_, value_removed_bytes) = cost_return_on_error_no_add!( - cost, - split_removal_bytes(old_element_flags, 0, removed_bytes) - ); - storage_costs.removed_bytes = value_removed_bytes; - } - - let mut new_element_cloned = original_new_element.clone(); - - let new_flags = cost_return_on_error_no_add!( - cost, - new_element_cloned - .get_flags_mut() - .as_mut() - .ok_or(Error::CorruptedCodeExecution( - "element has no flags for just-in-time update", - )) - ); - let update = cost_return_on_error_no_add!( - cost, - (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) - .map_err(client_error) - ); - if update == ElementFlagsUpdate::Unchanged { - // There are no storage flags, we can just hash new element - - let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } else { - // There are no storage flags, we can just hash new element - let new_serialized_bytes = cost_return_on_error_into_no_add!( - cost, - new_element_cloned.serialize(grove_version) - ); - - if update == ElementFlagsUpdate::SettleOwnerChange { - // The apply stores the new element with the flags the - // callback left it, whatever its size. - let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } - - new_storage_cost = KV::node_value_byte_cost_size( - key.len() as u32, - new_serialized_bytes.len() as u32, - in_tree_type.inner_node_type(), - ); - - if serialization_to_use == new_serialized_bytes { - // it hasn't actually changed, let's do the value hash of it - let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } - - serialization_to_use = Cow::Owned(new_serialized_bytes); - } - - // Prevent potential infinite loop - if i > 8 { - return Err(Error::CyclicError( - "updated value based on costs too many times in reference", - )) - .wrap_with_cost(cost); - } - i += 1; - } - } -} diff --git a/grovedb/src/batch/just_in_time_reference_update/mod.rs b/grovedb/src/batch/just_in_time_reference_update/mod.rs new file mode 100644 index 000000000..1b4c19fc4 --- /dev/null +++ b/grovedb/src/batch/just_in_time_reference_update/mod.rs @@ -0,0 +1,136 @@ +//! The value hashes a reference written in a batch commits to when its +//! target is a pending write of the same batch. +//! +//! [`TreeCacheMerkByPath::process_old_element_flags`] predicts the bytes a +//! pending item write finally stores, after the apply has run the caller's +//! flags update on it. Each `v*.rs` defines a `process_old_element_flags_v*`, +//! selected by `apply_batch.same_batch_reference_target_prediction`: +//! +//! - version 0 is a hand-written copy of Merk's just-in-time value update. +//! For a sum item it assumes the new element keeps the stored flags and +//! does not consult the flags update, while the apply stores the flags the +//! flags update leaves it. When those differ (an owned sum item updated in +//! a later epoch, an unowned one gaining an owner) the reference commits +//! to bytes that are never stored and the grove no longer verifies. Grove +//! v1..v3 are consensus-locked to this. +//! - version 1 runs Merk's own just-in-time value update on the stored and +//! new bytes, with the put the apply performs, so the reference commits to +//! exactly the bytes the apply stores. + +mod v0; +mod v1; + +use grovedb_costs::{ + cost_return_on_error_into_no_add, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, + CostResult, CostsExt, OperationCost, +}; +use grovedb_merk::{tree::value_hash, tree_type::TreeType, CryptoHash, Merk}; +use grovedb_storage::StorageContext; +use grovedb_version::{error::GroveVersionError, version::GroveVersion}; + +use crate::{batch::TreeCacheMerkByPath, Element, ElementFlags, Error}; + +impl<'db, S, F, F2> TreeCacheMerkByPath +where + F: FnMut(&[Vec], bool) -> CostResult, Error>, + F2: FnMut( + &[Vec], + Option<&Element>, + ) -> CostResult)>, Error>, + S: StorageContext<'db>, +{ + /// The hash a reference written in this batch commits to for a pending + /// write of a backward-references ITEM at `qualified_path`: the logical + /// (stripped) hash of the bytes the apply finally stores there — the + /// preprocessor's prediction when the caller's flags update may rewrite + /// them, the op's own element otherwise. + pub(crate) fn landed_backward_references_item_value_hash( + &self, + qualified_path: &[Vec], + element: &Element, + grove_version: &GroveVersion, + ) -> CostResult { + let mut cost = OperationCost::default(); + let landed = self + .landed_backward_references_items + .get(qualified_path) + .unwrap_or(element); + let serialized = cost_return_on_error_into_no_add!( + cost, + landed + .stripped_of_backward_references() + .serialize(grove_version) + ); + let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); + Ok(val_hash).wrap_with_cost(cost) + } + + /// The value hash of the bytes a pending write of `new_element` over the + /// stored `old_element` finally stores, after the apply has run the + /// caller's flags update on it, so that a reference written in the same + /// batch commits to them. See the module docs for the version semantics. + #[allow(clippy::too_many_arguments)] + pub(crate) fn process_old_element_flags( + key: &[u8], + serialized: &[u8], + new_element: &mut Element, + old_element: Element, + old_serialized_element: &[u8], + in_tree_type: TreeType, + settle_owner_changes: bool, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, + ) -> CostResult + where + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, + { + match grove_version + .grovedb_versions + .apply_batch + .same_batch_reference_target_prediction + { + 0 => v0::process_old_element_flags_v0( + key, + serialized, + new_element, + old_element, + old_serialized_element, + in_tree_type, + settle_owner_changes, + flags_update, + split_removal_bytes, + grove_version, + ), + 1 => v1::process_old_element_flags_v1( + key, + serialized, + new_element, + &old_element, + old_serialized_element, + in_tree_type, + flags_update, + split_removal_bytes, + grove_version, + ), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "process_old_element_flags".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )) + .wrap_with_cost(OperationCost::default()), + } + } +} diff --git a/grovedb/src/batch/just_in_time_reference_update/v0.rs b/grovedb/src/batch/just_in_time_reference_update/v0.rs new file mode 100644 index 000000000..dc5ac5a3c --- /dev/null +++ b/grovedb/src/batch/just_in_time_reference_update/v0.rs @@ -0,0 +1,260 @@ +use std::borrow::Cow; + +use grovedb_costs::{ + cost_return_on_error_into_no_add, cost_return_on_error_no_add, + storage_cost::{ + removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, + StorageCost, + }, + CostResult, CostsExt, OperationCost, +}; +use grovedb_merk::{ + element::costs::ElementCostExtensions, + tree::{kv::KV, value_hash, TreeNode}, + tree_type::TreeType, + CryptoHash, +}; +use grovedb_version::version::GroveVersion; + +use crate::{batch::MerkError, Element, ElementFlags, Error}; + +/// Version 0 of the same-batch reference target prediction, selected by +/// `apply_batch.same_batch_reference_target_prediction == 0` (grove +/// v1..v3): a hand-written copy of Merk's just-in-time value update. +/// +/// It differs from version 1 in how it measures and asks the flags update. +/// A sum item is taken to keep its stored flags without consulting the +/// flags update (except, with `settle_owner_changes`, for an answer that +/// settles an owner change), while the apply stores the flags the flags +/// update leaves it, so a reference to a sum item whose flags change +/// commits to bytes that are never stored. +#[allow(clippy::too_many_arguments)] +pub(super) fn process_old_element_flags_v0( + key: &[u8], + serialized: &[u8], + new_element: &mut Element, + old_element: Element, + old_serialized_element: &[u8], + in_tree_type: TreeType, + settle_owner_changes: bool, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, +) -> CostResult +where + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, +{ + let mut cost = OperationCost::default(); + let client_error = |e: Error| -> Error { + match e { + Error::JustInTimeElementFlagsClientError(_) => { + MerkError::ClientCorruptionError(e.to_string()).into() + } + _ => MerkError::ClientCorruptionError("non client error".to_string()).into(), + } + }; + if old_element.is_sum_item() { + return if new_element.is_sum_item() { + let maybe_old_flags = old_element.get_flags_owned(); + if settle_owner_changes + && maybe_old_flags.is_some() + && new_element.get_flags().is_some() + { + // Merk measures a sum item by its own specialized cost, + // new flags included, against the stored one's. + let node_type = in_tree_type.inner_node_type(); + let old_storage_cost = cost_return_on_error_no_add!( + cost, + Element::specialized_costs_for_key_value( + key, + old_serialized_element, + node_type, + grove_version, + ) + .map_err(Error::MerkError) + ); + let new_storage_cost = cost_return_on_error_no_add!( + cost, + Element::specialized_costs_for_key_value( + key, + serialized, + node_type, + grove_version, + ) + .map_err(Error::MerkError) + ); + let mut storage_costs = + TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); + if let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes { + let mut old_flags = maybe_old_flags.clone().unwrap_or_default(); + let (_, value_removed_bytes) = cost_return_on_error_no_add!( + cost, + split_removal_bytes(&mut old_flags, 0, removed_bytes) + ); + storage_costs.removed_bytes = value_removed_bytes; + } + let mut settled_element = new_element.clone(); + if let Some(new_flags) = settled_element.get_flags_mut().as_mut() { + let update = cost_return_on_error_no_add!( + cost, + (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) + .map_err(client_error) + ); + if update == ElementFlagsUpdate::SettleOwnerChange { + let settled_bytes = cost_return_on_error_into_no_add!( + cost, + settled_element.serialize(grove_version) + ); + let val_hash = value_hash(&settled_bytes).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + } + } + if maybe_old_flags.is_some() { + let mut updated_new_element_with_old_flags = new_element.clone(); + updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); + // There are no storage flags, we can just hash new element + let new_serialized_bytes = cost_return_on_error_into_no_add!( + cost, + updated_new_element_with_old_flags.serialize(grove_version) + ); + let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); + Ok(val_hash).wrap_with_cost(cost) + } else { + let val_hash = value_hash(serialized).unwrap_add_cost(&mut cost); + Ok(val_hash).wrap_with_cost(cost) + } + } else { + Err(Error::NotSupported( + "going from a sum item to a not sum item is not supported".to_string(), + )) + .wrap_with_cost(cost) + }; + } else if new_element.is_sum_item() { + return Err(Error::NotSupported( + "going from an item to a sum item is not supported".to_string(), + )) + .wrap_with_cost(cost); + } + let mut maybe_old_flags = old_element.get_flags_owned(); + + let old_storage_cost = KV::node_value_byte_cost_size( + key.len() as u32, + old_serialized_element.len() as u32, + in_tree_type.inner_node_type(), + ); + + let original_new_element = new_element.clone(); + + let mut serialization_to_use = Cow::Borrowed(serialized); + + let mut new_storage_cost = if maybe_old_flags.is_some() { + // we need to get the new storage_cost as if it had the same storage flags as + // before + let mut updated_new_element_with_old_flags = original_new_element.clone(); + updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); + + let serialized_with_old_flags = cost_return_on_error_into_no_add!( + cost, + updated_new_element_with_old_flags.serialize(grove_version) + ); + KV::node_value_byte_cost_size( + key.len() as u32, + serialized_with_old_flags.len() as u32, + in_tree_type.inner_node_type(), + ) + } else { + KV::node_value_byte_cost_size( + key.len() as u32, + serialized.len() as u32, + in_tree_type.inner_node_type(), + ) + }; + + let mut i = 0; + + loop { + // Calculate storage costs + let mut storage_costs = + TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); + + if let Some(old_element_flags) = maybe_old_flags.as_mut() + && let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes + { + let (_, value_removed_bytes) = cost_return_on_error_no_add!( + cost, + split_removal_bytes(old_element_flags, 0, removed_bytes) + ); + storage_costs.removed_bytes = value_removed_bytes; + } + + let mut new_element_cloned = original_new_element.clone(); + + let new_flags = cost_return_on_error_no_add!( + cost, + new_element_cloned + .get_flags_mut() + .as_mut() + .ok_or(Error::CorruptedCodeExecution( + "element has no flags for just-in-time update", + )) + ); + let update = cost_return_on_error_no_add!( + cost, + (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) + .map_err(client_error) + ); + if update == ElementFlagsUpdate::Unchanged { + // There are no storage flags, we can just hash new element + + let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } else { + // There are no storage flags, we can just hash new element + let new_serialized_bytes = cost_return_on_error_into_no_add!( + cost, + new_element_cloned.serialize(grove_version) + ); + + if update == ElementFlagsUpdate::SettleOwnerChange { + // The apply stores the new element with the flags the + // callback left it, whatever its size. + let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + + new_storage_cost = KV::node_value_byte_cost_size( + key.len() as u32, + new_serialized_bytes.len() as u32, + in_tree_type.inner_node_type(), + ); + + if serialization_to_use == new_serialized_bytes { + // it hasn't actually changed, let's do the value hash of it + let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + + serialization_to_use = Cow::Owned(new_serialized_bytes); + } + + // Prevent potential infinite loop + if i > 8 { + return Err(Error::CyclicError( + "updated value based on costs too many times in reference", + )) + .wrap_with_cost(cost); + } + i += 1; + } +} diff --git a/grovedb/src/batch/just_in_time_reference_update/v1.rs b/grovedb/src/batch/just_in_time_reference_update/v1.rs new file mode 100644 index 000000000..8d0d0642b --- /dev/null +++ b/grovedb/src/batch/just_in_time_reference_update/v1.rs @@ -0,0 +1,100 @@ +use grovedb_costs::{ + cost_return_on_error_into_no_add, cost_return_on_error_no_add, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, + CostResult, CostsExt, OperationCost, +}; +use grovedb_merk::{ + element::{costs::ElementCostExtensions, tree_type::ElementTreeTypeExtensions}, + tree::{kv::ValueDefinedCostType::SpecializedValueDefinedCost, value_hash}, + tree_type::TreeType, + CryptoHash, +}; +use grovedb_version::version::GroveVersion; + +use crate::{ + batch::just_in_time_value_update::predict_put_final_bytes, Element, ElementFlags, Error, +}; + +/// Version 1 of the same-batch reference target prediction, selected by +/// `apply_batch.same_batch_reference_target_prediction == 1` (grove v4+). +/// +/// It differs from version 0 in how it gets the stored bytes: it runs +/// Merk's own just-in-time value update on the stored and new bytes, with +/// the put the apply performs (a sum item at its specialized cost, any other +/// item as an ordinary put), so the reference commits to exactly what the +/// apply stores whatever the flags update answers. +#[allow(clippy::too_many_arguments)] +pub(super) fn process_old_element_flags_v1( + key: &[u8], + serialized: &[u8], + new_element: &Element, + old_element: &Element, + old_serialized_element: &[u8], + in_tree_type: TreeType, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, +) -> CostResult +where + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, +{ + let mut cost = OperationCost::default(); + match (old_element.is_sum_item(), new_element.is_sum_item()) { + (true, false) => { + return Err(Error::NotSupported( + "going from a sum item to a not sum item is not supported".to_string(), + )) + .wrap_with_cost(cost) + } + (false, true) => { + return Err(Error::NotSupported( + "going from an item to a sum item is not supported".to_string(), + )) + .wrap_with_cost(cost) + } + _ => {} + } + // The put the apply performs (`insert_into_batch_operations`). + let value_defined_cost = if new_element.is_sum_item() { + Some(SpecializedValueDefinedCost(cost_return_on_error_no_add!( + cost, + new_element + .specialized_value_defined_cost(grove_version) + .ok_or(Error::CorruptedCodeExecution( + "sum items should always have a value defined cost" + )) + ))) + } else { + None + }; + let feature_type = + cost_return_on_error_into_no_add!(cost, new_element.get_feature_type(in_tree_type)); + let stored_bytes = cost_return_on_error_no_add!( + cost, + predict_put_final_bytes( + key, + old_serialized_element, + serialized.to_vec(), + feature_type, + value_defined_cost, + in_tree_type, + flags_update, + split_removal_bytes, + grove_version, + ) + .map_err(|e| Error::CorruptedData(format!( + "predicting the just-in-time value update of a reference target: {e}" + ))) + ); + let val_hash = value_hash(&stored_bytes).unwrap_add_cost(&mut cost); + Ok(val_hash).wrap_with_cost(cost) +} diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index ebcc08fb6..fdcd206e6 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -23,7 +23,7 @@ use grovedb_merk::{ ValueDefinedCostType, ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, }, - TreeNode, + TreeFeatureType, TreeNode, }, tree_type::{CostSize, TreeType, SUM_ITEM_COST_SIZE}, Error as MerkError, @@ -63,6 +63,25 @@ where } } +/// The caller's flags-update callback with its answer as an +/// [`ElementFlagsUpdate`] (a `bool` answer is `Changed` or `Unchanged`). +pub(crate) fn flags_update_answers( + mut flags_update: impl FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, +) -> impl FnMut(&StorageCost, Option, &mut ElementFlags) -> Result +where + U: Into, +{ + move |storage_cost: &StorageCost, + old_flags: Option, + new_flags: &mut ElementFlags| { + flags_update(storage_cost, old_flags, new_flags).map(Into::into) + } +} + /// The storage cost of the stored value a write replaces. pub(super) fn old_specialized_cost( key: &[u8], @@ -242,22 +261,25 @@ where } } -/// The element a batch apply finally stores when it writes `new_element` -/// (a backward-references family element, which the apply writes as a -/// provided-value-hash put) over the stored `old_serialized` bytes at `key` -/// in a subtree of `in_tree_type`, with the caller's callbacks. +/// The bytes a batch apply finally stores when it puts `new_serialized` +/// (with `feature_type`, and the value-defined cost `value_defined_cost` the +/// put stamps on the node) over the stored `old_serialized` bytes at `key` in +/// a subtree of `in_tree_type`, with the caller's callbacks: Merk's own +/// just-in-time value update, run on a detached node. /// -/// The callbacks must answer the apply the same way they answer here: the -/// batch checks after the apply that the stored bytes are the predicted ones. -pub(crate) fn predict_provided_value_hash_put( +/// The callbacks must answer the apply the same way they answer here. +#[allow(clippy::too_many_arguments)] +pub(crate) fn predict_put_final_bytes( key: &[u8], old_serialized: &[u8], - new_element: &Element, + new_serialized: Vec, + feature_type: TreeFeatureType, + value_defined_cost: Option, in_tree_type: TreeType, flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, -) -> Result +) -> Result, MerkError> where G: FnMut( &StorageCost, @@ -270,13 +292,12 @@ where u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { - let new_serialized = new_element.serialize(grove_version)?; - let feature_type = new_element.get_feature_type(in_tree_type)?; - let final_bytes = TreeNode::provided_value_hash_put_final_value( + TreeNode::provided_value_hash_put_final_value( key.to_vec(), old_serialized.to_vec(), - new_serialized.clone(), + new_serialized, feature_type, + value_defined_cost, &|key, value| old_specialized_cost(key, value, in_tree_type, grove_version), &|old_value, new_value| new_value_with_old_flags(old_value, new_value, grove_version), &mut |storage_costs, old_value, new_value| { @@ -297,6 +318,51 @@ where grove_version, ) }, + grove_version, + ) +} + +/// The element a batch apply finally stores when it writes `new_element` +/// (a backward-references family element, which the apply writes as a +/// provided-value-hash put) over the stored `old_serialized` bytes at `key` +/// in a subtree of `in_tree_type`, with the caller's callbacks. +/// +/// The callbacks must answer the apply the same way they answer here: the +/// batch checks after the apply that the stored bytes are the predicted ones. +pub(crate) fn predict_provided_value_hash_put( + key: &[u8], + old_serialized: &[u8], + new_element: &Element, + in_tree_type: TreeType, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, +) -> Result +where + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, +{ + let new_serialized = new_element.serialize(grove_version)?; + let feature_type = new_element.get_feature_type(in_tree_type)?; + // A provided-value-hash put drops any value-defined cost. + let final_bytes = predict_put_final_bytes( + key, + old_serialized, + new_serialized.clone(), + feature_type, + None, + in_tree_type, + flags_update, + split_removal_bytes, + grove_version, ) .map_err(|e| { Error::CorruptedData(format!( diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 78108063a..149f8c4f0 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -32,6 +32,8 @@ mod just_in_time_value_update; mod options; mod refresh_reference_mode; #[cfg(test)] +mod same_batch_reference_prediction_tests; +#[cfg(test)] mod settle_owner_change_cost_tests; #[cfg(test)] mod single_deletion_cost_tests; @@ -5635,9 +5637,9 @@ impl GroveDb { cidx_overwrite_cleanup_paths: Default::default(), deleted_tree_actual_types: Default::default(), indexed_mirror_rekey_churn_bytes: 0, - settle_owner_changes: batch_apply_options - .as_ref() - .is_some_and(|options| options.settle_owner_changes), + settle_owner_changes: BatchApplyOptions::settle_owner_changes_in( + &batch_apply_options, + ), }, grove_version ) @@ -6784,9 +6786,7 @@ impl GroveDb { ); let mut update_element_flags_function = just_in_time_value_update::batch_flags_update( update_element_flags_function, - batch_apply_options - .as_ref() - .is_some_and(|options| options.settle_owner_changes), + BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), ); let _storage_removal_version_guard = grovedb_costs::storage_cost::removal::use_basic_sectioned_removal_addition_version( @@ -7285,9 +7285,7 @@ impl GroveDb { ); let mut update_element_flags_function = just_in_time_value_update::batch_flags_update( update_element_flags_function, - batch_apply_options - .as_ref() - .is_some_and(|options| options.settle_owner_changes), + BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), ); let _storage_removal_version_guard = grovedb_costs::storage_cost::removal::use_basic_sectioned_removal_addition_version( @@ -8054,12 +8052,11 @@ impl GroveDb { .apply_batch .estimated_case_operations_for_batch ); - let update_element_flags_function = just_in_time_value_update::batch_flags_update( - update_element_flags_function, - batch_apply_options - .as_ref() - .is_some_and(|options| options.settle_owner_changes), - ); + // The estimators never call the flags update (an estimate follows + // the batch options, not the answers the flags update would give), + // so it only needs its answer type. + let update_element_flags_function = + just_in_time_value_update::flags_update_answers(update_element_flags_function); let mut cost = OperationCost::default(); if ops.is_empty() { diff --git a/grovedb/src/batch/options.rs b/grovedb/src/batch/options.rs index 51b6ef177..128226823 100644 --- a/grovedb/src/batch/options.rs +++ b/grovedb/src/batch/options.rs @@ -91,11 +91,12 @@ pub struct BatchApplyOptions { /// /// The estimators read it too: an estimate with it set charges every /// write that may settle an owner change (`InsertOrReplace`, `Replace`, - /// `Patch` and a trusted `RefreshReference` of an element with flags) at - /// least the bytes its element adds when inserted, with the referrer - /// entries a backward-references element carries over counted at its - /// declared capacity, so the estimate is never below what the apply - /// records. + /// `Patch` and a trusted `RefreshReference` of an element with flags, and + /// the write of a flagged tree the batch also writes under) at least the + /// bytes its element adds when inserted, with the referrer entries a + /// backward-references element carries over counted at its declared + /// capacity, each at the largest entry registration admits, so the + /// estimate is never below what the apply records. /// /// Off by default, and every cost is then what it was before the option /// existed. @@ -118,6 +119,14 @@ impl Default for BatchApplyOptions { #[cfg(feature = "minimal")] impl BatchApplyOptions { + /// Whether the batch options `options`, when given, set + /// [`Self::settle_owner_changes`]. + pub(crate) fn settle_owner_changes_in(options: &Option) -> bool { + options + .as_ref() + .is_some_and(|options| options.settle_owner_changes) + } + /// As insert options pub(crate) fn as_insert_options(&self) -> InsertOptions { InsertOptions { diff --git a/grovedb/src/batch/same_batch_reference_prediction_tests.rs b/grovedb/src/batch/same_batch_reference_prediction_tests.rs new file mode 100644 index 000000000..5a7ff4f21 --- /dev/null +++ b/grovedb/src/batch/same_batch_reference_prediction_tests.rs @@ -0,0 +1,413 @@ +//! A replacement whose new value keeps flags of its own: a reference written +//! in the same batch commits to the bytes the replacement stores +//! (`apply_batch.same_batch_reference_target_prediction`), and the +//! replacement is charged for those bytes +//! (`merk_versions.tree.just_in_time_value_update`). + +#[cfg(feature = "minimal")] +mod tests { + use grovedb_costs::{ + storage_cost::{ + removal::StorageRemovedBytes::NoStorageRemoval, transition::ElementFlagsUpdate, + }, + OperationCost, + }; + use grovedb_epoch_based_storage_flags::StorageFlags; + use grovedb_merk::{element::costs::ElementCostExtensions, tree_type::TreeType}; + use grovedb_version::version::{v3::GROVE_V3, GroveVersion}; + + use crate::{ + batch::{BatchApplyOptions, QualifiedGroveDbOp}, + reference_path::ReferencePathType, + tests::{common::EMPTY_PATH, make_empty_grovedb, TempGroveDb}, + Element, Error, + }; + + const OLD_OWNER: [u8; 32] = [1; 32]; + const NEW_OWNER: [u8; 32] = [2; 32]; + const KEY: &[u8] = b"key1"; + + fn owned_flags(epoch: u16, owner: [u8; 32]) -> Option> { + Some(StorageFlags::SingleEpochOwned(epoch, owner).to_element_flags()) + } + + fn unowned_flags(epoch: u16) -> Option> { + Some(StorageFlags::SingleEpoch(epoch).to_element_flags()) + } + + fn target() -> ReferencePathType { + ReferencePathType::AbsolutePathReference(vec![b"tree".to_vec(), KEY.to_vec()]) + } + + /// Drive's flags update, merging or settling owner changes. + #[derive(Clone, Copy, Debug, PartialEq)] + enum Mode { + Merging, + Settling, + } + + fn apply( + db: &TempGroveDb, + ops: Vec, + mode: Mode, + grove_version: &GroveVersion, + ) -> Result { + db.apply_batch_with_element_flags_update( + ops, + Some(BatchApplyOptions { + settle_owner_changes: mode == Mode::Settling, + ..Default::default() + }), + |cost, old_flags, new_flags| { + match mode { + Mode::Merging => StorageFlags::update_element_flags(cost, old_flags, new_flags) + .map(ElementFlagsUpdate::from), + Mode::Settling => StorageFlags::update_element_flags_settling_owner_changes( + cost, old_flags, new_flags, + ), + } + .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) + }, + |flags, removed_key_bytes, removed_value_bytes| { + StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) + .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) + }, + None, + grove_version, + ) + .cost_as_result() + } + + /// A grove holding `old` at `[tree]/key1` (a tree of `tree_type`), and, + /// `with_reference`, a reference to it at `[index, value]/reference`, + /// deeper than its target as a Drive index reference is. + fn grove_with( + old: &Element, + tree_type: TreeType, + with_reference: bool, + grove_version: &GroveVersion, + ) -> TempGroveDb { + let db = make_empty_grovedb(); + let tree = match tree_type { + TreeType::SumTree => Element::empty_sum_tree(), + _ => Element::empty_tree(), + }; + db.insert(EMPTY_PATH, b"tree", tree, None, None, grove_version) + .unwrap() + .expect("expected to insert the tree"); + db.insert( + [b"tree".as_slice()].as_ref(), + KEY, + old.clone(), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the old element"); + db.insert( + EMPTY_PATH, + b"index", + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the index tree"); + db.insert( + [b"index".as_slice()].as_ref(), + b"value", + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the index value tree"); + if !with_reference { + return db; + } + db.insert( + [b"index".as_slice(), b"value"].as_ref(), + b"reference", + Element::new_reference(target()), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the reference"); + db + } + + fn write_op(element: &Element) -> QualifiedGroveDbOp { + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"tree".to_vec()], + KEY.to_vec(), + element.clone(), + ) + } + + fn refresh_op(trusted: bool) -> QualifiedGroveDbOp { + QualifiedGroveDbOp::refresh_reference_op( + vec![b"index".to_vec(), b"value".to_vec()], + b"reference".to_vec(), + target(), + None, + None, + false, + trusted, + ) + } + + /// The ways a batch rewrites the references to the element it updates. + fn reference_ops() -> Vec<(&'static str, Vec)> { + vec![ + ( + "an untrusted refresh of a deeper reference", + vec![refresh_op(false)], + ), + ( + "a trusted refresh of a deeper reference", + vec![refresh_op(true)], + ), + ( + "a rewrite of a deeper reference", + vec![QualifiedGroveDbOp::insert_or_replace_op( + vec![b"index".to_vec(), b"value".to_vec()], + b"reference".to_vec(), + Element::new_reference(target()), + )], + ), + ( + "a refresh and a new reference beside the tree", + vec![ + refresh_op(true), + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"index".to_vec()], + b"another reference".to_vec(), + Element::new_reference(target()), + ), + ], + ), + ] + } + + /// Updates whose stored flags are not the new element's as written, nor + /// the old element's: the flags update merges them. + fn flag_changing_updates() -> Vec<(&'static str, Element, Element, TreeType)> { + vec![ + ( + "an item with a sum item, same owner, grown in a later epoch", + Element::new_item_with_sum_item_with_flags( + vec![7; 20], + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_item_with_sum_item_with_flags( + vec![8; 45], + 9, + owned_flags(2, OLD_OWNER), + ), + TreeType::SumTree, + ), + ( + "a sum item gaining an owner in the same epoch", + Element::new_sum_item_with_flags(5, unowned_flags(0)), + Element::new_sum_item_with_flags(900, owned_flags(0, NEW_OWNER)), + TreeType::SumTree, + ), + ( + "a sum item gaining an owner in a later epoch", + Element::new_sum_item_with_flags(5, unowned_flags(0)), + Element::new_sum_item_with_flags(900, owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ( + "an item, same owner, grown in a later epoch", + Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 45], owned_flags(2, OLD_OWNER)), + TreeType::NormalTree, + ), + ( + "an item gaining an owner in a later epoch", + Element::new_item_with_flags(vec![7; 20], unowned_flags(0)), + Element::new_item_with_flags(vec![8; 45], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ] + } + + fn issues(db: &TempGroveDb, grove_version: &GroveVersion) -> usize { + db.visualize_verify_grovedb(None, true, false, grove_version) + .expect("expected to verify the grove") + .len() + } + + #[test] + fn a_reference_rewritten_in_the_same_batch_commits_to_the_stored_bytes() { + let grove_version = GroveVersion::latest(); + for (label, old, new, tree_type) in flag_changing_updates() { + for mode in [Mode::Merging, Mode::Settling] { + for (reference_label, reference_ops) in reference_ops() { + let db = grove_with(&old, tree_type, true, grove_version); + let mut ops = vec![write_op(&new)]; + ops.extend(reference_ops); + apply(&db, ops, mode, grove_version).unwrap_or_else(|e| { + panic!("{label}, {reference_label}, {mode:?}: expected to apply: {e}") + }); + assert_eq!( + issues(&db, grove_version), + 0, + "{label}, {reference_label}, {mode:?}: the reference is stale" + ); + } + } + } + } + + /// Grove v1..v3 keep the hand-written prediction, which takes a sum item + /// to keep its stored flags (consensus-locked). + #[test] + fn grove_v3_keeps_the_legacy_sum_item_prediction() { + let grove_version = &GROVE_V3; + for (label, old, new, tree_type) in flag_changing_updates() { + if !old.is_sum_item() { + continue; + } + for (reference_label, reference_ops) in reference_ops() { + let db = grove_with(&old, tree_type, true, grove_version); + let references = reference_ops.len(); + let mut ops = vec![write_op(&new)]; + ops.extend(reference_ops); + apply(&db, ops, Mode::Merging, grove_version).unwrap_or_else(|e| { + panic!("{label}, {reference_label}: expected to apply: {e}") + }); + assert_eq!( + issues(&db, grove_version), + references, + "{label}, {reference_label}: expected every reference stale" + ); + } + } + } + + /// The bytes the node of `element` at `key1` holds besides its key. + fn value_bytes(element: &Element, tree_type: TreeType, grove_version: &GroveVersion) -> u32 { + Element::specialized_costs_for_key_value( + KEY, + &element + .serialize(grove_version) + .expect("expected to serialize"), + tree_type.inner_node_type(), + grove_version, + ) + .expect("expected value bytes") + } + + /// Replacements whose new flags stand as written (the flags update + /// answers `Unchanged`) but are not the length of the old ones. + fn unchanged_flags_of_another_length() -> Vec<(&'static str, Element, Element, Option)> { + vec![ + ( + "longer flags, no flags update", + Element::new_item_with_flags(b"v".to_vec(), Some(vec![0])), + Element::new_item_with_flags(b"w".to_vec(), Some(vec![1, 2, 3])), + None, + ), + ( + "flags dropped, no flags update", + Element::new_item_with_flags(b"v".to_vec(), Some(vec![0])), + Element::new_item(b"w".to_vec()), + None, + ), + ( + "an unowned item gaining an owner in the same epoch, grown", + Element::new_item_with_flags(vec![7; 20], unowned_flags(0)), + Element::new_item_with_flags(vec![8; 45], owned_flags(0, NEW_OWNER)), + Some(Mode::Merging), + ), + ] + } + + fn replace( + db: &TempGroveDb, + new: &Element, + mode: Option, + grove_version: &GroveVersion, + ) -> Result { + match mode { + Some(mode) => apply(db, vec![write_op(new)], mode, grove_version), + None => db + .apply_batch(vec![write_op(new)], None, None, grove_version) + .cost_as_result(), + } + } + + #[test] + fn a_replacement_keeping_flags_of_another_length_is_charged_for_its_bytes() { + let grove_version = GroveVersion::latest(); + for (label, old, new, mode) in unchanged_flags_of_another_length() { + // Rewriting `old` over itself replaces its own bytes plus what + // the update rewrites around it. + let rewrite_db = grove_with(&old, TreeType::NormalTree, false, grove_version); + let rewrite = replace(&rewrite_db, &old, mode, grove_version) + .unwrap_or_else(|e| panic!("{label}: expected to rewrite: {e}")); + let old_bytes = value_bytes(&old, TreeType::NormalTree, grove_version); + let new_bytes = value_bytes(&new, TreeType::NormalTree, grove_version); + let around = rewrite.storage_cost.replaced_bytes - old_bytes; + + let db = grove_with(&old, TreeType::NormalTree, false, grove_version); + let cost = replace(&db, &new, mode, grove_version) + .unwrap_or_else(|e| panic!("{label}: expected to apply: {e}")); + assert_eq!( + cost.storage_cost.replaced_bytes, + around + old_bytes.min(new_bytes), + "{label}" + ); + assert_eq!( + cost.storage_cost.added_bytes, + new_bytes.saturating_sub(old_bytes), + "{label}" + ); + assert_eq!( + cost.storage_cost.removed_bytes.total_removed_bytes(), + old_bytes.saturating_sub(new_bytes), + "{label}" + ); + assert_eq!(issues(&db, grove_version), 0, "{label}"); + } + } + + /// Grove v1..v3 keep the measurement taken with the old flags, which the + /// commit refuses (consensus-locked). + #[test] + fn grove_v3_refuses_a_replacement_keeping_flags_of_another_length() { + let grove_version = &GROVE_V3; + for (label, old, new, mode) in unchanged_flags_of_another_length() { + let db = grove_with(&old, TreeType::NormalTree, false, grove_version); + let error = replace(&db, &new, mode, grove_version) + .expect_err(&format!("{label}: expected the legacy cost mismatch")); + assert!( + error.to_string().contains("cost mismatch"), + "{label}: unexpected error {error}" + ); + } + // the same replacement with flags of the same length is accepted + let db = grove_with( + &Element::new_item_with_flags(b"v".to_vec(), Some(vec![0])), + TreeType::NormalTree, + false, + grove_version, + ); + let cost = replace( + &db, + &Element::new_item_with_flags(b"w".to_vec(), Some(vec![1])), + None, + grove_version, + ) + .expect("expected a same-length replacement to apply"); + assert_eq!(cost.storage_cost.removed_bytes, NoStorageRemoval); + } +} diff --git a/grovedb/src/batch/settle_owner_change_cost_tests.rs b/grovedb/src/batch/settle_owner_change_cost_tests.rs index b7bcba881..bc8f8e7ad 100644 --- a/grovedb/src/batch/settle_owner_change_cost_tests.rs +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -496,7 +496,8 @@ mod tests { /// A backward-references item keeps the referrers registered on the item /// it replaces, and a settled write adds their entries too. Both - /// estimates cover them through the item's declared capacity. + /// estimates cover them through the item's declared capacity, wherever + /// the referrers sit. #[test] fn estimates_cover_the_referrers_a_settled_item_carries_over() { let grove_version = GroveVersion::latest(); @@ -509,53 +510,102 @@ mod tests { }; let old = family_item(vec![7; 20], owned_flags(0, OLD_OWNER)); let new = family_item(vec![8; 20], owned_flags(2, NEW_OWNER)); - let db = grove_with(&old, TreeType::NormalTree, grove_version); - for referrer in [b"r1".as_slice(), b"r2", b"r3"] { + let siblings = |db: &TempGroveDb| { + for referrer in [b"r1".as_slice(), b"r2", b"r3"] { + db.insert( + [b"tree".as_slice()].as_ref(), + referrer, + Element::new_bidirectional_reference(ReferencePathType::SiblingReference( + KEY.to_vec(), + )), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to register a referrer"); + } + }; + // Referrers far from the item under long keys, whose entries hold + // absolute paths much longer than the item's own. + let distant = |db: &TempGroveDb| { + let (outer, inner) = (vec![b'x'; 200], vec![b'y'; 200]); db.insert( - [b"tree".as_slice()].as_ref(), - referrer, - Element::new_bidirectional_reference(ReferencePathType::SiblingReference( - KEY.to_vec(), - )), + [b"targets".as_slice()].as_ref(), + &outer, + Element::empty_tree(), None, None, grove_version, ) .unwrap() - .expect("expected to register a referrer"); - } - let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) - .expect("expected the settling update to apply"); - let landed = stored_in_merk(&db, grove_version); - assert_eq!( - landed.backward_references().map(|refs| refs.len()), - Some(3), - "the item keeps its referrers" - ); - assert_eq!( - storage_flags_of(&landed), - StorageFlags::SingleEpochOwned(2, NEW_OWNER) - ); - // the referrer entries are part of what the settled apply adds - assert_eq!( - applied.storage_cost.added_bytes, - key_bytes() + value_bytes(&landed, TreeType::NormalTree, grove_version) - ); - assert!( - applied.storage_cost.added_bytes - > key_bytes() + value_bytes(&new, TreeType::NormalTree, grove_version) - ); - for average_case_tree_type in [Some(TreeType::NormalTree), None] { - let estimate = estimate( - vec![write_op(&new)], - Some(options(Mode::Settling)), - average_case_tree_type, + .expect("expected to insert a tree"); + db.insert( + [b"targets".as_slice(), outer.as_slice()].as_ref(), + &inner, + Element::empty_tree(), + None, + None, grove_version, + ) + .unwrap() + .expect("expected to insert a tree"); + for i in 0..3u8 { + let mut referrer = vec![b'z'; 199]; + referrer.push(i); + db.insert( + [b"targets".as_slice(), outer.as_slice(), inner.as_slice()].as_ref(), + &referrer, + Element::new_bidirectional_reference(ReferencePathType::AbsolutePathReference( + vec![b"tree".to_vec(), KEY.to_vec()], + )), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to register a referrer"); + } + }; + let placements: [(&str, &dyn Fn(&TempGroveDb)); 2] = + [("siblings", &siblings), ("distant", &distant)]; + for (placement, register_referrers) in placements { + let db = grove_with(&old, TreeType::NormalTree, grove_version); + register_referrers(&db); + let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) + .expect("expected the settling update to apply"); + let landed = stored_in_merk(&db, grove_version); + assert_eq!( + landed.backward_references().map(|refs| refs.len()), + Some(3), + "{placement}: the item keeps its referrers" + ); + assert_eq!( + storage_flags_of(&landed), + StorageFlags::SingleEpochOwned(2, NEW_OWNER) + ); + // the referrer entries are part of what the settled apply adds + assert_eq!( + applied.storage_cost.added_bytes, + key_bytes() + value_bytes(&landed, TreeType::NormalTree, grove_version) ); assert!( - estimate.storage_cost.added_bytes >= applied.storage_cost.added_bytes, - "estimate {estimate:?} adds less than the settled apply {applied:?}" + applied.storage_cost.added_bytes + > key_bytes() + value_bytes(&new, TreeType::NormalTree, grove_version) ); + for average_case_tree_type in [Some(TreeType::NormalTree), None] { + let estimate = estimate( + vec![write_op(&new)], + Some(options(Mode::Settling)), + average_case_tree_type, + grove_version, + ); + assert!( + estimate.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "{placement}: estimate {estimate:?} adds less than the settled apply \ + {applied:?}" + ); + } } } @@ -899,13 +949,10 @@ mod tests { .expect("expected an estimate") } - /// With the option, the average-case and worst-case estimates of every - /// write that may settle an owner change are never below the settled - /// apply; without it they are what they always were. - #[test] - fn estimates_with_the_option_cover_a_settled_owner_change() { - let grove_version = GroveVersion::latest(); - let cases = [ + /// Updates of every element kind an estimate may see settle: the old + /// element, the new one, and the tree they are in. + fn estimate_cases() -> [(Element, Element, TreeType); 7] { + [ ( Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), Element::new_item_with_flags(vec![8; 60], owned_flags(2, NEW_OWNER)), @@ -957,35 +1004,67 @@ mod tests { Element::empty_tree_with_flags(owned_flags(2, NEW_OWNER)), TreeType::NormalTree, ), - ]; - for (old, new, tree_type) in cases { - let db = grove_with(&old, tree_type, grove_version); - let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) - .expect("expected the settling update to apply"); + ] + } - let replace = - QualifiedGroveDbOp::replace_op(vec![b"tree".to_vec()], KEY.to_vec(), new.clone()); - let mut ops = vec![write_op(&new), replace]; - if let Element::Item(..) = new { - ops.push(QualifiedGroveDbOp::patch_op( - vec![b"tree".to_vec()], - KEY.to_vec(), - new.clone(), - 0, - )); - } - if let Element::Reference(path, max_hop, flags) = &new { - ops.push(QualifiedGroveDbOp::refresh_reference_op( - vec![b"tree".to_vec()], + /// Every write of `new` at `key1` that may settle an owner change. + fn settling_writes(new: &Element) -> Vec<(&'static str, QualifiedGroveDbOp)> { + let path = vec![b"tree".to_vec()]; + let mut ops = vec![ + ("insert_or_replace", write_op(new)), + ( + "insert_or_replace_dont_check", + write_op(new).dont_check_for_backwards_references(), + ), + ( + "replace", + QualifiedGroveDbOp::replace_op(path.clone(), KEY.to_vec(), new.clone()), + ), + ( + "replace_dont_check", + QualifiedGroveDbOp::replace_op(path.clone(), KEY.to_vec(), new.clone()) + .dont_check_for_backwards_references(), + ), + ]; + if let Element::Item(..) = new { + ops.push(( + "patch", + QualifiedGroveDbOp::patch_op(path.clone(), KEY.to_vec(), new.clone(), 0), + )); + ops.push(( + "patch_dont_check", + QualifiedGroveDbOp::patch_op(path.clone(), KEY.to_vec(), new.clone(), 0) + .dont_check_for_backwards_references(), + )); + } + if let Element::Reference(reference_path, max_hop, flags) = new { + ops.push(( + "refresh_trusted", + QualifiedGroveDbOp::refresh_reference_op( + path, KEY.to_vec(), - path.clone(), + reference_path.clone(), *max_hop, flags.clone(), false, true, - )); - } - for op in ops { + ), + )); + } + ops + } + + /// With the option, the average-case and worst-case estimates of every + /// write that may settle an owner change are never below the settled + /// apply. + #[test] + fn estimates_with_the_option_cover_a_settled_owner_change() { + let grove_version = GroveVersion::latest(); + for (old, new, tree_type) in estimate_cases() { + let db = grove_with(&old, tree_type, grove_version); + let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) + .expect("expected the settling update to apply"); + for (label, op) in settling_writes(&new) { for average_case_tree_type in [Some(tree_type), None] { let settling = estimate( vec![op.clone()], @@ -997,32 +1076,409 @@ mod tests { settling .storage_cost .worse_or_eq_than(&applied.storage_cost), - "estimate {settling:?} of {op:?} is below the settled apply {applied:?}" - ); - assert!( - settling.storage_cost.added_bytes >= applied.storage_cost.added_bytes, - "estimate {settling:?} of {op:?} adds less than the settled apply \ + "estimate {settling:?} of {label} of {new:?} is below the settled apply \ {applied:?}" ); - // without the option the estimate is what it always was - let merging = estimate( - vec![op.clone()], - Some(options(Mode::Merging)), - average_case_tree_type, - grove_version, - ); - assert_eq!( - merging, - estimate( + } + } + } + } + + /// The estimates of those writes without the option, as they were + /// before it existed: (case, write, average case, seeks, added bytes, + /// replaced bytes, loaded bytes, hash calls), taken from develop at + /// a720f7c1. + const ESTIMATES_BEFORE_THE_OPTION: &[(usize, &str, bool, u32, u32, u32, u64, u32)] = &[ + (0, "insert_or_replace", true, 34, 243, 4289, 6170, 79), + ( + 0, + "insert_or_replace", + false, + 506892, + 243, + 4294967295, + 27857252525, + 87703315, + ), + ( + 0, + "insert_or_replace_dont_check", + true, + 13, + 243, + 1047, + 1379, + 23, + ), + ( + 0, + "insert_or_replace_dont_check", + false, + 12, + 243, + 409674, + 394925, + 275, + ), + (0, "replace", true, 34, 0, 4568, 6170, 80), + ( + 0, + "replace", + false, + 506892, + 206, + 4294967295, + 27857252525, + 87703315, + ), + (0, "replace_dont_check", true, 13, 0, 1326, 1379, 24), + (0, "replace_dont_check", false, 12, 206, 409711, 394925, 275), + (0, "patch", true, 34, 0, 4568, 6170, 80), + ( + 0, + "patch", + false, + 506892, + 206, + 4294967295, + 27857252525, + 87703315, + ), + (0, "patch_dont_check", true, 13, 0, 1326, 1379, 24), + (0, "patch_dont_check", false, 12, 206, 409711, 394925, 275), + (1, "insert_or_replace", true, 34, 187, 4289, 6170, 78), + ( + 1, + "insert_or_replace", + false, + 506892, + 187, + 4294967295, + 27857252525, + 87703314, + ), + ( + 1, + "insert_or_replace_dont_check", + true, + 13, + 187, + 1047, + 1379, + 22, + ), + ( + 1, + "insert_or_replace_dont_check", + false, + 12, + 187, + 409674, + 394925, + 274, + ), + (1, "replace", true, 34, 0, 4513, 6170, 79), + ( + 1, + "replace", + false, + 506892, + 150, + 4294967295, + 27857252525, + 87703314, + ), + (1, "replace_dont_check", true, 13, 0, 1271, 1379, 23), + (1, "replace_dont_check", false, 12, 150, 409711, 394925, 274), + (1, "patch", true, 34, 0, 4513, 6170, 79), + ( + 1, + "patch", + false, + 506892, + 150, + 4294967295, + 27857252525, + 87703314, + ), + (1, "patch_dont_check", true, 13, 0, 1271, 1379, 23), + (1, "patch_dont_check", false, 12, 150, 409711, 394925, 274), + (2, "insert_or_replace", true, 34, 245, 4545, 6578, 79), + ( + 2, + "insert_or_replace", + false, + 506892, + 229, + 4294967295, + 27857252525, + 87703315, + ), + ( + 2, + "insert_or_replace_dont_check", + true, + 13, + 245, + 1095, + 1451, + 23, + ), + ( + 2, + "insert_or_replace_dont_check", + false, + 12, + 229, + 409674, + 394925, + 275, + ), + (2, "replace", true, 34, 0, 4790, 6578, 79), + ( + 2, + "replace", + false, + 506892, + 192, + 4294967295, + 27857252525, + 87703315, + ), + (2, "replace_dont_check", true, 13, 0, 1340, 1451, 23), + (2, "replace_dont_check", false, 12, 192, 409711, 394925, 275), + (3, "insert_or_replace", true, 34, 198, 4545, 6578, 78), + ( + 3, + "insert_or_replace", + false, + 506892, + 182, + 4294967295, + 27857252525, + 87703314, + ), + ( + 3, + "insert_or_replace_dont_check", + true, + 13, + 198, + 1095, + 1451, + 22, + ), + ( + 3, + "insert_or_replace_dont_check", + false, + 12, + 182, + 409674, + 394925, + 274, + ), + (3, "replace", true, 34, 0, 4751, 6578, 78), + ( + 3, + "replace", + false, + 506892, + 0, + 4294967295, + 27857252525, + 87703314, + ), + (3, "replace_dont_check", true, 13, 0, 1301, 1451, 22), + (3, "replace_dont_check", false, 12, 0, 409864, 394925, 274), + (4, "insert_or_replace", true, 34, 223, 4289, 6170, 79), + ( + 4, + "insert_or_replace", + false, + 506892, + 223, + 4294967295, + 27857252525, + 87703315, + ), + ( + 4, + "insert_or_replace_dont_check", + true, + 13, + 223, + 1047, + 1379, + 23, + ), + ( + 4, + "insert_or_replace_dont_check", + false, + 12, + 223, + 409674, + 394925, + 275, + ), + (4, "replace", true, 34, 0, 4512, 6170, 79), + ( + 4, + "replace", + false, + 506892, + 186, + 4294967295, + 27857252525, + 87703315, + ), + (4, "replace_dont_check", true, 13, 0, 1270, 1379, 23), + (4, "replace_dont_check", false, 12, 186, 409711, 394925, 275), + (4, "refresh_trusted", true, 13, 0, 1270, 1379, 23), + (4, "refresh_trusted", false, 12, 186, 409711, 394925, 275), + (5, "insert_or_replace", true, 34, 240, 4545, 6578, 79), + ( + 5, + "insert_or_replace", + false, + 506892, + 224, + 4294967295, + 27857252525, + 87703315, + ), + ( + 5, + "insert_or_replace_dont_check", + true, + 13, + 240, + 1095, + 1451, + 23, + ), + ( + 5, + "insert_or_replace_dont_check", + false, + 12, + 224, + 409674, + 394925, + 275, + ), + (5, "replace", true, 34, 0, 4785, 6578, 79), + ( + 5, + "replace", + false, + 506892, + 187, + 4294967295, + 27857252525, + 87703315, + ), + (5, "replace_dont_check", true, 13, 0, 1335, 1451, 23), + (5, "replace_dont_check", false, 12, 187, 409711, 394925, 275), + (6, "insert_or_replace", true, 34, 151, 4289, 6170, 80), + ( + 6, + "insert_or_replace", + false, + 506892, + 151, + 4294967295, + 27857252525, + 87703316, + ), + ( + 6, + "insert_or_replace_dont_check", + true, + 13, + 151, + 1047, + 1379, + 24, + ), + ( + 6, + "insert_or_replace_dont_check", + false, + 12, + 151, + 409674, + 394925, + 276, + ), + (6, "replace", true, 34, 0, 4440, 6170, 80), + ( + 6, + "replace", + false, + 506892, + 0, + 4294967295, + 27857252525, + 87703316, + ), + (6, "replace_dont_check", true, 13, 0, 1198, 1379, 24), + (6, "replace_dont_check", false, 12, 0, 409825, 394925, 276), + ]; + + /// Without the option, every estimate is what it was before the option + /// existed. + #[test] + fn estimates_without_the_option_are_unchanged() { + let grove_version = GroveVersion::latest(); + let mut pinned = ESTIMATES_BEFORE_THE_OPTION.iter(); + for (case, (_, new, tree_type)) in estimate_cases().into_iter().enumerate() { + for (label, op) in settling_writes(&new) { + for average_case in [true, false] { + let average_case_tree_type = average_case.then_some(tree_type); + for batch_apply_options in [None, Some(options(Mode::Merging))] { + let estimate = estimate( vec![op.clone()], - None, + batch_apply_options, average_case_tree_type, - grove_version - ) - ); + grove_version, + ); + let &( + pinned_case, + pinned_label, + pinned_average_case, + seek_count, + added_bytes, + replaced_bytes, + storage_loaded_bytes, + hash_node_calls, + ) = pinned.clone().next().expect("expected a pinned estimate"); + assert_eq!( + (pinned_case, pinned_label, pinned_average_case), + (case, label, average_case) + ); + assert_eq!( + estimate, + OperationCost { + seek_count, + storage_cost: grovedb_costs::storage_cost::StorageCost { + added_bytes, + replaced_bytes, + removed_bytes: NoStorageRemoval, + }, + storage_loaded_bytes, + hash_node_calls, + sinsemilla_hash_calls: 0, + }, + "case {case}, {label}, average case {average_case}" + ); + } + pinned.next(); } } } + assert!(pinned.next().is_none(), "expected every pinned estimate"); } /// A replacement estimated without the option charges no added bytes, @@ -1061,4 +1517,94 @@ mod tests { merging.storage_cost.replaced_bytes ); } + + /// A flagged tree written together with a write under it reaches the + /// estimators as the `InsertTreeWithRootHash` its propagation makes of + /// it; with the option that op too is charged the insertion a settled + /// owner change records, whatever tree it lands in. + #[test] + fn estimates_with_the_option_cover_a_settled_tree_written_with_its_children() { + let grove_version = GroveVersion::latest(); + for parent_tree in [ + Element::empty_tree(), + Element::empty_sum_tree(), + Element::empty_big_sum_tree(), + Element::empty_count_tree(), + ] { + let parent_tree_type = parent_tree.tree_type().expect("expected a tree"); + let db = make_empty_grovedb(); + db.insert( + EMPTY_PATH, + b"parent", + parent_tree, + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the parent tree"); + db.insert( + [b"parent".as_slice()].as_ref(), + b"tree", + Element::empty_tree_with_flags(owned_flags(0, OLD_OWNER)), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the tree"); + db.insert( + [b"parent".as_slice(), b"tree"].as_ref(), + b"child", + Element::new_item(b"child".to_vec()), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the child"); + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"parent".to_vec()], + b"tree".to_vec(), + Element::empty_tree_with_flags(owned_flags(2, NEW_OWNER)), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"parent".to_vec(), b"tree".to_vec()], + b"another child".to_vec(), + Element::new_item(b"another child".to_vec()), + ), + ]; + let applied = apply(&db, ops.clone(), Mode::Settling, grove_version) + .expect("expected the settling update to apply"); + + let mut paths = HashMap::new(); + paths.insert(KeyInfoPath(vec![]), MaxElementsNumber(2)); + paths.insert( + KeyInfoPath::from_known_path([b"parent".as_slice()]), + MaxElementsNumber(2), + ); + paths.insert( + KeyInfoPath::from_known_path([b"parent".as_slice(), b"tree"]), + MaxElementsNumber(2), + ); + let worst = GroveDb::estimated_case_operations_for_batch( + WorstCaseCostsType(paths), + ops, + Some(options(Mode::Settling)), + |_cost, _old_flags, _new_flags| Ok(false), + |_flags, _removed_key_bytes, _removed_value_bytes| { + Ok((NoStorageRemoval, NoStorageRemoval)) + }, + grove_version, + ) + .cost_as_result() + .expect("expected an estimate"); + assert!( + worst.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "in a {parent_tree_type:?}: worst-case estimate {worst:?} is below the settled \ + apply {applied:?}" + ); + } + } } diff --git a/merk/src/tree/just_in_time_value_update.rs b/merk/src/tree/just_in_time_value_update/mod.rs similarity index 68% rename from merk/src/tree/just_in_time_value_update.rs rename to merk/src/tree/just_in_time_value_update/mod.rs index 1c63994fa..157762726 100644 --- a/merk/src/tree/just_in_time_value_update.rs +++ b/merk/src/tree/just_in_time_value_update/mod.rs @@ -1,12 +1,31 @@ +//! The just-in-time value update a replacement of a stored value runs: the +//! client callbacks carry the old storage flags into the size measurement, +//! rewrite the new value's flags, and section the bytes the replacement +//! frees. +//! +//! Each `v*.rs` defines a `just_in_time_tree_node_value_update_v*`. The +//! dispatcher below selects it by +//! `GroveVersion::merk_versions.tree.just_in_time_value_update`: +//! +//! - version 0 keeps the first measurement, taken with the new value +//! carrying the OLD value's flags, when the flags-update callback answers +//! `Unchanged`. A new value stored with flags of a different length is +//! then charged for bytes it does not hold, and the commit fails with a +//! storage cost mismatch. Grove v1..v3 are consensus-locked to this. +//! - version 1 measures the replacement again from the bytes it stores when +//! their size differs from that measurement, and charges every update that +//! version 0 accepts exactly as version 0 does. + +mod v0; +mod v1; + use grovedb_costs::storage_cost::{ - key_value_cost::KeyValueStorageCost, - removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, - transition::ElementFlagsUpdate, - StorageCost, + key_value_cost::KeyValueStorageCost, removal::StorageRemovedBytes, + transition::ElementFlagsUpdate, StorageCost, }; +use grovedb_version::{error::GroveVersionError, version::GroveVersion}; use crate::{ - merk::defaults::MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES, tree::{ kv::{ValueDefinedCostType, KV}, TreeFeatureType, TreeNode, TreeNodeInner, NULL_HASH, @@ -15,27 +34,29 @@ use crate::{ }; impl TreeNode { - /// The value bytes an apply finally stores when an - /// [`Op::PutWithProvidedValueHash`](crate::tree::Op::PutWithProvidedValueHash) - /// of `new_value` replaces the stored `old_value` — after the - /// just-in-time value update has run the client callbacks (storage-flags - /// carry-over and rewrite). + /// The value bytes an apply finally stores when a put of `new_value` + /// replaces the stored `old_value` — after the just-in-time value update + /// has run the client callbacks (storage-flags carry-over and rewrite). /// /// Runs the apply's own steps on a detached node: the node holding - /// `old_value` takes the new value and feature type exactly as - /// `put_value_with_provided_value_hash` installs them (which drops any - /// value-defined cost the predecessor carried), and then goes through - /// [`Self::just_in_time_tree_node_value_update`]. With the same - /// (deterministic) callbacks the result is byte-for-byte what the apply - /// writes, so a caller that must commit to the final bytes BEFORE the - /// apply — a backward-references referrer holding its target's hash — - /// can compute them. + /// `old_value` takes the new value and feature type exactly as the put + /// installs them, and then goes through + /// [`Self::just_in_time_tree_node_value_update`]. `value_defined_cost` is + /// the cost the put stamps on the node: `None` for an ordinary put + /// (`Op::Put` from merk tree version 1 on, and + /// [`Op::PutWithProvidedValueHash`](crate::tree::Op::PutWithProvidedValueHash), + /// which drop any cost the predecessor carried), and the op's own cost + /// for `Op::PutWithSpecializedCost`. With the same (deterministic) + /// callbacks the result is byte-for-byte what the apply writes, so a + /// caller that must commit to the final bytes BEFORE the apply — a + /// reference holding its target's hash — can compute them. #[allow(clippy::too_many_arguments)] pub fn provided_value_hash_put_final_value( key: Vec, old_value: Vec, new_value: Vec, feature_type: TreeFeatureType, + value_defined_cost: Option, old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, get_temp_new_value_with_old_flags: &impl Fn( &Vec, @@ -57,6 +78,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> Result, Error> { // The stored node (no hashes are needed: the update only measures // sizes and consults the callbacks). @@ -66,21 +88,31 @@ impl TreeNode { right: None, kv, }); - // The put, as `put_value_with_provided_value_hash` performs it. - node.inner.kv = node - .inner - .kv - .put_ordinary_value_no_update_of_hashes(new_value); + // The put, as the apply performs it. + node.inner.kv = match value_defined_cost { + None => node + .inner + .kv + .put_ordinary_value_no_update_of_hashes(new_value), + Some(value_defined_cost) => node + .inner + .kv + .put_value_with_fixed_cost_no_update_of_hashes(new_value, value_defined_cost), + }; node.inner.kv.feature_type = feature_type; node.just_in_time_tree_node_value_update( old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, )?; Ok(node.inner.kv.value) } + /// Runs the just-in-time value update on a node that has taken a new + /// value over a stored one. See the module docs for the version + /// semantics. pub(in crate::tree) fn just_in_time_tree_node_value_update( &mut self, old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, @@ -104,108 +136,40 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> Result<(), Error> { - let mut i = 0; - - if let Some(old_value) = self.old_value.clone() { - // At this point the tree value can be updated based on client requirements - // For example to store the costs - // todo: clean up clones - let original_new_value = self.value_ref().clone(); - - let new_value_with_old_flags = if self.inner.kv.value_defined_cost.is_none() { - // for items - get_temp_new_value_with_old_flags(&old_value, &original_new_value)? - } else { - // don't do this for sum items or trees - None - }; - - let (mut current_tree_plus_hook_size, mut storage_costs) = self - .kv_with_parent_hook_size_and_storage_cost_change_for_value( - old_specialized_cost, - new_value_with_old_flags, - )?; - - loop { - if let BasicStorageRemoval(removed_bytes) = - storage_costs.value_storage_cost.removed_bytes - { - let (_, value_removed_bytes) = - section_removal_bytes(&old_value, 0, removed_bytes)?; - storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; - } - - let (flags_update, value_defined_cost) = update_tree_value_based_on_costs( - &storage_costs.value_storage_cost, - &old_value, - self.value_mut_ref(), - )?; - match flags_update { - ElementFlagsUpdate::Unchanged => break, - ElementFlagsUpdate::SettleOwnerChange => { - // The value now holds the new element as the client - // left it, and the update is accounted as the removal - // of the old element plus the insertion of this one. - self.inner.kv.value_defined_cost = value_defined_cost; - self.known_storage_cost = Some(self.settled_owner_change_storage_cost( - &old_value, - old_specialized_cost, - section_removal_bytes, - )?); - self.old_value = Some(self.value_ref().clone()); - return Ok(()); - } - ElementFlagsUpdate::Changed => { - self.inner.kv.value_defined_cost = value_defined_cost; - let after_update_tree_plus_hook_size = - self.value_encoding_length_with_parent_to_child_reference(); - if after_update_tree_plus_hook_size == current_tree_plus_hook_size { - break; - } - // we are calling this with merged flags that are were put in through value - // mut ref - let new_size_and_storage_costs = - self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; - current_tree_plus_hook_size = new_size_and_storage_costs.0; - storage_costs = new_size_and_storage_costs.1; - self.set_value(original_new_value.clone()) - } - } - if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { - return Err(Error::CyclicError( - "updated value based on costs too many times", - )); - } - i += 1; - } - - if let BasicStorageRemoval(removed_bytes) = - storage_costs.value_storage_cost.removed_bytes - { - let (_, value_removed_bytes) = section_removal_bytes(&old_value, 0, removed_bytes)?; - storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; - } - self.known_storage_cost = Some(storage_costs); - } else { - let (_, storage_costs) = - self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; - self.known_storage_cost = Some(storage_costs); + match grove_version.merk_versions.tree.just_in_time_value_update { + 0 => self.just_in_time_tree_node_value_update_v0( + old_specialized_cost, + get_temp_new_value_with_old_flags, + update_tree_value_based_on_costs, + section_removal_bytes, + ), + 1 => self.just_in_time_tree_node_value_update_v1( + old_specialized_cost, + get_temp_new_value_with_old_flags, + update_tree_value_based_on_costs, + section_removal_bytes, + ), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "just_in_time_tree_node_value_update".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )), } - - self.old_value = Some(self.value_ref().clone()); - - Ok(()) } /// The storage an update that settles an owner change records: the old /// element removed, key included, with the removal sectioned through its /// flags exactly as a deletion sections it, and the new element as this - /// node now holds it added, key included. + /// node now holds it added, key included. `old_value_bytes` is the + /// stored value's own cost. fn settled_owner_change_storage_cost( &self, old_value: &Vec, - old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + old_value_bytes: u32, section_removal_bytes: &mut impl FnMut( &Vec, u32, @@ -216,7 +180,6 @@ impl TreeNode { >, ) -> Result { let key_bytes = KV::node_key_byte_cost_size(self.key().len() as u32); - let old_value_bytes = old_specialized_cost(self.key_as_ref(), old_value)?; let (removed_key_bytes, removed_value_bytes) = section_removal_bytes(old_value, key_bytes, old_value_bytes)?; Ok(KeyValueStorageCost { @@ -354,10 +317,12 @@ mod tests { old.clone(), new.clone(), BasicMerkNode, + None, &old_cost, &no_temp_value, &mut stamp_costs, &mut basic_removal, + grove_version, ) .expect("prediction"); assert_ne!(predicted, new, "the callback rewrote the flags"); @@ -425,6 +390,7 @@ mod tests { sectioned.push((value.clone(), key_bytes, value_bytes)); basic_removal(value, key_bytes, value_bytes) }, + grove_version, ) .expect("expected the update to settle"); diff --git a/merk/src/tree/just_in_time_value_update/v0.rs b/merk/src/tree/just_in_time_value_update/v0.rs new file mode 100644 index 000000000..af3e1bfe6 --- /dev/null +++ b/merk/src/tree/just_in_time_value_update/v0.rs @@ -0,0 +1,137 @@ +use grovedb_costs::storage_cost::{ + removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, + StorageCost, +}; + +use crate::{ + merk::defaults::MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES, + tree::{kv::ValueDefinedCostType, TreeNode}, + Error, +}; + +impl TreeNode { + /// Version 0 of the just-in-time value update, selected by + /// `merk_versions.tree.just_in_time_value_update == 0` (grove v1..v3). + /// + /// It differs from version 1 only in what an `Unchanged` answer keeps: + /// the measurement taken with the new value carrying the OLD value's + /// flags, whatever the length of the flags the new value is stored with. + pub(super) fn just_in_time_tree_node_value_update_v0( + &mut self, + old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + get_temp_new_value_with_old_flags: &impl Fn( + &Vec, + &Vec, + ) -> Result>, Error>, + update_tree_value_based_on_costs: &mut impl FnMut( + &StorageCost, + &Vec, + &mut Vec, + ) -> Result< + (ElementFlagsUpdate, Option), + Error, + >, + section_removal_bytes: &mut impl FnMut( + &Vec, + u32, + u32, + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + ) -> Result<(), Error> { + let mut i = 0; + + if let Some(old_value) = self.old_value.clone() { + // At this point the tree value can be updated based on client requirements + // For example to store the costs + // todo: clean up clones + let original_new_value = self.value_ref().clone(); + + let new_value_with_old_flags = if self.inner.kv.value_defined_cost.is_none() { + // for items + get_temp_new_value_with_old_flags(&old_value, &original_new_value)? + } else { + // don't do this for sum items or trees + None + }; + + let (mut current_tree_plus_hook_size, mut storage_costs) = self + .kv_with_parent_hook_size_and_storage_cost_change_for_value( + old_specialized_cost, + new_value_with_old_flags, + )?; + + loop { + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = + section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + + let (flags_update, value_defined_cost) = update_tree_value_based_on_costs( + &storage_costs.value_storage_cost, + &old_value, + self.value_mut_ref(), + )?; + match flags_update { + ElementFlagsUpdate::Unchanged => break, + ElementFlagsUpdate::SettleOwnerChange => { + // The value now holds the new element as the client + // left it, and the update is accounted as the removal + // of the old element plus the insertion of this one. + self.inner.kv.value_defined_cost = value_defined_cost; + let old_value_bytes = old_specialized_cost(self.key_as_ref(), &old_value)?; + self.known_storage_cost = Some(self.settled_owner_change_storage_cost( + &old_value, + old_value_bytes, + section_removal_bytes, + )?); + self.old_value = Some(self.value_ref().clone()); + return Ok(()); + } + ElementFlagsUpdate::Changed => { + self.inner.kv.value_defined_cost = value_defined_cost; + let after_update_tree_plus_hook_size = + self.value_encoding_length_with_parent_to_child_reference(); + if after_update_tree_plus_hook_size == current_tree_plus_hook_size { + break; + } + // we are calling this with merged flags that are were put in through value + // mut ref + let new_size_and_storage_costs = + self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; + current_tree_plus_hook_size = new_size_and_storage_costs.0; + storage_costs = new_size_and_storage_costs.1; + self.set_value(original_new_value.clone()) + } + } + if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { + return Err(Error::CyclicError( + "updated value based on costs too many times", + )); + } + i += 1; + } + + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + self.known_storage_cost = Some(storage_costs); + } else { + let (_, storage_costs) = + self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; + self.known_storage_cost = Some(storage_costs); + } + + self.old_value = Some(self.value_ref().clone()); + + Ok(()) + } +} diff --git a/merk/src/tree/just_in_time_value_update/v1.rs b/merk/src/tree/just_in_time_value_update/v1.rs new file mode 100644 index 000000000..1737e2197 --- /dev/null +++ b/merk/src/tree/just_in_time_value_update/v1.rs @@ -0,0 +1,157 @@ +use grovedb_costs::storage_cost::{ + removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, + StorageCost, +}; + +use crate::{ + merk::defaults::MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES, + tree::{kv::ValueDefinedCostType, TreeNode}, + Error, +}; + +impl TreeNode { + /// Version 1 of the just-in-time value update, selected by + /// `merk_versions.tree.just_in_time_value_update == 1` (grove v4+). + /// + /// It differs from version 0 only in what an `Unchanged` answer keeps: + /// when the value the update stores is not the size the last + /// measurement took (the first one sizes the new value with the OLD + /// value's flags), the replacement is measured again from the stored + /// bytes, so the recorded cost is the cost of what is written. The + /// stored value's own cost is also measured once rather than on every + /// measurement, which leaves every figure unchanged. + pub(super) fn just_in_time_tree_node_value_update_v1( + &mut self, + old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + get_temp_new_value_with_old_flags: &impl Fn( + &Vec, + &Vec, + ) -> Result>, Error>, + update_tree_value_based_on_costs: &mut impl FnMut( + &StorageCost, + &Vec, + &mut Vec, + ) -> Result< + (ElementFlagsUpdate, Option), + Error, + >, + section_removal_bytes: &mut impl FnMut( + &Vec, + u32, + u32, + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + ) -> Result<(), Error> { + let mut i = 0; + + if let Some(old_value) = self.old_value.clone() { + // At this point the tree value can be updated based on client requirements + // For example to store the costs + // todo: clean up clones + let original_new_value = self.value_ref().clone(); + + // The stored value does not change while the update runs, so its + // cost is measured once. + let old_value_bytes = old_specialized_cost(self.key_as_ref(), &old_value)?; + let old_cost = + |_key: &Vec, _value: &Vec| -> Result { Ok(old_value_bytes) }; + + let new_value_with_old_flags = if self.inner.kv.value_defined_cost.is_none() { + // for items + get_temp_new_value_with_old_flags(&old_value, &original_new_value)? + } else { + // don't do this for sum items or trees + None + }; + + let (mut current_tree_plus_hook_size, mut storage_costs) = self + .kv_with_parent_hook_size_and_storage_cost_change_for_value( + &old_cost, + new_value_with_old_flags, + )?; + + loop { + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = + section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + + let (flags_update, value_defined_cost) = update_tree_value_based_on_costs( + &storage_costs.value_storage_cost, + &old_value, + self.value_mut_ref(), + )?; + match flags_update { + ElementFlagsUpdate::Unchanged => { + // The value is stored as it stands. When that is not + // the size the last measurement took, the + // replacement is measured from its own bytes. + if self.value_encoding_length_with_parent_to_child_reference() + != current_tree_plus_hook_size + { + storage_costs = + self.kv_with_parent_hook_size_and_storage_cost(&old_cost)?.1; + } + break; + } + ElementFlagsUpdate::SettleOwnerChange => { + // The value now holds the new element as the client + // left it, and the update is accounted as the removal + // of the old element plus the insertion of this one. + self.inner.kv.value_defined_cost = value_defined_cost; + self.known_storage_cost = Some(self.settled_owner_change_storage_cost( + &old_value, + old_value_bytes, + section_removal_bytes, + )?); + self.old_value = Some(self.value_ref().clone()); + return Ok(()); + } + ElementFlagsUpdate::Changed => { + self.inner.kv.value_defined_cost = value_defined_cost; + let after_update_tree_plus_hook_size = + self.value_encoding_length_with_parent_to_child_reference(); + if after_update_tree_plus_hook_size == current_tree_plus_hook_size { + break; + } + // we are calling this with merged flags that are were put in through value + // mut ref + let new_size_and_storage_costs = + self.kv_with_parent_hook_size_and_storage_cost(&old_cost)?; + current_tree_plus_hook_size = new_size_and_storage_costs.0; + storage_costs = new_size_and_storage_costs.1; + self.set_value(original_new_value.clone()) + } + } + if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { + return Err(Error::CyclicError( + "updated value based on costs too many times", + )); + } + i += 1; + } + + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + self.known_storage_cost = Some(storage_costs); + } else { + let (_, storage_costs) = + self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; + self.known_storage_cost = Some(storage_costs); + } + + self.old_value = Some(self.value_ref().clone()); + + Ok(()) + } +} diff --git a/merk/src/tree/mod.rs b/merk/src/tree/mod.rs index fda8addf1..ac74ca280 100644 --- a/merk/src/tree/mod.rs +++ b/merk/src/tree/mod.rs @@ -1157,6 +1157,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); self.inner.kv = self.inner.kv.put_value_with_fixed_cost_no_update_of_hashes( @@ -1175,7 +1176,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -1244,7 +1246,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); // The provided hash was computed over the bytes supplied by the @@ -1380,6 +1383,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -1396,7 +1400,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -1439,6 +1444,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -1458,7 +1464,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } diff --git a/merk/src/tree/ops.rs b/merk/src/tree/ops.rs index 3519e6f7f..5e081518d 100644 --- a/merk/src/tree/ops.rs +++ b/merk/src/tree/ops.rs @@ -593,7 +593,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ) } @@ -641,6 +642,7 @@ where get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ) ) } @@ -670,6 +672,7 @@ where get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ) ) } diff --git a/merk/src/tree/put_value/mod.rs b/merk/src/tree/put_value/mod.rs index 38a681cb3..cbd95f20e 100644 --- a/merk/src/tree/put_value/mod.rs +++ b/merk/src/tree/put_value/mod.rs @@ -73,6 +73,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), 1 => self.put_value_v1( value, @@ -81,6 +82,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), version => Err(Error::VersionError( GroveVersionError::UnknownVersionMismatch { @@ -134,6 +136,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), 1 => self.put_value_and_reference_value_hash_v1( value, @@ -143,6 +146,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), version => Err(Error::VersionError( GroveVersionError::UnknownVersionMismatch { diff --git a/merk/src/tree/put_value/v0.rs b/merk/src/tree/put_value/v0.rs index 3eeb16531..96e63d5fd 100644 --- a/merk/src/tree/put_value/v0.rs +++ b/merk/src/tree/put_value/v0.rs @@ -9,6 +9,8 @@ use grovedb_costs::{ CostResult, CostsExt, OperationCost, }; +use grovedb_version::version::GroveVersion; + use crate::{ tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, Error, @@ -42,6 +44,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -58,7 +61,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -95,6 +99,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -111,7 +116,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } diff --git a/merk/src/tree/put_value/v1.rs b/merk/src/tree/put_value/v1.rs index e3834d258..77d91ff35 100644 --- a/merk/src/tree/put_value/v1.rs +++ b/merk/src/tree/put_value/v1.rs @@ -10,6 +10,8 @@ use grovedb_costs::{ CostResult, CostsExt, OperationCost, }; +use grovedb_version::version::GroveVersion; + use crate::{ tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, Error, @@ -43,6 +45,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -59,7 +62,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -96,6 +100,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -112,7 +117,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } diff --git a/merk/src/tree/walk/mod.rs b/merk/src/tree/walk/mod.rs index 5f44fba3b..bbac19877 100644 --- a/merk/src/tree/walk/mod.rs +++ b/merk/src/tree/walk/mod.rs @@ -292,6 +292,7 @@ where (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); cost_return_on_error_no_add!( @@ -304,7 +305,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version ) .unwrap_add_cost(&mut cost)) ); @@ -438,6 +440,7 @@ where (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); cost_return_on_error_no_add!( @@ -451,7 +454,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version ) .unwrap_add_cost(&mut cost)) ); @@ -489,6 +493,7 @@ where (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); cost_return_on_error_no_add!( @@ -503,7 +508,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version ) .unwrap_add_cost(&mut cost)) ); From 9a2e032aa5a69225ae26cdd8793afdc8701f9fbf Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 2 Oct 2026 07:56:45 +0700 Subject: [PATCH 03/10] fix(batch): second review fixes for settling storage owner changes Same-batch reference prediction (v1 of apply_batch.same_batch_reference_target_prediction): - A reference to a target this pass has already written now hashes the bytes it stores. d1c771eb replayed the just-in-time update over the landed bytes, so with Drive's merging callback an ItemWithSumItem grown by 1-3 bytes left a root-level or later-sorting reference stale on V4. - A partial-batch add-on drops the targets it writes again from that record. - Pending targets are predicted once per target and reused. - The sum-item <-> item refusal is gone; merk's routine predicts both directions exactly. - The put the apply performs comes from the new shared grovedb_merk::element::insert::specialized_put_cost. - The detached node is loaded with the stored node's value-defined cost and installs the value as put_value does at the grove version (the new PredictedPut), so the prediction matches the apply for any put_value version. Settle mode predicts with v1 on every grove version, and v0 is develop's body again. Merk just_in_time_value_update v1 restores the put's value-defined cost whenever a Changed round restores the put's value. Settle estimates size a settled tree written with its children by the written layer's declared tree type in the average case, and by the largest Merk-tree cost size in the worst case. flags_update_answers is gated on estimated_costs. Docs: - the determinism contract covers plain same-batch references; - the option-off guarantee is limited to GROVE_V1..V3; - slot and module docs corrected; - the CHANGELOG BREAKING list names the Walker and observer APIs. Tests: - a shared settle_test_support fixture; - after-target, cost-stamping and partial-batch reference cases; - sum-item/item swaps; - GROVE_V3 settle runs; - typed-tree estimates and a worst-case bound unit test; - option-off estimates pinned on GROVE_V3 as well, from develop a720f7c1. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 37 +- .../src/version/grovedb_versions.rs | 30 +- grovedb-version/src/version/merk_versions.rs | 5 +- .../estimated_costs/average_case_costs.rs | 24 +- grovedb/src/batch/estimated_costs/mod.rs | 90 ++- .../batch/estimated_costs/worst_case_costs.rs | 2 +- .../just_in_time_reference_update/mod.rs | 84 +- .../batch/just_in_time_reference_update/v0.rs | 97 +-- .../batch/just_in_time_reference_update/v1.rs | 74 +- .../src/batch/just_in_time_value_update.rs | 29 +- grovedb/src/batch/mod.rs | 74 +- grovedb/src/batch/options.rs | 7 +- .../same_batch_reference_prediction_tests.rs | 473 +++++++---- .../batch/settle_owner_change_cost_tests.rs | 762 +++++++++++------- grovedb/src/batch/settle_test_support.rs | 132 +++ .../backward_references_flags_update_tests.rs | 12 +- merk/src/element/insert.rs | 64 +- .../src/tree/just_in_time_value_update/mod.rs | 93 ++- merk/src/tree/just_in_time_value_update/v1.rs | 13 +- merk/src/tree/mod.rs | 2 + merk/src/tree/put_value/mod.rs | 27 +- merk/src/tree/put_value/v0.rs | 15 +- merk/src/tree/put_value/v1.rs | 15 +- 23 files changed, 1418 insertions(+), 743 deletions(-) create mode 100644 grovedb/src/batch/settle_test_support.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ffab75ca..1edcddc8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,8 +35,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 under, at least the bytes its node adds when inserted (for a backward-references element, with the referrers it carries over counted at its declared capacity, each at the largest entry registration admits), so - an estimate is never below a settled apply. With the option off, every - cost, estimate, stored element and hash is what it was before. + an estimate is never below a settled apply. With the option off on + GROVE_V1..V3, every cost, estimate, stored element and hash is what it was + before; GROVE_V4 also carries the two fixes below (same-batch references to + updated items, and replacements keeping flags of another length), which + change some outcomes with the option off too. A batch with the option + predicts a same-batch reference's target as GROVE_V4 does on every grove + version. `grovedb_merk::element::insert::specialized_put_cost` is new: the + value-defined cost an ordinary write puts an element with. ### Changed - **BREAKING**: `BatchApplyOptions` has a new public field, @@ -44,8 +50,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 every field without `..Default::default()` must add it. - **BREAKING**: Merk's just-in-time value update callback (the `update_tree_value_based_on_costs` argument of - `Merk::apply_with_costs_just_in_time_value_update`, `Merk::apply_unchecked` - and the `TreeNode` put functions) answers + `Merk::apply_with_costs_just_in_time_value_update`, `Merk::apply_unchecked`, + `Merk::apply_unchecked_with_old_value_observer`, `Walker::apply_to`, the + `TreeNode` put functions and the `Walker::put_value*` family) answers `(ElementFlagsUpdate, Option)` in place of `(bool, Option)`. GroveDB's own flags callback (in `apply_batch_with_element_flags_update`, @@ -59,7 +66,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `TreeNode::put_value_with_two_reference_value_hashes_and_value_cost` and their `Walker` counterparts take a `&GroveVersion`, and `TreeNode::provided_value_hash_put_final_value` takes the value-defined - cost the put stamps on the node and a `&GroveVersion`. + cost the stored node is loaded with, the put it predicts (the new + `grovedb_merk::tree::PredictedPut`: an ordinary put as `TreeNode::put_value` + installs it at the grove version, a provided-value-hash put, or a put at a + specialized cost) and a `&GroveVersion`. - **BREAKING**: `delete_operation_for_delete_internal` and `delete_operations_for_delete_up_tree_while_empty` take the operations already pending in the batch as any `impl PendingOperations` in place of a @@ -106,17 +116,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `ItemWithSumItem`) whose storage flags the flags update merges: an owned item updated in a later epoch, or an unowned one gaining an owner. The prediction assumed a sum item keeps its stored flags, while the apply - stores the merged ones, so the grove no longer verified. From GROVE_V4 - (`apply_batch.same_batch_reference_target_prediction`) the prediction runs - Merk's own just-in-time value update, so the reference commits to exactly - the stored bytes; GROVE_V1..V3 keep the old prediction. + stores the merged ones, so the grove no longer verified; it also refused a + sum item replaced by an item, or the reverse, that applies on its own. From + GROVE_V4 (`apply_batch.same_batch_reference_target_prediction`) a reference + to a target the batch has already written commits to the bytes it stores, + and one to a target still to be written commits to the bytes Merk's own + just-in-time value update predicts for the put the apply performs, once + per target; the reference commits to exactly the stored bytes whatever the + order the batch writes the two in. GROVE_V1..V3 keep the old prediction. - A replacement whose flags update answered `Unchanged` was charged as if the new value carried the old value's flags. When its own flags had a different length (a write with longer, shorter or no flags without a flags update, or an unowned item gaining an owner as it grows) the commit failed with a storage cost mismatch. From GROVE_V4 (`merk_versions.tree.just_in_time_value_update`) the replacement is - measured from the bytes it stores; GROVE_V1..V3 keep the old measurement. + measured from the bytes it stores, and a sum item or tree measured again + after its flags update rewrote and then restored its value is measured with + the value-defined cost of the bytes it holds; GROVE_V1..V3 keep the old + measurement. - From `GROVE_V4`, `delete_operation_for_delete_internal`, and the up-tree builders through it, no longer count a pending `DeleteTree` with `SubelementsDeletionBehavior::Skip` as removing its child when deciding diff --git a/grovedb-version/src/version/grovedb_versions.rs b/grovedb-version/src/version/grovedb_versions.rs index fbafce562..ec32e7fbb 100644 --- a/grovedb-version/src/version/grovedb_versions.rs +++ b/grovedb-version/src/version/grovedb_versions.rs @@ -249,20 +249,26 @@ pub struct GroveDBApplyBatchVersions { /// are versioned by their own `insert_on_transaction` and /// `delete_internal_on_transaction` slots. pub backward_references_maintenance: FeatureVersion, - /// How a reference written in a batch predicts the value hash of a - /// target item the same batch is still to update. + /// How a reference written in a batch gets the value hash of a target + /// item the same batch updates. /// /// - `0` (V1..V3): a hand-written copy of Merk's just-in-time value - /// update. For a sum item it assumes the new element keeps the stored - /// flags and does not consult the flags update, while the apply stores - /// the flags the flags update leaves, so when those differ (an owned - /// item updated in a later epoch, an unowned one gaining an owner) the - /// reference commits to bytes that are never stored. - /// - `1` (V4+): the prediction runs Merk's own just-in-time value update - /// on the stored and new bytes, with the value-defined cost the apply - /// puts the element with, so the reference commits to exactly the - /// bytes the apply stores, for every item kind and every answer of the - /// flags update. + /// update over the target's bytes as the Merk cache holds them. For a + /// sum item it assumes the new element keeps the stored flags and does + /// not consult the flags update, while the apply stores the flags the + /// flags update leaves, so when those differ (an owned item updated in + /// a later epoch, an unowned one gaining an owner) the reference + /// commits to bytes that are never stored. A sum item replaced by an + /// item, or the reverse, is refused. + /// - `1` (V4+): once the batch has written the target, the hash of the + /// bytes it stores; before, Merk's own just-in-time value update run on + /// the stored and new bytes with the put the apply performs, once per + /// target. The reference commits to exactly the bytes the apply stores, + /// for every item kind, every answer of the flags update and every + /// order the batch writes the reference and its target in. + /// + /// A batch with `settle_owner_changes` uses version `1` on every grove + /// version: the option has no legacy behaviour to keep. pub same_batch_reference_target_prediction: FeatureVersion, } diff --git a/grovedb-version/src/version/merk_versions.rs b/grovedb-version/src/version/merk_versions.rs index 93f1bed7a..842ffb879 100644 --- a/grovedb-version/src/version/merk_versions.rs +++ b/grovedb-version/src/version/merk_versions.rs @@ -42,8 +42,9 @@ pub struct MerkTreeVersions { /// /// Version 1 re-measures the replacement from the bytes it stores when /// the callback answers `Unchanged` and their size differs from the - /// first measurement. Every update that version 0 accepts is charged - /// the same. + /// last measurement taken, and restores the put's value-defined cost + /// whenever it restores the put's value. Every update whose cost + /// version 0 records for the bytes it stores is charged the same. pub just_in_time_value_update: FeatureVersion, } diff --git a/grovedb/src/batch/estimated_costs/average_case_costs.rs b/grovedb/src/batch/estimated_costs/average_case_costs.rs index 639e68db7..f6039ba56 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -20,7 +20,9 @@ use grovedb_merk::estimated_costs::{ add_cost_case_merk_replace_layered, add_cost_case_merk_replace_same_size, }; use grovedb_merk::{ - element::tree_type::ElementTreeTypeExtensions, tree::AggregateData, tree_type::TreeType, + element::tree_type::ElementTreeTypeExtensions, + tree::AggregateData, + tree_type::{CostSize, TreeType}, RootHashKeyAndAggregateData, }; #[cfg(feature = "minimal")] @@ -66,6 +68,7 @@ impl GroveOp { append_tree_chunk_power, backwards_references, propagate, + None, &BatchApplyOptions::default(), grove_version, ) @@ -96,6 +99,10 @@ impl GroveOp { // it declares `Check`. backwards_references: BackwardsReferences, propagate: bool, + // The declared tree type of the layer an `InsertTreeWithRootHash` + // writes, which sizes it when it settles an owner change (the op + // carries no aggregate data from which the type could be read). + written_tree_type: Option, // The batch's options: with `settle_owner_changes`, a write that may // settle an owner change is charged as the insertion it then records // too. @@ -268,7 +275,10 @@ impl GroveOp { insert_cost, key, flags, - aggregate_data.parent_tree_type(), + written_tree_type + .map_or(super::WORST_CASE_SETTLED_TREE_COST_SIZE, |tree_type| { + tree_type.cost_size() + }), wrapper_overhead, in_tree_type.inner_node_type(), ) @@ -1219,6 +1229,15 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { &mut cost, path, &key, tree_type, ); } + // The layer an `InsertTreeWithRootHash` writes, whose declared + // tree type sizes the tree when it settles an owner change. + let written_tree_type = if let GroveOp::InsertTreeWithRootHash { .. } = &op { + let mut written_layer = path.clone(); + written_layer.push(key.clone()); + self.paths.get(&written_layer).map(|layer| layer.tree_type) + } else { + None + }; cost_return_on_error!( &mut cost, op.average_case_cost_with_options( @@ -1228,6 +1247,7 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { append_tree_chunk_power, op.backwards_references(), false, + written_tree_type, batch_apply_options, grove_version ) diff --git a/grovedb/src/batch/estimated_costs/mod.rs b/grovedb/src/batch/estimated_costs/mod.rs index ecb054c9e..b3806daac 100644 --- a/grovedb/src/batch/estimated_costs/mod.rs +++ b/grovedb/src/batch/estimated_costs/mod.rs @@ -173,22 +173,30 @@ pub(in crate::batch) fn with_settled_owner_change( value.wrap_with_cost(cost) } +/// The largest cost size of a Merk tree an `InsertTreeWithRootHash` can +/// write (a count-sum tree's), which a worst-case estimate sizes a settled +/// tree with: the op carries no aggregate data from which the tree type +/// could be read. +#[cfg(feature = "minimal")] +pub(in crate::batch) const WORST_CASE_SETTLED_TREE_COST_SIZE: u32 = + grovedb_merk::tree_type::COUNT_SUM_TREE_COST_SIZE; + /// [`with_settled_owner_change`] for the `InsertTreeWithRootHash` that a /// write of a tree becomes when the batch also writes under it: `base` is -/// raised by the tree element that op writes — a tree of `tree_type` with -/// `flags`, and `wrapper_overhead` wrapper bytes — sized as the apply sizes -/// it in a tree of `node_type` nodes. +/// raised by the tree element that op writes — a tree of `tree_cost_size` +/// with `flags`, and `wrapper_overhead` wrapper bytes — sized as the apply +/// sizes it in a tree of `node_type` nodes. #[cfg(feature = "minimal")] pub(in crate::batch) fn with_settled_tree_owner_change( base: grovedb_costs::CostResult<(), crate::Error>, key: &crate::batch::KeyInfo, flags: &Option, - tree_type: grovedb_merk::tree_type::TreeType, + tree_cost_size: u32, wrapper_overhead: u32, node_type: grovedb_merk::merk::NodeType, ) -> grovedb_costs::CostResult<(), crate::Error> { use grovedb_costs::CostsExt; - use grovedb_merk::{tree::kv::KV, tree_type::CostSize}; + use grovedb_merk::tree::kv::KV; use grovedb_storage::worst_case_costs::WorstKeyLength; use integer_encoding::VarInt; @@ -197,10 +205,8 @@ pub(in crate::batch) fn with_settled_tree_owner_change( && let Some(flags) = flags { let flags_len = flags.len() as u32; - let value_cost = tree_type.cost_size() - + flags_len - + flags_len.required_space() as u32 - + wrapper_overhead; + let value_cost = + tree_cost_size + flags_len + flags_len.required_space() as u32 + wrapper_overhead; let settled_bytes = KV::layered_node_byte_cost_size_for_key_and_value_lengths( key.max_length() as u32, value_cost, @@ -798,3 +804,69 @@ pub enum EstimatedCostsType { /// Worst case estimated costs type WorstCaseCostsType(HashMap), } + +#[cfg(all(test, feature = "minimal"))] +mod tests { + use grovedb_costs::{CostsExt, OperationCost}; + use grovedb_merk::{ + merk::NodeType, + tree::kv::KV, + tree_type::{CostSize, TreeType}, + }; + + use crate::batch::KeyInfo; + + /// The worst-case size of a settled tree bounds the node of every Merk + /// tree an `InsertTreeWithRootHash` can write, in a tree of any node + /// type. + #[test] + fn the_worst_case_settled_tree_bounds_every_merk_tree_in_every_node_type() { + let key = KeyInfo::KnownKey(b"tree".to_vec()); + let flags = Some(vec![0; 35]); + let raised = super::with_settled_tree_owner_change( + Ok(()).wrap_with_cost(OperationCost::default()), + &key, + &flags, + super::WORST_CASE_SETTLED_TREE_COST_SIZE, + 0, + super::WORST_CASE_SETTLED_NODE_TYPE, + ) + .cost + .storage_cost + .added_bytes; + for tree_type in [ + TreeType::NormalTree, + TreeType::SumTree, + TreeType::BigSumTree, + TreeType::CountTree, + TreeType::CountSumTree, + TreeType::ProvableCountTree, + TreeType::ProvableCountSumTree, + TreeType::ProvableSumTree, + TreeType::ProvableCountProvableSumTree, + ] { + for node_type in [ + NodeType::NormalNode, + NodeType::SumNode, + NodeType::BigSumNode, + NodeType::CountNode, + NodeType::CountSumNode, + NodeType::ProvableCountNode, + NodeType::ProvableCountSumNode, + NodeType::ProvableSumNode, + NodeType::ProvableCountProvableSumNode, + ] { + let settled = KV::layered_node_byte_cost_size_for_key_and_value_lengths( + 4, + tree_type.cost_size() + 36, + node_type, + ); + assert!( + raised >= settled, + "a {tree_type:?} in a tree of {node_type:?} nodes adds {settled}, above \ + the worst-case {raised}" + ); + } + } + } +} diff --git a/grovedb/src/batch/estimated_costs/worst_case_costs.rs b/grovedb/src/batch/estimated_costs/worst_case_costs.rs index 3d1f68792..2c33b4c38 100644 --- a/grovedb/src/batch/estimated_costs/worst_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/worst_case_costs.rs @@ -254,7 +254,7 @@ impl GroveOp { insert_cost, key, flags, - aggregate_data.parent_tree_type(), + super::WORST_CASE_SETTLED_TREE_COST_SIZE, wrapper_overhead, super::WORST_CASE_SETTLED_NODE_TYPE, ) diff --git a/grovedb/src/batch/just_in_time_reference_update/mod.rs b/grovedb/src/batch/just_in_time_reference_update/mod.rs index 1b4c19fc4..bee636ed0 100644 --- a/grovedb/src/batch/just_in_time_reference_update/mod.rs +++ b/grovedb/src/batch/just_in_time_reference_update/mod.rs @@ -1,25 +1,34 @@ //! The value hashes a reference written in a batch commits to when its //! target is a pending write of the same batch. //! -//! [`TreeCacheMerkByPath::process_old_element_flags`] predicts the bytes a -//! pending item write finally stores, after the apply has run the caller's -//! flags update on it. Each `v*.rs` defines a `process_old_element_flags_v*`, -//! selected by `apply_batch.same_batch_reference_target_prediction`: +//! `TreeCacheMerkByPath::process_old_element_flags` gives the hash of the +//! bytes a pending item write finally stores, after the apply has run the +//! caller's flags update on it. Each `v*.rs` defines a +//! `process_old_element_flags_v*`, selected by +//! `apply_batch.same_batch_reference_target_prediction`: //! -//! - version 0 is a hand-written copy of Merk's just-in-time value update. -//! For a sum item it assumes the new element keeps the stored flags and -//! does not consult the flags update, while the apply stores the flags the -//! flags update leaves it. When those differ (an owned sum item updated in -//! a later epoch, an unowned one gaining an owner) the reference commits -//! to bytes that are never stored and the grove no longer verifies. Grove -//! v1..v3 are consensus-locked to this. -//! - version 1 runs Merk's own just-in-time value update on the stored and -//! new bytes, with the put the apply performs, so the reference commits to -//! exactly the bytes the apply stores. +//! - version 0 is a hand-written copy of Merk's just-in-time value update +//! over the target's bytes as the Merk cache holds them. For a sum item it +//! assumes the new element keeps the stored flags and does not consult +//! the flags update, while the apply stores the flags the flags update +//! leaves it. When those differ (an owned sum item updated in a later +//! epoch, an unowned one gaining an owner) the reference commits to bytes +//! that are never stored and the grove no longer verifies. Grove v1..v3 +//! are consensus-locked to this. +//! - version 1 hashes the bytes the target stores once this pass has written +//! it, and otherwise predicts them by running Merk's own just-in-time value +//! update on the stored bytes and the new ones, with the put the apply +//! performs, once per target. The reference commits to exactly the bytes +//! the apply stores. +//! +//! `settle_owner_changes` is new and has no legacy behaviour to keep, so a +//! batch with it predicts with version 1 on every grove version. mod v0; mod v1; +use std::collections::HashMap; + use grovedb_costs::{ cost_return_on_error_into_no_add, storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, @@ -66,19 +75,25 @@ where Ok(val_hash).wrap_with_cost(cost) } - /// The value hash of the bytes a pending write of `new_element` over the - /// stored `old_element` finally stores, after the apply has run the - /// caller's flags update on it, so that a reference written in the same - /// batch commits to them. See the module docs for the version semantics. + /// The value hash of the bytes a pending write of `new_element` at + /// `qualified_path` over the stored `old_element` finally stores, after + /// the apply has run the caller's flags update on it, so that a + /// reference written in the same batch commits to them. `target_written` + /// says this pass has already written the target, so the stored bytes + /// are the ones it stores. See the module docs for the version + /// semantics. #[allow(clippy::too_many_arguments)] pub(crate) fn process_old_element_flags( key: &[u8], - serialized: &[u8], - new_element: &mut Element, + qualified_path: &[Vec], + serialized: Vec, + new_element: &Element, old_element: Element, - old_serialized_element: &[u8], + old_serialized_element: Vec, in_tree_type: TreeType, settle_owner_changes: bool, + target_written: bool, + predicted_targets: &mut HashMap>, Vec>, flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, @@ -95,35 +110,38 @@ where u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { - match grove_version - .grovedb_versions - .apply_batch - .same_batch_reference_target_prediction - { - 0 => v0::process_old_element_flags_v0( + match ( + grove_version + .grovedb_versions + .apply_batch + .same_batch_reference_target_prediction, + settle_owner_changes, + ) { + (0, false) => v0::process_old_element_flags_v0( key, - serialized, + &serialized, new_element, old_element, - old_serialized_element, + &old_serialized_element, in_tree_type, - settle_owner_changes, flags_update, split_removal_bytes, grove_version, ), - 1 => v1::process_old_element_flags_v1( + (0, true) | (1, _) => v1::process_old_element_flags_v1( key, + qualified_path, serialized, new_element, - &old_element, old_serialized_element, in_tree_type, + target_written, + predicted_targets, flags_update, split_removal_bytes, grove_version, ), - version => Err(Error::VersionError( + (version, _) => Err(Error::VersionError( GroveVersionError::UnknownVersionMismatch { method: "process_old_element_flags".to_string(), known_versions: vec![0, 1], diff --git a/grovedb/src/batch/just_in_time_reference_update/v0.rs b/grovedb/src/batch/just_in_time_reference_update/v0.rs index dc5ac5a3c..501f5c2b2 100644 --- a/grovedb/src/batch/just_in_time_reference_update/v0.rs +++ b/grovedb/src/batch/just_in_time_reference_update/v0.rs @@ -10,7 +10,6 @@ use grovedb_costs::{ CostResult, CostsExt, OperationCost, }; use grovedb_merk::{ - element::costs::ElementCostExtensions, tree::{kv::KV, value_hash, TreeNode}, tree_type::TreeType, CryptoHash, @@ -21,23 +20,22 @@ use crate::{batch::MerkError, Element, ElementFlags, Error}; /// Version 0 of the same-batch reference target prediction, selected by /// `apply_batch.same_batch_reference_target_prediction == 0` (grove -/// v1..v3): a hand-written copy of Merk's just-in-time value update. +/// v1..v3) for a batch without `settle_owner_changes`: a hand-written copy +/// of Merk's just-in-time value update. /// -/// It differs from version 1 in how it measures and asks the flags update. -/// A sum item is taken to keep its stored flags without consulting the -/// flags update (except, with `settle_owner_changes`, for an answer that -/// settles an owner change), while the apply stores the flags the flags -/// update leaves it, so a reference to a sum item whose flags change -/// commits to bytes that are never stored. +/// It differs from version 1 in how it gets the stored bytes. It measures +/// and asks the flags update itself, over the target's bytes as the Merk +/// cache holds them, and takes a sum item to keep its stored flags without +/// consulting the flags update, while the apply stores the flags the flags +/// update leaves it. #[allow(clippy::too_many_arguments)] pub(super) fn process_old_element_flags_v0( key: &[u8], serialized: &[u8], - new_element: &mut Element, + new_element: &Element, old_element: Element, old_serialized_element: &[u8], in_tree_type: TreeType, - settle_owner_changes: bool, flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, @@ -55,71 +53,9 @@ where ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { let mut cost = OperationCost::default(); - let client_error = |e: Error| -> Error { - match e { - Error::JustInTimeElementFlagsClientError(_) => { - MerkError::ClientCorruptionError(e.to_string()).into() - } - _ => MerkError::ClientCorruptionError("non client error".to_string()).into(), - } - }; if old_element.is_sum_item() { return if new_element.is_sum_item() { let maybe_old_flags = old_element.get_flags_owned(); - if settle_owner_changes - && maybe_old_flags.is_some() - && new_element.get_flags().is_some() - { - // Merk measures a sum item by its own specialized cost, - // new flags included, against the stored one's. - let node_type = in_tree_type.inner_node_type(); - let old_storage_cost = cost_return_on_error_no_add!( - cost, - Element::specialized_costs_for_key_value( - key, - old_serialized_element, - node_type, - grove_version, - ) - .map_err(Error::MerkError) - ); - let new_storage_cost = cost_return_on_error_no_add!( - cost, - Element::specialized_costs_for_key_value( - key, - serialized, - node_type, - grove_version, - ) - .map_err(Error::MerkError) - ); - let mut storage_costs = - TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); - if let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes { - let mut old_flags = maybe_old_flags.clone().unwrap_or_default(); - let (_, value_removed_bytes) = cost_return_on_error_no_add!( - cost, - split_removal_bytes(&mut old_flags, 0, removed_bytes) - ); - storage_costs.removed_bytes = value_removed_bytes; - } - let mut settled_element = new_element.clone(); - if let Some(new_flags) = settled_element.get_flags_mut().as_mut() { - let update = cost_return_on_error_no_add!( - cost, - (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) - .map_err(client_error) - ); - if update == ElementFlagsUpdate::SettleOwnerChange { - let settled_bytes = cost_return_on_error_into_no_add!( - cost, - settled_element.serialize(grove_version) - ); - let val_hash = value_hash(&settled_bytes).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } - } - } if maybe_old_flags.is_some() { let mut updated_new_element_with_old_flags = new_element.clone(); updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); @@ -211,8 +147,14 @@ where ); let update = cost_return_on_error_no_add!( cost, - (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags) - .map_err(client_error) + (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags).map_err(|e| { + match e { + Error::JustInTimeElementFlagsClientError(_) => { + MerkError::ClientCorruptionError(e.to_string()).into() + } + _ => MerkError::ClientCorruptionError("non client error".to_string()).into(), + } + }) ); if update == ElementFlagsUpdate::Unchanged { // There are no storage flags, we can just hash new element @@ -226,13 +168,6 @@ where new_element_cloned.serialize(grove_version) ); - if update == ElementFlagsUpdate::SettleOwnerChange { - // The apply stores the new element with the flags the - // callback left it, whatever its size. - let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } - new_storage_cost = KV::node_value_byte_cost_size( key.len() as u32, new_serialized_bytes.len() as u32, diff --git a/grovedb/src/batch/just_in_time_reference_update/v1.rs b/grovedb/src/batch/just_in_time_reference_update/v1.rs index 8d0d0642b..7584474dc 100644 --- a/grovedb/src/batch/just_in_time_reference_update/v1.rs +++ b/grovedb/src/batch/just_in_time_reference_update/v1.rs @@ -1,11 +1,13 @@ +use std::collections::HashMap; + use grovedb_costs::{ cost_return_on_error_into_no_add, cost_return_on_error_no_add, storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; use grovedb_merk::{ - element::{costs::ElementCostExtensions, tree_type::ElementTreeTypeExtensions}, - tree::{kv::ValueDefinedCostType::SpecializedValueDefinedCost, value_hash}, + element::{insert::specialized_put_cost, tree_type::ElementTreeTypeExtensions}, + tree::{value_hash, PredictedPut}, tree_type::TreeType, CryptoHash, }; @@ -16,21 +18,27 @@ use crate::{ }; /// Version 1 of the same-batch reference target prediction, selected by -/// `apply_batch.same_batch_reference_target_prediction == 1` (grove v4+). +/// `apply_batch.same_batch_reference_target_prediction == 1` (grove v4+), +/// and on every grove version for a batch with `settle_owner_changes`. /// -/// It differs from version 0 in how it gets the stored bytes: it runs -/// Merk's own just-in-time value update on the stored and new bytes, with -/// the put the apply performs (a sum item at its specialized cost, any other -/// item as an ordinary put), so the reference commits to exactly what the -/// apply stores whatever the flags update answers. +/// It differs from version 0 in how it gets the stored bytes. Once this pass +/// has written the target, `old_serialized_element` (the target's bytes as +/// the Merk cache holds them) are the bytes it stores. Before that, they are +/// the bytes the apply will update, and the stored bytes are predicted by +/// running Merk's own just-in-time value update on them and the new ones, +/// with the put the apply performs (`specialized_put_cost`), once per target. +/// The reference commits to exactly what the apply stores whatever the flags +/// update answers, and whether the target is a sum item or not. #[allow(clippy::too_many_arguments)] pub(super) fn process_old_element_flags_v1( key: &[u8], - serialized: &[u8], + qualified_path: &[Vec], + serialized: Vec, new_element: &Element, - old_element: &Element, - old_serialized_element: &[u8], + old_serialized_element: Vec, in_tree_type: TreeType, + target_written: bool, + predicted_targets: &mut HashMap>, Vec>, flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, @@ -48,33 +56,20 @@ where ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { let mut cost = OperationCost::default(); - match (old_element.is_sum_item(), new_element.is_sum_item()) { - (true, false) => { - return Err(Error::NotSupported( - "going from a sum item to a not sum item is not supported".to_string(), - )) - .wrap_with_cost(cost) - } - (false, true) => { - return Err(Error::NotSupported( - "going from an item to a sum item is not supported".to_string(), - )) - .wrap_with_cost(cost) - } - _ => {} + if target_written { + let val_hash = value_hash(&old_serialized_element).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); } - // The put the apply performs (`insert_into_batch_operations`). - let value_defined_cost = if new_element.is_sum_item() { - Some(SpecializedValueDefinedCost(cost_return_on_error_no_add!( - cost, - new_element - .specialized_value_defined_cost(grove_version) - .ok_or(Error::CorruptedCodeExecution( - "sum items should always have a value defined cost" - )) - ))) - } else { - None + if let Some(stored_bytes) = predicted_targets.get(qualified_path) { + let val_hash = value_hash(stored_bytes).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + let put = match cost_return_on_error_no_add!( + cost, + specialized_put_cost(new_element, grove_version).map_err(Error::MerkError) + ) { + Some(value_cost) => PredictedPut::SpecializedCost(value_cost), + None => PredictedPut::Ordinary, }; let feature_type = cost_return_on_error_into_no_add!(cost, new_element.get_feature_type(in_tree_type)); @@ -83,9 +78,9 @@ where predict_put_final_bytes( key, old_serialized_element, - serialized.to_vec(), + serialized, feature_type, - value_defined_cost, + put, in_tree_type, flags_update, split_removal_bytes, @@ -96,5 +91,6 @@ where ))) ); let val_hash = value_hash(&stored_bytes).unwrap_add_cost(&mut cost); + predicted_targets.insert(qualified_path.to_vec(), stored_bytes); Ok(val_hash).wrap_with_cost(cost) } diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index fdcd206e6..31df8211b 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -23,7 +23,7 @@ use grovedb_merk::{ ValueDefinedCostType, ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, }, - TreeFeatureType, TreeNode, + PredictedPut, TreeFeatureType, TreeNode, }, tree_type::{CostSize, TreeType, SUM_ITEM_COST_SIZE}, Error as MerkError, @@ -65,6 +65,7 @@ where /// The caller's flags-update callback with its answer as an /// [`ElementFlagsUpdate`] (a `bool` answer is `Changed` or `Unchanged`). +#[cfg(feature = "estimated_costs")] pub(crate) fn flags_update_answers( mut flags_update: impl FnMut( &StorageCost, @@ -261,20 +262,20 @@ where } } -/// The bytes a batch apply finally stores when it puts `new_serialized` -/// (with `feature_type`, and the value-defined cost `value_defined_cost` the -/// put stamps on the node) over the stored `old_serialized` bytes at `key` in -/// a subtree of `in_tree_type`, with the caller's callbacks: Merk's own -/// just-in-time value update, run on a detached node. +/// The bytes a batch apply finally stores when `put` of `new_serialized` +/// (with `feature_type`) replaces the stored `old_serialized` bytes at `key` +/// in a subtree of `in_tree_type`, with the caller's callbacks: Merk's own +/// just-in-time value update, run on a detached node loaded as the apply +/// loads the stored node. /// /// The callbacks must answer the apply the same way they answer here. #[allow(clippy::too_many_arguments)] pub(crate) fn predict_put_final_bytes( key: &[u8], - old_serialized: &[u8], + old_serialized: Vec, new_serialized: Vec, feature_type: TreeFeatureType, - value_defined_cost: Option, + put: PredictedPut, in_tree_type: TreeType, flags_update: &mut G, split_removal_bytes: &mut SR, @@ -292,12 +293,15 @@ where u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { + let old_value_defined_cost = + Element::value_defined_cost_for_serialized_value(&old_serialized, grove_version); TreeNode::provided_value_hash_put_final_value( key.to_vec(), - old_serialized.to_vec(), + old_serialized, + old_value_defined_cost, new_serialized, feature_type, - value_defined_cost, + put, &|key, value| old_specialized_cost(key, value, in_tree_type, grove_version), &|old_value, new_value| new_value_with_old_flags(old_value, new_value, grove_version), &mut |storage_costs, old_value, new_value| { @@ -352,13 +356,12 @@ where { let new_serialized = new_element.serialize(grove_version)?; let feature_type = new_element.get_feature_type(in_tree_type)?; - // A provided-value-hash put drops any value-defined cost. let final_bytes = predict_put_final_bytes( key, - old_serialized, + old_serialized.to_vec(), new_serialized.clone(), feature_type, - None, + PredictedPut::ProvidedValueHash, in_tree_type, flags_update, split_removal_bytes, diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 149f8c4f0..d61b2afe3 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -36,6 +36,8 @@ mod same_batch_reference_prediction_tests; #[cfg(test)] mod settle_owner_change_cost_tests; #[cfg(test)] +pub(crate) mod settle_test_support; +#[cfg(test)] mod single_deletion_cost_tests; #[cfg(test)] mod single_insert_cost_tests; @@ -1968,9 +1970,20 @@ struct TreeCacheMerkByPath { /// move is physically a delete plus an insert but logically an update. indexed_mirror_rekey_churn_bytes: u32, /// The batch's `settle_owner_changes` option: a flags update may settle - /// an owner change, so the hash a same-batch reference commits to must - /// ask the flags update about a sum item too. + /// an owner change, so a same-batch reference to a pending item write + /// predicts the bytes it stores through Merk's own just-in-time value + /// update on every grove version (see `just_in_time_reference_update`). settle_owner_changes: bool, + /// The qualified paths of the targets this pass has already written. A + /// same-batch reference to one of them commits to the bytes it now + /// stores instead of predicting them (from + /// `apply_batch.same_batch_reference_target_prediction` version 1). + written_qualified_paths: HashSet>>, + /// The bytes predicted for a pending item write that a same-batch + /// reference points at, by the target's qualified path, so references + /// sharing a target predict it once (from + /// `apply_batch.same_batch_reference_target_prediction` version 1). + predicted_reference_targets: HashMap>, Vec>, } impl fmt::Debug for TreeCacheMerkByPath { @@ -2827,8 +2840,6 @@ where let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); Ok(val_hash).wrap_with_cost(cost) } else { - let mut new_element = element.clone(); - // it can be unmerged, let's get the value on disk let (key, reference_path) = qualified_path .split_last() @@ -2844,16 +2855,21 @@ where if let Some((old_element, old_serialized_element, is_in_sum_tree)) = serialized_element_result { + let target_written = + self.written_qualified_paths.contains(qualified_path); let value_hash = cost_return_on_error!( &mut cost, Self::process_old_element_flags( key, - &serialized, - &mut new_element, + qualified_path, + serialized, + element, old_element, - &old_serialized_element, + old_serialized_element, is_in_sum_tree, self.settle_owner_changes, + target_written, + &mut self.predicted_reference_targets, flags_update, split_removal_bytes, grove_version, @@ -3210,6 +3226,16 @@ where let p = path.to_path(); let path = &p; self.unused_new_merks.remove(path); + // The targets this path writes, recorded as written once it has + // applied them. + let written_qualified_paths: Vec>> = ops_at_path_by_key + .keys() + .map(|key_info| { + let mut qualified_path = path.to_vec(); + qualified_path.push(key_info.get_key_clone()); + qualified_path + }) + .collect(); // This also populates Merk trees cache let in_tree_type = { @@ -5047,6 +5073,7 @@ where .insert(path.to_vec(), per_axis); } + self.written_qualified_paths.extend(written_qualified_paths); let merk = self.merks.get_mut(path).expect("the Merk is cached"); merk.root_hash_key_and_aggregate_data() .add_cost(cost) @@ -5640,6 +5667,8 @@ impl GroveDb { settle_owner_changes: BatchApplyOptions::settle_owner_changes_in( &batch_apply_options, ), + written_qualified_paths: Default::default(), + predicted_reference_targets: Default::default(), }, grove_version ) @@ -5710,6 +5739,19 @@ impl GroveDb { merk_tree_cache.merks.remove(&path); } } + // The additional ops write their targets again, so a reference to one + // predicts the bytes it stores from what the first segment left, and + // no prediction made before the continuation still holds. + for op in &additional_ops { + if let Some(key) = &op.key { + let mut qualified_path = op.path.to_path(); + qualified_path.push(key.get_key_clone()); + merk_tree_cache + .written_qualified_paths + .remove(&qualified_path); + } + } + merk_tree_cache.predicted_reference_targets.clear(); let mut cost = OperationCost::default(); let batch_structure = cost_return_on_error!( &mut cost, @@ -6747,11 +6789,19 @@ impl GroveDb { /// Applies batch of operations on GroveDB /// /// `update_element_flags_function` and `split_removal_bytes_function` - /// run whenever a write replaces a stored value. They must be - /// deterministic: under backward-references maintenance the batch - /// predicts the bytes a flagged item will store (its referrers commit to - /// them) by consulting the same callbacks before the apply, and refuses - /// the batch if the apply stores anything else. + /// run whenever a write replaces a stored value, and may run more than + /// once for one write. They must be deterministic, answering the same + /// inputs the same way every time: the batch predicts the bytes a + /// flagged item will store by consulting the same callbacks before the + /// apply, wherever something commits to those bytes first. + /// + /// - A reference the batch writes or refreshes to an item the batch + /// also updates commits to the predicted bytes. Nothing checks them + /// after the apply, so a callback that answers the apply differently + /// leaves the reference committed to bytes that are never stored. + /// - Under backward-references maintenance the referrers of a flagged + /// backward-references item commit to them, and the batch refuses the + /// batch if the apply stores anything else. /// /// The flags callback answers a `bool` (whether it rewrote the flags) or /// an [`ElementFlagsUpdate`]; `SettleOwnerChange` is accepted only when diff --git a/grovedb/src/batch/options.rs b/grovedb/src/batch/options.rs index 128226823..e37abcca1 100644 --- a/grovedb/src/batch/options.rs +++ b/grovedb/src/batch/options.rs @@ -98,8 +98,11 @@ pub struct BatchApplyOptions { /// capacity, each at the largest entry registration admits, so the /// estimate is never below what the apply records. /// - /// Off by default, and every cost is then what it was before the option - /// existed. + /// Off by default. On GROVE_V1..V3 every cost, estimate, stored element + /// and hash is then what it was before the option existed; GROVE_V4 also + /// carries the fixes of `apply_batch.same_batch_reference_target_prediction` + /// and `merk_versions.tree.just_in_time_value_update`, which change + /// some outcomes with the option off too. pub settle_owner_changes: bool, } diff --git a/grovedb/src/batch/same_batch_reference_prediction_tests.rs b/grovedb/src/batch/same_batch_reference_prediction_tests.rs index 5a7ff4f21..03541c3ad 100644 --- a/grovedb/src/batch/same_batch_reference_prediction_tests.rs +++ b/grovedb/src/batch/same_batch_reference_prediction_tests.rs @@ -7,80 +7,40 @@ #[cfg(feature = "minimal")] mod tests { use grovedb_costs::{ - storage_cost::{ - removal::StorageRemovedBytes::NoStorageRemoval, transition::ElementFlagsUpdate, - }, + storage_cost::removal::StorageRemovedBytes::{BasicStorageRemoval, NoStorageRemoval}, OperationCost, }; - use grovedb_epoch_based_storage_flags::StorageFlags; - use grovedb_merk::{element::costs::ElementCostExtensions, tree_type::TreeType}; + use grovedb_merk::tree_type::TreeType; use grovedb_version::version::{v3::GROVE_V3, GroveVersion}; use crate::{ - batch::{BatchApplyOptions, QualifiedGroveDbOp}, + batch::{ + settle_test_support::{ + apply, flags_update, options, owned_flags, split_removal_bytes, unowned_flags, + value_bytes, write_op, Mode, KEY, NEW_OWNER, OLD_OWNER, + }, + QualifiedGroveDbOp, + }, reference_path::ReferencePathType, - tests::{common::EMPTY_PATH, make_empty_grovedb, TempGroveDb}, + tests::{make_empty_grovedb, TempGroveDb}, Element, Error, }; - const OLD_OWNER: [u8; 32] = [1; 32]; - const NEW_OWNER: [u8; 32] = [2; 32]; - const KEY: &[u8] = b"key1"; - - fn owned_flags(epoch: u16, owner: [u8; 32]) -> Option> { - Some(StorageFlags::SingleEpochOwned(epoch, owner).to_element_flags()) - } - - fn unowned_flags(epoch: u16) -> Option> { - Some(StorageFlags::SingleEpoch(epoch).to_element_flags()) - } - fn target() -> ReferencePathType { ReferencePathType::AbsolutePathReference(vec![b"tree".to_vec(), KEY.to_vec()]) } - /// Drive's flags update, merging or settling owner changes. - #[derive(Clone, Copy, Debug, PartialEq)] - enum Mode { - Merging, - Settling, - } - - fn apply( - db: &TempGroveDb, - ops: Vec, - mode: Mode, - grove_version: &GroveVersion, - ) -> Result { - db.apply_batch_with_element_flags_update( - ops, - Some(BatchApplyOptions { - settle_owner_changes: mode == Mode::Settling, - ..Default::default() - }), - |cost, old_flags, new_flags| { - match mode { - Mode::Merging => StorageFlags::update_element_flags(cost, old_flags, new_flags) - .map(ElementFlagsUpdate::from), - Mode::Settling => StorageFlags::update_element_flags_settling_owner_changes( - cost, old_flags, new_flags, - ), - } - .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) - }, - |flags, removed_key_bytes, removed_value_bytes| { - StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) - .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) - }, - None, - grove_version, - ) - .cost_as_result() + fn insert(db: &TempGroveDb, path: &[&[u8]], key: &[u8], element: Element, gv: &GroveVersion) { + db.insert(path, key, element, None, None, gv) + .unwrap() + .expect("expected to insert"); } - /// A grove holding `old` at `[tree]/key1` (a tree of `tree_type`), and, - /// `with_reference`, a reference to it at `[index, value]/reference`, - /// deeper than its target as a Drive index reference is. + /// A grove holding `old` at `[tree]/key1` (a tree of `tree_type`), next + /// to an `index` tree with a `value` tree under it and a `zindex` tree + /// that sorts after `tree`, and, `with_reference`, a reference to it at + /// `[index, value]/reference`, deeper than its target as a Drive index + /// reference is. fn grove_with( old: &Element, tree_type: TreeType, @@ -92,63 +52,29 @@ mod tests { TreeType::SumTree => Element::empty_sum_tree(), _ => Element::empty_tree(), }; - db.insert(EMPTY_PATH, b"tree", tree, None, None, grove_version) - .unwrap() - .expect("expected to insert the tree"); - db.insert( - [b"tree".as_slice()].as_ref(), - KEY, - old.clone(), - None, - None, - grove_version, - ) - .unwrap() - .expect("expected to insert the old element"); - db.insert( - EMPTY_PATH, - b"index", - Element::empty_tree(), - None, - None, - grove_version, - ) - .unwrap() - .expect("expected to insert the index tree"); - db.insert( - [b"index".as_slice()].as_ref(), + insert(&db, &[], b"tree", tree, grove_version); + insert(&db, &[b"tree"], KEY, old.clone(), grove_version); + insert(&db, &[], b"index", Element::empty_tree(), grove_version); + insert( + &db, + &[b"index"], b"value", Element::empty_tree(), - None, - None, grove_version, - ) - .unwrap() - .expect("expected to insert the index value tree"); - if !with_reference { - return db; + ); + insert(&db, &[], b"zindex", Element::empty_tree(), grove_version); + if with_reference { + insert( + &db, + &[b"index", b"value"], + b"reference", + Element::new_reference(target()), + grove_version, + ); } - db.insert( - [b"index".as_slice(), b"value"].as_ref(), - b"reference", - Element::new_reference(target()), - None, - None, - grove_version, - ) - .unwrap() - .expect("expected to insert the reference"); db } - fn write_op(element: &Element) -> QualifiedGroveDbOp { - QualifiedGroveDbOp::insert_or_replace_op( - vec![b"tree".to_vec()], - KEY.to_vec(), - element.clone(), - ) - } - fn refresh_op(trusted: bool) -> QualifiedGroveDbOp { QualifiedGroveDbOp::refresh_reference_op( vec![b"index".to_vec(), b"value".to_vec()], @@ -161,35 +87,67 @@ mod tests { ) } - /// The ways a batch rewrites the references to the element it updates. - fn reference_ops() -> Vec<(&'static str, Vec)> { + fn reference_op(path: Vec>, key: &[u8]) -> QualifiedGroveDbOp { + QualifiedGroveDbOp::insert_or_replace_op( + path, + key.to_vec(), + Element::new_reference(target()), + ) + } + + /// The ways a batch rewrites the references to the element it updates: + /// whether the grove holds the deeper reference (which a way then + /// rewrites too), and how many of the references resolve before the + /// batch writes the element. + fn reference_ops() -> Vec<(&'static str, Vec, bool, usize)> { vec![ ( "an untrusted refresh of a deeper reference", vec![refresh_op(false)], + true, + 1, ), ( "a trusted refresh of a deeper reference", vec![refresh_op(true)], + true, + 1, ), ( "a rewrite of a deeper reference", - vec![QualifiedGroveDbOp::insert_or_replace_op( + vec![reference_op( vec![b"index".to_vec(), b"value".to_vec()], - b"reference".to_vec(), - Element::new_reference(target()), + b"reference", )], + true, + 1, ), ( - "a refresh and a new reference beside the tree", + "a refresh and a new reference in a tree before the target's", vec![ refresh_op(true), - QualifiedGroveDbOp::insert_or_replace_op( - vec![b"index".to_vec()], - b"another reference".to_vec(), - Element::new_reference(target()), - ), + reference_op(vec![b"index".to_vec()], b"another"), ], + true, + 2, + ), + ( + "a refresh and a new reference at the root", + vec![refresh_op(true), reference_op(vec![], b"root reference")], + true, + 1, + ), + ( + "a new reference at the root", + vec![reference_op(vec![], b"root reference")], + false, + 0, + ), + ( + "a new reference in a tree after the target's", + vec![reference_op(vec![b"zindex".to_vec()], b"another")], + false, + 0, ), ] } @@ -197,8 +155,9 @@ mod tests { /// Updates whose stored flags are not the new element's as written, nor /// the old element's: the flags update merges them. fn flag_changing_updates() -> Vec<(&'static str, Element, Element, TreeType)> { - vec![ - ( + let mut updates = vec![]; + for growth in [1, 3, 25] { + updates.push(( "an item with a sum item, same owner, grown in a later epoch", Element::new_item_with_sum_item_with_flags( vec![7; 20], @@ -206,12 +165,14 @@ mod tests { owned_flags(0, OLD_OWNER), ), Element::new_item_with_sum_item_with_flags( - vec![8; 45], + vec![8; 20 + growth], 9, owned_flags(2, OLD_OWNER), ), TreeType::SumTree, - ), + )); + } + updates.extend([ ( "a sum item gaining an owner in the same epoch", Element::new_sum_item_with_flags(5, unowned_flags(0)), @@ -236,7 +197,8 @@ mod tests { Element::new_item_with_flags(vec![8; 45], owned_flags(2, NEW_OWNER)), TreeType::NormalTree, ), - ] + ]); + updates } fn issues(db: &TempGroveDb, grove_version: &GroveVersion) -> usize { @@ -245,30 +207,56 @@ mod tests { .len() } + /// Applies `new` and `reference_ops` to a fresh grove holding `old` (and, + /// `with_reference`, the deeper reference), and counts the stale + /// references. + fn stale_references( + old: &Element, + new: &Element, + tree_type: TreeType, + reference_ops: Vec, + with_reference: bool, + mode: Mode, + grove_version: &GroveVersion, + ) -> Result { + let db = grove_with(old, tree_type, with_reference, grove_version); + let mut ops = vec![write_op(new)]; + ops.extend(reference_ops); + apply(&db, ops, mode, grove_version)?; + Ok(issues(&db, grove_version)) + } + #[test] fn a_reference_rewritten_in_the_same_batch_commits_to_the_stored_bytes() { let grove_version = GroveVersion::latest(); for (label, old, new, tree_type) in flag_changing_updates() { for mode in [Mode::Merging, Mode::Settling] { - for (reference_label, reference_ops) in reference_ops() { - let db = grove_with(&old, tree_type, true, grove_version); - let mut ops = vec![write_op(&new)]; - ops.extend(reference_ops); - apply(&db, ops, mode, grove_version).unwrap_or_else(|e| { + for (reference_label, reference_ops, with_reference, _) in reference_ops() { + let stale = stale_references( + &old, + &new, + tree_type, + reference_ops, + with_reference, + mode, + grove_version, + ) + .unwrap_or_else(|e| { panic!("{label}, {reference_label}, {mode:?}: expected to apply: {e}") }); assert_eq!( - issues(&db, grove_version), - 0, - "{label}, {reference_label}, {mode:?}: the reference is stale" + stale, 0, + "{label} ({new:?}), {reference_label}, {mode:?}: a reference is stale" ); } } } } - /// Grove v1..v3 keep the hand-written prediction, which takes a sum item - /// to keep its stored flags (consensus-locked). + /// Grove v1..v3 keep the hand-written prediction without the option, + /// which takes a sum item to keep its stored flags: a reference that + /// resolves before the batch writes the sum item commits to bytes that + /// are never stored (consensus-locked). #[test] fn grove_v3_keeps_the_legacy_sum_item_prediction() { let grove_version = &GROVE_V3; @@ -276,34 +264,205 @@ mod tests { if !old.is_sum_item() { continue; } - for (reference_label, reference_ops) in reference_ops() { - let db = grove_with(&old, tree_type, true, grove_version); - let references = reference_ops.len(); - let mut ops = vec![write_op(&new)]; - ops.extend(reference_ops); - apply(&db, ops, Mode::Merging, grove_version).unwrap_or_else(|e| { - panic!("{label}, {reference_label}: expected to apply: {e}") - }); + for (reference_label, reference_ops, with_reference, resolving_first) in reference_ops() + { + let stale = stale_references( + &old, + &new, + tree_type, + reference_ops, + with_reference, + Mode::Merging, + grove_version, + ) + .unwrap_or_else(|e| panic!("{label}, {reference_label}: expected to apply: {e}")); assert_eq!( - issues(&db, grove_version), - references, - "{label}, {reference_label}: expected every reference stale" + stale, resolving_first, + "{label} ({new:?}), {reference_label}: expected the legacy stale references" ); } } } - /// The bytes the node of `element` at `key1` holds besides its key. - fn value_bytes(element: &Element, tree_type: TreeType, grove_version: &GroveVersion) -> u32 { - Element::specialized_costs_for_key_value( - KEY, - &element - .serialize(grove_version) - .expect("expected to serialize"), - tree_type.inner_node_type(), - grove_version, - ) - .expect("expected value bytes") + /// Settling owner changes is new on every grove version and has no + /// legacy prediction to keep: with the option, grove v1..v3 predict a + /// same-batch reference's target through Merk's own update too. + #[test] + fn grove_v3_predicts_the_stored_bytes_with_the_option() { + let grove_version = &GROVE_V3; + for (label, old, new, tree_type) in flag_changing_updates() { + for (reference_label, reference_ops, with_reference, _) in reference_ops() { + let stale = stale_references( + &old, + &new, + tree_type, + reference_ops, + with_reference, + Mode::Settling, + grove_version, + ) + .unwrap_or_else(|e| panic!("{label}, {reference_label}: expected to apply: {e}")); + assert_eq!( + stale, 0, + "{label} ({new:?}), {reference_label}: a reference is stale" + ); + } + } + } + + /// A sum item replaced by an item, or the reverse, applies with a + /// same-batch reference to it as it does alone. + #[test] + fn a_sum_item_and_an_item_replace_each_other_under_a_same_batch_reference() { + let swaps = [ + ( + Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(b"x".to_vec(), owned_flags(1, OLD_OWNER)), + ), + ( + Element::new_item_with_flags(b"x".to_vec(), owned_flags(0, OLD_OWNER)), + Element::new_sum_item_with_flags(5, owned_flags(1, OLD_OWNER)), + ), + ]; + for (old, new) in swaps { + for (reference_label, reference_ops, with_reference, _) in reference_ops() { + let stale = stale_references( + &old, + &new, + TreeType::SumTree, + reference_ops, + with_reference, + Mode::Merging, + GroveVersion::latest(), + ) + .unwrap_or_else(|e| panic!("{old:?} -> {new:?}, {reference_label}: {e}")); + assert_eq!(stale, 0, "{old:?} -> {new:?}, {reference_label}"); + } + // grove v1..v3 refuse it when the reference resolves first + // (consensus-locked) + let error = stale_references( + &old, + &new, + TreeType::SumTree, + vec![refresh_op(true)], + true, + Mode::Merging, + &GROVE_V3, + ) + .expect_err("expected the legacy refusal"); + assert!( + error.to_string().contains("is not supported"), + "unexpected error {error}" + ); + } + } + + /// A deterministic flags update that stamps the measured storage change + /// into the new flags, so the stored bytes depend on every measurement + /// the update takes. + fn stamp_costs( + cost: &grovedb_costs::storage_cost::StorageCost, + _old_flags: Option>, + new_flags: &mut Vec, + ) -> Result { + let removed = match cost.removed_bytes { + BasicStorageRemoval(bytes) => bytes, + _ => 0, + }; + let mut stamp = cost.added_bytes.to_be_bytes().to_vec(); + stamp.extend(removed.to_be_bytes()); + stamp.extend(cost.replaced_bytes.to_be_bytes()); + if *new_flags == stamp { + return Ok(false); + } + *new_flags = stamp; + Ok(true) + } + + /// A reference that resolves after the batch has written its target + /// commits to the bytes the target stores, whatever the flags update. + #[test] + fn a_reference_to_a_target_written_earlier_in_the_batch_commits_to_its_stored_bytes() { + let grove_version = GroveVersion::latest(); + for (target_path, reference_path) in [ + (vec![b"a".to_vec(), b"b".to_vec()], vec![b"c".to_vec()]), + (vec![b"a".to_vec()], vec![b"c".to_vec(), b"d".to_vec()]), + ] { + let db = make_empty_grovedb(); + insert(&db, &[], b"a", Element::empty_tree(), grove_version); + insert(&db, &[b"a"], b"b", Element::empty_tree(), grove_version); + insert(&db, &[], b"c", Element::empty_tree(), grove_version); + insert(&db, &[b"c"], b"d", Element::empty_tree(), grove_version); + let path: Vec<&[u8]> = target_path.iter().map(Vec::as_slice).collect(); + insert( + &db, + &path, + b"k", + Element::new_item_with_flags(b"v".to_vec(), Some(vec![0])), + grove_version, + ); + let mut qualified_target = target_path.clone(); + qualified_target.push(b"k".to_vec()); + db.apply_batch_with_element_flags_update( + vec![ + QualifiedGroveDbOp::replace_op( + target_path.clone(), + b"k".to_vec(), + Element::new_item_with_flags(b"vv".to_vec(), Some(vec![0])), + ), + QualifiedGroveDbOp::insert_or_replace_op( + reference_path.clone(), + b"r".to_vec(), + Element::new_reference(ReferencePathType::AbsolutePathReference( + qualified_target, + )), + ), + ], + None, + stamp_costs, + |_flags, removed_key_bytes, removed_value_bytes| { + Ok(( + BasicStorageRemoval(removed_key_bytes), + BasicStorageRemoval(removed_value_bytes), + )) + }, + None, + grove_version, + ) + .cost_as_result() + .expect("expected the batch to apply"); + assert_eq!( + issues(&db, grove_version), + 0, + "target at {target_path:?}, reference at {reference_path:?}" + ); + } + } + + /// A reference a partial batch's add-on writes to an item its initial + /// segment updated commits to the bytes that item stores. + #[test] + fn an_add_on_reference_commits_to_the_bytes_the_initial_segment_stored() { + let grove_version = GroveVersion::latest(); + let old = + Element::new_item_with_sum_item_with_flags(vec![7; 20], 5, owned_flags(0, OLD_OWNER)); + let new = + Element::new_item_with_sum_item_with_flags(vec![8; 21], 9, owned_flags(2, OLD_OWNER)); + for mode in [Mode::Merging, Mode::Settling] { + let db = grove_with(&old, TreeType::SumTree, false, grove_version); + db.apply_partial_batch_with_element_flags_update( + vec![write_op(&new)], + Some(options(mode)), + flags_update(mode), + split_removal_bytes, + |_cost, _leftover_operations| Ok(vec![reference_op(vec![], b"root reference")]), + None, + grove_version, + ) + .cost_as_result() + .expect("expected the partial batch to apply"); + assert_eq!(issues(&db, grove_version), 0, "{mode:?}"); + } } /// Replacements whose new flags stand as written (the flags update diff --git a/grovedb/src/batch/settle_owner_change_cost_tests.rs b/grovedb/src/batch/settle_owner_change_cost_tests.rs index bc8f8e7ad..0c06e6e8b 100644 --- a/grovedb/src/batch/settle_owner_change_cost_tests.rs +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -8,18 +8,12 @@ mod tests { use std::collections::{BTreeMap, HashMap}; use grovedb_costs::{ - storage_cost::{ - removal::StorageRemovedBytes::{NoStorageRemoval, SectionedStorageRemoval}, - transition::ElementFlagsUpdate, - }, + storage_cost::removal::StorageRemovedBytes::{NoStorageRemoval, SectionedStorageRemoval}, OperationCost, }; use grovedb_epoch_based_storage_flags::StorageFlags; use grovedb_merk::{ - element::{ - costs::ElementCostExtensions, get::ElementFetchFromStorageExtensions, - tree_type::ElementTreeTypeExtensions, - }, + element::{get::ElementFetchFromStorageExtensions, tree_type::ElementTreeTypeExtensions}, estimated_costs::{ average_case_costs::{ EstimatedLayerCount::ApproximateElements, @@ -32,9 +26,13 @@ mod tests { tree::kv::KV, tree_type::TreeType, }; - use grovedb_version::version::GroveVersion; + use grovedb_version::version::{v3::GROVE_V3, GroveVersion}; use intmap::IntMap; + use crate::batch::settle_test_support::{ + apply, merging_flags_update, options, owned_flags, settling_flags_update, + split_removal_bytes, unowned_flags, value_bytes, write_op, Mode, KEY, NEW_OWNER, OLD_OWNER, + }; use crate::{ batch::{ estimated_costs::EstimatedCostsType::{AverageCaseCostsType, WorstCaseCostsType}, @@ -42,21 +40,9 @@ mod tests { }, reference_path::ReferencePathType, tests::{common::EMPTY_PATH, make_empty_grovedb, TempGroveDb}, - BackwardReferences, Element, Error, GroveDb, + BackwardReferences, Element, GroveDb, }; - const OLD_OWNER: [u8; 32] = [1; 32]; - const NEW_OWNER: [u8; 32] = [2; 32]; - const KEY: &[u8] = b"key1"; - - fn owned_flags(epoch: u16, owner: [u8; 32]) -> Option> { - Some(StorageFlags::SingleEpochOwned(epoch, owner).to_element_flags()) - } - - fn unowned_flags(epoch: u16) -> Option> { - Some(StorageFlags::SingleEpoch(epoch).to_element_flags()) - } - fn target_path() -> ReferencePathType { ReferencePathType::AbsolutePathReference(vec![b"targets".to_vec(), b"target".to_vec()]) } @@ -68,51 +54,6 @@ mod tests { ]) } - /// How a batch is applied. - #[derive(Clone, Copy, PartialEq)] - enum Mode { - /// Today's flags update, without the option. - Merging, - /// The settling flags update, with the option. - Settling, - } - - fn options(mode: Mode) -> BatchApplyOptions { - BatchApplyOptions { - settle_owner_changes: mode == Mode::Settling, - ..Default::default() - } - } - - fn apply( - db: &TempGroveDb, - ops: Vec, - mode: Mode, - grove_version: &GroveVersion, - ) -> Result { - db.apply_batch_with_element_flags_update( - ops, - Some(options(mode)), - |cost, old_flags, new_flags| { - match mode { - Mode::Merging => StorageFlags::update_element_flags(cost, old_flags, new_flags) - .map(ElementFlagsUpdate::from), - Mode::Settling => StorageFlags::update_element_flags_settling_owner_changes( - cost, old_flags, new_flags, - ), - } - .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) - }, - |flags, removed_key_bytes, removed_value_bytes| { - StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) - .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) - }, - None, - grove_version, - ) - .cost_as_result() - } - /// A grove holding `old` at `[tree]/key1` next to an unflagged sibling, /// in a tree of `tree_type`, and a `targets` tree whose items references /// can point at. @@ -170,14 +111,6 @@ mod tests { db } - fn write_op(element: &Element) -> QualifiedGroveDbOp { - QualifiedGroveDbOp::insert_or_replace_op( - vec![b"tree".to_vec()], - KEY.to_vec(), - element.clone(), - ) - } - fn delete_op(element: &Element) -> QualifiedGroveDbOp { match element.tree_type() { Some(tree_type) => QualifiedGroveDbOp::delete_tree_op( @@ -225,20 +158,6 @@ mod tests { .expect("expected storage flags") } - /// The bytes the node of `element` at `key1` holds besides its key, as - /// the apply sizes them. - fn value_bytes(element: &Element, tree_type: TreeType, grove_version: &GroveVersion) -> u32 { - Element::specialized_costs_for_key_value( - KEY, - &element - .serialize(grove_version) - .expect("expected to serialize"), - tree_type.inner_node_type(), - grove_version, - ) - .expect("expected value bytes") - } - fn key_bytes() -> u32 { KV::node_key_byte_cost_size(KEY.len() as u32) } @@ -381,6 +300,59 @@ mod tests { assert_settles(&old, &new, TreeType::NormalTree, grove_version); } + /// Settling owner changes does not depend on the grove version: on grove + /// v1..v3, whose Merk runs version 0 of the just-in-time value update, + /// an owner change settles exactly as it does on the latest version. + #[test] + fn owner_changes_settle_on_grove_v3() { + let grove_version = &GROVE_V3; + let item = |len: usize, epoch, owner| { + Element::new_item_with_flags(vec![8; len], owned_flags(epoch, owner)) + }; + for (old, new, tree_type) in [ + ( + item(20, 0, OLD_OWNER), + item(60, 2, NEW_OWNER), + TreeType::NormalTree, + ), + ( + item(60, 0, OLD_OWNER), + item(5, 2, NEW_OWNER), + TreeType::NormalTree, + ), + ( + item(20, 0, OLD_OWNER), + item(20, 2, NEW_OWNER), + TreeType::NormalTree, + ), + ( + item(20, 0, OLD_OWNER), + item(30, 0, NEW_OWNER), + TreeType::NormalTree, + ), + ( + Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), + Element::new_sum_item_with_flags(900, owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ( + Element::new_item_with_sum_item_with_flags( + vec![7; 20], + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_item_with_sum_item_with_flags( + vec![8; 45], + 9, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ] { + assert_settles(&old, &new, tree_type, grove_version); + } + } + #[test] fn owner_change_of_multi_epoch_flags_refunds_every_epoch() { let grove_version = GroveVersion::latest(); @@ -624,16 +596,8 @@ mod tests { .apply_partial_batch_with_element_flags_update( vec![write_op(&new)], Some(options(Mode::Settling)), - |cost, old_flags, new_flags| { - StorageFlags::update_element_flags_settling_owner_changes( - cost, old_flags, new_flags, - ) - .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) - }, - |flags, removed_key_bytes, removed_value_bytes| { - StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) - .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) - }, + settling_flags_update, + split_removal_bytes, |cost, _leftover_operations| { initial_cost = Some(cost.clone()); Ok(vec![]) @@ -767,19 +731,8 @@ mod tests { settle_owner_changes, ..Default::default() }), - |cost, old_flags, new_flags| { - StorageFlags::update_element_flags(cost, old_flags, new_flags).map_err( - |e| Error::JustInTimeElementFlagsClientError(e.to_string()), - ) - }, - |flags, removed_key_bytes, removed_value_bytes| { - StorageFlags::split_removal_bytes( - flags, - removed_key_bytes, - removed_value_bytes, - ) - .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) - }, + merging_flags_update, + split_removal_bytes, None, grove_version, ) @@ -804,16 +757,8 @@ mod tests { .apply_batch_with_element_flags_update( vec![write_op(&new)], None, - |cost, old_flags, new_flags| { - StorageFlags::update_element_flags_settling_owner_changes( - cost, old_flags, new_flags, - ) - .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) - }, - |flags, removed_key_bytes, removed_value_bytes| { - StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) - .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) - }, + settling_flags_update, + split_removal_bytes, None, grove_version, ) @@ -830,64 +775,66 @@ mod tests { } /// A reference written in the same batch commits to the bytes the - /// settled element stores: its new flags, not the old owner's. + /// settled element stores: its new flags, not the old owner's, on the + /// latest grove version and on grove v1..v3 alike. #[test] fn a_reference_in_the_same_batch_commits_to_the_settled_element() { - let grove_version = GroveVersion::latest(); - let cases = [ - ( - Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), - Element::new_item_with_flags(vec![8; 20], owned_flags(2, NEW_OWNER)), - TreeType::NormalTree, - ), - ( - Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), - Element::new_item_with_flags(vec![8; 70], owned_flags(2, NEW_OWNER)), - TreeType::NormalTree, - ), - ( - Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), - Element::new_sum_item_with_flags(900, owned_flags(2, NEW_OWNER)), - TreeType::SumTree, - ), - ( - Element::new_item_with_sum_item_with_flags( - vec![7; 20], - 5, - owned_flags(0, OLD_OWNER), + for grove_version in [GroveVersion::latest(), &GROVE_V3] { + let cases = [ + ( + Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 20], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, ), - Element::new_item_with_sum_item_with_flags( - vec![8; 20], - 9, - owned_flags(2, NEW_OWNER), + ( + Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 70], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, ), - TreeType::SumTree, - ), - ]; - for (old, new, tree_type) in cases { - let db = grove_with(&old, tree_type, grove_version); - apply( - &db, - vec![ - write_op(&new), - QualifiedGroveDbOp::insert_or_replace_op( - vec![b"targets".to_vec()], - b"reference".to_vec(), - Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ - b"tree".to_vec(), - KEY.to_vec(), - ])), + ( + Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), + Element::new_sum_item_with_flags(900, owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ( + Element::new_item_with_sum_item_with_flags( + vec![7; 20], + 5, + owned_flags(0, OLD_OWNER), ), - ], - Mode::Settling, - grove_version, - ) - .expect("expected the batch to apply"); - assert_eq!(stored(&db, grove_version), new); - let issues = db - .visualize_verify_grovedb(None, true, false, grove_version) - .expect("expected to verify the grove"); - assert!(issues.is_empty(), "reference issues: {issues:?}"); + Element::new_item_with_sum_item_with_flags( + vec![8; 20], + 9, + owned_flags(2, NEW_OWNER), + ), + TreeType::SumTree, + ), + ]; + for (old, new, tree_type) in cases { + let db = grove_with(&old, tree_type, grove_version); + apply( + &db, + vec![ + write_op(&new), + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"targets".to_vec()], + b"reference".to_vec(), + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + b"tree".to_vec(), + KEY.to_vec(), + ])), + ), + ], + Mode::Settling, + grove_version, + ) + .expect("expected the batch to apply"); + assert_eq!(stored(&db, grove_version), new); + let issues = db + .visualize_verify_grovedb(None, true, false, grove_version) + .expect("expected to verify the grove"); + assert!(issues.is_empty(), "reference issues: {issues:?}"); + } } } @@ -1084,10 +1031,11 @@ mod tests { } } - /// The estimates of those writes without the option, as they were - /// before it existed: (case, write, average case, seeks, added bytes, - /// replaced bytes, loaded bytes, hash calls), taken from develop at - /// a720f7c1. + /// The estimates of those writes without the option on the latest grove + /// version, as they were before it existed: (case, write, average case, + /// seeks, added bytes, replaced bytes, loaded bytes, hash calls), taken + /// from develop at a720f7c1. The worst-case rows whose replaced bytes + /// saturate at `u32::MAX` still pin every other figure. const ESTIMATES_BEFORE_THE_OPTION: &[(usize, &str, bool, u32, u32, u32, u64, u32)] = &[ (0, "insert_or_replace", true, 34, 243, 4289, 6170, 79), ( @@ -1427,23 +1375,215 @@ mod tests { (6, "replace_dont_check", false, 12, 0, 409825, 394925, 276), ]; + /// The same estimates on grove v3 (no backward-references fan-out), + /// taken from develop at a720f7c1. + const GROVE_V3_ESTIMATES_BEFORE_THE_OPTION: &[(usize, &str, bool, u32, u32, u32, u64, u32)] = &[ + (0, "insert_or_replace", true, 13, 243, 1047, 1379, 23), + (0, "insert_or_replace", false, 12, 243, 409674, 394925, 275), + ( + 0, + "insert_or_replace_dont_check", + true, + 13, + 243, + 1047, + 1379, + 23, + ), + ( + 0, + "insert_or_replace_dont_check", + false, + 12, + 243, + 409674, + 394925, + 275, + ), + (0, "replace", true, 13, 0, 1326, 1379, 24), + (0, "replace", false, 12, 206, 409711, 394925, 275), + (0, "replace_dont_check", true, 13, 0, 1326, 1379, 24), + (0, "replace_dont_check", false, 12, 206, 409711, 394925, 275), + (0, "patch", true, 13, 0, 1326, 1379, 24), + (0, "patch", false, 12, 206, 409711, 394925, 275), + (0, "patch_dont_check", true, 13, 0, 1326, 1379, 24), + (0, "patch_dont_check", false, 12, 206, 409711, 394925, 275), + (1, "insert_or_replace", true, 13, 187, 1047, 1379, 22), + (1, "insert_or_replace", false, 12, 187, 409674, 394925, 274), + ( + 1, + "insert_or_replace_dont_check", + true, + 13, + 187, + 1047, + 1379, + 22, + ), + ( + 1, + "insert_or_replace_dont_check", + false, + 12, + 187, + 409674, + 394925, + 274, + ), + (1, "replace", true, 13, 0, 1271, 1379, 23), + (1, "replace", false, 12, 150, 409711, 394925, 274), + (1, "replace_dont_check", true, 13, 0, 1271, 1379, 23), + (1, "replace_dont_check", false, 12, 150, 409711, 394925, 274), + (1, "patch", true, 13, 0, 1271, 1379, 23), + (1, "patch", false, 12, 150, 409711, 394925, 274), + (1, "patch_dont_check", true, 13, 0, 1271, 1379, 23), + (1, "patch_dont_check", false, 12, 150, 409711, 394925, 274), + (2, "insert_or_replace", true, 13, 245, 1095, 1451, 23), + (2, "insert_or_replace", false, 12, 229, 409674, 394925, 275), + ( + 2, + "insert_or_replace_dont_check", + true, + 13, + 245, + 1095, + 1451, + 23, + ), + ( + 2, + "insert_or_replace_dont_check", + false, + 12, + 229, + 409674, + 394925, + 275, + ), + (2, "replace", true, 13, 0, 1340, 1451, 23), + (2, "replace", false, 12, 192, 409711, 394925, 275), + (2, "replace_dont_check", true, 13, 0, 1340, 1451, 23), + (2, "replace_dont_check", false, 12, 192, 409711, 394925, 275), + (3, "insert_or_replace", true, 13, 198, 1095, 1451, 22), + (3, "insert_or_replace", false, 12, 182, 409674, 394925, 274), + ( + 3, + "insert_or_replace_dont_check", + true, + 13, + 198, + 1095, + 1451, + 22, + ), + ( + 3, + "insert_or_replace_dont_check", + false, + 12, + 182, + 409674, + 394925, + 274, + ), + (3, "replace", true, 13, 0, 1301, 1451, 22), + (3, "replace", false, 12, 0, 409864, 394925, 274), + (3, "replace_dont_check", true, 13, 0, 1301, 1451, 22), + (3, "replace_dont_check", false, 12, 0, 409864, 394925, 274), + (4, "insert_or_replace", true, 13, 223, 1047, 1379, 23), + (4, "insert_or_replace", false, 12, 223, 409674, 394925, 275), + ( + 4, + "insert_or_replace_dont_check", + true, + 13, + 223, + 1047, + 1379, + 23, + ), + ( + 4, + "insert_or_replace_dont_check", + false, + 12, + 223, + 409674, + 394925, + 275, + ), + (4, "replace", true, 13, 0, 1270, 1379, 23), + (4, "replace", false, 12, 186, 409711, 394925, 275), + (4, "replace_dont_check", true, 13, 0, 1270, 1379, 23), + (4, "replace_dont_check", false, 12, 186, 409711, 394925, 275), + (4, "refresh_trusted", true, 13, 0, 1270, 1379, 23), + (4, "refresh_trusted", false, 12, 186, 409711, 394925, 275), + (5, "insert_or_replace", true, 13, 240, 1095, 1451, 23), + (5, "insert_or_replace", false, 12, 224, 409674, 394925, 275), + ( + 5, + "insert_or_replace_dont_check", + true, + 13, + 240, + 1095, + 1451, + 23, + ), + ( + 5, + "insert_or_replace_dont_check", + false, + 12, + 224, + 409674, + 394925, + 275, + ), + (5, "replace", true, 13, 0, 1335, 1451, 23), + (5, "replace", false, 12, 187, 409711, 394925, 275), + (5, "replace_dont_check", true, 13, 0, 1335, 1451, 23), + (5, "replace_dont_check", false, 12, 187, 409711, 394925, 275), + (6, "insert_or_replace", true, 13, 151, 1047, 1379, 24), + (6, "insert_or_replace", false, 12, 151, 409674, 394925, 276), + ( + 6, + "insert_or_replace_dont_check", + true, + 13, + 151, + 1047, + 1379, + 24, + ), + ( + 6, + "insert_or_replace_dont_check", + false, + 12, + 151, + 409674, + 394925, + 276, + ), + (6, "replace", true, 13, 0, 1198, 1379, 24), + (6, "replace", false, 12, 0, 409825, 394925, 276), + (6, "replace_dont_check", true, 13, 0, 1198, 1379, 24), + (6, "replace_dont_check", false, 12, 0, 409825, 394925, 276), + ]; + /// Without the option, every estimate is what it was before the option - /// existed. + /// existed, on the latest grove version and on grove v3. #[test] fn estimates_without_the_option_are_unchanged() { - let grove_version = GroveVersion::latest(); - let mut pinned = ESTIMATES_BEFORE_THE_OPTION.iter(); - for (case, (_, new, tree_type)) in estimate_cases().into_iter().enumerate() { - for (label, op) in settling_writes(&new) { - for average_case in [true, false] { - let average_case_tree_type = average_case.then_some(tree_type); - for batch_apply_options in [None, Some(options(Mode::Merging))] { - let estimate = estimate( - vec![op.clone()], - batch_apply_options, - average_case_tree_type, - grove_version, - ); + for (grove_version, pins) in [ + (GroveVersion::latest(), ESTIMATES_BEFORE_THE_OPTION), + (&GROVE_V3, GROVE_V3_ESTIMATES_BEFORE_THE_OPTION), + ] { + let mut pinned = pins.iter(); + for (case, (_, new, tree_type)) in estimate_cases().into_iter().enumerate() { + for (label, op) in settling_writes(&new) { + for average_case in [true, false] { let &( pinned_case, pinned_label, @@ -1453,32 +1593,39 @@ mod tests { replaced_bytes, storage_loaded_bytes, hash_node_calls, - ) = pinned.clone().next().expect("expected a pinned estimate"); + ) = pinned.next().expect("expected a pinned estimate"); assert_eq!( (pinned_case, pinned_label, pinned_average_case), (case, label, average_case) ); - assert_eq!( - estimate, - OperationCost { - seek_count, - storage_cost: grovedb_costs::storage_cost::StorageCost { - added_bytes, - replaced_bytes, - removed_bytes: NoStorageRemoval, - }, - storage_loaded_bytes, - hash_node_calls, - sinsemilla_hash_calls: 0, + let before = OperationCost { + seek_count, + storage_cost: grovedb_costs::storage_cost::StorageCost { + added_bytes, + replaced_bytes, + removed_bytes: NoStorageRemoval, }, - "case {case}, {label}, average case {average_case}" - ); + storage_loaded_bytes, + hash_node_calls, + sinsemilla_hash_calls: 0, + }; + for batch_apply_options in [None, Some(options(Mode::Merging))] { + assert_eq!( + estimate( + vec![op.clone()], + batch_apply_options, + average_case.then_some(tree_type), + grove_version, + ), + before, + "case {case}, {label}, average case {average_case}" + ); + } } - pinned.next(); } } + assert!(pinned.next().is_none(), "expected every pinned estimate"); } - assert!(pinned.next().is_none(), "expected every pinned estimate"); } /// A replacement estimated without the option charges no added bytes, @@ -1521,10 +1668,20 @@ mod tests { /// A flagged tree written together with a write under it reaches the /// estimators as the `InsertTreeWithRootHash` its propagation makes of /// it; with the option that op too is charged the insertion a settled - /// owner change records, whatever tree it lands in. + /// owner change records, whatever tree it is and whatever tree it lands + /// in. #[test] fn estimates_with_the_option_cover_a_settled_tree_written_with_its_children() { let grove_version = GroveVersion::latest(); + let trees: [fn(Option>) -> Element; 7] = [ + Element::empty_tree_with_flags, + Element::empty_sum_tree_with_flags, + Element::empty_big_sum_tree_with_flags, + Element::empty_count_tree_with_flags, + Element::empty_count_sum_tree_with_flags, + Element::empty_provable_count_tree_with_flags, + Element::empty_provable_count_sum_tree_with_flags, + ]; for parent_tree in [ Element::empty_tree(), Element::empty_sum_tree(), @@ -1532,79 +1689,118 @@ mod tests { Element::empty_count_tree(), ] { let parent_tree_type = parent_tree.tree_type().expect("expected a tree"); - let db = make_empty_grovedb(); - db.insert( - EMPTY_PATH, - b"parent", - parent_tree, - None, - None, - grove_version, - ) - .unwrap() - .expect("expected to insert the parent tree"); - db.insert( - [b"parent".as_slice()].as_ref(), - b"tree", - Element::empty_tree_with_flags(owned_flags(0, OLD_OWNER)), - None, - None, - grove_version, - ) - .unwrap() - .expect("expected to insert the tree"); - db.insert( - [b"parent".as_slice(), b"tree"].as_ref(), - b"child", - Element::new_item(b"child".to_vec()), - None, - None, - grove_version, - ) - .unwrap() - .expect("expected to insert the child"); - let ops = vec![ - QualifiedGroveDbOp::insert_or_replace_op( - vec![b"parent".to_vec()], - b"tree".to_vec(), - Element::empty_tree_with_flags(owned_flags(2, NEW_OWNER)), - ), - QualifiedGroveDbOp::insert_or_replace_op( - vec![b"parent".to_vec(), b"tree".to_vec()], - b"another child".to_vec(), - Element::new_item(b"another child".to_vec()), - ), - ]; - let applied = apply(&db, ops.clone(), Mode::Settling, grove_version) - .expect("expected the settling update to apply"); + for tree in trees { + let tree_type = tree(None).tree_type().expect("expected a tree"); + let db = make_empty_grovedb(); + db.insert( + EMPTY_PATH, + b"parent", + parent_tree.clone(), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the parent tree"); + db.insert( + [b"parent".as_slice()].as_ref(), + b"tree", + tree(owned_flags(0, OLD_OWNER)), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the tree"); + db.insert( + [b"parent".as_slice(), b"tree"].as_ref(), + b"child", + Element::new_item(b"child".to_vec()), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the child"); + let ops = vec![ + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"parent".to_vec()], + b"tree".to_vec(), + tree(owned_flags(2, NEW_OWNER)), + ), + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"parent".to_vec(), b"tree".to_vec()], + b"another child".to_vec(), + Element::new_item(b"another child".to_vec()), + ), + ]; + let applied = apply(&db, ops.clone(), Mode::Settling, grove_version) + .expect("expected the settling update to apply"); - let mut paths = HashMap::new(); - paths.insert(KeyInfoPath(vec![]), MaxElementsNumber(2)); - paths.insert( - KeyInfoPath::from_known_path([b"parent".as_slice()]), - MaxElementsNumber(2), - ); - paths.insert( - KeyInfoPath::from_known_path([b"parent".as_slice(), b"tree"]), - MaxElementsNumber(2), - ); - let worst = GroveDb::estimated_case_operations_for_batch( - WorstCaseCostsType(paths), - ops, - Some(options(Mode::Settling)), - |_cost, _old_flags, _new_flags| Ok(false), - |_flags, _removed_key_bytes, _removed_value_bytes| { - Ok((NoStorageRemoval, NoStorageRemoval)) - }, - grove_version, - ) - .cost_as_result() - .expect("expected an estimate"); - assert!( - worst.storage_cost.added_bytes >= applied.storage_cost.added_bytes, - "in a {parent_tree_type:?}: worst-case estimate {worst:?} is below the settled \ - apply {applied:?}" - ); + let mut average_case_layers = HashMap::new(); + average_case_layers.insert( + KeyInfoPath(vec![]), + EstimatedLayerInformation { + tree_type: TreeType::NormalTree, + estimated_layer_count: ApproximateElements(2), + estimated_layer_sizes: AllSubtrees(7, NoSumTrees, None), + }, + ); + average_case_layers.insert( + KeyInfoPath::from_known_path([b"parent".as_slice()]), + EstimatedLayerInformation { + tree_type: parent_tree_type, + estimated_layer_count: ApproximateElements(2), + estimated_layer_sizes: AllSubtrees(4, NoSumTrees, Some(35)), + }, + ); + average_case_layers.insert( + KeyInfoPath::from_known_path([b"parent".as_slice(), b"tree"]), + EstimatedLayerInformation { + tree_type, + estimated_layer_count: ApproximateElements(2), + estimated_layer_sizes: AllItems(13, 13, None), + }, + ); + let mut worst_case_layers = HashMap::new(); + worst_case_layers.insert(KeyInfoPath(vec![]), MaxElementsNumber(2)); + worst_case_layers.insert( + KeyInfoPath::from_known_path([b"parent".as_slice()]), + MaxElementsNumber(2), + ); + worst_case_layers.insert( + KeyInfoPath::from_known_path([b"parent".as_slice(), b"tree"]), + MaxElementsNumber(2), + ); + // The worst case sizes the item written under the tree as + // under a normal tree whatever the tree is, so only a normal + // tree's batch is bounded as a whole there; the worst-case + // settled tree itself is bounded for every tree in + // `estimated_costs`' tests. + let mut estimated_costs_types = vec![AverageCaseCostsType(average_case_layers)]; + if tree_type == TreeType::NormalTree { + estimated_costs_types.push(WorstCaseCostsType(worst_case_layers)); + } + for estimated_costs_type in estimated_costs_types { + let estimate = GroveDb::estimated_case_operations_for_batch( + estimated_costs_type, + ops.clone(), + Some(options(Mode::Settling)), + |_cost, _old_flags, _new_flags| Ok(false), + |_flags, _removed_key_bytes, _removed_value_bytes| { + Ok((NoStorageRemoval, NoStorageRemoval)) + }, + grove_version, + ) + .cost_as_result() + .expect("expected an estimate"); + assert!( + estimate.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "a {tree_type:?} in a {parent_tree_type:?}: estimate {estimate:?} is \ + below the settled apply {applied:?}" + ); + } + } } } } diff --git a/grovedb/src/batch/settle_test_support.rs b/grovedb/src/batch/settle_test_support.rs new file mode 100644 index 000000000..47914ab55 --- /dev/null +++ b/grovedb/src/batch/settle_test_support.rs @@ -0,0 +1,132 @@ +//! Fixtures shared by the tests of batch writes that run Drive's flags +//! update: owned and unowned storage flags, Drive's flags-update callbacks +//! (merging, or settling owner changes) and removal callback, and the +//! element at `[tree]/key1`. + +#![cfg(feature = "minimal")] + +use grovedb_costs::{ + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, + OperationCost, +}; +use grovedb_epoch_based_storage_flags::StorageFlags; +use grovedb_merk::{element::costs::ElementCostExtensions, tree_type::TreeType}; +use grovedb_version::version::GroveVersion; + +use crate::{ + batch::{BatchApplyOptions, QualifiedGroveDbOp}, + tests::TempGroveDb, + Element, ElementFlags, Error, +}; + +pub(crate) const OLD_OWNER: [u8; 32] = [1; 32]; +pub(crate) const NEW_OWNER: [u8; 32] = [2; 32]; +pub(crate) const KEY: &[u8] = b"key1"; + +pub(crate) fn owned_flags(epoch: u16, owner: [u8; 32]) -> Option> { + Some(StorageFlags::SingleEpochOwned(epoch, owner).to_element_flags()) +} + +pub(crate) fn unowned_flags(epoch: u16) -> Option> { + Some(StorageFlags::SingleEpoch(epoch).to_element_flags()) +} + +/// How a batch is applied. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) enum Mode { + /// Drive's merging flags update, without the option. + Merging, + /// The settling flags update, with the option. + Settling, +} + +/// Drive's flags update, merging the storage flags. +pub(crate) fn merging_flags_update( + cost: &StorageCost, + old_flags: Option, + new_flags: &mut ElementFlags, +) -> Result { + StorageFlags::update_element_flags(cost, old_flags, new_flags) + .map(ElementFlagsUpdate::from) + .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) +} + +/// Drive's flags update, settling owner changes. +pub(crate) fn settling_flags_update( + cost: &StorageCost, + old_flags: Option, + new_flags: &mut ElementFlags, +) -> Result { + StorageFlags::update_element_flags_settling_owner_changes(cost, old_flags, new_flags) + .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) +} + +/// The flags update of `mode`. +pub(crate) fn flags_update( + mode: Mode, +) -> fn(&StorageCost, Option, &mut ElementFlags) -> Result +{ + match mode { + Mode::Merging => merging_flags_update, + Mode::Settling => settling_flags_update, + } +} + +/// Drive's removal callback, sectioning freed bytes by epoch and owner. +pub(crate) fn split_removal_bytes( + flags: &mut ElementFlags, + removed_key_bytes: u32, + removed_value_bytes: u32, +) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error> { + StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) + .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) +} + +/// The batch options of `mode`. +pub(crate) fn options(mode: Mode) -> BatchApplyOptions { + BatchApplyOptions { + settle_owner_changes: mode == Mode::Settling, + ..Default::default() + } +} + +/// Applies `ops` with the flags update and options of `mode`. +pub(crate) fn apply( + db: &TempGroveDb, + ops: Vec, + mode: Mode, + grove_version: &GroveVersion, +) -> Result { + db.apply_batch_with_element_flags_update( + ops, + Some(options(mode)), + flags_update(mode), + split_removal_bytes, + None, + grove_version, + ) + .cost_as_result() +} + +/// A write of `element` at `[tree]/key1`. +pub(crate) fn write_op(element: &Element) -> QualifiedGroveDbOp { + QualifiedGroveDbOp::insert_or_replace_op(vec![b"tree".to_vec()], KEY.to_vec(), element.clone()) +} + +/// The bytes the node of `element` at `key1` holds besides its key, as the +/// apply sizes them in a tree of `tree_type`. +pub(crate) fn value_bytes( + element: &Element, + tree_type: TreeType, + grove_version: &GroveVersion, +) -> u32 { + Element::specialized_costs_for_key_value( + KEY, + &element + .serialize(grove_version) + .expect("expected to serialize"), + tree_type.inner_node_type(), + grove_version, + ) + .expect("expected value bytes") +} diff --git a/grovedb/src/tests/backward_references_flags_update_tests.rs b/grovedb/src/tests/backward_references_flags_update_tests.rs index 9ac0ebca2..b48bbfeff 100644 --- a/grovedb/src/tests/backward_references_flags_update_tests.rs +++ b/grovedb/src/tests/backward_references_flags_update_tests.rs @@ -375,16 +375,8 @@ fn settled_owner_change_keeps_referrers_bound() { settle_owner_changes: true, ..Default::default() }), - |cost, old_flags, new_flags| { - StorageFlags::update_element_flags_settling_owner_changes( - cost, old_flags, new_flags, - ) - .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) - }, - |flags, removed_key_bytes, removed_value_bytes| { - StorageFlags::split_removal_bytes(flags, removed_key_bytes, removed_value_bytes) - .map_err(|e| Error::SplitRemovalBytesClientError(e.to_string())) - }, + crate::batch::settle_test_support::settling_flags_update, + crate::batch::settle_test_support::split_removal_bytes, None, grove_version, ) diff --git a/merk/src/element/insert.rs b/merk/src/element/insert.rs index 9c162c525..67e1d35df 100644 --- a/merk/src/element/insert.rs +++ b/merk/src/element/insert.rs @@ -46,6 +46,28 @@ impl Delta<'_> { } } +/// The specialized value-defined cost an ordinary write puts `element` with +/// (`Op::PutWithSpecializedCost`), or `None` when it is put as an `Op::Put`. +/// +/// A sum item takes its specialized cost; `is_sum_item` looks through the +/// `NonCounted` / `NotSummed` / `NotCountedOrSummed` wrappers, so a wrapped +/// sum item takes the same path as a bare one. +pub fn specialized_put_cost( + element: &Element, + grove_version: &GroveVersion, +) -> Result, Error> { + if element.is_sum_item() { + element + .specialized_value_defined_cost(grove_version) + .map(Some) + .ok_or(Error::CorruptedCodeExecution( + "sum items should always have a value defined cost", + )) + } else { + Ok(None) + } +} + /// Extension trait for inserting elements into Merk storage. pub trait ElementInsertToStorageExtensions { /// Whether this element may legally live in a tree of `tree_type`. @@ -365,22 +387,14 @@ impl ElementInsertToStorageExtensions for Element { .map_err(|e| Error::CorruptedData(e.to_string())) .add_cost(cost); } - // Use is_sum_item() (which looks through NonCounted) so that a - // NonCounted(SumItem(..)) takes the same specialized cost path as a - // bare SumItem(..). - let batch_operations = if self.is_sum_item() { - let cost = cost_return_on_error_default!(self - .specialized_value_defined_cost(grove_version) - .ok_or(Error::CorruptedCodeExecution( - "sum items should always have a value defined cost" - ))); - [( - key, - Op::PutWithSpecializedCost(serialized, cost, merk_feature_type), - )] - } else { - [(key, Op::Put(serialized, merk_feature_type))] - }; + let batch_operations = + match cost_return_on_error_default!(specialized_put_cost(self, grove_version)) { + Some(cost) => [( + key, + Op::PutWithSpecializedCost(serialized, cost, merk_feature_type), + )], + None => [(key, Op::Put(serialized, merk_feature_type))], + }; let tree_type = merk.tree_type; merk.apply_with_specialized_costs::<_, Vec>( &batch_operations, @@ -424,22 +438,12 @@ impl ElementInsertToStorageExtensions for Element { Err(e) => return Err(e.into()).wrap_with_cost(Default::default()), }; - // Use is_sum_item() (which looks through NonCounted) so that a - // NonCounted(SumItem(..)) takes the same specialized cost path as a - // bare SumItem(..). - let entry = if self.is_sum_item() { - let cost = cost_return_on_error_default!(self - .specialized_value_defined_cost(grove_version) - .ok_or(Error::CorruptedCodeExecution( - "sum items should always have a value defined cost" - ))); - - ( + let entry = match cost_return_on_error_default!(specialized_put_cost(self, grove_version)) { + Some(cost) => ( key, Op::PutWithSpecializedCost(serialized, cost, feature_type), - ) - } else { - (key, Op::Put(serialized, feature_type)) + ), + None => (key, Op::Put(serialized, feature_type)), }; batch_operations.push(entry); Ok(()).wrap_with_cost(Default::default()) diff --git a/merk/src/tree/just_in_time_value_update/mod.rs b/merk/src/tree/just_in_time_value_update/mod.rs index 157762726..b71cf458c 100644 --- a/merk/src/tree/just_in_time_value_update/mod.rs +++ b/merk/src/tree/just_in_time_value_update/mod.rs @@ -13,8 +13,10 @@ //! then charged for bytes it does not hold, and the commit fails with a //! storage cost mismatch. Grove v1..v3 are consensus-locked to this. //! - version 1 measures the replacement again from the bytes it stores when -//! their size differs from that measurement, and charges every update that -//! version 0 accepts exactly as version 0 does. +//! their size differs from the last measurement taken, and restores the +//! put's value-defined cost each time it restores the put's value. Every +//! update whose cost version 0 records for the bytes it stores is charged +//! exactly as version 0 charges it. mod v0; mod v1; @@ -33,30 +35,43 @@ use crate::{ Error, }; +/// The put whose final bytes [`TreeNode::provided_value_hash_put_final_value`] +/// predicts: how it installs the new value on the stored node, which decides +/// the value-defined cost the just-in-time value update measures with. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PredictedPut { + /// An `Op::Put`: installed as [`TreeNode::put_value`] installs an + /// ordinary value at the grove version (merk tree version 0 keeps the + /// value-defined cost the stored node was loaded with). + Ordinary, + /// An [`Op::PutWithProvidedValueHash`](crate::tree::Op::PutWithProvidedValueHash), + /// which drops any value-defined cost. + ProvidedValueHash, + /// An `Op::PutWithSpecializedCost`, at the op's own cost. + SpecializedCost(u32), +} + impl TreeNode { - /// The value bytes an apply finally stores when a put of `new_value` + /// The value bytes an apply finally stores when `put` of `new_value` /// replaces the stored `old_value` — after the just-in-time value update /// has run the client callbacks (storage-flags carry-over and rewrite). /// /// Runs the apply's own steps on a detached node: the node holding - /// `old_value` takes the new value and feature type exactly as the put - /// installs them, and then goes through - /// [`Self::just_in_time_tree_node_value_update`]. `value_defined_cost` is - /// the cost the put stamps on the node: `None` for an ordinary put - /// (`Op::Put` from merk tree version 1 on, and - /// [`Op::PutWithProvidedValueHash`](crate::tree::Op::PutWithProvidedValueHash), - /// which drop any cost the predecessor carried), and the op's own cost - /// for `Op::PutWithSpecializedCost`. With the same (deterministic) - /// callbacks the result is byte-for-byte what the apply writes, so a - /// caller that must commit to the final bytes BEFORE the apply — a - /// reference holding its target's hash — can compute them. + /// `old_value`, loaded with `old_value_defined_cost` as the apply loads + /// it, takes the new value and feature type exactly as `put` installs + /// them, and then goes through the just-in-time value update + /// (`just_in_time_tree_node_value_update`). With the same + /// (deterministic) callbacks the result is byte-for-byte what the apply + /// writes, so a caller that must commit to the final bytes BEFORE the + /// apply — a reference holding its target's hash — can compute them. #[allow(clippy::too_many_arguments)] pub fn provided_value_hash_put_final_value( key: Vec, old_value: Vec, + old_value_defined_cost: Option, new_value: Vec, feature_type: TreeFeatureType, - value_defined_cost: Option, + put: PredictedPut, old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, get_temp_new_value_with_old_flags: &impl Fn( &Vec, @@ -80,26 +95,31 @@ impl TreeNode { >, grove_version: &GroveVersion, ) -> Result, Error> { - // The stored node (no hashes are needed: the update only measures - // sizes and consults the callbacks). - let kv = KV::from_fields(key, old_value, NULL_HASH, NULL_HASH, feature_type); - let mut node = TreeNode::new_with_tree_inner(TreeNodeInner { - left: None, - right: None, - kv, - }); + // The stored node as loaded (no hashes are needed: the update only + // measures sizes and consults the callbacks); its value is installed + // by the put below, and the stored bytes are its old value. + let mut kv = KV::from_fields(key, Vec::new(), NULL_HASH, NULL_HASH, feature_type); + kv.value_defined_cost = old_value_defined_cost; // The put, as the apply performs it. - node.inner.kv = match value_defined_cost { - None => node - .inner - .kv - .put_ordinary_value_no_update_of_hashes(new_value), - Some(value_defined_cost) => node - .inner - .kv - .put_value_with_fixed_cost_no_update_of_hashes(new_value, value_defined_cost), + let mut kv = match put { + PredictedPut::Ordinary => Self::install_ordinary_value(kv, new_value, grove_version)?, + PredictedPut::ProvidedValueHash => kv.put_ordinary_value_no_update_of_hashes(new_value), + PredictedPut::SpecializedCost(value_cost) => kv + .put_value_with_fixed_cost_no_update_of_hashes( + new_value, + ValueDefinedCostType::SpecializedValueDefinedCost(value_cost), + ), + }; + kv.feature_type = feature_type; + let mut node = TreeNode { + inner: Box::new(TreeNodeInner { + left: None, + right: None, + kv, + }), + old_value: Some(old_value), + known_storage_cost: None, }; - node.inner.kv.feature_type = feature_type; node.just_in_time_tree_node_value_update( old_specialized_cost, get_temp_new_value_with_old_flags, @@ -214,8 +234,8 @@ mod tests { merk::NodeType, test_utils::TempMerk, tree::{ - kv::ValueDefinedCostType, kv::KV, TreeFeatureType::BasicMerkNode, TreeNode, - TreeNodeInner, NULL_HASH, + just_in_time_value_update::PredictedPut, kv::ValueDefinedCostType, kv::KV, + TreeFeatureType::BasicMerkNode, TreeNode, TreeNodeInner, NULL_HASH, }, Error, Op, }; @@ -315,9 +335,10 @@ mod tests { let predicted = TreeNode::provided_value_hash_put_final_value( b"key".to_vec(), old.clone(), + None, new.clone(), BasicMerkNode, - None, + PredictedPut::ProvidedValueHash, &old_cost, &no_temp_value, &mut stamp_costs, diff --git a/merk/src/tree/just_in_time_value_update/v1.rs b/merk/src/tree/just_in_time_value_update/v1.rs index 1737e2197..9c9f28bfe 100644 --- a/merk/src/tree/just_in_time_value_update/v1.rs +++ b/merk/src/tree/just_in_time_value_update/v1.rs @@ -18,9 +18,12 @@ impl TreeNode { /// when the value the update stores is not the size the last /// measurement took (the first one sizes the new value with the OLD /// value's flags), the replacement is measured again from the stored - /// bytes, so the recorded cost is the cost of what is written. The - /// stored value's own cost is also measured once rather than on every - /// measurement, which leaves every figure unchanged. + /// bytes, so the recorded cost is the cost of what is written. For that, + /// a `Changed` round that restores the put's value restores the put's + /// value-defined cost with it, so a sum item or tree is measured with + /// the cost of the bytes it holds. The stored value's own cost is also + /// measured once rather than on every measurement, which leaves every + /// figure unchanged. pub(super) fn just_in_time_tree_node_value_update_v1( &mut self, old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, @@ -52,6 +55,7 @@ impl TreeNode { // For example to store the costs // todo: clean up clones let original_new_value = self.value_ref().clone(); + let original_value_defined_cost = self.inner.kv.value_defined_cost.clone(); // The stored value does not change while the update runs, so its // cost is measured once. @@ -126,7 +130,8 @@ impl TreeNode { self.kv_with_parent_hook_size_and_storage_cost(&old_cost)?; current_tree_plus_hook_size = new_size_and_storage_costs.0; storage_costs = new_size_and_storage_costs.1; - self.set_value(original_new_value.clone()) + self.set_value(original_new_value.clone()); + self.inner.kv.value_defined_cost = original_value_defined_cost.clone(); } } if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { diff --git a/merk/src/tree/mod.rs b/merk/src/tree/mod.rs index ac74ca280..b46ca2fe7 100644 --- a/merk/src/tree/mod.rs +++ b/merk/src/tree/mod.rs @@ -15,6 +15,8 @@ mod iter; #[cfg(feature = "minimal")] mod just_in_time_value_update; #[cfg(feature = "minimal")] +pub use just_in_time_value_update::PredictedPut; +#[cfg(feature = "minimal")] pub mod kv; #[cfg(feature = "minimal")] mod link; diff --git a/merk/src/tree/put_value/mod.rs b/merk/src/tree/put_value/mod.rs index cbd95f20e..198118c6c 100644 --- a/merk/src/tree/put_value/mod.rs +++ b/merk/src/tree/put_value/mod.rs @@ -29,7 +29,10 @@ use grovedb_costs::{ use grovedb_version::{error::GroveVersionError, version::GroveVersion}; use crate::{ - tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, + tree::{ + kv::{ValueDefinedCostType, KV}, + CryptoHash, TreeFeatureType, TreeNode, + }, Error, }; @@ -158,6 +161,28 @@ impl TreeNode { .wrap_with_cost(Default::default()), } } + + /// Installs an ordinary value on a node exactly as [`Self::put_value`] + /// does, without the just-in-time value update or hashing: what a + /// prediction of the bytes an `Op::Put` finally stores must start from. + /// See the module docs for the version semantics. + pub(in crate::tree) fn install_ordinary_value( + kv: KV, + value: Vec, + grove_version: &GroveVersion, + ) -> Result { + match grove_version.merk_versions.tree.put_value { + 0 => Ok(Self::install_ordinary_value_v0(kv, value)), + 1 => Ok(Self::install_ordinary_value_v1(kv, value)), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "install_ordinary_value".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )), + } + } } #[cfg(test)] diff --git a/merk/src/tree/put_value/v0.rs b/merk/src/tree/put_value/v0.rs index 96e63d5fd..bee7834ff 100644 --- a/merk/src/tree/put_value/v0.rs +++ b/merk/src/tree/put_value/v0.rs @@ -12,11 +12,20 @@ use grovedb_costs::{ use grovedb_version::version::GroveVersion; use crate::{ - tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, + tree::{ + kv::{ValueDefinedCostType, KV}, + CryptoHash, TreeFeatureType, TreeNode, + }, Error, }; impl TreeNode { + /// How version 0 installs an ordinary value on a node: it keeps the `value_defined_cost` the + /// node was loaded with. + pub(super) fn install_ordinary_value_v0(kv: KV, value: Vec) -> KV { + kv.put_value_no_update_of_hashes(value) + } + /// Version 0 of [`TreeNode::put_value`]. #[inline] pub(super) fn put_value_v0( @@ -48,7 +57,7 @@ impl TreeNode { ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v0(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { @@ -103,7 +112,7 @@ impl TreeNode { ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v0(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { diff --git a/merk/src/tree/put_value/v1.rs b/merk/src/tree/put_value/v1.rs index 77d91ff35..0c1122060 100644 --- a/merk/src/tree/put_value/v1.rs +++ b/merk/src/tree/put_value/v1.rs @@ -13,11 +13,20 @@ use grovedb_costs::{ use grovedb_version::version::GroveVersion; use crate::{ - tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, + tree::{ + kv::{ValueDefinedCostType, KV}, + CryptoHash, TreeFeatureType, TreeNode, + }, Error, }; impl TreeNode { + /// How version 1 installs an ordinary value on a node: it clears the `value_defined_cost` the + /// node was loaded with. + pub(super) fn install_ordinary_value_v1(kv: KV, value: Vec) -> KV { + kv.put_ordinary_value_no_update_of_hashes(value) + } + /// Version 1 of [`TreeNode::put_value`]. #[inline] pub(super) fn put_value_v1( @@ -49,7 +58,7 @@ impl TreeNode { ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_ordinary_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v1(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { @@ -104,7 +113,7 @@ impl TreeNode { ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_ordinary_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v1(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { From 530ac7a5df11fb62c910b4df3b8567bb16bbc9be Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 4 Oct 2026 00:05:53 +0700 Subject: [PATCH 04/10] fix(batch): third review fixes for settling storage owner changes Estimates: - The average-case settle raise charges the typical referrer shape (the average item fan-out, each entry sized from the write's own position) instead of the declared capacity at the largest entry; the worst case keeps the capacity bound. Both size a backward-references element without the entries it was written with, which it does not land with. - A settled tree is sized through Element::value_defined_cost on the empty tree of its type, instead of a copy of the layered-cost formula. - One SettledOwnerChange helper replaces the per-estimator closures and the two raise functions. Apply: - written_qualified_paths is only recorded when the version 1 target prediction can read it (GROVE_V4, or settle_owner_changes). - The same-batch reference path reads the target's bytes and decodes the element only on the version 0 route. - A SettleOwnerChange answer without settle_owner_changes now fails the batch with Error::InvalidBatchOperation instead of surfacing as a Merk client-corruption error, on the full and partial batch paths. TreeNode::provided_value_hash_put_final_value is renamed TreeNode::predict_put_final_value: it predicts every put kind. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 20 +- .../estimated_costs/average_case_costs.rs | 82 ++-- grovedb/src/batch/estimated_costs/mod.rs | 359 +++++++++++++----- .../batch/estimated_costs/worst_case_costs.rs | 67 ++-- .../just_in_time_reference_update/mod.rs | 59 ++- .../src/batch/just_in_time_value_update.rs | 31 +- grovedb/src/batch/mod.rs | 171 +++++++-- grovedb/src/batch/options.rs | 14 +- .../batch/settle_owner_change_cost_tests.rs | 99 +++-- .../src/tree/just_in_time_value_update/mod.rs | 8 +- 10 files changed, 631 insertions(+), 279 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1edcddc8a..a034a490a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,14 +28,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 keep the owner, or that have no owner on either side, are unchanged. The flags callback can now answer an `ElementFlagsUpdate` (`Unchanged`, `Changed`, `SettleOwnerChange`, new in `grovedb_costs`) in place of a - `bool`; a batch refuses `SettleOwnerChange` unless the option is set. With - the option, the average-case and worst-case batch estimates charge every - `InsertOrReplace`, `Replace`, `Patch` and trusted `RefreshReference` of an - element with flags, and the write of a flagged tree the batch also writes - under, at least the bytes its node adds when inserted (for a - backward-references element, with the referrers it carries over counted at - its declared capacity, each at the largest entry registration admits), so - an estimate is never below a settled apply. With the option off on + `bool`; a batch refuses `SettleOwnerChange` unless the option is set, with + `Error::InvalidBatchOperation`. With the option, the average-case and + worst-case batch estimates charge every `InsertOrReplace`, `Replace`, + `Patch` and trusted `RefreshReference` of an element with flags, and the + write of a flagged tree the batch also writes under, at least the bytes its + node adds when inserted. For a backward-references element the worst case + counts the referrers it carries over at its declared capacity, each at the + largest entry registration admits, so a worst-case estimate is never below + a settled apply; the average case counts the typical referrer shape. With the option off on GROVE_V1..V3, every cost, estimate, stored element and hash is what it was before; GROVE_V4 also carries the two fixes below (same-batch references to updated items, and replacements keeping flags of another length), which @@ -65,7 +66,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `TreeNode::put_value_with_reference_value_hash_and_value_cost`, `TreeNode::put_value_with_two_reference_value_hashes_and_value_cost` and their `Walker` counterparts take a `&GroveVersion`, and - `TreeNode::provided_value_hash_put_final_value` takes the value-defined + `TreeNode::provided_value_hash_put_final_value` is renamed + `TreeNode::predict_put_final_value` and takes the value-defined cost the stored node is loaded with, the put it predicts (the new `grovedb_merk::tree::PredictedPut`: an ordinary put as `TreeNode::put_value` installs it at the grove version, a provided-value-hash put, or a put at a diff --git a/grovedb/src/batch/estimated_costs/average_case_costs.rs b/grovedb/src/batch/estimated_costs/average_case_costs.rs index f6039ba56..30b9c4bf2 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -20,9 +20,7 @@ use grovedb_merk::estimated_costs::{ add_cost_case_merk_replace_layered, add_cost_case_merk_replace_same_size, }; use grovedb_merk::{ - element::tree_type::ElementTreeTypeExtensions, - tree::AggregateData, - tree_type::{CostSize, TreeType}, + element::tree_type::ElementTreeTypeExtensions, tree::AggregateData, tree_type::TreeType, RootHashKeyAndAggregateData, }; #[cfg(feature = "minimal")] @@ -110,19 +108,17 @@ impl GroveOp { grove_version: &GroveVersion, ) -> CostResult<(), Error> { let in_tree_type = layer_element_estimates.tree_type; - let settling = |base: CostResult<(), Error>, element: &Element| { - if batch_apply_options.settle_owner_changes { - super::with_settled_owner_change( - base, - key, - element, - in_tree_type.inner_node_type(), - grove_version, - ) - } else { - base - } - }; + // A backward-references element is charged the typical referrer + // shape, each referrer at the entry of one at this op's own position. + let settling = super::SettledOwnerChange::new( + batch_apply_options, + key, + in_tree_type.inner_node_type(), + super::CarriedReferrers::Average { + entry_bytes: super::backward_reference_entry_bound(path, key), + }, + grove_version, + ); let propagate_if_input = || { if propagate { Some(layer_element_estimates) @@ -251,44 +247,32 @@ impl GroveOp { not_summed, not_counted_or_summed, .. - } => { - // Account for the wrapper byte if the op rebuilds the - // tree as `NonCounted(...)`, `NotSummed(...)`, or - // `NotCountedOrSummed(...)`. They share the same +1 - // discriminant overhead and are mutually exclusive on - // the rebuilt element. - let wrapper_overhead = - super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed); - let insert_cost = GroveDb::average_case_merk_insert_tree( + } => settling.raise_tree( + GroveDb::average_case_merk_insert_tree( key, flags, aggregate_data.parent_tree_type(), in_tree_type, - wrapper_overhead, + // Account for the wrapper byte if the op rebuilds the + // tree as `NonCounted(...)`, `NotSummed(...)`, or + // `NotCountedOrSummed(...)`. They share the same +1 + // discriminant overhead and are mutually exclusive on + // the rebuilt element. + super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed), propagate_if_input(), grove_version, - ); - // The write of a tree the batch also writes under: it may - // settle an owner change as a write of its element does. - if batch_apply_options.settle_owner_changes { - super::with_settled_tree_owner_change( - insert_cost, - key, - flags, - written_tree_type - .map_or(super::WORST_CASE_SETTLED_TREE_COST_SIZE, |tree_type| { - tree_type.cost_size() - }), - wrapper_overhead, - in_tree_type.inner_node_type(), - ) - } else { - insert_cost - } - } + ), + // The write of a tree the batch also writes under may settle + // an owner change as a write of its element does. + written_tree_type.unwrap_or(super::WORST_CASE_SETTLED_TREE_TYPE), + flags, + *non_counted, + *not_summed, + *not_counted_or_summed, + ), GroveOp::InsertOrReplace { element } | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - settling( + settling.raise( GroveDb::average_case_merk_insert_element( key, element, @@ -394,14 +378,14 @@ impl GroveOp { // An untrusted refresh writes the stored flags back, so only // a trusted one can change the owner. if mode.is_trusted() { - settling(replace_cost, &element) + settling.raise(replace_cost, &element) } else { replace_cost } } GroveOp::Replace { element } | GroveOp::ReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - settling( + settling.raise( GroveDb::average_case_merk_replace_element( key, element, @@ -421,7 +405,7 @@ impl GroveOp { element, change_in_bytes, } => with_fan_out( - settling( + settling.raise( GroveDb::average_case_merk_patch_element( key, element, diff --git a/grovedb/src/batch/estimated_costs/mod.rs b/grovedb/src/batch/estimated_costs/mod.rs index b3806daac..15fab7a64 100644 --- a/grovedb/src/batch/estimated_costs/mod.rs +++ b/grovedb/src/batch/estimated_costs/mod.rs @@ -89,22 +89,55 @@ pub(in crate::batch) const WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND: u32 = + grovedb_merk::estimated_costs::worst_case_costs::MERK_BIGGEST_KEY_SIZE) + REFERRER_ENTRY_FRAMING_BYTES; +/// How a settle estimate sizes the referrer entries a backward-references +/// element lands with: the referrers registered on the element it replaces, +/// in place of the ones it was written with. (Only backward-references +/// elements declare a capacity.) +#[cfg(feature = "minimal")] +#[derive(Clone, Copy, Debug)] +pub(in crate::batch) enum CarriedReferrers { + /// Every referrer the element's declared capacity admits, each at + /// [`WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND`]: the referrers can sit + /// anywhere registration admits them, so no settled apply adds more. + Worst, + /// The typical shape: as many referrers as the average item fan-out + /// ([`BACKWARD_REFERENCES_AVERAGE_ITEM_FAN_OUT`], never more than the + /// declared capacity), each with an entry of `entry_bytes`. + Average { + /// The bytes of one referrer entry. + entry_bytes: u32, + }, +} + +#[cfg(feature = "minimal")] +impl CarriedReferrers { + /// The bytes of the referrer entries an element declaring `capacity` + /// referrers lands with. + fn bytes(self, capacity: u16) -> u32 { + match self { + CarriedReferrers::Worst => { + (capacity as u32).saturating_mul(WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND) + } + CarriedReferrers::Average { entry_bytes } => BACKWARD_REFERENCES_AVERAGE_ITEM_FAN_OUT + .min(capacity as u32) + .saturating_mul(entry_bytes), + } + } +} + /// The bytes a write of `element` at a key of `key_len` bytes adds when it /// settles an owner change: the whole node, key and value, sized exactly as /// the apply sizes the node it stores in a tree of `node_type` nodes. /// -/// A backward-references element lands with the referrers registered on the -/// element it replaces in place of the ones it was written with, and the -/// settled apply adds their entries too. Those referrers can sit anywhere -/// registration admits them, so they are charged at the element's declared -/// capacity, each at [`WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND`]. (Only -/// backward-references elements declare a capacity, and none of them has a -/// value-defined cost.) +/// A backward-references element is sized without the referrer entries it +/// was written with, plus the entries `carried_referrers` gives it. (None of +/// those elements has a value-defined cost.) #[cfg(feature = "minimal")] fn settled_owner_change_added_bytes( key_len: u32, element: &crate::Element, node_type: grovedb_merk::merk::NodeType, + carried_referrers: CarriedReferrers, grove_version: &grovedb_version::version::GroveVersion, ) -> Result { use grovedb_merk::{ @@ -122,12 +155,13 @@ fn settled_owner_change_added_bytes( )) } Some(SpecializedValueDefinedCost(value_cost)) => value_cost, - None => { - let carried_referrer_bytes = element.max_incoming_references().map_or(0, |capacity| { - (capacity as u32).saturating_mul(WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND) - }); - (element.serialized_size(grove_version)? as u32).saturating_add(carried_referrer_bytes) - } + None => match element.max_incoming_references() { + Some(capacity) => (element + .stripped_of_backward_references() + .serialized_size(grove_version)? as u32) + .saturating_add(carried_referrers.bytes(capacity)), + None => element.serialized_size(grove_version)? as u32, + }, }; Ok(KV::node_byte_cost_size_for_key_and_raw_value_lengths( key_len, @@ -136,85 +170,141 @@ fn settled_owner_change_added_bytes( )) } -/// `base`, the estimate of a write of `element` over a stored element, -/// charged for the case that the write settles an owner change +/// The Merk tree a worst-case estimate sizes a settled +/// `InsertTreeWithRootHash` with (a count-sum tree, the largest any such op +/// writes): the op carries no aggregate data from which the tree type could +/// be read. +#[cfg(feature = "minimal")] +pub(in crate::batch) const WORST_CASE_SETTLED_TREE_TYPE: grovedb_merk::tree_type::TreeType = + grovedb_merk::tree_type::TreeType::CountSumTree; + +/// The empty Merk tree of `tree_type` with `flags`: its node is sized as the +/// node of any tree of that type with those flags. An `InsertTreeWithRootHash` +/// only writes Merk trees, so any other type is sized as +/// [`WORST_CASE_SETTLED_TREE_TYPE`]. +#[cfg(feature = "minimal")] +fn empty_merk_tree_with_flags( + tree_type: grovedb_merk::tree_type::TreeType, + flags: Option, +) -> crate::Element { + use grovedb_merk::tree_type::TreeType; + + use crate::Element; + + match tree_type { + TreeType::NormalTree => Element::empty_tree_with_flags(flags), + TreeType::SumTree => Element::empty_sum_tree_with_flags(flags), + TreeType::BigSumTree => Element::empty_big_sum_tree_with_flags(flags), + TreeType::CountTree => Element::empty_count_tree_with_flags(flags), + TreeType::CountSumTree => Element::empty_count_sum_tree_with_flags(flags), + TreeType::ProvableCountTree => Element::empty_provable_count_tree_with_flags(flags), + TreeType::ProvableCountSumTree => Element::empty_provable_count_sum_tree_with_flags(flags), + TreeType::ProvableSumTree => Element::empty_provable_sum_tree_with_flags(flags), + TreeType::ProvableCountProvableSumTree => { + Element::empty_provable_count_provable_sum_tree_with_flags(flags) + } + _ => empty_merk_tree_with_flags(WORST_CASE_SETTLED_TREE_TYPE, flags), + } +} + +/// The raise an estimate applies to a write that may settle an owner change /// ([`BatchApplyOptions::settle_owner_changes`](crate::batch::BatchApplyOptions::settle_owner_changes)): -/// its added bytes are raised to every byte of the element's node, key -/// included, sized for a tree of `node_type` nodes with the referrer entries -/// a backward-references element may carry over, so the estimate covers both -/// the replacement it models and the insertion a settled owner change -/// records. An element without flags names no owner and never settles, so -/// its estimate is left as it is. -#[cfg(feature = "minimal")] -pub(in crate::batch) fn with_settled_owner_change( - base: grovedb_costs::CostResult<(), crate::Error>, - key: &crate::batch::KeyInfo, - element: &crate::Element, +/// the write's added bytes are raised to every byte of the node it writes, +/// key included, sized for a tree of `node_type` nodes with the referrer +/// entries `carried_referrers` gives a backward-references element, so the +/// estimate covers both the replacement it models and the insertion a +/// settled owner change records. Without the option, or for an element +/// without flags (which names no owner and never settles), the estimate is +/// left as it is. +#[cfg(feature = "minimal")] +pub(in crate::batch) struct SettledOwnerChange<'a> { + settle_owner_changes: bool, + key: &'a crate::batch::KeyInfo, node_type: grovedb_merk::merk::NodeType, - grove_version: &grovedb_version::version::GroveVersion, -) -> grovedb_costs::CostResult<(), crate::Error> { - use grovedb_costs::CostsExt; - use grovedb_storage::worst_case_costs::WorstKeyLength; + carried_referrers: CarriedReferrers, + grove_version: &'a grovedb_version::version::GroveVersion, +} - let grovedb_costs::CostContext { value, mut cost } = base; - if value.is_ok() && element.get_flags().is_some() { - match settled_owner_change_added_bytes( - key.max_length() as u32, - element, +#[cfg(feature = "minimal")] +impl<'a> SettledOwnerChange<'a> { + /// The raise for a write at `key` in a tree of `node_type` nodes. + pub(in crate::batch) fn new( + batch_apply_options: &crate::batch::BatchApplyOptions, + key: &'a crate::batch::KeyInfo, + node_type: grovedb_merk::merk::NodeType, + carried_referrers: CarriedReferrers, + grove_version: &'a grovedb_version::version::GroveVersion, + ) -> Self { + Self { + settle_owner_changes: batch_apply_options.settle_owner_changes, + key, node_type, + carried_referrers, grove_version, - ) { - Ok(settled_bytes) => { - cost.storage_cost.added_bytes = cost.storage_cost.added_bytes.max(settled_bytes) + } + } + + /// `base`, the estimate of a write of `element` over a stored element, + /// raised for the case that the write settles an owner change. + pub(in crate::batch) fn raise( + &self, + base: grovedb_costs::CostResult<(), crate::Error>, + element: &crate::Element, + ) -> grovedb_costs::CostResult<(), crate::Error> { + use grovedb_costs::CostsExt; + use grovedb_storage::worst_case_costs::WorstKeyLength; + + if !self.settle_owner_changes || element.get_flags().is_none() { + return base; + } + let grovedb_costs::CostContext { value, mut cost } = base; + if value.is_ok() { + match settled_owner_change_added_bytes( + self.key.max_length() as u32, + element, + self.node_type, + self.carried_referrers, + self.grove_version, + ) { + Ok(settled_bytes) => { + cost.storage_cost.added_bytes = cost.storage_cost.added_bytes.max(settled_bytes) + } + Err(e) => return Err(e).wrap_with_cost(cost), } - Err(e) => return Err(e).wrap_with_cost(cost), } + value.wrap_with_cost(cost) } - value.wrap_with_cost(cost) -} -/// The largest cost size of a Merk tree an `InsertTreeWithRootHash` can -/// write (a count-sum tree's), which a worst-case estimate sizes a settled -/// tree with: the op carries no aggregate data from which the tree type -/// could be read. -#[cfg(feature = "minimal")] -pub(in crate::batch) const WORST_CASE_SETTLED_TREE_COST_SIZE: u32 = - grovedb_merk::tree_type::COUNT_SUM_TREE_COST_SIZE; + /// [`Self::raise`] for the `InsertTreeWithRootHash` that a write of a + /// tree becomes when the batch also writes under it: the tree element + /// that op writes, a tree of `tree_type` with `flags` in the wrapper the + /// op rebuilds it in. + pub(in crate::batch) fn raise_tree( + &self, + base: grovedb_costs::CostResult<(), crate::Error>, + tree_type: grovedb_merk::tree_type::TreeType, + flags: &Option, + non_counted: bool, + not_summed: bool, + not_counted_or_summed: bool, + ) -> grovedb_costs::CostResult<(), crate::Error> { + use crate::Element; -/// [`with_settled_owner_change`] for the `InsertTreeWithRootHash` that a -/// write of a tree becomes when the batch also writes under it: `base` is -/// raised by the tree element that op writes — a tree of `tree_cost_size` -/// with `flags`, and `wrapper_overhead` wrapper bytes — sized as the apply -/// sizes it in a tree of `node_type` nodes. -#[cfg(feature = "minimal")] -pub(in crate::batch) fn with_settled_tree_owner_change( - base: grovedb_costs::CostResult<(), crate::Error>, - key: &crate::batch::KeyInfo, - flags: &Option, - tree_cost_size: u32, - wrapper_overhead: u32, - node_type: grovedb_merk::merk::NodeType, -) -> grovedb_costs::CostResult<(), crate::Error> { - use grovedb_costs::CostsExt; - use grovedb_merk::tree::kv::KV; - use grovedb_storage::worst_case_costs::WorstKeyLength; - use integer_encoding::VarInt; - - let grovedb_costs::CostContext { value, mut cost } = base; - if value.is_ok() - && let Some(flags) = flags - { - let flags_len = flags.len() as u32; - let value_cost = - tree_cost_size + flags_len + flags_len.required_space() as u32 + wrapper_overhead; - let settled_bytes = KV::layered_node_byte_cost_size_for_key_and_value_lengths( - key.max_length() as u32, - value_cost, - node_type, - ); - cost.storage_cost.added_bytes = cost.storage_cost.added_bytes.max(settled_bytes); + if !self.settle_owner_changes || flags.is_none() { + return base; + } + let tree = empty_merk_tree_with_flags(tree_type, flags.clone()); + let tree = if non_counted { + Element::NonCounted(Box::new(tree)) + } else if not_summed { + Element::NotSummed(Box::new(tree)) + } else if not_counted_or_summed { + Element::NotCountedOrSummed(Box::new(tree)) + } else { + tree + }; + self.raise(base, &tree) } - value.wrap_with_cost(cost) } // ── Backward-references fan-out estimation model ──────────────────────── @@ -813,23 +903,55 @@ mod tests { tree::kv::KV, tree_type::{CostSize, TreeType}, }; + use grovedb_version::version::GroveVersion; + + use crate::{ + batch::{BatchApplyOptions, KeyInfo}, + bidirectional_references::BackwardReference, + reference_path::ReferencePathType, + BackwardReferences, Element, + }; - use crate::batch::KeyInfo; + fn settle_options() -> BatchApplyOptions { + BatchApplyOptions { + settle_owner_changes: true, + ..Default::default() + } + } + + /// The added bytes the settle raise gives a write of `element` at `key` + /// whose own estimate adds nothing. + fn raised(settling: &super::SettledOwnerChange, element: &Element) -> u32 { + settling + .raise(Ok(()).wrap_with_cost(OperationCost::default()), element) + .cost + .storage_cost + .added_bytes + } /// The worst-case size of a settled tree bounds the node of every Merk /// tree an `InsertTreeWithRootHash` can write, in a tree of any node /// type. #[test] fn the_worst_case_settled_tree_bounds_every_merk_tree_in_every_node_type() { + let grove_version = GroveVersion::latest(); + let options = settle_options(); let key = KeyInfo::KnownKey(b"tree".to_vec()); let flags = Some(vec![0; 35]); - let raised = super::with_settled_tree_owner_change( - Ok(()).wrap_with_cost(OperationCost::default()), + let raised = super::SettledOwnerChange::new( + &options, &key, - &flags, - super::WORST_CASE_SETTLED_TREE_COST_SIZE, - 0, super::WORST_CASE_SETTLED_NODE_TYPE, + super::CarriedReferrers::Worst, + grove_version, + ) + .raise_tree( + Ok(()).wrap_with_cost(OperationCost::default()), + super::WORST_CASE_SETTLED_TREE_TYPE, + &flags, + false, + false, + false, ) .cost .storage_cost @@ -869,4 +991,67 @@ mod tests { } } } + + /// A backward-references element lands with the referrers of the + /// element it replaces, so the settle raise sizes it without the + /// entries it was written with: the worst case adds its declared + /// capacity of the largest entries, the average case the typical + /// number of entries at the size it is given. + #[test] + fn a_settled_backward_references_item_is_sized_by_its_carried_referrers() { + let grove_version = GroveVersion::latest(); + let options = settle_options(); + let key = KeyInfo::KnownKey(b"item".to_vec()); + let flags = Some(vec![0; 35]); + let item = |entries: Vec| { + Element::ItemWithBackwardsReferences( + vec![7; 20], + BackwardReferences::new(32, entries), + flags.clone(), + ) + }; + let entry = |key: &[u8]| BackwardReference { + inverted_reference: ReferencePathType::SiblingReference(key.to_vec()), + cascade_on_update: false, + }; + let without_entries = item(vec![]); + let with_entries = item(vec![entry(b"r1"), entry(b"r2"), entry(b"r3")]); + let node_type = NodeType::NormalNode; + let entry_bytes = 40; + let worst = super::SettledOwnerChange::new( + &options, + &key, + node_type, + super::CarriedReferrers::Worst, + grove_version, + ); + let average = super::SettledOwnerChange::new( + &options, + &key, + node_type, + super::CarriedReferrers::Average { entry_bytes }, + grove_version, + ); + for settling in [&worst, &average] { + assert_eq!( + raised(settling, &with_entries), + raised(settling, &without_entries), + "the entries the item was written with are not the ones it lands with" + ); + } + let element_bytes = without_entries.serialized_size(grove_version).unwrap() as u32; + let node_bytes = |raw_value_len| { + KV::node_byte_cost_size_for_key_and_raw_value_lengths(4, raw_value_len, node_type) + }; + assert_eq!( + raised(&worst, &without_entries), + node_bytes(element_bytes + 32 * super::WORST_CASE_BACKWARD_REFERENCE_ENTRY_BOUND) + ); + assert_eq!( + raised(&average, &without_entries), + node_bytes( + element_bytes + super::BACKWARD_REFERENCES_AVERAGE_ITEM_FAN_OUT * entry_bytes + ) + ); + } } diff --git a/grovedb/src/batch/estimated_costs/worst_case_costs.rs b/grovedb/src/batch/estimated_costs/worst_case_costs.rs index 2c33b4c38..e512c5e6d 100644 --- a/grovedb/src/batch/estimated_costs/worst_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/worst_case_costs.rs @@ -98,19 +98,13 @@ impl GroveOp { None } }; - let settling = |base: CostResult<(), Error>, element: &Element| { - if batch_apply_options.settle_owner_changes { - super::with_settled_owner_change( - base, - key, - element, - super::WORST_CASE_SETTLED_NODE_TYPE, - grove_version, - ) - } else { - base - } - }; + let settling = super::SettledOwnerChange::new( + batch_apply_options, + key, + super::WORST_CASE_SETTLED_NODE_TYPE, + super::CarriedReferrers::Worst, + grove_version, + ); let fan_out_version = grove_version .grovedb_versions .operations @@ -230,41 +224,28 @@ impl GroveOp { not_summed, not_counted_or_summed, .. - } => { - // Account for the wrapper byte if the op rebuilds the - // tree as `NonCounted(...)`, `NotSummed(...)`, or - // `NotCountedOrSummed(...)`. They share the same +1 - // discriminant overhead and are mutually exclusive on - // the rebuilt element. - let wrapper_overhead = - super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed); - let insert_cost = GroveDb::worst_case_merk_insert_tree( + } => settling.raise_tree( + GroveDb::worst_case_merk_insert_tree( key, flags, aggregate_data.parent_tree_type(), in_parent_tree_type, - wrapper_overhead, + // See the comment in the corresponding average-case arm. + super::wrapper_overhead_for(*non_counted, *not_summed, *not_counted_or_summed), propagate_if_input(), grove_version, - ); - // The write of a tree the batch also writes under: it may - // settle an owner change as a write of its element does. - if batch_apply_options.settle_owner_changes { - super::with_settled_tree_owner_change( - insert_cost, - key, - flags, - super::WORST_CASE_SETTLED_TREE_COST_SIZE, - wrapper_overhead, - super::WORST_CASE_SETTLED_NODE_TYPE, - ) - } else { - insert_cost - } - } + ), + // The write of a tree the batch also writes under may settle + // an owner change as a write of its element does. + super::WORST_CASE_SETTLED_TREE_TYPE, + flags, + *non_counted, + *not_summed, + *not_counted_or_summed, + ), GroveOp::InsertOrReplace { element } | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - settling( + settling.raise( GroveDb::worst_case_merk_insert_element( key, element, @@ -361,14 +342,14 @@ impl GroveOp { // An untrusted refresh writes the stored flags back, so only // a trusted one can change the owner. if mode.is_trusted() { - settling(replace_cost, &element) + settling.raise(replace_cost, &element) } else { replace_cost } } GroveOp::Replace { element } | GroveOp::ReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - settling( + settling.raise( GroveDb::worst_case_merk_replace_element( key, element, @@ -388,7 +369,7 @@ impl GroveOp { element, change_in_bytes: _, } => with_fan_out( - settling( + settling.raise( GroveDb::worst_case_merk_replace_element( key, element, diff --git a/grovedb/src/batch/just_in_time_reference_update/mod.rs b/grovedb/src/batch/just_in_time_reference_update/mod.rs index bee636ed0..1b643db31 100644 --- a/grovedb/src/batch/just_in_time_reference_update/mod.rs +++ b/grovedb/src/batch/just_in_time_reference_update/mod.rs @@ -30,7 +30,7 @@ mod v1; use std::collections::HashMap; use grovedb_costs::{ - cost_return_on_error_into_no_add, + cost_return_on_error_into_no_add, cost_return_on_error_no_add, storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; @@ -75,20 +75,32 @@ where Ok(val_hash).wrap_with_cost(cost) } + /// Whether a same-batch reference predicts its target with version 1, + /// which reads whether this pass has already written the target, so + /// the pass must record the targets it writes + /// (`written_qualified_paths`). + pub(crate) fn tracks_written_targets(&self, grove_version: &GroveVersion) -> bool { + self.settle_owner_changes + || grove_version + .grovedb_versions + .apply_batch + .same_batch_reference_target_prediction + != 0 + } + /// The value hash of the bytes a pending write of `new_element` at - /// `qualified_path` over the stored `old_element` finally stores, after - /// the apply has run the caller's flags update on it, so that a - /// reference written in the same batch commits to them. `target_written` - /// says this pass has already written the target, so the stored bytes - /// are the ones it stores. See the module docs for the version - /// semantics. + /// `qualified_path` over the stored `old_serialized_element` finally + /// stores, after the apply has run the caller's flags update on it, so + /// that a reference written in the same batch commits to them. + /// `target_written` says this pass has already written the target, so + /// the stored bytes are the ones it stores. See the module docs for the + /// version semantics. #[allow(clippy::too_many_arguments)] pub(crate) fn process_old_element_flags( key: &[u8], qualified_path: &[Vec], serialized: Vec, new_element: &Element, - old_element: Element, old_serialized_element: Vec, in_tree_type: TreeType, settle_owner_changes: bool, @@ -117,17 +129,26 @@ where .same_batch_reference_target_prediction, settle_owner_changes, ) { - (0, false) => v0::process_old_element_flags_v0( - key, - &serialized, - new_element, - old_element, - &old_serialized_element, - in_tree_type, - flags_update, - split_removal_bytes, - grove_version, - ), + (0, false) => { + let cost = OperationCost::default(); + let old_element = cost_return_on_error_no_add!( + cost, + Element::deserialize(&old_serialized_element, grove_version).map_err(|e| { + Error::CorruptedData(format!("unable to deserialize element: {e}")) + }) + ); + v0::process_old_element_flags_v0( + key, + &serialized, + new_element, + old_element, + &old_serialized_element, + in_tree_type, + flags_update, + split_removal_bytes, + grove_version, + ) + } (0, true) | (1, _) => v1::process_old_element_flags_v1( key, qualified_path, diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index 31df8211b..026c7de10 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -11,6 +11,8 @@ //! cross-epoch update. [`predict_provided_value_hash_put`] runs this update //! through Merk's own routine to get those bytes. +use std::cell::Cell; + use grovedb_costs::storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, transition::ElementFlagsUpdate, @@ -33,10 +35,17 @@ use integer_encoding::VarInt; use crate::{Element, ElementFlags, Error}; +/// Why a batch whose flags update settled an owner change was refused: its +/// options do not set `settle_owner_changes`. +pub(crate) const SETTLE_OWNER_CHANGES_NOT_SET: &str = + "the flags update settled an owner change, but the batch options do not set \ + settle_owner_changes"; + /// The caller's flags-update callback as a batch runs it: its answer as an /// [`ElementFlagsUpdate`] (a `bool` answer is `Changed` or `Unchanged`), and /// `SettleOwnerChange` refused unless the batch's options set -/// `settle_owner_changes`. +/// `settle_owner_changes`. A refusal is recorded in `refused`, so the batch +/// reports it with [`settle_refusal_error`] whatever error it surfaces as. pub(crate) fn batch_flags_update( mut flags_update: impl FnMut( &StorageCost, @@ -44,6 +53,7 @@ pub(crate) fn batch_flags_update( &mut ElementFlags, ) -> Result, settle_owner_changes: bool, + refused: &Cell, ) -> impl FnMut(&StorageCost, Option, &mut ElementFlags) -> Result where U: Into, @@ -53,16 +63,27 @@ where new_flags: &mut ElementFlags| { let update = flags_update(storage_cost, old_flags, new_flags)?.into(); if update == ElementFlagsUpdate::SettleOwnerChange && !settle_owner_changes { + refused.set(true); return Err(Error::JustInTimeElementFlagsClientError( - "the flags update settled an owner change, but the batch options do not set \ - settle_owner_changes" - .to_owned(), + SETTLE_OWNER_CHANGES_NOT_SET.to_owned(), )); } Ok(update) } } +/// The error a batch run with [`batch_flags_update`] reports: the refusal of +/// a settled owner change, as the invalid batch it is, when `refused` says +/// one happened (the Merk and prediction layers it surfaced through report +/// it as client or data corruption), and `error` otherwise. +pub(crate) fn settle_refusal_error(error: Error, refused: &Cell) -> Error { + if refused.get() { + Error::InvalidBatchOperation(SETTLE_OWNER_CHANGES_NOT_SET) + } else { + error + } +} + /// The caller's flags-update callback with its answer as an /// [`ElementFlagsUpdate`] (a `bool` answer is `Changed` or `Unchanged`). #[cfg(feature = "estimated_costs")] @@ -295,7 +316,7 @@ where { let old_value_defined_cost = Element::value_defined_cost_for_serialized_value(&old_serialized, grove_version); - TreeNode::provided_value_hash_put_final_value( + TreeNode::predict_put_final_value( key.to_vec(), old_serialized, old_value_defined_cost, diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index d61b2afe3..63cfe1b59 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -49,6 +49,7 @@ mod single_sum_item_insert_cost_tests; use crate::{bidirectional_references::batch_maintains_backward_references, BackwardsReferences}; use core::fmt; use std::{ + cell::Cell, cmp::Ordering, collections::{btree_map::Entry, hash_map::Entry as HashMapEntry, BTreeMap, HashMap, HashSet}, hash::{Hash, Hasher}, @@ -2511,6 +2512,36 @@ where ) -> CostResult, TreeType)>, Error> { let mut cost = OperationCost::default(); + let referenced_element = cost_return_on_error!( + &mut cost, + self.get_referenced_element_bytes(key, reference_path, grove_version) + ); + + if let Some((referenced_element, tree_type)) = referenced_element { + let element = cost_return_on_error_no_add!( + cost, + Element::deserialize(referenced_element.as_slice(), grove_version).map_err(|e| { + Error::CorruptedData(format!("unable to deserialize element: {e}")) + }) + ); + + Ok(Some((element, referenced_element, tree_type))).wrap_with_cost(cost) + } else { + Ok(None).wrap_with_cost(cost) + } + } + + /// The serialized bytes of the element at `key` under `reference_path` + /// as the Merk cache holds them, and the type of the tree holding it; + /// [`Self::get_and_deserialize_referenced_element`] without the decode. + fn get_referenced_element_bytes( + &mut self, + key: &[u8], + reference_path: &[Vec], + grove_version: &GroveVersion, + ) -> CostResult, TreeType)>, Error> { + let mut cost = OperationCost::default(); + let merk = match self.merks.entry(reference_path.to_vec()) { HashMapEntry::Occupied(o) => o.into_mut(), HashMapEntry::Vacant(v) => v.insert(cost_return_on_error!( @@ -2532,18 +2563,8 @@ where let tree_type = merk.tree_type; - if let Some(referenced_element) = referenced_element { - let element = cost_return_on_error_no_add!( - cost, - Element::deserialize(referenced_element.as_slice(), grove_version).map_err(|e| { - Error::CorruptedData(format!("unable to deserialize element: {e}")) - }) - ); - - Ok(Some((element, referenced_element, tree_type))).wrap_with_cost(cost) - } else { - Ok(None).wrap_with_cost(cost) - } + Ok(referenced_element.map(|referenced_element| (referenced_element, tree_type))) + .wrap_with_cost(cost) } /// Processes a reference with a hop count greater than one, handling the @@ -2846,13 +2867,13 @@ where .expect("path validated non-empty above"); let serialized_element_result = cost_return_on_error!( &mut cost, - self.get_and_deserialize_referenced_element( + self.get_referenced_element_bytes( key, reference_path, grove_version ) ); - if let Some((old_element, old_serialized_element, is_in_sum_tree)) = + if let Some((old_serialized_element, is_in_sum_tree)) = serialized_element_result { let target_written = @@ -2864,7 +2885,6 @@ where qualified_path, serialized, element, - old_element, old_serialized_element, is_in_sum_tree, self.settle_owner_changes, @@ -3227,15 +3247,20 @@ where let path = &p; self.unused_new_merks.remove(path); // The targets this path writes, recorded as written once it has - // applied them. - let written_qualified_paths: Vec>> = ops_at_path_by_key - .keys() - .map(|key_info| { - let mut qualified_path = path.to_vec(); - qualified_path.push(key_info.get_key_clone()); - qualified_path - }) - .collect(); + // applied them (only read by the version 1 target prediction). + let written_qualified_paths: Vec>> = + if self.tracks_written_targets(grove_version) { + ops_at_path_by_key + .keys() + .map(|key_info| { + let mut qualified_path = path.to_vec(); + qualified_path.push(key_info.get_key_clone()); + qualified_path + }) + .collect() + } else { + Vec::new() + }; // This also populates Merk trees cache let in_tree_type = { @@ -5743,6 +5768,9 @@ impl GroveDb { // predicts the bytes it stores from what the first segment left, and // no prediction made before the continuation still holds. for op in &additional_ops { + if merk_tree_cache.written_qualified_paths.is_empty() { + break; + } if let Some(key) = &op.key { let mut qualified_path = op.path.to_path(); qualified_path.push(key.get_key_clone()); @@ -6806,7 +6834,8 @@ impl GroveDb { /// The flags callback answers a `bool` (whether it rewrote the flags) or /// an [`ElementFlagsUpdate`]; `SettleOwnerChange` is accepted only when /// the options set - /// [`settle_owner_changes`](BatchApplyOptions::settle_owner_changes). + /// [`settle_owner_changes`](BatchApplyOptions::settle_owner_changes); + /// without it the batch fails with [`Error::InvalidBatchOperation`]. pub fn apply_batch_with_element_flags_update>( &self, ops: Vec, @@ -6816,6 +6845,45 @@ impl GroveDb { Option, &mut ElementFlags, ) -> Result, + split_removal_bytes_function: impl FnMut( + &mut ElementFlags, + u32, // key removed bytes + u32, // value removed bytes + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + transaction: TransactionArg, + grove_version: &GroveVersion, + ) -> CostResult<(), Error> { + let settle_refused = Cell::new(false); + let update_element_flags_function = just_in_time_value_update::batch_flags_update( + update_element_flags_function, + BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), + &settle_refused, + ); + self.apply_batch_with_batch_flags_update( + ops, + batch_apply_options, + update_element_flags_function, + split_removal_bytes_function, + transaction, + grove_version, + ) + .map_err(|e| just_in_time_value_update::settle_refusal_error(e, &settle_refused)) + } + + /// [`Self::apply_batch_with_element_flags_update`] with the flags + /// callback as [`just_in_time_value_update::batch_flags_update`] runs it. + fn apply_batch_with_batch_flags_update( + &self, + ops: Vec, + batch_apply_options: Option, + mut update_element_flags_function: impl FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, mut split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -6834,10 +6902,6 @@ impl GroveDb { .apply_batch .apply_batch_with_element_flags_update ); - let mut update_element_flags_function = just_in_time_value_update::batch_flags_update( - update_element_flags_function, - BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), - ); let _storage_removal_version_guard = grovedb_costs::storage_cost::removal::use_basic_sectioned_removal_addition_version( grove_version @@ -7311,6 +7375,51 @@ impl GroveDb { Option, &mut ElementFlags, ) -> Result, + split_removal_bytes_function: impl FnMut( + &mut ElementFlags, + u32, // key removed bytes + u32, // value removed bytes + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + add_on_operations: impl FnMut( + &OperationCost, + &Option, + ) -> Result, Error>, + transaction: TransactionArg, + grove_version: &GroveVersion, + ) -> CostResult<(), Error> { + let settle_refused = Cell::new(false); + let update_element_flags_function = just_in_time_value_update::batch_flags_update( + update_element_flags_function, + BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), + &settle_refused, + ); + self.apply_partial_batch_with_batch_flags_update( + ops, + batch_apply_options, + update_element_flags_function, + split_removal_bytes_function, + add_on_operations, + transaction, + grove_version, + ) + .map_err(|e| just_in_time_value_update::settle_refusal_error(e, &settle_refused)) + } + + /// [`Self::apply_partial_batch_with_element_flags_update`] with the + /// flags callback as [`just_in_time_value_update::batch_flags_update`] + /// runs it. + fn apply_partial_batch_with_batch_flags_update( + &self, + ops: Vec, + batch_apply_options: Option, + mut update_element_flags_function: impl FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, mut split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -7333,10 +7442,6 @@ impl GroveDb { .apply_batch .apply_partial_batch_with_element_flags_update ); - let mut update_element_flags_function = just_in_time_value_update::batch_flags_update( - update_element_flags_function, - BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), - ); let _storage_removal_version_guard = grovedb_costs::storage_cost::removal::use_basic_sectioned_removal_addition_version( grove_version diff --git a/grovedb/src/batch/options.rs b/grovedb/src/batch/options.rs index e37abcca1..de3e8b424 100644 --- a/grovedb/src/batch/options.rs +++ b/grovedb/src/batch/options.rs @@ -87,16 +87,20 @@ pub struct BatchApplyOptions { /// sectioned by the removal-bytes callback as a deletion sections them; /// the new element's bytes, key included, count as added, and it keeps /// the flags the callback leaves it with. A batch that does not set this - /// refuses a flags update that answers `SettleOwnerChange`. + /// refuses a flags update that answers `SettleOwnerChange` with + /// [`Error::InvalidBatchOperation`](crate::Error::InvalidBatchOperation). /// /// The estimators read it too: an estimate with it set charges every /// write that may settle an owner change (`InsertOrReplace`, `Replace`, /// `Patch` and a trusted `RefreshReference` of an element with flags, and /// the write of a flagged tree the batch also writes under) at least the - /// bytes its element adds when inserted, with the referrer entries a - /// backward-references element carries over counted at its declared - /// capacity, each at the largest entry registration admits, so the - /// estimate is never below what the apply records. + /// bytes its element adds when inserted. The referrer entries a + /// backward-references element carries over are counted, in the worst + /// case, at its declared capacity, each at the largest entry + /// registration admits, so a worst-case estimate is never below what the + /// apply records; the average case counts the typical shape (the + /// average item fan-out, each entry the size of one from the write's own + /// position). /// /// Off by default. On GROVE_V1..V3 every cost, estimate, stored element /// and hash is then what it was before the option existed; GROVE_V4 also diff --git a/grovedb/src/batch/settle_owner_change_cost_tests.rs b/grovedb/src/batch/settle_owner_change_cost_tests.rs index 0c06e6e8b..6659df585 100644 --- a/grovedb/src/batch/settle_owner_change_cost_tests.rs +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -40,7 +40,7 @@ mod tests { }, reference_path::ReferencePathType, tests::{common::EMPTY_PATH, make_empty_grovedb, TempGroveDb}, - BackwardReferences, Element, GroveDb, + BackwardReferences, Element, Error, GroveDb, }; fn target_path() -> ReferencePathType { @@ -467,9 +467,10 @@ mod tests { } /// A backward-references item keeps the referrers registered on the item - /// it replaces, and a settled write adds their entries too. Both - /// estimates cover them through the item's declared capacity, wherever - /// the referrers sit. + /// it replaces, and a settled write adds their entries too. The + /// worst-case estimate covers them through the item's declared capacity, + /// wherever the referrers sit; the average case charges the typical + /// shape, which covers referrers beside the item. #[test] fn estimates_cover_the_referrers_a_settled_item_carries_over() { let grove_version = GroveVersion::latest(); @@ -565,17 +566,35 @@ mod tests { applied.storage_cost.added_bytes > key_bytes() + value_bytes(&new, TreeType::NormalTree, grove_version) ); - for average_case_tree_type in [Some(TreeType::NormalTree), None] { - let estimate = estimate( - vec![write_op(&new)], - Some(options(Mode::Settling)), - average_case_tree_type, - grove_version, - ); + let worst_case = estimate( + vec![write_op(&new)], + Some(options(Mode::Settling)), + None, + grove_version, + ); + assert!( + worst_case.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "{placement}: worst-case estimate {worst_case:?} adds less than the settled \ + apply {applied:?}" + ); + // The average case charges the typical referrer shape: it covers + // referrers beside the item, and stays below the worst case. + let average_case = estimate( + vec![write_op(&new)], + Some(options(Mode::Settling)), + Some(TreeType::NormalTree), + grove_version, + ); + assert!( + average_case.storage_cost.added_bytes < worst_case.storage_cost.added_bytes, + "{placement}: average-case estimate {average_case:?} is not below the worst \ + case {worst_case:?}" + ); + if placement == "siblings" { assert!( - estimate.storage_cost.added_bytes >= applied.storage_cost.added_bytes, - "{placement}: estimate {estimate:?} adds less than the settled apply \ - {applied:?}" + average_case.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "{placement}: average-case estimate {average_case:?} adds less than the \ + settled apply {applied:?}" ); } } @@ -746,31 +765,61 @@ mod tests { } } + /// A settling answer without the option refuses the batch as an invalid + /// batch, whether the apply meets it first, a same-batch reference's + /// prediction does, or a partial batch's initial segment does. #[test] fn a_settling_flags_update_is_refused_without_the_option() { let grove_version = GroveVersion::latest(); let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); let new = Element::new_item_with_flags(vec![8; 60], owned_flags(2, NEW_OWNER)); + let reference = QualifiedGroveDbOp::insert_or_replace_op( + vec![b"targets".to_vec()], + b"reference".to_vec(), + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + b"tree".to_vec(), + KEY.to_vec(), + ])), + ); + let assert_refused = |error: Error| { + assert!( + matches!(error, Error::InvalidBatchOperation(reason) if reason.contains("settle_owner_changes")), + "unexpected error: {error}" + ); + }; + for ops in [vec![write_op(&new)], vec![write_op(&new), reference]] { + let db = grove_with(&old, TreeType::NormalTree, grove_version); + let root_hash = db.root_hash(None, grove_version).unwrap().unwrap(); + assert_refused( + db.apply_batch_with_element_flags_update( + ops, + None, + settling_flags_update, + split_removal_bytes, + None, + grove_version, + ) + .unwrap() + .expect_err("expected the settling answer to be refused"), + ); + assert_eq!( + db.root_hash(None, grove_version).unwrap().unwrap(), + root_hash + ); + } let db = grove_with(&old, TreeType::NormalTree, grove_version); - let root_hash = db.root_hash(None, grove_version).unwrap().unwrap(); - let error = db - .apply_batch_with_element_flags_update( + assert_refused( + db.apply_partial_batch_with_element_flags_update( vec![write_op(&new)], None, settling_flags_update, split_removal_bytes, + |_cost, _leftover_operations| Ok(vec![]), None, grove_version, ) .unwrap() - .expect_err("expected the settling answer to be refused"); - assert!( - error.to_string().contains("settle_owner_changes"), - "unexpected error: {error}" - ); - assert_eq!( - db.root_hash(None, grove_version).unwrap().unwrap(), - root_hash + .expect_err("expected the settling answer to be refused"), ); } diff --git a/merk/src/tree/just_in_time_value_update/mod.rs b/merk/src/tree/just_in_time_value_update/mod.rs index b71cf458c..d77c7ca97 100644 --- a/merk/src/tree/just_in_time_value_update/mod.rs +++ b/merk/src/tree/just_in_time_value_update/mod.rs @@ -35,7 +35,7 @@ use crate::{ Error, }; -/// The put whose final bytes [`TreeNode::provided_value_hash_put_final_value`] +/// The put whose final bytes [`TreeNode::predict_put_final_value`] /// predicts: how it installs the new value on the stored node, which decides /// the value-defined cost the just-in-time value update measures with. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -65,7 +65,7 @@ impl TreeNode { /// writes, so a caller that must commit to the final bytes BEFORE the /// apply — a reference holding its target's hash — can compute them. #[allow(clippy::too_many_arguments)] - pub fn provided_value_hash_put_final_value( + pub fn predict_put_final_value( key: Vec, old_value: Vec, old_value_defined_cost: Option, @@ -309,7 +309,7 @@ mod tests { /// The detached prediction stores exactly what a real provided-hash put /// over a stored value stores, for a growing and a shrinking write. #[test] - fn provided_value_hash_put_final_value_matches_the_apply() { + fn predicted_provided_value_hash_put_matches_the_apply() { let grove_version = GroveVersion::latest(); let item = |value: &[u8], flags: u8| { Element::ItemWithBackwardsReferences( @@ -332,7 +332,7 @@ mod tests { ); merk.commit(grove_version); - let predicted = TreeNode::provided_value_hash_put_final_value( + let predicted = TreeNode::predict_put_final_value( b"key".to_vec(), old.clone(), None, From 49a9a1068c10ae88d3eb1874347ed61a0addd577 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Tue, 6 Oct 2026 07:12:56 +0200 Subject: [PATCH 05/10] fix(batch): fourth review fixes for settling storage owner changes - A target's prediction is dropped when the target is recorded as written: a reference to a written target hashes the bytes it stores and never reads the prediction again, and the continuation that unmarks a target clears every prediction anyway. - The estimate-coverage test applies each settling write (insert or replace, replace, patch, trusted refresh, and their DontCheck forms) on its own fixture and compares its estimates with that apply, instead of with an InsertOrReplace applied once. - The GROVE_V4 snapshot lists `apply_batch.same_batch_reference_target_prediction` and `merk_versions.tree.just_in_time_value_update`, and the latter has its own slot comment. - A merk test pins that a Changed round rewriting a sum item with another value-defined cost, restored and then answered Unchanged, is measured as an immediate Unchanged, with the item's own cost. Co-Authored-By: Claude Opus 5.5 --- grovedb-version/src/version/v4.rs | 31 ++++++- grovedb/src/batch/mod.rs | 8 +- .../batch/settle_owner_change_cost_tests.rs | 8 +- .../src/tree/just_in_time_value_update/mod.rs | 92 +++++++++++++++++++ 4 files changed, 132 insertions(+), 7 deletions(-) diff --git a/grovedb-version/src/version/v4.rs b/grovedb-version/src/version/v4.rs index 125cbe5fc..1c21c87ff 100644 --- a/grovedb-version/src/version/v4.rs +++ b/grovedb-version/src/version/v4.rs @@ -307,6 +307,29 @@ //! Identity-owned sections are unaffected; Drive accounts for the default //! section as system removals, separately from identity fee refunds. //! +//! - `apply_batch.same_batch_reference_target_prediction: 1` — a reference +//! written in the same batch as an update of its target commits to the +//! bytes the target stores. A target the batch has already written is +//! hashed from those bytes; one still to be written is predicted once, +//! through Merk's own just-in-time value update for the put the apply +//! performs. V1..V3 assume a sum item keeps its stored flags, so when the +//! flags update merges them (an owned item updated in a later epoch, an +//! unowned one gaining an owner) the reference commits to bytes never +//! stored and the grove stops verifying; they also refuse a sum item +//! replaced by an item, or the reverse, that applies on its own. Gated +//! because it moves a committed root and flips a rejected/accepted +//! outcome. +//! +//! - `merk_versions.tree.just_in_time_value_update: 1` — a replacement +//! whose flags update answers `Unchanged` is charged for the bytes it +//! stores. V1..V3 keep the measurement taken with the old value's flags, +//! so a replacement whose own flags differ in length fails its commit +//! with a storage cost mismatch. A `Changed` round that restores the +//! put's value also restores its value-defined cost, so a sum item or tree +//! measured again is measured with the cost of the bytes it holds. Gated +//! because it changes tracked costs and flips a failed commit into a +//! successful one. +//! //! Note that `GroveVersion::latest()` resolves to this version, so anything //! defaulting to "latest" — tests, benchmarks, tools — exercises every gate //! listed above rather than V3 behaviour. @@ -677,10 +700,14 @@ pub const GROVE_V4: GroveVersion = GroveVersion { // proof envelope. prove_count_offset_on_range: 0, }, - // Bumped 0 -> 1: ordinary (Item / Reference) replacements of a - // specialized value are charged from their own bytes (issue #908). tree: MerkTreeVersions { + // Bumped 0 -> 1: ordinary (Item / Reference) replacements of a + // specialized value are charged from their own bytes (issue #908). put_value: 1, + // Bumped 0 -> 1: a replacement whose flags update answers + // `Unchanged` is measured from the bytes it stores, and a sum + // item or tree measured again after its value was restored is + // measured with that value's own value-defined cost. just_in_time_value_update: 1, }, }, diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 618be1530..969e21f53 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -5098,7 +5098,13 @@ where .insert(path.to_vec(), per_axis); } - self.written_qualified_paths.extend(written_qualified_paths); + for qualified_path in written_qualified_paths { + // A reference to a written target hashes the bytes it stores + // and never reads its prediction again, so the prediction can + // go. + self.predicted_reference_targets.remove(&qualified_path); + self.written_qualified_paths.insert(qualified_path); + } let merk = self.merks.get_mut(path).expect("the Merk is cached"); merk.root_hash_key_and_aggregate_data() .add_cost(cost) diff --git a/grovedb/src/batch/settle_owner_change_cost_tests.rs b/grovedb/src/batch/settle_owner_change_cost_tests.rs index 6659df585..2bf198f21 100644 --- a/grovedb/src/batch/settle_owner_change_cost_tests.rs +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -1052,15 +1052,15 @@ mod tests { /// With the option, the average-case and worst-case estimates of every /// write that may settle an owner change are never below the settled - /// apply. + /// apply of that same write. #[test] fn estimates_with_the_option_cover_a_settled_owner_change() { let grove_version = GroveVersion::latest(); for (old, new, tree_type) in estimate_cases() { - let db = grove_with(&old, tree_type, grove_version); - let applied = apply(&db, vec![write_op(&new)], Mode::Settling, grove_version) - .expect("expected the settling update to apply"); for (label, op) in settling_writes(&new) { + let db = grove_with(&old, tree_type, grove_version); + let applied = apply(&db, vec![op.clone()], Mode::Settling, grove_version) + .unwrap_or_else(|e| panic!("expected {label} of {new:?} to apply: {e}")); for average_case_tree_type in [Some(tree_type), None] { let settling = estimate( vec![op.clone()], diff --git a/merk/src/tree/just_in_time_value_update/mod.rs b/merk/src/tree/just_in_time_value_update/mod.rs index d77c7ca97..161a00850 100644 --- a/merk/src/tree/just_in_time_value_update/mod.rs +++ b/merk/src/tree/just_in_time_value_update/mod.rs @@ -446,4 +446,96 @@ mod tests { assert_eq!(node.old_value, Some(new)); } } + + /// A `Changed` round whose rewrite changes the value-defined cost, which + /// the update then restores with the value, followed by `Unchanged`, is + /// measured exactly as an immediate `Unchanged`: the sum item it stores + /// is measured with its own value-defined cost, not the rewrite's. + #[test] + fn a_changed_round_then_unchanged_measures_the_value_it_stores() { + use crate::element::insert::specialized_put_cost; + + let grove_version = GroveVersion::latest(); + let key = b"key".to_vec(); + let sum_item = + |sum: i64, flags: Vec| Element::new_sum_item_with_flags(sum, Some(flags)); + let old = sum_item(5, vec![1]).serialize(grove_version).unwrap(); + let new_element = sum_item(7, vec![2]); + let new = new_element.serialize(grove_version).unwrap(); + let new_cost = specialized_put_cost(&new_element, grove_version) + .unwrap() + .expect("a sum item has a specialized cost"); + let rewritten_element = sum_item(7, vec![2; 9]); + let rewritten = rewritten_element.serialize(grove_version).unwrap(); + let rewritten_cost = specialized_put_cost(&rewritten_element, grove_version) + .unwrap() + .expect("a sum item has a specialized cost"); + assert_ne!(new_cost, rewritten_cost); + + let putting_new = || { + let kv = KV::from_fields( + key.clone(), + old.clone(), + NULL_HASH, + NULL_HASH, + BasicMerkNode, + ); + let mut node = TreeNode::new_with_tree_inner(TreeNodeInner { + left: None, + right: None, + kv, + }); + node.inner.kv = node.inner.kv.put_value_with_fixed_cost_no_update_of_hashes( + new.clone(), + ValueDefinedCostType::SpecializedValueDefinedCost(new_cost), + ); + node + }; + + let mut at_once = putting_new(); + at_once + .just_in_time_tree_node_value_update( + &old_cost, + &no_temp_value, + &mut |_cost: &StorageCost, _old: &Vec, _new: &mut Vec| { + Ok((ElementFlagsUpdate::Unchanged, None)) + }, + &mut basic_removal, + grove_version, + ) + .expect("expected the update to apply"); + + let mut rounds = 0; + let mut retried = putting_new(); + retried + .just_in_time_tree_node_value_update( + &old_cost, + &no_temp_value, + &mut |_cost: &StorageCost, _old: &Vec, value: &mut Vec| { + rounds += 1; + if rounds == 1 { + *value = rewritten.clone(); + Ok(( + ElementFlagsUpdate::Changed, + Some(ValueDefinedCostType::SpecializedValueDefinedCost( + rewritten_cost, + )), + )) + } else { + Ok((ElementFlagsUpdate::Unchanged, None)) + } + }, + &mut basic_removal, + grove_version, + ) + .expect("expected the update to apply"); + + assert_eq!(rounds, 2, "the resized rewrite must force a second round"); + assert_eq!(retried.inner.kv.value, new); + assert_eq!( + retried.inner.kv.value_defined_cost, + Some(ValueDefinedCostType::SpecializedValueDefinedCost(new_cost)) + ); + assert_eq!(retried.known_storage_cost, at_once.known_storage_cost); + } } From 8c050b2d9e08d09d51ab13e865173bd29a490b43 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Tue, 6 Oct 2026 08:11:54 +0200 Subject: [PATCH 06/10] fix(batch): skip the settled-tree lookup off the settling path The average-case estimator cloned and hashed an `InsertTreeWithRootHash`'s written layer path for every such op, though only a settling estimate of a flagged tree reads the layer's tree type: `raise_tree` returns the base estimate otherwise. The lookup now runs only with `settle_owner_changes` and flags present; every estimate is unchanged (the option-off figures stay pinned). A new regression covers a partial batch whose add-on rewrites the item its initial segment updated, together with the deeper reference to it, which resolves first. It must predict the second write instead of reading the first one's stored bytes, under the merging and the settling update (including an owner change); the item ends as two sequential batches leave it and every reference verifies. It fails if the continuation stops unmarking the targets it writes again. Co-Authored-By: Claude Opus 5.5 --- .../estimated_costs/average_case_costs.rs | 10 ++- .../same_batch_reference_prediction_tests.rs | 68 +++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/grovedb/src/batch/estimated_costs/average_case_costs.rs b/grovedb/src/batch/estimated_costs/average_case_costs.rs index 30b9c4bf2..39b8b37d0 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -1213,9 +1213,13 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { &mut cost, path, &key, tree_type, ); } - // The layer an `InsertTreeWithRootHash` writes, whose declared - // tree type sizes the tree when it settles an owner change. - let written_tree_type = if let GroveOp::InsertTreeWithRootHash { .. } = &op { + // The layer a flagged `InsertTreeWithRootHash` writes, whose + // declared tree type sizes the tree when it settles an owner + // change. Only a settling estimate reads it, so the lookup is + // skipped otherwise. + let written_tree_type = if batch_apply_options.settle_owner_changes + && let GroveOp::InsertTreeWithRootHash { flags: Some(_), .. } = &op + { let mut written_layer = path.clone(); written_layer.push(key.clone()); self.paths.get(&written_layer).map(|layer| layer.tree_type) diff --git a/grovedb/src/batch/same_batch_reference_prediction_tests.rs b/grovedb/src/batch/same_batch_reference_prediction_tests.rs index 03541c3ad..10c4dac43 100644 --- a/grovedb/src/batch/same_batch_reference_prediction_tests.rs +++ b/grovedb/src/batch/same_batch_reference_prediction_tests.rs @@ -465,6 +465,74 @@ mod tests { } } + /// A partial batch whose initial segment updates a flagged item and + /// whose add-on rewrites that item together with the deeper reference to + /// it. The reference sits deeper, so the add-on resolves it before it + /// writes the item again: it has to predict that second write rather + /// than read the bytes the initial segment stored. The item ends exactly + /// as the two updates applied as separate batches leave it, and every + /// reference verifies, with the merging update and with the settling one, + /// including a second update that changes the owner. + #[test] + fn an_add_on_rewriting_an_initial_segment_target_predicts_its_new_bytes() { + let grove_version = GroveVersion::latest(); + let item = |value: u8, len: usize, sum: i64, flags| { + Element::new_item_with_sum_item_with_flags(vec![value; len], sum, flags) + }; + let old = item(7, 20, 5, owned_flags(0, OLD_OWNER)); + let first = item(8, 21, 9, owned_flags(1, OLD_OWNER)); + let cases = [ + (Mode::Merging, item(9, 23, 11, owned_flags(2, OLD_OWNER))), + (Mode::Settling, item(9, 23, 11, owned_flags(2, OLD_OWNER))), + (Mode::Settling, item(9, 23, 11, owned_flags(2, NEW_OWNER))), + ]; + for (mode, second) in cases { + for reference in [refresh_op(true), refresh_op(false)] { + let label = format!("{mode:?}, second {second:?}, {reference:?}"); + + let sequential = grove_with(&old, TreeType::SumTree, true, grove_version); + apply(&sequential, vec![write_op(&first)], mode, grove_version) + .unwrap_or_else(|e| panic!("{label}: first batch: {e}")); + apply( + &sequential, + vec![write_op(&second), reference.clone()], + mode, + grove_version, + ) + .unwrap_or_else(|e| panic!("{label}: second batch: {e}")); + + let partial = grove_with(&old, TreeType::SumTree, true, grove_version); + partial + .apply_partial_batch_with_element_flags_update( + vec![write_op(&first)], + Some(options(mode)), + flags_update(mode), + split_removal_bytes, + |_cost, _leftover_operations| { + Ok(vec![write_op(&second), reference.clone()]) + }, + None, + grove_version, + ) + .cost_as_result() + .unwrap_or_else(|e| panic!("{label}: partial batch: {e}")); + + assert_eq!(issues(&partial, grove_version), 0, "{label}"); + let stored = |db: &TempGroveDb| { + db.get([b"tree".as_slice()].as_ref(), KEY, None, grove_version) + .unwrap() + .expect("expected the item") + }; + assert_eq!(stored(&partial), stored(&sequential), "{label}"); + assert_eq!( + stored(&partial).get_flags(), + stored(&sequential).get_flags(), + "{label}" + ); + } + } + } + /// Replacements whose new flags stand as written (the flags update /// answers `Unchanged`) but are not the length of the old ones. fn unchanged_flags_of_another_length() -> Vec<(&'static str, Element, Element, Option)> { From 5695f6620b152cf345f7dd1e5e49afff938fbd35 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Tue, 6 Oct 2026 09:51:57 +0200 Subject: [PATCH 07/10] fix(batch): refuse settling a write that keeps the stored flags An untrusted RefreshReference writes the stored reference's flags back, and a tree's root update rewrites the stored tree element, so neither brings an owner of its own and the estimators never charge them for a settlement. A flags callback could still answer SettleOwnerChange for them, for instance after rewriting the flags to another owner of the same length, and the apply would then record a whole insertion the estimate did not cover. batch_flags_update now refuses SettleOwnerChange on a write whose new flags are the stored ones, as it refuses one without the option, with Error::InvalidBatchOperation. The settle refusal records its reason so the batch reports the right one. Two estimator and tracking trims: - The average-case estimator measures the carried-referrer entry size only when the estimate settles. - Written targets are recorded only for the ops a same-batch reference predicts (a flagged Item, SumItem or ItemWithSumItem written by an insert-or-replace, replace or patch), through one predicate the lookup also asserts. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 +- .../estimated_costs/average_case_costs.rs | 13 +- .../batch/estimated_costs/worst_case_costs.rs | 5 +- .../src/batch/just_in_time_value_update.rs | 47 +++++--- grovedb/src/batch/mod.rs | 39 +++++- .../batch/settle_owner_change_cost_tests.rs | 112 ++++++++++++++++++ 6 files changed, 193 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 961f06b05..4269c6f2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,7 +62,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 keep the owner, or that have no owner on either side, are unchanged. The flags callback can now answer an `ElementFlagsUpdate` (`Unchanged`, `Changed`, `SettleOwnerChange`, new in `grovedb_costs`) in place of a - `bool`; a batch refuses `SettleOwnerChange` unless the option is set, with + `bool`; a batch refuses `SettleOwnerChange` unless the option is set, and + on a write that keeps the stored flags (an untrusted `RefreshReference`, or + a tree's root update), which brings no owner of its own, with `Error::InvalidBatchOperation`. With the option, the average-case and worst-case batch estimates charge every `InsertOrReplace`, `Replace`, `Patch` and trusted `RefreshReference` of an element with flags, and the diff --git a/grovedb/src/batch/estimated_costs/average_case_costs.rs b/grovedb/src/batch/estimated_costs/average_case_costs.rs index 39b8b37d0..bc209d27e 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -110,12 +110,18 @@ impl GroveOp { let in_tree_type = layer_element_estimates.tree_type; // A backward-references element is charged the typical referrer // shape, each referrer at the entry of one at this op's own position. + // Only a settling estimate reads that entry size, so it is measured + // only then. let settling = super::SettledOwnerChange::new( batch_apply_options, key, in_tree_type.inner_node_type(), super::CarriedReferrers::Average { - entry_bytes: super::backward_reference_entry_bound(path, key), + entry_bytes: if batch_apply_options.settle_owner_changes { + super::backward_reference_entry_bound(path, key) + } else { + 0 + }, }, grove_version, ); @@ -375,8 +381,9 @@ impl GroveOp { propagate_if_input(), grove_version, ); - // An untrusted refresh writes the stored flags back, so only - // a trusted one can change the owner. + // An untrusted refresh writes the stored flags back, and the + // batch refuses to settle a write that keeps the stored + // flags, so only a trusted one can change the owner. if mode.is_trusted() { settling.raise(replace_cost, &element) } else { diff --git a/grovedb/src/batch/estimated_costs/worst_case_costs.rs b/grovedb/src/batch/estimated_costs/worst_case_costs.rs index e512c5e6d..d4c0337b8 100644 --- a/grovedb/src/batch/estimated_costs/worst_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/worst_case_costs.rs @@ -339,8 +339,9 @@ impl GroveOp { propagate_if_input(), grove_version, ); - // An untrusted refresh writes the stored flags back, so only - // a trusted one can change the owner. + // An untrusted refresh writes the stored flags back, and the + // batch refuses to settle a write that keeps the stored + // flags, so only a trusted one can change the owner. if mode.is_trusted() { settling.raise(replace_cost, &element) } else { diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index 026c7de10..bf0a5c9b0 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -41,11 +41,21 @@ pub(crate) const SETTLE_OWNER_CHANGES_NOT_SET: &str = "the flags update settled an owner change, but the batch options do not set \ settle_owner_changes"; +/// Why a batch whose flags update settled an owner change was refused: the +/// write keeps the stored flags (an untrusted reference refresh, or a tree's +/// root update), so it brings no owner of its own to change to, and the +/// estimators do not charge it for a settlement. +pub(crate) const SETTLED_A_WRITE_KEEPING_ITS_FLAGS: &str = + "the flags update settled an owner change on a write that keeps the stored flags"; + /// The caller's flags-update callback as a batch runs it: its answer as an -/// [`ElementFlagsUpdate`] (a `bool` answer is `Changed` or `Unchanged`), and -/// `SettleOwnerChange` refused unless the batch's options set -/// `settle_owner_changes`. A refusal is recorded in `refused`, so the batch -/// reports it with [`settle_refusal_error`] whatever error it surfaces as. +/// [`ElementFlagsUpdate`] (a `bool` answer is `Changed` or `Unchanged`). +/// `SettleOwnerChange` is refused unless the batch's options set +/// `settle_owner_changes`, and refused on a write that keeps the stored +/// flags, whatever the callback does with them: only a write that brings +/// flags of its own can change the owner. A refusal records its reason in +/// `refused`, so the batch reports it with [`settle_refusal_error`] whatever +/// error it surfaces as. pub(crate) fn batch_flags_update( mut flags_update: impl FnMut( &StorageCost, @@ -53,7 +63,7 @@ pub(crate) fn batch_flags_update( &mut ElementFlags, ) -> Result, settle_owner_changes: bool, - refused: &Cell, + refused: &Cell>, ) -> impl FnMut(&StorageCost, Option, &mut ElementFlags) -> Result where U: Into, @@ -61,12 +71,20 @@ where move |storage_cost: &StorageCost, old_flags: Option, new_flags: &mut ElementFlags| { + let keeps_stored_flags = old_flags.as_deref() == Some(new_flags.as_slice()); let update = flags_update(storage_cost, old_flags, new_flags)?.into(); - if update == ElementFlagsUpdate::SettleOwnerChange && !settle_owner_changes { - refused.set(true); - return Err(Error::JustInTimeElementFlagsClientError( - SETTLE_OWNER_CHANGES_NOT_SET.to_owned(), - )); + if update == ElementFlagsUpdate::SettleOwnerChange { + let refusal = if !settle_owner_changes { + Some(SETTLE_OWNER_CHANGES_NOT_SET) + } else if keeps_stored_flags { + Some(SETTLED_A_WRITE_KEEPING_ITS_FLAGS) + } else { + None + }; + if let Some(reason) = refusal { + refused.set(Some(reason)); + return Err(Error::JustInTimeElementFlagsClientError(reason.to_owned())); + } } Ok(update) } @@ -76,11 +94,10 @@ where /// a settled owner change, as the invalid batch it is, when `refused` says /// one happened (the Merk and prediction layers it surfaced through report /// it as client or data corruption), and `error` otherwise. -pub(crate) fn settle_refusal_error(error: Error, refused: &Cell) -> Error { - if refused.get() { - Error::InvalidBatchOperation(SETTLE_OWNER_CHANGES_NOT_SET) - } else { - error +pub(crate) fn settle_refusal_error(error: Error, refused: &Cell>) -> Error { + match refused.get() { + Some(reason) => Error::InvalidBatchOperation(reason), + None => error, } } diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 969e21f53..4632f1272 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -1987,6 +1987,28 @@ struct TreeCacheMerkByPath { predicted_reference_targets: HashMap>, Vec>, } +/// Whether a same-batch reference to the target `op` writes predicts that +/// target: a flagged `Item`, `SumItem` or `ItemWithSumItem` (through any +/// wrapper) written by an insert-or-replace, a replace or a patch. Such a +/// reference reads whether the pass has already written the target, so only +/// these targets are recorded in `written_qualified_paths`. +fn writes_a_predicted_reference_target(op: &GroveOp) -> bool { + match op { + GroveOp::InsertOrReplace { element } + | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } + | GroveOp::Replace { element } + | GroveOp::ReplaceDontCheckForBackwardsReferences { element } + | GroveOp::Patch { element, .. } + | GroveOp::PatchDontCheckForBackwardsReferences { element, .. } => { + matches!( + element.underlying(), + Element::Item(..) | Element::SumItem(..) | Element::ItemWithSumItem(..) + ) && element.get_flags().is_some() + } + _ => false, + } +} + impl fmt::Debug for TreeCacheMerkByPath { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("TreeCacheMerkByPath").finish() @@ -2876,6 +2898,9 @@ where if let Some((old_serialized_element, is_in_sum_tree)) = serialized_element_result { + // Recorded for exactly the ops this arm + // handles (`writes_a_predicted_reference_target`). + debug_assert!(writes_a_predicted_reference_target(op)); let target_written = self.written_qualified_paths.contains(qualified_path); let value_hash = cost_return_on_error!( @@ -3246,13 +3271,15 @@ where let p = path.to_path(); let path = &p; self.unused_new_merks.remove(path); - // The targets this path writes, recorded as written once it has - // applied them (only read by the version 1 target prediction). + // The targets this path writes that a same-batch reference may + // predict, recorded as written once it has applied them (only read by + // the version 1 target prediction). let written_qualified_paths: Vec>> = if self.tracks_written_targets(grove_version) { ops_at_path_by_key - .keys() - .map(|key_info| { + .iter() + .filter(|(_, op)| writes_a_predicted_reference_target(op)) + .map(|(key_info, _)| { let mut qualified_path = path.to_vec(); qualified_path.push(key_info.get_key_clone()); qualified_path @@ -6863,7 +6890,7 @@ impl GroveDb { transaction: TransactionArg, grove_version: &GroveVersion, ) -> CostResult<(), Error> { - let settle_refused = Cell::new(false); + let settle_refused = Cell::new(None); let update_element_flags_function = just_in_time_value_update::batch_flags_update( update_element_flags_function, BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), @@ -7397,7 +7424,7 @@ impl GroveDb { transaction: TransactionArg, grove_version: &GroveVersion, ) -> CostResult<(), Error> { - let settle_refused = Cell::new(false); + let settle_refused = Cell::new(None); let update_element_flags_function = just_in_time_value_update::batch_flags_update( update_element_flags_function, BatchApplyOptions::settle_owner_changes_in(&batch_apply_options), diff --git a/grovedb/src/batch/settle_owner_change_cost_tests.rs b/grovedb/src/batch/settle_owner_change_cost_tests.rs index 2bf198f21..ef2edbbe7 100644 --- a/grovedb/src/batch/settle_owner_change_cost_tests.rs +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -823,6 +823,118 @@ mod tests { ); } + /// A write that keeps the stored flags brings no owner of its own, so a + /// settling answer for it is refused, whatever the callback does with the + /// flags: an untrusted refresh, which writes the stored reference's flags + /// back, even when the callback rewrites them to another owner of the + /// same length, and a flagged tree's root update when a write under it + /// propagates. The estimators charge neither a settlement, so neither may + /// settle. A write that brings flags of its own still settles. + #[test] + fn a_write_that_keeps_the_stored_flags_cannot_settle() { + use grovedb_costs::storage_cost::transition::ElementFlagsUpdate; + + let grove_version = GroveVersion::latest(); + let assert_refused = |error: Error| { + assert!( + matches!(error, Error::InvalidBatchOperation(reason) if reason.contains("keeps the stored flags")), + "unexpected error: {error}" + ); + }; + let rewriting_to_a_new_owner = |_cost: &grovedb_costs::storage_cost::StorageCost, + _old: Option>, + new: &mut Vec| { + *new = owned_flags(0, NEW_OWNER).expect("owned flags"); + Ok(ElementFlagsUpdate::SettleOwnerChange) + }; + let always_settling = + |_cost: &grovedb_costs::storage_cost::StorageCost, + _old: Option>, + _new: &mut Vec| { Ok(ElementFlagsUpdate::SettleOwnerChange) }; + assert_eq!( + owned_flags(0, OLD_OWNER).map(|flags| flags.len()), + owned_flags(0, NEW_OWNER).map(|flags| flags.len()) + ); + + // An untrusted refresh of a reference owned by the old owner. + let old = Element::new_reference_with_flags(target_path(), owned_flags(0, OLD_OWNER)); + let db = grove_with(&old, TreeType::NormalTree, grove_version); + let root_hash = db.root_hash(None, grove_version).unwrap().unwrap(); + assert_refused( + db.apply_batch_with_element_flags_update( + vec![QualifiedGroveDbOp::refresh_reference_op( + vec![b"tree".to_vec()], + KEY.to_vec(), + target_path(), + None, + owned_flags(0, NEW_OWNER), + false, + false, + )], + Some(options(Mode::Settling)), + rewriting_to_a_new_owner, + split_removal_bytes, + None, + grove_version, + ) + .unwrap() + .expect_err("an untrusted refresh must not settle"), + ); + assert_eq!( + db.root_hash(None, grove_version).unwrap().unwrap(), + root_hash + ); + + // A flagged tree whose root a write under it updates. + let db = make_empty_grovedb(); + db.insert( + EMPTY_PATH, + b"flagged", + Element::empty_tree_with_flags(owned_flags(0, OLD_OWNER)), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the flagged tree"); + let root_hash = db.root_hash(None, grove_version).unwrap().unwrap(); + assert_refused( + db.apply_batch_with_element_flags_update( + vec![QualifiedGroveDbOp::insert_or_replace_op( + vec![b"flagged".to_vec()], + b"child".to_vec(), + Element::new_item(vec![1]), + )], + Some(options(Mode::Settling)), + always_settling, + split_removal_bytes, + None, + grove_version, + ) + .unwrap() + .expect_err("a tree's root update must not settle"), + ); + assert_eq!( + db.root_hash(None, grove_version).unwrap().unwrap(), + root_hash + ); + + // A write bringing flags of its own settles. + let old = Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)); + let new = Element::new_item_with_flags(vec![8; 20], owned_flags(0, NEW_OWNER)); + let db = grove_with(&old, TreeType::NormalTree, grove_version); + db.apply_batch_with_element_flags_update( + vec![write_op(&new)], + Some(options(Mode::Settling)), + always_settling, + split_removal_bytes, + None, + grove_version, + ) + .unwrap() + .expect("a write bringing its own flags settles"); + } + /// A reference written in the same batch commits to the bytes the /// settled element stores: its new flags, not the old owner's, on the /// latest grove version and on grove v1..v3 alike. From c50e2cc33fb62e98412909c0d707f7f31a66be5f Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Tue, 6 Oct 2026 10:16:38 +0200 Subject: [PATCH 08/10] perf(batch): skip settle-only work when it cannot matter - batch_flags_update compares the new flags with the stored ones only with settle_owner_changes: without the option every settling answer is refused anyway. The comparison still runs before the callback, which may rewrite the flags. - The same-batch reference prediction decodes the stored element for its value-defined cost only for an ordinary put: a provided-value-hash put clears that cost and a specialized-cost put replaces it. Neither changes a stored byte, hash or recorded cost. Co-Authored-By: Claude Opus 5.5 --- grovedb/src/batch/just_in_time_value_update.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index bf0a5c9b0..aadde674b 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -71,7 +71,10 @@ where move |storage_cost: &StorageCost, old_flags: Option, new_flags: &mut ElementFlags| { - let keeps_stored_flags = old_flags.as_deref() == Some(new_flags.as_slice()); + // Compared before the callback, which may rewrite the flags. Without + // the option any settling answer is refused anyway. + let keeps_stored_flags = + settle_owner_changes && old_flags.as_deref() == Some(new_flags.as_slice()); let update = flags_update(storage_cost, old_flags, new_flags)?.into(); if update == ElementFlagsUpdate::SettleOwnerChange { let refusal = if !settle_owner_changes { @@ -331,8 +334,15 @@ where u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { - let old_value_defined_cost = - Element::value_defined_cost_for_serialized_value(&old_serialized, grove_version); + // Only an ordinary put can keep the stored node's value-defined cost: a + // provided-value-hash put clears it and a specialized-cost put replaces + // it, so the stored element is decoded for it only then. + let old_value_defined_cost = match put { + PredictedPut::Ordinary => { + Element::value_defined_cost_for_serialized_value(&old_serialized, grove_version) + } + PredictedPut::ProvidedValueHash | PredictedPut::SpecializedCost(_) => None, + }; TreeNode::predict_put_final_value( key.to_vec(), old_serialized, From 3bb713e268c1550bceed8a9faf399127cbfb64a6 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Tue, 6 Oct 2026 10:23:29 +0200 Subject: [PATCH 09/10] test(batch): pin the refused settlement of a root-level untrusted refresh An external review reproduced the untrusted-refresh settlement on 8c050b2d with a reference at the grove's root. 5695f662 refuses it; the regression now covers the root case alongside the subtree one. Co-Authored-By: Claude Opus 5.5 --- .../batch/settle_owner_change_cost_tests.rs | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/grovedb/src/batch/settle_owner_change_cost_tests.rs b/grovedb/src/batch/settle_owner_change_cost_tests.rs index ef2edbbe7..4058232cd 100644 --- a/grovedb/src/batch/settle_owner_change_cost_tests.rs +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -827,9 +827,10 @@ mod tests { /// settling answer for it is refused, whatever the callback does with the /// flags: an untrusted refresh, which writes the stored reference's flags /// back, even when the callback rewrites them to another owner of the - /// same length, and a flagged tree's root update when a write under it - /// propagates. The estimators charge neither a settlement, so neither may - /// settle. A write that brings flags of its own still settles. + /// same length (in a subtree and at the grove's root), and a flagged + /// tree's root update when a write under it propagates. The estimators + /// charge neither a settlement, so neither may settle. A write that + /// brings flags of its own still settles. #[test] fn a_write_that_keeps_the_stored_flags_cannot_settle() { use grovedb_costs::storage_cost::transition::ElementFlagsUpdate; @@ -885,6 +886,44 @@ mod tests { root_hash ); + // The same refresh of a reference at the grove's root. + let db = grove_with(&old, TreeType::NormalTree, grove_version); + db.insert( + EMPTY_PATH, + b"root reference", + Element::new_reference_with_flags(target_path(), owned_flags(0, OLD_OWNER)), + None, + None, + grove_version, + ) + .unwrap() + .expect("expected to insert the root reference"); + let root_hash = db.root_hash(None, grove_version).unwrap().unwrap(); + assert_refused( + db.apply_batch_with_element_flags_update( + vec![QualifiedGroveDbOp::refresh_reference_op( + vec![], + b"root reference".to_vec(), + target_path(), + None, + owned_flags(0, NEW_OWNER), + false, + false, + )], + Some(options(Mode::Settling)), + rewriting_to_a_new_owner, + split_removal_bytes, + None, + grove_version, + ) + .unwrap() + .expect_err("an untrusted refresh at the root must not settle"), + ); + assert_eq!( + db.root_hash(None, grove_version).unwrap().unwrap(), + root_hash + ); + // A flagged tree whose root a write under it updates. let db = make_empty_grovedb(); db.insert( From 814dfc765747cec0beb7a39e1cdf04d301ce9ac3 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Tue, 6 Oct 2026 11:42:30 +0200 Subject: [PATCH 10/10] perf(batch): defer reference-target work the prediction does not use - A same-batch reference to a flagged target serializes the target only where its bytes are hashed or predicted: the unflagged and unstored cases, the legacy v0 route (which serializes before decoding the stored element, as before), and the version 1 prediction on a cache miss. A written target or a cached prediction no longer pays for a payload-sized copy. - The prediction decodes the stored element's value-defined cost only for an ordinary put whose install keeps it, which merk now reports through `TreeNode::ordinary_put_keeps_value_defined_cost` (true for `merk_versions.tree.put_value` 0, false for 1, unknown versions refused). - CHANGELOG names the two new version-table fields as breaking and the values a hand-built table needs. No stored byte, hash or recorded cost changes. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 +++++ .../just_in_time_reference_update/mod.rs | 4 ++-- .../batch/just_in_time_reference_update/v1.rs | 4 +++- .../src/batch/just_in_time_value_update.rs | 15 ++++++++++----- grovedb/src/batch/mod.rs | 13 ++++++++----- merk/src/tree/put_value/mod.rs | 19 +++++++++++++++++++ 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4269c6f2a..39a344728 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **BREAKING**: `BatchApplyOptions` has a new public field, `settle_owner_changes` (`false` by default), so a struct literal that names every field without `..Default::default()` must add it. +- **BREAKING**: the version tables gain two required fields, + `GroveDBApplyBatchVersions::same_batch_reference_target_prediction` and + `MerkTreeVersions::just_in_time_value_update`, so code building those + structs by hand must add them: `0` for a table matching GROVE_V1..V3, which + keeps the released behaviour, and `1` for one matching GROVE_V4. - **BREAKING**: Merk's just-in-time value update callback (the `update_tree_value_based_on_costs` argument of `Merk::apply_with_costs_just_in_time_value_update`, `Merk::apply_unchecked`, diff --git a/grovedb/src/batch/just_in_time_reference_update/mod.rs b/grovedb/src/batch/just_in_time_reference_update/mod.rs index 1b643db31..83ad7e701 100644 --- a/grovedb/src/batch/just_in_time_reference_update/mod.rs +++ b/grovedb/src/batch/just_in_time_reference_update/mod.rs @@ -99,7 +99,6 @@ where pub(crate) fn process_old_element_flags( key: &[u8], qualified_path: &[Vec], - serialized: Vec, new_element: &Element, old_serialized_element: Vec, in_tree_type: TreeType, @@ -131,6 +130,8 @@ where ) { (0, false) => { let cost = OperationCost::default(); + let serialized = + cost_return_on_error_into_no_add!(cost, new_element.serialize(grove_version)); let old_element = cost_return_on_error_no_add!( cost, Element::deserialize(&old_serialized_element, grove_version).map_err(|e| { @@ -152,7 +153,6 @@ where (0, true) | (1, _) => v1::process_old_element_flags_v1( key, qualified_path, - serialized, new_element, old_serialized_element, in_tree_type, diff --git a/grovedb/src/batch/just_in_time_reference_update/v1.rs b/grovedb/src/batch/just_in_time_reference_update/v1.rs index 7584474dc..83a2de1c5 100644 --- a/grovedb/src/batch/just_in_time_reference_update/v1.rs +++ b/grovedb/src/batch/just_in_time_reference_update/v1.rs @@ -33,7 +33,6 @@ use crate::{ pub(super) fn process_old_element_flags_v1( key: &[u8], qualified_path: &[Vec], - serialized: Vec, new_element: &Element, old_serialized_element: Vec, in_tree_type: TreeType, @@ -64,6 +63,9 @@ where let val_hash = value_hash(stored_bytes).unwrap_add_cost(&mut cost); return Ok(val_hash).wrap_with_cost(cost); } + // Serialized only now: a written target or a cached prediction does not + // need the incoming bytes. + let serialized = cost_return_on_error_into_no_add!(cost, new_element.serialize(grove_version)); let put = match cost_return_on_error_no_add!( cost, specialized_put_cost(new_element, grove_version).map_err(Error::MerkError) diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index aadde674b..5eb77f805 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -334,14 +334,19 @@ where u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { - // Only an ordinary put can keep the stored node's value-defined cost: a - // provided-value-hash put clears it and a specialized-cost put replaces - // it, so the stored element is decoded for it only then. + // Only an ordinary put whose install keeps the stored node's + // value-defined cost (`merk_versions.tree.put_value` 0) reads it: version 1 + // clears it, a provided-value-hash put clears it and a specialized-cost + // put replaces it, so the stored element is decoded for it only then. let old_value_defined_cost = match put { - PredictedPut::Ordinary => { + PredictedPut::Ordinary + if TreeNode::ordinary_put_keeps_value_defined_cost(grove_version)? => + { Element::value_defined_cost_for_serialized_value(&old_serialized, grove_version) } - PredictedPut::ProvidedValueHash | PredictedPut::SpecializedCost(_) => None, + PredictedPut::Ordinary + | PredictedPut::ProvidedValueHash + | PredictedPut::SpecializedCost(_) => None, }; TreeNode::predict_put_final_value( key.to_vec(), diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 4632f1272..550611651 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -2874,12 +2874,12 @@ where // wrapper for hashing so the value hash matches storage. match element.underlying() { Element::Item(..) | Element::SumItem(..) | Element::ItemWithSumItem(..) => { - let serialized = cost_return_on_error_into_no_add!( - cost, - element.serialize(grove_version) - ); if element.get_flags().is_none() { // There are no storage flags, we can just hash new element + let serialized = cost_return_on_error_into_no_add!( + cost, + element.serialize(grove_version) + ); let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); Ok(val_hash).wrap_with_cost(cost) } else { @@ -2908,7 +2908,6 @@ where Self::process_old_element_flags( key, qualified_path, - serialized, element, old_serialized_element, is_in_sum_tree, @@ -2922,6 +2921,10 @@ where ); Ok(value_hash).wrap_with_cost(cost) } else { + let serialized = cost_return_on_error_into_no_add!( + cost, + element.serialize(grove_version) + ); let value_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); Ok(value_hash).wrap_with_cost(cost) diff --git a/merk/src/tree/put_value/mod.rs b/merk/src/tree/put_value/mod.rs index 198118c6c..a78b66140 100644 --- a/merk/src/tree/put_value/mod.rs +++ b/merk/src/tree/put_value/mod.rs @@ -183,6 +183,25 @@ impl TreeNode { )), } } + + /// Whether [`Self::install_ordinary_value`] keeps the value-defined cost + /// the node was loaded with (version 0) rather than clearing it (version + /// 1), so a prediction knows whether it needs the stored node's cost. + pub fn ordinary_put_keeps_value_defined_cost( + grove_version: &GroveVersion, + ) -> Result { + match grove_version.merk_versions.tree.put_value { + 0 => Ok(true), + 1 => Ok(false), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "ordinary_put_keeps_value_defined_cost".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )), + } + } } #[cfg(test)]