20#include <xrpld/shamap/SHAMap.h>
21#include <xrpld/shamap/SHAMapAccountStateLeafNode.h>
22#include <xrpld/shamap/SHAMapNodeID.h>
23#include <xrpld/shamap/SHAMapSyncFilter.h>
24#include <xrpld/shamap/SHAMapTxLeafNode.h>
25#include <xrpld/shamap/SHAMapTxPlusMetaLeafNode.h>
26#include <xrpl/basics/contract.h>
33 boost::intrusive_ptr<SHAMapItem const> item,
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 " +
71 , journal_(other.f_.journal())
72 , cowid_(other.cowid_ + 1)
73 , ledgerSeq_(other.ledgerSeq_)
77 , backed_(other.backed_)
90 return std::make_shared<SHAMap>(*
this, isMutable);
106 "ripple::SHAMap::dirtyUp : valid state");
108 child && (child->cowid() ==
cowid_),
109 "ripple::SHAMap::dirtyUp : valid child input");
111 while (!stack.
empty())
114 std::dynamic_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
117 XRPL_ASSERT(node,
"ripple::SHAMap::dirtyUp : non-null node");
120 XRPL_ASSERT(branch >= 0,
"ripple::SHAMap::dirtyUp : valid branch");
123 node->setChild(branch, std::move(child));
125 child = std::move(node);
133 stack ==
nullptr || stack->
empty(),
134 "ripple::SHAMap::walkTowardsKey : empty stack input");
138 while (inNode->isInner())
140 if (stack !=
nullptr)
141 stack->
push({inNode, nodeID});
143 auto const inner = std::static_pointer_cast<SHAMapInnerNode>(inNode);
145 if (inner->isEmptyBranch(branch))
152 if (stack !=
nullptr)
153 stack->
push({inNode, nodeID});
161 if (leaf && leaf->
peekItem()->key() !=
id)
169 XRPL_ASSERT(
backed_,
"ripple::SHAMap::fetchNodeFromDB : is backed");
179 XRPL_ASSERT(
backed_,
"ripple::SHAMap::finishFetch : is backed");
206 <<
"finishFetch exception: unknonw exception: " << hash;
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));
368 parent->
isInner(),
"ripple::SHAMap::descend : valid parent input");
371 "ripple::SHAMap::descend : valid branch input");
374 "ripple::SHAMap::descend : parent branch is non-empty");
387 child = childNode.
get();
421 [
this, hash, cb{std::move(callback)}](
423 auto node = finishFetch(hash, object);
444 "ripple::SHAMap::unshareNode : node valid for cowid");
445 if (node->cowid() !=
cowid_)
450 "ripple::SHAMap::unshareNode : not immutable");
451 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
466 auto& [init, cmp, incr] = loopParams;
469 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
473 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
477 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
478 for (
int i = init; cmp(i);)
480 if (!inner->isEmptyBranch(i))
485 "ripple::SHAMap::belowHelper : non-empty stack");
488 auto n = std::static_pointer_cast<SHAMapLeafNode>(node);
492 inner = std::static_pointer_cast<SHAMapInnerNode>(node);
493 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
508 auto cmp = [](
int i) {
return i >= 0; };
509 auto incr = [](
int& i) { --i; };
511 return belowHelper(node, stack, branch, {init, cmp, incr});
521 auto incr = [](
int& i) { ++i; };
523 return belowHelper(node, stack, branch, {init, cmp, incr});
525static const boost::intrusive_ptr<SHAMapItem const>
no_item;
527boost::intrusive_ptr<SHAMapItem const>
const&
538 if (!inner->isEmptyBranch(i))
549 UNREACHABLE(
"ripple::SHAMap::onlyBelow : no next node");
560 leaf->peekItem() || (leaf ==
root_.get()),
561 "ripple::SHAMap::onlyBelow : valid inner node");
562 return leaf->peekItem();
569 stack.
empty(),
"ripple::SHAMap::peekFirstItem : empty stack input");
573 while (!stack.
empty())
584 !stack.
empty(),
"ripple::SHAMap::peekNextItem : non-empty stack input");
587 "ripple::SHAMap::peekNextItem : stack starts with leaf");
589 while (!stack.
empty())
591 auto [node, nodeID] = stack.
top();
594 "ripple::SHAMap::peekNextItem : another node is not leaf");
595 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
598 if (!inner->isEmptyBranch(i))
603 Throw<SHAMapMissingNode>(
type_,
id);
606 "ripple::SHAMap::peekNextItem : leaf is valid");
616boost::intrusive_ptr<SHAMapItem const>
const&
627boost::intrusive_ptr<SHAMapItem const>
const&
644 while (!stack.
empty())
646 auto [node, nodeID] = stack.
top();
650 if (leaf->peekItem()->key() > id)
652 this, leaf->peekItem().get(), std::move(stack));
656 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
661 if (!inner->isEmptyBranch(branch))
666 Throw<SHAMapMissingNode>(
type_,
id);
668 this, leaf->peekItem().get(), std::move(stack));
681 while (!stack.
empty())
683 auto [node, nodeID] = stack.
top();
687 if (leaf->peekItem()->key() < id)
689 this, leaf->peekItem().get(), std::move(stack));
693 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
694 for (
int branch =
selectBranch(nodeID,
id) - 1; branch >= 0;
697 if (!inner->isEmptyBranch(branch))
700 auto leaf =
lastBelow(node, stack, branch);
702 Throw<SHAMapMissingNode>(
type_,
id);
704 this, leaf->peekItem().get(), std::move(stack));
717 return (
findKey(
id) !=
nullptr);
726 "ripple::SHAMap::delItem : not immutable");
732 Throw<SHAMapMissingNode>(
type_,
id);
734 auto leaf = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
737 if (!leaf || (leaf->peekItem()->key() !=
id))
746 while (!stack.
empty())
749 std::static_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
754 node->setChild(
selectBranch(nodeID,
id), std::move(prevNode));
760 const int bc = node->getBranchCount();
775 if (!node->isEmptyBranch(i))
777 node->setChild(i,
nullptr);
786 prevNode = std::move(node);
792 prevNode = std::move(node);
803 boost::intrusive_ptr<SHAMapItem const> item)
807 "ripple::SHAMap::addGiveItem : not immutable");
810 "ripple::SHAMap::addGiveItem : valid type input");
819 Throw<SHAMapMissingNode>(
type_, tag);
821 auto [node, nodeID] = stack.
top();
826 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
827 if (leaf->peekItem()->key() == tag)
834 auto inner = std::static_pointer_cast<SHAMapInnerNode>(node);
837 inner->isEmptyBranch(branch),
838 "ripple::SHAMap::addGiveItem : inner branch is empty");
845 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(node);
846 auto otherItem = leaf->peekItem();
848 otherItem && (tag != otherItem->key()),
849 "ripple::SHAMap::addGiveItem : non-null item");
851 node = std::make_shared<SHAMapInnerNode>(node->cowid());
858 stack.
push({node, nodeID});
862 nodeID = nodeID.getChildNodeID(b1);
863 node = std::make_shared<SHAMapInnerNode>(
cowid_);
868 node->isInner(),
"ripple::SHAMap::addGiveItem : node is inner");
882 boost::intrusive_ptr<SHAMapItem const> item)
890 auto hash =
root_->getHash();
894 hash =
root_->getHash();
902 boost::intrusive_ptr<SHAMapItem const> item)
909 "ripple::SHAMap::updateGiveItem : not immutable");
915 Throw<SHAMapMissingNode>(
type_, tag);
917 auto node = std::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
921 if (!node || (node->peekItem()->key() != tag))
923 UNREACHABLE(
"ripple::SHAMap::updateGiveItem : invalid node");
927 if (node->getType() != type)
929 JLOG(
journal_.
fatal()) <<
"SHAMap::updateGiveItem: cross-type change!";
935 if (node->setItem(item))
944 if (hash ==
root_->getHash())
951 stream <<
"Fetch root TXN node " << hash;
955 stream <<
"Fetch root STATE node " << hash;
959 stream <<
"Fetch root SHAMap node " << hash;
969 root_->getHash() == hash,
970 "ripple::SHAMap::fetchRoot : root hash do match");
993 node->cowid() == 0,
"ripple::SHAMap::writeNode : valid input node");
994 XRPL_ASSERT(
backed_,
"ripple::SHAMap::writeNode : is backed");
999 node->serializeWithPrefix(s);
1008template <
class Node>
1015 node->cowid(),
"ripple::SHAMap::preFlushNode : valid input node");
1017 if (node->cowid() !=
cowid_)
1021 node = std::static_pointer_cast<Node>(node->clone(
cowid_));
1044 !doWrite ||
backed_,
"ripple::SHAMap::walkSubTree : valid input");
1051 if (
root_->isLeaf())
1054 root_->updateHash();
1063 auto node = std::static_pointer_cast<SHAMapInnerNode>(
root_);
1065 if (node->isEmpty())
1067 root_ = std::make_shared<SHAMapInnerNode>(0);
1085 if (node->isEmptyBranch(pos))
1094 auto child = node->getChild(pos++);
1096 if (child && (child->cowid() != 0))
1102 if (child->isInner())
1106 stack.
emplace(std::move(node), branch);
1110 node = std::static_pointer_cast<SHAMapInnerNode>(
1121 "ripple::SHAMap::walkSubTree : node cowid do "
1123 child->updateHash();
1129 node->shareChild(branch, child);
1136 node->updateHashDeep();
1142 node = std::static_pointer_cast<SHAMapInnerNode>(
1150 auto parent = std::move(stack.
top().first);
1151 pos = stack.
top().second;
1156 parent->cowid() ==
cowid_,
1157 "ripple::SHAMap::walkSubTree : parent cowid do match");
1158 parent->shareChild(pos, node);
1161 node = std::move(parent);
1166 root_ = std::move(node);
1182 auto [node, nodeID] = stack.
top();
1191 if (node->isInner())
1196 if (!inner->isEmptyBranch(i))
1198 auto child = inner->getChildPointer(i);
1202 child->getHash() == inner->getChildHash(i),
1203 "ripple::SHAMap::dump : child hash do match");
1204 stack.
push({child, nodeID.getChildNodeID(i)});
1211 }
while (!stack.
empty());
1213 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1221 !ret || !ret->cowid(),
1222 "ripple::SHAMap::cacheLookup : not found or zero cowid");
1231 XRPL_ASSERT(
backed_,
"ripple::SHAMap::canonicalize : is backed");
1233 node->cowid() == 0,
"ripple::SHAMap::canonicalize : valid node input");
1235 node->getHash() == hash,
1236 "ripple::SHAMap::canonicalize : node hash do match");
1245 auto node =
root_.get();
1246 XRPL_ASSERT(node,
"ripple::SHAMap::invariants : non-null root node");
1248 !node->isLeaf(),
"ripple::SHAMap::invariants : root node is not leaf");
1253 node->invariants(
true);
Stream trace() const
Severity stream access functions.
virtual beast::Journal const & journal()=0
virtual std::shared_ptr< TreeNodeCache > getTreeNodeCache()=0
Return a pointer to the Family Tree Node Cache.
virtual void missingNodeAcquireBySeq(std::uint32_t refNum, uint256 const &nodeHash)=0
Acquire ledger that has a missing node by ledger sequence.
virtual NodeStore::Database & db()=0
virtual void asyncFetch(uint256 const &hash, std::uint32_t ledgerSeq, std::function< void(std::shared_ptr< NodeObject > const &)> &&callback)
Fetch an object without waiting.
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous, bool duplicate=false)
Fetch a node object.
virtual void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t ledgerSeq)=0
Store the object.
uint256 const & as_uint256() const
bool isInner() const override
Determines if this is an inner node.
bool isEmptyBranch(int m) const
void setChild(int m, std::shared_ptr< SHAMapTreeNode > child)
SHAMapHash const & getChildHash(int m) const
std::shared_ptr< SHAMapTreeNode > canonicalizeChild(int branch, std::shared_ptr< SHAMapTreeNode > node)
SHAMapTreeNode * getChildPointer(int branch)
boost::intrusive_ptr< SHAMapItem const > const & peekItem() const
Identifies a node inside a SHAMap.
SHAMapNodeID getChildNodeID(unsigned int m) const
virtual void gotNode(bool fromFilter, SHAMapHash const &nodeHash, std::uint32_t ledgerSeq, Blob &&nodeData, SHAMapNodeType type) const =0
virtual std::optional< Blob > getNode(SHAMapHash const &nodeHash) const =0
virtual bool isLeaf() const =0
Determines if this is a leaf node.
static std::shared_ptr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
SHAMapHash const & getHash() const
Return the hash of this node.
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
std::shared_ptr< SHAMapTreeNode > fetchNodeNT(SHAMapHash const &hash) const
SHAMapTreeNode * descendAsync(SHAMapInnerNode *parent, int branch, SHAMapSyncFilter *filter, bool &pending, descendCallback &&) const
bool hasItem(uint256 const &id) const
Does the tree have an item with the given ID?
std::shared_ptr< SHAMapTreeNode > checkFilter(SHAMapHash const &hash, SHAMapSyncFilter *filter) const
static constexpr unsigned int leafDepth
The depth of the hash map: data is only present in the leaves.
void dump(bool withHashes=false) const
SHAMapLeafNode * firstBelow(std::shared_ptr< SHAMapTreeNode >, SharedPtrNodeStack &stack, int branch=0) const
boost::intrusive_ptr< SHAMapItem const > const & onlyBelow(SHAMapTreeNode *) const
If there is only one leaf below this node, get its contents.
SHAMapTreeNode * descendThrow(SHAMapInnerNode *, int branch) const
void dirtyUp(SharedPtrNodeStack &stack, uint256 const &target, std::shared_ptr< SHAMapTreeNode > terminal)
Update hashes up to the root.
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
SHAMapLeafNode * belowHelper(std::shared_ptr< SHAMapTreeNode > node, SharedPtrNodeStack &stack, int branch, std::tuple< int, std::function< bool(int)>, std::function< void(int &)> > const &loopParams) const
SHAMapLeafNode * lastBelow(std::shared_ptr< SHAMapTreeNode > node, SharedPtrNodeStack &stack, int branch=branchFactor) const
std::shared_ptr< SHAMapTreeNode > writeNode(NodeObjectType t, std::shared_ptr< SHAMapTreeNode > node) const
write and canonicalize modified node
bool addGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
SHAMapLeafNode * walkTowardsKey(uint256 const &id, SharedPtrNodeStack *stack=nullptr) const
Walk towards the specified id, returning the node.
SHAMapTreeNode * descend(SHAMapInnerNode *, int branch) const
SHAMapLeafNode const * peekNextItem(uint256 const &id, SharedPtrNodeStack &stack) const
std::shared_ptr< Node > preFlushNode(std::shared_ptr< Node > node) const
prepare a node to be modified before flushing
std::shared_ptr< SHAMapTreeNode > root_
int walkSubTree(bool doWrite, NodeObjectType t)
std::shared_ptr< SHAMapTreeNode > fetchNode(SHAMapHash const &hash) const
const_iterator end() const
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
const_iterator upper_bound(uint256 const &id) const
Find the first item after the given item.
std::uint32_t cowid_
ID to distinguish this map for all others we're sharing nodes with.
SHAMapHash getHash() const
bool updateGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
std::shared_ptr< SHAMapTreeNode > descendNoStore(std::shared_ptr< SHAMapInnerNode > const &, int branch) const
SHAMapLeafNode const * peekFirstItem(SharedPtrNodeStack &stack) const
std::shared_ptr< SHAMapTreeNode > cacheLookup(SHAMapHash const &hash) const
std::shared_ptr< SHAMapTreeNode > fetchNodeFromDB(SHAMapHash const &hash) const
std::uint32_t ledgerSeq_
The sequence of the ledger that this map references, if any.
void canonicalize(SHAMapHash const &hash, std::shared_ptr< SHAMapTreeNode > &) const
bool delItem(uint256 const &id)
std::shared_ptr< Node > unshareNode(std::shared_ptr< Node >, SHAMapNodeID const &nodeID)
Unshare the node, allowing it to be modified.
bool fetchRoot(SHAMapHash const &hash, SHAMapSyncFilter *filter)
const_iterator lower_bound(uint256 const &id) const
Find the object with the greatest object id smaller than the input id.
std::shared_ptr< SHAMap > snapShot(bool isMutable) const
int flushDirty(NodeObjectType t)
Flush modified nodes to the nodestore and convert them to shared.
int unshare()
Convert any modified nodes to shared.
SHAMapLeafNode * findKey(uint256 const &id) const
Return nullptr if key not found.
static constexpr unsigned int branchFactor
Number of children each non-leaf node has (the 'radix tree' part of the map)
std::shared_ptr< SHAMapTreeNode > finishFetch(SHAMapHash const &hash, std::shared_ptr< NodeObject > const &object) const
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
unsigned int selectBranch(SHAMapNodeID const &id, uint256 const &hash)
Returns the branch that would contain the given hash.
static const boost::intrusive_ptr< SHAMapItem const > no_item
SHAMapState
Describes the current state of a given SHAMap.
@ Immutable
The map is set in stone and cannot be changed.
@ Synching
The map's hash is fixed but valid nodes may be missing and can be added.
@ Modifying
The map is in flux and objects can be added and removed.
NodeObjectType
The types of node objects.
@ pending
List will be valid in the future.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
std::shared_ptr< SHAMapLeafNode > makeTypedLeaf(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item, std::uint32_t owner)
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.