-
Notifications
You must be signed in to change notification settings - Fork 15
/
check.cpp
1540 lines (1101 loc) · 41.9 KB
/
check.cpp
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string>
#include <iostream>
#include <stdexcept>
#include <functional>
#include <vector>
#include <bitset>
#include <random>
#include "quadrable.h"
#include "quadrable/transport.h"
#include "quadrable/debug.h"
namespace quadrable {
void doTests() {
std::string dbDir = "testdb/";
lmdb::env lmdb_env = lmdb::env::create();
lmdb_env.set_max_dbs(64);
lmdb_env.set_mapsize(1UL * 1024UL * 1024UL * 1024UL * 1024UL);
lmdb_env.open(dbDir.c_str(), MDB_CREATE, 0664);
lmdb_env.reader_check();
quadrable::Quadrable db;
//db.trackKeys = true;
{
auto txn = lmdb::txn::begin(lmdb_env, nullptr, 0);
db.init(txn);
txn.commit();
}
auto txn = lmdb::txn::begin(lmdb_env, nullptr, 0);
# define verify(condition) do { if (!(condition)) throw quaderr(#condition, " | ", __FILE__, ":", __LINE__); } while(0)
# define verifyThrow(condition, expected) { \
bool caught = false; \
std::string errorMsg; \
try { condition; } \
catch (const std::runtime_error &e) { \
caught = true; \
errorMsg = e.what(); \
} \
if (!caught) throw quaderr(#condition, " | expected error, but didn't get one (", expected, ")"); \
if (errorMsg.find(expected) == std::string::npos) throw quaderr(#condition, " | error msg not what we expected: ", errorMsg, " (not ", expected, ")"); \
}
auto test = [&](std::string testName, std::function<void()> cb) {
db.checkout();
std::cout << "TEST: " << testName << std::endl;
try {
cb();
} catch (const std::runtime_error& error) {
throw quaderr(testName, " | ", error.what());
}
std::cout << "OK." << std::endl;
};
auto equivHeads = [&](std::string desc, std::function<void()> cb1, std::function<void()> cb2, bool expectEqual = true) {
if (desc.size()) std::cout << " - " << desc << std::endl;
db.checkout();
cb1();
auto root1 = db.root(txn);
db.checkout();
cb2();
auto root2 = db.root(txn);
verify((root1 == root2) == expectEqual);
};
auto proofRoundtrip = [](const Proof &p) {
return quadrable::transport::decodeProof(quadrable::transport::encodeProof(p));
};
auto syncRequestsRoundtrip = [](const SyncRequests &reqs) {
return quadrable::transport::decodeSyncRequests(quadrable::transport::encodeSyncRequests(reqs));
};
auto syncResponsesRoundtrip = [](const SyncResponses &resps) {
return quadrable::transport::decodeSyncResponses(quadrable::transport::encodeSyncResponses(resps));
};
auto dump = [&]{ quadrable::dumpDb(db, txn); };
auto stats = [&]{ quadrable::dumpStats(db, txn); };
auto stop = [&]{ throw quaderr("STOP"); };
(void)dump;
(void)stop;
(void)stats;
test("basic put/get", [&]{
db.change()
.put("hello", "world")
.apply(txn);
std::string_view val;
verify(db.get(txn, "hello", val));
verify(val == "world");
auto stats = db.stats(txn);
verify(stats.numLeafNodes == 1);
});
test("zero-length keys", [&]{
verifyThrow(db.change().put("", "1").apply(txn), "zero-length keys not allowed");
verifyThrow(db.change().del("").apply(txn), "zero-length keys not allowed");
});
test("overwriting updates before apply", [&]{
equivHeads("double put", [&]{
db.change().put("a", "1").apply(txn);
db.change().put("a", "1").apply(txn);
},[&]{
db.change().put("a", "1").apply(txn);
});
equivHeads("del overwrites put", [&]{
db.change().put("a", "1").del("a").apply(txn);
},[&]{
});
equivHeads("put overwrites del overwrites put", [&]{
db.change().put("a", "1").del("a").put("a", "2").apply(txn);
},[&]{
db.change().put("a", "2").apply(txn);
});
});
test("saves nodeId", [&]{
uint64_t nodeId = 999999;
db.change()
.put("A", "1", &nodeId)
.apply(txn);
verify(nodeId != 0);
verify(nodeId == db.getHeadNodeId(txn));
verify(Quadrable::ParsedNode(&db, txn, nodeId).leafVal() == "1");
// Puting identical leaf returns 0
nodeId = 999999;
db.change()
.put("A", "1", &nodeId)
.apply(txn);
verify(nodeId == 0);
// Puting new leaf with same key but different value returns non-0
nodeId = 999999;
db.change()
.put("A", "2", &nodeId)
.apply(txn);
verify(nodeId != 0);
verify(nodeId == db.getHeadNodeId(txn));
uint64_t origHead = db.getHeadNodeId(txn);
// Deletions, etc
uint64_t nodeId1 = 55555, nodeId2 = 44444, nodeId3 = 33333;
uint64_t nodeIdDel1 = 8888, nodeIdDel2 = 7777;
db.change()
.del("A", &nodeIdDel1)
.put("B", "2", &nodeId1)
.put("D", "4", &nodeId2)
.put("C", "3")
.put("E", "5", &nodeId3)
.del("NONE", &nodeIdDel2)
.apply(txn);
verify(Quadrable::ParsedNode(&db, txn, nodeId1).leafVal() == "2");
verify(Quadrable::ParsedNode(&db, txn, nodeId2).leafVal() == "4");
verify(Quadrable::ParsedNode(&db, txn, nodeId3).leafVal() == "5");
verify(nodeIdDel1 == origHead);
verify(nodeIdDel2 == 0);
// Unchanged leaf:
uint64_t nodeIdDup;
db.change()
.put("B", "2", &nodeIdDup)
.apply(txn);
verify(nodeIdDup == 0);
});
test("integer round-trips", [&]{
for (uint64_t i = 0; i < 100'000; i++) {
verify(quadrable::Key::fromInteger(i).toInteger() == i);
}
for (uint64_t i = std::numeric_limits<uint64_t>::max() - 100'000; i <= std::numeric_limits<uint64_t>::max() - 2; i++) {
verify(quadrable::Key::fromInteger(i).toInteger() == i);
}
for (uint64_t i = 10; i <= 64; i++) {
uint64_t n = (1LL << i) - 5;
verify(quadrable::Key::fromInteger(n).toInteger() == n);
}
{
uint64_t i = std::numeric_limits<uint64_t>::max() - 1;
verifyThrow(quadrable::Key::fromInteger(i), "int range exceeded");
}
{
uint64_t i = std::numeric_limits<uint64_t>::max();
verifyThrow(quadrable::Key::fromInteger(i), "int range exceeded");
}
});
test("empty heads", [&]{
verify(Key::null() == db.root(txn));
std::string_view val;
verify(!db.get(txn, "hello", val));
auto stats = db.stats(txn);
verify(stats.numLeafNodes == 0);
db.change().put("a", "1").apply(txn);
verify(Key::null() != db.root(txn));
db.change().del("a").apply(txn);
verify(Key::null() == db.root(txn));
});
test("batch insert", [&]{
db.change()
.put("a", "1")
.put("b", "2")
.put("c", "3")
.apply(txn);
auto stats = db.stats(txn);
verify(stats.numLeafNodes == 3);
std::string_view val;
verify(db.get(txn, "b", val));
verify(val == "2");
});
test("getMulti", [&]{
auto changes = db.change();
for (int i=0; i<100; i++) {
std::string s = std::to_string(i);
changes.put(s, std::string("N = ") + s);
}
changes.apply(txn);
auto query = db.get(txn, { "30", "31", "32", "blah", "nope", });
verify(query["30"].exists && query["30"].val == "N = 30");
verify(query["31"].exists && query["31"].val == "N = 31");
verify(query["32"].exists && query["32"].val == "N = 32");
verify(!query["blah"].exists);
verify(!query["nope"].exists);
});
test("del", [&]{
{
auto changes = db.change();
changes.put("a", "1");
changes.put("b", "2");
changes.put("c", "3");
changes.apply(txn);
}
db.change()
.del("b")
.apply(txn);
auto stats = db.stats(txn);
verify(stats.numLeafNodes == 2);
std::string_view val;
verify(!db.get(txn, "b", val));
});
test("del bubble", [&]{
equivHeads("bubble right", [&]{
db.change().put("a", "1").put("b", "2").apply(txn);
db.change().del("b").apply(txn);
},[&]{
db.change().put("a", "1").apply(txn);
});
equivHeads("bubble left", [&]{
db.change().put("a", "1").put("b", "2").apply(txn);
db.change().del("a").apply(txn);
},[&]{
db.change().put("b", "2").apply(txn);
});
equivHeads("delete both sides of a branch in same update, leaving empty node", [&]{
db.change().put("a", "1").put("b", "2").apply(txn);
db.change().del("a").del("b").apply(txn);
},[&]{
});
equivHeads("delete both sides of a branch in same update, which causes sibling leaf to bubble up", [&]{
db.change().put("a", "1").put("b", "2").put("c", "3").apply(txn);
db.change().del("a").del("c").apply(txn);
},[&]{
db.change().put("b", "2").apply(txn);
});
equivHeads("delete one side of a branch and a sibling leaf in same update, which causes remaining side of branch to bubble up", [&]{
db.change().put("a", "1").put("b", "2").put("c", "3").apply(txn);
db.change().del("b").del("c").apply(txn);
},[&]{
db.change().put("a", "1").apply(txn);
});
equivHeads("same as previous, but other side of the branch", [&]{
db.change().put("a", "1").put("b", "2").put("c", "3").apply(txn);
db.change().del("b").del("a").apply(txn);
},[&]{
db.change().put("c", "3").apply(txn);
});
equivHeads("deleted neighbour node", [&]{
db.change()
.put(quadrable::Key::fromInteger(1), "B")
.apply(txn);
},[&]{
db.change()
.put(quadrable::Key::fromInteger(1), "A")
.apply(txn);
db.change()
.del(quadrable::Key::fromInteger(2))
.put(quadrable::Key::fromInteger(1), "B")
.apply(txn);
});
equivHeads("long bubble", [&]{
db.change()
.put("146365204598", "A") // 11111111...
.put("967276293879", "B") // 11111110...
.apply(txn);
db.change()
.del("146365204598")
.apply(txn);
},[&]{
db.change()
.put("967276293879", "B") // 11111110...
.apply(txn);
});
equivHeads("long bubble, double deletion", [&]{
db.change()
.put("146365204598", "A") // 11111111...
.put("967276293879", "B") // 11111110...
.put("948464225881", "C") // 1110...
.apply(txn);
db.change()
.del("967276293879")
.del("948464225881")
.apply(txn);
},[&]{
db.change()
.put("146365204598", "A") // 11111110...
.apply(txn);
});
});
test("mix del and put", [&]{
equivHeads("left", [&]{
db.change().put("a", "1").put("b", "2").put("c", "3").apply(txn);
db.change().del("a").put("c", "4").apply(txn);
},[&]{
db.change().put("b", "2").put("c", "4").apply(txn);
});
equivHeads("right", [&]{
db.change().put("a", "1").put("b", "2").put("c", "3").apply(txn);
db.change().del("a").put("d", "4").apply(txn);
},[&]{
db.change().put("b", "2").put("c", "3").put("d", "4").apply(txn);
});
});
test("del non-existent", [&]{
equivHeads("empty root", [&]{
db.change().del("a").apply(txn);
},[&]{
});
equivHeads("simple", [&]{
db.change().put("a", "1").put("b", "2").put("c", "3").apply(txn);
db.change().del("d").apply(txn);
},[&]{
db.change().put("a", "1").put("b", "2").put("c", "3").apply(txn);
});
equivHeads("delete a node, and try to delete a non-existent node underneath it", [&]{
db.change().put("a", "1").apply(txn);
db.change().del("a").del("849686319312").apply(txn); // 01...
},[&]{
});
equivHeads("same as previous, but requires bubbling", [&]{
db.change().put("a", "1").put("b", "2").apply(txn);
db.change().del("a").del("849686319312").apply(txn); // 01...
},[&]{
db.change().put("b", "2").apply(txn);
});
});
test("leaf splitting while deleting/updating split leaf", [&]{
equivHeads("first", [&]{
db.change().put("a", "1").apply(txn); // 0...
db.change().del("a").put("849686319312", "2").apply(txn); // 01...
},[&]{
db.change().put("849686319312", "2").apply(txn); // 01...
});
equivHeads("second", [&]{
db.change().put("a", "1").apply(txn); // 0...
db.change().put("a", "3").put("849686319312", "2").apply(txn); // 01...
},[&]{
db.change().put("a", "3").put("849686319312", "2").apply(txn); // 01...
});
});
test("bunch of strings", [&]{
int n = 1000;
auto changes = db.change();
for (int i=0; i<n; i++) {
std::string s = std::to_string(i);
changes.put(s, s+s);
}
changes.apply(txn); // in batch
auto stats = db.stats(txn);
verify(stats.numLeafNodes == (uint64_t)n);
for (int i=0; i<n; i++) {
std::string s = std::to_string(i);
std::string_view val;
verify(db.get(txn, s, val));
verify(val == s+s);
}
auto origRoot = db.root(txn);
db.checkout("bunch of ints, added one by one");
verify(db.root(txn) == std::string(32, '\0'));
for (int i=0; i<n; i++) {
std::string s = std::to_string(i);
db.put(txn, s, s+s); // not in batch
}
stats = db.stats(txn);
verify(stats.numLeafNodes == (uint64_t)n);
verify(db.root(txn) == origRoot);
db.checkout("bunch of ints, added one by one in reverse");
verify(db.root(txn) == std::string(32, '\0'));
for (int i=n-1; i>=0; i--) {
std::string s = std::to_string(i);
db.change().put(s, s+s).apply(txn); // not in batch
}
stats = db.stats(txn);
verify(stats.numLeafNodes == (uint64_t)n);
verify(db.root(txn) == origRoot);
});
test("large mixed update/del", [&]{
equivHeads("", [&]{
auto changes = db.change();
for (int i=0; i<600; i++) {
std::string s = std::to_string(i);
changes.put(s, s+s);
}
changes.apply(txn);
// delete 0-99
for (int i=0; i<100; i++) changes.del(std::to_string(i));
// update 100-199
for (int i=100; i<200; i++) {
std::string s = std::to_string(i);
changes.put(s, s+s+"updated");
}
// add 600-699
for (int i=600; i<700; i++) {
std::string s = std::to_string(i);
changes.put(s, s+s);
}
changes.apply(txn);
},[&]{
auto changes = db.change();
for (int i=100; i<200; i++) {
std::string s = std::to_string(i);
changes.put(s, s+s+"updated");
}
for (int i=200; i<700; i++) {
std::string s = std::to_string(i);
changes.put(s, s+s);
}
changes.apply(txn);
});
});
test("back up start of iterator window", [&]{
db.change()
.put("a", "A")
.put("b", "B")
.apply(txn);
std::string_view val;
verify(db.get(txn, "a", val));
verify(val == "A");
verify(db.get(txn, "b", val));
verify(val == "B");
auto stats = db.stats(txn);
verify(stats.numLeafNodes == 2);
});
test("fork", [&]{
std::string_view val;
db.change()
.put("a", "A")
.put("b", "B")
.put("c", "C")
.put("d", "D")
.apply(txn);
auto origNodeId = db.getHeadNodeId(txn);
db.fork(txn);
db.change()
.put("e", "E")
.apply(txn);
auto newNodeId = db.getHeadNodeId(txn);
verify(db.get(txn, "a", val));
verify(val == "A");
verify(db.get(txn, "e", val));
verify(val == "E");
{
auto stats = db.stats(txn);
verify(stats.numLeafNodes == 5);
}
db.checkout(origNodeId);
verify(db.get(txn, "a", val));
verify(val == "A");
verify(!db.get(txn, "e", val));
{
auto stats = db.stats(txn);
verify(stats.numLeafNodes == 4);
}
db.checkout(newNodeId);
verify(db.get(txn, "a", val));
verify(val == "A");
verify(db.get(txn, "e", val));
verify(val == "E");
});
test("re-use leafs", [&]{
// Setup a simple tree
{
auto changes = db.change();
for (int i=0; i<10; i++) {
std::string s = std::to_string(i);
changes.put(quadrable::Key::fromInteger(i), std::string("N = ") + s);
}
changes.apply(txn);
}
uint64_t origHeadNodeId = db.getHeadNodeId(txn);
auto origRoot = db.root(txn);
// Get the nodeId for one of the leafs to test later
uint64_t sampleLeafNodeId = 99999999;
{
std::string_view val;
db.getRaw(txn, quadrable::Key::fromInteger(6).str(), val, &sampleLeafNodeId);
}
// Build up a change-set that reuses all the leafs
auto changes = db.change();
auto it = db.iterate(txn, quadrable::Key::null());
uint64_t newSampleNodeId = 0;
while (!it.atEnd()) {
changes.putReuse(txn, it.get().nodeId, it.get().key().toInteger() == 6 ? &newSampleNodeId : nullptr);
it.next();
}
// Checkout a new tree and apply the changeset
db.checkout();
changes.apply(txn);
// Roots are equal but node ids aren't
verify(db.root(txn) == origRoot);
verify(db.getHeadNodeId(txn) != origHeadNodeId);
verify(newSampleNodeId == sampleLeafNodeId);
});
test("basic proof", [&]{
auto changes = db.change();
for (int i=0; i<100; i++) {
std::string s = std::to_string(i);
changes.put(s, s + "val");
}
changes.put("long", std::string(789, 'A')); // test varints
changes.apply(txn);
auto origRoot = db.root(txn);
auto proof = proofRoundtrip(db.exportProof(txn, {
"99",
"68",
"long",
"asdf",
}));
db.checkout();
db.importProof(txn, proof, origRoot);
std::string_view val;
verify(db.get(txn, "99", val));
verify(val == "99val");
verify(db.get(txn, "68", val));
verify(val == "68val");
verify(db.get(txn, "long", val));
verify(val == std::string(789, 'A'));
verify(!db.get(txn, "asdf", val));
verifyThrow(db.get(txn, "0", val), "incomplete tree");
});
test("use same empty node for multiple keys", [&]{
db.change()
.put("735838777414", "A") // 000...
.put("367300200150", "B") // 001...
.apply(txn);
auto origRoot = db.root(txn);
auto proof = proofRoundtrip(db.exportProof(txn, {
"582086612140", // 010
"37481825503", // 011
}));
db.checkout();
db.importProof(txn, proof, origRoot);
std::string_view val;
verify(!db.get(txn, "582086612140", val));
verify(!db.get(txn, "37481825503", val));
verify(!db.get(txn, "915377487270", val)); // another 011... (uses empty)
verifyThrow(db.get(txn, "735838777414", val), "incomplete tree"); // exists as a witness only
verifyThrow(db.get(txn, "367300200150", val), "incomplete tree"); // exists as a witness only
});
test("more proofs", [&]{
db.change()
.put("983467173326", "A") // 10...
.put("50728759955", "B") // 11...
.put("679040280359", "C") // 01...
.put("685903554406", "D") // 000...
.put("66727828072", "E") // 00001...
.apply(txn);
auto origRoot = db.root(txn);
auto proof = proofRoundtrip(db.exportProof(txn, {
"983467173326",
"50728759955",
"836336493412", // 00..
"826547358742", // 001..
"231172376960", // 001..
}));
db.checkout();
db.importProof(txn, proof, origRoot);
std::string_view val;
verify(db.get(txn, "983467173326", val));
verify(val == "A");
verify(db.get(txn, "50728759955", val));
verify(val == "B");
verifyThrow(db.get(txn, "679040280359", val), "incomplete tree"); // exists as a witness only
verify(!db.get(txn, "826547358742", val));
verify(!db.get(txn, "836336493412", val));
verify(!db.get(txn, "231172376960", val));
});
test("big proof test", [&]{
auto changes = db.change();
for (int i=0; i<1000; i++) {
std::string s = std::to_string(i);
changes.put(s, s + "val");
}
changes.apply(txn);
auto origRoot = db.root(txn);
std::vector<std::string> keys;
for (int i=-500; i<500; i++) {
keys.emplace_back(std::to_string(i));
}
auto proof = proofRoundtrip(db.exportProof(txn, keys));
db.checkout();
db.importProof(txn, proof, origRoot);
quadrable::GetMultiQuery query{};
for (int i=-500; i<500; i++) query.emplace(std::to_string(i), GetMultiResult{});
db.getMulti(txn, query);
for (int i=-500; i<500; i++) {
auto str = std::to_string(i);
if (i < 0) {
verify(!query[str].exists);
} else {
verify(query[str].exists && query[str].val == str+"val");
}
}
});
test("sub-proof test", [&]{
std::string_view val;
auto changes = db.change();
for (int i=0; i<100; i++) {
std::string s = std::to_string(i);
changes.put(s, s + "val");
}
changes.apply(txn);
auto origRoot = db.root(txn);
quadrable::Proof proof, proof2;
{
std::vector<std::string> keys;
for (int i=-50; i<50; i++) {
keys.emplace_back(std::to_string(i));
}
proof = proofRoundtrip(db.exportProof(txn, keys));
}
db.checkout();
db.importProof(txn, proof, origRoot);
verify(db.get(txn, "33", val));
verify(val == "33val");
{
std::vector<std::string> keys;
for (int i=-10; i<10; i++) {
keys.emplace_back(std::to_string(i));
}
proof2 = proofRoundtrip(db.exportProof(txn, keys));
}
db.checkout();
db.importProof(txn, proof2, origRoot);
quadrable::GetMultiQuery query{};
for (int i=-10; i<10; i++) query.emplace(std::to_string(i), GetMultiResult{});
db.getMulti(txn, query);
for (int i=-10; i<10; i++) {
auto str = std::to_string(i);
if (i < 0) {
verify(!query[str].exists);
} else {
verify(query[str].exists && query[str].val == str+"val");
}
}
verifyThrow(db.get(txn, "33", val), "incomplete tree");
});
test("no unnecessary empty witnesses", [&]{
db.change()
.put("983467173326", "A") // 10...
.put("50728759955", "B") // 11...
.apply(txn);
auto origRoot = db.root(txn);
auto proof = proofRoundtrip(db.exportProof(txn, {
"983467173326",
"14864808866", // 00...
}));
verify(proof.strands.size() == 1); // No separate WitnessEmpty is needed because a HashEmpty cmd is on existing node's path
db.checkout();
db.importProof(txn, proof, origRoot);
std::string_view val;
verify(db.get(txn, "983467173326", val));
verify(val == "A");
verifyThrow(db.get(txn, "50728759955", val), "incomplete tree");
verify(!db.get(txn, "14864808866", val));
});
test("update proof", [&]{
auto setupDb = [&]{
db.change()
.put("353568684874", "A") // 01...
.put("852771900452", "B") // 1000...
.put("877307249616", "C") // 101...
.put("640237942109", "D") // 1001...
.apply(txn);
};
quadrable::Proof proof;
std::string origRoot, newRoot;
std::string_view val;
equivHeads("update leaf, fail trying to update witness", [&]{
setupDb();
proof = proofRoundtrip(db.exportProof(txn, {
"353568684874",
}));
origRoot = db.root(txn);
db.change().put("353568684874", "A2").apply(txn);
newRoot = db.root(txn);
}, [&]{
db.importProof(txn, proof, origRoot);
db.change().put("353568684874", "A2").apply(txn);
verify(db.root(txn) == newRoot); // also checked by equivHeads
verifyThrow(db.change().put("852771900452", "B2").apply(txn), "encountered witness during update");
});
equivHeads("update 2 leafs at different levels", [&]{
setupDb();
proof = proofRoundtrip(db.exportProof(txn, {
"852771900452",
"877307249616",
}));
origRoot = db.root(txn);
db.change().put("852771900452", "B2").apply(txn);
db.change().put("877307249616", "C2").apply(txn);
}, [&]{
db.importProof(txn, proof, origRoot);
db.change().put("852771900452", "B2").apply(txn);
db.change().put("877307249616", "C2").apply(txn);
});
equivHeads("split leaf", [&]{
setupDb();
proof = proofRoundtrip(db.exportProof(txn, {
"852771900452",
}));
origRoot = db.root(txn);
db.change().put("762909246408", "E").apply(txn); // 1000...
}, [&]{
db.importProof(txn, proof, origRoot);
db.change().put("762909246408", "E").apply(txn); // 1000...
});
equivHeads("no change to witness leaf", [&]{
setupDb();