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>
27#include <xrpl/basics/TaggedCache.ipp>
28#include <xrpl/basics/contract.h>
32[[nodiscard]] intr_ptr::SharedPtr<SHAMapLeafNode>
35 boost::intrusive_ptr<SHAMapItem const> item,
39 return intr_ptr::make_shared<SHAMapTxLeafNode>(std::move(item), owner);
42 return intr_ptr::make_shared<SHAMapTxPlusMetaLeafNode>(
43 std::move(item), owner);
46 return intr_ptr::make_shared<SHAMapAccountStateLeafNode>(
47 std::move(item), owner);
50 "Attempt to create leaf node of unknown type " +
58 root_ = intr_ptr::make_shared<SHAMapInnerNode>(
cowid_);
68 root_ = intr_ptr::make_shared<SHAMapInnerNode>(
cowid_);
73 , journal_(other.f_.journal())
74 , cowid_(other.cowid_ + 1)
75 , ledgerSeq_(other.ledgerSeq_)
79 , backed_(other.backed_)
92 return std::make_shared<SHAMap>(*
this, isMutable);
108 "ripple::SHAMap::dirtyUp : valid state");
110 child && (child->cowid() ==
cowid_),
111 "ripple::SHAMap::dirtyUp : valid child input");
113 while (!stack.
empty())
116 intr_ptr::dynamic_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
119 XRPL_ASSERT(node,
"ripple::SHAMap::dirtyUp : non-null node");
122 XRPL_ASSERT(branch >= 0,
"ripple::SHAMap::dirtyUp : valid branch");
125 node->setChild(branch, std::move(child));
127 child = std::move(node);
135 stack ==
nullptr || stack->
empty(),
136 "ripple::SHAMap::walkTowardsKey : empty stack input");
140 while (inNode->isInner())
142 if (stack !=
nullptr)
143 stack->
push({inNode, nodeID});
146 intr_ptr::static_pointer_cast<SHAMapInnerNode>(inNode);
148 if (inner->isEmptyBranch(branch))
155 if (stack !=
nullptr)
156 stack->
push({inNode, nodeID});
164 if (leaf && leaf->
peekItem()->key() !=
id)
172 XRPL_ASSERT(
backed_,
"ripple::SHAMap::fetchNodeFromDB : is backed");
182 XRPL_ASSERT(
backed_,
"ripple::SHAMap::finishFetch : is backed");
209 <<
"finishFetch exception: unknonw exception: " << hash;
219 if (
auto nodeData = filter->
getNode(hash))
231 std::move(*nodeData),
241 <<
"Invalid node/data, hash=" << hash <<
": " << x.
what();
290 Throw<SHAMapMissingNode>(
type_, hash);
367 parent->
isInner(),
"ripple::SHAMap::descend : valid parent input");
370 "ripple::SHAMap::descend : valid branch input");
373 "ripple::SHAMap::descend : parent branch is non-empty");
386 child = childNode.
get();
420 [
this, hash, cb{std::move(callback)}](
422 auto node = finishFetch(hash, object);
443 "ripple::SHAMap::unshareNode : node valid for cowid");
444 if (node->cowid() !=
cowid_)
449 "ripple::SHAMap::unshareNode : not immutable");
450 node = intr_ptr::static_pointer_cast<Node>(node->clone(
cowid_));
465 auto& [init, cmp, incr] = loopParams;
468 auto n = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
472 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
476 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
477 for (
int i = init; cmp(i);)
479 if (!inner->isEmptyBranch(i))
484 "ripple::SHAMap::belowHelper : non-empty stack");
487 auto n = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
491 inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
492 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
507 auto cmp = [](
int i) {
return i >= 0; };
508 auto incr = [](
int& i) { --i; };
510 return belowHelper(node, stack, branch, {init, cmp, incr});
520 auto incr = [](
int& i) { ++i; };
522 return belowHelper(node, stack, branch, {init, cmp, incr});
524static const boost::intrusive_ptr<SHAMapItem const>
no_item;
526boost::intrusive_ptr<SHAMapItem const>
const&
537 if (!inner->isEmptyBranch(i))
548 UNREACHABLE(
"ripple::SHAMap::onlyBelow : no next node");
559 leaf->peekItem() || (leaf ==
root_.get()),
560 "ripple::SHAMap::onlyBelow : valid inner node");
561 return leaf->peekItem();
568 stack.
empty(),
"ripple::SHAMap::peekFirstItem : empty stack input");
572 while (!stack.
empty())
583 !stack.
empty(),
"ripple::SHAMap::peekNextItem : non-empty stack input");
586 "ripple::SHAMap::peekNextItem : stack starts with leaf");
588 while (!stack.
empty())
590 auto [node, nodeID] = stack.
top();
593 "ripple::SHAMap::peekNextItem : another node is not leaf");
594 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
597 if (!inner->isEmptyBranch(i))
602 Throw<SHAMapMissingNode>(
type_,
id);
605 "ripple::SHAMap::peekNextItem : leaf is valid");
615boost::intrusive_ptr<SHAMapItem const>
const&
626boost::intrusive_ptr<SHAMapItem const>
const&
643 while (!stack.
empty())
645 auto [node, nodeID] = stack.
top();
649 if (leaf->peekItem()->key() > id)
651 this, leaf->peekItem().get(), std::move(stack));
655 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
660 if (!inner->isEmptyBranch(branch))
665 Throw<SHAMapMissingNode>(
type_,
id);
667 this, leaf->peekItem().get(), std::move(stack));
680 while (!stack.
empty())
682 auto [node, nodeID] = stack.
top();
686 if (leaf->peekItem()->key() < id)
688 this, leaf->peekItem().get(), std::move(stack));
692 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
693 for (
int branch =
selectBranch(nodeID,
id) - 1; branch >= 0;
696 if (!inner->isEmptyBranch(branch))
699 auto leaf =
lastBelow(node, stack, branch);
701 Throw<SHAMapMissingNode>(
type_,
id);
703 this, leaf->peekItem().get(), std::move(stack));
716 return (
findKey(
id) !=
nullptr);
725 "ripple::SHAMap::delItem : not immutable");
731 Throw<SHAMapMissingNode>(
type_,
id);
734 intr_ptr::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
737 if (!leaf || (leaf->peekItem()->key() !=
id))
746 while (!stack.
empty())
749 intr_ptr::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))
787 prevNode = std::move(node);
793 prevNode = std::move(node);
804 boost::intrusive_ptr<SHAMapItem const> item)
808 "ripple::SHAMap::addGiveItem : not immutable");
811 "ripple::SHAMap::addGiveItem : valid type input");
820 Throw<SHAMapMissingNode>(
type_, tag);
822 auto [node, nodeID] = stack.
top();
827 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
828 if (leaf->peekItem()->key() == tag)
835 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
838 inner->isEmptyBranch(branch),
839 "ripple::SHAMap::addGiveItem : inner branch is empty");
846 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
847 auto otherItem = leaf->peekItem();
849 otherItem && (tag != otherItem->key()),
850 "ripple::SHAMap::addGiveItem : non-null item");
852 node = intr_ptr::make_shared<SHAMapInnerNode>(node->cowid());
859 stack.
push({node, nodeID});
863 nodeID = nodeID.getChildNodeID(b1);
864 node = intr_ptr::make_shared<SHAMapInnerNode>(
cowid_);
869 node->isInner(),
"ripple::SHAMap::addGiveItem : node is inner");
883 boost::intrusive_ptr<SHAMapItem const> item)
891 auto hash =
root_->getHash();
895 hash =
root_->getHash();
903 boost::intrusive_ptr<SHAMapItem const> item)
910 "ripple::SHAMap::updateGiveItem : not immutable");
916 Throw<SHAMapMissingNode>(
type_, tag);
919 intr_ptr::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
923 if (!node || (node->peekItem()->key() != tag))
925 UNREACHABLE(
"ripple::SHAMap::updateGiveItem : invalid node");
929 if (node->getType() != type)
931 JLOG(
journal_.
fatal()) <<
"SHAMap::updateGiveItem: cross-type change!";
937 if (node->setItem(item))
946 if (hash ==
root_->getHash())
953 stream <<
"Fetch root TXN node " << hash;
957 stream <<
"Fetch root STATE node " << hash;
961 stream <<
"Fetch root SHAMap node " << hash;
971 root_->getHash() == hash,
972 "ripple::SHAMap::fetchRoot : root hash do match");
996 node->cowid() == 0,
"ripple::SHAMap::writeNode : valid input node");
997 XRPL_ASSERT(
backed_,
"ripple::SHAMap::writeNode : is backed");
1002 node->serializeWithPrefix(s);
1011template <
class Node>
1018 node->cowid(),
"ripple::SHAMap::preFlushNode : valid input node");
1020 if (node->cowid() !=
cowid_)
1024 node = intr_ptr::static_pointer_cast<Node>(node->clone(
cowid_));
1047 !doWrite ||
backed_,
"ripple::SHAMap::walkSubTree : valid input");
1054 if (
root_->isLeaf())
1057 root_->updateHash();
1066 auto node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
root_);
1068 if (node->isEmpty())
1070 root_ = intr_ptr::make_shared<SHAMapInnerNode>(0);
1088 if (node->isEmptyBranch(pos))
1097 auto child = node->getChild(pos++);
1099 if (child && (child->cowid() != 0))
1105 if (child->isInner())
1109 stack.
emplace(std::move(node), branch);
1113 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
1124 "ripple::SHAMap::walkSubTree : node cowid do "
1126 child->updateHash();
1132 node->shareChild(branch, child);
1139 node->updateHashDeep();
1145 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
1153 auto parent = std::move(stack.
top().first);
1154 pos = stack.
top().second;
1159 parent->cowid() ==
cowid_,
1160 "ripple::SHAMap::walkSubTree : parent cowid do match");
1161 parent->shareChild(pos, node);
1164 node = std::move(parent);
1169 root_ = std::move(node);
1185 auto [node, nodeID] = stack.
top();
1194 if (node->isInner())
1199 if (!inner->isEmptyBranch(i))
1201 auto child = inner->getChildPointer(i);
1205 child->getHash() == inner->getChildHash(i),
1206 "ripple::SHAMap::dump : child hash do match");
1207 stack.
push({child, nodeID.getChildNodeID(i)});
1214 }
while (!stack.
empty());
1216 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1224 !ret || !ret->cowid(),
1225 "ripple::SHAMap::cacheLookup : not found or zero cowid");
1234 XRPL_ASSERT(
backed_,
"ripple::SHAMap::canonicalize : is backed");
1236 node->cowid() == 0,
"ripple::SHAMap::canonicalize : valid node input");
1238 node->getHash() == hash,
1239 "ripple::SHAMap::canonicalize : node hash do match");
1248 auto node =
root_.get();
1249 XRPL_ASSERT(node,
"ripple::SHAMap::invariants : non-null root node");
1251 !node->isLeaf(),
"ripple::SHAMap::invariants : root node is not leaf");
1256 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.
void setChild(int m, intr_ptr::SharedPtr< SHAMapTreeNode > child)
bool isEmptyBranch(int m) const
intr_ptr::SharedPtr< SHAMapTreeNode > getChild(int branch)
SHAMapHash const & getChildHash(int m) const
intr_ptr::SharedPtr< SHAMapTreeNode > canonicalizeChild(int branch, intr_ptr::SharedPtr< 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 intr_ptr::SharedPtr< 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.
intr_ptr::SharedPtr< Node > preFlushNode(intr_ptr::SharedPtr< Node > node) const
prepare a node to be modified before flushing
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?
intr_ptr::SharedPtr< SHAMapTreeNode > cacheLookup(SHAMapHash const &hash) const
intr_ptr::SharedPtr< Node > unshareNode(intr_ptr::SharedPtr< Node >, SHAMapNodeID const &nodeID)
Unshare the node, allowing it to be modified.
static constexpr unsigned int leafDepth
The depth of the hash map: data is only present in the leaves.
void dump(bool withHashes=false) 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
intr_ptr::SharedPtr< SHAMapTreeNode > root_
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
intr_ptr::SharedPtr< SHAMapTreeNode > finishFetch(SHAMapHash const &hash, std::shared_ptr< NodeObject > const &object) const
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
void canonicalize(SHAMapHash const &hash, intr_ptr::SharedPtr< SHAMapTreeNode > &) const
int walkSubTree(bool doWrite, NodeObjectType t)
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.
intr_ptr::SharedPtr< SHAMapTreeNode > writeNode(NodeObjectType t, intr_ptr::SharedPtr< SHAMapTreeNode > node) const
write and canonicalize modified node
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNodeNT(SHAMapHash const &hash) const
std::uint32_t cowid_
ID to distinguish this map for all others we're sharing nodes with.
SHAMapHash getHash() const
void dirtyUp(SharedPtrNodeStack &stack, uint256 const &target, intr_ptr::SharedPtr< SHAMapTreeNode > terminal)
Update hashes up to the root.
bool updateGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
SHAMapLeafNode const * peekFirstItem(SharedPtrNodeStack &stack) const
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNode(SHAMapHash const &hash) const
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNodeFromDB(SHAMapHash const &hash) const
intr_ptr::SharedPtr< SHAMapTreeNode > descendNoStore(SHAMapInnerNode &, int branch) const
std::uint32_t ledgerSeq_
The sequence of the ledger that this map references, if any.
bool delItem(uint256 const &id)
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.
SHAMapLeafNode * lastBelow(intr_ptr::SharedPtr< SHAMapTreeNode > node, SharedPtrNodeStack &stack, int branch=branchFactor) const
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.
intr_ptr::SharedPtr< SHAMapTreeNode > checkFilter(SHAMapHash const &hash, SHAMapSyncFilter *filter) const
SHAMapLeafNode * belowHelper(intr_ptr::SharedPtr< SHAMapTreeNode > node, SharedPtrNodeStack &stack, int branch, std::tuple< int, std::function< bool(int)>, std::function< void(int &)> > const &loopParams) const
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)
SHAMapLeafNode * firstBelow(intr_ptr::SharedPtr< SHAMapTreeNode >, SharedPtrNodeStack &stack, int branch=0) const
A shared intrusive pointer class that supports weak pointers.
void adopt(T *p)
Adopt the raw pointer.
T * get() const
Get the raw pointer.
void reset()
Set the pointer to null, decrement the strong count, and run the appropriate release action.
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.
intr_ptr::SharedPtr< SHAMapLeafNode > makeTypedLeaf(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item, std::uint32_t owner)
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)
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.