diff --git a/grovedb/Cargo.toml b/grovedb/Cargo.toml index 9175aa5f0..6eb64e980 100644 --- a/grovedb/Cargo.toml +++ b/grovedb/Cargo.toml @@ -20,7 +20,7 @@ grovedb-version = { version = "3.0.0", path = "../grovedb-version" } grovedb-visualize = { version = "3.0.0", path = "../visualize", optional = true } axum = { version = "=0.7.5", features = ["macros"], optional = true } -bincode = { version = "2.0.0-rc.3" } +bincode = "=2.0.0-rc.3" blake3 = "1.5.5" hex = "0.4.3" indexmap = "2.7.0" @@ -88,7 +88,8 @@ grovedbg = [ "axum", "tower-http", "zip-extensions", - "tempfile" + "tempfile", + "bincode/serde" ] [build-dependencies] diff --git a/grovedb/src/debugger.rs b/grovedb/src/debugger.rs index 85e56d4f5..8413d79c3 100644 --- a/grovedb/src/debugger.rs +++ b/grovedb/src/debugger.rs @@ -325,6 +325,24 @@ async fn fetch_with_path_query( )?)) } +pub(crate) fn dump_proof_grovedbg_stdout(proof: GroveDBProof) { + let grovedbg_proof = proof_to_grovedbg(proof).map_err(|e| e.to_string()); + let encoded = grovedbg_proof.and_then(|p| { + bincode::serde::encode_to_vec(p, bincode::config::standard()).map_err(|e| e.to_string()) + }); + + match encoded { + Ok(p) => { + println!("==========GroveDBG proof dump starts after this line=========="); + println!("{}", hex::encode(p)); + println!("==========GroveDBG proof dump ends before this line==========="); + } + Err(e) => { + eprintln!("Unable to dump proof to grovedbg: {e}"); + } + } +} + fn query_result_to_grovedbg( db: &GroveDb, tx: &Transaction, @@ -536,137 +554,51 @@ fn query_item_to_grovedb(item: QueryItem) -> crate::QueryItem { fn element_to_grovedbg(element: crate::Element) -> grovedbg_types::Element { match element { - crate::Element::Item(value, element_flags) - | crate::Element::ItemWithBackwardsReferences(value, element_flags) => { - grovedbg_types::Element::Item { + crate::Element::Item(value, element_flags) => grovedbg_types::Element::Item { + value, + element_flags, + }, + crate::Element::ItemWithBackwardsReferences(value, element_flags) => { + grovedbg_types::Element::ItemWithBackwardReferences { value, element_flags, } } - crate::Element::Tree(root_key, element_flags) => grovedbg_types::Element::Subtree { - root_key, + + crate::Element::SumItem(value, element_flags) => grovedbg_types::Element::SumItem { + value, element_flags, }, - crate::Element::Reference( - ReferencePathType::AbsolutePathReference(path), - _, - element_flags, - ) - | crate::Element::BidirectionalReference(BidirectionalReference { - forward_reference_path: ReferencePathType::AbsolutePathReference(path), - flags: element_flags, - .. - }) => { - grovedbg_types::Element::Reference(grovedbg_types::Reference::AbsolutePathReference { - path, + crate::Element::SumItemWithBackwardsReferences(value, element_flags) => { + grovedbg_types::Element::SumItemWithBackwardReferences { + value, element_flags, - }) + } } - crate::Element::Reference( - ReferencePathType::UpstreamRootHeightReference(n_keep, path_append), - _, - element_flags, - ) - | crate::Element::BidirectionalReference(BidirectionalReference { - forward_reference_path: - ReferencePathType::UpstreamRootHeightReference(n_keep, path_append), - flags: element_flags, - .. - }) => grovedbg_types::Element::Reference( - grovedbg_types::Reference::UpstreamRootHeightReference { - n_keep: n_keep.into(), - path_append, - element_flags, - }, - ), - crate::Element::Reference( - ReferencePathType::UpstreamRootHeightWithParentPathAdditionReference( - n_keep, - path_append, - ), - _, - element_flags, - ) - | crate::Element::BidirectionalReference(BidirectionalReference { - forward_reference_path: - ReferencePathType::UpstreamRootHeightWithParentPathAdditionReference( - n_keep, - path_append, - ), - flags: element_flags, - .. - }) => grovedbg_types::Element::Reference( - grovedbg_types::Reference::UpstreamRootHeightWithParentPathAdditionReference { - n_keep: n_keep.into(), - path_append, - element_flags, - }, - ), - crate::Element::Reference( - ReferencePathType::UpstreamFromElementHeightReference(n_remove, path_append), - _, - element_flags, - ) - | crate::Element::BidirectionalReference(BidirectionalReference { - forward_reference_path: - ReferencePathType::UpstreamFromElementHeightReference(n_remove, path_append), - flags: element_flags, - .. - }) => grovedbg_types::Element::Reference( - grovedbg_types::Reference::UpstreamFromElementHeightReference { - n_remove: n_remove.into(), - path_append, - element_flags, - }, - ), - crate::Element::Reference( - ReferencePathType::CousinReference(swap_parent), - _, - element_flags, - ) - | crate::Element::BidirectionalReference(BidirectionalReference { - forward_reference_path: ReferencePathType::CousinReference(swap_parent), - flags: element_flags, - .. - }) => grovedbg_types::Element::Reference(grovedbg_types::Reference::CousinReference { - swap_parent, - element_flags, - }), - crate::Element::Reference( - ReferencePathType::RemovedCousinReference(swap_parent), - _, - element_flags, - ) - | crate::Element::BidirectionalReference(BidirectionalReference { - forward_reference_path: ReferencePathType::RemovedCousinReference(swap_parent), - flags: element_flags, - .. - }) => { - grovedbg_types::Element::Reference(grovedbg_types::Reference::RemovedCousinReference { - swap_parent, + + crate::Element::Reference(reference, _, element_flags) => { + grovedbg_types::Element::Reference { + reference_path: reference_path_to_grovedbg(reference), element_flags, - }) + } } - crate::Element::Reference( - ReferencePathType::SiblingReference(sibling_key), - _, - element_flags, - ) - | crate::Element::BidirectionalReference(BidirectionalReference { - forward_reference_path: ReferencePathType::SiblingReference(sibling_key), - flags: element_flags, + crate::Element::BidirectionalReference(BidirectionalReference { + forward_reference_path, + flags, + backward_reference_slot, + cascade_on_update, .. - }) => grovedbg_types::Element::Reference(grovedbg_types::Reference::SiblingReference { - sibling_key, + }) => grovedbg_types::Element::BidirectionalReference { + reference_path: reference_path_to_grovedbg(forward_reference_path), + element_flags: flags, + slot_idx: backward_reference_slot as u8, + cascade_on_update, + }, + + crate::Element::Tree(root_key, element_flags) => grovedbg_types::Element::Subtree { + root_key, element_flags, - }), - crate::Element::SumItem(value, element_flags) - | crate::Element::SumItemWithBackwardsReferences(value, element_flags) => { - grovedbg_types::Element::SumItem { - value, - element_flags, - } - } + }, crate::Element::SumTree(root_key, sum, element_flags) => grovedbg_types::Element::Sumtree { root_key, sum, @@ -697,6 +629,43 @@ fn element_to_grovedbg(element: crate::Element) -> grovedbg_types::Element { } } +fn reference_path_to_grovedbg(reference: ReferencePathType) -> grovedbg_types::ReferencePath { + match reference { + ReferencePathType::AbsolutePathReference(path) => { + grovedbg_types::ReferencePath::AbsolutePathReference { path } + } + + ReferencePathType::UpstreamRootHeightReference(n_keep, path_append) => { + grovedbg_types::ReferencePath::UpstreamRootHeightReference { + n_keep: n_keep.into(), + path_append, + } + } + ReferencePathType::UpstreamRootHeightWithParentPathAdditionReference( + n_keep, + path_append, + ) => grovedbg_types::ReferencePath::UpstreamRootHeightWithParentPathAdditionReference { + n_keep: n_keep.into(), + path_append, + }, + ReferencePathType::UpstreamFromElementHeightReference(n_remove, path_append) => { + grovedbg_types::ReferencePath::UpstreamFromElementHeightReference { + n_remove: n_remove.into(), + path_append, + } + } + ReferencePathType::CousinReference(swap_parent) => { + grovedbg_types::ReferencePath::CousinReference { swap_parent } + } + ReferencePathType::RemovedCousinReference(swap_parent) => { + grovedbg_types::ReferencePath::RemovedCousinReference { swap_parent } + } + ReferencePathType::SiblingReference(sibling_key) => { + grovedbg_types::ReferencePath::SiblingReference { sibling_key } + } + } +} + fn node_to_update( path: Path, NodeDbg { diff --git a/grovedb/src/lib.rs b/grovedb/src/lib.rs index 4f8f42b62..421945909 100644 --- a/grovedb/src/lib.rs +++ b/grovedb/src/lib.rs @@ -161,7 +161,7 @@ use std::sync::Arc; use std::{collections::HashMap, option::Option::None, path::Path}; #[cfg(feature = "minimal")] -use bidirectional_references::BidirectionalReference; +pub use bidirectional_references::BidirectionalReference; #[cfg(feature = "grovedbg")] use debugger::start_visualizer; #[cfg(any(feature = "minimal", feature = "verify"))] diff --git a/grovedb/src/operations/proof/mod.rs b/grovedb/src/operations/proof/mod.rs index cadc60326..3730b90c3 100644 --- a/grovedb/src/operations/proof/mod.rs +++ b/grovedb/src/operations/proof/mod.rs @@ -57,13 +57,13 @@ impl Default for ProveOptions { } } -#[derive(Encode, Decode)] +#[derive(Clone, Encode, Decode)] pub struct LayerProof { pub merk_proof: Vec, pub lower_layers: BTreeMap, } -#[derive(Encode, Decode)] +#[derive(Clone, Encode, Decode)] pub enum GroveDBProof { V0(GroveDBProofV0), } @@ -176,7 +176,7 @@ impl GroveDBProof { } } -#[derive(Encode, Decode)] +#[derive(Clone, Encode, Decode)] pub struct GroveDBProofV0 { pub root_layer: LayerProof, pub prove_options: ProveOptions, diff --git a/grovedbg-types/src/lib.rs b/grovedbg-types/src/lib.rs index fb9c6d90f..08bf6a957 100644 --- a/grovedbg-types/src/lib.rs +++ b/grovedbg-types/src/lib.rs @@ -61,51 +61,37 @@ pub struct NodeUpdate { #[serde_as] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub enum Reference { +pub enum ReferencePath { AbsolutePathReference { #[serde_as(as = "Vec")] path: Path, - #[serde_as(as = "Option")] - element_flags: Option>, }, UpstreamRootHeightReference { n_keep: u32, #[serde_as(as = "Vec")] path_append: Vec, - #[serde_as(as = "Option")] - element_flags: Option>, }, UpstreamRootHeightWithParentPathAdditionReference { n_keep: u32, #[serde_as(as = "Vec")] path_append: Vec, - #[serde_as(as = "Option")] - element_flags: Option>, }, UpstreamFromElementHeightReference { n_remove: u32, #[serde_as(as = "Vec")] path_append: Vec, - #[serde_as(as = "Option")] - element_flags: Option>, }, CousinReference { #[serde_as(as = "Base64")] swap_parent: PathSegment, - #[serde_as(as = "Option")] - element_flags: Option>, }, RemovedCousinReference { #[serde_as(as = "Vec")] swap_parent: Vec, - #[serde_as(as = "Option")] - element_flags: Option>, }, SiblingReference { #[serde_as(as = "Base64")] sibling_key: Key, - #[serde_as(as = "Option")] - element_flags: Option>, }, } @@ -153,12 +139,34 @@ pub enum Element { #[serde_as(as = "Option")] element_flags: Option>, }, + ItemWithBackwardReferences { + #[serde_as(as = "Base64")] + value: Vec, + #[serde_as(as = "Option")] + element_flags: Option>, + }, SumItem { value: i64, #[serde_as(as = "Option")] element_flags: Option>, }, - Reference(Reference), + SumItemWithBackwardReferences { + value: i64, + #[serde_as(as = "Option")] + element_flags: Option>, + }, + Reference { + reference_path: ReferencePath, + #[serde_as(as = "Option")] + element_flags: Option>, + }, + BidirectionalReference { + reference_path: ReferencePath, + #[serde_as(as = "Option")] + element_flags: Option>, + slot_idx: u8, + cascade_on_update: bool, + }, } #[serde_as]