rippled
Loading...
Searching...
No Matches
SHAMapTreeNode.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_SHAMAP_SHAMAPTREENODE_H_INCLUDED
21#define RIPPLE_SHAMAP_SHAMAPTREENODE_H_INCLUDED
22
23#include <xrpl/basics/IntrusivePointer.h>
24#include <xrpl/basics/IntrusiveRefCounts.h>
25#include <xrpl/basics/SHAMapHash.h>
26#include <xrpl/protocol/Serializer.h>
27#include <xrpl/shamap/SHAMapItem.h>
28#include <xrpl/shamap/SHAMapNodeID.h>
29
30#include <cstdint>
31#include <string>
32
33namespace ripple {
34
35// These are wire-protocol identifiers used during serialization to encode the
36// type of a node. They should not be arbitrarily be changed.
37static constexpr unsigned char const wireTypeTransaction = 0;
38static constexpr unsigned char const wireTypeAccountState = 1;
39static constexpr unsigned char const wireTypeInner = 2;
40static constexpr unsigned char const wireTypeCompressedInner = 3;
41static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
42
43enum class SHAMapNodeType {
44 tnINNER = 1,
45 tnTRANSACTION_NM = 2, // transaction, no metadata
46 tnTRANSACTION_MD = 3, // transaction, with metadata
48};
49
51{
52protected:
54
62
63protected:
66 operator=(SHAMapTreeNode const&) = delete;
67
75 {
76 }
77
80 SHAMapHash const& hash) noexcept
81 : hash_(hash), cowid_(cowid)
82 {
83 }
86public:
87 virtual ~SHAMapTreeNode() noexcept = default;
88
89 // Needed to support weak intrusive pointers
90 virtual void
92
117 cowid() const
118 {
119 return cowid_;
120 }
121
127 void
129 {
130 cowid_ = 0;
131 }
132
139 virtual void
141
143 SHAMapHash const&
144 getHash() const
145 {
146 return hash_;
147 }
148
150 virtual SHAMapNodeType
151 getType() const = 0;
152
154 virtual bool
155 isLeaf() const = 0;
156
158 virtual bool
159 isInner() const = 0;
160
162 virtual void
164
166 virtual void
168
169 virtual std::string
170 getString(SHAMapNodeID const&) const;
171
172 virtual void
173 invariants(bool is_root = false) const = 0;
174
176 makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
177
179 makeFromWire(Slice rawNode);
180
181private:
183 makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
184
186 makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
187
189 makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
190};
191
192} // namespace ripple
193
194#endif
Identifies a node inside a SHAMap.
SHAMapTreeNode & operator=(SHAMapTreeNode const &)=delete
static intr_ptr::SharedPtr< SHAMapTreeNode > makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
virtual ~SHAMapTreeNode() noexcept=default
virtual std::string getString(SHAMapNodeID const &) const
virtual bool isLeaf() const =0
Determines if this is a leaf node.
static intr_ptr::SharedPtr< SHAMapTreeNode > makeTransaction(Slice data, SHAMapHash const &hash, bool hashValid)
SHAMapTreeNode(SHAMapTreeNode const &)=delete
virtual void partialDestructor()
static intr_ptr::SharedPtr< SHAMapTreeNode > makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
virtual void updateHash()=0
Recalculate the hash of this node.
SHAMapTreeNode(std::uint32_t cowid) noexcept
Construct a node.
virtual bool isInner() const =0
Determines if this is an inner node.
std::uint32_t cowid_
Determines the owning SHAMap, if any.
virtual void invariants(bool is_root=false) const =0
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
SHAMapTreeNode(std::uint32_t cowid, SHAMapHash const &hash) noexcept
SHAMapHash const & getHash() const
Return the hash of this node.
virtual void serializeForWire(Serializer &) const =0
Serialize the node in a format appropriate for sending over the wire.
virtual void serializeWithPrefix(Serializer &) const =0
Serialize the node in a format appropriate for hashing.
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFromWire(Slice rawNode)
virtual SHAMapNodeType getType() const =0
Determines the type of node.
A shared intrusive pointer class that supports weak pointers.
An immutable linear range of bytes.
Definition Slice.h:46
void unshare()
If this node is shared with another map, mark it as no longer shared.
virtual intr_ptr::SharedPtr< SHAMapTreeNode > clone(std::uint32_t cowid) const =0
Make a copy of this node, setting the owner.
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
static constexpr unsigned char const wireTypeAccountState
static constexpr unsigned char const wireTypeCompressedInner
static constexpr unsigned char const wireTypeInner
static constexpr unsigned char const wireTypeTransaction
static constexpr unsigned char const wireTypeTransactionWithMeta
Implement the strong count, weak count, and bit flags for an intrusive pointer.