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)
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);
721 node->setChild(
selectBranch(nodeID,
id), std::move(prevNode));
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));
804 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
806 assert(otherItem && (tag != otherItem->key()));
808 node = std::make_shared<SHAMapInnerNode>(node->cowid());
815 stack.
push({node, nodeID});
819 nodeID = nodeID.getChildNodeID(b1);
820 node = std::make_shared<SHAMapInnerNode>(
cowid_);
824 assert(node->isInner());
838 return addGiveItem(type, std::make_shared<SHAMapItem const>(std::move(i)));
844 auto hash =
root_->getHash();
848 hash =
root_->getHash();
867 Throw<SHAMapMissingNode>(
type_, tag);
869 auto node = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
873 if (!node || (node->peekItem()->key() != tag))
879 if (node->getType() != type)
881 JLOG(
journal_.
fatal()) <<
"SHAMap::setItem: cross-type change!";
887 if (node->setItem(std::move(item)))
896 if (hash ==
root_->getHash())
903 stream <<
"Fetch root TXN node " << hash;
907 stream <<
"Fetch root STATE node " << hash;
911 stream <<
"Fetch root SHAMap node " << hash;
920 assert(
root_->getHash() == hash);
942 assert(node->cowid() == 0);
948 node->serializeWithPrefix(s);
957 template <
class Node>
963 assert(node->cowid() != 0);
965 if (node->cowid() !=
cowid_)
969 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
1001 root_->updateHash();
1010 auto node = std::static_pointer_cast<SHAMapInnerNode>(
root_);
1012 if (node->isEmpty())
1014 root_ = std::make_shared<SHAMapInnerNode>(0);
1032 if (node->isEmptyBranch(pos))
1041 auto child = node->getChild(pos++);
1043 if (child && (child->cowid() != 0))
1049 if (child->isInner())
1053 stack.
emplace(std::move(node), branch);
1057 node = std::static_pointer_cast<SHAMapInnerNode>(
1066 assert(node->cowid() ==
cowid_);
1067 child->updateHash();
1073 node->shareChild(branch, child);
1080 node->updateHashDeep();
1086 node = std::static_pointer_cast<SHAMapInnerNode>(
1094 auto parent = std::move(stack.
top().first);
1095 pos = stack.
top().second;
1099 assert(parent->cowid() ==
cowid_);
1100 parent->shareChild(pos, node);
1103 node = std::move(parent);
1108 root_ = std::move(node);
1124 auto [node, nodeID] = stack.
top();
1133 if (node->isInner())
1138 if (!inner->isEmptyBranch(i))
1140 auto child = inner->getChildPointer(i);
1143 assert(child->getHash() == inner->getChildHash(i));
1144 stack.
push({child, nodeID.getChildNodeID(i)});
1151 }
while (!stack.
empty());
1153 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1160 assert(!ret || !ret->cowid());
1170 assert(node->cowid() == 0);
1171 assert(node->getHash() == hash);
1174 ->canonicalize_replace_client(hash.
as_uint256(), node);
1181 auto node =
root_.get();
1182 assert(node !=
nullptr);
1183 assert(!node->isLeaf());
1188 node->invariants(
true);