1#ifndef XRPL_SHAMAP_SHAMAP_H_INCLUDED
2#define XRPL_SHAMAP_SHAMAP_H_INCLUDED
4#include <xrpl/basics/IntrusivePointer.h>
5#include <xrpl/basics/UnorderedContainers.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/nodestore/Database.h>
9#include <xrpl/nodestore/NodeObject.h>
10#include <xrpl/shamap/Family.h>
11#include <xrpl/shamap/SHAMapAddNode.h>
12#include <xrpl/shamap/SHAMapInnerNode.h>
13#include <xrpl/shamap/SHAMapItem.h>
14#include <xrpl/shamap/SHAMapLeafNode.h>
15#include <xrpl/shamap/SHAMapMissingNode.h>
16#include <xrpl/shamap/SHAMapTreeNode.h>
25class SHAMapSyncFilter;
105 boost::intrusive_ptr<SHAMapItem const>,
106 boost::intrusive_ptr<SHAMapItem const>>;
188 boost::intrusive_ptr<SHAMapItem const> item);
193 boost::intrusive_ptr<SHAMapItem const> item);
197 boost::intrusive_ptr<SHAMapItem const>
const&
199 boost::intrusive_ptr<SHAMapItem const>
const&
247 void(boost::intrusive_ptr<SHAMapItem const>
const&)>
const&)
const;
300 Slice const& rootNode,
305 Slice const& rawNode,
323 compare(
SHAMap const& otherMap,
Delta& differences,
int maxCount)
const;
338 int maxMissing)
const;
346 dump(
bool withHashes =
false)
const;
354 boost::intrusive_ptr<SHAMapItem const>,
355 boost::intrusive_ptr<SHAMapItem const>>;
395 template <
class Node>
400 template <
class Node>
413 int branch = 0)
const;
469 boost::intrusive_ptr<SHAMapItem const>
const&
484 boost::intrusive_ptr<SHAMapItem const>
const& otherMapItem,
487 int& maxCount)
const;
587 "ripple::SHAMap::setImmutable : state is valid");
670 "ripple::SHAMap::const_iterator::const_iterator : non-null input");
673 item_ = temp->peekItem().get();
685 : stack_(
std::move(stack)), map_(map), item_(item)
704 if (
auto temp = map_->peekNextItem(item_->key(), stack_))
705 item_ = temp->peekItem().get();
724 "ripple::operator==(SHAMap::const_iterator, SHAMap::const_iterator) : "
725 "inputs map do match");
735inline SHAMap::const_iterator
A generic endpoint for log messages.
const_iterator(ReadView const &view, Keylet const &root, Keylet const &page)
reference operator*() const
value_type const & reference
const_iterator & operator++()
value_type const * pointer
pointer operator->() const
static constexpr unsigned int branchFactor
Each inner node has 16 children (the 'radix tree' part of the map)
Identifies a node inside a SHAMap.
value_type const * pointer
SharedPtrNodeStack stack_
const_iterator(const_iterator const &other)=default
const_iterator & operator++()
reference operator*() const
value_type const & reference
~const_iterator()=default
friend bool operator==(const_iterator const &x, const_iterator const &y)
const_iterator & operator=(const_iterator const &other)=default
pointer operator->() const
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
intr_ptr::SharedPtr< Node > preFlushNode(intr_ptr::SharedPtr< Node > node) const
prepare a node to be modified before flushing
SHAMapTreeNode * descendAsync(SHAMapInnerNode *parent, int branch, SHAMapSyncFilter *filter, bool &pending, descendCallback &&) const
bool hasItem(uint256 const &id) const
Does the tree have an item with the given ID?
intr_ptr::SharedPtr< SHAMapTreeNode > cacheLookup(SHAMapHash const &hash) const
intr_ptr::SharedPtr< Node > unshareNode(intr_ptr::SharedPtr< Node >, SHAMapNodeID const &nodeID)
Unshare the node, allowing it to be modified.
static constexpr unsigned int leafDepth
The depth of the hash map: data is only present in the leaves.
void dump(bool withHashes=false) const
boost::intrusive_ptr< SHAMapItem const > const & onlyBelow(SHAMapTreeNode *) const
If there is only one leaf below this node, get its contents.
void gmn_ProcessNodes(MissingNodes &, MissingNodes::StackEntry &node)
SHAMapTreeNode * descendThrow(SHAMapInnerNode *, int branch) const
std::map< uint256, DeltaItem > Delta
intr_ptr::SharedPtr< SHAMapTreeNode > root_
boost::intrusive_ptr< SHAMapItem const > const & peekItem(uint256 const &id) const
Family const & family() const
bool getNodeFat(SHAMapNodeID const &wanted, std::vector< std::pair< SHAMapNodeID, Blob > > &data, bool fatLeaves, std::uint32_t depth) const
intr_ptr::SharedPtr< SHAMapTreeNode > finishFetch(SHAMapHash const &hash, std::shared_ptr< NodeObject > const &object) const
bool addGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
SHAMapLeafNode * walkTowardsKey(uint256 const &id, SharedPtrNodeStack *stack=nullptr) const
Walk towards the specified id, returning the node.
SHAMapTreeNode * descend(SHAMapInnerNode *, int branch) const
SHAMapLeafNode const * peekNextItem(uint256 const &id, SharedPtrNodeStack &stack) const
SHAMapAddNode addKnownNode(SHAMapNodeID const &nodeID, Slice const &rawNode, SHAMapSyncFilter *filter)
void canonicalize(SHAMapHash const &hash, intr_ptr::SharedPtr< SHAMapTreeNode > &) const
SHAMapAddNode addRootNode(SHAMapHash const &hash, Slice const &rootNode, SHAMapSyncFilter *filter)
int walkSubTree(bool doWrite, NodeObjectType t)
void setLedgerSeq(std::uint32_t lseq)
const_iterator end() const
void serializeRoot(Serializer &s) const
Serializes the root in a format appropriate for sending over the wire.
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
const_iterator upper_bound(uint256 const &id) const
Find the first item after the given item.
bool hasInnerNode(SHAMapNodeID const &nodeID, SHAMapHash const &hash) const
Does this map have this inner node?
intr_ptr::SharedPtr< SHAMapTreeNode > writeNode(NodeObjectType t, intr_ptr::SharedPtr< SHAMapTreeNode > node) const
write and canonicalize modified node
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNodeNT(SHAMapHash const &hash) const
void gmn_ProcessDeferredReads(MissingNodes &)
std::uint32_t cowid_
ID to distinguish this map for all others we're sharing nodes with.
SHAMapHash getHash() const
void walkMap(std::vector< SHAMapMissingNode > &missingNodes, int maxMissing) const
bool walkMapParallel(std::vector< SHAMapMissingNode > &missingNodes, int maxMissing) const
void dirtyUp(SharedPtrNodeStack &stack, uint256 const &target, intr_ptr::SharedPtr< SHAMapTreeNode > terminal)
Update hashes up to the root.
bool updateGiveItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
SHAMapLeafNode const * peekFirstItem(SharedPtrNodeStack &stack) const
std::vector< std::pair< SHAMapNodeID, uint256 > > getMissingNodes(int maxNodes, SHAMapSyncFilter *filter)
Check for nodes in the SHAMap not available.
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNode(SHAMapHash const &hash) 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?
intr_ptr::SharedPtr< SHAMapTreeNode > fetchNodeFromDB(SHAMapHash const &hash) const
SHAMap & operator=(SHAMap const &)=delete
SHAMap(SHAMap const &)=delete
const_iterator begin() const
intr_ptr::SharedPtr< SHAMapTreeNode > descendNoStore(SHAMapInnerNode &, int branch) const
std::uint32_t ledgerSeq_
The sequence of the ledger that this map references, if any.
bool delItem(uint256 const &id)
bool fetchRoot(SHAMapHash const &hash, SHAMapSyncFilter *filter)
const_iterator lower_bound(uint256 const &id) const
Find the object with the greatest object id smaller than the input id.
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.
SHAMapLeafNode * lastBelow(intr_ptr::SharedPtr< SHAMapTreeNode > node, SharedPtrNodeStack &stack, int branch=branchFactor) const
bool walkBranch(SHAMapTreeNode *node, boost::intrusive_ptr< SHAMapItem const > const &otherMapItem, bool isFirstMap, Delta &differences, int &maxCount) const
std::shared_ptr< SHAMap > snapShot(bool isMutable) const
int flushDirty(NodeObjectType t)
Flush modified nodes to the nodestore and convert them to shared.
int unshare()
Convert any modified nodes to shared.
intr_ptr::SharedPtr< SHAMapTreeNode > checkFilter(SHAMapHash const &hash, SHAMapSyncFilter *filter) const
bool deepCompare(SHAMap &other) const
static bool verifyProofPath(uint256 const &rootHash, uint256 const &key, std::vector< Blob > const &path)
Verify the proof path.
SHAMapLeafNode * belowHelper(intr_ptr::SharedPtr< SHAMapTreeNode > node, SharedPtrNodeStack &stack, int branch, std::tuple< int, std::function< bool(int)>, std::function< void(int &)> > const &loopParams) const
SHAMapLeafNode * findKey(uint256 const &id) const
Return nullptr if key not found.
static constexpr unsigned int branchFactor
Number of children each non-leaf node has (the 'radix tree' part of the map)
std::optional< std::vector< Blob > > getProofPath(uint256 const &key) const
Get the proof path of the key.
SHAMapLeafNode * firstBelow(intr_ptr::SharedPtr< SHAMapTreeNode >, SharedPtrNodeStack &stack, int branch=0) const
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.
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
SHAMapState
Describes the current state of a given SHAMap.
@ Immutable
The map is set in stone and cannot be changed.
@ Invalid
The map is known to not be valid.
@ Synching
The map's hash is fixed but valid nodes may be missing and can be added.
@ Modifying
The map is in flux and objects can be added and removed.
NodeObjectType
The types of node objects.
@ pending
List will be valid in the future.
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
std::set< SHAMapHash > missingHashes_
std::stack< StackEntry, std::deque< StackEntry > > stack_
std::condition_variable deferCondVar_
std::tuple< SHAMapInnerNode *, SHAMapNodeID, int, int, bool > StackEntry
MissingNodes(int max, SHAMapSyncFilter *filter, int maxDefer, std::uint32_t generation)
std::uint32_t generation_
std::vector< std::pair< SHAMapNodeID, uint256 > > missingNodes_
MissingNodes(MissingNodes const &)=delete
MissingNodes & operator=(MissingNodes const &)=delete
std::map< SHAMapInnerNode *, SHAMapNodeID > resumes_
SHAMapSyncFilter * filter_
std::vector< DeferredNode > finishedReads_