grovedb 3.1.0

Fully featured database using balanced hierarchical authenticated data structures
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// 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.

//! Tree hashes tests

use grovedb_merk::tree::{
    combine_hash, kv::ValueDefinedCostType, kv_digest_to_kv_hash, node_hash, value_hash, NULL_HASH,
};
use grovedb_storage::StorageBatch;
use grovedb_version::version::GroveVersion;

use crate::{
    tests::{make_test_grovedb, TEST_LEAF},
    Element,
};

#[test]
fn test_node_hashes_when_inserting_item() {
    let grove_version = GroveVersion::latest();
    let db = make_test_grovedb(grove_version);

    db.insert(
        [TEST_LEAF].as_ref(),
        b"key1",
        Element::new_item(b"baguette".to_vec()),
        None,
        None,
        grove_version,
    )
    .unwrap()
    .expect("successful subtree insert");

    let batch = StorageBatch::new();
    let transaction = db.start_transaction();

    let test_leaf_merk = db
        .open_transactional_merk_at_path(
            [TEST_LEAF].as_ref().into(),
            &transaction,
            Some(&batch),
            grove_version,
        )
        .unwrap()
        .expect("should open merk");

    let (elem_value, elem_value_hash) = test_leaf_merk
        .get_value_and_value_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let elem_kv_hash = test_leaf_merk
        .get_kv_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let elem_node_hash = test_leaf_merk
        .get_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let actual_value_hash = value_hash(&elem_value).unwrap();

    assert_eq!(elem_value_hash, actual_value_hash);

    let kv_hash = kv_digest_to_kv_hash(b"key1", &elem_value_hash).unwrap();

    assert_eq!(elem_kv_hash, kv_hash);

    let node_hash = node_hash(&kv_hash, &NULL_HASH, &NULL_HASH).unwrap();

    assert_eq!(elem_node_hash, node_hash);
}

#[test]
fn test_tree_hashes_when_inserting_empty_tree() {
    let grove_version = GroveVersion::latest();
    let db = make_test_grovedb(grove_version);

    db.insert(
        [TEST_LEAF].as_ref(),
        b"key1",
        Element::empty_tree(),
        None,
        None,
        grove_version,
    )
    .unwrap()
    .expect("successful subtree insert");

    let batch = StorageBatch::new();
    let transaction = db.start_transaction();

    let test_leaf_merk = db
        .open_transactional_merk_at_path(
            [TEST_LEAF].as_ref().into(),
            &transaction,
            Some(&batch),
            grove_version,
        )
        .unwrap()
        .expect("should open merk");

    let (elem_value, elem_value_hash) = test_leaf_merk
        .get_value_and_value_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let elem_kv_hash = test_leaf_merk
        .get_kv_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let elem_node_hash = test_leaf_merk
        .get_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let underlying_merk = db
        .open_transactional_merk_at_path(
            [TEST_LEAF, b"key1"].as_ref().into(),
            &transaction,
            Some(&batch),
            grove_version,
        )
        .unwrap()
        .expect("should open merk");

    let root_hash = underlying_merk.root_hash().unwrap();

    let actual_value_hash = value_hash(&elem_value).unwrap();
    let combined_value_hash = combine_hash(&actual_value_hash, &root_hash).unwrap();

    assert_eq!(elem_value_hash, combined_value_hash);

    let kv_hash = kv_digest_to_kv_hash(b"key1", &elem_value_hash).unwrap();

    assert_eq!(elem_kv_hash, kv_hash);

    let node_hash = node_hash(&kv_hash, &NULL_HASH, &NULL_HASH).unwrap();

    assert_eq!(elem_node_hash, node_hash);
}

