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 "ripple::SHAMap::dirtyUp : valid state");
90 child && (child->cowid() ==
cowid_),
91 "ripple::SHAMap::dirtyUp : valid child input");
93 while (!stack.
empty())
96 intr_ptr::dynamic_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
99 XRPL_ASSERT(node,
"ripple::SHAMap::dirtyUp : non-null node");
102 XRPL_ASSERT(branch >= 0,
"ripple::SHAMap::dirtyUp : valid branch");
105 node->setChild(branch, std::move(child));
107 child = std::move(node);
115 stack ==
nullptr || stack->
empty(),
116 "ripple::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_,
"ripple::SHAMap::fetchNodeFromDB : is backed");
162 XRPL_ASSERT(
backed_,
"ripple::SHAMap::finishFetch : is backed");
189 <<
"finishFetch exception: unknonw 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(),
"ripple::SHAMap::descend : valid parent input");
350 "ripple::SHAMap::descend : valid branch input");
353 "ripple::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 "ripple::SHAMap::unshareNode : node valid for cowid");
424 if (node->cowid() !=
cowid_)
429 "ripple::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))
464 "ripple::SHAMap::belowHelper : non-empty stack");
467 auto n = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
471 inner = intr_ptr::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});
504static boost::intrusive_ptr<SHAMapItem const>
const no_item;
506boost::intrusive_ptr<SHAMapItem const>
const&
517 if (!inner->isEmptyBranch(i))
529 UNREACHABLE(
"ripple::SHAMap::onlyBelow : no next node");
541 leaf->peekItem() || (leaf ==
root_.get()),
542 "ripple::SHAMap::onlyBelow : valid inner node");
543 return leaf->peekItem();
550 stack.
empty(),
"ripple::SHAMap::peekFirstItem : empty stack input");
554 while (!stack.
empty())
565 !stack.
empty(),
"ripple::SHAMap::peekNextItem : non-empty stack input");
568 "ripple::SHAMap::peekNextItem : stack starts with leaf");
570 while (!stack.
empty())
572 auto [node, nodeID] = stack.
top();
575 "ripple::SHAMap::peekNextItem : another node is not leaf");
576 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
579 if (!inner->isEmptyBranch(i))
584 Throw<SHAMapMissingNode>(
type_,
id);
587 "ripple::SHAMap::peekNextItem : leaf is valid");
597boost::intrusive_ptr<SHAMapItem const>
const&
608boost::intrusive_ptr<SHAMapItem const>
const&
625 while (!stack.
empty())
627 auto [node, nodeID] = stack.
top();
631 if (leaf->peekItem()->key() > id)
633 this, leaf->peekItem().get(), std::move(stack));
637 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
642 if (!inner->isEmptyBranch(branch))
647 Throw<SHAMapMissingNode>(
type_,
id);
649 this, leaf->peekItem().get(), std::move(stack));
662 while (!stack.
empty())
664 auto [node, nodeID] = stack.
top();
668 if (leaf->peekItem()->key() < id)
670 this, leaf->peekItem().get(), std::move(stack));
674 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
675 for (
int branch =
selectBranch(nodeID,
id) - 1; branch >= 0;
678 if (!inner->isEmptyBranch(branch))
681 auto leaf =
lastBelow(node, stack, branch);
683 Throw<SHAMapMissingNode>(
type_,
id);
685 this, leaf->peekItem().get(), std::move(stack));
698 return (
findKey(
id) !=
nullptr);
707 "ripple::SHAMap::delItem : not immutable");
713 Throw<SHAMapMissingNode>(
type_,
id);
716 intr_ptr::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
719 if (!leaf || (leaf->peekItem()->key() !=
id))
728 while (!stack.
empty())
731 intr_ptr::static_pointer_cast<SHAMapInnerNode>(stack.
top().
first);
736 node->setChild(
selectBranch(nodeID,
id), std::move(prevNode));
742 int const bc = node->getBranchCount();
757 if (!node->isEmptyBranch(i))
769 prevNode = std::move(node);
775 prevNode = std::move(node);
786 boost::intrusive_ptr<SHAMapItem const> item)
790 "ripple::SHAMap::addGiveItem : not immutable");
793 "ripple::SHAMap::addGiveItem : valid type input");
802 Throw<SHAMapMissingNode>(
type_, tag);
804 auto [node, nodeID] = stack.
top();
809 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
810 if (leaf->peekItem()->key() == tag)
817 auto inner = intr_ptr::static_pointer_cast<SHAMapInnerNode>(node);
820 inner->isEmptyBranch(branch),
821 "ripple::SHAMap::addGiveItem : inner branch is empty");
828 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(node);
829 auto otherItem = leaf->peekItem();
831 otherItem && (tag != otherItem->key()),
832 "ripple::SHAMap::addGiveItem : non-null item");
834 node = intr_ptr::make_shared<SHAMapInnerNode>(node->cowid());
841 stack.
push({node, nodeID});
845 nodeID = nodeID.getChildNodeID(b1);
846 node = intr_ptr::make_shared<SHAMapInnerNode>(
cowid_);
851 node->isInner(),
"ripple::SHAMap::addGiveItem : node is inner");
865 boost::intrusive_ptr<SHAMapItem const> item)
873 auto hash =
root_->getHash();
877 hash =
root_->getHash();
885 boost::intrusive_ptr<SHAMapItem const> item)
892 "ripple::SHAMap::updateGiveItem : not immutable");
898 Throw<SHAMapMissingNode>(
type_, tag);
901 intr_ptr::dynamic_pointer_cast<SHAMapLeafNode>(stack.
top().
first);
905 if (!node || (node->peekItem()->key() != tag))
908 UNREACHABLE(
"ripple::SHAMap::updateGiveItem : invalid node");
913 if (node->getType() != type)
915 JLOG(
journal_.
fatal()) <<
"SHAMap::updateGiveItem: cross-type change!";
921 if (node->setItem(item))
930 if (hash ==
root_->getHash())
937 stream <<
"Fetch root TXN node " << hash;
941 stream <<
"Fetch root STATE node " << hash;
945 stream <<
"Fetch root SHAMap node " << hash;
955 root_->getHash() == hash,
956 "ripple::SHAMap::fetchRoot : root hash do match");
980 node->cowid() == 0,
"ripple::SHAMap::writeNode : valid input node");
981 XRPL_ASSERT(
backed_,
"ripple::SHAMap::writeNode : is backed");
986 node->serializeWithPrefix(s);
1002 node->cowid(),
"ripple::SHAMap::preFlushNode : valid input node");
1004 if (node->cowid() !=
cowid_)
1008 node = intr_ptr::static_pointer_cast<Node>(node->clone(
cowid_));
1031 !doWrite ||
backed_,
"ripple::SHAMap::walkSubTree : valid input");
1038 if (
root_->isLeaf())
1041 root_->updateHash();
1050 auto node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
root_);
1052 if (node->isEmpty())
1054 root_ = intr_ptr::make_shared<SHAMapInnerNode>(0);
1072 if (node->isEmptyBranch(pos))
1081 auto child = node->getChild(pos++);
1083 if (child && (child->cowid() != 0))
1089 if (child->isInner())
1093 stack.
emplace(std::move(node), branch);
1097 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
1108 "ripple::SHAMap::walkSubTree : node cowid do "
1110 child->updateHash();
1116 node->shareChild(branch, child);
1123 node->updateHashDeep();
1129 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
1137 auto parent = std::move(stack.
top().first);
1138 pos = stack.
top().second;
1143 parent->cowid() ==
cowid_,
1144 "ripple::SHAMap::walkSubTree : parent cowid do match");
1145 parent->shareChild(pos, node);
1148 node = std::move(parent);
1153 root_ = std::move(node);
1169 auto [node, nodeID] = stack.
top();
1178 if (node->isInner())
1183 if (!inner->isEmptyBranch(i))
1185 auto child = inner->getChildPointer(i);
1189 child->getHash() == inner->getChildHash(i),
1190 "ripple::SHAMap::dump : child hash do match");
1191 stack.
push({child, nodeID.getChildNodeID(i)});
1198 }
while (!stack.
empty());
1200 JLOG(
journal_.
info()) << leafCount <<
" resident leaves";
1208 !ret || !ret->cowid(),
1209 "ripple::SHAMap::cacheLookup : not found or zero cowid");
1218 XRPL_ASSERT(
backed_,
"ripple::SHAMap::canonicalize : is backed");
1220 node->cowid() == 0,
"ripple::SHAMap::canonicalize : valid node input");
1222 node->getHash() == hash,
1223 "ripple::SHAMap::canonicalize : node hash do match");
1233 XRPL_ASSERT(node,
"ripple::SHAMap::invariants : non-null root node");
1235 !node->isLeaf(),
"ripple::SHAMap::invariants : root node is not leaf");
1240 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.
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)
unsigned int selectBranch(SHAMapNodeID const &id, uint256 const &hash)
Returns the branch that would contain the given hash.
static boost::intrusive_ptr< SHAMapItem const > const no_item
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.