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#include <xrpl/basics/CountedObject.h>
26#include <xrpl/basics/SHAMapHash.h>
27#include <xrpl/basics/TaggedCache.h>
28#include <xrpl/beast/utility/Journal.h>
29#include <xrpl/protocol/Serializer.h>
30
31#include <cstdint>
32#include <memory>
33#include <mutex>
34#include <string>
35
36namespace ripple {
37
38// These are wire-protocol identifiers used during serialization to encode the
39// type of a node. They should not be arbitrarily be changed.
40static constexpr unsigned char const wireTypeTransaction = 0;
41static constexpr unsigned char const wireTypeAccountState = 1;
42static constexpr unsigned char const wireTypeInner = 2;
43static constexpr unsigned char const wireTypeCompressedInner = 3;
44static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
45
46enum class SHAMapNodeType {
47 tnINNER = 1,
48 tnTRANSACTION_NM = 2, // transaction, no metadata
49 tnTRANSACTION_MD = 3, // transaction, with metadata
51};
52
54{
55protected:
57
65
66protected:
69 operator=(SHAMapTreeNode const&) = delete;
70
78 {
79 }
80
83 SHAMapHash const& hash) noexcept
84 : hash_(hash), cowid_(cowid)
85 {
86 }
89public:
90 virtual ~SHAMapTreeNode() noexcept = default;
91
115 std::uint32_t
116 cowid() const
117 {
118 return cowid_;
119 }
120
126 void
128 {
129 cowid_ = 0;
130 }
131
138 virtual void
140
142 SHAMapHash const&
143 getHash() const
144 {
145 return hash_;
146 }
147
149 virtual SHAMapNodeType
150 getType() const = 0;
151
153 virtual bool
154 isLeaf() const = 0;
155
157 virtual bool
158 isInner() const = 0;
159
161 virtual void
163
165 virtual void
167
168 virtual std::string
169 getString(SHAMapNodeID const&) const;
170
171 virtual void
172 invariants(bool is_root = false) const = 0;
173
175 makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
176
178 makeFromWire(Slice rawNode);
179
180private:
182 makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
183
185 makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
186
188 makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
189};
190
191} // namespace ripple
192
193#endif
Identifies a node inside a SHAMap.
Definition: SHAMapNodeID.h:34
SHAMapTreeNode & operator=(SHAMapTreeNode const &)=delete
static std::shared_ptr< SHAMapTreeNode > makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
virtual ~SHAMapTreeNode() noexcept=default
virtual std::string getString(SHAMapNodeID const &) const
static std::shared_ptr< SHAMapTreeNode > makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
virtual bool isLeaf() const =0
Determines if this is a leaf node.
static std::shared_ptr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
SHAMapTreeNode(SHAMapTreeNode const &)=delete
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 std::shared_ptr< SHAMapTreeNode > makeTransaction(Slice data, SHAMapHash const &hash, bool hashValid)
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 std::shared_ptr< SHAMapTreeNode > makeFromWire(Slice rawNode)
virtual SHAMapNodeType getType() const =0
Determines the type of node.
An immutable linear range of bytes.
Definition: Slice.h:45
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.
virtual std::shared_ptr< SHAMapTreeNode > clone(std::uint32_t cowid) const =0
Make a copy of this node, setting the owner.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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
STL namespace.