rippled
Loading...
Searching...
No Matches
SHAMapTreeNode.cpp
1#include <xrpl/basics/IntrusivePointer.ipp>
2#include <xrpl/basics/Slice.h>
3#include <xrpl/basics/contract.h>
4#include <xrpl/basics/safe_cast.h>
5#include <xrpl/protocol/HashPrefix.h>
6#include <xrpl/protocol/digest.h>
7#include <xrpl/shamap/SHAMapAccountStateLeafNode.h>
8#include <xrpl/shamap/SHAMapInnerNode.h>
9#include <xrpl/shamap/SHAMapTreeNode.h>
10#include <xrpl/shamap/SHAMapTxLeafNode.h>
11#include <xrpl/shamap/SHAMapTxPlusMetaLeafNode.h>
12
13namespace ripple {
14
15intr_ptr::SharedPtr<SHAMapTreeNode>
17 Slice data,
18 SHAMapHash const& hash,
19 bool hashValid)
20{
21 auto item =
23
24 if (hashValid)
25 return intr_ptr::make_shared<SHAMapTxLeafNode>(
26 std::move(item), 0, hash);
27
28 return intr_ptr::make_shared<SHAMapTxLeafNode>(std::move(item), 0);
29}
30
33 Slice data,
34 SHAMapHash const& hash,
35 bool hashValid)
36{
37 Serializer s(data.data(), data.size());
38
39 uint256 tag;
40
41 if (s.size() < tag.bytes)
42 Throw<std::runtime_error>("Short TXN+MD node");
43
44 // FIXME: improve this interface so that the above check isn't needed
45 if (!s.getBitString(tag, s.size() - tag.bytes))
46 Throw<std::out_of_range>(
47 "Short TXN+MD node (" + std::to_string(s.size()) + ")");
48
49 s.chop(tag.bytes);
50
51 auto item = make_shamapitem(tag, s.slice());
52
53 if (hashValid)
54 return intr_ptr::make_shared<SHAMapTxPlusMetaLeafNode>(
55 std::move(item), 0, hash);
56
57 return intr_ptr::make_shared<SHAMapTxPlusMetaLeafNode>(std::move(item), 0);
58}
59
62 Slice data,
63 SHAMapHash const& hash,
64 bool hashValid)
65{
66 Serializer s(data.data(), data.size());
67
68 uint256 tag;
69
70 if (s.size() < tag.bytes)
71 Throw<std::runtime_error>("short AS node");
72
73 // FIXME: improve this interface so that the above check isn't needed
74 if (!s.getBitString(tag, s.size() - tag.bytes))
75 Throw<std::out_of_range>(
76 "Short AS node (" + std::to_string(s.size()) + ")");
77
78 s.chop(tag.bytes);
79
80 if (tag.isZero())
81 Throw<std::runtime_error>("Invalid AS node");
82
83 auto item = make_shamapitem(tag, s.slice());
84
85 if (hashValid)
86 return intr_ptr::make_shared<SHAMapAccountStateLeafNode>(
87 std::move(item), 0, hash);
88
89 return intr_ptr::make_shared<SHAMapAccountStateLeafNode>(
90 std::move(item), 0);
91}
92
95{
96 if (rawNode.empty())
97 return {};
98
99 auto const type = rawNode[rawNode.size() - 1];
100
101 rawNode.remove_suffix(1);
102
103 bool const hashValid = false;
104 SHAMapHash const hash;
105
106 if (type == wireTypeTransaction)
107 return makeTransaction(rawNode, hash, hashValid);
108
109 if (type == wireTypeAccountState)
110 return makeAccountState(rawNode, hash, hashValid);
111
112 if (type == wireTypeInner)
113 return SHAMapInnerNode::makeFullInner(rawNode, hash, hashValid);
114
115 if (type == wireTypeCompressedInner)
117
118 if (type == wireTypeTransactionWithMeta)
119 return makeTransactionWithMeta(rawNode, hash, hashValid);
120
121 Throw<std::runtime_error>(
122 "wire: Unknown type (" + std::to_string(type) + ")");
123}
124
127{
128 if (rawNode.size() < 4)
129 Throw<std::runtime_error>("prefix: short node");
130
131 // FIXME: Use SerialIter::get32?
132 // Extract the prefix
133 auto const type = safe_cast<HashPrefix>(
134 (safe_cast<std::uint32_t>(rawNode[0]) << 24) +
135 (safe_cast<std::uint32_t>(rawNode[1]) << 16) +
136 (safe_cast<std::uint32_t>(rawNode[2]) << 8) +
137 (safe_cast<std::uint32_t>(rawNode[3])));
138
139 rawNode.remove_prefix(4);
140
141 bool const hashValid = true;
142
143 if (type == HashPrefix::transactionID)
144 return makeTransaction(rawNode, hash, hashValid);
145
146 if (type == HashPrefix::leafNode)
147 return makeAccountState(rawNode, hash, hashValid);
148
149 if (type == HashPrefix::innerNode)
150 return SHAMapInnerNode::makeFullInner(rawNode, hash, hashValid);
151
152 if (type == HashPrefix::txNode)
153 return makeTransactionWithMeta(rawNode, hash, hashValid);
154
155 Throw<std::runtime_error>(
156 "prefix: unknown type (" +
158 ")");
159}
160
163{
164 return to_string(id);
165}
166
167} // namespace ripple
static intr_ptr::SharedPtr< SHAMapTreeNode > makeCompressedInner(Slice data)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFullInner(Slice data, SHAMapHash const &hash, bool hashValid)
Identifies a node inside a SHAMap.
static intr_ptr::SharedPtr< SHAMapTreeNode > makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
virtual std::string getString(SHAMapNodeID const &) const
static intr_ptr::SharedPtr< SHAMapTreeNode > makeTransaction(Slice data, SHAMapHash const &hash, bool hashValid)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFromWire(Slice rawNode)
std::size_t size() const noexcept
Definition Serializer.h:53
bool getBitString(base_uint< Bits, Tag > &data, int offset) const
Definition Serializer.h:161
Slice slice() const noexcept
Definition Serializer.h:47
bool chop(int num)
A shared intrusive pointer class that supports weak pointers.
An immutable linear range of bytes.
Definition Slice.h:27
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:51
void remove_suffix(std::size_t n)
Shrinks the slice by moving its end backward by n characters.
Definition Slice.h:124
void remove_prefix(std::size_t n)
Shrinks the slice by moving its start forward by n characters.
Definition Slice.h:116
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:62
static std::size_t constexpr bytes
Definition base_uint.h:89
bool isZero() const
Definition base_uint.h:521
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
static constexpr unsigned char const wireTypeAccountState
static constexpr unsigned char const wireTypeCompressedInner
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safe_cast(Src s) noexcept
Definition safe_cast.h:22
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition SHAMapItem.h:142
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
static constexpr unsigned char const wireTypeInner
static constexpr unsigned char const wireTypeTransaction
static constexpr unsigned char const wireTypeTransactionWithMeta
@ leafNode
account state
@ txNode
transaction plus metadata
@ transactionID
transaction plus signature to give transaction ID
@ innerNode
inner node in V1 tree
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:205
T to_string(T... args)