commonware_storage/qmdb/current/unordered/
db.rs1use crate::{
7 index::Unordered as UnorderedIndex,
8 journal::contiguous::{Contiguous, Mutable},
9 merkle::{self, Location},
10 qmdb::{
11 any::{
12 operation::update::Unordered as UnorderedUpdate,
13 unordered::{Operation, Update},
14 ValueEncoding,
15 },
16 current::proof::OperationProof,
17 Error,
18 },
19 Context,
20};
21use commonware_codec::Codec;
22use commonware_cryptography::Hasher;
23use commonware_utils::Array;
24
25pub type KeyValueProof<F, D, const N: usize> = OperationProof<F, D, N>;
27
28pub type Db<F, E, C, K, V, I, H, const N: usize> =
33 crate::qmdb::current::db::Db<F, E, C, I, H, Update<K, V>, N>;
34
35impl<
37 F: merkle::Graftable,
38 E: Context,
39 C: Contiguous<Item = Operation<F, K, V>>,
40 K: Array,
41 V: ValueEncoding,
42 I: UnorderedIndex<Value = Location<F>>,
43 H: Hasher,
44 const N: usize,
45 > Db<F, E, C, K, V, I, H, N>
46where
47 Operation<F, K, V>: Codec,
48{
49 pub async fn get(&self, key: &K) -> Result<Option<V::Value>, Error<F>> {
51 self.any.get(key).await
52 }
53
54 pub fn verify_key_value_proof(
57 hasher: &mut H,
58 key: K,
59 value: V::Value,
60 proof: &KeyValueProof<F, H::Digest, N>,
61 root: &H::Digest,
62 ) -> bool {
63 let op = Operation::Update(UnorderedUpdate(key, value));
64
65 proof.verify(hasher, op, root)
66 }
67}
68
69impl<
70 F: merkle::Graftable,
71 E: Context,
72 C: Mutable<Item = Operation<F, K, V>>,
73 K: Array,
74 V: ValueEncoding,
75 I: UnorderedIndex<Value = Location<F>>,
76 H: Hasher,
77 const N: usize,
78 > Db<F, E, C, K, V, I, H, N>
79where
80 Operation<F, K, V>: Codec,
81{
82 pub async fn key_value_proof(
90 &self,
91 hasher: &mut H,
92 key: K,
93 ) -> Result<KeyValueProof<F, H::Digest, N>, Error<F>> {
94 let op_loc = self.any.get_with_loc(&key).await?;
95 let Some((_, loc)) = op_loc else {
96 return Err(Error::<F>::KeyNotFound);
97 };
98 self.operation_proof(hasher, loc).await
99 }
100}