20#include <xrpld/shamap/SHAMap.h>
21#include <xrpld/shamap/SHAMapLeafNode.h>
22#include <xrpld/shamap/SHAMapSyncFilter.h>
24#include <xrpl/basics/random.h>
30 std::function<
void(boost::intrusive_ptr<SHAMapItem const>
const&
31 item)>
const& leafFunction)
const
48 if (!
root_->isInner())
54 auto node = intr_ptr::static_pointer_cast<SHAMapInnerNode>(
root_);
61 if (!node->isEmptyBranch(pos))
65 if (!function(*child))
73 while ((pos != 15) && (node->isEmptyBranch(pos + 1)))
84 intr_ptr::static_pointer_cast<SHAMapInnerNode>(child);
112 if (
root_->getHash().isZero())
115 if (have && (
root_->getHash() == have->
root_->getHash()))
120 auto leaf = intr_ptr::static_pointer_cast<SHAMapLeafNode>(
root_);
122 !have->
hasLeafNode(leaf->peekItem()->key(), leaf->getHash()))
132 while (!stack.
empty())
134 auto const [node, nodeID] = stack.
top();
138 if (!function(*node))
142 for (
int i = 0; i < 16; ++i)
144 if (!node->isEmptyBranch(i))
146 auto const& childHash = node->getChildHash(i);
162 if (!function(*next))
183 while (currentChild < 16)
185 int branch = (firstChild + currentChild++) % 16;
206 [node, nodeID, branch, &mn](
210 std::unique_lock<std::mutex> lock{mn.deferLock_};
212 node, nodeID, branch, std::move(found));
254 node->setFullBelowGen(mn.generation_);
257 f_.getFullBelowCache()->insert(node->getHash().as_uint256());
291 auto const& nodeHash = parent->getChildHash(branch);
295 nodePtr = parent->canonicalizeChild(branch, std::move(nodePtr));
304 parentID.getChildNodeID(branch), nodeHash.as_uint256());
322 root_->getHash().isNonZero(),
323 "ripple::SHAMap::getMissingNodes : nonzero root hash");
324 XRPL_ASSERT(max > 0,
"ripple::SHAMap::getMissingNodes : valid max input");
330 f_.getFullBelowCache()->getGeneration());
332 if (!root_->isInner() ||
333 intr_ptr::static_pointer_cast<SHAMapInnerNode>(root_)->isFullBelow(
361 gmn_ProcessNodes(mn, pos);
366 if ((node ==
nullptr) && !mn.
stack_.empty())
369 bool was = fullBelow;
381 fullBelow = fullBelow && was;
385 "ripple::SHAMap::getMissingNodes : first non-null node");
392 gmn_ProcessDeferredReads(mn);
418 "ripple::SHAMap::getMissingNodes : second non-null node");
426 }
while (node !=
nullptr);
444 auto node = root_.get();
451 if (inner->isEmptyBranch(branch))
453 node = descendThrow(inner, branch);
457 if (node ==
nullptr || wanted != nodeID)
459 JLOG(journal_.info())
460 <<
"peer requested node that is not in the map: " << wanted
461 <<
" but found " << nodeID;
467 JLOG(journal_.warn()) <<
"peer requests empty node";
472 stack.
emplace(node, nodeID, depth);
476 while (!stack.
empty())
483 node->serializeForWire(s);
493 if ((depth > 0) || (bc == 1))
496 for (
int i = 0; i < 16; ++i)
498 if (!inner->isEmptyBranch(i))
500 auto const childNode = descendThrow(inner, i);
503 if (childNode->isInner() && ((depth > 1) || (bc == 1)))
510 (bc > 1) ? (depth - 1) : depth);
512 else if (childNode->isInner() || fatLeaves)
516 childNode->serializeForWire(s);
532 root_->serializeForWire(s);
538 Slice const& rootNode,
542 if (root_->getHash().isNonZero())
544 JLOG(journal_.trace()) <<
"got root node, already have one";
546 root_->getHash() == hash,
547 "ripple::SHAMap::addRootNode : valid hash input");
548 return SHAMapAddNode::duplicate();
551 XRPL_ASSERT(cowid_ >= 1,
"ripple::SHAMap::addRootNode : valid cowid");
552 auto node = SHAMapTreeNode::makeFromWire(rootNode);
553 if (!node || node->getHash() != hash)
554 return SHAMapAddNode::invalid();
557 canonicalize(hash, node);
567 root_->serializeWithPrefix(s);
576 return SHAMapAddNode::useful();
582 Slice const& rawNode,
586 !node.
isRoot(),
"ripple::SHAMap::addKnownNode : valid node input");
590 JLOG(journal_.trace()) <<
"AddKnownNode while not synching";
591 return SHAMapAddNode::duplicate();
594 auto const generation = f_.getFullBelowCache()->getGeneration();
596 auto currNode = root_.get();
598 while (currNode->isInner() &&
603 XRPL_ASSERT(branch >= 0,
"ripple::SHAMap::addKnownNode : valid branch");
605 if (inner->isEmptyBranch(branch))
607 JLOG(journal_.warn()) <<
"Add known node for empty branch" << node;
608 return SHAMapAddNode::invalid();
611 auto childHash = inner->getChildHash(branch);
612 if (f_.getFullBelowCache()->touch_if_exists(childHash.as_uint256()))
614 return SHAMapAddNode::duplicate();
617 auto prevNode = inner;
619 descend(inner, currNodeID, branch, filter);
621 if (currNode !=
nullptr)
624 auto newNode = SHAMapTreeNode::makeFromWire(rawNode);
626 if (!newNode || childHash != newNode->getHash())
628 JLOG(journal_.warn()) <<
"Corrupt node received";
629 return SHAMapAddNode::invalid();
637 if (newNode->isLeaf())
639 auto const& actualKey =
645 auto const expectedNodeID =
646 SHAMapNodeID::createID(node.
getDepth(), actualKey);
647 if (expectedNodeID.getNodeID() != node.
getNodeID())
649 JLOG(journal_.debug())
650 <<
"Leaf node position mismatch: "
651 <<
"expected=" << expectedNodeID.getNodeID()
653 return SHAMapAddNode::invalid();
660 if ((currNodeID.
getDepth() > leafDepth) ||
661 (newNode->isInner() && currNodeID.
getDepth() == leafDepth))
664 state_ = SHAMapState::Invalid;
665 return SHAMapAddNode::useful();
668 if (currNodeID != node)
671 JLOG(journal_.warn()) <<
"unable to hook node " << node;
672 JLOG(journal_.info()) <<
" stuck at " << currNodeID;
673 JLOG(journal_.info()) <<
"got depth=" << node.
getDepth()
674 <<
", walked to= " << currNodeID.
getDepth();
675 return SHAMapAddNode::useful();
679 canonicalize(childHash, newNode);
681 newNode = prevNode->canonicalizeChild(branch, std::move(newNode));
686 newNode->serializeWithPrefix(s);
695 return SHAMapAddNode::useful();
698 JLOG(journal_.trace()) <<
"got node, already had it (late)";
699 return SHAMapAddNode::duplicate();
708 stack.
push({root_.get(), other.
root_.get()});
710 while (!stack.
empty())
712 auto const [node, otherNode] = stack.
top();
715 if (!node || !otherNode)
717 JLOG(journal_.info()) <<
"unable to fetch node";
720 else if (otherNode->getHash() != node->getHash())
722 JLOG(journal_.warn()) <<
"node hash mismatch";
728 if (!otherNode->isLeaf())
731 auto& otherNodePeek =
733 if (nodePeek->key() != otherNodePeek->key())
735 if (nodePeek->slice() != otherNodePeek->slice())
738 else if (node->isInner())
740 if (!otherNode->isInner())
744 for (
int i = 0; i < 16; ++i)
746 if (node_inner->isEmptyBranch(i))
748 if (!other_inner->isEmptyBranch(i))
753 if (other_inner->isEmptyBranch(i))
756 auto next = descend(node_inner, i);
757 auto otherNext = other.
descend(other_inner, i);
758 if (!next || !otherNext)
760 JLOG(journal_.warn()) <<
"unable to fetch inner node";
763 stack.
push({next, otherNext});
779 auto node = root_.get();
786 if (inner->isEmptyBranch(branch))
789 node = descendThrow(inner, branch);
793 return (node->isInner()) && (node->getHash() == targetNodeHash);
801 auto node = root_.get();
804 if (!node->isInner())
805 return node->getHash() == targetNodeHash;
811 if (inner->isEmptyBranch(branch))
814 if (inner->getChildHash(branch) ==
818 node = descendThrow(inner, branch);
820 }
while (node->isInner());
830 walkTowardsKey(key, &stack);
834 JLOG(journal_.debug()) <<
"no path to " << key;
838 if (
auto const& node = stack.
top().
first; !node || node->isInner() ||
839 intr_ptr::static_pointer_cast<SHAMapLeafNode>(node)
843 JLOG(journal_.debug()) <<
"no path to " << key;
848 path.reserve(stack.
size());
849 while (!stack.
empty())
852 stack.
top().
first->serializeForWire(s);
853 path.emplace_back(std::move(s.
modData()));
857 JLOG(journal_.debug()) <<
"getPath for key " << key <<
", path length "
863SHAMap::verifyProofPath(
868 if (path.empty() || path.size() > 65)
874 for (
auto rit = path.rbegin(); rit != path.rend(); ++rit)
876 auto const& blob = *rit;
877 auto node = SHAMapTreeNode::makeFromWire(
makeSlice(blob));
881 if (node->getHash() != hash)
887 auto nodeId = SHAMapNodeID::createID(depth, key);
894 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_