diff --git a/CHANGELOG.md b/CHANGELOG.md index 85b4c0de8..51b7a1ef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,8 +28,52 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 by hand). The proof envelope gains no variant; the `verify` feature now builds `grovedb-private-document-store` (without storage) for the store's root. +- 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, 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 + 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, every cost, estimate, stored element and hash is what it + was before. A batch with the option predicts a same-batch reference's + target as `GROVE_V4` does on every grove version. ### 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`, + `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`, + `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**: `Query` adds the public `offset: Option` field for branch-local committed-count-unit pagination. Exhaustive struct literals must supply the field or use a default update. Offset-bearing queries use 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..d4dc4c934 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,63 @@ -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 { + // 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 + ); + 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( cost: &StorageCost, old_flags: Option, @@ -12,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 })?; @@ -28,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() @@ -146,7 +227,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 +393,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-version/src/version/grovedb_versions.rs b/grovedb-version/src/version/grovedb_versions.rs index e424f620d..002adca04 100644 --- a/grovedb-version/src/version/grovedb_versions.rs +++ b/grovedb-version/src/version/grovedb_versions.rs @@ -288,6 +288,9 @@ pub struct GroveDBApplyBatchVersions { /// 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, /// Whether a batch may carry the same keyless append more than once /// (issue #1031). `QualifiedGroveDbOp::verify_consistency_of_operations` 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 cb0dae643..3c5ac9dcb 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 bc6239e69..eb0f095ca 100644 --- a/grovedb/src/batch/estimated_costs/average_case_costs.rs +++ b/grovedb/src/batch/estimated_costs/average_case_costs.rs @@ -45,10 +45,38 @@ 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, + None, + &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,9 +97,35 @@ 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's + // aggregate data names that type only from GROVE_V4, and a batch can + // settle on every grove version). + 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. + batch_apply_options: &BatchApplyOptions, grove_version: &GroveVersion, ) -> CostResult<(), Error> { 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: if batch_apply_options.settle_owner_changes { + super::backward_reference_entry_bound(path, key) + } else { + 0 + }, + }, + grove_version, + ); let propagate_if_input = || { if propagate { Some(layer_element_estimates) @@ -93,20 +147,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, + super::backward_reference_entry_bound(path, key), )) } // A backward-references ITEM write carries its own referrer @@ -213,23 +257,44 @@ impl GroveOp { not_summed, not_counted_or_summed, .. - } => GroveDb::average_case_merk_insert_tree( - key, + } => settling.raise_tree( + 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, + ), + // 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, - 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, + *non_counted, + *not_summed, + *not_counted_or_summed, ), GroveOp::InsertOrReplace { element } - | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } - | GroveOp::InsertWithKnownToNotAlreadyExist { element } => with_fan_out( + | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( + settling.raise( + 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, @@ -313,22 +378,33 @@ 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, 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 { + replace_cost + } } GroveOp::Replace { element } | GroveOp::ReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - GroveDb::average_case_merk_replace_element( - key, + settling.raise( + 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)), ), @@ -340,13 +416,16 @@ impl GroveOp { element, change_in_bytes, } => with_fan_out( - GroveDb::average_case_merk_patch_element( - key, + settling.raise( + 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)), ), @@ -982,7 +1061,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, @@ -1145,15 +1224,30 @@ impl TreeCache for AverageCaseTreeCacheKnownPaths { &mut cost, path, &key, tree_type, ); } + // 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) + } else { + None + }; 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, + 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 bc1fa4c9a..15fab7a64 100644 --- a/grovedb/src/batch/estimated_costs/mod.rs +++ b/grovedb/src/batch/estimated_costs/mod.rs @@ -41,6 +41,272 @@ 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; + +/// 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: +/// [`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) + * (REFERRER_ENTRY_SEGMENT_BYTES + + 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 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::{ + element::costs::ElementCostExtensions, + tree::kv::{ + ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, + KV, + }, + }; + + let raw_value_len = match element.value_defined_cost(grove_version) { + Some(LayeredValueDefinedCost(value_cost)) => { + return Ok(KV::layered_node_byte_cost_size_for_key_and_value_lengths( + key_len, value_cost, node_type, + )) + } + Some(SpecializedValueDefinedCost(value_cost)) => value_cost, + 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, + raw_value_len, + node_type, + )) +} + +/// 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)): +/// 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, + carried_referrers: CarriedReferrers, + grove_version: &'a grovedb_version::version::GroveVersion, +} + +#[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, + } + } + + /// `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), + } + } + value.wrap_with_cost(cost) + } + + /// [`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; + + 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) + } +} + // ── Backward-references fan-out estimation model ──────────────────────── // // Under `BatchApplyOptions::backwards_references` (GROVE_V4+), a @@ -628,3 +894,164 @@ 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 grovedb_version::version::GroveVersion; + + use crate::{ + batch::{BatchApplyOptions, KeyInfo}, + bidirectional_references::BackwardReference, + reference_path::ReferencePathType, + BackwardReferences, Element, + }; + + 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::SettledOwnerChange::new( + &options, + &key, + 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 + .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}" + ); + } + } + } + + /// 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 ee5c2919f..f729ba16f 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,6 +85,10 @@ impl GroveOp { // it declares `Check`. backwards_references: BackwardsReferences, propagate: 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 = || { @@ -67,6 +98,13 @@ impl GroveOp { None } }; + 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 @@ -80,20 +118,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 @@ -199,19 +227,40 @@ impl GroveOp { not_summed, not_counted_or_summed, .. - } => GroveDb::worst_case_merk_insert_tree( - key, + } => settling.raise_tree( + 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, + ), + // 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, - 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, + *non_counted, + *not_summed, + *not_counted_or_summed, ), GroveOp::InsertOrReplace { element } - | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } - | GroveOp::InsertWithKnownToNotAlreadyExist { element } => with_fan_out( + | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } => with_fan_out( + settling.raise( + 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, @@ -286,22 +335,33 @@ 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, 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 { + replace_cost + } } GroveOp::Replace { element } | GroveOp::ReplaceDontCheckForBackwardsReferences { element } => with_fan_out( - GroveDb::worst_case_merk_replace_element( - key, + settling.raise( + 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)), ), @@ -313,12 +373,15 @@ impl GroveOp { element, change_in_bytes: _, } => with_fan_out( - GroveDb::worst_case_merk_replace_element( - key, + settling.raise( + 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)), ), @@ -855,7 +918,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, @@ -905,13 +968,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, grove_version ) ); 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 f9b038916..cb8ca75f8 100644 --- a/grovedb/src/batch/just_in_time_reference_update/mod.rs +++ b/grovedb/src/batch/just_in_time_reference_update/mod.rs @@ -20,6 +20,9 @@ //! 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; @@ -28,7 +31,7 @@ use std::collections::HashMap; use grovedb_costs::{ cost_return_on_error_into_no_add, cost_return_on_error_no_add, - storage_cost::{removal::StorageRemovedBytes, StorageCost}, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; use grovedb_merk::{tree::value_hash, tree_type::TreeType, CryptoHash, Merk}; @@ -76,12 +79,13 @@ where /// 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(grove_version: &GroveVersion) -> bool { - grove_version - .grovedb_versions - .apply_batch - .same_batch_reference_target_prediction - != 0 + 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 @@ -100,6 +104,7 @@ where new_element: &Element, old_serialized_element: Vec, in_tree_type: TreeType, + settle_owner_changes: bool, target_written: bool, runs_value_update: bool, predicted_targets: &mut HashMap>, Vec>, @@ -108,19 +113,25 @@ 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, u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { - match grove_version - .grovedb_versions - .apply_batch - .same_batch_reference_target_prediction - { - 0 => { + match ( + grove_version + .grovedb_versions + .apply_batch + .same_batch_reference_target_prediction, + settle_owner_changes, + ) { + (0, false) => { let cost = OperationCost::default(); let serialized = cost_return_on_error_into_no_add!(cost, new_element.serialize(grove_version)); @@ -142,7 +153,7 @@ where grove_version, ) } - 1 => v1::process_old_element_flags_v1( + (0, true) | (1, _) => v1::process_old_element_flags_v1( key, qualified_path, new_element, @@ -155,7 +166,7 @@ where 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 f14ffc94d..501f5c2b2 100644 --- a/grovedb/src/batch/just_in_time_reference_update/v0.rs +++ b/grovedb/src/batch/just_in_time_reference_update/v0.rs @@ -4,6 +4,7 @@ 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, @@ -19,7 +20,8 @@ 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 gets the stored bytes. It measures /// and asks the flags update itself, over the target's bytes as the Merk @@ -39,7 +41,11 @@ pub(super) fn process_old_element_flags_v0( grove_version: &GroveVersion, ) -> CostResult where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -139,7 +145,7 @@ 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 { @@ -150,7 +156,7 @@ where } }) ); - 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); 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 dc39ae900..5bdc2884b 100644 --- a/grovedb/src/batch/just_in_time_reference_update/v1.rs +++ b/grovedb/src/batch/just_in_time_reference_update/v1.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use grovedb_costs::{ cost_return_on_error_into_no_add, cost_return_on_error_no_add, - storage_cost::{removal::StorageRemovedBytes, StorageCost}, + storage_cost::{removal::StorageRemovedBytes, transition::ElementFlagsUpdate, StorageCost}, CostResult, CostsExt, OperationCost, }; use grovedb_merk::{ @@ -18,7 +18,8 @@ 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. Once this pass /// has written the target, `old_serialized_element` (the target's bytes as @@ -46,7 +47,11 @@ pub(super) fn process_old_element_flags_v1( grove_version: &GroveVersion, ) -> CostResult 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/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index 015acf18b..ddb73f972 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -13,8 +13,11 @@ //! [`predict_put_final_bytes`] does the same for any put, which a //! same-batch reference to a pending item write commits to. +use std::cell::Cell; + use grovedb_costs::storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }; use grovedb_merk::{ @@ -34,6 +37,95 @@ 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"; + +/// 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`). +/// `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, + Option, + &mut ElementFlags, + ) -> Result, + settle_owner_changes: bool, + refused: &Cell>, +) -> impl FnMut(&StorageCost, Option, &mut ElementFlags) -> Result +where + U: Into, +{ + move |storage_cost: &StorageCost, + old_flags: Option, + new_flags: &mut ElementFlags| { + // 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 { + 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) + } +} + +/// 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 { + match refused.get() { + Some(reason) => Error::InvalidBatchOperation(reason), + None => error, + } +} + +/// 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, + 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], @@ -74,17 +166,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) @@ -95,16 +192,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 @@ -145,14 +242,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; @@ -162,12 +262,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)) } } } @@ -222,22 +325,30 @@ pub(crate) fn predict_put_final_bytes( grove_version: &GroveVersion, ) -> Result, MerkError> where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + G: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { - // The stored node as the apply loads it (its value-defined cost through - // the same function the apply passes Merk). Only an ordinary put can keep - // that cost, as `TreeNode::put_value` installs it at the grove version; a - // provided-value-hash put clears it and a specialized-cost put replaces it. + // 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(), @@ -287,7 +398,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 913b196d8..3faf24b06 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -36,6 +36,10 @@ mod refresh_reference_mode; #[cfg(test)] 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; @@ -47,6 +51,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}, @@ -65,6 +70,7 @@ use grovedb_costs::{ cost_return_on_error_no_add, storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }, CostResult, CostsExt, OperationCost, @@ -2124,6 +2130,11 @@ 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 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, /// Trees moved by the levels executed so far, for the post-apply /// storage copy. moved_subtrees: Vec, @@ -2502,7 +2513,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, @@ -2821,7 +2836,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, @@ -2951,7 +2970,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, @@ -3090,11 +3113,11 @@ where let target_written = self.written_qualified_paths.contains(qualified_path); // Only the version 1 prediction reads it. - let runs_value_update = - !Self::tracks_written_targets(grove_version) - || self.merks.get(reference_path).is_none_or(|merk| { - merk.replacement_runs_value_update(key) - }); + let runs_value_update = !self + .tracks_written_targets(grove_version) + || self.merks.get(reference_path).is_none_or(|merk| { + merk.replacement_runs_value_update(key) + }); let value_hash = cost_return_on_error!( &mut cost, Self::process_old_element_flags( @@ -3103,6 +3126,7 @@ where element, old_serialized_element, is_in_sum_tree, + self.settle_owner_changes, target_written, runs_value_update, &mut self.predicted_reference_targets, @@ -3395,7 +3419,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, @@ -3510,7 +3538,7 @@ where // 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) { + if self.tracks_written_targets(grove_version) { ops_at_path_by_key .iter() .filter(|(_, op)| writes_a_predicted_reference_target(op)) @@ -5663,7 +5691,11 @@ impl GroveDb { Error, > where - F: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + F: FnMut( + &StorageCost, + Option, + &mut ElementFlags, + ) -> Result, SR: FnMut( &mut ElementFlags, u32, @@ -6014,7 +6046,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, split_removed_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -6070,6 +6102,9 @@ impl GroveDb { cidx_overwrite_cleanup_paths: Default::default(), deleted_tree_actual_types: Default::default(), indexed_mirror_rekey_churn_bytes: 0, + settle_owner_changes: BatchApplyOptions::settle_owner_changes_in( + &batch_apply_options, + ), moved_subtrees: Vec::new(), moved_positions: Default::default(), kept_by_conditional_insert: HashSet::new(), @@ -6106,7 +6141,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, split_removed_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -7249,7 +7284,52 @@ impl GroveDb { /// - 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. - 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); + /// without it the batch fails with [`Error::InvalidBatchOperation`]. + pub fn apply_batch_with_element_flags_update>( + &self, + ops: Vec, + batch_apply_options: Option, + update_element_flags_function: impl FnMut( + &StorageCost, + 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(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), + &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, @@ -7257,7 +7337,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, mut split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -7764,7 +7844,55 @@ 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, + update_element_flags_function: impl FnMut( + &StorageCost, + 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(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), + &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, @@ -7772,7 +7900,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, mut split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -8549,7 +8677,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, @@ -8557,7 +8689,7 @@ impl GroveDb { &StorageCost, Option, &mut ElementFlags, - ) -> Result, + ) -> Result, split_removal_bytes_function: impl FnMut( &mut ElementFlags, u32, // key removed bytes @@ -8575,6 +8707,11 @@ impl GroveDb { .apply_batch .estimated_case_operations_for_batch ); + // 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() { @@ -8819,6 +8956,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 @@ -9424,6 +9562,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 @@ -9465,6 +9604,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 @@ -9498,6 +9638,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..de3e8b424 100644 --- a/grovedb/src/batch/options.rs +++ b/grovedb/src/batch/options.rs @@ -78,6 +78,36 @@ 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` 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. 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 + /// 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, } #[cfg(feature = "minimal")] @@ -89,12 +119,21 @@ impl Default for BatchApplyOptions { disable_operation_consistency_check: false, base_root_storage_is_free: true, batch_pause_height: None, + settle_owner_changes: false, } } } #[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 index 08e1b5d00..6a7600593 100644 --- a/grovedb/src/batch/same_batch_reference_prediction_tests.rs +++ b/grovedb/src/batch/same_batch_reference_prediction_tests.rs @@ -8,10 +8,7 @@ mod tests { use grovedb_costs::{ storage_cost::{ - removal::{ - StorageRemovedBytes, - StorageRemovedBytes::{BasicStorageRemoval, NoStorageRemoval}, - }, + removal::StorageRemovedBytes::{BasicStorageRemoval, NoStorageRemoval}, StorageCost, }, OperationCost, @@ -19,88 +16,22 @@ mod tests { use std::collections::BTreeMap; 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, + }, + BatchApplyOptions, QualifiedGroveDbOp, + }, reference_path::ReferencePathType, tests::{make_empty_grovedb, TempGroveDb}, - Element, ElementFlags, Error, GroveDb, PathQuery, + Element, Error, GroveDb, PathQuery, }; - 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()) - } - - /// Drive's flags update, merging the storage flags. - fn merging_flags_update( - cost: &StorageCost, - old_flags: Option, - new_flags: &mut ElementFlags, - ) -> Result { - StorageFlags::update_element_flags(cost, old_flags, new_flags) - .map_err(|e| Error::JustInTimeElementFlagsClientError(e.to_string())) - } - - /// Drive's removal callback, sectioning freed bytes by epoch and owner. - 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())) - } - - /// Applies `ops` with Drive's merging flags update. - fn apply( - db: &TempGroveDb, - ops: Vec, - grove_version: &GroveVersion, - ) -> Result { - db.apply_batch_with_element_flags_update( - ops, - Some(BatchApplyOptions::default()), - merging_flags_update, - split_removal_bytes, - None, - grove_version, - ) - .cost_as_result() - } - - /// A write of `element` at `[tree]/key1`. - 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`. - 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 target() -> ReferencePathType { ReferencePathType::AbsolutePathReference(vec![b"tree".to_vec(), KEY.to_vec()]) } @@ -310,12 +241,13 @@ mod tests { 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, grove_version)?; + apply(&db, ops, mode, grove_version)?; Ok(issues(&db, grove_version)) } @@ -323,20 +255,25 @@ mod tests { 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 (reference_label, reference_ops, with_reference, _) in reference_ops() { - let stale = stale_references( - &old, - &new, - tree_type, - reference_ops, - with_reference, - 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" - ); + for mode in [Mode::Merging, Mode::Settling] { + 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!( + stale, 0, + "{label} ({new:?}), {reference_label}, {mode:?}: a reference is stale" + ); + } } } } @@ -435,7 +372,13 @@ mod tests { Element::empty_sum_tree(), grove_version, ); - apply(&db, vec![write_op(&new), reference_op], grove_version).unwrap_or_else(|e| { + apply( + &db, + vec![write_op(&new), reference_op], + Mode::Merging, + grove_version, + ) + .unwrap_or_else(|e| { panic!("{label} in {tree_type:?}, {reference_label}: expected to apply: {e}") }); assert_eq!( @@ -515,8 +458,13 @@ mod tests { Element::new_item_with_sum_item_with_flags(vec![8; 23], 9, owned_flags(2, OLD_OWNER)); for (grove_version, verifies) in [(GroveVersion::latest(), true), (&GROVE_V3, false)] { let db = grove_with(&old, TreeType::SumTree, true, grove_version); - apply(&db, vec![write_op(&new), refresh_op(false)], grove_version) - .expect("expected to apply"); + apply( + &db, + vec![write_op(&new), refresh_op(false)], + Mode::Merging, + grove_version, + ) + .expect("expected to apply"); assert_eq!( reference_proof_verifies(&db, grove_version), verifies, @@ -534,49 +482,81 @@ mod tests { let mut grove_version = GroveVersion::latest().clone(); grove_version.merk_versions.tree.put_value = 0; for (label, old, new, tree_type) in flag_changing_updates() { - for (reference_label, reference_ops, with_reference, _) in reference_ops() { + for mode in [Mode::Merging, Mode::Settling] { + 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!( + stale, 0, + "{label} ({new:?}), {reference_label}, {mode:?}: a reference is stale" + ); + } + } + } + } + + /// 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; + for (label, old, new, tree_type) in flag_changing_updates() { + if !old.is_sum_item() { + continue; + } + for (reference_label, reference_ops, with_reference, resolving_first) in reference_ops() + { let stale = stale_references( &old, &new, tree_type, reference_ops, with_reference, - &grove_version, + Mode::Merging, + 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" + stale, resolving_first, + "{label} ({new:?}), {reference_label}: expected the legacy stale references" ); } } } - /// Grove v1..v3 keep the hand-written prediction, 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). + /// 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_keeps_the_legacy_sum_item_prediction() { + 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() { - if !old.is_sum_item() { - continue; - } - for (reference_label, reference_ops, with_reference, resolving_first) in reference_ops() - { + 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, resolving_first, - "{label} ({new:?}), {reference_label}: expected the legacy stale references" + stale, 0, + "{label} ({new:?}), {reference_label}: a reference is stale" ); } } @@ -604,6 +584,7 @@ mod tests { TreeType::SumTree, reference_ops, with_reference, + Mode::Merging, GroveVersion::latest(), ) .unwrap_or_else(|e| panic!("{old:?} -> {new:?}, {reference_label}: {e}")); @@ -617,6 +598,7 @@ mod tests { TreeType::SumTree, vec![refresh_op(true)], true, + Mode::Merging, &GROVE_V3, ) .expect_err("expected the legacy refusal"); @@ -718,19 +700,21 @@ mod tests { 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)); - let db = grove_with(&old, TreeType::SumTree, false, grove_version); - db.apply_partial_batch_with_element_flags_update( - vec![write_op(&new)], - Some(BatchApplyOptions::default()), - merging_flags_update, - 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); + 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:?}"); + } } /// A partial batch whose initial segment updates a flagged item and @@ -739,7 +723,8 @@ mod tests { /// 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. + /// 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(); @@ -748,46 +733,55 @@ mod tests { }; let old = item(7, 20, 5, owned_flags(0, OLD_OWNER)); let first = item(8, 21, 9, owned_flags(1, OLD_OWNER)); - let second = item(9, 23, 11, owned_flags(2, OLD_OWNER)); - for reference in [refresh_op(true), refresh_op(false)] { - let label = format!("{reference:?}"); - - let sequential = grove_with(&old, TreeType::SumTree, true, grove_version); - apply(&sequential, vec![write_op(&first)], grove_version) - .unwrap_or_else(|e| panic!("{label}: first batch: {e}")); - apply( - &sequential, - vec![write_op(&second), reference.clone()], - 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(BatchApplyOptions::default()), - merging_flags_update, - split_removal_bytes, - |_cost, _leftover_operations| Ok(vec![write_op(&second), reference.clone()]), - None, + 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, ) - .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}" - ); + .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}" + ); + } } } @@ -817,53 +811,54 @@ mod tests { b"fresh reference".to_vec(), Element::new_reference(ReferencePathType::AbsolutePathReference(fresh_path)), ); - let db = grove_with( - &item(owned_flags(0, OLD_OWNER)), - TreeType::SumTree, - false, - grove_version, - ); - db.apply_partial_batch_with_element_flags_update( - vec![write(&inserted)], - Some(BatchApplyOptions::default()), - merging_flags_update, - split_removal_bytes, - |_cost, _leftover_operations| Ok(vec![write(&rewritten), reference.clone()]), - None, - grove_version, - ) - .cost_as_result() - .expect("expected the partial batch to apply"); - assert_eq!(issues(&db, grove_version), 0); - let stored = db - .get([b"tree".as_slice()].as_ref(), b"fresh", None, grove_version) - .unwrap() - .expect("expected the item"); - assert_eq!(stored, rewritten); + for mode in [Mode::Merging, Mode::Settling] { + let db = grove_with( + &item(owned_flags(0, OLD_OWNER)), + TreeType::SumTree, + false, + grove_version, + ); + db.apply_partial_batch_with_element_flags_update( + vec![write(&inserted)], + Some(options(mode)), + flags_update(mode), + split_removal_bytes, + |_cost, _leftover_operations| Ok(vec![write(&rewritten), reference.clone()]), + None, + grove_version, + ) + .cost_as_result() + .unwrap_or_else(|e| panic!("{mode:?}: expected the partial batch to apply: {e}")); + assert_eq!(issues(&db, grove_version), 0, "{mode:?}"); + let stored = db + .get([b"tree".as_slice()].as_ref(), b"fresh", None, grove_version) + .unwrap() + .expect("expected the item"); + assert_eq!(stored, rewritten, "{mode:?}"); + } } /// Replacements whose new flags stand as written (the flags update - /// leaves them unchanged) but are not the length of the old ones, and - /// whether they run Drive's merging flags update. - fn unchanged_flags_of_another_length() -> Vec<(&'static str, Element, Element, bool)> { + /// 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])), - false, + 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()), - false, + 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)), - true, + Some(Mode::Merging), ), ] } @@ -871,32 +866,32 @@ mod tests { fn replace( db: &TempGroveDb, new: &Element, - merging: bool, + mode: Option, grove_version: &GroveVersion, ) -> Result { - if merging { - apply(db, vec![write_op(new)], grove_version) - } else { - db.apply_batch(vec![write_op(new)], None, None, grove_version) - .cost_as_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, merging) in unchanged_flags_of_another_length() { + 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, merging, 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, merging, 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, @@ -922,9 +917,9 @@ mod tests { #[test] fn grove_v3_refuses_a_replacement_keeping_flags_of_another_length() { let grove_version = &GROVE_V3; - for (label, old, new, merging) in unchanged_flags_of_another_length() { + 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, merging, 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"), @@ -941,7 +936,7 @@ mod tests { let cost = replace( &db, &Element::new_item_with_flags(b"w".to_vec(), Some(vec![1])), - false, + None, grove_version, ) .expect("expected a same-length replacement to apply"); 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..249a591a3 --- /dev/null +++ b/grovedb/src/batch/settle_owner_change_cost_tests.rs @@ -0,0 +1,2006 @@ +//! 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}, + OperationCost, + }; + use grovedb_epoch_based_storage_flags::StorageFlags; + use grovedb_merk::{ + element::{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::{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}, + BatchApplyOptions, KeyInfoPath, QualifiedGroveDbOp, SubelementsDeletionBehavior, + }, + reference_path::ReferencePathType, + tests::{common::EMPTY_PATH, make_empty_grovedb, TempGroveDb}, + BackwardReferences, Element, Error, GroveDb, + }; + + 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(), + ]) + } + + /// 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 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") + } + + 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); + } + + /// 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(); + 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. 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(); + 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 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"targets".as_slice()].as_ref(), + &outer, + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .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!( + applied.storage_cost.added_bytes + > key_bytes() + value_bytes(&new, TreeType::NormalTree, 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!( + average_case.storage_cost.added_bytes >= applied.storage_cost.added_bytes, + "{placement}: average-case estimate {average_case:?} 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)), + settling_flags_update, + split_removal_bytes, + |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() + }), + merging_flags_update, + split_removal_bytes, + 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]); + } + } + + /// 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); + 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"), + ); + } + + /// 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 (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; + + 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 + ); + + // 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( + 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. + #[test] + fn a_reference_in_the_same_batch_commits_to_the_settled_element() { + 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_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") + } + + /// 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)), + 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, + ), + ] + } + + /// 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(), + reference_path.clone(), + *max_hop, + flags.clone(), + false, + true, + ), + )); + } + 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 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() { + 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()], + Some(options(Mode::Settling)), + average_case_tree_type, + grove_version, + ); + assert!( + settling + .storage_cost + .worse_or_eq_than(&applied.storage_cost), + "estimate {settling:?} of {label} of {new:?} is below the settled apply \ + {applied:?}" + ); + } + } + } + } + + /// 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 0ea327d7. 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), + ( + 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, 253, 4554, 6578, 79), + ( + 2, + "insert_or_replace", + false, + 506892, + 237, + 4294967295, + 27857252525, + 87703315, + ), + ( + 2, + "insert_or_replace_dont_check", + true, + 13, + 253, + 1104, + 1451, + 23, + ), + ( + 2, + "insert_or_replace_dont_check", + false, + 12, + 237, + 409674, + 394925, + 275, + ), + (2, "replace", true, 34, 0, 4807, 6578, 79), + ( + 2, + "replace", + false, + 506892, + 200, + 4294967295, + 27857252525, + 87703315, + ), + (2, "replace_dont_check", true, 13, 0, 1357, 1451, 23), + (2, "replace_dont_check", false, 12, 200, 409711, 394925, 275), + (3, "insert_or_replace", true, 34, 206, 4554, 6578, 78), + ( + 3, + "insert_or_replace", + false, + 506892, + 190, + 4294967295, + 27857252525, + 87703314, + ), + ( + 3, + "insert_or_replace_dont_check", + true, + 13, + 206, + 1104, + 1451, + 22, + ), + ( + 3, + "insert_or_replace_dont_check", + false, + 12, + 190, + 409674, + 394925, + 274, + ), + (3, "replace", true, 34, 0, 4760, 6578, 78), + ( + 3, + "replace", + false, + 506892, + 0, + 4294967295, + 27857252525, + 87703314, + ), + (3, "replace_dont_check", true, 13, 0, 1310, 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, 4554, 6578, 79), + ( + 5, + "insert_or_replace", + false, + 506892, + 224, + 4294967295, + 27857252525, + 87703315, + ), + ( + 5, + "insert_or_replace_dont_check", + true, + 13, + 240, + 1104, + 1451, + 23, + ), + ( + 5, + "insert_or_replace_dont_check", + false, + 12, + 224, + 409674, + 394925, + 275, + ), + (5, "replace", true, 34, 0, 4794, 6578, 79), + ( + 5, + "replace", + false, + 506892, + 187, + 4294967295, + 27857252525, + 87703315, + ), + (5, "replace_dont_check", true, 13, 0, 1344, 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), + ]; + + /// The same estimates on grove v3 (no backward-references fan-out), + /// taken from develop at 0ea327d7. + 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, on the latest grove version and on grove v3. + #[test] + fn estimates_without_the_option_are_unchanged() { + 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, + pinned_average_case, + seek_count, + added_bytes, + replaced_bytes, + storage_loaded_bytes, + hash_node_calls, + ) = pinned.next().expect("expected a pinned estimate"); + assert_eq!( + (pinned_case, pinned_label, pinned_average_case), + (case, label, average_case) + ); + let before = 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, + }; + 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}" + ); + } + } + } + } + assert!(pinned.next().is_none(), "expected every pinned estimate"); + } + } + + /// 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 + ); + } + + /// 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 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(), + Element::empty_big_sum_tree(), + Element::empty_count_tree(), + ] { + let parent_tree_type = parent_tree.tree_type().expect("expected a tree"); + 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 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 4da6a09d3..b48bbfeff 100644 --- a/grovedb/src/tests/backward_references_flags_update_tests.rs +++ b/grovedb/src/tests/backward_references_flags_update_tests.rs @@ -317,6 +317,85 @@ 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() + }), + crate::batch::settle_test_support::settling_flags_update, + crate::batch::settle_test_support::split_removal_bytes, + 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 +1062,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 a2ee87d85..7cf78108e 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/merk/mod.rs b/merk/src/merk/mod.rs index a413a4f9d..247a9f974 100644 --- a/merk/src/merk/mod.rs +++ b/merk/src/merk/mod.rs @@ -1263,7 +1263,10 @@ mod test { &|_, _| Ok(None), &mut |_, _, _| { replacements += 1; - Ok((false, None)) + Ok(( + grovedb_costs::storage_cost::transition::ElementFlagsUpdate::Unchanged, + None, + )) }, &mut |_, key_bytes, value_bytes| { Ok(( 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/mod.rs b/merk/src/tree/just_in_time_value_update/mod.rs index eb79ffd60..09439e3e3 100644 --- a/merk/src/tree/just_in_time_value_update/mod.rs +++ b/merk/src/tree/just_in_time_value_update/mod.rs @@ -22,7 +22,10 @@ mod v0; mod v1; -use grovedb_costs::storage_cost::{removal::StorageRemovedBytes, StorageCost}; +use grovedb_costs::storage_cost::{ + key_value_cost::KeyValueStorageCost, removal::StorageRemovedBytes, + transition::ElementFlagsUpdate, StorageCost, +}; use grovedb_version::{error::GroveVersionError, version::GroveVersion}; use crate::{ @@ -80,7 +83,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -143,7 +146,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -178,12 +181,51 @@ impl TreeNode { )), } } + + /// 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. `old_value_bytes` is the + /// stored value's own cost. + fn settled_owner_change_storage_cost( + &self, + old_value: &Vec, + old_value_bytes: u32, + 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 (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}; @@ -221,7 +263,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()))?; @@ -236,7 +278,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( @@ -327,6 +369,86 @@ mod tests { } } + /// 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) + }, + grove_version, + ) + .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)); + } + } + /// A round that rewrites the flags and changes the value-defined cost, /// which the update then restores with the value, followed by an /// unchanged answer, is measured exactly as an immediate unchanged @@ -376,7 +498,9 @@ mod tests { .just_in_time_tree_node_value_update( &old_cost, &no_temp_value, - &mut |_cost: &StorageCost, _old: &Vec, _new: &mut Vec| Ok((false, None)), + &mut |_cost: &StorageCost, _old: &Vec, _new: &mut Vec| { + Ok((ElementFlagsUpdate::Unchanged, None)) + }, &mut basic_removal, grove_version, ) @@ -393,13 +517,13 @@ mod tests { if rounds == 1 { *value = rewritten.clone(); Ok(( - true, + ElementFlagsUpdate::Changed, Some(ValueDefinedCostType::SpecializedValueDefinedCost( rewritten_cost, )), )) } else { - Ok((false, None)) + Ok((ElementFlagsUpdate::Unchanged, None)) } }, &mut basic_removal, diff --git a/merk/src/tree/just_in_time_value_update/v0.rs b/merk/src/tree/just_in_time_value_update/v0.rs index 361db9c96..8c64e17b4 100644 --- a/merk/src/tree/just_in_time_value_update/v0.rs +++ b/merk/src/tree/just_in_time_value_update/v0.rs @@ -1,5 +1,6 @@ use grovedb_costs::storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }; @@ -29,7 +30,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -72,27 +73,42 @@ 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; + 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()) } - // 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( 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 7a51e11c2..9a36aca72 100644 --- a/merk/src/tree/just_in_time_value_update/v1.rs +++ b/merk/src/tree/just_in_time_value_update/v1.rs @@ -1,5 +1,6 @@ use grovedb_costs::storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }; @@ -35,7 +36,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -85,37 +86,53 @@ 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 { - // 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; - } 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 { + 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; } - // 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()); - self.inner.kv.value_defined_cost = original_value_defined_cost.clone(); + 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()); + self.inner.kv.value_defined_cost = original_value_defined_cost.clone(); + } } if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { return Err(Error::CyclicError( diff --git a/merk/src/tree/mod.rs b/merk/src/tree/mod.rs index ea9e0d461..9355c171b 100644 --- a/merk/src/tree/mod.rs +++ b/merk/src/tree/mod.rs @@ -42,6 +42,7 @@ use grovedb_costs::{ storage_cost::{ key_value_cost::KeyValueStorageCost, removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + transition::ElementFlagsUpdate, StorageCost, }, CostContext, CostResult, CostsExt, OperationCost, @@ -1280,7 +1281,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -1346,7 +1347,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -1506,7 +1507,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -1567,7 +1568,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 29fceeaa2..e662bcaee 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, @@ -244,7 +245,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), { @@ -334,7 +335,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), { @@ -544,7 +545,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), @@ -579,7 +580,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), { @@ -1002,7 +1003,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), @@ -1329,7 +1330,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 039d57716..a78b66140 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::{ @@ -52,7 +55,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -114,7 +117,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -180,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)] diff --git a/merk/src/tree/put_value/v0.rs b/merk/src/tree/put_value/v0.rs index e0308b871..59423c245 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, }; @@ -42,7 +42,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -97,7 +97,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 636eb6382..55276b006 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, }; @@ -43,7 +43,7 @@ impl TreeNode { &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -98,7 +98,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 322522d30..7d61ded5b 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( @@ -329,7 +329,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -379,7 +379,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -463,7 +463,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut( @@ -516,7 +516,7 @@ where &Vec, &mut Vec, ) -> Result< - (bool, Option), + (ElementFlagsUpdate, Option), Error, >, section_removal_bytes: &mut impl FnMut(