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, std::move(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)
216 if (
auto nodeData = filter->
getNode(hash))
228 std::move(*nodeData),
238 <<
"Invalid node/data, hash=" << hash <<
": " << x.
what();
287 Throw<SHAMapMissingNode>(
type_, hash);
309 if (!ret && !parent->isEmptyBranch(branch))
310 Throw<SHAMapMissingNode>(
type_, parent->getChildHash(branch));
339 node =
fetchNode(parent->getChildHash(branch));
343 node = parent->canonicalizeChild(branch, std::move(node));
356 ret =
fetchNode(parent->getChildHash(branch));
382 child = childNode.
get();
416 [
this, hash, cb{std::move(callback)}](
418 auto node = finishFetch(hash, object);
432 template <
class Node>
437 assert(node->cowid() <=
cowid_);
438 if (node->cowid() !=
cowid_)
442 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
457 auto& [init, cmp, incr] = loopParams;
460 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
464 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
468 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
469 for (
int i = init; cmp(i);)
471 if (!inner->isEmptyBranch(i))
474 assert(!stack.
empty());
477 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
481 inner = std::static_pointer_cast<SHAMapInnerNode>(node);
482 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
497 auto cmp = [](
int i) {
return i >= 0; };
498 auto incr = [](
int& i) { --i; };
500 return belowHelper(node, stack, branch, {init, cmp, incr});
510 auto incr = [](
int& i) { ++i; };
512 return belowHelper(node, stack, branch, {init, cmp, incr});
527 if (!inner->isEmptyBranch(i))
548 assert(leaf->peekItem() || (leaf ==
root_.get()));
549 return leaf->peekItem();
555 assert(stack.
empty());
559 while (!stack.
empty())
569 assert(!stack.
empty());
570 assert(stack.
top().
first->isLeaf());
572 while (!stack.
empty())
574 auto [node, nodeID] = stack.
top();
575 assert(!node->isLeaf());
576 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
579 if (!inner->isEmptyBranch(i))
584 Throw<SHAMapMissingNode>(
type_,
id);
585 assert(leaf->isLeaf());
623 while (!stack.
empty())
625 auto [node, nodeID] = stack.
top();
629 if (leaf->peekItem()->key() > id)
631 this, leaf->peekItem().get(), std::move(stack));
635 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
640 if (!inner->isEmptyBranch(branch))
645 Throw<SHAMapMissingNode>(
type_,
id);
647 this, leaf->peekItem().get(), std::move(stack));
660 while (!stack.
empty())
662 auto [node, nodeID] = stack.
top();
666 if (leaf->peekItem()->key() < id)
668 this, leaf->peekItem().get(), std::move(stack));
672 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
673 for (
int branch =
selectBranch(nodeID,
id) - 1; branch >= 0;
676 if (!inner->isEmptyBranch(branch))
679 auto leaf =
lastBelow(node, stack, branch);
681 Throw<SHAMapMissingNode>(
type_,
id);
683 this, leaf->peekItem().get(), std::move(stack));
696 return (
findKey(
id) !=
nullptr);
709 Throw<SHAMapMissingNode>(
type_,
id);
711 auto leaf = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
714 if (!leaf || (leaf->peekItem()->key() !=
id))
723 while (!stack.
empty())
726 std::static_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
731 node->setChild(
selectBranch(nodeID,
id), std::move(prevNode));
737 const int bc = node->getBranchCount();
752 if (!node->isEmptyBranch(i))
754 node->setChild(i,
nullptr);
763 prevNode = std::move(node);
769 prevNode = std::move(node);
790 Throw<SHAMapMissingNode>(
type_, tag);
792 auto [node, nodeID] = stack.
top();
797 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
798 if (leaf->peekItem()->key() == tag)
805 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
807 assert(inner->isEmptyBranch(branch));
814 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
816 assert(otherItem && (tag != otherItem->key()));
818 node = std::make_shared<SHAMapInnerNode>(node->cowid());
825 stack.
push({node, nodeID});
829 nodeID = nodeID.getChildNodeID(b1);
830 node = std::make_shared<SHAMapInnerNode>(
cowid_);
834 assert(node->isInner());
848 return addGiveItem(type, std::make_shared<SHAMapItem const>(std::move(i)));
854 auto hash =
root_->getHash();
858 hash =
root_->getHash();
877 Throw<SHAMapMissingNode>(
type_, tag);
879 auto node = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
883 if (!node || (node->peekItem()->key() != tag))
889 if (node->getType() != type)
891 JLOG(
journal_.
fatal()) <<
"SHAMap::setItem: cross-type change!";
897 if (node->setItem(std::move(item)))
906 if (hash ==
root_->getHash())
913 stream <<
"Fetch root TXN node " << hash;
917 stream <<
"Fetch root STATE node " << hash;
921 stream <<
"Fetch root SHAMap node " << hash;
930 assert(
root_->getHash() == hash);
952 assert(node->cowid() == 0);
958 node->serializeWithPrefix(s);
967 template <
class Node>
973 assert(node->cowid() != 0);
975 if (node->cowid() !=
cowid_)
979 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
1008 if (
root_->isLeaf())
1011 root_->updateHash();
1020 auto node = std::static_pointer_cast<SHAMapInnerNode>(
root_);
1022 if (node->isEmpty())
1024 root_ = std::make_shared<SHAMapInnerNode>(0);
1042 if (node->isEmptyBranch(pos))
1051 auto child = node->getChild(pos++);
1053 if (child && (child->cowid() != 0))
1059 if (child->isInner())
1063 stack.
emplace(std::move(node), branch);
1067 node = std::static_pointer_cast<SHAMapInnerNode>(
1076 assert(node->cowid() ==
cowid_);
1077 child->updateHash();
1083 node->shareChild(branch, child);
1090 node->updateHashDeep();
1096 node = std::static_pointer_cast<SHAMapInnerNode>(
1104 auto parent = std::move(stack.
top().first);
1105 pos = stack.
top().second;
1109 assert(parent->cowid() ==
cowid_);
1110 parent->shareChild(pos, node);
1113 node = std::move(parent);
1118 root_ = std::move(node);
1134 auto [node, nodeID] = stack.
top();
1143 if (node->isInner())
1148 if (!inner->isEmptyBranch(i))
1150 auto child = inner->getChildPointer(i);
1153 assert(child->getHash() == inner->getChildHash(i));
1154 stack.
push({child, nodeID.getChildNodeID(i)});
1161 }
while (!stack.
empty());
1163 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1170 assert(!ret || !ret->cowid());
1180 assert(node->cowid() == 0);
1181 assert(node->getHash() == hash);
1184 ->canonicalize_replace_client(hash.
as_uint256(), node);
1191 auto node =
root_.get();
1192 assert(node !=
nullptr);
1193 assert(!node->isLeaf());
1198 node->invariants(
true);