20 #include <ripple/basics/contract.h>
21 #include <ripple/shamap/SHAMap.h>
22 #include <ripple/shamap/SHAMapAccountStateLeafNode.h>
23 #include <ripple/shamap/SHAMapNodeID.h>
24 #include <ripple/shamap/SHAMapSyncFilter.h>
25 #include <ripple/shamap/SHAMapTxLeafNode.h>
26 #include <ripple/shamap/SHAMapTxPlusMetaLeafNode.h>
37 return std::make_shared<SHAMapTxLeafNode>(std::move(item), owner);
40 return std::make_shared<SHAMapTxPlusMetaLeafNode>(
41 std::move(item), owner);
44 return std::make_shared<SHAMapAccountStateLeafNode>(
45 std::move(item), owner);
48 "Attempt to create leaf node of unknown type " +
72 auto ret = std::make_shared<SHAMap>(
type_,
f_);
107 assert(child && (child->cowid() ==
cowid_));
109 while (!stack.
empty())
112 std::dynamic_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
115 assert(node !=
nullptr);
121 node->setChild(branch, child);
123 child = std::move(node);
130 assert(stack ==
nullptr || stack->
empty());
134 while (inNode->isInner())
136 if (stack !=
nullptr)
137 stack->
push({inNode, nodeID});
139 auto const inner = std::static_pointer_cast<SHAMapInnerNode>(inNode);
141 if (inner->isEmptyBranch(branch))
148 if (stack !=
nullptr)
149 stack->
push({inNode, nodeID});
157 if (leaf && leaf->
peekItem()->key() !=
id)
206 if (
auto nodeData = filter->
getNode(hash))
218 std::move(*nodeData),
228 <<
"Invalid node/data, hash=" << hash <<
": " << x.
what();
277 Throw<SHAMapMissingNode>(
type_, hash);
299 if (!ret && !parent->isEmptyBranch(branch))
300 Throw<SHAMapMissingNode>(
type_, parent->getChildHash(branch));
329 node =
fetchNode(parent->getChildHash(branch));
333 node = parent->canonicalizeChild(branch, std::move(node));
346 ret =
fetchNode(parent->getChildHash(branch));
372 child = childNode.
get();
406 [
this, hash, cb{std::move(callback)}](
408 auto node = finishFetch(hash, object);
422 template <
class Node>
427 assert(node->cowid() <=
cowid_);
428 if (node->cowid() !=
cowid_)
432 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
447 auto& [init, cmp, incr] = loopParams;
450 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
454 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
458 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
459 for (
int i = init; cmp(i);)
461 if (!inner->isEmptyBranch(i))
464 assert(!stack.
empty());
467 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
471 inner = std::static_pointer_cast<SHAMapInnerNode>(node);
472 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
487 auto cmp = [](
int i) {
return i >= 0; };
488 auto incr = [](
int& i) { --i; };
490 return belowHelper(node, stack, branch, {init, cmp, incr});
500 auto incr = [](
int& i) { ++i; };
502 return belowHelper(node, stack, branch, {init, cmp, incr});
517 if (!inner->isEmptyBranch(i))
538 assert(leaf->peekItem() || (leaf ==
root_.get()));
539 return leaf->peekItem();
545 assert(stack.
empty());
549 while (!stack.
empty())
559 assert(!stack.
empty());
560 assert(stack.
top().
first->isLeaf());
562 while (!stack.
empty())
564 auto [node, nodeID] = stack.
top();
565 assert(!node->isLeaf());
566 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
569 if (!inner->isEmptyBranch(i))
574 Throw<SHAMapMissingNode>(
type_,
id);
575 assert(leaf->isLeaf());
613 while (!stack.
empty())
615 auto [node, nodeID] = stack.
top();
619 if (leaf->peekItem()->key() > id)
621 this, leaf->peekItem().get(), std::move(stack));
625 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
630 if (!inner->isEmptyBranch(branch))
635 Throw<SHAMapMissingNode>(
type_,
id);
637 this, leaf->peekItem().get(), std::move(stack));
650 while (!stack.
empty())
652 auto [node, nodeID] = stack.
top();
656 if (leaf->peekItem()->key() < id)
658 this, leaf->peekItem().get(), std::move(stack));
662 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
663 for (
int branch =
selectBranch(nodeID,
id) - 1; branch >= 0;
666 if (!inner->isEmptyBranch(branch))
669 auto leaf =
lastBelow(node, stack, branch);
671 Throw<SHAMapMissingNode>(
type_,
id);
673 this, leaf->peekItem().get(), std::move(stack));
686 return (
findKey(
id) !=
nullptr);
699 Throw<SHAMapMissingNode>(
type_,
id);
701 auto leaf = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
704 if (!leaf || (leaf->peekItem()->key() !=
id))
713 while (!stack.
empty())
716 std::static_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
727 const int bc = node->getBranchCount();
742 if (!node->isEmptyBranch(i))
744 node->setChild(i,
nullptr);
753 prevNode = std::move(node);
759 prevNode = std::move(node);
780 Throw<SHAMapMissingNode>(
type_, tag);
782 auto [node, nodeID] = stack.
top();
787 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
788 if (leaf->peekItem()->key() == tag)
795 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
797 assert(inner->isEmptyBranch(branch));
799 inner->setChild(branch, newNode);
805 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
807 assert(otherItem && (tag != otherItem->key()));
809 node = std::make_shared<SHAMapInnerNode>(node->cowid());
816 stack.
push({node, nodeID});
820 nodeID = nodeID.getChildNodeID(b1);
821 node = std::make_shared<SHAMapInnerNode>(
cowid_);
825 assert(node->isInner());
839 return addGiveItem(type, std::make_shared<SHAMapItem const>(std::move(i)));
845 auto hash =
root_->getHash();
849 hash =
root_->getHash();
868 Throw<SHAMapMissingNode>(
type_, tag);
870 auto node = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
874 if (!node || (node->peekItem()->key() != tag))
880 if (node->getType() != type)
882 JLOG(
journal_.
fatal()) <<
"SHAMap::setItem: cross-type change!";
888 if (node->setItem(std::move(item)))
897 if (hash ==
root_->getHash())
904 stream <<
"Fetch root TXN node " << hash;
908 stream <<
"Fetch root STATE node " << hash;
912 stream <<
"Fetch root SHAMap node " << hash;
921 assert(
root_->getHash() == hash);
943 assert(node->cowid() == 0);
949 node->serializeWithPrefix(s);
958 template <
class Node>
964 assert(node->cowid() != 0);
966 if (node->cowid() !=
cowid_)
970 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
1002 root_->updateHash();
1011 auto node = std::static_pointer_cast<SHAMapInnerNode>(
root_);
1013 if (node->isEmpty())
1015 root_ = std::make_shared<SHAMapInnerNode>(0);
1033 if (node->isEmptyBranch(pos))
1042 auto child = node->getChild(pos++);
1044 if (child && (child->cowid() != 0))
1050 if (child->isInner())
1054 stack.
emplace(std::move(node), branch);
1058 node = std::static_pointer_cast<SHAMapInnerNode>(
1067 assert(node->cowid() ==
cowid_);
1068 child->updateHash();
1074 node->shareChild(branch, child);
1081 node->updateHashDeep();
1087 node = std::static_pointer_cast<SHAMapInnerNode>(
1095 auto parent = std::move(stack.
top().first);
1096 pos = stack.
top().second;
1100 assert(parent->cowid() ==
cowid_);
1101 parent->shareChild(pos, node);
1104 node = std::move(parent);
1109 root_ = std::move(node);
1125 auto [node, nodeID] = stack.
top();
1134 if (node->isInner())
1139 if (!inner->isEmptyBranch(i))
1141 auto child = inner->getChildPointer(i);
1144 assert(child->getHash() == inner->getChildHash(i));
1145 stack.
push({child, nodeID.getChildNodeID(i)});
1152 }
while (!stack.
empty());
1154 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1161 assert(!ret || !ret->cowid());
1171 assert(node->cowid() == 0);
1172 assert(node->getHash() == hash);
1175 ->canonicalize_replace_client(hash.
as_uint256(), node);
1182 auto node =
root_.get();
1183 assert(node !=
nullptr);
1184 assert(!node->isLeaf());
1189 node->invariants(
true);