diff --git a/CHANGELOG.md b/CHANGELOG.md index 975a74c56..c83c72e47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 collection kept as one aux entry per member could not be read back. (#968) ### Changed +- **BREAKING**: `TreeNode::put_value_with_fixed_cost`, + `TreeNode::put_value_with_reference_value_hash_and_value_cost`, + `TreeNode::put_value_with_two_reference_value_hashes_and_value_cost` and + their `Walker` counterparts take a `&GroveVersion`, which selects the + just-in-time value update (see the `merk_versions.tree.just_in_time_value_update` + entry under Fixed). `TreeNode::provided_value_hash_put_final_value` is + renamed `TreeNode::predict_put_final_value` and takes the value-defined + cost the stored node is loaded with, the put it predicts (the new + `grovedb_merk::tree::PredictedPut`: an ordinary put as + `TreeNode::put_value` installs it at the grove version, a + provided-value-hash put, or a put at a specialized cost) and a + `&GroveVersion`. `grovedb_merk::element::insert::specialized_put_cost`, + the value-defined cost an ordinary write puts an element with, and + `Merk::replacement_runs_value_update`, whether a write over a key runs + the just-in-time value update, are new. - **BREAKING**: `delete_operation_for_delete_internal` and `delete_operations_for_delete_up_tree_while_empty` take the operations already pending in the batch as any `impl PendingOperations` in place of a @@ -170,6 +185,37 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 replay, through the new `operations.worst_case.worst_case_sum_item_cost_size` slot (**BREAKING** for code building `GroveDBOperationsWorstCaseVersions` by hand). +- From `GROVE_V4`, a reference a batch writes or refreshes alongside an + update of its target item commits to the bytes the update stores. Before, + the batch predicted those bytes with a hand-written copy of Merk's + just-in-time value update that took a sum item (`SumItem`, + `ItemWithSumItem`) to keep its stored flags, while the apply stores the + flags the flags update leaves it. When the flags update merges them (an + owned item updated in a later epoch, an unowned one gaining an owner) the + reference committed to bytes that are never stored, which + `verify_grovedb` reports. Drive writes this batch for a document of a + summable type: a refresh of a deeper index reference next to the + primary-storage update. The prediction also refused a sum item replaced + by an item, or the reverse, that applies on its own. Now a reference to + a target the batch has already written hashes the bytes stored there, and + one to a target still to be written commits to the bytes Merk's own + just-in-time value update gives for the put the apply performs, once per + target. V1..V3 keep the old prediction for replay, through the new + `apply_batch.same_batch_reference_target_prediction` slot (**BREAKING** + for code building `GroveDBApplyBatchVersions` by hand). +- From `GROVE_V4`, a replacement whose flags update leaves the flags + unchanged is charged for the bytes it stores. Before, Merk measured the + replacement with the new value carrying the old value's flags and kept + that measurement, so when the new value's own flags had another length + (a plain `apply_batch` replacing a flagged item with longer, shorter or + no flags, or Drive's merging callback on an unowned item gaining an owner + as it grows) the commit failed with `storage_cost cost mismatch`. A round + that rewrites the flags and then restores the put's value now also + restores its value-defined cost, so a sum item or tree measured again is + measured with the cost of the bytes it holds. Every replacement V1..V3 + accept is charged the same. V1..V3 keep the old measurement for replay, + through the new `merk_versions.tree.just_in_time_value_update` slot + (**BREAKING** for code building `MerkTreeVersions` by hand). - From `GROVE_V4`, a reference a batch resolves through a pending `InsertIfNotExists` reaches the element already stored at that key when there is one. Before, it always reached the op's element, so a reference diff --git a/grovedb-version/src/version/grovedb_versions.rs b/grovedb-version/src/version/grovedb_versions.rs index 0907d1fda..fba0d14ba 100644 --- a/grovedb-version/src/version/grovedb_versions.rs +++ b/grovedb-version/src/version/grovedb_versions.rs @@ -268,6 +268,24 @@ pub struct GroveDBApplyBatchVersions { /// the same existence read the insert's own level makes; the op's /// element otherwise. pub reference_through_conditional_insert: FeatureVersion, + /// How a reference written in a batch gets the value hash of a target + /// item the same batch updates. + /// + /// - `0` (V1..V3): a hand-written copy of Merk's just-in-time value + /// update over the target's bytes as the Merk cache holds them. For a + /// sum item it assumes the new element keeps the stored flags and does + /// not consult the flags update, while the apply stores the flags the + /// flags update leaves, so when those differ (an owned item updated in + /// a later epoch, an unowned one gaining an owner) the reference + /// commits to bytes that are never stored. A sum item replaced by an + /// item, or the reverse, is refused. + /// - `1` (V4+): once the batch has written the target, the hash of the + /// bytes it stores; before, Merk's own just-in-time value update run on + /// the stored and new bytes with the put the apply performs, once per + /// target. The reference commits to exactly the bytes the apply stores, + /// for every item kind, every answer of the flags update and every + /// order the batch writes the reference and its target in. + pub same_batch_reference_target_prediction: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/grovedb-version/src/version/merk_versions.rs b/grovedb-version/src/version/merk_versions.rs index 658711975..fa4f3bd21 100644 --- a/grovedb-version/src/version/merk_versions.rs +++ b/grovedb-version/src/version/merk_versions.rs @@ -29,6 +29,24 @@ pub struct MerkTreeVersions { /// accounting is unchanged: it is always computed from the stored /// predecessor bytes. pub put_value: FeatureVersion, + /// `TreeNode::just_in_time_tree_node_value_update`: the client-driven + /// update a replacement of a stored value runs (flags carry-over, + /// flags-update callback, removal sectioning). + /// + /// Version 0 measures the replacement with the new value carrying the + /// OLD value's flags, and keeps that measurement when the flags-update + /// callback answers that it left the flags unchanged. The new value is + /// then stored with its own flags, so when those differ in length from + /// the old ones the recorded cost does not match the bytes written and + /// the commit fails with a storage cost mismatch. Consensus-locked for + /// grove v1..v3. + /// + /// Version 1 re-measures the replacement from the bytes it stores when + /// the callback answers unchanged and their size differs from the last + /// measurement taken, and restores the put's value-defined cost + /// whenever it restores the put's value. Every update whose cost + /// version 0 records for the bytes it stores is charged the same. + pub just_in_time_value_update: FeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/grovedb-version/src/version/v1.rs b/grovedb-version/src/version/v1.rs index 1a98c33dd..0d9b2f5a6 100644 --- a/grovedb-version/src/version/v1.rs +++ b/grovedb-version/src/version/v1.rs @@ -46,6 +46,7 @@ pub const GROVE_V1: GroveVersion = GroveVersion { backward_references_maintenance: 0, move_element: 0, reference_through_conditional_insert: 0, + same_batch_reference_target_prediction: 0, }, element: GroveDBElementMethodVersions { delete: 0, @@ -294,7 +295,10 @@ pub const GROVE_V1: GroveVersion = GroveVersion { }, // Ordinary replacements keep the loaded `value_defined_cost` // metadata (issue #908); consensus-locked, bumped in v4. - tree: MerkTreeVersions { put_value: 0 }, + tree: MerkTreeVersions { + put_value: 0, + just_in_time_value_update: 0, + }, }, // MMR hash charges: the shipped accounting, which billed the // storage reads each operation performed but not the blake3 merges diff --git a/grovedb-version/src/version/v2.rs b/grovedb-version/src/version/v2.rs index 26dd8bb1f..dc831adc9 100644 --- a/grovedb-version/src/version/v2.rs +++ b/grovedb-version/src/version/v2.rs @@ -46,6 +46,7 @@ pub const GROVE_V2: GroveVersion = GroveVersion { backward_references_maintenance: 0, move_element: 0, reference_through_conditional_insert: 0, + same_batch_reference_target_prediction: 0, }, element: GroveDBElementMethodVersions { delete: 0, @@ -293,7 +294,10 @@ pub const GROVE_V2: GroveVersion = GroveVersion { }, // Ordinary replacements keep the loaded `value_defined_cost` // metadata (issue #908); consensus-locked, bumped in v4. - tree: MerkTreeVersions { put_value: 0 }, + tree: MerkTreeVersions { + put_value: 0, + just_in_time_value_update: 0, + }, }, // MMR hash charges: the shipped accounting, which billed the // storage reads each operation performed but not the blake3 merges diff --git a/grovedb-version/src/version/v3.rs b/grovedb-version/src/version/v3.rs index 8c6fe3b87..bd55fc203 100644 --- a/grovedb-version/src/version/v3.rs +++ b/grovedb-version/src/version/v3.rs @@ -46,6 +46,7 @@ pub const GROVE_V3: GroveVersion = GroveVersion { backward_references_maintenance: 0, move_element: 0, reference_through_conditional_insert: 0, + same_batch_reference_target_prediction: 0, }, element: GroveDBElementMethodVersions { delete: 0, @@ -311,7 +312,10 @@ pub const GROVE_V3: GroveVersion = GroveVersion { }, // Ordinary replacements keep the loaded `value_defined_cost` // metadata (issue #908); consensus-locked, bumped in v4. - tree: MerkTreeVersions { put_value: 0 }, + tree: MerkTreeVersions { + put_value: 0, + just_in_time_value_update: 0, + }, }, // MMR hash charges: the shipped accounting, which billed the // storage reads each operation performed but not the blake3 merges diff --git a/grovedb-version/src/version/v4.rs b/grovedb-version/src/version/v4.rs index 37b8abb21..bcbb85050 100644 --- a/grovedb-version/src/version/v4.rs +++ b/grovedb-version/src/version/v4.rs @@ -346,6 +346,29 @@ //! Identity-owned sections are unaffected; Drive accounts for the default //! section as system removals, separately from identity fee refunds. //! +//! - `apply_batch.same_batch_reference_target_prediction: 1`: a reference +//! written in the same batch as an update of its target commits to the +//! bytes the target stores. A target the batch has already written is +//! hashed from those bytes; one still to be written is predicted once, +//! through Merk's own just-in-time value update for the put the apply +//! performs. V1..V3 assume a sum item keeps its stored flags, so when the +//! flags update merges them (an owned item updated in a later epoch, an +//! unowned one gaining an owner) the reference commits to bytes never +//! stored and the grove stops verifying; they also refuse a sum item +//! replaced by an item, or the reverse, that applies on its own. Gated +//! because it moves a committed root and flips a rejected/accepted +//! outcome. +//! +//! - `merk_versions.tree.just_in_time_value_update: 1`: a replacement +//! whose flags update leaves the flags unchanged is charged for the bytes +//! it stores. V1..V3 keep the measurement taken with the old value's +//! flags, so a replacement whose own flags differ in length fails its +//! commit with a storage cost mismatch. A round that rewrites the flags +//! and then restores the put's value also restores its value-defined +//! cost, so a sum item or tree measured again is measured with the cost +//! of the bytes it holds. Gated because it changes tracked costs and +//! flips a failed commit into a successful one. +//! //! Note that `GroveVersion::latest()` resolves to this version, so anything //! defaulting to "latest" — tests, benchmarks, tools — exercises every gate //! listed above rather than V3 behaviour. @@ -417,6 +440,10 @@ pub const GROVE_V4: GroveVersion = GroveVersion { // v1: a reference resolved through a skipped InsertIfNotExists // reaches the element already stored there. reference_through_conditional_insert: 1, + // v1: a reference written in the same batch as its target's + // update predicts the target's stored bytes through Merk's own + // just-in-time value update. + same_batch_reference_target_prediction: 1, }, element: GroveDBElementMethodVersions { delete: 0, @@ -720,9 +747,16 @@ pub const GROVE_V4: GroveVersion = GroveVersion { // proof envelope. prove_count_offset_on_range: 0, }, - // Bumped 0 -> 1: ordinary (Item / Reference) replacements of a - // specialized value are charged from their own bytes (issue #908). - tree: MerkTreeVersions { put_value: 1 }, + tree: MerkTreeVersions { + // Bumped 0 -> 1: ordinary (Item / Reference) replacements of a + // specialized value are charged from their own bytes (issue #908). + put_value: 1, + // Bumped 0 -> 1: a replacement whose flags update leaves the + // flags unchanged is measured from the bytes it stores, and a + // sum item or tree measured again after its value was restored + // is measured with that value's own value-defined cost. + just_in_time_value_update: 1, + }, }, // MMR hash charges: one hash per blake3 merge actually computed — // `push` per collapsed peak, `get_root` and `gen_proof` per peak diff --git a/grovedb/src/batch/just_in_time_reference_update.rs b/grovedb/src/batch/just_in_time_reference_update.rs deleted file mode 100644 index 78cb715fb..000000000 --- a/grovedb/src/batch/just_in_time_reference_update.rs +++ /dev/null @@ -1,221 +0,0 @@ -use std::borrow::Cow; - -use grovedb_costs::{ - cost_return_on_error_into_no_add, cost_return_on_error_no_add, - storage_cost::{ - removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, - StorageCost, - }, - CostResult, CostsExt, OperationCost, -}; -use grovedb_merk::{ - tree::{kv::KV, value_hash, TreeNode}, - tree_type::TreeType, - CryptoHash, Merk, -}; -use grovedb_storage::StorageContext; -use grovedb_version::version::GroveVersion; - -use crate::{ - batch::{MerkError, TreeCacheMerkByPath}, - Element, ElementFlags, Error, -}; - -impl<'db, S, F, F2> TreeCacheMerkByPath -where - F: FnMut(&[Vec], bool) -> CostResult, Error>, - F2: FnMut( - &[Vec], - Option<&Element>, - ) -> CostResult)>, Error>, - S: StorageContext<'db>, -{ - /// The hash a reference written in this batch commits to for a pending - /// write of a backward-references ITEM at `qualified_path`: the logical - /// (stripped) hash of the bytes the apply finally stores there — the - /// preprocessor's prediction when the caller's flags update may rewrite - /// them, the op's own element otherwise. - pub(crate) fn landed_backward_references_item_value_hash( - &self, - qualified_path: &[Vec], - element: &Element, - grove_version: &GroveVersion, - ) -> CostResult { - let mut cost = OperationCost::default(); - let landed = self - .landed_backward_references_items - .get(qualified_path) - .unwrap_or(element); - let serialized = cost_return_on_error_into_no_add!( - cost, - landed - .stripped_of_backward_references() - .serialize(grove_version) - ); - let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); - Ok(val_hash).wrap_with_cost(cost) - } - - pub(crate) fn process_old_element_flags( - key: &[u8], - serialized: &[u8], - new_element: &mut Element, - old_element: Element, - old_serialized_element: &[u8], - in_tree_type: TreeType, - flags_update: &mut G, - split_removal_bytes: &mut SR, - grove_version: &GroveVersion, - ) -> CostResult - where - G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, - SR: FnMut( - &mut ElementFlags, - u32, - u32, - ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, - { - let mut cost = OperationCost::default(); - if old_element.is_sum_item() { - return if new_element.is_sum_item() { - let maybe_old_flags = old_element.get_flags_owned(); - if maybe_old_flags.is_some() { - let mut updated_new_element_with_old_flags = new_element.clone(); - updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); - // There are no storage flags, we can just hash new element - let new_serialized_bytes = cost_return_on_error_into_no_add!( - cost, - updated_new_element_with_old_flags.serialize(grove_version) - ); - let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); - Ok(val_hash).wrap_with_cost(cost) - } else { - let val_hash = value_hash(serialized).unwrap_add_cost(&mut cost); - Ok(val_hash).wrap_with_cost(cost) - } - } else { - Err(Error::NotSupported( - "going from a sum item to a not sum item is not supported".to_string(), - )) - .wrap_with_cost(cost) - }; - } else if new_element.is_sum_item() { - return Err(Error::NotSupported( - "going from an item to a sum item is not supported".to_string(), - )) - .wrap_with_cost(cost); - } - let mut maybe_old_flags = old_element.get_flags_owned(); - - let old_storage_cost = KV::node_value_byte_cost_size( - key.len() as u32, - old_serialized_element.len() as u32, - in_tree_type.inner_node_type(), - ); - - let original_new_element = new_element.clone(); - - let mut serialization_to_use = Cow::Borrowed(serialized); - - let mut new_storage_cost = if maybe_old_flags.is_some() { - // we need to get the new storage_cost as if it had the same storage flags as - // before - let mut updated_new_element_with_old_flags = original_new_element.clone(); - updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); - - let serialized_with_old_flags = cost_return_on_error_into_no_add!( - cost, - updated_new_element_with_old_flags.serialize(grove_version) - ); - KV::node_value_byte_cost_size( - key.len() as u32, - serialized_with_old_flags.len() as u32, - in_tree_type.inner_node_type(), - ) - } else { - KV::node_value_byte_cost_size( - key.len() as u32, - serialized.len() as u32, - in_tree_type.inner_node_type(), - ) - }; - - let mut i = 0; - - loop { - // Calculate storage costs - let mut storage_costs = - TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); - - if let Some(old_element_flags) = maybe_old_flags.as_mut() - && let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes - { - let (_, value_removed_bytes) = cost_return_on_error_no_add!( - cost, - split_removal_bytes(old_element_flags, 0, removed_bytes) - ); - storage_costs.removed_bytes = value_removed_bytes; - } - - let mut new_element_cloned = original_new_element.clone(); - - let new_flags = cost_return_on_error_no_add!( - cost, - new_element_cloned - .get_flags_mut() - .as_mut() - .ok_or(Error::CorruptedCodeExecution( - "element has no flags for just-in-time update", - )) - ); - let changed = cost_return_on_error_no_add!( - cost, - (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags).map_err(|e| { - match e { - Error::JustInTimeElementFlagsClientError(_) => { - MerkError::ClientCorruptionError(e.to_string()).into() - } - _ => { - MerkError::ClientCorruptionError("non client error".to_string()).into() - } - } - }) - ); - if !changed { - // There are no storage flags, we can just hash new element - - let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } else { - // There are no storage flags, we can just hash new element - let new_serialized_bytes = cost_return_on_error_into_no_add!( - cost, - new_element_cloned.serialize(grove_version) - ); - - new_storage_cost = KV::node_value_byte_cost_size( - key.len() as u32, - new_serialized_bytes.len() as u32, - in_tree_type.inner_node_type(), - ); - - if serialization_to_use == new_serialized_bytes { - // it hasn't actually changed, let's do the value hash of it - let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); - return Ok(val_hash).wrap_with_cost(cost); - } - - serialization_to_use = Cow::Owned(new_serialized_bytes); - } - - // Prevent potential infinite loop - if i > 8 { - return Err(Error::CyclicError( - "updated value based on costs too many times in reference", - )) - .wrap_with_cost(cost); - } - i += 1; - } - } -} diff --git a/grovedb/src/batch/just_in_time_reference_update/mod.rs b/grovedb/src/batch/just_in_time_reference_update/mod.rs new file mode 100644 index 000000000..f9b038916 --- /dev/null +++ b/grovedb/src/batch/just_in_time_reference_update/mod.rs @@ -0,0 +1,168 @@ +//! The value hashes a reference written in a batch commits to when its +//! target is a pending write of the same batch. +//! +//! `TreeCacheMerkByPath::process_old_element_flags` gives the hash of the +//! bytes a pending item write finally stores, after the apply has run the +//! caller's flags update on it. Each `v*.rs` defines a +//! `process_old_element_flags_v*`, selected by +//! `apply_batch.same_batch_reference_target_prediction`: +//! +//! - version 0 is a hand-written copy of Merk's just-in-time value update +//! over the target's bytes as the Merk cache holds them. For a sum item it +//! assumes the new element keeps the stored flags and does not consult +//! the flags update, while the apply stores the flags the flags update +//! leaves it. When those differ (an owned sum item updated in a later +//! epoch, an unowned one gaining an owner) the reference commits to bytes +//! that are never stored and the grove no longer verifies. Grove v1..v3 +//! are consensus-locked to this. +//! - version 1 hashes the bytes the target stores once this pass has written +//! it, and otherwise predicts them by running Merk's own just-in-time value +//! update on the stored bytes and the new ones, with the put the apply +//! performs, once per target. The reference commits to exactly the bytes +//! the apply stores. + +mod v0; +mod v1; + +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}, + CostResult, CostsExt, OperationCost, +}; +use grovedb_merk::{tree::value_hash, tree_type::TreeType, CryptoHash, Merk}; +use grovedb_storage::StorageContext; +use grovedb_version::{error::GroveVersionError, version::GroveVersion}; + +use crate::{batch::TreeCacheMerkByPath, Element, ElementFlags, Error}; + +impl<'db, S, F, F2> TreeCacheMerkByPath +where + F: FnMut(&[Vec], bool) -> CostResult, Error>, + F2: FnMut( + &[Vec], + Option<&Element>, + ) -> CostResult)>, Error>, + S: StorageContext<'db>, +{ + /// The hash a reference written in this batch commits to for a pending + /// write of a backward-references ITEM at `qualified_path`: the logical + /// (stripped) hash of the bytes the apply finally stores there — the + /// preprocessor's prediction when the caller's flags update may rewrite + /// them, the op's own element otherwise. + pub(crate) fn landed_backward_references_item_value_hash( + &self, + qualified_path: &[Vec], + element: &Element, + grove_version: &GroveVersion, + ) -> CostResult { + let mut cost = OperationCost::default(); + let landed = self + .landed_backward_references_items + .get(qualified_path) + .unwrap_or(element); + let serialized = cost_return_on_error_into_no_add!( + cost, + landed + .stripped_of_backward_references() + .serialize(grove_version) + ); + let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); + Ok(val_hash).wrap_with_cost(cost) + } + + /// Whether a same-batch reference predicts its target with version 1, + /// which reads whether this pass has already written the target, so + /// the pass must record the targets it writes + /// (`written_qualified_paths`). + pub(crate) fn tracks_written_targets(grove_version: &GroveVersion) -> bool { + grove_version + .grovedb_versions + .apply_batch + .same_batch_reference_target_prediction + != 0 + } + + /// The value hash of the bytes a pending write of `new_element` at + /// `qualified_path` over the stored `old_serialized_element` finally + /// stores, after the apply has run the caller's flags update on it, so + /// that a reference written in the same batch commits to them. + /// `target_written` says this pass has already written the target, so + /// the stored bytes are the ones it stores, and `runs_value_update` + /// whether the apply's write over them runs the just-in-time value update + /// (read by version 1 only). See the module docs for the version + /// semantics. + #[allow(clippy::too_many_arguments)] + pub(crate) fn process_old_element_flags( + key: &[u8], + qualified_path: &[Vec], + new_element: &Element, + old_serialized_element: Vec, + in_tree_type: TreeType, + target_written: bool, + runs_value_update: bool, + predicted_targets: &mut HashMap>, Vec>, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, + ) -> CostResult + where + G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, + { + match grove_version + .grovedb_versions + .apply_batch + .same_batch_reference_target_prediction + { + 0 => { + let cost = OperationCost::default(); + let serialized = + cost_return_on_error_into_no_add!(cost, new_element.serialize(grove_version)); + let old_element = cost_return_on_error_no_add!( + cost, + Element::deserialize(&old_serialized_element, grove_version).map_err(|e| { + Error::CorruptedData(format!("unable to deserialize element: {e}")) + }) + ); + v0::process_old_element_flags_v0( + key, + &serialized, + new_element, + old_element, + &old_serialized_element, + in_tree_type, + flags_update, + split_removal_bytes, + grove_version, + ) + } + 1 => v1::process_old_element_flags_v1( + key, + qualified_path, + new_element, + old_serialized_element, + in_tree_type, + target_written, + runs_value_update, + predicted_targets, + flags_update, + split_removal_bytes, + grove_version, + ), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "process_old_element_flags".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )) + .wrap_with_cost(OperationCost::default()), + } + } +} diff --git a/grovedb/src/batch/just_in_time_reference_update/v0.rs b/grovedb/src/batch/just_in_time_reference_update/v0.rs new file mode 100644 index 000000000..f14ffc94d --- /dev/null +++ b/grovedb/src/batch/just_in_time_reference_update/v0.rs @@ -0,0 +1,189 @@ +use std::borrow::Cow; + +use grovedb_costs::{ + cost_return_on_error_into_no_add, cost_return_on_error_no_add, + storage_cost::{ + removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + StorageCost, + }, + CostResult, CostsExt, OperationCost, +}; +use grovedb_merk::{ + tree::{kv::KV, value_hash, TreeNode}, + tree_type::TreeType, + CryptoHash, +}; +use grovedb_version::version::GroveVersion; + +use crate::{batch::MerkError, Element, ElementFlags, Error}; + +/// Version 0 of the same-batch reference target prediction, selected by +/// `apply_batch.same_batch_reference_target_prediction == 0` (grove +/// v1..v3): a hand-written copy of Merk's just-in-time value update. +/// +/// It differs from version 1 in how it gets the stored bytes. It measures +/// and asks the flags update itself, over the target's bytes as the Merk +/// cache holds them, and takes a sum item to keep its stored flags without +/// consulting the flags update, while the apply stores the flags the flags +/// update leaves it. +#[allow(clippy::too_many_arguments)] +pub(super) fn process_old_element_flags_v0( + key: &[u8], + serialized: &[u8], + new_element: &Element, + old_element: Element, + old_serialized_element: &[u8], + in_tree_type: TreeType, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, +) -> CostResult +where + G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, +{ + let mut cost = OperationCost::default(); + if old_element.is_sum_item() { + return if new_element.is_sum_item() { + let maybe_old_flags = old_element.get_flags_owned(); + if maybe_old_flags.is_some() { + let mut updated_new_element_with_old_flags = new_element.clone(); + updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); + // There are no storage flags, we can just hash new element + let new_serialized_bytes = cost_return_on_error_into_no_add!( + cost, + updated_new_element_with_old_flags.serialize(grove_version) + ); + let val_hash = value_hash(&new_serialized_bytes).unwrap_add_cost(&mut cost); + Ok(val_hash).wrap_with_cost(cost) + } else { + let val_hash = value_hash(serialized).unwrap_add_cost(&mut cost); + Ok(val_hash).wrap_with_cost(cost) + } + } else { + Err(Error::NotSupported( + "going from a sum item to a not sum item is not supported".to_string(), + )) + .wrap_with_cost(cost) + }; + } else if new_element.is_sum_item() { + return Err(Error::NotSupported( + "going from an item to a sum item is not supported".to_string(), + )) + .wrap_with_cost(cost); + } + let mut maybe_old_flags = old_element.get_flags_owned(); + + let old_storage_cost = KV::node_value_byte_cost_size( + key.len() as u32, + old_serialized_element.len() as u32, + in_tree_type.inner_node_type(), + ); + + let original_new_element = new_element.clone(); + + let mut serialization_to_use = Cow::Borrowed(serialized); + + let mut new_storage_cost = if maybe_old_flags.is_some() { + // we need to get the new storage_cost as if it had the same storage flags as + // before + let mut updated_new_element_with_old_flags = original_new_element.clone(); + updated_new_element_with_old_flags.set_flags(maybe_old_flags.clone()); + + let serialized_with_old_flags = cost_return_on_error_into_no_add!( + cost, + updated_new_element_with_old_flags.serialize(grove_version) + ); + KV::node_value_byte_cost_size( + key.len() as u32, + serialized_with_old_flags.len() as u32, + in_tree_type.inner_node_type(), + ) + } else { + KV::node_value_byte_cost_size( + key.len() as u32, + serialized.len() as u32, + in_tree_type.inner_node_type(), + ) + }; + + let mut i = 0; + + loop { + // Calculate storage costs + let mut storage_costs = + TreeNode::storage_cost_for_update(new_storage_cost, old_storage_cost); + + if let Some(old_element_flags) = maybe_old_flags.as_mut() + && let BasicStorageRemoval(removed_bytes) = storage_costs.removed_bytes + { + let (_, value_removed_bytes) = cost_return_on_error_no_add!( + cost, + split_removal_bytes(old_element_flags, 0, removed_bytes) + ); + storage_costs.removed_bytes = value_removed_bytes; + } + + let mut new_element_cloned = original_new_element.clone(); + + let new_flags = cost_return_on_error_no_add!( + cost, + new_element_cloned + .get_flags_mut() + .as_mut() + .ok_or(Error::CorruptedCodeExecution( + "element has no flags for just-in-time update", + )) + ); + let changed = cost_return_on_error_no_add!( + cost, + (flags_update)(&storage_costs, maybe_old_flags.clone(), new_flags).map_err(|e| { + match e { + Error::JustInTimeElementFlagsClientError(_) => { + MerkError::ClientCorruptionError(e.to_string()).into() + } + _ => MerkError::ClientCorruptionError("non client error".to_string()).into(), + } + }) + ); + if !changed { + // There are no storage flags, we can just hash new element + + let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } else { + // There are no storage flags, we can just hash new element + let new_serialized_bytes = cost_return_on_error_into_no_add!( + cost, + new_element_cloned.serialize(grove_version) + ); + + new_storage_cost = KV::node_value_byte_cost_size( + key.len() as u32, + new_serialized_bytes.len() as u32, + in_tree_type.inner_node_type(), + ); + + if serialization_to_use == new_serialized_bytes { + // it hasn't actually changed, let's do the value hash of it + let val_hash = value_hash(&serialization_to_use).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + + serialization_to_use = Cow::Owned(new_serialized_bytes); + } + + // Prevent potential infinite loop + if i > 8 { + return Err(Error::CyclicError( + "updated value based on costs too many times in reference", + )) + .wrap_with_cost(cost); + } + i += 1; + } +} diff --git a/grovedb/src/batch/just_in_time_reference_update/v1.rs b/grovedb/src/batch/just_in_time_reference_update/v1.rs new file mode 100644 index 000000000..dc39ae900 --- /dev/null +++ b/grovedb/src/batch/just_in_time_reference_update/v1.rs @@ -0,0 +1,102 @@ +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}, + CostResult, CostsExt, OperationCost, +}; +use grovedb_merk::{ + element::{insert::specialized_put_cost, tree_type::ElementTreeTypeExtensions}, + tree::{value_hash, PredictedPut}, + tree_type::TreeType, + CryptoHash, +}; +use grovedb_version::version::GroveVersion; + +use crate::{ + batch::just_in_time_value_update::predict_put_final_bytes, Element, ElementFlags, Error, +}; + +/// Version 1 of the same-batch reference target prediction, selected by +/// `apply_batch.same_batch_reference_target_prediction == 1` (grove v4+). +/// +/// It differs from version 0 in how it gets the stored bytes. Once this pass +/// has written the target, `old_serialized_element` (the target's bytes as +/// the Merk cache holds them) are the bytes it stores. Before that, they are +/// the bytes the apply will update, and the stored bytes are predicted by +/// running Merk's own just-in-time value update on them and the new ones, +/// with the put the apply performs (`specialized_put_cost`), once per target. +/// The reference commits to exactly what the apply stores whatever the flags +/// update answers, and whether the target is a sum item or not. A target the +/// apply rewrites without the update (`runs_value_update` false: a node a +/// partial batch's initial segment inserted, still held in the Merk) stores +/// its bytes as given. +#[allow(clippy::too_many_arguments)] +pub(super) fn process_old_element_flags_v1( + key: &[u8], + qualified_path: &[Vec], + new_element: &Element, + old_serialized_element: Vec, + in_tree_type: TreeType, + target_written: bool, + runs_value_update: bool, + predicted_targets: &mut HashMap>, Vec>, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, +) -> CostResult +where + G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, +{ + let mut cost = OperationCost::default(); + if target_written { + let val_hash = value_hash(&old_serialized_element).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + if let Some(stored_bytes) = predicted_targets.get(qualified_path) { + let val_hash = value_hash(stored_bytes).unwrap_add_cost(&mut cost); + return Ok(val_hash).wrap_with_cost(cost); + } + // Serialized only now: a written target or a cached prediction does not + // need the incoming bytes. + let serialized = cost_return_on_error_into_no_add!(cost, new_element.serialize(grove_version)); + if !runs_value_update { + let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); + predicted_targets.insert(qualified_path.to_vec(), serialized); + return Ok(val_hash).wrap_with_cost(cost); + } + let put = match cost_return_on_error_no_add!( + cost, + specialized_put_cost(new_element, grove_version).map_err(Error::MerkError) + ) { + Some(value_cost) => PredictedPut::SpecializedCost(value_cost), + None => PredictedPut::Ordinary, + }; + let feature_type = + cost_return_on_error_into_no_add!(cost, new_element.get_feature_type(in_tree_type)); + let stored_bytes = cost_return_on_error_no_add!( + cost, + predict_put_final_bytes( + key, + old_serialized_element, + serialized, + feature_type, + put, + in_tree_type, + flags_update, + split_removal_bytes, + grove_version, + ) + .map_err(|e| Error::CorruptedData(format!( + "predicting the just-in-time value update of a reference target: {e}" + ))) + ); + let val_hash = value_hash(&stored_bytes).unwrap_add_cost(&mut cost); + predicted_targets.insert(qualified_path.to_vec(), stored_bytes); + Ok(val_hash).wrap_with_cost(cost) +} diff --git a/grovedb/src/batch/just_in_time_value_update.rs b/grovedb/src/batch/just_in_time_value_update.rs index f55a6287e..015acf18b 100644 --- a/grovedb/src/batch/just_in_time_value_update.rs +++ b/grovedb/src/batch/just_in_time_value_update.rs @@ -9,7 +9,9 @@ //! commits to the item's logical hash — flags included — and a flags //! callback (Drive's epoch-based storage flags) rewrites flags on every //! cross-epoch update. [`predict_provided_value_hash_put`] runs this update -//! through Merk's own routine to get those bytes. +//! through Merk's own routine to get those bytes, and +//! [`predict_put_final_bytes`] does the same for any put, which a +//! same-batch reference to a pending item write commits to. use grovedb_costs::storage_cost::{ removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, @@ -22,7 +24,7 @@ use grovedb_merk::{ ValueDefinedCostType, ValueDefinedCostType::{LayeredValueDefinedCost, SpecializedValueDefinedCost}, }, - TreeNode, + PredictedPut, TreeFeatureType, TreeNode, }, tree_type::{CostSize, TreeType, SUM_ITEM_COST_SIZE}, Error as MerkError, @@ -200,22 +202,25 @@ where } } -/// The element a batch apply finally stores when it writes `new_element` -/// (a backward-references family element, which the apply writes as a -/// provided-value-hash put) over the stored `old_serialized` bytes at `key` -/// in a subtree of `in_tree_type`, with the caller's callbacks. +/// The bytes a batch apply finally stores when `put` of `new_serialized` +/// (with `feature_type`) replaces the stored `old_serialized` bytes at `key` +/// in a subtree of `in_tree_type`, with the caller's callbacks: Merk's own +/// just-in-time value update, run on a detached node loaded as the apply +/// loads the stored node. /// -/// The callbacks must answer the apply the same way they answer here: the -/// batch checks after the apply that the stored bytes are the predicted ones. -pub(crate) fn predict_provided_value_hash_put( +/// The callbacks must answer the apply the same way they answer here. +#[allow(clippy::too_many_arguments)] +pub(crate) fn predict_put_final_bytes( key: &[u8], - old_serialized: &[u8], - new_element: &Element, + old_serialized: Vec, + new_serialized: Vec, + feature_type: TreeFeatureType, + put: PredictedPut, in_tree_type: TreeType, flags_update: &mut G, split_removal_bytes: &mut SR, grove_version: &GroveVersion, -) -> Result +) -> Result, MerkError> where G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, SR: FnMut( @@ -224,13 +229,23 @@ where u32, ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, { - let new_serialized = new_element.serialize(grove_version)?; - let feature_type = new_element.get_feature_type(in_tree_type)?; - let final_bytes = TreeNode::provided_value_hash_put_final_value( + // 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. + let old_value_defined_cost = match put { + PredictedPut::Ordinary => { + Element::value_defined_cost_for_serialized_value(&old_serialized, grove_version) + } + PredictedPut::ProvidedValueHash | PredictedPut::SpecializedCost(_) => None, + }; + TreeNode::predict_put_final_value( key.to_vec(), - old_serialized.to_vec(), - new_serialized.clone(), + old_serialized, + old_value_defined_cost, + new_serialized, feature_type, + put, &|key, value| old_specialized_cost(key, value, in_tree_type, grove_version), &|old_value, new_value| new_value_with_old_flags(old_value, new_value, grove_version), &mut |storage_costs, old_value, new_value| { @@ -251,6 +266,46 @@ where grove_version, ) }, + grove_version, + ) +} + +/// The element a batch apply finally stores when it writes `new_element` +/// (a backward-references family element, which the apply writes as a +/// provided-value-hash put) over the stored `old_serialized` bytes at `key` +/// in a subtree of `in_tree_type`, with the caller's callbacks. +/// +/// The callbacks must answer the apply the same way they answer here: the +/// batch checks after the apply that the stored bytes are the predicted ones. +pub(crate) fn predict_provided_value_hash_put( + key: &[u8], + old_serialized: &[u8], + new_element: &Element, + in_tree_type: TreeType, + flags_update: &mut G, + split_removal_bytes: &mut SR, + grove_version: &GroveVersion, +) -> Result +where + G: FnMut(&StorageCost, Option, &mut ElementFlags) -> Result, + SR: FnMut( + &mut ElementFlags, + u32, + u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error>, +{ + let new_serialized = new_element.serialize(grove_version)?; + let feature_type = new_element.get_feature_type(in_tree_type)?; + let final_bytes = predict_put_final_bytes( + key, + old_serialized.to_vec(), + new_serialized.clone(), + feature_type, + PredictedPut::ProvidedValueHash, + in_tree_type, + flags_update, + split_removal_bytes, + grove_version, ) .map_err(|e| { Error::CorruptedData(format!( diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 2d8c35e5b..046122a25 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -34,6 +34,8 @@ mod just_in_time_value_update; mod options; mod refresh_reference_mode; #[cfg(test)] +mod same_batch_reference_prediction_tests; +#[cfg(test)] mod single_deletion_cost_tests; #[cfg(test)] mod single_insert_cost_tests; @@ -2073,6 +2075,40 @@ struct TreeCacheMerkByPath { /// already holding an element: the insert writes nothing there, so a /// moved reference through one does not reach a write. kept_by_conditional_insert: HashSet>>, + /// The qualified paths of the targets this pass has already written. A + /// same-batch reference to one of them commits to the bytes it now + /// stores instead of predicting them (from + /// `apply_batch.same_batch_reference_target_prediction` version 1). + written_qualified_paths: HashSet>>, + /// The bytes predicted for a pending item write that a same-batch + /// reference points at, by the target's qualified path, so references + /// sharing a target predict it once (from + /// `apply_batch.same_batch_reference_target_prediction` version 1). + predicted_reference_targets: HashMap>, Vec>, +} + +/// Whether a same-batch reference to the target `op` writes predicts that +/// target: a flagged `Item`, `SumItem` or `ItemWithSumItem` (through any +/// wrapper) written by an insert-or-replace, a replace or a patch. Such a +/// reference reads whether the pass has already written the target, so only +/// these targets are recorded in `written_qualified_paths`; the reference arm +/// of `follow_reference_get_value_hash` predicts exactly the targets this +/// accepts. +fn writes_a_predicted_reference_target(op: &GroveOp) -> bool { + match op { + GroveOp::InsertOrReplace { element } + | GroveOp::InsertOrReplaceDontCheckForBackwardsReferences { element } + | GroveOp::Replace { element } + | GroveOp::ReplaceDontCheckForBackwardsReferences { element } + | GroveOp::Patch { element, .. } + | GroveOp::PatchDontCheckForBackwardsReferences { element, .. } => { + matches!( + element.underlying(), + Element::Item(..) | Element::SumItem(..) | Element::ItemWithSumItem(..) + ) && element.get_flags().is_some() + } + _ => false, + } } impl fmt::Debug for TreeCacheMerkByPath { @@ -2611,6 +2647,36 @@ where ) -> CostResult, TreeType)>, Error> { let mut cost = OperationCost::default(); + let referenced_element = cost_return_on_error!( + &mut cost, + self.get_referenced_element_bytes(key, reference_path, grove_version) + ); + + if let Some((referenced_element, tree_type)) = referenced_element { + let element = cost_return_on_error_no_add!( + cost, + Element::deserialize(referenced_element.as_slice(), grove_version).map_err(|e| { + Error::CorruptedData(format!("unable to deserialize element: {e}")) + }) + ); + + Ok(Some((element, referenced_element, tree_type))).wrap_with_cost(cost) + } else { + Ok(None).wrap_with_cost(cost) + } + } + + /// The serialized bytes of the element at `key` under `reference_path` + /// as the Merk cache holds them, and the type of the tree holding it; + /// [`Self::get_and_deserialize_referenced_element`] without the decode. + fn get_referenced_element_bytes( + &mut self, + key: &[u8], + reference_path: &[Vec], + grove_version: &GroveVersion, + ) -> CostResult, TreeType)>, Error> { + let mut cost = OperationCost::default(); + let merk = match self.merks.entry(reference_path.to_vec()) { HashMapEntry::Occupied(o) => o.into_mut(), HashMapEntry::Vacant(v) => v.insert(cost_return_on_error!( @@ -2632,18 +2698,8 @@ where let tree_type = merk.tree_type; - if let Some(referenced_element) = referenced_element { - let element = cost_return_on_error_no_add!( - cost, - Element::deserialize(referenced_element.as_slice(), grove_version).map_err(|e| { - Error::CorruptedData(format!("unable to deserialize element: {e}")) - }) - ); - - Ok(Some((element, referenced_element, tree_type))).wrap_with_cost(cost) - } else { - Ok(None).wrap_with_cost(cost) - } + Ok(referenced_element.map(|referenced_element| (referenced_element, tree_type))) + .wrap_with_cost(cost) } /// Processes a reference with a hop count greater than one, handling the @@ -2945,41 +3001,52 @@ where // wrapper for hashing so the value hash matches storage. match element.underlying() { Element::Item(..) | Element::SumItem(..) | Element::ItemWithSumItem(..) => { - let serialized = cost_return_on_error_into_no_add!( - cost, - element.serialize(grove_version) - ); - if element.get_flags().is_none() { + // The same predicate decides which targets the pass + // records as written, so every target predicted here + // is recorded. + if !writes_a_predicted_reference_target(op) { // There are no storage flags, we can just hash new element + let serialized = cost_return_on_error_into_no_add!( + cost, + element.serialize(grove_version) + ); let val_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); Ok(val_hash).wrap_with_cost(cost) } else { - let mut new_element = element.clone(); - // it can be unmerged, let's get the value on disk let (key, reference_path) = qualified_path .split_last() .expect("empty path refused at the top of this function"); let serialized_element_result = cost_return_on_error!( &mut cost, - self.get_and_deserialize_referenced_element( + self.get_referenced_element_bytes( key, reference_path, grove_version ) ); - if let Some((old_element, old_serialized_element, is_in_sum_tree)) = + if let Some((old_serialized_element, is_in_sum_tree)) = serialized_element_result { + let target_written = + 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 value_hash = cost_return_on_error!( &mut cost, Self::process_old_element_flags( key, - &serialized, - &mut new_element, - old_element, - &old_serialized_element, + qualified_path, + element, + old_serialized_element, is_in_sum_tree, + target_written, + runs_value_update, + &mut self.predicted_reference_targets, flags_update, split_removal_bytes, grove_version, @@ -2987,6 +3054,10 @@ where ); Ok(value_hash).wrap_with_cost(cost) } else { + let serialized = cost_return_on_error_into_no_add!( + cost, + element.serialize(grove_version) + ); let value_hash = value_hash(&serialized).unwrap_add_cost(&mut cost); Ok(value_hash).wrap_with_cost(cost) @@ -3376,6 +3447,23 @@ where let p = path.to_path(); let path = &p; self.unused_new_merks.remove(path); + // The targets this path writes that a same-batch reference may + // predict, recorded as written once it has applied them (only read by + // the version 1 target prediction). + let written_qualified_paths: Vec>> = + if Self::tracks_written_targets(grove_version) { + ops_at_path_by_key + .iter() + .filter(|(_, op)| writes_a_predicted_reference_target(op)) + .map(|(key_info, _)| { + let mut qualified_path = path.to_vec(); + qualified_path.push(key_info.get_key_clone()); + qualified_path + }) + .collect() + } else { + Vec::new() + }; // This also populates Merk trees cache let in_tree_type = { @@ -5291,6 +5379,42 @@ where .insert(path.to_vec(), per_axis); } + for qualified_path in written_qualified_paths { + // A reference to a written target hashes the bytes it stores + // and never reads its prediction again, so the prediction can + // go. A reference resolved before the write committed to the + // prediction, so the write must have stored exactly those bytes: + // refuse the batch otherwise, as for a backward-references item. + // The read is of the node this pass just wrote and is not + // charged, so the check changes no cost. + if let (Some(predicted), Some(key)) = ( + self.predicted_reference_targets.remove(&qualified_path), + qualified_path.last(), + ) { + let merk = self.merks.get_mut(path).expect("the Merk is cached"); + let stored = cost_return_on_error_no_add!( + cost, + merk.get( + key, + true, + Some(&Element::value_defined_cost_for_serialized_value), + grove_version, + ) + .unwrap() + .map_err(|e| Error::CorruptedData(e.to_string())) + ); + if stored.as_deref() != Some(predicted.as_slice()) { + return Err(Error::JustInTimeElementFlagsClientError( + "a pending item write stored other bytes than the ones predicted \ + for a reference to it in the same batch; the flags callbacks must \ + be deterministic" + .to_owned(), + )) + .wrap_with_cost(cost); + } + } + self.written_qualified_paths.insert(qualified_path); + } let merk = self.merks.get_mut(path).expect("the Merk is cached"); merk.root_hash_key_and_aggregate_data() .add_cost(cost) @@ -5890,6 +6014,8 @@ impl GroveDb { moved_subtrees: Vec::new(), moved_positions: Default::default(), kept_by_conditional_insert: HashSet::new(), + written_qualified_paths: Default::default(), + predicted_reference_targets: Default::default(), }, grove_version ) @@ -5960,6 +6086,22 @@ impl GroveDb { merk_tree_cache.merks.remove(&path); } } + // The additional ops write their targets again, so a reference to one + // predicts the bytes it stores from what the first segment left, and + // no prediction made before the continuation still holds. + for op in &additional_ops { + if merk_tree_cache.written_qualified_paths.is_empty() { + break; + } + if let Some(key) = &op.key { + let mut qualified_path = op.path.to_path(); + qualified_path.push(key.get_key_clone()); + merk_tree_cache + .written_qualified_paths + .remove(&qualified_path); + } + } + merk_tree_cache.predicted_reference_targets.clear(); let mut cost = OperationCost::default(); let batch_structure = cost_return_on_error!( &mut cost, @@ -7035,11 +7177,19 @@ impl GroveDb { /// Applies batch of operations on GroveDB /// /// `update_element_flags_function` and `split_removal_bytes_function` - /// run whenever a write replaces a stored value. They must be - /// deterministic: under backward-references maintenance the batch - /// predicts the bytes a flagged item will store (its referrers commit to - /// them) by consulting the same callbacks before the apply, and refuses - /// the batch if the apply stores anything else. + /// run whenever a write replaces a stored value, and may run more than + /// once for one write. They must be deterministic, answering the same + /// inputs the same way every time: the batch predicts the bytes a + /// flagged item will store by consulting the same callbacks before the + /// apply, wherever something commits to those bytes first. + /// + /// - A reference the batch writes or refreshes to an item the batch + /// also updates commits to the predicted bytes. Nothing checks them + /// after the apply, so a callback that answers the apply differently + /// leaves the reference committed to bytes that are never stored. + /// - Under backward-references maintenance the referrers of a flagged + /// backward-references item commit to them, and the batch refuses the + /// batch if the apply stores anything else. pub fn apply_batch_with_element_flags_update( &self, ops: Vec, diff --git a/grovedb/src/batch/same_batch_reference_prediction_tests.rs b/grovedb/src/batch/same_batch_reference_prediction_tests.rs new file mode 100644 index 000000000..08e1b5d00 --- /dev/null +++ b/grovedb/src/batch/same_batch_reference_prediction_tests.rs @@ -0,0 +1,950 @@ +//! A replacement whose new value keeps flags of its own: a reference written +//! in the same batch commits to the bytes the replacement stores +//! (`apply_batch.same_batch_reference_target_prediction`), and the +//! replacement is charged for those bytes +//! (`merk_versions.tree.just_in_time_value_update`). + +#[cfg(feature = "minimal")] +mod tests { + use grovedb_costs::{ + storage_cost::{ + removal::{ + StorageRemovedBytes, + StorageRemovedBytes::{BasicStorageRemoval, NoStorageRemoval}, + }, + StorageCost, + }, + OperationCost, + }; + use std::collections::BTreeMap; + + use grovedb_epoch_based_storage_flags::StorageFlags; + use grovedb_merk::{element::costs::ElementCostExtensions, tree_type::TreeType}; + use grovedb_version::version::{v3::GROVE_V3, GroveVersion}; + + use crate::{ + batch::{BatchApplyOptions, QualifiedGroveDbOp}, + reference_path::ReferencePathType, + tests::{make_empty_grovedb, TempGroveDb}, + Element, ElementFlags, 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()]) + } + + fn insert(db: &TempGroveDb, path: &[&[u8]], key: &[u8], element: Element, gv: &GroveVersion) { + db.insert(path, key, element, None, None, gv) + .unwrap() + .expect("expected to insert"); + } + + /// A grove holding `old` at `[tree]/key1` (a tree of `tree_type`), next + /// to an `index` tree with a `value` tree under it and a `zindex` tree + /// that sorts after `tree`, and, `with_reference`, a reference to it at + /// `[index, value]/reference`, deeper than its target as a Drive index + /// reference is. + fn grove_with( + old: &Element, + tree_type: TreeType, + with_reference: bool, + grove_version: &GroveVersion, + ) -> TempGroveDb { + grove_with_reference( + old, + tree_type, + with_reference.then(|| Element::new_reference(target())), + Element::empty_tree(), + grove_version, + ) + } + + /// [`grove_with`], with `value_tree` as the `[index]/value` tree and + /// `reference`, if any, as the deeper reference. + fn grove_with_reference( + old: &Element, + tree_type: TreeType, + reference: Option, + value_tree: Element, + grove_version: &GroveVersion, + ) -> TempGroveDb { + let db = make_empty_grovedb(); + let tree = match tree_type { + TreeType::SumTree => Element::empty_sum_tree(), + TreeType::CountSumTree => Element::empty_count_sum_tree(), + TreeType::ProvableSumTree => Element::empty_provable_sum_tree(), + TreeType::ProvableCountProvableSumTree => { + Element::empty_provable_count_provable_sum_tree() + } + _ => Element::empty_tree(), + }; + insert(&db, &[], b"tree", tree, grove_version); + insert(&db, &[b"tree"], KEY, old.clone(), grove_version); + insert(&db, &[], b"index", Element::empty_tree(), grove_version); + insert(&db, &[b"index"], b"value", value_tree, grove_version); + insert(&db, &[], b"zindex", Element::empty_tree(), grove_version); + if let Some(reference) = reference { + insert( + &db, + &[b"index", b"value"], + b"reference", + reference, + grove_version, + ); + } + db + } + + fn refresh_op(trusted: bool) -> QualifiedGroveDbOp { + QualifiedGroveDbOp::refresh_reference_op( + vec![b"index".to_vec(), b"value".to_vec()], + b"reference".to_vec(), + target(), + None, + None, + false, + trusted, + ) + } + + fn reference_op(path: Vec>, key: &[u8]) -> QualifiedGroveDbOp { + QualifiedGroveDbOp::insert_or_replace_op( + path, + key.to_vec(), + Element::new_reference(target()), + ) + } + + /// The ways a batch rewrites the references to the element it updates: + /// whether the grove holds the deeper reference (which a way then + /// rewrites too), and how many of the references resolve before the + /// batch writes the element. + fn reference_ops() -> Vec<(&'static str, Vec, bool, usize)> { + vec![ + ( + "an untrusted refresh of a deeper reference", + vec![refresh_op(false)], + true, + 1, + ), + ( + "a trusted refresh of a deeper reference", + vec![refresh_op(true)], + true, + 1, + ), + ( + "a rewrite of a deeper reference", + vec![reference_op( + vec![b"index".to_vec(), b"value".to_vec()], + b"reference", + )], + true, + 1, + ), + ( + "a refresh and a new reference in a tree before the target's", + vec![ + refresh_op(true), + reference_op(vec![b"index".to_vec()], b"another"), + ], + true, + 2, + ), + ( + "a refresh and a new reference at the root", + vec![refresh_op(true), reference_op(vec![], b"root reference")], + true, + 1, + ), + ( + "a new reference at the root", + vec![reference_op(vec![], b"root reference")], + false, + 0, + ), + ( + "a new reference in a tree after the target's", + vec![reference_op(vec![b"zindex".to_vec()], b"another")], + false, + 0, + ), + ] + } + + /// Updates whose stored flags are not the new element's as written, nor + /// the old element's: the flags update merges them. The sum item cases + /// are the shape Drive writes for a document of a summable type (the + /// primary-storage item next to a deeper index reference to it). + fn flag_changing_updates() -> Vec<(&'static str, Element, Element, TreeType)> { + let mut updates = vec![]; + for growth in [1, 3, 25] { + updates.push(( + "an item with a sum item, same owner, grown in a later epoch", + Element::new_item_with_sum_item_with_flags( + vec![7; 20], + 5, + owned_flags(0, OLD_OWNER), + ), + Element::new_item_with_sum_item_with_flags( + vec![8; 20 + growth], + 9, + owned_flags(2, OLD_OWNER), + ), + TreeType::SumTree, + )); + } + updates.extend([ + ( + "a sum item gaining an owner in the same epoch", + Element::new_sum_item_with_flags(5, unowned_flags(0)), + Element::new_sum_item_with_flags(900, owned_flags(0, NEW_OWNER)), + TreeType::SumTree, + ), + ( + "a sum item gaining an owner in a later epoch", + Element::new_sum_item_with_flags(5, unowned_flags(0)), + Element::new_sum_item_with_flags(900, owned_flags(2, NEW_OWNER)), + TreeType::SumTree, + ), + ( + "an item, same owner, grown in a later epoch", + Element::new_item_with_flags(vec![7; 20], owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(vec![8; 45], owned_flags(2, OLD_OWNER)), + TreeType::NormalTree, + ), + ( + "an item gaining an owner in a later epoch", + Element::new_item_with_flags(vec![7; 20], unowned_flags(0)), + Element::new_item_with_flags(vec![8; 45], owned_flags(2, NEW_OWNER)), + TreeType::NormalTree, + ), + ]); + updates + } + + fn issues(db: &TempGroveDb, grove_version: &GroveVersion) -> usize { + db.visualize_verify_grovedb(None, true, false, grove_version) + .expect("expected to verify the grove") + .len() + } + + /// Applies `new` and `reference_ops` to a fresh grove holding `old` (and, + /// `with_reference`, the deeper reference), and counts the stale + /// references. + fn stale_references( + old: &Element, + new: &Element, + tree_type: TreeType, + reference_ops: Vec, + with_reference: bool, + 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)?; + Ok(issues(&db, grove_version)) + } + + #[test] + fn a_reference_rewritten_in_the_same_batch_commits_to_the_stored_bytes() { + let grove_version = GroveVersion::latest(); + for (label, old, new, tree_type) in flag_changing_updates() { + for (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" + ); + } + } + } + + /// The trees and writes Drive uses for a document of a summable type + /// that [`flag_changing_updates`] does not: a provable sum or count-sum + /// primary tree, a `NonCounted` target in a count-sum tree, and a + /// target holding bytes of two epochs shrunk in a later one (which + /// sections the freed bytes during the prediction). + fn drive_shaped_updates() -> Vec<(&'static str, Element, Element, TreeType)> { + let item = |value: u8, len: usize, sum: i64, flags| { + Element::new_item_with_sum_item_with_flags(vec![value; len], sum, flags) + }; + let two_epochs = Some( + StorageFlags::MultiEpochOwned(0, BTreeMap::from([(1, 10)]), OLD_OWNER) + .to_element_flags(), + ); + let mut updates = vec![]; + for tree_type in [ + TreeType::SumTree, + TreeType::CountSumTree, + TreeType::ProvableSumTree, + TreeType::ProvableCountProvableSumTree, + ] { + updates.push(( + "grown in a later epoch", + item(7, 20, 5, owned_flags(0, OLD_OWNER)), + item(8, 23, 9, owned_flags(2, OLD_OWNER)), + tree_type, + )); + updates.push(( + "holding two epochs, shrunk in a later epoch", + item(7, 30, 5, two_epochs.clone()), + item(8, 22, 9, owned_flags(3, OLD_OWNER)), + tree_type, + )); + } + let non_counted = |element| Element::new_non_counted(element).expect("a NonCounted item"); + updates.push(( + "NonCounted, grown in a later epoch", + non_counted(item(7, 20, 5, owned_flags(0, OLD_OWNER))), + non_counted(item(8, 23, 9, owned_flags(2, OLD_OWNER))), + TreeType::CountSumTree, + )); + updates + } + + /// The deeper index reference as Drive writes it for a summable index: + /// a flagged `ReferenceWithSumItem` in a sum tree, refreshed (trusted or + /// not) or rewritten in the same batch as its target. + fn drive_shaped_reference_ops() -> Vec<(&'static str, QualifiedGroveDbOp)> { + let path = vec![b"index".to_vec(), b"value".to_vec()]; + let refresh = |trusted| { + QualifiedGroveDbOp::refresh_reference_with_sum_item_op( + path.clone(), + b"reference".to_vec(), + target(), + None, + 9, + owned_flags(2, OLD_OWNER), + false, + trusted, + ) + }; + vec![ + ("an untrusted refresh", refresh(false)), + ("a trusted refresh", refresh(true)), + ( + "a rewrite", + QualifiedGroveDbOp::insert_or_replace_op( + path.clone(), + b"reference".to_vec(), + Element::new_reference_with_sum_item_with_flags( + target(), + 9, + owned_flags(2, OLD_OWNER), + ), + ), + ), + ] + } + + #[test] + fn a_drive_shaped_reference_commits_to_the_stored_bytes() { + let grove_version = GroveVersion::latest(); + for (label, old, new, tree_type) in drive_shaped_updates() { + for (reference_label, reference_op) in drive_shaped_reference_ops() { + let db = grove_with_reference( + &old, + tree_type, + Some(Element::new_reference_with_sum_item_with_flags( + target(), + 5, + owned_flags(0, OLD_OWNER), + )), + Element::empty_sum_tree(), + grove_version, + ); + apply(&db, vec![write_op(&new), reference_op], grove_version).unwrap_or_else(|e| { + panic!("{label} in {tree_type:?}, {reference_label}: expected to apply: {e}") + }); + assert_eq!( + issues(&db, grove_version), + 0, + "{label} in {tree_type:?}, {reference_label}: a reference is stale" + ); + } + } + } + + /// A flags update that answers the apply otherwise than the prediction + /// would leave the reference committed to bytes the target does not + /// hold, so the batch is refused once the target is written. + #[test] + fn a_target_storing_other_bytes_than_its_prediction_refuses_the_batch() { + let grove_version = GroveVersion::latest(); + let old = Element::new_item_with_flags(b"v".to_vec(), Some(vec![0])); + let new = Element::new_item_with_flags(b"w".to_vec(), Some(vec![0])); + let db = grove_with(&old, TreeType::NormalTree, true, grove_version); + let mut calls = 0u8; + let error = db + .apply_batch_with_element_flags_update( + vec![write_op(&new), refresh_op(false)], + Some(BatchApplyOptions::default()), + |_cost: &StorageCost, _old_flags: Option>, new_flags: &mut Vec| { + calls += 1; + *new_flags = vec![calls]; + Ok(true) + }, + |_flags: &mut Vec, removed_key_bytes, removed_value_bytes| { + Ok(( + BasicStorageRemoval(removed_key_bytes), + BasicStorageRemoval(removed_value_bytes), + )) + }, + None, + grove_version, + ) + .cost_as_result() + .expect_err("expected the batch to be refused"); + assert!( + error.to_string().contains("predicted for a reference"), + "unexpected error {error}" + ); + } + + /// Whether a proof of the deeper reference verifies against the grove's + /// root hash. + fn reference_proof_verifies(db: &TempGroveDb, grove_version: &GroveVersion) -> bool { + let query = PathQuery::new_single_key( + vec![b"index".to_vec(), b"value".to_vec()], + b"reference".to_vec(), + ); + let Ok(proof) = db.prove_query(&query, None, grove_version).unwrap() else { + return false; + }; + let Ok((root_hash, results)) = GroveDb::verify_query(&proof, &query, grove_version) else { + return false; + }; + results.len() == 1 + && root_hash + == db + .root_hash(None, grove_version) + .unwrap() + .expect("expected the root hash") + } + + /// A proof read through the reference, which a stale reference fails, + /// verifies once the batch has updated its target in a later epoch; on + /// grove v1..v3 (consensus-locked) it does not. + #[test] + fn a_proof_through_a_same_batch_reference_verifies() { + let old = + Element::new_item_with_sum_item_with_flags(vec![7; 20], 5, owned_flags(0, OLD_OWNER)); + let new = + Element::new_item_with_sum_item_with_flags(vec![8; 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"); + assert_eq!( + reference_proof_verifies(&db, grove_version), + verifies, + "grove protocol version {}", + grove_version.protocol_version + ); + } + } + + /// The prediction also holds for a hand-built version pairing it with + /// Merk's `put_value` version 0, under which an ordinary put keeps the + /// value-defined cost the stored node was loaded with. + #[test] + fn a_reference_predicted_under_merk_put_value_0_commits_to_the_stored_bytes() { + 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() { + 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" + ); + } + } + } + + /// 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). + #[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, + ) + .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" + ); + } + } + } + + /// A sum item replaced by an item, or the reverse, applies with a + /// same-batch reference to it as it does alone. + #[test] + fn a_sum_item_and_an_item_replace_each_other_under_a_same_batch_reference() { + let swaps = [ + ( + Element::new_sum_item_with_flags(5, owned_flags(0, OLD_OWNER)), + Element::new_item_with_flags(b"x".to_vec(), owned_flags(1, OLD_OWNER)), + ), + ( + Element::new_item_with_flags(b"x".to_vec(), owned_flags(0, OLD_OWNER)), + Element::new_sum_item_with_flags(5, owned_flags(1, OLD_OWNER)), + ), + ]; + for (old, new) in swaps { + for (reference_label, reference_ops, with_reference, _) in reference_ops() { + let stale = stale_references( + &old, + &new, + TreeType::SumTree, + reference_ops, + with_reference, + GroveVersion::latest(), + ) + .unwrap_or_else(|e| panic!("{old:?} -> {new:?}, {reference_label}: {e}")); + assert_eq!(stale, 0, "{old:?} -> {new:?}, {reference_label}"); + } + // grove v1..v3 refuse it when the reference resolves first + // (consensus-locked) + let error = stale_references( + &old, + &new, + TreeType::SumTree, + vec![refresh_op(true)], + true, + &GROVE_V3, + ) + .expect_err("expected the legacy refusal"); + assert!( + error.to_string().contains("is not supported"), + "unexpected error {error}" + ); + } + } + + /// A deterministic flags update that stamps the measured storage change + /// into the new flags, so the stored bytes depend on every measurement + /// the update takes. + fn stamp_costs( + cost: &StorageCost, + _old_flags: Option>, + new_flags: &mut Vec, + ) -> Result { + let removed = match cost.removed_bytes { + BasicStorageRemoval(bytes) => bytes, + _ => 0, + }; + let mut stamp = cost.added_bytes.to_be_bytes().to_vec(); + stamp.extend(removed.to_be_bytes()); + stamp.extend(cost.replaced_bytes.to_be_bytes()); + if *new_flags == stamp { + return Ok(false); + } + *new_flags = stamp; + Ok(true) + } + + /// A reference that resolves after the batch has written its target + /// commits to the bytes the target stores, whatever the flags update. + #[test] + fn a_reference_to_a_target_written_earlier_in_the_batch_commits_to_its_stored_bytes() { + let grove_version = GroveVersion::latest(); + for (target_path, reference_path) in [ + (vec![b"a".to_vec(), b"b".to_vec()], vec![b"c".to_vec()]), + (vec![b"a".to_vec()], vec![b"c".to_vec(), b"d".to_vec()]), + ] { + let db = make_empty_grovedb(); + insert(&db, &[], b"a", Element::empty_tree(), grove_version); + insert(&db, &[b"a"], b"b", Element::empty_tree(), grove_version); + insert(&db, &[], b"c", Element::empty_tree(), grove_version); + insert(&db, &[b"c"], b"d", Element::empty_tree(), grove_version); + let path: Vec<&[u8]> = target_path.iter().map(Vec::as_slice).collect(); + insert( + &db, + &path, + b"k", + Element::new_item_with_flags(b"v".to_vec(), Some(vec![0])), + grove_version, + ); + let mut qualified_target = target_path.clone(); + qualified_target.push(b"k".to_vec()); + db.apply_batch_with_element_flags_update( + vec![ + QualifiedGroveDbOp::replace_op( + target_path.clone(), + b"k".to_vec(), + Element::new_item_with_flags(b"vv".to_vec(), Some(vec![0])), + ), + QualifiedGroveDbOp::insert_or_replace_op( + reference_path.clone(), + b"r".to_vec(), + Element::new_reference(ReferencePathType::AbsolutePathReference( + qualified_target, + )), + ), + ], + None, + stamp_costs, + |_flags, removed_key_bytes, removed_value_bytes| { + Ok(( + BasicStorageRemoval(removed_key_bytes), + BasicStorageRemoval(removed_value_bytes), + )) + }, + None, + grove_version, + ) + .cost_as_result() + .expect("expected the batch to apply"); + assert_eq!( + issues(&db, grove_version), + 0, + "target at {target_path:?}, reference at {reference_path:?}" + ); + } + } + + /// A reference a partial batch's add-on writes to an item its initial + /// segment updated commits to the bytes that item stores. + #[test] + fn an_add_on_reference_commits_to_the_bytes_the_initial_segment_stored() { + let grove_version = GroveVersion::latest(); + let old = + Element::new_item_with_sum_item_with_flags(vec![7; 20], 5, owned_flags(0, OLD_OWNER)); + let new = + Element::new_item_with_sum_item_with_flags(vec![8; 21], 9, owned_flags(2, OLD_OWNER)); + 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); + } + + /// A partial batch whose initial segment updates a flagged item and + /// whose add-on rewrites that item together with the deeper reference to + /// it. The reference sits deeper, so the add-on resolves it before it + /// writes the item again: it has to predict that second write rather + /// than read the bytes the initial segment stored. The item ends exactly + /// as the two updates applied as separate batches leave it, and every + /// reference verifies. + #[test] + fn an_add_on_rewriting_an_initial_segment_target_predicts_its_new_bytes() { + let grove_version = GroveVersion::latest(); + let item = |value: u8, len: usize, sum: i64, flags| { + Element::new_item_with_sum_item_with_flags(vec![value; len], sum, flags) + }; + let old = item(7, 20, 5, owned_flags(0, OLD_OWNER)); + let first = item(8, 21, 9, owned_flags(1, OLD_OWNER)); + let 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, + 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}" + ); + } + } + + /// A partial batch whose initial segment inserts a flagged item at a new + /// key and whose add-on rewrites it together with a deeper reference to + /// it. The Merk still holds the inserted node with no stored value to + /// replace, so the rewrite stores its bytes as given, without the flags + /// update; the reference commits to those bytes. + #[test] + fn an_add_on_rewriting_an_item_the_initial_segment_inserted_commits_to_its_bytes() { + let grove_version = GroveVersion::latest(); + let item = |flags| Element::new_item_with_sum_item_with_flags(vec![7; 20], 5, flags); + // The merging update would keep the epoch-0 flags over these + // same-size epoch-2 ones, had the rewrite replaced a stored value. + let inserted = item(owned_flags(0, OLD_OWNER)); + let rewritten = item(owned_flags(2, OLD_OWNER)); + let fresh_path = vec![b"tree".to_vec(), b"fresh".to_vec()]; + let write = |element: &Element| { + QualifiedGroveDbOp::insert_or_replace_op( + vec![b"tree".to_vec()], + b"fresh".to_vec(), + element.clone(), + ) + }; + let reference = QualifiedGroveDbOp::insert_or_replace_op( + vec![b"index".to_vec(), b"value".to_vec()], + 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); + } + + /// 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)> { + 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, + ), + ( + "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, + ), + ( + "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, + ), + ] + } + + fn replace( + db: &TempGroveDb, + new: &Element, + merging: bool, + 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() + } + } + + #[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() { + // 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) + .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) + .unwrap_or_else(|e| panic!("{label}: expected to apply: {e}")); + assert_eq!( + cost.storage_cost.replaced_bytes, + around + old_bytes.min(new_bytes), + "{label}" + ); + assert_eq!( + cost.storage_cost.added_bytes, + new_bytes.saturating_sub(old_bytes), + "{label}" + ); + assert_eq!( + cost.storage_cost.removed_bytes.total_removed_bytes(), + old_bytes.saturating_sub(new_bytes), + "{label}" + ); + assert_eq!(issues(&db, grove_version), 0, "{label}"); + } + } + + /// Grove v1..v3 keep the measurement taken with the old flags, which the + /// commit refuses (consensus-locked). + #[test] + fn grove_v3_refuses_a_replacement_keeping_flags_of_another_length() { + let grove_version = &GROVE_V3; + for (label, old, new, merging) in unchanged_flags_of_another_length() { + let db = grove_with(&old, TreeType::NormalTree, false, grove_version); + let error = replace(&db, &new, merging, grove_version) + .expect_err(&format!("{label}: expected the legacy cost mismatch")); + assert!( + error.to_string().contains("cost mismatch"), + "{label}: unexpected error {error}" + ); + } + // the same replacement with flags of the same length is accepted + let db = grove_with( + &Element::new_item_with_flags(b"v".to_vec(), Some(vec![0])), + TreeType::NormalTree, + false, + grove_version, + ); + let cost = replace( + &db, + &Element::new_item_with_flags(b"w".to_vec(), Some(vec![1])), + false, + grove_version, + ) + .expect("expected a same-length replacement to apply"); + assert_eq!(cost.storage_cost.removed_bytes, NoStorageRemoval); + } +} diff --git a/merk/src/element/insert.rs b/merk/src/element/insert.rs index 9c162c525..67e1d35df 100644 --- a/merk/src/element/insert.rs +++ b/merk/src/element/insert.rs @@ -46,6 +46,28 @@ impl Delta<'_> { } } +/// The specialized value-defined cost an ordinary write puts `element` with +/// (`Op::PutWithSpecializedCost`), or `None` when it is put as an `Op::Put`. +/// +/// A sum item takes its specialized cost; `is_sum_item` looks through the +/// `NonCounted` / `NotSummed` / `NotCountedOrSummed` wrappers, so a wrapped +/// sum item takes the same path as a bare one. +pub fn specialized_put_cost( + element: &Element, + grove_version: &GroveVersion, +) -> Result, Error> { + if element.is_sum_item() { + element + .specialized_value_defined_cost(grove_version) + .map(Some) + .ok_or(Error::CorruptedCodeExecution( + "sum items should always have a value defined cost", + )) + } else { + Ok(None) + } +} + /// Extension trait for inserting elements into Merk storage. pub trait ElementInsertToStorageExtensions { /// Whether this element may legally live in a tree of `tree_type`. @@ -365,22 +387,14 @@ impl ElementInsertToStorageExtensions for Element { .map_err(|e| Error::CorruptedData(e.to_string())) .add_cost(cost); } - // Use is_sum_item() (which looks through NonCounted) so that a - // NonCounted(SumItem(..)) takes the same specialized cost path as a - // bare SumItem(..). - let batch_operations = if self.is_sum_item() { - let cost = cost_return_on_error_default!(self - .specialized_value_defined_cost(grove_version) - .ok_or(Error::CorruptedCodeExecution( - "sum items should always have a value defined cost" - ))); - [( - key, - Op::PutWithSpecializedCost(serialized, cost, merk_feature_type), - )] - } else { - [(key, Op::Put(serialized, merk_feature_type))] - }; + let batch_operations = + match cost_return_on_error_default!(specialized_put_cost(self, grove_version)) { + Some(cost) => [( + key, + Op::PutWithSpecializedCost(serialized, cost, merk_feature_type), + )], + None => [(key, Op::Put(serialized, merk_feature_type))], + }; let tree_type = merk.tree_type; merk.apply_with_specialized_costs::<_, Vec>( &batch_operations, @@ -424,22 +438,12 @@ impl ElementInsertToStorageExtensions for Element { Err(e) => return Err(e.into()).wrap_with_cost(Default::default()), }; - // Use is_sum_item() (which looks through NonCounted) so that a - // NonCounted(SumItem(..)) takes the same specialized cost path as a - // bare SumItem(..). - let entry = if self.is_sum_item() { - let cost = cost_return_on_error_default!(self - .specialized_value_defined_cost(grove_version) - .ok_or(Error::CorruptedCodeExecution( - "sum items should always have a value defined cost" - ))); - - ( + let entry = match cost_return_on_error_default!(specialized_put_cost(self, grove_version)) { + Some(cost) => ( key, Op::PutWithSpecializedCost(serialized, cost, feature_type), - ) - } else { - (key, Op::Put(serialized, feature_type)) + ), + None => (key, Op::Put(serialized, feature_type)), }; batch_operations.push(entry); Ok(()).wrap_with_cost(Default::default()) diff --git a/merk/src/merk/get.rs b/merk/src/merk/get.rs index f1bebd456..6ab0d7b6e 100644 --- a/merk/src/merk/get.rs +++ b/merk/src/merk/get.rs @@ -4,7 +4,7 @@ use grovedb_version::version::GroveVersion; use crate::{ proofs::query::QueryItem, - tree::{kv::ValueDefinedCostType, RefWalker, TreeNode}, + tree::{kv::ValueDefinedCostType, Link, RefWalker, TreeNode}, CryptoHash, Error, Error::StorageError, Merk, TreeFeatureType, @@ -106,6 +106,29 @@ where }) } + /// Whether a write replacing the value at `key` in this Merk runs the + /// just-in-time value update, as the put functions do on a node holding + /// the value it was loaded or last updated with. A node this Merk + /// inserted and still holds in memory has none, so a write over it + /// stores its bytes as given; a node held only in storage is loaded with + /// one. Reads no storage: only the nodes already in memory are walked. + pub fn replacement_runs_value_update(&self, key: &[u8]) -> bool { + self.use_tree(|maybe_tree| { + let Some(mut node) = maybe_tree else { + return true; + }; + loop { + if node.key() == key { + return node.old_value.is_some(); + } + match node.link(key < node.key()).and_then(Link::tree) { + Some(child) => node = child, + None => return true, + } + } + }) + } + /// Gets an auxiliary value. pub fn get_aux(&self, key: &[u8]) -> CostResult>, Error> { self.storage.get_aux(key).map_err(StorageError) diff --git a/merk/src/tree/just_in_time_value_update.rs b/merk/src/tree/just_in_time_value_update.rs deleted file mode 100644 index 7e5514328..000000000 --- a/merk/src/tree/just_in_time_value_update.rs +++ /dev/null @@ -1,325 +0,0 @@ -use grovedb_costs::storage_cost::{ - removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, - StorageCost, -}; - -use crate::{ - merk::defaults::MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES, - tree::{ - kv::{ValueDefinedCostType, KV}, - TreeFeatureType, TreeNode, TreeNodeInner, NULL_HASH, - }, - Error, -}; - -impl TreeNode { - /// The value bytes an apply finally stores when an - /// [`Op::PutWithProvidedValueHash`](crate::tree::Op::PutWithProvidedValueHash) - /// of `new_value` replaces the stored `old_value` — after the - /// just-in-time value update has run the client callbacks (storage-flags - /// carry-over and rewrite). - /// - /// Runs the apply's own steps on a detached node: the node holding - /// `old_value` takes the new value and feature type exactly as - /// `put_value_with_provided_value_hash` installs them (which drops any - /// value-defined cost the predecessor carried), and then goes through - /// [`Self::just_in_time_tree_node_value_update`]. With the same - /// (deterministic) callbacks the result is byte-for-byte what the apply - /// writes, so a caller that must commit to the final bytes BEFORE the - /// apply — a backward-references referrer holding its target's hash — - /// can compute them. - #[allow(clippy::too_many_arguments)] - pub fn provided_value_hash_put_final_value( - key: Vec, - old_value: Vec, - new_value: Vec, - feature_type: TreeFeatureType, - old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, - get_temp_new_value_with_old_flags: &impl Fn( - &Vec, - &Vec, - ) -> Result>, Error>, - update_tree_value_based_on_costs: &mut impl FnMut( - &StorageCost, - &Vec, - &mut Vec, - ) -> Result< - (bool, Option), - Error, - >, - section_removal_bytes: &mut impl FnMut( - &Vec, - u32, - u32, - ) -> Result< - (StorageRemovedBytes, StorageRemovedBytes), - Error, - >, - ) -> Result, Error> { - // The stored node (no hashes are needed: the update only measures - // sizes and consults the callbacks). - let kv = KV::from_fields(key, old_value, NULL_HASH, NULL_HASH, feature_type); - let mut node = TreeNode::new_with_tree_inner(TreeNodeInner { - left: None, - right: None, - kv, - }); - // The put, as `put_value_with_provided_value_hash` performs it. - node.inner.kv = node - .inner - .kv - .put_ordinary_value_no_update_of_hashes(new_value); - node.inner.kv.feature_type = feature_type; - node.just_in_time_tree_node_value_update( - old_specialized_cost, - get_temp_new_value_with_old_flags, - update_tree_value_based_on_costs, - section_removal_bytes, - )?; - Ok(node.inner.kv.value) - } - - pub(in crate::tree) fn just_in_time_tree_node_value_update( - &mut self, - old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, - get_temp_new_value_with_old_flags: &impl Fn( - &Vec, - &Vec, - ) -> Result>, Error>, - update_tree_value_based_on_costs: &mut impl FnMut( - &StorageCost, - &Vec, - &mut Vec, - ) -> Result< - (bool, Option), - Error, - >, - section_removal_bytes: &mut impl FnMut( - &Vec, - u32, - u32, - ) -> Result< - (StorageRemovedBytes, StorageRemovedBytes), - Error, - >, - ) -> Result<(), Error> { - let mut i = 0; - - if let Some(old_value) = self.old_value.clone() { - // At this point the tree value can be updated based on client requirements - // For example to store the costs - // todo: clean up clones - let original_new_value = self.value_ref().clone(); - - let new_value_with_old_flags = if self.inner.kv.value_defined_cost.is_none() { - // for items - get_temp_new_value_with_old_flags(&old_value, &original_new_value)? - } else { - // don't do this for sum items or trees - None - }; - - let (mut current_tree_plus_hook_size, mut storage_costs) = self - .kv_with_parent_hook_size_and_storage_cost_change_for_value( - old_specialized_cost, - new_value_with_old_flags, - )?; - - loop { - if let BasicStorageRemoval(removed_bytes) = - storage_costs.value_storage_cost.removed_bytes - { - let (_, value_removed_bytes) = - section_removal_bytes(&old_value, 0, removed_bytes)?; - storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; - } - - let (flags_changed, 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; - } - // we are calling this with merged flags that are were put in through value mut - // ref - let new_size_and_storage_costs = - self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; - current_tree_plus_hook_size = new_size_and_storage_costs.0; - storage_costs = new_size_and_storage_costs.1; - self.set_value(original_new_value.clone()) - } - if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { - return Err(Error::CyclicError( - "updated value based on costs too many times", - )); - } - i += 1; - } - - if let BasicStorageRemoval(removed_bytes) = - storage_costs.value_storage_cost.removed_bytes - { - let (_, value_removed_bytes) = section_removal_bytes(&old_value, 0, removed_bytes)?; - storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; - } - self.known_storage_cost = Some(storage_costs); - } else { - let (_, storage_costs) = - self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; - self.known_storage_cost = Some(storage_costs); - } - - self.old_value = Some(self.value_ref().clone()); - - Ok(()) - } -} - -#[cfg(all(test, feature = "full"))] -mod tests { - use grovedb_costs::storage_cost::{ - removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, - StorageCost, - }; - use grovedb_element::{BackwardReferences, Element}; - use grovedb_version::version::GroveVersion; - - use crate::{ - merk::NodeType, - test_utils::TempMerk, - tree::{kv::ValueDefinedCostType, kv::KV, TreeFeatureType::BasicMerkNode, TreeNode}, - Error, Op, - }; - - // The signature is the callback type Merk's apply takes. - #[allow(clippy::ptr_arg)] - fn old_cost(key: &Vec, value: &Vec) -> Result { - Ok(KV::node_value_byte_cost_size( - key.len() as u32, - value.len() as u32, - NodeType::NormalNode, - )) - } - - fn no_temp_value(_old: &Vec, _new: &Vec) -> Result>, Error> { - Ok(None) - } - - /// Stamps the measured storage change into the new element's flags, so - /// the final bytes depend on every size the update measures (and the - /// stamp's own length forces the re-measuring loop to run). - fn stamp_costs( - cost: &StorageCost, - _old: &Vec, - new: &mut Vec, - ) -> Result<(bool, Option), Error> { - let grove_version = GroveVersion::latest(); - let mut element = Element::deserialize(new, grove_version) - .map_err(|e| Error::ClientCorruptionError(e.to_string()))?; - let removed = match cost.removed_bytes { - BasicStorageRemoval(bytes) => bytes, - _ => 0, - }; - let mut stamp = cost.added_bytes.to_be_bytes().to_vec(); - stamp.extend(removed.to_be_bytes()); - stamp.extend(cost.replaced_bytes.to_be_bytes()); - element.set_flags(Some(stamp)); - *new = element - .serialize(grove_version) - .map_err(|e| Error::ClientCorruptionError(e.to_string()))?; - Ok((true, None)) - } - - fn basic_removal( - _value: &Vec, - key_bytes: u32, - value_bytes: u32, - ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error> { - Ok(( - BasicStorageRemoval(key_bytes), - BasicStorageRemoval(value_bytes), - )) - } - - fn apply(merk: &mut TempMerk, op: Op, grove_version: &GroveVersion) { - merk.apply_with_costs_just_in_time_value_update::<_, Vec>( - &[(b"key".to_vec(), op)], - &[], - None, - &old_cost, - None::<&fn(&[u8], &GroveVersion) -> Option>, - &no_temp_value, - &mut stamp_costs, - &mut basic_removal, - grove_version, - ) - .unwrap() - .expect("apply"); - } - - /// The detached prediction stores exactly what a real provided-hash put - /// over a stored value stores, for a growing and a shrinking write. - #[test] - fn provided_value_hash_put_final_value_matches_the_apply() { - let grove_version = GroveVersion::latest(); - let item = |value: &[u8], flags: u8| { - Element::ItemWithBackwardsReferences( - value.to_vec(), - BackwardReferences::default(), - 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)), - ] { - let mut merk = TempMerk::new(grove_version); - apply( - &mut merk, - Op::Put(old.clone(), BasicMerkNode), - grove_version, - ); - merk.commit(grove_version); - - let predicted = TreeNode::provided_value_hash_put_final_value( - b"key".to_vec(), - old.clone(), - new.clone(), - BasicMerkNode, - &old_cost, - &no_temp_value, - &mut stamp_costs, - &mut basic_removal, - ) - .expect("prediction"); - assert_ne!(predicted, new, "the callback rewrote the flags"); - - apply( - &mut merk, - Op::PutWithProvidedValueHash(new, [0; 32], None, BasicMerkNode), - grove_version, - ); - let stored = merk - .get( - b"key", - true, - None::<&fn(&[u8], &GroveVersion) -> Option>, - grove_version, - ) - .unwrap() - .unwrap() - .expect("stored"); - assert_eq!(predicted, stored); - } - } -} diff --git a/merk/src/tree/just_in_time_value_update/mod.rs b/merk/src/tree/just_in_time_value_update/mod.rs new file mode 100644 index 000000000..eb79ffd60 --- /dev/null +++ b/merk/src/tree/just_in_time_value_update/mod.rs @@ -0,0 +1,418 @@ +//! The just-in-time value update a replacement of a stored value runs: the +//! client callbacks carry the old storage flags into the size measurement, +//! rewrite the new value's flags, and section the bytes the replacement +//! frees. +//! +//! Each `v*.rs` defines a `just_in_time_tree_node_value_update_v*`. The +//! dispatcher below selects it by +//! `GroveVersion::merk_versions.tree.just_in_time_value_update`: +//! +//! - version 0 keeps the first measurement, taken with the new value +//! carrying the OLD value's flags, when the flags-update callback answers +//! that it left the flags unchanged. A new value stored with flags of a +//! different length is then charged for bytes it does not hold, and the +//! commit fails with a storage cost mismatch. Grove v1..v3 are +//! consensus-locked to this. +//! - version 1 measures the replacement again from the bytes it stores when +//! their size differs from the last measurement taken, and restores the +//! put's value-defined cost each time it restores the put's value. Every +//! update whose cost version 0 records for the bytes it stores is charged +//! exactly as version 0 charges it. + +mod v0; +mod v1; + +use grovedb_costs::storage_cost::{removal::StorageRemovedBytes, StorageCost}; +use grovedb_version::{error::GroveVersionError, version::GroveVersion}; + +use crate::{ + tree::{ + kv::{ValueDefinedCostType, KV}, + TreeFeatureType, TreeNode, TreeNodeInner, NULL_HASH, + }, + Error, +}; + +/// The put whose final bytes [`TreeNode::predict_put_final_value`] +/// predicts: how it installs the new value on the stored node, which decides +/// the value-defined cost the just-in-time value update measures with. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PredictedPut { + /// An `Op::Put`: installed as [`TreeNode::put_value`] installs an + /// ordinary value at the grove version (merk tree version 0 keeps the + /// value-defined cost the stored node was loaded with). + Ordinary, + /// An [`Op::PutWithProvidedValueHash`](crate::tree::Op::PutWithProvidedValueHash), + /// which drops any value-defined cost. + ProvidedValueHash, + /// An `Op::PutWithSpecializedCost`, at the op's own cost. + SpecializedCost(u32), +} + +impl TreeNode { + /// The value bytes an apply finally stores when `put` of `new_value` + /// replaces the stored `old_value`, after the just-in-time value update + /// has run the client callbacks (storage-flags carry-over and rewrite). + /// + /// Runs the apply's own steps on a detached node: the node holding + /// `old_value`, loaded with `old_value_defined_cost` as the apply loads + /// it, takes the new value and feature type exactly as `put` installs + /// them, and then goes through the just-in-time value update + /// (`just_in_time_tree_node_value_update`). With the same + /// (deterministic) callbacks the result is byte-for-byte what the apply + /// writes, so a caller that must commit to the final bytes BEFORE the + /// apply (a reference holding its target's hash) can compute them. + #[allow(clippy::too_many_arguments)] + pub fn predict_put_final_value( + key: Vec, + old_value: Vec, + old_value_defined_cost: Option, + new_value: Vec, + feature_type: TreeFeatureType, + put: PredictedPut, + old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + get_temp_new_value_with_old_flags: &impl Fn( + &Vec, + &Vec, + ) -> Result>, Error>, + update_tree_value_based_on_costs: &mut impl FnMut( + &StorageCost, + &Vec, + &mut Vec, + ) -> Result< + (bool, Option), + Error, + >, + section_removal_bytes: &mut impl FnMut( + &Vec, + u32, + u32, + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + grove_version: &GroveVersion, + ) -> Result, Error> { + // The stored node as loaded (no hashes are needed: the update only + // measures sizes and consults the callbacks); its value is installed + // by the put below, and the stored bytes are its old value. + let mut kv = KV::from_fields(key, Vec::new(), NULL_HASH, NULL_HASH, feature_type); + kv.value_defined_cost = old_value_defined_cost; + // The put, as the apply performs it. + let mut kv = match put { + PredictedPut::Ordinary => Self::install_ordinary_value(kv, new_value, grove_version)?, + PredictedPut::ProvidedValueHash => kv.put_ordinary_value_no_update_of_hashes(new_value), + PredictedPut::SpecializedCost(value_cost) => kv + .put_value_with_fixed_cost_no_update_of_hashes( + new_value, + ValueDefinedCostType::SpecializedValueDefinedCost(value_cost), + ), + }; + kv.feature_type = feature_type; + let mut node = TreeNode { + inner: Box::new(TreeNodeInner { + left: None, + right: None, + kv, + }), + old_value: Some(old_value), + known_storage_cost: None, + }; + node.just_in_time_tree_node_value_update( + old_specialized_cost, + get_temp_new_value_with_old_flags, + update_tree_value_based_on_costs, + section_removal_bytes, + grove_version, + )?; + Ok(node.inner.kv.value) + } + + /// Runs the just-in-time value update on a node that has taken a new + /// value over a stored one. See the module docs for the version + /// semantics. + pub(in crate::tree) fn just_in_time_tree_node_value_update( + &mut self, + old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + get_temp_new_value_with_old_flags: &impl Fn( + &Vec, + &Vec, + ) -> Result>, Error>, + update_tree_value_based_on_costs: &mut impl FnMut( + &StorageCost, + &Vec, + &mut Vec, + ) -> Result< + (bool, Option), + Error, + >, + section_removal_bytes: &mut impl FnMut( + &Vec, + u32, + u32, + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + grove_version: &GroveVersion, + ) -> Result<(), Error> { + match grove_version.merk_versions.tree.just_in_time_value_update { + 0 => self.just_in_time_tree_node_value_update_v0( + old_specialized_cost, + get_temp_new_value_with_old_flags, + update_tree_value_based_on_costs, + section_removal_bytes, + ), + 1 => self.just_in_time_tree_node_value_update_v1( + old_specialized_cost, + get_temp_new_value_with_old_flags, + update_tree_value_based_on_costs, + section_removal_bytes, + ), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "just_in_time_tree_node_value_update".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )), + } + } +} + +#[cfg(all(test, feature = "full"))] +mod tests { + use grovedb_costs::storage_cost::{ + removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + StorageCost, + }; + use grovedb_element::{BackwardReferences, Element}; + use grovedb_version::version::GroveVersion; + + use crate::{ + element::insert::specialized_put_cost, + merk::NodeType, + test_utils::TempMerk, + tree::{ + just_in_time_value_update::PredictedPut, kv::ValueDefinedCostType, kv::KV, + TreeFeatureType::BasicMerkNode, TreeNode, TreeNodeInner, NULL_HASH, + }, + Error, Op, + }; + + // The signature is the callback type Merk's apply takes. + #[allow(clippy::ptr_arg)] + fn old_cost(key: &Vec, value: &Vec) -> Result { + Ok(KV::node_value_byte_cost_size( + key.len() as u32, + value.len() as u32, + NodeType::NormalNode, + )) + } + + fn no_temp_value(_old: &Vec, _new: &Vec) -> Result>, Error> { + Ok(None) + } + + /// Stamps the measured storage change into the new element's flags, so + /// the final bytes depend on every size the update measures (and the + /// stamp's own length forces the re-measuring loop to run). + fn stamp_costs( + cost: &StorageCost, + _old: &Vec, + new: &mut Vec, + ) -> Result<(bool, Option), Error> { + let grove_version = GroveVersion::latest(); + let mut element = Element::deserialize(new, grove_version) + .map_err(|e| Error::ClientCorruptionError(e.to_string()))?; + let removed = match cost.removed_bytes { + BasicStorageRemoval(bytes) => bytes, + _ => 0, + }; + let mut stamp = cost.added_bytes.to_be_bytes().to_vec(); + stamp.extend(removed.to_be_bytes()); + stamp.extend(cost.replaced_bytes.to_be_bytes()); + element.set_flags(Some(stamp)); + *new = element + .serialize(grove_version) + .map_err(|e| Error::ClientCorruptionError(e.to_string()))?; + Ok((true, None)) + } + + fn basic_removal( + _value: &Vec, + key_bytes: u32, + value_bytes: u32, + ) -> Result<(StorageRemovedBytes, StorageRemovedBytes), Error> { + Ok(( + BasicStorageRemoval(key_bytes), + BasicStorageRemoval(value_bytes), + )) + } + + fn apply(merk: &mut TempMerk, op: Op, grove_version: &GroveVersion) { + merk.apply_with_costs_just_in_time_value_update::<_, Vec>( + &[(b"key".to_vec(), op)], + &[], + None, + &old_cost, + None::<&fn(&[u8], &GroveVersion) -> Option>, + &no_temp_value, + &mut stamp_costs, + &mut basic_removal, + grove_version, + ) + .unwrap() + .expect("apply"); + } + + /// The detached prediction stores exactly what a real provided-hash put + /// over a stored value stores, for a growing and a shrinking write. + #[test] + fn predicted_provided_value_hash_put_matches_the_apply() { + let grove_version = GroveVersion::latest(); + let item = |value: &[u8], flags: u8| { + Element::ItemWithBackwardsReferences( + value.to_vec(), + BackwardReferences::default(), + 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)), + ] { + let mut merk = TempMerk::new(grove_version); + apply( + &mut merk, + Op::Put(old.clone(), BasicMerkNode), + grove_version, + ); + merk.commit(grove_version); + + let predicted = TreeNode::predict_put_final_value( + b"key".to_vec(), + old.clone(), + None, + new.clone(), + BasicMerkNode, + PredictedPut::ProvidedValueHash, + &old_cost, + &no_temp_value, + &mut stamp_costs, + &mut basic_removal, + grove_version, + ) + .expect("prediction"); + assert_ne!(predicted, new, "the callback rewrote the flags"); + + apply( + &mut merk, + Op::PutWithProvidedValueHash(new, [0; 32], None, BasicMerkNode), + grove_version, + ); + let stored = merk + .get( + b"key", + true, + None::<&fn(&[u8], &GroveVersion) -> Option>, + grove_version, + ) + .unwrap() + .unwrap() + .expect("stored"); + assert_eq!(predicted, stored); + } + } + + /// 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 + /// answer: the sum item it stores is measured with its own value-defined + /// cost, not the rewrite's. + #[test] + fn a_changed_round_then_unchanged_measures_the_value_it_stores() { + let grove_version = GroveVersion::latest(); + let key = b"key".to_vec(); + let sum_item = + |sum: i64, flags: Vec| Element::new_sum_item_with_flags(sum, Some(flags)); + let old = sum_item(5, vec![1]).serialize(grove_version).unwrap(); + let new_element = sum_item(7, vec![2]); + let new = new_element.serialize(grove_version).unwrap(); + let new_cost = specialized_put_cost(&new_element, grove_version) + .unwrap() + .expect("a sum item has a specialized cost"); + let rewritten_element = sum_item(7, vec![2; 9]); + let rewritten = rewritten_element.serialize(grove_version).unwrap(); + let rewritten_cost = specialized_put_cost(&rewritten_element, grove_version) + .unwrap() + .expect("a sum item has a specialized cost"); + assert_ne!(new_cost, rewritten_cost); + + let putting_new = || { + let kv = KV::from_fields( + key.clone(), + old.clone(), + NULL_HASH, + NULL_HASH, + BasicMerkNode, + ); + let mut node = TreeNode::new_with_tree_inner(TreeNodeInner { + left: None, + right: None, + kv, + }); + node.inner.kv = node.inner.kv.put_value_with_fixed_cost_no_update_of_hashes( + new.clone(), + ValueDefinedCostType::SpecializedValueDefinedCost(new_cost), + ); + node + }; + + let mut at_once = putting_new(); + at_once + .just_in_time_tree_node_value_update( + &old_cost, + &no_temp_value, + &mut |_cost: &StorageCost, _old: &Vec, _new: &mut Vec| Ok((false, None)), + &mut basic_removal, + grove_version, + ) + .expect("expected the update to apply"); + + let mut rounds = 0; + let mut retried = putting_new(); + retried + .just_in_time_tree_node_value_update( + &old_cost, + &no_temp_value, + &mut |_cost: &StorageCost, _old: &Vec, value: &mut Vec| { + rounds += 1; + if rounds == 1 { + *value = rewritten.clone(); + Ok(( + true, + Some(ValueDefinedCostType::SpecializedValueDefinedCost( + rewritten_cost, + )), + )) + } else { + Ok((false, None)) + } + }, + &mut basic_removal, + grove_version, + ) + .expect("expected the update to apply"); + + assert_eq!(rounds, 2, "the resized rewrite must force a second round"); + assert_eq!(retried.inner.kv.value, new); + assert_eq!( + retried.inner.kv.value_defined_cost, + Some(ValueDefinedCostType::SpecializedValueDefinedCost(new_cost)) + ); + assert_eq!(retried.known_storage_cost, at_once.known_storage_cost); + } +} diff --git a/merk/src/tree/just_in_time_value_update/v0.rs b/merk/src/tree/just_in_time_value_update/v0.rs new file mode 100644 index 000000000..361db9c96 --- /dev/null +++ b/merk/src/tree/just_in_time_value_update/v0.rs @@ -0,0 +1,122 @@ +use grovedb_costs::storage_cost::{ + removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + StorageCost, +}; + +use crate::{ + merk::defaults::MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES, + tree::{kv::ValueDefinedCostType, TreeNode}, + Error, +}; + +impl TreeNode { + /// Version 0 of the just-in-time value update, selected by + /// `merk_versions.tree.just_in_time_value_update == 0` (grove v1..v3). + /// + /// It differs from version 1 only in what an unchanged answer of the + /// flags update keeps: the measurement taken with the new value carrying + /// the OLD value's flags, whatever the length of the flags the new value + /// is stored with. + pub(super) fn just_in_time_tree_node_value_update_v0( + &mut self, + old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + get_temp_new_value_with_old_flags: &impl Fn( + &Vec, + &Vec, + ) -> Result>, Error>, + update_tree_value_based_on_costs: &mut impl FnMut( + &StorageCost, + &Vec, + &mut Vec, + ) -> Result< + (bool, Option), + Error, + >, + section_removal_bytes: &mut impl FnMut( + &Vec, + u32, + u32, + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + ) -> Result<(), Error> { + let mut i = 0; + + if let Some(old_value) = self.old_value.clone() { + // At this point the tree value can be updated based on client requirements + // For example to store the costs + // todo: clean up clones + let original_new_value = self.value_ref().clone(); + + let new_value_with_old_flags = if self.inner.kv.value_defined_cost.is_none() { + // for items + get_temp_new_value_with_old_flags(&old_value, &original_new_value)? + } else { + // don't do this for sum items or trees + None + }; + + let (mut current_tree_plus_hook_size, mut storage_costs) = self + .kv_with_parent_hook_size_and_storage_cost_change_for_value( + old_specialized_cost, + new_value_with_old_flags, + )?; + + loop { + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = + section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + + let (flags_changed, 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; + } + // we are calling this with merged flags that are were put in through value mut + // ref + let new_size_and_storage_costs = + self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; + current_tree_plus_hook_size = new_size_and_storage_costs.0; + storage_costs = new_size_and_storage_costs.1; + self.set_value(original_new_value.clone()) + } + if i > MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES { + return Err(Error::CyclicError( + "updated value based on costs too many times", + )); + } + i += 1; + } + + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + self.known_storage_cost = Some(storage_costs); + } else { + let (_, storage_costs) = + self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; + self.known_storage_cost = Some(storage_costs); + } + + self.old_value = Some(self.value_ref().clone()); + + Ok(()) + } +} diff --git a/merk/src/tree/just_in_time_value_update/v1.rs b/merk/src/tree/just_in_time_value_update/v1.rs new file mode 100644 index 000000000..7a51e11c2 --- /dev/null +++ b/merk/src/tree/just_in_time_value_update/v1.rs @@ -0,0 +1,145 @@ +use grovedb_costs::storage_cost::{ + removal::{StorageRemovedBytes, StorageRemovedBytes::BasicStorageRemoval}, + StorageCost, +}; + +use crate::{ + merk::defaults::MAX_UPDATE_VALUE_BASED_ON_COSTS_TIMES, + tree::{kv::ValueDefinedCostType, TreeNode}, + Error, +}; + +impl TreeNode { + /// Version 1 of the just-in-time value update, selected by + /// `merk_versions.tree.just_in_time_value_update == 1` (grove v4+). + /// + /// It differs from version 0 only in what an unchanged answer of the + /// flags update keeps: when the value the update stores is not the size + /// the last measurement took (the first one sizes the new value with the + /// OLD value's flags), the replacement is measured again from the stored + /// bytes, so the recorded cost is the cost of what is written. For that, + /// a changed round that restores the put's value restores the put's + /// value-defined cost with it, so a sum item or tree is measured with + /// the cost of the bytes it holds. The stored value's own cost is also + /// measured once rather than on every measurement, which leaves every + /// figure unchanged. + pub(super) fn just_in_time_tree_node_value_update_v1( + &mut self, + old_specialized_cost: &impl Fn(&Vec, &Vec) -> Result, + get_temp_new_value_with_old_flags: &impl Fn( + &Vec, + &Vec, + ) -> Result>, Error>, + update_tree_value_based_on_costs: &mut impl FnMut( + &StorageCost, + &Vec, + &mut Vec, + ) -> Result< + (bool, Option), + Error, + >, + section_removal_bytes: &mut impl FnMut( + &Vec, + u32, + u32, + ) -> Result< + (StorageRemovedBytes, StorageRemovedBytes), + Error, + >, + ) -> Result<(), Error> { + let mut i = 0; + + if let Some(old_value) = self.old_value.clone() { + // At this point the tree value can be updated based on client requirements + // For example to store the costs + // todo: clean up clones + let original_new_value = self.value_ref().clone(); + let original_value_defined_cost = self.inner.kv.value_defined_cost.clone(); + + // The stored value does not change while the update runs, so its + // cost is measured once. + let old_value_bytes = old_specialized_cost(self.key_as_ref(), &old_value)?; + let old_cost = + |_key: &Vec, _value: &Vec| -> Result { Ok(old_value_bytes) }; + + let new_value_with_old_flags = if self.inner.kv.value_defined_cost.is_none() { + // for items + get_temp_new_value_with_old_flags(&old_value, &original_new_value)? + } else { + // don't do this for sum items or trees + None + }; + + let (mut current_tree_plus_hook_size, mut storage_costs) = self + .kv_with_parent_hook_size_and_storage_cost_change_for_value( + &old_cost, + new_value_with_old_flags, + )?; + + loop { + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = + section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + + let (flags_changed, 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 { + 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( + "updated value based on costs too many times", + )); + } + i += 1; + } + + if let BasicStorageRemoval(removed_bytes) = + storage_costs.value_storage_cost.removed_bytes + { + let (_, value_removed_bytes) = section_removal_bytes(&old_value, 0, removed_bytes)?; + storage_costs.value_storage_cost.removed_bytes = value_removed_bytes; + } + self.known_storage_cost = Some(storage_costs); + } else { + let (_, storage_costs) = + self.kv_with_parent_hook_size_and_storage_cost(old_specialized_cost)?; + self.known_storage_cost = Some(storage_costs); + } + + self.old_value = Some(self.value_ref().clone()); + + Ok(()) + } +} diff --git a/merk/src/tree/mod.rs b/merk/src/tree/mod.rs index db18ac766..ea9e0d461 100644 --- a/merk/src/tree/mod.rs +++ b/merk/src/tree/mod.rs @@ -15,6 +15,8 @@ mod iter; #[cfg(feature = "minimal")] mod just_in_time_value_update; #[cfg(feature = "minimal")] +pub use just_in_time_value_update::PredictedPut; +#[cfg(feature = "minimal")] pub mod kv; #[cfg(feature = "minimal")] mod link; @@ -1289,6 +1291,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); self.inner.kv = self.inner.kv.put_value_with_fixed_cost_no_update_of_hashes( @@ -1307,7 +1310,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -1376,7 +1380,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); // The provided hash was computed over the bytes supplied by the @@ -1512,6 +1517,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -1528,7 +1534,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -1571,6 +1578,7 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); @@ -1590,7 +1598,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } diff --git a/merk/src/tree/ops.rs b/merk/src/tree/ops.rs index d0f4b8c9b..29fceeaa2 100644 --- a/merk/src/tree/ops.rs +++ b/merk/src/tree/ops.rs @@ -640,7 +640,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ) } @@ -688,6 +689,7 @@ where get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ) ) } @@ -717,6 +719,7 @@ where get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ) ) } diff --git a/merk/src/tree/put_value/mod.rs b/merk/src/tree/put_value/mod.rs index 1f81e3377..039d57716 100644 --- a/merk/src/tree/put_value/mod.rs +++ b/merk/src/tree/put_value/mod.rs @@ -26,7 +26,10 @@ use grovedb_costs::{storage_cost::removal::StorageRemovedBytes, CostResult, Cost use grovedb_version::{error::GroveVersionError, version::GroveVersion}; use crate::{ - tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, + tree::{ + kv::{ValueDefinedCostType, KV}, + CryptoHash, TreeFeatureType, TreeNode, + }, Error, }; @@ -70,6 +73,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), 1 => self.put_value_v1( value, @@ -78,6 +82,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), version => Err(Error::VersionError( GroveVersionError::UnknownVersionMismatch { @@ -131,6 +136,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), 1 => self.put_value_and_reference_value_hash_v1( value, @@ -140,6 +146,7 @@ impl TreeNode { get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, section_removal_bytes, + grove_version, ), version => Err(Error::VersionError( GroveVersionError::UnknownVersionMismatch { @@ -151,6 +158,28 @@ impl TreeNode { .wrap_with_cost(Default::default()), } } + + /// Installs an ordinary value on a node exactly as [`Self::put_value`] + /// does, without the just-in-time value update or hashing: what a + /// prediction of the bytes an `Op::Put` finally stores must start from. + /// See the module docs for the version semantics. + pub(in crate::tree) fn install_ordinary_value( + kv: KV, + value: Vec, + grove_version: &GroveVersion, + ) -> Result { + match grove_version.merk_versions.tree.put_value { + 0 => Ok(Self::install_ordinary_value_v0(kv, value)), + 1 => Ok(Self::install_ordinary_value_v1(kv, value)), + version => Err(Error::VersionError( + GroveVersionError::UnknownVersionMismatch { + method: "install_ordinary_value".to_string(), + known_versions: vec![0, 1], + received: version, + }, + )), + } + } } #[cfg(test)] diff --git a/merk/src/tree/put_value/v0.rs b/merk/src/tree/put_value/v0.rs index a77491231..e0308b871 100644 --- a/merk/src/tree/put_value/v0.rs +++ b/merk/src/tree/put_value/v0.rs @@ -9,12 +9,23 @@ use grovedb_costs::{ CostResult, CostsExt, OperationCost, }; +use grovedb_version::version::GroveVersion; + use crate::{ - tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, + tree::{ + kv::{ValueDefinedCostType, KV}, + CryptoHash, TreeFeatureType, TreeNode, + }, Error, }; impl TreeNode { + /// How version 0 installs an ordinary value on a node: it keeps the + /// `value_defined_cost` the node was loaded with. + pub(super) fn install_ordinary_value_v0(kv: KV, value: Vec) -> KV { + kv.put_value_no_update_of_hashes(value) + } + /// Version 0 of [`TreeNode::put_value`]. #[inline] pub(super) fn put_value_v0( @@ -42,10 +53,11 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v0(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { @@ -58,7 +70,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -95,10 +108,11 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v0(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { @@ -111,7 +125,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } diff --git a/merk/src/tree/put_value/v1.rs b/merk/src/tree/put_value/v1.rs index 90c431d34..636eb6382 100644 --- a/merk/src/tree/put_value/v1.rs +++ b/merk/src/tree/put_value/v1.rs @@ -10,12 +10,23 @@ use grovedb_costs::{ CostResult, CostsExt, OperationCost, }; +use grovedb_version::version::GroveVersion; + use crate::{ - tree::{kv::ValueDefinedCostType, CryptoHash, TreeFeatureType, TreeNode}, + tree::{ + kv::{ValueDefinedCostType, KV}, + CryptoHash, TreeFeatureType, TreeNode, + }, Error, }; impl TreeNode { + /// How version 1 installs an ordinary value on a node: it clears the + /// `value_defined_cost` the node was loaded with. + pub(super) fn install_ordinary_value_v1(kv: KV, value: Vec) -> KV { + kv.put_ordinary_value_no_update_of_hashes(value) + } + /// Version 1 of [`TreeNode::put_value`]. #[inline] pub(super) fn put_value_v1( @@ -43,10 +54,11 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_ordinary_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v1(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { @@ -59,7 +71,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } @@ -96,10 +109,11 @@ impl TreeNode { (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); - self.inner.kv = self.inner.kv.put_ordinary_value_no_update_of_hashes(value); + self.inner.kv = Self::install_ordinary_value_v1(self.inner.kv, value); self.inner.kv.feature_type = feature_type; if self.old_value.is_some() { @@ -112,7 +126,8 @@ impl TreeNode { old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version, ) ); } diff --git a/merk/src/tree/walk/mod.rs b/merk/src/tree/walk/mod.rs index 499dd2605..322522d30 100644 --- a/merk/src/tree/walk/mod.rs +++ b/merk/src/tree/walk/mod.rs @@ -292,6 +292,7 @@ where (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); cost_return_on_error_no_add!( @@ -304,7 +305,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version ) .unwrap_add_cost(&mut cost)) ); @@ -472,6 +474,7 @@ where (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); cost_return_on_error_no_add!( @@ -485,7 +488,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version ) .unwrap_add_cost(&mut cost)) ); @@ -523,6 +527,7 @@ where (StorageRemovedBytes, StorageRemovedBytes), Error, >, + grove_version: &GroveVersion, ) -> CostResult { let mut cost = OperationCost::default(); cost_return_on_error_no_add!( @@ -537,7 +542,8 @@ where old_specialized_cost, get_temp_new_value_with_old_flags, update_tree_value_based_on_costs, - section_removal_bytes + section_removal_bytes, + grove_version ) .unwrap_add_cost(&mut cost)) );