1#include <xrpl/basics/random.h>
2#include <xrpl/shamap/SHAMap.h>
3#include <xrpl/shamap/SHAMapLeafNode.h>
4#include <xrpl/shamap/SHAMapSyncFilter.h>
26 if (!
root_->isInner())
32 auto node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
root_);
39 if (!node->isEmptyBranch(pos))
42 if (!function(*child))
50 while ((pos != 15) && (node->isEmptyBranch(pos + 1)))
60 node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(child);
86 if (
root_->getHash().isZero())
89 if (have && (
root_->getHash() == have->
root_->getHash()))
94 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(
root_);
95 if (!have || !have->
hasLeafNode(leaf->peekItem()->key(), leaf->getHash()))
105 while (!stack.
empty())
107 auto const [node, nodeID] = stack.
top();
111 if (!function(*node))
115 for (
int i = 0; i < 16; ++i)
117 if (!node->isEmptyBranch(i))
119 auto const& childHash = node->getChildHash(i);
130 if (!function(*next))
151 while (currentChild < 16)
153 int branch = (firstChild + currentChild++) % 16;
174 std::unique_lock<std::mutex> lock{mn.deferLock_};
175 mn.
finishedReads_.emplace_back(node, nodeID, branch, std::move(found));
214 node->setFullBelowGen(mn.generation_);
217 f_.getFullBelowCache()->insert(node->getHash().as_uint256());
246 auto const& nodeHash = parent->getChildHash(branch);
250 nodePtr = parent->canonicalizeChild(branch, std::move(nodePtr));
258 mn.
missingNodes_.emplace_back(parentID.getChildNodeID(branch), nodeHash.as_uint256());
275 XRPL_ASSERT(root_->getHash().isNonZero(),
"xrpl::SHAMap::getMissingNodes : nonzero root hash");
276 XRPL_ASSERT(max > 0,
"xrpl::SHAMap::getMissingNodes : valid max input");
282 f_.getFullBelowCache()->getGeneration());
284 if (!root_->isInner() || intr_ptr::static_pointer_cast<SHAMapInnerNode>(root_)->isFullBelow(mn.
generation_))
306 gmn_ProcessNodes(mn, pos);
311 if ((node ==
nullptr) && !mn.
stack_.empty())
314 bool was = fullBelow;
326 fullBelow = fullBelow && was;
328 XRPL_ASSERT(node,
"xrpl::SHAMap::getMissingNodes : first non-null node");
335 gmn_ProcessDeferredReads(mn);
358 XRPL_ASSERT(node,
"xrpl::SHAMap::getMissingNodes : second non-null node");
366 }
while (node !=
nullptr);
384 auto node = root_.get();
391 if (inner->isEmptyBranch(branch))
393 node = descendThrow(inner, branch);
397 if (node ==
nullptr || wanted != nodeID)
399 JLOG(journal_.info()) <<
"peer requested node that is not in the map: " << wanted <<
" but found " << nodeID;
405 JLOG(journal_.warn()) <<
"peer requests empty node";
410 stack.
emplace(node, nodeID, depth);
414 while (!stack.
empty())
421 node->serializeForWire(s);
431 if ((depth > 0) || (bc == 1))
434 for (
int i = 0; i < 16; ++i)
436 if (!inner->isEmptyBranch(i))
438 auto const childNode = descendThrow(inner, i);
441 if (childNode->isInner() && ((depth > 1) || (bc == 1)))
445 stack.
emplace(childNode, childID, (bc > 1) ? (depth - 1) : depth);
447 else if (childNode->isInner() || fatLeaves)
451 childNode->serializeForWire(s);
466 root_->serializeForWire(s);
473 if (root_->getHash().isNonZero())
475 JLOG(journal_.trace()) <<
"got root node, already have one";
476 XRPL_ASSERT(root_->getHash() == hash,
"xrpl::SHAMap::addRootNode : valid hash input");
477 return SHAMapAddNode::duplicate();
480 XRPL_ASSERT(cowid_ >= 1,
"xrpl::SHAMap::addRootNode : valid cowid");
481 auto node = SHAMapTreeNode::makeFromWire(rootNode);
482 if (!node || node->getHash() != hash)
483 return SHAMapAddNode::invalid();
486 canonicalize(hash, node);
496 root_->serializeWithPrefix(s);
497 filter->
gotNode(
false, root_->getHash(), ledgerSeq_, std::move(s.
modData()), root_->getType());
500 return SHAMapAddNode::useful();
506 XRPL_ASSERT(!node.
isRoot(),
"xrpl::SHAMap::addKnownNode : valid node input");
510 JLOG(journal_.trace()) <<
"AddKnownNode while not synching";
511 return SHAMapAddNode::duplicate();
514 auto const generation = f_.getFullBelowCache()->getGeneration();
516 auto currNode = root_.get();
522 XRPL_ASSERT(branch >= 0,
"xrpl::SHAMap::addKnownNode : valid branch");
524 if (inner->isEmptyBranch(branch))
526 JLOG(journal_.warn()) <<
"Add known node for empty branch" << node;
527 return SHAMapAddNode::invalid();
530 auto childHash = inner->getChildHash(branch);
531 if (f_.getFullBelowCache()->touch_if_exists(childHash.as_uint256()))
533 return SHAMapAddNode::duplicate();
536 auto prevNode = inner;
537 std::tie(currNode, currNodeID) = descend(inner, currNodeID, branch, filter);
539 if (currNode !=
nullptr)
542 auto newNode = SHAMapTreeNode::makeFromWire(rawNode);
544 if (!newNode || childHash != newNode->getHash())
546 JLOG(journal_.warn()) <<
"Corrupt node received";
547 return SHAMapAddNode::invalid();
555 if (newNode->isLeaf())
557 auto const& actualKey =
static_cast<SHAMapLeafNode const*
>(newNode.get())->peekItem()->key();
560 auto const expectedNodeID = SHAMapNodeID::createID(node.
getDepth(), actualKey);
561 if (expectedNodeID.getNodeID() != node.
getNodeID())
563 JLOG(journal_.debug()) <<
"Leaf node position mismatch: "
564 <<
"expected=" << expectedNodeID.getNodeID() <<
", actual=" << node.
getNodeID();
565 return SHAMapAddNode::invalid();
572 if ((currNodeID.
getDepth() > leafDepth) || (newNode->isInner() && currNodeID.
getDepth() == leafDepth))
575 state_ = SHAMapState::Invalid;
576 return SHAMapAddNode::useful();
579 if (currNodeID != node)
582 JLOG(journal_.warn()) <<
"unable to hook node " << node;
583 JLOG(journal_.info()) <<
" stuck at " << currNodeID;
584 JLOG(journal_.info()) <<
"got depth=" << node.
getDepth() <<
", walked to= " << currNodeID.
getDepth();
585 return SHAMapAddNode::useful();
589 canonicalize(childHash, newNode);
591 newNode = prevNode->canonicalizeChild(branch, std::move(newNode));
596 newNode->serializeWithPrefix(s);
597 filter->
gotNode(
false, childHash, ledgerSeq_, std::move(s.
modData()), newNode->getType());
600 return SHAMapAddNode::useful();
603 JLOG(journal_.trace()) <<
"got node, already had it (late)";
604 return SHAMapAddNode::duplicate();
613 stack.
push({root_.get(), other.
root_.get()});
615 while (!stack.
empty())
617 auto const [node, otherNode] = stack.
top();
620 if (!node || !otherNode)
622 JLOG(journal_.info()) <<
"unable to fetch node";
625 else if (otherNode->getHash() != node->getHash())
627 JLOG(journal_.warn()) <<
"node hash mismatch";
633 if (!otherNode->isLeaf())
636 auto& otherNodePeek =
static_cast<SHAMapLeafNode*
>(otherNode)->peekItem();
637 if (nodePeek->key() != otherNodePeek->key())
639 if (nodePeek->slice() != otherNodePeek->slice())
642 else if (node->isInner())
644 if (!otherNode->isInner())
648 for (
int i = 0; i < 16; ++i)
650 if (node_inner->isEmptyBranch(i))
652 if (!other_inner->isEmptyBranch(i))
657 if (other_inner->isEmptyBranch(i))
660 auto next = descend(node_inner, i);
661 auto otherNext = other.
descend(other_inner, i);
662 if (!next || !otherNext)
664 JLOG(journal_.warn()) <<
"unable to fetch inner node";
667 stack.
push({next, otherNext});
681 auto node = root_.get();
688 if (inner->isEmptyBranch(branch))
691 node = descendThrow(inner, branch);
695 return (node->isInner()) && (node->getHash() == targetNodeHash);
703 auto node = root_.get();
706 if (!node->isInner())
707 return node->getHash() == targetNodeHash;
713 if (inner->isEmptyBranch(branch))
716 if (inner->getChildHash(branch) == targetNodeHash)
719 node = descendThrow(inner, branch);
721 }
while (node->isInner());
731 walkTowardsKey(key, &stack);
735 JLOG(journal_.debug()) <<
"no path to " << key;
739 if (
auto const& node = stack.
top().
first;
740 !node || node->isInner() || intr_ptr::static_pointer_cast<SHAMapLeafNode>(node)->peekItem()->key() != key)
742 JLOG(journal_.debug()) <<
"no path to " << key;
748 while (!stack.
empty())
751 stack.
top().
first->serializeForWire(s);
756 JLOG(journal_.debug()) <<
"getPath for key " << key <<
", path length " << path.
size();
769 for (
auto rit = path.
rbegin(); rit != path.
rend(); ++rit)
771 auto const& blob = *rit;
772 auto node = SHAMapTreeNode::makeFromWire(
makeSlice(blob));
776 if (node->getHash() != hash)
782 auto nodeId = SHAMapNodeID::createID(depth, key);
788 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_