-
Notifications
You must be signed in to change notification settings - Fork 30
/
SHAMap.cpp
1381 lines (1198 loc) · 39.2 KB
/
SHAMap.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
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/basics/contract.h>
#include <ripple/shamap/SHAMap.h>
namespace ripple {
SHAMap::SHAMap (
SHAMapType t,
Family& f,
version v)
: f_ (f)
, journal_(f.journal())
, seq_ (1)
, state_ (SHAMapState::Modifying)
, type_ (t)
{
if (v == version{2})
root_ = std::make_shared<SHAMapInnerNodeV2>(seq_, 0);
else
root_ = std::make_shared<SHAMapInnerNode>(seq_);
}
SHAMap::SHAMap (
SHAMapType t,
uint256 const& hash,
Family& f,
version v)
: f_ (f)
, journal_(f.journal())
, seq_ (1)
, state_ (SHAMapState::Synching)
, type_ (t)
{
if (v == version{2})
root_ = std::make_shared<SHAMapInnerNodeV2>(seq_, 0);
else
root_ = std::make_shared<SHAMapInnerNode>(seq_);
}
SHAMap::~SHAMap ()
{
state_ = SHAMapState::Invalid;
}
std::shared_ptr<SHAMap>
SHAMap::snapShot (bool isMutable) const
{
auto ret = std::make_shared<SHAMap> (type_, f_, get_version());
SHAMap& newMap = *ret;
if (!isMutable)
newMap.state_ = SHAMapState::Immutable;
newMap.seq_ = seq_ + 1;
newMap.ledgerSeq_ = ledgerSeq_;
newMap.root_ = root_;
newMap.backed_ = backed_;
if ((state_ != SHAMapState::Immutable) || !isMutable)
{
// If either map may change, they cannot share nodes
newMap.unshare ();
}
return ret;
}
std::shared_ptr<SHAMap>
SHAMap::make_v2() const
{
assert(!is_v2());
auto ret = std::make_shared<SHAMap>(type_, f_, version{2});
ret->seq_ = seq_ + 1;
SharedPtrNodeStack stack;
for (auto leaf = peekFirstItem(stack); leaf != nullptr;
leaf = peekNextItem(leaf->peekItem()->key(), stack))
{
auto node_type = leaf->getType();
ret->addGiveItem(leaf->peekItem(),
node_type != SHAMapTreeNode::tnACCOUNT_STATE,
node_type == SHAMapTreeNode::tnTRANSACTION_MD);
}
NodeObjectType t;
switch (type_)
{
case SHAMapType::TRANSACTION:
t = hotTRANSACTION_NODE;
break;
case SHAMapType::STATE:
t = hotACCOUNT_NODE;
break;
default:
t = hotUNKNOWN;
break;
}
ret->flushDirty(t, ret->seq_);
ret->unshare();
return ret;
}
std::shared_ptr<SHAMap>
SHAMap::make_v1() const
{
assert(is_v2());
auto ret = std::make_shared<SHAMap>(type_, f_, version{1});
ret->seq_ = seq_ + 1;
SharedPtrNodeStack stack;
for (auto leaf = peekFirstItem(stack); leaf != nullptr;
leaf = peekNextItem(leaf->peekItem()->key(), stack))
{
auto node_type = leaf->getType();
ret->addGiveItem(leaf->peekItem(),
node_type != SHAMapTreeNode::tnACCOUNT_STATE,
node_type == SHAMapTreeNode::tnTRANSACTION_MD);
}
NodeObjectType t;
switch (type_)
{
case SHAMapType::TRANSACTION:
t = hotTRANSACTION_NODE;
break;
case SHAMapType::STATE:
t = hotACCOUNT_NODE;
break;
default:
t = hotUNKNOWN;
break;
}
ret->flushDirty(t, ret->seq_);
ret->unshare();
return ret;
}
void
SHAMap::dirtyUp (SharedPtrNodeStack& stack,
uint256 const& target, std::shared_ptr<SHAMapAbstractNode> child)
{
// walk the tree up from through the inner nodes to the root_
// update hashes and links
// stack is a path of inner nodes up to, but not including, child
// child can be an inner node or a leaf
assert ((state_ != SHAMapState::Synching) && (state_ != SHAMapState::Immutable));
assert (child && (child->getSeq() == seq_));
while (!stack.empty ())
{
auto node = std::dynamic_pointer_cast<SHAMapInnerNode>(stack.top ().first);
SHAMapNodeID nodeID = stack.top ().second;
stack.pop ();
assert (node != nullptr);
int branch = nodeID.selectBranch (target);
assert (branch >= 0);
node = unshareNode(std::move(node), nodeID);
node->setChild (branch, child);
child = std::move (node);
}
}
SHAMapTreeNode*
SHAMap::walkTowardsKey(uint256 const& id, SharedPtrNodeStack* stack) const
{
assert(stack == nullptr || stack->empty());
auto inNode = root_;
SHAMapNodeID nodeID;
auto const isv2 = is_v2();
while (inNode->isInner())
{
if (stack != nullptr)
stack->push({inNode, nodeID});
if (isv2)
{
auto n = std::static_pointer_cast<SHAMapInnerNodeV2>(inNode);
if (!n->has_common_prefix(id))
return nullptr;
}
auto const inner = std::static_pointer_cast<SHAMapInnerNode>(inNode);
auto const branch = nodeID.selectBranch (id);
if (inner->isEmptyBranch (branch))
return nullptr;
inNode = descendThrow (inner, branch);
if (isv2)
{
if (inNode->isInner())
{
auto n = std::dynamic_pointer_cast<SHAMapInnerNodeV2>(inNode);
if (n == nullptr)
{
assert (false);
return nullptr;
}
nodeID = SHAMapNodeID{n->depth(), n->common()};
}
else
{
nodeID = SHAMapNodeID{64, inNode->key()};
}
}
else
{
nodeID = nodeID.getChildNodeID (branch);
}
}
if (stack != nullptr)
stack->push({inNode, nodeID});
return static_cast<SHAMapTreeNode*>(inNode.get());
}
SHAMapTreeNode*
SHAMap::findKey(uint256 const& id) const
{
SHAMapTreeNode* leaf = walkTowardsKey(id);
if (leaf && leaf->peekItem()->key() != id)
leaf = nullptr;
return leaf;
}
std::shared_ptr<SHAMapAbstractNode>
SHAMap::fetchNodeFromDB (SHAMapHash const& hash) const
{
std::shared_ptr<SHAMapAbstractNode> node;
if (backed_)
{
if (auto obj = f_.db().fetch(hash.as_uint256(), ledgerSeq_))
{
try
{
node = SHAMapAbstractNode::make(makeSlice(obj->getData()),
0, snfPREFIX, hash, true, f_.journal());
if (node && node->isInner())
{
bool isv2 = std::dynamic_pointer_cast<SHAMapInnerNodeV2>(node) != nullptr;
if (isv2 != is_v2())
{
auto root = std::dynamic_pointer_cast<SHAMapInnerNode>(root_);
assert(root);
assert(root->isEmpty());
if (isv2)
{
auto temp = make_v2();
swap(temp->root_, const_cast<std::shared_ptr<SHAMapAbstractNode>&>(root_));
}
else
{
auto temp = make_v1();
swap(temp->root_, const_cast<std::shared_ptr<SHAMapAbstractNode>&>(root_));
}
}
}
if (node)
canonicalize (hash, node);
}
catch (std::exception const&)
{
JLOG(journal_.warn()) <<
"Invalid DB node " << hash;
return std::shared_ptr<SHAMapTreeNode> ();
}
}
else if (full_)
{
f_.missing_node(ledgerSeq_);
const_cast<bool&>(full_) = false;
}
}
return node;
}
// See if a sync filter has a node
std::shared_ptr<SHAMapAbstractNode>
SHAMap::checkFilter(SHAMapHash const& hash,
SHAMapSyncFilter* filter) const
{
std::shared_ptr<SHAMapAbstractNode> node;
if (auto nodeData = filter->getNode (hash))
{
node = SHAMapAbstractNode::make(
makeSlice(*nodeData), 0, snfPREFIX, hash, true, f_.journal ());
if (node)
{
filter->gotNode (true, hash, ledgerSeq_,
std::move(*nodeData), node->getType ());
if (backed_)
canonicalize (hash, node);
}
}
return node;
}
// Get a node without throwing
// Used on maps where missing nodes are expected
std::shared_ptr<SHAMapAbstractNode> SHAMap::fetchNodeNT(
SHAMapHash const& hash,
SHAMapSyncFilter* filter) const
{
std::shared_ptr<SHAMapAbstractNode> node = getCache (hash);
if (node)
return node;
if (backed_)
{
node = fetchNodeFromDB (hash);
if (node)
{
canonicalize (hash, node);
return node;
}
}
if (filter)
node = checkFilter (hash, filter);
return node;
}
std::shared_ptr<SHAMapAbstractNode> SHAMap::fetchNodeNT (SHAMapHash const& hash) const
{
auto node = getCache (hash);
if (!node && backed_)
node = fetchNodeFromDB (hash);
return node;
}
// Throw if the node is missing
std::shared_ptr<SHAMapAbstractNode> SHAMap::fetchNode (SHAMapHash const& hash) const
{
auto node = fetchNodeNT (hash);
if (!node)
Throw<SHAMapMissingNode> (type_, hash);
return node;
}
SHAMapAbstractNode* SHAMap::descendThrow (SHAMapInnerNode* parent, int branch) const
{
SHAMapAbstractNode* ret = descend (parent, branch);
if (! ret && ! parent->isEmptyBranch (branch))
Throw<SHAMapMissingNode> (type_, parent->getChildHash (branch));
return ret;
}
std::shared_ptr<SHAMapAbstractNode>
SHAMap::descendThrow (std::shared_ptr<SHAMapInnerNode> const& parent, int branch) const
{
std::shared_ptr<SHAMapAbstractNode> ret = descend (parent, branch);
if (! ret && ! parent->isEmptyBranch (branch))
Throw<SHAMapMissingNode> (type_, parent->getChildHash (branch));
return ret;
}
SHAMapAbstractNode* SHAMap::descend (SHAMapInnerNode* parent, int branch) const
{
SHAMapAbstractNode* ret = parent->getChildPointer (branch);
if (ret || !backed_)
return ret;
std::shared_ptr<SHAMapAbstractNode> node = fetchNodeNT (parent->getChildHash (branch));
if (!node || isInconsistentNode(node))
return nullptr;
node = parent->canonicalizeChild (branch, std::move(node));
return node.get ();
}
std::shared_ptr<SHAMapAbstractNode>
SHAMap::descend (std::shared_ptr<SHAMapInnerNode> const& parent, int branch) const
{
std::shared_ptr<SHAMapAbstractNode> node = parent->getChild (branch);
if (node || !backed_)
return node;
node = fetchNode (parent->getChildHash (branch));
if (!node || isInconsistentNode(node))
return nullptr;
node = parent->canonicalizeChild (branch, std::move(node));
return node;
}
// Gets the node that would be hooked to this branch,
// but doesn't hook it up.
std::shared_ptr<SHAMapAbstractNode>
SHAMap::descendNoStore (std::shared_ptr<SHAMapInnerNode> const& parent, int branch) const
{
std::shared_ptr<SHAMapAbstractNode> ret = parent->getChild (branch);
if (!ret && backed_)
ret = fetchNode (parent->getChildHash (branch));
return ret;
}
std::pair <SHAMapAbstractNode*, SHAMapNodeID>
SHAMap::descend (SHAMapInnerNode * parent, SHAMapNodeID const& parentID,
int branch, SHAMapSyncFilter * filter) const
{
assert (parent->isInner ());
assert ((branch >= 0) && (branch < 16));
assert (!parent->isEmptyBranch (branch));
SHAMapAbstractNode* child = parent->getChildPointer (branch);
auto const& childHash = parent->getChildHash (branch);
if (!child)
{
std::shared_ptr<SHAMapAbstractNode> childNode = fetchNodeNT (childHash, filter);
if (childNode)
{
childNode = parent->canonicalizeChild (branch, std::move(childNode));
child = childNode.get ();
}
if (child && isInconsistentNode(childNode))
child = nullptr;
}
if (child && is_v2())
{
if (child->isInner())
{
auto n = static_cast<SHAMapInnerNodeV2*>(child);
return std::make_pair(child, SHAMapNodeID{n->depth(), n->key()});
}
return std::make_pair(child, SHAMapNodeID{64, child->key()});
}
return std::make_pair (child, parentID.getChildNodeID (branch));
}
SHAMapAbstractNode*
SHAMap::descendAsync (SHAMapInnerNode* parent, int branch,
SHAMapSyncFilter * filter, bool & pending) const
{
pending = false;
SHAMapAbstractNode* ret = parent->getChildPointer (branch);
if (ret)
return ret;
auto const& hash = parent->getChildHash (branch);
std::shared_ptr<SHAMapAbstractNode> ptr = getCache (hash);
if (!ptr)
{
if (filter)
ptr = checkFilter (hash, filter);
if (!ptr && backed_)
{
std::shared_ptr<NodeObject> obj;
if (! f_.db().asyncFetch (hash.as_uint256(), ledgerSeq_, obj))
{
pending = true;
return nullptr;
}
if (!obj)
return nullptr;
ptr = SHAMapAbstractNode::make(makeSlice(obj->getData()), 0, snfPREFIX,
hash, true, f_.journal());
if (ptr && backed_)
canonicalize (hash, ptr);
}
}
if (ptr && isInconsistentNode(ptr))
ptr = nullptr;
if (ptr)
ptr = parent->canonicalizeChild (branch, std::move(ptr));
return ptr.get ();
}
template <class Node>
std::shared_ptr<Node>
SHAMap::unshareNode (std::shared_ptr<Node> node, SHAMapNodeID const& nodeID)
{
// make sure the node is suitable for the intended operation (copy on write)
assert (node->isValid ());
assert (node->getSeq () <= seq_);
if (node->getSeq () != seq_)
{
// have a CoW
assert (state_ != SHAMapState::Immutable);
node = std::static_pointer_cast<Node>(node->clone(seq_));
assert (node->isValid ());
if (nodeID.isRoot ())
root_ = node;
}
return node;
}
SHAMapTreeNode*
SHAMap::firstBelow(std::shared_ptr<SHAMapAbstractNode> node,
SharedPtrNodeStack& stack, int branch) const
{
// Return the first item at or below this node
if (node->isLeaf())
{
auto n = std::static_pointer_cast<SHAMapTreeNode>(node);
stack.push({node, {64, n->peekItem()->key()}});
return n.get();
}
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
if (stack.empty())
stack.push({inner, SHAMapNodeID{}});
else
{
if (is_v2())
{
auto inner2 = std::dynamic_pointer_cast<SHAMapInnerNodeV2>(inner);
assert(inner2 != nullptr);
stack.push({inner2, {inner2->depth(), inner2->common()}});
}
else
{
stack.push({inner, stack.top().second.getChildNodeID(branch)});
}
}
for (int i = 0; i < 16;)
{
if (!inner->isEmptyBranch(i))
{
node = descendThrow(inner, i);
assert(!stack.empty());
if (node->isLeaf())
{
auto n = std::static_pointer_cast<SHAMapTreeNode>(node);
stack.push({n, {64, n->peekItem()->key()}});
return n.get();
}
inner = std::static_pointer_cast<SHAMapInnerNode>(node);
if (is_v2())
{
auto inner2 = std::static_pointer_cast<SHAMapInnerNodeV2>(inner);
stack.push({inner2, {inner2->depth(), inner2->common()}});
}
else
{
stack.push({inner, stack.top().second.getChildNodeID(branch)});
}
i = 0; // scan all 16 branches of this new node
}
else
++i; // scan next branch
}
return nullptr;
}
static const std::shared_ptr<SHAMapItem const> no_item;
std::shared_ptr<SHAMapItem const> const&
SHAMap::onlyBelow (SHAMapAbstractNode* node) const
{
// If there is only one item below this node, return it
while (!node->isLeaf ())
{
SHAMapAbstractNode* nextNode = nullptr;
auto inner = static_cast<SHAMapInnerNode*>(node);
for (int i = 0; i < 16; ++i)
{
if (!inner->isEmptyBranch (i))
{
if (nextNode)
return no_item;
nextNode = descendThrow (inner, i);
}
}
if (!nextNode)
{
assert (false);
return no_item;
}
node = nextNode;
}
// An inner node must have at least one leaf
// below it, unless it's the root_
auto leaf = static_cast<SHAMapTreeNode*>(node);
assert (leaf->hasItem () || (leaf == root_.get ()));
return leaf->peekItem ();
}
static std::shared_ptr<
SHAMapItem const> const nullConstSHAMapItem;
SHAMapTreeNode const*
SHAMap::peekFirstItem(SharedPtrNodeStack& stack) const
{
assert(stack.empty());
SHAMapTreeNode* node = firstBelow(root_, stack);
if (!node)
{
while (!stack.empty())
stack.pop();
return nullptr;
}
return node;
}
SHAMapTreeNode const*
SHAMap::peekNextItem(uint256 const& id, SharedPtrNodeStack& stack) const
{
assert(!stack.empty());
assert(stack.top().first->isLeaf());
stack.pop();
while (!stack.empty())
{
auto node = stack.top().first;
auto nodeID = stack.top().second;
assert(!node->isLeaf());
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
for (auto i = nodeID.selectBranch(id) + 1; i < 16; ++i)
{
if (!inner->isEmptyBranch(i))
{
node = descendThrow(inner, i);
auto leaf = firstBelow(node, stack, i);
if (!leaf)
Throw<SHAMapMissingNode> (type_, id);
assert(leaf->isLeaf());
return leaf;
}
}
stack.pop();
}
// must be last item
return nullptr;
}
std::shared_ptr<SHAMapItem const> const&
SHAMap::peekItem (uint256 const& id) const
{
SHAMapTreeNode* leaf = findKey(id);
if (!leaf)
return no_item;
return leaf->peekItem ();
}
std::shared_ptr<SHAMapItem const> const&
SHAMap::peekItem (uint256 const& id, SHAMapTreeNode::TNType& type) const
{
SHAMapTreeNode* leaf = findKey(id);
if (!leaf)
return no_item;
type = leaf->getType ();
return leaf->peekItem ();
}
std::shared_ptr<SHAMapItem const> const&
SHAMap::peekItem (uint256 const& id, SHAMapHash& hash) const
{
SHAMapTreeNode* leaf = findKey(id);
if (!leaf)
return no_item;
hash = leaf->getNodeHash ();
return leaf->peekItem ();
}
SHAMap::const_iterator
SHAMap::upper_bound(uint256 const& id) const
{
// Get a const_iterator to the next item in the tree after a given item
// item need not be in tree
SharedPtrNodeStack stack;
walkTowardsKey(id, &stack);
std::shared_ptr<SHAMapAbstractNode> node;
SHAMapNodeID nodeID;
auto const isv2 = is_v2();
while (!stack.empty())
{
std::tie(node, nodeID) = stack.top();
if (node->isLeaf())
{
auto leaf = static_cast<SHAMapTreeNode*>(node.get());
if (leaf->peekItem()->key() > id)
return const_iterator(this, leaf->peekItem().get(), std::move(stack));
}
else
{
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
int branch;
if (isv2)
{
auto n = std::static_pointer_cast<SHAMapInnerNodeV2>(inner);
if (n->has_common_prefix(id))
branch = nodeID.selectBranch(id) + 1;
else if (id < n->common())
branch = 0;
else
branch = 16;
}
else
{
branch = nodeID.selectBranch(id) + 1;
}
for (; branch < 16; ++branch)
{
if (!inner->isEmptyBranch(branch))
{
node = descendThrow(inner, branch);
auto leaf = firstBelow(node, stack, branch);
if (!leaf)
Throw<SHAMapMissingNode> (type_, id);
return const_iterator(this, leaf->peekItem().get(),
std::move(stack));
}
}
}
stack.pop();
}
return end();
}
bool SHAMap::hasItem (uint256 const& id) const
{
// does the tree have an item with this ID
SHAMapTreeNode* leaf = findKey(id);
return (leaf != nullptr);
}
bool SHAMap::delItem (uint256 const& id)
{
// delete the item with this ID
assert (state_ != SHAMapState::Immutable);
SharedPtrNodeStack stack;
walkTowardsKey(id, &stack);
if (stack.empty ())
Throw<SHAMapMissingNode> (type_, id);
auto leaf = std::dynamic_pointer_cast<SHAMapTreeNode>(stack.top ().first);
stack.pop ();
if (!leaf || (leaf->peekItem ()->key() != id))
return false;
SHAMapTreeNode::TNType type = leaf->getType ();
// What gets attached to the end of the chain
// (For now, nothing, since we deleted the leaf)
std::shared_ptr<SHAMapAbstractNode> prevNode;
while (!stack.empty ())
{
auto node = std::static_pointer_cast<SHAMapInnerNode>(stack.top().first);
SHAMapNodeID nodeID = stack.top().second;
stack.pop();
node = unshareNode(std::move(node), nodeID);
node->setChild(nodeID.selectBranch(id), prevNode);
if (!nodeID.isRoot ())
{
// we may have made this a node with 1 or 0 children
// And, if so, we need to remove this branch
int bc = node->getBranchCount();
if (is_v2())
{
assert(bc != 0);
if (bc == 1)
{
for (int i = 0; i < 16; ++i)
{
if (!node->isEmptyBranch (i))
{
prevNode = descendThrow(node, i);
break;
}
}
}
else // bc >= 2
{
// This node is now the end of the branch
prevNode = std::move(node);
}
}
else
{
if (bc == 0)
{
// no children below this branch
prevNode.reset ();
}
else if (bc == 1)
{
// If there's only one item, pull up on the thread
auto item = onlyBelow (node.get ());
if (item)
{
for (int i = 0; i < 16; ++i)
{
if (!node->isEmptyBranch (i))
{
node->setChild (i, nullptr);
break;
}
}
prevNode = std::make_shared<SHAMapTreeNode>(item, type, node->getSeq());
}
else
{
prevNode = std::move (node);
}
}
else
{
// This node is now the end of the branch
prevNode = std::move (node);
}
}
}
}
return true;
}
static
uint256
prefix(unsigned depth, uint256 const& key)
{
uint256 r{};
auto x = r.begin();
auto y = key.begin();
for (auto i = 0; i < depth/2; ++i, ++x, ++y)
*x = *y;
if (depth & 1)
*x = *y & 0xF0;
return r;
}
bool
SHAMap::addGiveItem (std::shared_ptr<SHAMapItem const> const& item,
bool isTransaction, bool hasMeta)
{
// add the specified item, does not update
uint256 tag = item->key();
SHAMapTreeNode::TNType type = !isTransaction ? SHAMapTreeNode::tnACCOUNT_STATE :
(hasMeta ? SHAMapTreeNode::tnTRANSACTION_MD : SHAMapTreeNode::tnTRANSACTION_NM);
assert (state_ != SHAMapState::Immutable);
SharedPtrNodeStack stack;
walkTowardsKey(tag, &stack);
if (stack.empty ())
Throw<SHAMapMissingNode> (type_, tag);
auto node = stack.top ().first;
auto nodeID = stack.top ().second;
stack.pop ();
if (node->isLeaf())
{
auto leaf = std::static_pointer_cast<SHAMapTreeNode>(node);
if (leaf->peekItem()->key() == tag)
return false;
}
node = unshareNode(std::move(node), nodeID);
if (is_v2())
{
if (node->isInner())
{
auto inner = std::static_pointer_cast<SHAMapInnerNodeV2>(node);
if (inner->has_common_prefix(tag))
{
int branch = nodeID.selectBranch(tag);
assert(inner->isEmptyBranch(branch));
auto newNode = std::make_shared<SHAMapTreeNode>(item, type, seq_);
inner->setChild(branch, newNode);
}
else
{
assert(!stack.empty());
auto parent = unshareNode(
std::static_pointer_cast<SHAMapInnerNodeV2>(stack.top().first),
stack.top().second);
stack.top().first = parent;
auto parent_depth = parent->depth();
auto depth = inner->get_common_prefix(tag);
auto new_inner = std::make_shared<SHAMapInnerNodeV2>(seq_);
nodeID = SHAMapNodeID{depth, prefix(depth, inner->common())};
new_inner->setChild(nodeID.selectBranch(inner->common()), inner);
nodeID = SHAMapNodeID{depth, prefix(depth, tag)};
new_inner->setChild(nodeID.selectBranch(tag),
std::make_shared<SHAMapTreeNode>(item, type, seq_));
new_inner->set_common(depth, prefix(depth, tag));
nodeID = SHAMapNodeID{parent_depth, prefix(parent_depth, tag)};
parent->setChild(nodeID.selectBranch(tag), new_inner);
node = new_inner;
}
}
else
{
auto leaf = std::static_pointer_cast<SHAMapTreeNode>(node);
auto inner = std::make_shared<SHAMapInnerNodeV2>(seq_);
inner->setChildren(leaf, std::make_shared<SHAMapTreeNode>(item, type, seq_));
assert(!stack.empty());
auto parent = unshareNode(
std::static_pointer_cast<SHAMapInnerNodeV2>(stack.top().first),
stack.top().second);
stack.top().first = parent;
node = inner;
}
}
else // !is_v2()
{
if (node->isInner ())
{
// easy case, we end on an inner node
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
int branch = nodeID.selectBranch (tag);
assert (inner->isEmptyBranch (branch));
auto newNode = std::make_shared<SHAMapTreeNode> (item, type, seq_);
inner->setChild (branch, newNode);
}
else
{
// this is a leaf node that has to be made an inner node holding two items
auto leaf = std::static_pointer_cast<SHAMapTreeNode>(node);
std::shared_ptr<SHAMapItem const> otherItem = leaf->peekItem ();
assert (otherItem && (tag != otherItem->key()));
node = std::make_shared<SHAMapInnerNode>(node->getSeq());
int b1, b2;
while ((b1 = nodeID.selectBranch (tag)) ==
(b2 = nodeID.selectBranch (otherItem->key())))
{
stack.push ({node, nodeID});
// we need a new inner node, since both go on same branch at this level
nodeID = nodeID.getChildNodeID (b1);
node = std::make_shared<SHAMapInnerNode> (seq_);
}
// we can add the two leaf nodes here
assert (node->isInner ());
std::shared_ptr<SHAMapTreeNode> newNode =
std::make_shared<SHAMapTreeNode> (item, type, seq_);
assert (newNode->isValid () && newNode->isLeaf ());
auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
inner->setChild (b1, newNode);
newNode = std::make_shared<SHAMapTreeNode> (otherItem, type, seq_);
assert (newNode->isValid () && newNode->isLeaf ());
inner->setChild (b2, newNode);
}
}
dirtyUp (stack, tag, node);
return true;