20#include <xrpld/shamap/SHAMap.h>
21#include <xrpld/shamap/SHAMapSyncFilter.h>
23#include <xrpl/basics/random.h>
29 std::function<
void(boost::intrusive_ptr<SHAMapItem const>
const&
30 item)>
const& leafFunction)
const
47 if (!
root_->isInner())
53 auto node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
root_);
60 if (!node->isEmptyBranch(pos))
64 if (!function(*child))
72 while ((pos != 15) && (node->isEmptyBranch(pos + 1)))
83 intr_ptr::static_pointer_cast<SHAMapInnerNode>(child);
111 if (
root_->getHash().isZero())
114 if (have && (
root_->getHash() == have->
root_->getHash()))
119 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(
root_);
121 !have->
hasLeafNode(leaf->peekItem()->key(), leaf->getHash()))
131 while (!stack.
empty())
133 auto const [node, nodeID] = stack.
top();
137 if (!function(*node))
141 for (
int i = 0; i < 16; ++i)
143 if (!node->isEmptyBranch(i))
145 auto const& childHash = node->getChildHash(i);
161 if (!function(*next))
182 while (currentChild < 16)
184 int branch = (firstChild + currentChild++) % 16;
205 [node, nodeID, branch, &mn](
209 std::unique_lock<std::mutex> lock{mn.deferLock_};
211 node, nodeID, branch, std::move(found));
253 node->setFullBelowGen(mn.generation_);
256 f_.getFullBelowCache()->insert(node->getHash().as_uint256());
290 auto const& nodeHash = parent->getChildHash(branch);
294 nodePtr = parent->canonicalizeChild(branch, std::move(nodePtr));
303 parentID.getChildNodeID(branch), nodeHash.as_uint256());
321 root_->getHash().isNonZero(),
322 "ripple::SHAMap::getMissingNodes : nonzero root hash");
323 XRPL_ASSERT(max > 0,
"ripple::SHAMap::getMissingNodes : valid max input");
329 f_.getFullBelowCache()->getGeneration());
331 if (!root_->isInner() ||
332 intr_ptr::static_pointer_cast<SHAMapInnerNode>(root_)->isFullBelow(
360 gmn_ProcessNodes(mn, pos);
365 if ((node ==
nullptr) && !mn.
stack_.empty())
368 bool was = fullBelow;
380 fullBelow = fullBelow && was;
384 "ripple::SHAMap::getMissingNodes : first non-null node");
391 gmn_ProcessDeferredReads(mn);
417 "ripple::SHAMap::getMissingNodes : second non-null node");
425 }
while (node !=
nullptr);
443 auto node = root_.get();
450 if (inner->isEmptyBranch(branch))
452 node = descendThrow(inner, branch);
456 if (node ==
nullptr || wanted != nodeID)
458 JLOG(journal_.info())
459 <<
"peer requested node that is not in the map: " << wanted
460 <<
" but found " << nodeID;
466 JLOG(journal_.warn()) <<
"peer requests empty node";
471 stack.
emplace(node, nodeID, depth);
475 while (!stack.
empty())
482 node->serializeForWire(s);
492 if ((depth > 0) || (bc == 1))
495 for (
int i = 0; i < 16; ++i)
497 if (!inner->isEmptyBranch(i))
499 auto const childNode = descendThrow(inner, i);
502 if (childNode->isInner() && ((depth > 1) || (bc == 1)))
509 (bc > 1) ? (depth - 1) : depth);
511 else if (childNode->isInner() || fatLeaves)
515 childNode->serializeForWire(s);
531 root_->serializeForWire(s);
537 Slice const& rootNode,
541 if (root_->getHash().isNonZero())
543 JLOG(journal_.trace()) <<
"got root node, already have one";
545 root_->getHash() == hash,
546 "ripple::SHAMap::addRootNode : valid hash input");
547 return SHAMapAddNode::duplicate();
550 XRPL_ASSERT(cowid_ >= 1,
"ripple::SHAMap::addRootNode : valid cowid");
551 auto node = SHAMapTreeNode::makeFromWire(rootNode);
552 if (!node || node->getHash() != hash)
553 return SHAMapAddNode::invalid();
556 canonicalize(hash, node);
566 root_->serializeWithPrefix(s);
575 return SHAMapAddNode::useful();
581 Slice const& rawNode,
585 !node.
isRoot(),
"ripple::SHAMap::addKnownNode : valid node input");
589 JLOG(journal_.trace()) <<
"AddKnownNode while not synching";
590 return SHAMapAddNode::duplicate();
593 auto const generation = f_.getFullBelowCache()->getGeneration();
595 auto iNode = root_.get();
597 while (iNode->isInner() &&
602 XRPL_ASSERT(branch >= 0,
"ripple::SHAMap::addKnownNode : valid branch");
604 if (inner->isEmptyBranch(branch))
606 JLOG(journal_.warn()) <<
"Add known node for empty branch" << node;
607 return SHAMapAddNode::invalid();
610 auto childHash = inner->getChildHash(branch);
611 if (f_.getFullBelowCache()->touch_if_exists(childHash.as_uint256()))
613 return SHAMapAddNode::duplicate();
616 auto prevNode = inner;
617 std::tie(iNode, iNodeID) = descend(inner, iNodeID, branch, filter);
619 if (iNode ==
nullptr)
621 auto newNode = SHAMapTreeNode::makeFromWire(rawNode);
623 if (!newNode || childHash != newNode->getHash())
625 JLOG(journal_.warn()) <<
"Corrupt node received";
626 return SHAMapAddNode::invalid();
632 if ((iNodeID.
getDepth() > leafDepth) ||
633 (newNode->isInner() && iNodeID.
getDepth() == leafDepth))
636 state_ = SHAMapState::Invalid;
637 return SHAMapAddNode::useful();
643 JLOG(journal_.warn()) <<
"unable to hook node " << node;
644 JLOG(journal_.info()) <<
" stuck at " << iNodeID;
645 JLOG(journal_.info()) <<
"got depth=" << node.
getDepth()
646 <<
", walked to= " << iNodeID.
getDepth();
647 return SHAMapAddNode::useful();
651 canonicalize(childHash, newNode);
653 newNode = prevNode->canonicalizeChild(branch, std::move(newNode));
658 newNode->serializeWithPrefix(s);
667 return SHAMapAddNode::useful();
671 JLOG(journal_.trace()) <<
"got node, already had it (late)";
672 return SHAMapAddNode::duplicate();
681 stack.
push({root_.get(), other.
root_.get()});
683 while (!stack.
empty())
685 auto const [node, otherNode] = stack.
top();
688 if (!node || !otherNode)
690 JLOG(journal_.info()) <<
"unable to fetch node";
693 else if (otherNode->getHash() != node->getHash())
695 JLOG(journal_.warn()) <<
"node hash mismatch";
701 if (!otherNode->isLeaf())
704 auto& otherNodePeek =
706 if (nodePeek->key() != otherNodePeek->key())
708 if (nodePeek->slice() != otherNodePeek->slice())
711 else if (node->isInner())
713 if (!otherNode->isInner())
717 for (
int i = 0; i < 16; ++i)
719 if (node_inner->isEmptyBranch(i))
721 if (!other_inner->isEmptyBranch(i))
726 if (other_inner->isEmptyBranch(i))
729 auto next = descend(node_inner, i);
730 auto otherNext = other.
descend(other_inner, i);
731 if (!next || !otherNext)
733 JLOG(journal_.warn()) <<
"unable to fetch inner node";
736 stack.
push({next, otherNext});
752 auto node = root_.get();
759 if (inner->isEmptyBranch(branch))
762 node = descendThrow(inner, branch);
766 return (node->isInner()) && (node->getHash() == targetNodeHash);
774 auto node = root_.get();
777 if (!node->isInner())
778 return node->getHash() == targetNodeHash;
784 if (inner->isEmptyBranch(branch))
787 if (inner->getChildHash(branch) ==
791 node = descendThrow(inner, branch);
793 }
while (node->isInner());
803 walkTowardsKey(key, &stack);
807 JLOG(journal_.debug()) <<
"no path to " << key;
811 if (
auto const& node = stack.
top().
first; !node || node->isInner() ||
812 intr_ptr::static_pointer_cast<SHAMapLeafNode>(node)
816 JLOG(journal_.debug()) <<
"no path to " << key;
821 path.reserve(stack.
size());
822 while (!stack.
empty())
825 stack.
top().
first->serializeForWire(s);
826 path.emplace_back(std::move(s.
modData()));
830 JLOG(journal_.debug()) <<
"getPath for key " << key <<
", path length "
836SHAMap::verifyProofPath(
841 if (path.empty() || path.size() > 65)
847 for (
auto rit = path.rbegin(); rit != path.rend(); ++rit)
849 auto const& blob = *rit;
850 auto node = SHAMapTreeNode::makeFromWire(
makeSlice(blob));
854 if (node->getHash() != hash)
860 auto nodeId = SHAMapNodeID::createID(depth, key);
867 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
intr_ptr::SharedPtr< SHAMapTreeNode > root_
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
SHAMapTreeNode * descend(SHAMapInnerNode *, int branch) const
bool hasInnerNode(SHAMapNodeID const &nodeID, SHAMapHash const &hash) const
Does this map have this inner node?
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?
intr_ptr::SharedPtr< SHAMapTreeNode > descendNoStore(SHAMapInnerNode &, int branch) 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.
void visitLeaves(std::function< void(boost::intrusive_ptr< SHAMapItem const > const &)> const &) const
Visit every leaf node in this SHAMap.
A shared intrusive pointer class that supports weak pointers.
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_