Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer committed Jul 6, 2024
1 parent 40b4cda commit e3a477d
Show file tree
Hide file tree
Showing 11 changed files with 3,390 additions and 3,395 deletions.
17 changes: 17 additions & 0 deletions grovedb/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
//! GroveDB Errors
use std::convert::Infallible;

/// GroveDB Errors
#[cfg(any(feature = "full", feature = "verify"))]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("infallible")]
/// This error can not happen, used for generics
Infallible,
// Input data errors
#[error("cyclic reference path")]
/// Cyclic reference
Expand Down Expand Up @@ -143,3 +148,15 @@ pub enum Error {
/// Merk error
MerkError(grovedb_merk::error::Error),
}

impl From<Infallible> for Error {
fn from(_value: Infallible) -> Self {
Self::Infallible
}
}

impl From<grovedb_merk::error::Error> for Error {
fn from(value: grovedb_merk::Error) -> Self {
Error::MerkError(value)
}
}
12 changes: 4 additions & 8 deletions grovedb/src/operations/proof/generate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! Generate proof operations
use std::{
collections::{BTreeMap, BTreeSet},
fmt,
};
use std::{collections::BTreeMap, fmt};

use bincode::{Decode, Encode};
use derive_more::From;
Expand All @@ -15,7 +12,7 @@ use grovedb_merk::{
proofs::{
encode_into,
query::{Key, QueryItem},
Decoder, Node, Op, Tree,
Decoder, Node, Op,
},
tree::value_hash,
Merk, ProofWithoutEncodingResult,
Expand All @@ -24,9 +21,8 @@ use grovedb_path::SubtreePath;
use grovedb_storage::StorageContext;

use crate::{
query_result_type::{BTreeMapLevelResult, BTreeMapLevelResultOrItem, QueryResultType},
reference_path::path_from_reference_path_type,
Element, Error, GroveDb, PathQuery,
query_result_type::QueryResultType, reference_path::path_from_reference_path_type, Element,
Error, GroveDb, PathQuery,
};

#[derive(Debug, Clone, Copy, Encode, Decode)]
Expand Down
204 changes: 171 additions & 33 deletions grovedb/src/operations/proof/util.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,62 @@
use std::fmt;
#[cfg(any(feature = "full", feature = "verify"))]
use std::io::Read;
#[cfg(feature = "full")]
use std::io::Write;

use grovedb_merk::{
proofs::query::{Key, Path, ProvedKeyValue},
CryptoHash,
proofs::query::{Key, Path, ProvedKeyOptionalValue, ProvedKeyValue},
CryptoHash, Error,
};
#[cfg(any(feature = "full", feature = "verify"))]
use integer_encoding::{VarInt, VarIntReader};

use crate::Element;

#[cfg(any(feature = "full", feature = "verify"))]
pub type ProvedKeyValues = Vec<ProvedKeyValue>;

/// Proved path-key-values
#[cfg(any(feature = "full", feature = "verify"))]
pub type ProvedKeyOptionalValues = Vec<ProvedKeyOptionalValue>;

#[cfg(any(feature = "full", feature = "verify"))]
pub type ProvedPathKeyValues = Vec<ProvedPathKeyValue>;

#[cfg(any(feature = "full", feature = "verify"))]
pub type ProvedPathKeyOptionalValues = Vec<ProvedPathKeyOptionalValue>;

/// Proved path-key-value
#[cfg(any(feature = "full", feature = "verify"))]
#[derive(Debug, PartialEq, Eq)]
pub struct ProvedPathKeyOptionalValue {
/// Path
pub path: Path,
/// Key
pub key: Key,
/// Value
pub value: Option<Vec<u8>>,
/// Proof
pub proof: CryptoHash,
}

#[cfg(any(feature = "full", feature = "verify"))]
impl fmt::Display for ProvedPathKeyOptionalValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ProvedPathKeyValue {{\n")?;
write!(
f,
" path: [{}],\n",
self.path
.iter()
.map(|p| hex_to_ascii(p))
.collect::<Vec<_>>()
.join(", ")
)?;
write!(f, " key: {},\n", hex_to_ascii(&self.key))?;
write!(
f,
" value: {},\n",
optional_element_hex_to_ascii(self.value.as_ref())
)?;
write!(f, " proof: {}\n", hex::encode(self.proof))?;
write!(f, "}}")
}
}

/// Proved path-key-value
#[cfg(any(feature = "full", feature = "verify"))]
#[derive(Debug, PartialEq, Eq)]
Expand All @@ -34,7 +72,7 @@ pub struct ProvedPathKeyValue {
}

#[cfg(any(feature = "full", feature = "verify"))]
impl fmt::Display for ProvedPathKeyValue {
impl fmt::Display for crate::operations::proof::util::ProvedPathKeyValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ProvedPathKeyValue {{\n")?;
write!(
Expand All @@ -47,12 +85,66 @@ impl fmt::Display for ProvedPathKeyValue {
.join(", ")
)?;
write!(f, " key: {},\n", hex_to_ascii(&self.key))?;
write!(f, " value: {},\n", element_hex_to_ascii(&self.value))?;
write!(
f,
" value: {},\n",
element_hex_to_ascii(self.value.as_ref())
)?;
write!(f, " proof: {}\n", hex::encode(self.proof))?;
write!(f, "}}")
}
}

impl From<ProvedPathKeyValue> for ProvedPathKeyOptionalValue {
fn from(value: ProvedPathKeyValue) -> Self {
let ProvedPathKeyValue {
path,
key,
value,
proof,
} = value;

ProvedPathKeyOptionalValue {
path,
key,
value: Some(value),
proof,
}
}
}

impl TryFrom<ProvedPathKeyOptionalValue> for ProvedPathKeyValue {
type Error = Error;

fn try_from(value: ProvedPathKeyOptionalValue) -> Result<Self, Self::Error> {
let ProvedPathKeyOptionalValue {
path,
key,
value,
proof,
} = value;
let value = value.ok_or(Error::InvalidProofError(format!(
"expected {}",
hex_to_ascii(&key)
)))?;
Ok(ProvedPathKeyValue {
path,
key,
value,
proof,
})
}
}

fn optional_element_hex_to_ascii(hex_value: Option<&Vec<u8>>) -> String {
match hex_value {
None => "None".to_string(),
Some(hex_value) => Element::deserialize(hex_value)
.map(|e| e.to_string())
.unwrap_or_else(|_| hex::encode(hex_value)),
}
}

fn element_hex_to_ascii(hex_value: &[u8]) -> String {
Element::deserialize(hex_value)
.map(|e| e.to_string())
Expand Down Expand Up @@ -86,28 +178,54 @@ impl ProvedPathKeyValue {
}
}

impl ProvedPathKeyOptionalValue {
// TODO: make path a reference
/// Consumes the ProvedKeyValue and returns a ProvedPathKeyValue given a
/// Path
pub fn from_proved_key_value(path: Path, proved_key_value: ProvedKeyOptionalValue) -> Self {
Self {
path,
key: proved_key_value.key,
value: proved_key_value.value,
proof: proved_key_value.proof,
}
}

/// Transforms multiple ProvedKeyValues to their equivalent
/// ProvedPathKeyValue given a Path
pub fn from_proved_key_values(
path: Path,
proved_key_values: ProvedKeyOptionalValues,
) -> Vec<Self> {
proved_key_values
.into_iter()
.map(|pkv| Self::from_proved_key_value(path.clone(), pkv))
.collect()
}
}

#[cfg(test)]
mod tests {
use grovedb_merk::proofs::query::ProvedKeyValue;
use grovedb_merk::proofs::query::ProvedKeyOptionalValue;

use crate::operations::proof::util::ProvedPathKeyValue;
use crate::operations::proof::util::ProvedPathKeyOptionalValue;

#[test]
fn test_proved_path_from_single_proved_key_value() {
let path = vec![b"1".to_vec(), b"2".to_vec()];
let proved_key_value = ProvedKeyValue {
let proved_key_value = ProvedKeyOptionalValue {
key: b"a".to_vec(),
value: vec![5, 6],
value: Some(vec![5, 6]),
proof: [0; 32],
};
let proved_path_key_value =
ProvedPathKeyValue::from_proved_key_value(path.clone(), proved_key_value);
ProvedPathKeyOptionalValue::from_proved_key_value(path.clone(), proved_key_value);
assert_eq!(
proved_path_key_value,
ProvedPathKeyValue {
ProvedPathKeyOptionalValue {
path,
key: b"a".to_vec(),
value: vec![5, 6],
value: Some(vec![5, 6]),
proof: [0; 32]
}
);
Expand All @@ -116,49 +234,69 @@ mod tests {
#[test]
fn test_many_proved_path_from_many_proved_key_value() {
let path = vec![b"1".to_vec(), b"2".to_vec()];
let proved_key_value_a = ProvedKeyValue {
let proved_key_value_a = ProvedKeyOptionalValue {
key: b"a".to_vec(),
value: vec![5, 6],
value: Some(vec![5, 6]),
proof: [0; 32],
};
let proved_key_value_b = ProvedKeyValue {
let proved_key_value_b = ProvedKeyOptionalValue {
key: b"b".to_vec(),
value: vec![5, 7],
value: Some(vec![5, 7]),
proof: [1; 32],
};
let proved_key_value_c = ProvedKeyValue {
let proved_key_value_c = ProvedKeyOptionalValue {
key: b"c".to_vec(),
value: vec![6, 7],
value: Some(vec![6, 7]),
proof: [2; 32],
};
let proved_key_values = vec![proved_key_value_a, proved_key_value_b, proved_key_value_c];
let proved_key_value_d = ProvedKeyOptionalValue {
key: b"d".to_vec(),
value: None,
proof: [2; 32],
};
let proved_key_values = vec![
proved_key_value_a,
proved_key_value_b,
proved_key_value_c,
proved_key_value_d,
];
let proved_path_key_values =
ProvedPathKeyValue::from_proved_key_values(path.clone(), proved_key_values);
ProvedPathKeyOptionalValue::from_proved_key_values(path.clone(), proved_key_values);
assert_eq!(proved_path_key_values.len(), 3);
assert_eq!(
proved_path_key_values[0],
ProvedPathKeyValue {
ProvedPathKeyOptionalValue {
path: path.clone(),
key: b"a".to_vec(),
value: vec![5, 6],
value: Some(vec![5, 6]),
proof: [0; 32]
}
);
assert_eq!(
proved_path_key_values[1],
ProvedPathKeyValue {
ProvedPathKeyOptionalValue {
path: path.clone(),
key: b"b".to_vec(),
value: vec![5, 7],
value: Some(vec![5, 7]),
proof: [1; 32]
}
);
assert_eq!(
proved_path_key_values[2],
ProvedPathKeyValue {
path,
ProvedPathKeyOptionalValue {
path: path.clone(),
key: b"c".to_vec(),
value: vec![6, 7],
value: Some(vec![6, 7]),
proof: [2; 32]
}
);

assert_eq!(
proved_path_key_values[2],
ProvedPathKeyOptionalValue {
path,
key: b"d".to_vec(),
value: None,
proof: [2; 32]
}
);
Expand Down
Loading

0 comments on commit e3a477d

Please sign in to comment.