xrpld
Loading...
Searching...
No Matches
SHAMapTreeNode.h
1#pragma once
2
3#include <xrpl/basics/IntrusivePointer.h>
4#include <xrpl/basics/IntrusiveRefCounts.h>
5#include <xrpl/basics/SHAMapHash.h>
6#include <xrpl/protocol/Serializer.h>
7#include <xrpl/shamap/SHAMapItem.h>
8#include <xrpl/shamap/SHAMapNodeID.h>
9
10#include <cstddef>
11#include <cstdint>
12#include <string>
13
14namespace xrpl {
15
16class SHAMapTreeNode;
18
19// These are wire-protocol identifiers used during serialization to encode the
20// type of a node. They should not be arbitrarily be changed.
21static constexpr unsigned char const kWireTypeTransaction = 0;
22static constexpr unsigned char const kWireTypeAccountState = 1;
23static constexpr unsigned char const kWireTypeInner = 2;
24static constexpr unsigned char const kWireTypeCompressedInner = 3;
25static constexpr unsigned char const kWireTypeTransactionWithMeta = 4;
26
27// Lower bound on SHAMap leaf item payload size, in bytes.
28inline constexpr std::size_t kMinShaMapItemBytes = 12;
29
30enum class SHAMapNodeType {
32 TnTransactionNm = 2, // transaction, no metadata
33 TnTransactionMd = 3, // transaction, with metadata
35};
36
38{
39protected:
41
49
57 {
58 }
59
60 explicit SHAMapTreeNode(std::uint32_t cowid, SHAMapHash const& hash) noexcept
61 : hash_(hash), cowid_(cowid)
62 {
63 }
64
65
66public:
67 ~SHAMapTreeNode() noexcept override = default;
68
71 operator=(SHAMapTreeNode const&) = delete;
72
73 // Needed to support weak intrusive pointers
74 virtual void
76
101 cowid() const
102 {
103 return cowid_;
104 }
105
111 void
113 {
114 cowid_ = 0;
115 }
116
118 virtual SHAMapTreeNodePtr
121
123 virtual void
125
127 SHAMapHash const&
128 getHash() const
129 {
130 return hash_;
131 }
132
134 virtual SHAMapNodeType
135 getType() const = 0;
136
138 virtual bool
139 isLeaf() const = 0;
140
142 virtual bool
143 isInner() const = 0;
144
146 virtual void
148
150 virtual void
152
153 virtual std::string
154 getString(SHAMapNodeID const&) const;
155
156 virtual void
157 invariants(bool isRoot = false) const = 0;
158
159 static SHAMapTreeNodePtr
160 makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
161
162 static SHAMapTreeNodePtr
163 makeFromWire(Slice rawNode);
164
165private:
166 static SHAMapTreeNodePtr
167 makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
168
169 static SHAMapTreeNodePtr
170 makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
171
172 static SHAMapTreeNodePtr
173 makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
174};
175
176} // namespace xrpl
Identifies a node inside a SHAMap.
static SHAMapTreeNodePtr makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
static SHAMapTreeNodePtr makeFromWire(Slice rawNode)
static SHAMapTreeNodePtr makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
SHAMapHash const & getHash() const
Return the hash of this node.
virtual void serializeWithPrefix(Serializer &) const =0
Serialize the node in a format appropriate for hashing.
std::uint32_t cowid_
Determines the owning SHAMap, if any.
virtual void serializeForWire(Serializer &) const =0
Serialize the node in a format appropriate for sending over the wire.
virtual SHAMapNodeType getType() const =0
Determines the type of node.
virtual void partialDestructor()
virtual bool isLeaf() const =0
Determines if this is a leaf node.
static SHAMapTreeNodePtr makeTransaction(Slice data, SHAMapHash const &hash, bool hashValid)
virtual bool isInner() const =0
Determines if this is an inner node.
virtual void invariants(bool isRoot=false) const =0
SHAMapTreeNode(std::uint32_t cowid) noexcept
Construct a node.
~SHAMapTreeNode() noexcept override=default
SHAMapTreeNode(std::uint32_t cowid, SHAMapHash const &hash) noexcept
static SHAMapTreeNodePtr makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
virtual std::string getString(SHAMapNodeID const &) const
virtual void updateHash()=0
Recalculate the hash of this node.
An immutable linear range of bytes.
Definition Slice.h:26
virtual SHAMapTreeNodePtr clone(std::uint32_t cowid) const =0
Make a copy of this node, setting the owner.
void unshare()
If this node is shared with another map, mark it as no longer shared.
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
SharedIntrusive< T > SharedPtr
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
intr_ptr::SharedPtr< SHAMapTreeNode > SHAMapTreeNodePtr
static constexpr unsigned char const kWireTypeTransactionWithMeta
static constexpr unsigned char const kWireTypeCompressedInner
constexpr std::size_t kMinShaMapItemBytes
static constexpr unsigned char const kWireTypeAccountState
static constexpr unsigned char const kWireTypeTransaction
static constexpr unsigned char const kWireTypeInner
Implement the strong count, weak count, and bit flags for an intrusive pointer.