1#include <xrpl/basics/random.h>
2#include <xrpl/shamap/SHAMap.h>
3#include <xrpl/shamap/SHAMapLeafNode.h>
4#include <xrpl/shamap/SHAMapSyncFilter.h>
10 std::function<
void(boost::intrusive_ptr<SHAMapItem const>
const& item)>
const& leafFunction)
28 if (!
root_->isInner())
34 auto node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
root_);
41 if (!node->isEmptyBranch(pos))
44 if (!function(*child))
52 while ((pos != 15) && (node->isEmptyBranch(pos + 1)))
62 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(child);
90 if (
root_->getHash().isZero())
93 if (have && (
root_->getHash() == have->
root_->getHash()))
98 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(
root_);
99 if (!have || !have->
hasLeafNode(leaf->peekItem()->key(), leaf->getHash()))
109 while (!stack.
empty())
111 auto const [node, nodeID] = stack.
top();
115 if (!function(*node))
119 for (
int i = 0; i < 16; ++i)
121 if (!node->isEmptyBranch(i))
123 auto const& childHash = node->getChildHash(i);
137 if (!function(*next))
158 while (currentChild < 16)
160 int branch = (firstChild + currentChild++) % 16;
179 [node, nodeID, branch, &mn](
182 std::unique_lock<std::mutex> lock{mn.deferLock_};
183 mn.
finishedReads_.emplace_back(node, nodeID, branch, std::move(found));
223 node->setFullBelowGen(mn.generation_);
226 f_.getFullBelowCache()->insert(node->getHash().as_uint256());
256 auto const& nodeHash = parent->getChildHash(branch);
260 nodePtr = parent->canonicalizeChild(branch, std::move(nodePtr));
268 mn.
missingNodes_.emplace_back(parentID.getChildNodeID(branch), nodeHash.as_uint256());
285 XRPL_ASSERT(root_->getHash().isNonZero(),
"xrpl::SHAMap::getMissingNodes : nonzero root hash");
286 XRPL_ASSERT(max > 0,
"xrpl::SHAMap::getMissingNodes : valid max input");
292 f_.getFullBelowCache()->getGeneration());
294 if (!root_->isInner() ||
295 intr_ptr::static_pointer_cast<SHAMapInnerNode>(root_)->isFullBelow(mn.
generation_))
318 gmn_ProcessNodes(mn, pos);
323 if ((node ==
nullptr) && !mn.
stack_.empty())
326 bool was = fullBelow;
338 fullBelow = fullBelow && was;
340 XRPL_ASSERT(node,
"xrpl::SHAMap::getMissingNodes : first non-null node");
347 gmn_ProcessDeferredReads(mn);
370 XRPL_ASSERT(node,
"xrpl::SHAMap::getMissingNodes : second non-null node");
378 }
while (node !=
nullptr);
396 auto node = root_.get();
403 if (inner->isEmptyBranch(branch))
405 node = descendThrow(inner, branch);
409 if (node ==
nullptr || wanted != nodeID)
411 JLOG(journal_.info()) <<
"peer requested node that is not in the map: " << wanted
412 <<
" but found " << nodeID;
418 JLOG(journal_.warn()) <<
"peer requests empty node";
423 stack.
emplace(node, nodeID, depth);
427 while (!stack.
empty())
434 node->serializeForWire(s);
444 if ((depth > 0) || (bc == 1))
447 for (
int i = 0; i < 16; ++i)
449 if (!inner->isEmptyBranch(i))
451 auto const childNode = descendThrow(inner, i);
454 if (childNode->isInner() && ((depth > 1) || (bc == 1)))
458 stack.
emplace(childNode, childID, (bc > 1) ? (depth - 1) : depth);
460 else if (childNode->isInner() || fatLeaves)
464 childNode->serializeForWire(s);
479 root_->serializeForWire(s);
486 if (root_->getHash().isNonZero())
488 JLOG(journal_.trace()) <<
"got root node, already have one";
489 XRPL_ASSERT(root_->getHash() == hash,
"xrpl::SHAMap::addRootNode : valid hash input");
490 return SHAMapAddNode::duplicate();
493 XRPL_ASSERT(cowid_ >= 1,
"xrpl::SHAMap::addRootNode : valid cowid");
494 auto node = SHAMapTreeNode::makeFromWire(rootNode);
495 if (!node || node->getHash() != hash)
496 return SHAMapAddNode::invalid();
499 canonicalize(hash, node);
509 root_->serializeWithPrefix(s);
511 false, root_->getHash(), ledgerSeq_, std::move(s.
modData()), root_->getType());
514 return SHAMapAddNode::useful();
520 XRPL_ASSERT(!node.
isRoot(),
"xrpl::SHAMap::addKnownNode : valid node input");
524 JLOG(journal_.trace()) <<
"AddKnownNode while not synching";
525 return SHAMapAddNode::duplicate();
528 auto const generation = f_.getFullBelowCache()->getGeneration();
530 auto currNode = root_.get();
532 while (currNode->isInner() &&
537 XRPL_ASSERT(branch >= 0,
"xrpl::SHAMap::addKnownNode : valid branch");
539 if (inner->isEmptyBranch(branch))
541 JLOG(journal_.warn()) <<
"Add known node for empty branch" << node;
542 return SHAMapAddNode::invalid();
545 auto childHash = inner->getChildHash(branch);
546 if (f_.getFullBelowCache()->touch_if_exists(childHash.as_uint256()))
548 return SHAMapAddNode::duplicate();
551 auto prevNode = inner;
552 std::tie(currNode, currNodeID) = descend(inner, currNodeID, branch, filter);
554 if (currNode !=
nullptr)
557 auto newNode = SHAMapTreeNode::makeFromWire(rawNode);
559 if (!newNode || childHash != newNode->getHash())
561 JLOG(journal_.warn()) <<
"Corrupt node received";
562 return SHAMapAddNode::invalid();
570 if (newNode->isLeaf())
572 auto const& actualKey =
573 static_cast<SHAMapLeafNode const*
>(newNode.get())->peekItem()->key();
576 auto const expectedNodeID = SHAMapNodeID::createID(node.
getDepth(), actualKey);
577 if (expectedNodeID.getNodeID() != node.
getNodeID())
579 JLOG(journal_.debug())
580 <<
"Leaf node position mismatch: "
581 <<
"expected=" << expectedNodeID.getNodeID() <<
", actual=" << node.
getNodeID();
582 return SHAMapAddNode::invalid();
589 if ((currNodeID.
getDepth() > leafDepth) ||
590 (newNode->isInner() && currNodeID.
getDepth() == leafDepth))
593 state_ = SHAMapState::Invalid;
594 return SHAMapAddNode::useful();
597 if (currNodeID != node)
600 JLOG(journal_.warn()) <<
"unable to hook node " << node;
601 JLOG(journal_.info()) <<
" stuck at " << currNodeID;
602 JLOG(journal_.info()) <<
"got depth=" << node.
getDepth()
603 <<
", walked to= " << currNodeID.
getDepth();
604 return SHAMapAddNode::useful();
608 canonicalize(childHash, newNode);
610 newNode = prevNode->canonicalizeChild(branch, std::move(newNode));
615 newNode->serializeWithPrefix(s);
617 false, childHash, ledgerSeq_, std::move(s.
modData()), newNode->getType());
620 return SHAMapAddNode::useful();
623 JLOG(journal_.trace()) <<
"got node, already had it (late)";
624 return SHAMapAddNode::duplicate();
633 stack.
push({root_.get(), other.
root_.get()});
635 while (!stack.
empty())
637 auto const [node, otherNode] = stack.
top();
640 if (!node || !otherNode)
642 JLOG(journal_.info()) <<
"unable to fetch node";
645 else if (otherNode->getHash() != node->getHash())
647 JLOG(journal_.warn()) <<
"node hash mismatch";
653 if (!otherNode->isLeaf())
656 auto& otherNodePeek =
static_cast<SHAMapLeafNode*
>(otherNode)->peekItem();
657 if (nodePeek->key() != otherNodePeek->key())
659 if (nodePeek->slice() != otherNodePeek->slice())
662 else if (node->isInner())
664 if (!otherNode->isInner())
668 for (
int i = 0; i < 16; ++i)
670 if (node_inner->isEmptyBranch(i))
672 if (!other_inner->isEmptyBranch(i))
677 if (other_inner->isEmptyBranch(i))
680 auto next = descend(node_inner, i);
681 auto otherNext = other.
descend(other_inner, i);
682 if (!next || !otherNext)
684 JLOG(journal_.warn()) <<
"unable to fetch inner node";
687 stack.
push({next, otherNext});
701 auto node = root_.get();
708 if (inner->isEmptyBranch(branch))
711 node = descendThrow(inner, branch);
715 return (node->isInner()) && (node->getHash() == targetNodeHash);
723 auto node = root_.get();
726 if (!node->isInner())
727 return node->getHash() == targetNodeHash;
733 if (inner->isEmptyBranch(branch))
736 if (inner->getChildHash(branch) == targetNodeHash)
739 node = descendThrow(inner, branch);
741 }
while (node->isInner());
751 walkTowardsKey(key, &stack);
755 JLOG(journal_.debug()) <<
"no path to " << key;
759 if (
auto const& node = stack.
top().
first; !node || node->isInner() ||
760 intr_ptr::static_pointer_cast<SHAMapLeafNode>(node)->peekItem()->key() != key)
762 JLOG(journal_.debug()) <<
"no path to " << key;
768 while (!stack.
empty())
771 stack.
top().
first->serializeForWire(s);
776 JLOG(journal_.debug()) <<
"getPath for key " << key <<
", path length " << path.
size();
789 for (
auto rit = path.
rbegin(); rit != path.
rend(); ++rit)
791 auto const& blob = *rit;
792 auto node = SHAMapTreeNode::makeFromWire(
makeSlice(blob));
796 if (node->getHash() != hash)
802 auto nodeId = SHAMapNodeID::createID(depth, key);
809 return depth + 1 == path.
size();
virtual std::shared_ptr< FullBelowCache > getFullBelowCache()=0
Return a pointer to the Family Full Below Cache.
bool isFullBelow(std::uint32_t generation) const
SHAMapHash const & getChildHash(int m) const
int getBranchCount() const
bool isEmptyBranch(int m) const
boost::intrusive_ptr< SHAMapItem const > const & peekItem() const
Identifies a node inside a SHAMap.
uint256 const & getNodeID() const
SHAMapNodeID getChildNodeID(unsigned int m) const
unsigned int getDepth() const
virtual void gotNode(bool fromFilter, SHAMapHash const &nodeHash, std::uint32_t ledgerSeq, Blob &&nodeData, SHAMapNodeType type) const =0
virtual bool isInner() const =0
Determines if this is an inner node.
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
SHAMapTreeNode * descend(SHAMapInnerNode *, int branch) const
SHAMapTreeNode * descendAsync(SHAMapInnerNode *parent, int branch, SHAMapSyncFilter *filter, bool &pending, descendCallback &&) const
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
void visitDifferences(SHAMap const *have, std::function< bool(SHAMapTreeNode const &)> const &) const
Visit every node in this SHAMap that is not present in the specified SHAMap.
bool hasInnerNode(SHAMapNodeID const &nodeID, SHAMapHash const &hash) const
Does this map have this inner node?
void gmn_ProcessNodes(MissingNodes &, MissingNodes::StackEntry &node)
void visitLeaves(std::function< void(boost::intrusive_ptr< SHAMapItem const > const &)> const &) const
Visit every leaf node in this SHAMap.
bool hasLeafNode(uint256 const &tag, SHAMapHash const &hash) const
Does this map have this leaf node?
intr_ptr::SharedPtr< SHAMapTreeNode > descendNoStore(SHAMapInnerNode &, int branch) const
void visitNodes(std::function< bool(SHAMapTreeNode &)> const &function) const
Visit every node in this SHAMap.
SHAMapTreeNode * descendThrow(SHAMapInnerNode *, int branch) const
intr_ptr::SharedPtr< SHAMapTreeNode > root_
A shared intrusive pointer class that supports weak pointers.
An immutable linear range of bytes.
T emplace_back(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
@ pending
List will be valid in the future.
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
unsigned int selectBranch(SHAMapNodeID const &id, uint256 const &hash)
Returns the branch that would contain the given hash.
@ innerNode
inner node in V1 tree
std::enable_if_t< std::is_integral< Integral >::value &&detail::is_engine< Engine >::value, Integral > rand_int(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
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::condition_variable deferCondVar_
std::map< SHAMapInnerNode *, SHAMapNodeID > resumes_
std::vector< DeferredNode > finishedReads_
std::set< SHAMapHash > missingHashes_
std::vector< std::pair< SHAMapNodeID, uint256 > > missingNodes_
std::stack< StackEntry, std::deque< StackEntry > > stack_
SHAMapSyncFilter * filter_
std::uint32_t generation_