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 <xrpld/shamap/SHAMapItem.h>
24#include <xrpld/shamap/SHAMapNodeID.h>
25
26#include <xrpl/basics/IntrusivePointer.h>
27#include <xrpl/basics/IntrusiveRefCounts.h>
28#include <xrpl/basics/SHAMapHash.h>
29#include <xrpl/protocol/Serializer.h>
30
31#include <cstdint>
32#include <string>
33
34namespace ripple {
35
36// These are wire-protocol identifiers used during serialization to encode the
37// type of a node. They should not be arbitrarily be changed.
38static constexpr unsigned char const wireTypeTransaction = 0;
39static constexpr unsigned char const wireTypeAccountState = 1;
40static constexpr unsigned char const wireTypeInner = 2;
41static constexpr unsigned char const wireTypeCompressedInner = 3;
42static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
43
44enum class SHAMapNodeType {
45 tnINNER = 1,
46 tnTRANSACTION_NM = 2, // transaction, no metadata
47 tnTRANSACTION_MD = 3, // transaction, with metadata
49};
50
52{
53protected:
55
63
64protected:
67 operator=(SHAMapTreeNode const&) = delete;
68
76 {
77 }
78
81 SHAMapHash const& hash) noexcept
82 : hash_(hash), cowid_(cowid)
83 {
84 }
87public:
88 virtual ~SHAMapTreeNode() noexcept = default;
89
90 // Needed to support weak intrusive pointers
91 virtual void
93
118 cowid() const
119 {
120 return cowid_;
121 }
122
128 void
130 {
131 cowid_ = 0;
132 }
133
140 virtual void
142
144 SHAMapHash const&
145 getHash() const
146 {
147 return hash_;
148 }
149
151 virtual SHAMapNodeType
152 getType() const = 0;
153
155 virtual bool
156 isLeaf() const = 0;
157
159 virtual bool
160 isInner() const = 0;
161
163 virtual void
165
167 virtual void
169
170 virtual std::string
171 getString(SHAMapNodeID const&) const;
172
173 virtual void
174 invariants(bool is_root = false) const = 0;
175
177 makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
178
180 makeFromWire(Slice rawNode);
181
182private:
184 makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
185
187 makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
188
190 makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
191};
192
193} // namespace ripple
194
195#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.