20#include <xrpl/basics/random.h>
21#include <xrpl/shamap/SHAMap.h>
22#include <xrpl/shamap/SHAMapLeafNode.h>
23#include <xrpl/shamap/SHAMapSyncFilter.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 currNode = root_.get();
597 while (currNode->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;
618 descend(inner, currNodeID, branch, filter);
620 if (currNode !=
nullptr)
623 auto newNode = SHAMapTreeNode::makeFromWire(rawNode);
625 if (!newNode || childHash != newNode->getHash())
627 JLOG(journal_.warn()) <<
"Corrupt node received";
628 return SHAMapAddNode::invalid();
636 if (newNode->isLeaf())
638 auto const& actualKey =
644 auto const expectedNodeID =
645 SHAMapNodeID::createID(node.
getDepth(), actualKey);
646 if (expectedNodeID.getNodeID() != node.
getNodeID())
648 JLOG(journal_.debug())
649 <<
"Leaf node position mismatch: "
650 <<
"expected=" << expectedNodeID.getNodeID()
652 return SHAMapAddNode::invalid();
659 if ((currNodeID.
getDepth() > leafDepth) ||
660 (newNode->isInner() && currNodeID.
getDepth() == leafDepth))
663 state_ = SHAMapState::Invalid;
664 return SHAMapAddNode::useful();
667 if (currNodeID != node)
670 JLOG(journal_.warn()) <<
"unable to hook node " << node;
671 JLOG(journal_.info()) <<
" stuck at " << currNodeID;
672 JLOG(journal_.info()) <<
"got depth=" << node.
getDepth()
673 <<
", walked to= " << currNodeID.
getDepth();
674 return SHAMapAddNode::useful();
678 canonicalize(childHash, newNode);
680 newNode = prevNode->canonicalizeChild(branch, std::move(newNode));
685 newNode->serializeWithPrefix(s);
694 return SHAMapAddNode::useful();
697 JLOG(journal_.trace()) <<
"got node, already had it (late)";
698 return SHAMapAddNode::duplicate();
707 stack.
push({root_.get(), other.
root_.get()});
709 while (!stack.
empty())
711 auto const [node, otherNode] = stack.
top();
714 if (!node || !otherNode)
716 JLOG(journal_.info()) <<
"unable to fetch node";
719 else if (otherNode->getHash() != node->getHash())
721 JLOG(journal_.warn()) <<
"node hash mismatch";
727 if (!otherNode->isLeaf())
730 auto& otherNodePeek =
732 if (nodePeek->key() != otherNodePeek->key())
734 if (nodePeek->slice() != otherNodePeek->slice())
737 else if (node->isInner())
739 if (!otherNode->isInner())
743 for (
int i = 0; i < 16; ++i)
745 if (node_inner->isEmptyBranch(i))
747 if (!other_inner->isEmptyBranch(i))
752 if (other_inner->isEmptyBranch(i))
755 auto next = descend(node_inner, i);
756 auto otherNext = other.
descend(other_inner, i);
757 if (!next || !otherNext)
759 JLOG(journal_.warn()) <<
"unable to fetch inner node";
762 stack.
push({next, otherNext});
778 auto node = root_.get();
785 if (inner->isEmptyBranch(branch))
788 node = descendThrow(inner, branch);
792 return (node->isInner()) && (node->getHash() == targetNodeHash);
800 auto node = root_.get();
803 if (!node->isInner())
804 return node->getHash() == targetNodeHash;
810 if (inner->isEmptyBranch(branch))
813 if (inner->getChildHash(branch) ==
817 node = descendThrow(inner, branch);
819 }
while (node->isInner());
829 walkTowardsKey(key, &stack);
833 JLOG(journal_.debug()) <<
"no path to " << key;
837 if (
auto const& node = stack.
top().
first; !node || node->isInner() ||
838 intr_ptr::static_pointer_cast<SHAMapLeafNode>(node)
842 JLOG(journal_.debug()) <<
"no path to " << key;
847 path.reserve(stack.
size());
848 while (!stack.
empty())
851 stack.
top().
first->serializeForWire(s);
852 path.emplace_back(std::move(s.
modData()));
856 JLOG(journal_.debug()) <<
"getPath for key " << key <<
", path length "
862SHAMap::verifyProofPath(
867 if (path.empty() || path.size() > 65)
873 for (
auto rit = path.rbegin(); rit != path.rend(); ++rit)
875 auto const& blob = *rit;
876 auto node = SHAMapTreeNode::makeFromWire(
makeSlice(blob));
880 if (node->getHash() != hash)
886 auto nodeId = SHAMapNodeID::createID(depth, key);
893 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()
@ 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)
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::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_