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/SHAMapHash.h>
27#include <xrpl/protocol/Serializer.h>
28
29#include <cstdint>
30#include <string>
31
32namespace ripple {
33
34// These are wire-protocol identifiers used during serialization to encode the
35// type of a node. They should not be arbitrarily be changed.
36static constexpr unsigned char const wireTypeTransaction = 0;
37static constexpr unsigned char const wireTypeAccountState = 1;
38static constexpr unsigned char const wireTypeInner = 2;
39static constexpr unsigned char const wireTypeCompressedInner = 3;
40static constexpr unsigned char const wireTypeTransactionWithMeta = 4;
41
42enum class SHAMapNodeType {
43 tnINNER = 1,
44 tnTRANSACTION_NM = 2, // transaction, no metadata
45 tnTRANSACTION_MD = 3, // transaction, with metadata
47};
48
50{
51protected:
53
61
62protected:
65 operator=(SHAMapTreeNode const&) = delete;
66
74 {
75 }
76
79 SHAMapHash const& hash) noexcept
80 : hash_(hash), cowid_(cowid)
81 {
82 }
85public:
86 virtual ~SHAMapTreeNode() noexcept = default;
87
111 std::uint32_t
112 cowid() const
113 {
114 return cowid_;
115 }
116
122 void
124 {
125 cowid_ = 0;
126 }
127
134 virtual void
136
138 SHAMapHash const&
139 getHash() const
140 {
141 return hash_;
142 }
143
145 virtual SHAMapNodeType
146 getType() const = 0;
147
149 virtual bool
150 isLeaf() const = 0;
151
153 virtual bool
154 isInner() const = 0;
155
157 virtual void
159
161 virtual void
163
164 virtual std::string
165 getString(SHAMapNodeID const&) const;
166
167 virtual void
168 invariants(bool is_root = false) const = 0;
169
171 makeFromPrefix(Slice rawNode, SHAMapHash const& hash);
172
174 makeFromWire(Slice rawNode);
175
176private:
178 makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid);
179
181 makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid);
182
184 makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool hashValid);
185};
186
187} // namespace ripple
188
189#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.