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/SHAMapHash.h>
26#include <xrpl/protocol/Serializer.h>
27
28#include <cstdint>
29#include <string>
30
31namespace ripple {
32
33// These are wire-protocol identifiers used during serialization to encode the
34// type of a node. They should not be arbitrarily be changed.
35static constexpr unsigned char const wireTypeTransaction = 0;
36static constexpr unsigned char const wireTypeAccountState = 1;
37static constexpr unsigned char const wireTypeInner = 2;
38static constexpr unsigned char const wireTypeCompressedInner = 3;
39static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
40
41enum class SHAMapNodeType {
42 tnINNER = 1,
43 tnTRANSACTION_NM = 2, // transaction, no metadata
44 tnTRANSACTION_MD = 3, // transaction, with metadata
46};
47
49{
50protected:
52
60
61protected:
64 operator=(SHAMapTreeNode const&) = delete;
65
73 {
74 }
75
78 SHAMapHash const& hash) noexcept
79 : hash_(hash), cowid_(cowid)
80 {
81 }
84public:
85 virtual ~SHAMapTreeNode() noexcept = default;
86
110 std::uint32_t
111 cowid() const
112 {
113 return cowid_;
114 }
115
121 void
123 {
124 cowid_ = 0;
125 }
126
133 virtual void
135
137 SHAMapHash const&
138 getHash() const
139 {
140 return hash_;
141 }
142
144 virtual SHAMapNodeType
145 getType() const = 0;
146
148 virtual bool
149 isLeaf() const = 0;
150
152 virtual bool
153 isInner() const = 0;
154
156 virtual void
158
160 virtual void
162
163 virtual std::string
164 getString(SHAMapNodeID const&) const;
165
166 virtual void
167 invariants(bool is_root = false) const = 0;
168
170 makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
171
173 makeFromWire(Slice rawNode);
174
175private:
177 makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
178
180 makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
181
183 makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
184};
185
186} // namespace ripple
187
188#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:46
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.