Skip to content

Commit

Permalink
feat!: proof system v1 (from v0.1) (#309)
Browse files Browse the repository at this point in the history
* potential limit fix

* removed offset from proofs

* new proof system

* much more work on new proof system

* more work on new proof system

* more work on proofs

* much more work on proofs

* more work on proofs

* more work

* more fixes

* fmt

* more work

* fixed tests for merk proofs

* more work

* more work

* more work

* more work

* all tests passing

* trial

* more work

* complex conditional queries

* cleanup

* clippy fixes

* fmt

* small fix

* fmt

* reactivated test

* cleaned up merk

* cleaned up merk

* small fix

* cleaned up verification imports

* clippy fixes

* clippy fixes

* added documentation
  • Loading branch information
QuantumExplorer authored Jul 9, 2024
1 parent 1b95932 commit 9cecef9
Show file tree
Hide file tree
Showing 60 changed files with 8,935 additions and 9,988 deletions.
5 changes: 3 additions & 2 deletions grovedb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ grovedb-storage = { version = "1.0.0-rc.2", path = "../storage", optional = true
grovedb-visualize = { version = "1.0.0-rc.2", path = "../visualize", optional = true }
hex = { version = "0.4.3"}
itertools = { version = "0.12.1", optional = true }
derive_more = { version = "0.99.18" }
integer-encoding = { version = "4.0.0", optional = true }
grovedb-costs = { version = "1.0.0-rc.2", path = "../costs", optional = true }
nohash-hasher = { version = "0.2.0", optional = true }
indexmap = { version = "2.2.6", optional = true }
indexmap = { version = "2.2.6"}
intmap = { version = "2.0.0", optional = true }
grovedb-path = { version = "1.0.0-rc.2", path = "../path" }
grovedbg-types = { path = "../grovedbg-types", optional = true }
Expand All @@ -45,6 +46,7 @@ harness = false

[features]
default = ["full"]
proof_debug = ["grovedb-merk/proof_debug"]
full = [
"grovedb-merk/full",
"thiserror",
Expand All @@ -55,7 +57,6 @@ full = [
"integer-encoding",
"grovedb-costs",
"nohash-hasher",
"indexmap",
"intmap"
]
visualize = [
Expand Down
6 changes: 3 additions & 3 deletions grovedb/src/batch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ where
let mut merk = cost_return_on_error!(&mut cost, merk_wrapped);
merk.set_base_root_key(root_key)
.add_cost(cost)
.map_err(|_| Error::InternalError("unable to set base root key"))
.map_err(|_| Error::InternalError("unable to set base root key".to_string()))
}

fn execute_ops_on_path(
Expand Down Expand Up @@ -1804,7 +1804,7 @@ impl GroveDb {
.add_cost(cost)
} else {
Err(Error::CorruptedPath(
"cannot open a subtree as parent exists but is not a tree",
"cannot open a subtree as parent exists but is not a tree".to_string(),
))
.wrap_with_cost(OperationCost::default())
}
Expand Down Expand Up @@ -3493,7 +3493,7 @@ mod tests {
reference_key_query.insert_key(b"key1".to_vec());
let path_query = PathQuery::new_unsized(vec![TEST_LEAF.to_vec()], reference_key_query);
let proof = db
.prove_query(&path_query)
.prove_query(&path_query, None)
.unwrap()
.expect("should generate proof");
let verification_result = GroveDb::verify_query_raw(&proof, &path_query);
Expand Down
26 changes: 20 additions & 6 deletions grovedb/src/element/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ use grovedb_merk::{
#[cfg(feature = "full")]
use integer_encoding::VarInt;

#[cfg(feature = "full")]
use crate::reference_path::path_from_reference_path_type;
#[cfg(any(feature = "full", feature = "verify"))]
use crate::reference_path::{path_from_reference_path_type, ReferencePathType};
#[cfg(any(feature = "full", feature = "verify"))]
use crate::{element::SUM_ITEM_COST_SIZE, Element, Error};
use crate::reference_path::ReferencePathType;
#[cfg(feature = "full")]
use crate::{
element::{SUM_TREE_COST_SIZE, TREE_COST_SIZE},
element::{SUM_ITEM_COST_SIZE, SUM_TREE_COST_SIZE, TREE_COST_SIZE},
ElementFlags,
};
#[cfg(any(feature = "full", feature = "verify"))]
use crate::{Element, Error};

impl Element {
#[cfg(any(feature = "full", feature = "verify"))]
Expand Down Expand Up @@ -133,9 +135,15 @@ impl Element {
matches!(self, Element::SumTree(..))
}

#[cfg(any(feature = "full", feature = "verify"))]
/// Check if the element is a tree but not a sum tree
pub fn is_basic_tree(&self) -> bool {
matches!(self, Element::Tree(..))
}

#[cfg(any(feature = "full", feature = "verify"))]
/// Check if the element is a tree
pub fn is_tree(&self) -> bool {
pub fn is_any_tree(&self) -> bool {
matches!(self, Element::SumTree(..) | Element::Tree(..))
}

Expand All @@ -147,10 +155,16 @@ impl Element {

#[cfg(any(feature = "full", feature = "verify"))]
/// Check if the element is an item
pub fn is_item(&self) -> bool {
pub fn is_any_item(&self) -> bool {
matches!(self, Element::Item(..) | Element::SumItem(..))
}

#[cfg(any(feature = "full", feature = "verify"))]
/// Check if the element is an item
pub fn is_basic_item(&self) -> bool {
matches!(self, Element::Item(..))
}

#[cfg(any(feature = "full", feature = "verify"))]
/// Check if the element is a sum item
pub fn is_sum_item(&self) -> bool {
Expand Down
92 changes: 62 additions & 30 deletions grovedb/src/element/mod.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
// MIT LICENSE
//
// Copyright (c) 2021 Dash Core Group
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

//! Module for subtrees handling.
//! Subtrees handling is isolated so basically this module is about adapting
//! Merk API to GroveDB needs.
Expand All @@ -48,8 +20,8 @@ mod query;
pub use query::QueryOptions;
#[cfg(any(feature = "full", feature = "verify"))]
mod serialize;
#[cfg(feature = "full")]
use core::fmt;
#[cfg(any(feature = "full", feature = "verify"))]
use std::fmt;

use bincode::{Decode, Encode};
#[cfg(any(feature = "full", feature = "verify"))]
Expand All @@ -59,6 +31,7 @@ use grovedb_merk::estimated_costs::{LAYER_COST_SIZE, SUM_LAYER_COST_SIZE};
#[cfg(feature = "full")]
use grovedb_visualize::visualize_to_vec;

use crate::operations::proof::util::hex_to_ascii;
#[cfg(any(feature = "full", feature = "verify"))]
use crate::reference_path::ReferencePathType;

Expand Down Expand Up @@ -111,6 +84,65 @@ pub enum Element {
SumTree(Option<Vec<u8>>, SumValue, Option<ElementFlags>),
}

impl fmt::Display for Element {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Element::Item(data, flags) => {
write!(
f,
"Item({}{})",
hex_to_ascii(data),
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::Reference(path, max_hop, flags) => {
write!(
f,
"Reference({}, max_hop: {}{})",
path,
max_hop.map_or("None".to_string(), |h| h.to_string()),
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::Tree(root_key, flags) => {
write!(
f,
"Tree({}{})",
root_key.as_ref().map_or("None".to_string(), hex::encode),
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::SumItem(sum_value, flags) => {
write!(
f,
"SumItem({}{}",
sum_value,
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
Element::SumTree(root_key, sum_value, flags) => {
write!(
f,
"SumTree({}, {}{}",
root_key.as_ref().map_or("None".to_string(), hex::encode),
sum_value,
flags
.as_ref()
.map_or(String::new(), |f| format!(", flags: {:?}", f))
)
}
}
}
}

impl Element {
pub fn type_str(&self) -> &str {
match self {
Expand Down
Loading

0 comments on commit 9cecef9

Please sign in to comment.