1#include <xrpl/basics/TaggedCache.ipp>
2#include <xrpl/basics/contract.h>
3#include <xrpl/shamap/SHAMap.h>
4#include <xrpl/shamap/SHAMapAccountStateLeafNode.h>
5#include <xrpl/shamap/SHAMapNodeID.h>
6#include <xrpl/shamap/SHAMapSyncFilter.h>
7#include <xrpl/shamap/SHAMapTxLeafNode.h>
8#include <xrpl/shamap/SHAMapTxPlusMetaLeafNode.h>
12[[nodiscard]] intr_ptr::SharedPtr<SHAMapLeafNode>
15 boost::intrusive_ptr<SHAMapItem const> item,
19 return intr_ptr::make_shared<SHAMapTxLeafNode>(std::move(item), owner);
22 return intr_ptr::make_shared<SHAMapTxPlusMetaLeafNode>(
23 std::move(item), owner);
26 return intr_ptr::make_shared<SHAMapAccountStateLeafNode>(
27 std::move(item), owner);
30 "Attempt to create leaf node of unknown type " +
38 root_ = intr_ptr::make_shared<SHAMapInnerNode>(
cowid_);
48 root_ = intr_ptr::make_shared<SHAMapInnerNode>(
cowid_);
53 , journal_(other.f_.journal())
54 , cowid_(other.cowid_ + 1)
55 , ledgerSeq_(other.ledgerSeq_)
59 , backed_(other.backed_)
88 "xrpl::SHAMap::dirtyUp : valid state");
90 child && (child->cowid() ==
cowid_),
91 "xrpl::SHAMap::dirtyUp : valid child input");
93 while (!stack.
empty())
96 intr_ptr::dynamic_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
99 XRPL_ASSERT(node,
"xrpl::SHAMap::dirtyUp : non-null node");
102 XRPL_ASSERT(branch >= 0,
"xrpl::SHAMap::dirtyUp : valid branch");
105 node->setChild(branch, std::move(child));
107 child = std::move(node);
115 stack ==
nullptr || stack->
empty(),
116 "xrpl::SHAMap::walkTowardsKey : empty stack input");
120 while (inNode->isInner())
122 if (stack !=
nullptr)
123 stack->
push({inNode, nodeID});
126 intr_ptr::static_pointer_cast<SHAMapInnerNode>(inNode);
128 if (inner->isEmptyBranch(branch))
135 if (stack !=
nullptr)
136 stack->
push({inNode, nodeID});
144 if (leaf && leaf->
peekItem()->key() !=
id)
152 XRPL_ASSERT(
backed_,
"xrpl::SHAMap::fetchNodeFromDB : is backed");
162 XRPL_ASSERT(
backed_,
"xrpl::SHAMap::finishFetch : is backed");
189 <<
"finishFetch exception: unknown exception: " << hash;
199 if (
auto nodeData = filter->
getNode(hash))
211 std::move(*nodeData),
221 <<
"Invalid node/data, hash=" << hash <<
": " << x.
what();
270 Throw<SHAMapMissingNode>(
type_, hash);
347 parent->
isInner(),
"xrpl::SHAMap::descend : valid parent input");
350 "xrpl::SHAMap::descend : valid branch input");
353 "xrpl::SHAMap::descend : parent branch is non-empty");
366 child = childNode.
get();
400 [
this, hash, cb{std::move(callback)}](
402 auto node = finishFetch(hash, object);
423 "xrpl::SHAMap::unshareNode : node valid for cowid");
424 if (node->cowid() !=
cowid_)
429 "xrpl::SHAMap::unshareNode : not immutable");
430 node = intr_ptr::static_pointer_cast<Node>(node->clone(
cowid_));
445 auto& [init, cmp, incr] = loopParams;
448 auto n = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
452 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
456 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
457 for (
int i = init; cmp(i);)
459 if (!inner->isEmptyBranch(i))
463 !stack.
empty(),
"xrpl::SHAMap::belowHelper : non-empty stack");
466 auto n = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
470 inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
471 stack.
push({inner, stack.
top().second.getChildNodeID(branch)});
486 auto cmp = [](
int i) {
return i >= 0; };
487 auto incr = [](
int& i) { --i; };
489 return belowHelper(node, stack, branch, {init, cmp, incr});
499 auto incr = [](
int& i) { ++i; };
501 return belowHelper(node, stack, branch, {init, cmp, incr});
503static boost::intrusive_ptr<SHAMapItem const>
const no_item;
505boost::intrusive_ptr<SHAMapItem const>
const&
516 if (!inner->isEmptyBranch(i))
528 UNREACHABLE(
"xrpl::SHAMap::onlyBelow : no next node");
540 leaf->peekItem() || (leaf ==
root_.get()),
541 "xrpl::SHAMap::onlyBelow : valid inner node");
542 return leaf->peekItem();
549 stack.
empty(),
"xrpl::SHAMap::peekFirstItem : empty stack input");
553 while (!stack.
empty())
564 !stack.
empty(),
"xrpl::SHAMap::peekNextItem : non-empty stack input");
567 "xrpl::SHAMap::peekNextItem : stack starts with leaf");
569 while (!stack.
empty())
571 auto [node, nodeID] = stack.
top();
574 "xrpl::SHAMap::peekNextItem : another node is not leaf");
575 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
578 if (!inner->isEmptyBranch(i))
583 Throw<SHAMapMissingNode>(
type_,
id);
586 "xrpl::SHAMap::peekNextItem : leaf is valid");
596boost::intrusive_ptr<SHAMapItem const>
const&
607boost::intrusive_ptr<SHAMapItem const>
const&
624 while (!stack.
empty())
626 auto [node, nodeID] = stack.
top();
630 if (leaf->peekItem()->key() > id)
632 this, leaf->peekItem().get(), std::move(stack));
636 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
641 if (!inner->isEmptyBranch(branch))
646 Throw<SHAMapMissingNode>(
type_,
id);
648 this, leaf->peekItem().get(), std::move(stack));
661 while (!stack.
empty())
663 auto [node, nodeID] = stack.
top();
667 if (leaf->peekItem()->key() < id)
669 this, leaf->peekItem().get(), std::move(stack));
673 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
674 for (
int branch =
selectBranch(nodeID,
id) - 1; branch >= 0;
677 if (!inner->isEmptyBranch(branch))
680 auto leaf =
lastBelow(node, stack, branch);
682 Throw<SHAMapMissingNode>(
type_,
id);
684 this, leaf->peekItem().get(), std::move(stack));
697 return (
findKey(
id) !=
nullptr);
706 "xrpl::SHAMap::delItem : not immutable");
712 Throw<SHAMapMissingNode>(
type_,
id);
715 intr_ptr::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
718 if (!leaf || (leaf->peekItem()->key() !=
id))
727 while (!stack.
empty())
730 intr_ptr::static_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
735 node->setChild(
selectBranch(nodeID,
id), std::move(prevNode));
741 int const bc = node->getBranchCount();
756 if (!node->isEmptyBranch(i))
768 prevNode = std::move(node);
774 prevNode = std::move(node);
785 boost::intrusive_ptr<SHAMapItem const> item)
789 "xrpl::SHAMap::addGiveItem : not immutable");
792 "xrpl::SHAMap::addGiveItem : valid type input");
801 Throw<SHAMapMissingNode>(
type_, tag);
803 auto [node, nodeID] = stack.
top();
808 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
809 if (leaf->peekItem()->key() == tag)
816 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
819 inner->isEmptyBranch(branch),
820 "xrpl::SHAMap::addGiveItem : inner branch is empty");
827 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
828 auto otherItem = leaf->peekItem();
830 otherItem && (tag != otherItem->key()),
831 "xrpl::SHAMap::addGiveItem : non-null item");
833 node = intr_ptr::make_shared<SHAMapInnerNode>(node->cowid());
840 stack.
push({node, nodeID});
844 nodeID = nodeID.getChildNodeID(b1);
845 node = intr_ptr::make_shared<SHAMapInnerNode>(
cowid_);
850 node->isInner(),
"xrpl::SHAMap::addGiveItem : node is inner");
864 boost::intrusive_ptr<SHAMapItem const> item)
872 auto hash =
root_->getHash();
876 hash =
root_->getHash();
884 boost::intrusive_ptr<SHAMapItem const> item)
891 "xrpl::SHAMap::updateGiveItem : not immutable");
897 Throw<SHAMapMissingNode>(
type_, tag);
900 intr_ptr::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
904 if (!node || (node->peekItem()->key() != tag))
907 UNREACHABLE(
"xrpl::SHAMap::updateGiveItem : invalid node");
912 if (node->getType() != type)
914 JLOG(
journal_.
fatal()) <<
"SHAMap::updateGiveItem: cross-type change!";
920 if (node->setItem(item))
929 if (hash ==
root_->getHash())
936 stream <<
"Fetch root TXN node " << hash;
940 stream <<
"Fetch root STATE node " << hash;
944 stream <<
"Fetch root SHAMap node " << hash;
954 root_->getHash() == hash,
955 "xrpl::SHAMap::fetchRoot : root hash do match");
979 node->cowid() == 0,
"xrpl::SHAMap::writeNode : valid input node");
980 XRPL_ASSERT(
backed_,
"xrpl::SHAMap::writeNode : is backed");
985 node->serializeWithPrefix(s);
1000 XRPL_ASSERT(node->cowid(),
"xrpl::SHAMap::preFlushNode : valid input node");
1002 if (node->cowid() !=
cowid_)
1006 node = intr_ptr::static_pointer_cast<Node>(node->clone(
cowid_));
1028 XRPL_ASSERT(!doWrite ||
backed_,
"xrpl::SHAMap::walkSubTree : valid input");
1035 if (
root_->isLeaf())
1038 root_->updateHash();
1047 auto node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
root_);
1049 if (node->isEmpty())
1051 root_ = intr_ptr::make_shared<SHAMapInnerNode>(0);
1069 if (node->isEmptyBranch(pos))
1078 auto child = node->getChild(pos++);
1080 if (child && (child->cowid() != 0))
1086 if (child->isInner())
1090 stack.
emplace(std::move(node), branch);
1094 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
1105 "xrpl::SHAMap::walkSubTree : node cowid do "
1107 child->updateHash();
1113 node->shareChild(branch, child);
1120 node->updateHashDeep();
1126 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
1134 auto parent = std::move(stack.
top().first);
1135 pos = stack.
top().second;
1140 parent->cowid() ==
cowid_,
1141 "xrpl::SHAMap::walkSubTree : parent cowid do match");
1142 parent->shareChild(pos, node);
1145 node = std::move(parent);
1150 root_ = std::move(node);
1166 auto [node, nodeID] = stack.
top();
1175 if (node->isInner())
1180 if (!inner->isEmptyBranch(i))
1182 auto child = inner->getChildPointer(i);
1186 child->getHash() == inner->getChildHash(i),
1187 "xrpl::SHAMap::dump : child hash do match");
1188 stack.
push({child, nodeID.getChildNodeID(i)});
1195 }
while (!stack.
empty());
1197 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1205 !ret || !ret->cowid(),
1206 "xrpl::SHAMap::cacheLookup : not found or zero cowid");
1215 XRPL_ASSERT(
backed_,
"xrpl::SHAMap::canonicalize : is backed");
1217 node->cowid() == 0,
"xrpl::SHAMap::canonicalize : valid node input");
1219 node->getHash() == hash,
1220 "xrpl::SHAMap::canonicalize : node hash do match");
1230 XRPL_ASSERT(node,
"xrpl::SHAMap::invariants : non-null root node");
1232 !node->isLeaf(),
"xrpl::SHAMap::invariants : root node is not leaf");
1237 node->invariants(
true);
Stream trace() const
Severity stream access functions.
virtual beast::Journal const & journal()=0
virtual NodeStore::Database & db()=0
virtual void missingNodeAcquireBySeq(std::uint32_t refNum, uint256 const &nodeHash)=0
Acquire ledger that has a missing node by ledger sequence.
virtual std::shared_ptr< TreeNodeCache > getTreeNodeCache()=0
Return a pointer to the Family Tree Node Cache.
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
SHAMapHash const & getChildHash(int m) const
SHAMapTreeNode * getChildPointer(int branch)
bool isInner() const override
Determines if this is an inner node.
intr_ptr::SharedPtr< SHAMapTreeNode > canonicalizeChild(int branch, intr_ptr::SharedPtr< SHAMapTreeNode > node)
intr_ptr::SharedPtr< SHAMapTreeNode > getChild(int branch)
void setChild(int m, intr_ptr::SharedPtr< SHAMapTreeNode > child)
bool isEmptyBranch(int m) const
boost::intrusive_ptr< SHAMapItem const > const & peekItem() const
Identifies a node inside a SHAMap.
SHAMapNodeID getChildNodeID(unsigned int m) const
virtual std::optional< Blob > getNode(SHAMapHash const &nodeHash) const =0
virtual void gotNode(bool fromFilter, SHAMapHash const &nodeHash, std::uint32_t ledgerSeq, Blob &&nodeData, SHAMapNodeType type) const =0
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
SHAMapHash const & getHash() const
Return the hash of this node.
virtual bool isLeaf() const =0
Determines if this is a leaf node.
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
SHAMapTreeNode * descend(SHAMapInnerNode *, int branch) const
SHAMapLeafNode * walkTowardsKey(uint256 const &id, SharedPtrNodeStack *stack=nullptr) const
Walk towards the specified id, returning the node.
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.
const_iterator lower_bound(uint256 const &id) const
Find the object with the greatest object id smaller than the input id.
intr_ptr::SharedPtr< SHAMapTreeNode > checkFilter(SHAMapHash const &hash, SHAMapSyncFilter *filter) const
const_iterator end() const
SHAMapLeafNode * firstBelow(intr_ptr::SharedPtr< SHAMapTreeNode >, SharedPtrNodeStack &stack, int branch=0) const
intr_ptr::SharedPtr< SHAMapTreeNode > writeNode(NodeObjectType t, intr_ptr::SharedPtr< SHAMapTreeNode > node) const
write and canonicalize modified node
SHAMapTreeNode * descendAsync(SHAMapInnerNode *parent, int branch, SHAMapSyncFilter *filter, bool &pending, descendCallback &&) const
std::uint32_t cowid_
ID to distinguish this map for all others we're sharing nodes with.
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
int flushDirty(NodeObjectType t)
Flush modified nodes to the nodestore and convert them to shared.
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNodeFromDB(SHAMapHash const &hash) const
SHAMapLeafNode * lastBelow(intr_ptr::SharedPtr< SHAMapTreeNode > node, SharedPtrNodeStack &stack, int branch=branchFactor) const
static constexpr unsigned int leafDepth
The depth of the hash map: data is only present in the leaves.
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNode(SHAMapHash const &hash) const
SHAMapLeafNode const * peekNextItem(uint256 const &id, SharedPtrNodeStack &stack) const
void dump(bool withHashes=false) 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
static constexpr unsigned int branchFactor
Number of children each non-leaf node has (the 'radix tree' part of the map)
std::shared_ptr< SHAMap > snapShot(bool isMutable) const
bool hasItem(uint256 const &id) const
Does the tree have an item with the given ID?
int unshare()
Convert any modified nodes to shared.
bool updateGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
intr_ptr::SharedPtr< SHAMapTreeNode > finishFetch(SHAMapHash const &hash, std::shared_ptr< NodeObject > const &object) const
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNodeNT(SHAMapHash const &hash) const
void canonicalize(SHAMapHash const &hash, intr_ptr::SharedPtr< SHAMapTreeNode > &) const
SHAMapLeafNode * findKey(uint256 const &id) const
Return nullptr if key not found.
bool addGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
intr_ptr::SharedPtr< SHAMapTreeNode > cacheLookup(SHAMapHash const &hash) const
intr_ptr::SharedPtr< Node > preFlushNode(intr_ptr::SharedPtr< Node > node) const
prepare a node to be modified before flushing
intr_ptr::SharedPtr< Node > unshareNode(intr_ptr::SharedPtr< Node >, SHAMapNodeID const &nodeID)
Unshare the node, allowing it to be modified.
boost::intrusive_ptr< SHAMapItem const > const & onlyBelow(SHAMapTreeNode *) const
If there is only one leaf below this node, get its contents.
void dirtyUp(SharedPtrNodeStack &stack, uint256 const &target, intr_ptr::SharedPtr< SHAMapTreeNode > terminal)
Update hashes up to the root.
bool fetchRoot(SHAMapHash const &hash, SHAMapSyncFilter *filter)
std::uint32_t ledgerSeq_
The sequence of the ledger that this map references, if any.
intr_ptr::SharedPtr< SHAMapTreeNode > descendNoStore(SHAMapInnerNode &, int branch) const
int walkSubTree(bool doWrite, NodeObjectType t)
SHAMapHash getHash() const
bool delItem(uint256 const &id)
SHAMapTreeNode * descendThrow(SHAMapInnerNode *, int branch) const
intr_ptr::SharedPtr< SHAMapTreeNode > root_
SHAMapLeafNode const * peekFirstItem(SharedPtrNodeStack &stack) const
A shared intrusive pointer class that supports weak pointers.
T * get() const
Get the raw pointer.
void reset()
Set the pointer to null, decrement the strong count, and run the appropriate release action.
void adopt(T *p)
Adopt the raw pointer.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static boost::intrusive_ptr< SHAMapItem const > const no_item
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
@ pending
List will be valid in the future.
NodeObjectType
The types of node objects.
intr_ptr::SharedPtr< SHAMapLeafNode > makeTypedLeaf(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item, std::uint32_t owner)
unsigned int selectBranch(SHAMapNodeID const &id, uint256 const &hash)
Returns the branch that would contain the given hash.
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.
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)