20#include <xrpld/shamap/SHAMap.h>
21#include <xrpld/shamap/SHAMapSyncFilter.h>
22#include <xrpl/basics/random.h>
28 std::function<
void(boost::intrusive_ptr<SHAMapItem const>
const&
29 item)>
const& leafFunction)
const
46 if (!
root_->isInner())
52 auto node = std::static_pointer_cast<SHAMapInnerNode>(
root_);
59 if (!node->isEmptyBranch(pos))
63 if (!function(*child))
71 while ((pos != 15) && (node->isEmptyBranch(pos + 1)))
81 node = std::static_pointer_cast<SHAMapInnerNode>(child);
109 if (
root_->getHash().isZero())
112 if (have && (
root_->getHash() == have->
root_->getHash()))
117 auto leaf = std::static_pointer_cast<SHAMapLeafNode>(
root_);
119 !have->
hasLeafNode(leaf->peekItem()->key(), leaf->getHash()))
129 while (!stack.
empty())
131 auto const [node, nodeID] = stack.
top();
135 if (!function(*node))
139 for (
int i = 0; i < 16; ++i)
141 if (!node->isEmptyBranch(i))
143 auto const& childHash = node->getChildHash(i);
159 if (!function(*next))
176 int& firstChild = std::get<2>(se);
177 int& currentChild = std::get<3>(se);
178 bool& fullBelow = std::get<4>(se);
180 while (currentChild < 16)
182 int branch = (firstChild + currentChild++) % 16;
203 [node, nodeID, branch, &mn](
206 std::unique_lock<std::mutex> lock{mn.deferLock_};
208 node, nodeID, branch, std::move(found));
250 node->setFullBelowGen(mn.generation_);
253 f_.getFullBelowCache()->insert(node->getHash().as_uint256());
283 auto parent = std::get<0>(deferredNode);
284 auto const& parentID = std::get<1>(deferredNode);
285 auto branch = std::get<2>(deferredNode);
286 auto nodePtr = std::get<3>(deferredNode);
287 auto const& nodeHash = parent->getChildHash(branch);
291 nodePtr = parent->canonicalizeChild(branch, std::move(nodePtr));
300 parentID.getChildNodeID(branch), nodeHash.as_uint256());
318 root_->getHash().isNonZero(),
319 "ripple::SHAMap::getMissingNodes : nonzero root hash");
320 XRPL_ASSERT(max > 0,
"ripple::SHAMap::getMissingNodes : valid max input");
326 f_.getFullBelowCache()->getGeneration());
328 if (!root_->isInner() ||
329 std::static_pointer_cast<SHAMapInnerNode>(root_)->isFullBelow(
348 auto& node = std::get<0>(pos);
349 auto& nextChild = std::get<3>(pos);
350 auto& fullBelow = std::get<4>(pos);
357 gmn_ProcessNodes(mn, pos);
362 if ((node ==
nullptr) && !mn.
stack_.empty())
365 bool was = fullBelow;
377 fullBelow = fullBelow && was;
381 "ripple::SHAMap::getMissingNodes : first non-null node");
388 gmn_ProcessDeferredReads(mn);
414 "ripple::SHAMap::getMissingNodes : second non-null node");
422 }
while (node !=
nullptr);
440 auto node = root_.get();
447 if (inner->isEmptyBranch(branch))
449 node = descendThrow(inner, branch);
453 if (node ==
nullptr || wanted != nodeID)
455 JLOG(journal_.info())
456 <<
"peer requested node that is not in the map: " << wanted
457 <<
" but found " << nodeID;
463 JLOG(journal_.warn()) <<
"peer requests empty node";
468 stack.
emplace(node, nodeID, depth);
472 while (!stack.
empty())
479 node->serializeForWire(s);
489 if ((depth > 0) || (bc == 1))
492 for (
int i = 0; i < 16; ++i)
494 if (!inner->isEmptyBranch(i))
496 auto const childNode = descendThrow(inner, i);
499 if (childNode->isInner() && ((depth > 1) || (bc == 1)))
506 (bc > 1) ? (depth - 1) : depth);
508 else if (childNode->isInner() || fatLeaves)
512 childNode->serializeForWire(s);
528 root_->serializeForWire(s);
534 Slice const& rootNode,
538 if (root_->getHash().isNonZero())
540 JLOG(journal_.trace()) <<
"got root node, already have one";
542 root_->getHash() == hash,
543 "ripple::SHAMap::addRootNode : valid hash input");
544 return SHAMapAddNode::duplicate();
547 XRPL_ASSERT(cowid_ >= 1,
"ripple::SHAMap::addRootNode : valid cowid");
548 auto node = SHAMapTreeNode::makeFromWire(rootNode);
549 if (!node || node->getHash() != hash)
550 return SHAMapAddNode::invalid();
553 canonicalize(hash, node);
563 root_->serializeWithPrefix(s);
572 return SHAMapAddNode::useful();
578 Slice const& rawNode,
582 !node.
isRoot(),
"ripple::SHAMap::addKnownNode : valid node input");
586 JLOG(journal_.trace()) <<
"AddKnownNode while not synching";
587 return SHAMapAddNode::duplicate();
590 auto const generation = f_.getFullBelowCache()->getGeneration();
592 auto iNode = root_.get();
594 while (iNode->isInner() &&
599 XRPL_ASSERT(branch >= 0,
"ripple::SHAMap::addKnownNode : valid branch");
601 if (inner->isEmptyBranch(branch))
603 JLOG(journal_.warn()) <<
"Add known node for empty branch" << node;
604 return SHAMapAddNode::invalid();
607 auto childHash = inner->getChildHash(branch);
608 if (f_.getFullBelowCache()->touch_if_exists(childHash.as_uint256()))
610 return SHAMapAddNode::duplicate();
613 auto prevNode = inner;
614 std::tie(iNode, iNodeID) = descend(inner, iNodeID, branch, filter);
616 if (iNode ==
nullptr)
618 auto newNode = SHAMapTreeNode::makeFromWire(rawNode);
620 if (!newNode || childHash != newNode->getHash())
622 JLOG(journal_.warn()) <<
"Corrupt node received";
623 return SHAMapAddNode::invalid();
629 if ((iNodeID.
getDepth() > leafDepth) ||
630 (newNode->isInner() && iNodeID.
getDepth() == leafDepth))
633 state_ = SHAMapState::Invalid;
634 return SHAMapAddNode::useful();
640 JLOG(journal_.warn()) <<
"unable to hook node " << node;
641 JLOG(journal_.info()) <<
" stuck at " << iNodeID;
642 JLOG(journal_.info()) <<
"got depth=" << node.
getDepth()
643 <<
", walked to= " << iNodeID.
getDepth();
644 return SHAMapAddNode::useful();
648 canonicalize(childHash, newNode);
650 newNode = prevNode->canonicalizeChild(branch, std::move(newNode));
655 newNode->serializeWithPrefix(s);
664 return SHAMapAddNode::useful();
668 JLOG(journal_.trace()) <<
"got node, already had it (late)";
669 return SHAMapAddNode::duplicate();
678 stack.
push({root_.get(), other.
root_.get()});
680 while (!stack.
empty())
682 auto const [node, otherNode] = stack.
top();
685 if (!node || !otherNode)
687 JLOG(journal_.info()) <<
"unable to fetch node";
690 else if (otherNode->getHash() != node->getHash())
692 JLOG(journal_.warn()) <<
"node hash mismatch";
698 if (!otherNode->isLeaf())
701 auto& otherNodePeek =
703 if (nodePeek->key() != otherNodePeek->key())
705 if (nodePeek->slice() != otherNodePeek->slice())
708 else if (node->isInner())
710 if (!otherNode->isInner())
714 for (
int i = 0; i < 16; ++i)
716 if (node_inner->isEmptyBranch(i))
718 if (!other_inner->isEmptyBranch(i))
723 if (other_inner->isEmptyBranch(i))
726 auto next = descend(node_inner, i);
727 auto otherNext = other.
descend(other_inner, i);
728 if (!next || !otherNext)
730 JLOG(journal_.warn()) <<
"unable to fetch inner node";
733 stack.
push({next, otherNext});
749 auto node = root_.get();
756 if (inner->isEmptyBranch(branch))
759 node = descendThrow(inner, branch);
763 return (node->isInner()) && (node->getHash() == targetNodeHash);
771 auto node = root_.get();
774 if (!node->isInner())
775 return node->getHash() == targetNodeHash;
781 if (inner->isEmptyBranch(branch))
784 if (inner->getChildHash(branch) ==
788 node = descendThrow(inner, branch);
790 }
while (node->isInner());
800 walkTowardsKey(key, &stack);
804 JLOG(journal_.debug()) <<
"no path to " << key;
808 if (
auto const& node = stack.
top().
first; !node || node->isInner() ||
809 std::static_pointer_cast<SHAMapLeafNode>(node)->peekItem()->key() !=
812 JLOG(journal_.debug()) <<
"no path to " << key;
817 path.reserve(stack.
size());
818 while (!stack.
empty())
821 stack.
top().
first->serializeForWire(s);
822 path.emplace_back(std::move(s.
modData()));
826 JLOG(journal_.debug()) <<
"getPath for key " << key <<
", path length "
832SHAMap::verifyProofPath(
837 if (path.empty() || path.size() > 65)
843 for (
auto rit = path.rbegin(); rit != path.rend(); ++rit)
845 auto const& blob = *rit;
846 auto node = SHAMapTreeNode::makeFromWire(
makeSlice(blob));
850 if (node->getHash() != hash)
856 auto nodeId = SHAMapNodeID::createID(depth, key);
863 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
bool isEmptyBranch(int m) const
SHAMapHash const & getChildHash(int m) const
int getBranchCount() const
boost::intrusive_ptr< SHAMapItem const > const & peekItem() const
Identifies a node inside a SHAMap.
unsigned int getDepth() const
uint256 const & getNodeID() const
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 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 * descendAsync(SHAMapInnerNode *parent, int branch, SHAMapSyncFilter *filter, bool &pending, descendCallback &&) const
void gmn_ProcessNodes(MissingNodes &, MissingNodes::StackEntry &node)
SHAMapTreeNode * descendThrow(SHAMapInnerNode *, int branch) const
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
SHAMapTreeNode * descend(SHAMapInnerNode *, int branch) const
std::shared_ptr< SHAMapTreeNode > root_
bool hasInnerNode(SHAMapNodeID const &nodeID, SHAMapHash const &hash) const
Does this map have this inner node?
std::shared_ptr< SHAMapTreeNode > descendNoStore(std::shared_ptr< SHAMapInnerNode > const &, int branch) const
void visitNodes(std::function< bool(SHAMapTreeNode &)> const &function) const
Visit every node in this SHAMap.
bool hasLeafNode(uint256 const &tag, SHAMapHash const &hash) const
Does this map have this leaf node?
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.
void visitLeaves(std::function< void(boost::intrusive_ptr< SHAMapItem const > const &)> const &) const
Visit every leaf node in this SHAMap.
An immutable linear range of bytes.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
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.
@ pending
List will be valid in the future.
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)
@ innerNode
inner node in V1 tree
std::set< SHAMapHash > missingHashes_
std::stack< StackEntry, std::deque< StackEntry > > stack_
std::condition_variable deferCondVar_
std::uint32_t generation_
std::vector< std::pair< SHAMapNodeID, uint256 > > missingNodes_
std::map< SHAMapInnerNode *, SHAMapNodeID > resumes_
SHAMapSyncFilter * filter_
std::vector< DeferredNode > finishedReads_