#[test]
fn test_tree_hashes_when_inserting_empty_trees_twice_under_each_other() {
    let grove_version = GroveVersion::latest();
    let db = make_test_grovedb(grove_version);

    db.insert(
        [TEST_LEAF].as_ref(),
        b"key1",
        Element::empty_tree(),
        None,
        None,
        grove_version,
    )
    .unwrap()
    .expect("successful subtree insert");

    db.insert(
        [TEST_LEAF, b"key1"].as_ref(),
        b"key2",
        Element::empty_tree(),
        None,
        None,
        grove_version,
    )
    .unwrap()
    .expect("successful subtree insert");

    let batch = StorageBatch::new();
    let transaction = db.start_transaction();

    let under_top_merk = db
        .open_transactional_merk_at_path(
            [TEST_LEAF].as_ref().into(),
            &transaction,
            Some(&batch),
            grove_version,
        )
        .unwrap()
        .expect("should open merk");

    let middle_merk_key1 = db
        .open_transactional_merk_at_path(
            [TEST_LEAF, b"key1"].as_ref().into(),
            &transaction,
            Some(&batch),
            grove_version,
        )
        .unwrap()
        .expect("should open merk");

    // Let's first verify that the lowest nodes hashes are as we expect

    let (elem_value, elem_value_hash) = middle_merk_key1
        .get_value_and_value_hash(
            b"key2",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let bottom_merk = db
        .open_transactional_merk_at_path(
            [TEST_LEAF, b"key1", b"key2"].as_ref().into(),
            &transaction,
            Some(&batch),
            grove_version,
        )
        .unwrap()
        .expect("should open merk");

    let root_hash = bottom_merk.root_hash().unwrap();

    assert_eq!(root_hash, NULL_HASH);

    let actual_value_hash_key2 = value_hash(&elem_value).unwrap();
    let combined_value_hash_key2 = combine_hash(&actual_value_hash_key2, &root_hash).unwrap();

    assert_eq!(elem_value_hash, combined_value_hash_key2);

    let elem_kv_hash = middle_merk_key1
        .get_kv_hash(
            b"key2",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get kv hash")
        .expect("value hash should be some");

    let kv_hash_key2 = kv_digest_to_kv_hash(b"key2", &elem_value_hash).unwrap();

    assert_eq!(elem_kv_hash, kv_hash_key2);

    let elem_node_hash = middle_merk_key1
        .get_hash(
            b"key2",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get kv hash")
        .expect("value hash should be some");

    let node_hash_key2 = node_hash(&kv_hash_key2, &NULL_HASH, &NULL_HASH).unwrap();

    assert_eq!(elem_node_hash, node_hash_key2);

    // now lets verify the middle node

    let root_hash = middle_merk_key1.root_hash().unwrap();

    // the root hash should equal to the node_hash previously calculated

    assert_eq!(root_hash, node_hash_key2);

    let (middle_elem_value_key1, middle_elem_value_hash_key1) = under_top_merk
        .get_value_and_value_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    assert_eq!(
        hex::encode(middle_elem_value_key1.as_slice()),
        "0201046b65793200"
    );

    let element = Element::deserialize(middle_elem_value_key1.as_slice(), grove_version)
        .expect("expected to deserialize element");

    assert_eq!(element, Element::new_tree(Some(b"key2".to_vec())));

    let actual_value_hash = value_hash(&middle_elem_value_key1).unwrap();

    assert_eq!(
        hex::encode(actual_value_hash),
        "06df974f1ea519344393e681f40dcb1b366b042416e663e0ba942ee2fd4b81f4"
    );
    assert_eq!(
        hex::encode(root_hash),
        "f0ba6963b5280da600c89b9684e6ab386ff6146fadfc21a98d52d5bb524c1bd9"
    );

    let combined_value_hash_key1 = combine_hash(&actual_value_hash, &root_hash).unwrap();

    assert_eq!(
        hex::encode(middle_elem_value_hash_key1),
        hex::encode(combined_value_hash_key1)
    );
    assert_eq!(
        hex::encode(middle_elem_value_hash_key1),
        "1e15cd574fd55c2471a864a6ea855e126ac0610ad99d96088c34438e2b22490b"
    );

    let middle_elem_kv_hash_key1 = under_top_merk
        .get_kv_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let kv_hash_key2 = kv_digest_to_kv_hash(b"key1", &combined_value_hash_key1).unwrap();

    assert_eq!(
        hex::encode(middle_elem_kv_hash_key1),
        hex::encode(kv_hash_key2),
        "middle kv hashes don't match"
    );

    let middle_elem_node_hash_key1 = under_top_merk
        .get_hash(
            b"key1",
            true,
            None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
            grove_version,
        )
        .unwrap()
        .expect("should get value hash")
        .expect("value hash should be some");

    let node_hash_key1 = node_hash(&kv_hash_key2, &NULL_HASH, &NULL_HASH).unwrap();

    assert_eq!(node_hash_key1, middle_elem_node_hash_key1);

    // now lets verify the middle node

    let root_hash = under_top_merk.root_hash().unwrap();

    // the root hash should equal to the node_hash previously calculated

    assert_eq!(root_hash, node_hash_key1);
}