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_));
448 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
452 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
456 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
459 if (!inner->isEmptyBranch(i))
462 assert(!stack.
empty());
465 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
469 inner = std::static_pointer_cast<SHAMapInnerNode>(node);
470 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
492 if (!inner->isEmptyBranch(i))
513 assert(leaf->peekItem() || (leaf ==
root_.get()));
514 return leaf->peekItem();
520 assert(stack.
empty());
524 while (!stack.
empty())
534 assert(!stack.
empty());
535 assert(stack.
top().
first->isLeaf());
537 while (!stack.
empty())
539 auto [node, nodeID] = stack.
top();
540 assert(!node->isLeaf());
541 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
544 if (!inner->isEmptyBranch(i))
549 Throw<SHAMapMissingNode>(
type_,
id);
550 assert(leaf->isLeaf());
590 while (!stack.
empty())
592 auto [node, nodeID] = stack.
top();
596 if (leaf->peekItem()->key() > id)
598 this, leaf->peekItem().get(), std::move(stack));
602 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
607 if (!inner->isEmptyBranch(branch))
612 Throw<SHAMapMissingNode>(
type_,
id);
614 this, leaf->peekItem().get(), std::move(stack));
626 return (
findKey(
id) !=
nullptr);
639 Throw<SHAMapMissingNode>(
type_,
id);
641 auto leaf = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
644 if (!leaf || (leaf->peekItem()->key() !=
id))
653 while (!stack.
empty())
656 std::static_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
667 const int bc = node->getBranchCount();
682 if (!node->isEmptyBranch(i))
684 node->setChild(i,
nullptr);
693 prevNode = std::move(node);
699 prevNode = std::move(node);
720 Throw<SHAMapMissingNode>(
type_, tag);
722 auto [node, nodeID] = stack.
top();
727 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
728 if (leaf->peekItem()->key() == tag)
735 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
737 assert(inner->isEmptyBranch(branch));
739 inner->setChild(branch, newNode);
745 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
747 assert(otherItem && (tag != otherItem->key()));
749 node = std::make_shared<SHAMapInnerNode>(node->cowid());
756 stack.
push({node, nodeID});
760 nodeID = nodeID.getChildNodeID(b1);
761 node = std::make_shared<SHAMapInnerNode>(
cowid_);
765 assert(node->isInner());
779 return addGiveItem(type, std::make_shared<SHAMapItem const>(std::move(i)));
785 auto hash =
root_->getHash();
789 hash =
root_->getHash();
808 Throw<SHAMapMissingNode>(
type_, tag);
810 auto node = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
814 if (!node || (node->peekItem()->key() != tag))
820 if (node->getType() != type)
822 JLOG(
journal_.
fatal()) <<
"SHAMap::setItem: cross-type change!";
828 if (node->setItem(std::move(item)))
837 if (hash ==
root_->getHash())
844 stream <<
"Fetch root TXN node " << hash;
848 stream <<
"Fetch root STATE node " << hash;
852 stream <<
"Fetch root SHAMap node " << hash;
861 assert(
root_->getHash() == hash);
883 assert(node->cowid() == 0);
889 node->serializeWithPrefix(s);
898 template <
class Node>
904 assert(node->cowid() != 0);
906 if (node->cowid() !=
cowid_)
910 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
951 auto node = std::static_pointer_cast<SHAMapInnerNode>(
root_);
955 root_ = std::make_shared<SHAMapInnerNode>(0);
973 if (node->isEmptyBranch(pos))
982 auto child = node->getChild(pos++);
984 if (child && (child->cowid() != 0))
990 if (child->isInner())
994 stack.
emplace(std::move(node), branch);
998 node = std::static_pointer_cast<SHAMapInnerNode>(
1007 assert(node->cowid() ==
cowid_);
1008 child->updateHash();
1014 node->shareChild(branch, child);
1021 node->updateHashDeep();
1027 node = std::static_pointer_cast<SHAMapInnerNode>(
1035 auto parent = std::move(stack.
top().first);
1036 pos = stack.
top().second;
1040 assert(parent->cowid() ==
cowid_);
1041 parent->shareChild(pos, node);
1044 node = std::move(parent);
1049 root_ = std::move(node);
1065 auto [node, nodeID] = stack.
top();
1074 if (node->isInner())
1079 if (!inner->isEmptyBranch(i))
1081 auto child = inner->getChildPointer(i);
1084 assert(child->getHash() == inner->getChildHash(i));
1085 stack.
push({child, nodeID.getChildNodeID(i)});
1092 }
while (!stack.
empty());
1094 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1101 assert(!ret || !ret->cowid());
1111 assert(node->cowid() == 0);
1112 assert(node->getHash() == hash);
1115 ->canonicalize_replace_client(hash.
as_uint256(), node);
1122 auto node =
root_.get();
1123 assert(node !=
nullptr);
1124 assert(!node->isLeaf());
1129 node->invariants(
true);