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/basics/Slice.h>
7#include <xrpl/protocol/Serializer.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
50
59 {
60 }
61
62 explicit SHAMapTreeNode(std::uint32_t cowid, SHAMapHash const& hash) noexcept
63 : hash_(hash), cowid_(cowid)
64 {
65 }
66
67
68public:
69 ~SHAMapTreeNode() noexcept override = default;
70
73 operator=(SHAMapTreeNode const&) = delete;
74
75 // Needed to support weak intrusive pointers
76 virtual void
78
105 cowid() const
106 {
107 return cowid_;
108 }
109
116 void
118 {
119 cowid_ = 0;
120 }
121
125 virtual SHAMapTreeNodePtr
128
132 virtual void
134
138 SHAMapHash const&
139 getHash() const
140 {
141 return hash_;
142 }
143
147 virtual SHAMapNodeType
148 getType() const = 0;
149
153 virtual bool
154 isLeaf() const = 0;
155
159 virtual bool
160 isInner() const = 0;
161
165 virtual void
167
171 virtual void
173
174 virtual std::string
175 getString(SHAMapNodeID const&) const;
176
177 virtual void
178 invariants(bool isRoot = false) const = 0;
179
180 static SHAMapTreeNodePtr
181 makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
182
183 static SHAMapTreeNodePtr
184 makeFromWire(Slice rawNode);
185
186private:
187 static SHAMapTreeNodePtr
188 makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
189
190 static SHAMapTreeNodePtr
191 makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
192
193 static SHAMapTreeNodePtr
194 makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
195};
196
197} // 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:28
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.