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());
615 while (!stack.
empty())
617 auto [node, nodeID] = stack.
top();
621 if (leaf->peekItem()->key() > id)
623 this, leaf->peekItem().get(), std::move(stack));
627 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
632 if (!inner->isEmptyBranch(branch))
637 Throw<SHAMapMissingNode>(
type_,
id);
639 this, leaf->peekItem().get(), std::move(stack));
652 while (!stack.
empty())
654 auto [node, nodeID] = stack.
top();
658 if (leaf->peekItem()->key() < id)
660 this, leaf->peekItem().get(), std::move(stack));
664 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
665 for (
int branch =
selectBranch(nodeID,
id) - 1; branch >= 0;
668 if (!inner->isEmptyBranch(branch))
671 auto leaf =
lastBelow(node, stack, branch);
673 Throw<SHAMapMissingNode>(
type_,
id);
675 this, leaf->peekItem().get(), std::move(stack));
688 return (
findKey(
id) !=
nullptr);
701 Throw<SHAMapMissingNode>(
type_,
id);
703 auto leaf = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
706 if (!leaf || (leaf->peekItem()->key() !=
id))
715 while (!stack.
empty())
718 std::static_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
729 const int bc = node->getBranchCount();
744 if (!node->isEmptyBranch(i))
746 node->setChild(i,
nullptr);
755 prevNode = std::move(node);
761 prevNode = std::move(node);
782 Throw<SHAMapMissingNode>(
type_, tag);
784 auto [node, nodeID] = stack.
top();
789 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
790 if (leaf->peekItem()->key() == tag)
797 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
799 assert(inner->isEmptyBranch(branch));
801 inner->setChild(branch, newNode);
807 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
809 assert(otherItem && (tag != otherItem->key()));
811 node = std::make_shared<SHAMapInnerNode>(node->cowid());
818 stack.
push({node, nodeID});
822 nodeID = nodeID.getChildNodeID(b1);
823 node = std::make_shared<SHAMapInnerNode>(
cowid_);
827 assert(node->isInner());
841 return addGiveItem(type, std::make_shared<SHAMapItem const>(std::move(i)));
847 auto hash =
root_->getHash();
851 hash =
root_->getHash();
870 Throw<SHAMapMissingNode>(
type_, tag);
872 auto node = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
876 if (!node || (node->peekItem()->key() != tag))
882 if (node->getType() != type)
884 JLOG(
journal_.
fatal()) <<
"SHAMap::setItem: cross-type change!";
890 if (node->setItem(std::move(item)))
899 if (hash ==
root_->getHash())
906 stream <<
"Fetch root TXN node " << hash;
910 stream <<
"Fetch root STATE node " << hash;
914 stream <<
"Fetch root SHAMap node " << hash;
923 assert(
root_->getHash() == hash);
945 assert(node->cowid() == 0);
951 node->serializeWithPrefix(s);
960 template <
class Node>
966 assert(node->cowid() != 0);
968 if (node->cowid() !=
cowid_)
972 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
1001 if (
root_->isLeaf())
1004 root_->updateHash();
1013 auto node = std::static_pointer_cast<SHAMapInnerNode>(
root_);
1015 if (node->isEmpty())
1017 root_ = std::make_shared<SHAMapInnerNode>(0);
1035 if (node->isEmptyBranch(pos))
1044 auto child = node->getChild(pos++);
1046 if (child && (child->cowid() != 0))
1052 if (child->isInner())
1056 stack.
emplace(std::move(node), branch);
1060 node = std::static_pointer_cast<SHAMapInnerNode>(
1069 assert(node->cowid() ==
cowid_);
1070 child->updateHash();
1076 node->shareChild(branch, child);
1083 node->updateHashDeep();
1089 node = std::static_pointer_cast<SHAMapInnerNode>(
1097 auto parent = std::move(stack.
top().first);
1098 pos = stack.
top().second;
1102 assert(parent->cowid() ==
cowid_);
1103 parent->shareChild(pos, node);
1106 node = std::move(parent);
1111 root_ = std::move(node);
1127 auto [node, nodeID] = stack.
top();
1136 if (node->isInner())
1141 if (!inner->isEmptyBranch(i))
1143 auto child = inner->getChildPointer(i);
1146 assert(child->getHash() == inner->getChildHash(i));
1147 stack.
push({child, nodeID.getChildNodeID(i)});
1154 }
while (!stack.
empty());
1156 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1163 assert(!ret || !ret->cowid());
1173 assert(node->cowid() == 0);
1174 assert(node->getHash() == hash);
1177 ->canonicalize_replace_client(hash.
as_uint256(), node);
1184 auto node =
root_.get();
1185 assert(node !=
nullptr);
1186 assert(!node->isLeaf());
1191 node->invariants(
true);