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 xrpl {
14
15intr_ptr::SharedPtr<SHAMapTreeNode>
16SHAMapTreeNode::makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid)
17{
19
20 if (hashValid)
21 return intr_ptr::make_shared<SHAMapTxLeafNode>(std::move(item), 0, hash);
22
23 return intr_ptr::make_shared<SHAMapTxLeafNode>(std::move(item), 0);
24}
25
28{
29 Serializer s(data.data(), data.size());
30
31 uint256 tag;
32
33 if (s.size() < tag.bytes)
34 Throw<std::runtime_error>("Short TXN+MD node");
35
36 // FIXME: improve this interface so that the above check isn't needed
37 if (!s.getBitString(tag, s.size() - tag.bytes))
38 Throw<std::out_of_range>("Short TXN+MD node (" + std::to_string(s.size()) + ")");
39
40 s.chop(tag.bytes);
41
42 auto item = make_shamapitem(tag, s.slice());
43
44 if (hashValid)
45 return intr_ptr::make_shared<SHAMapTxPlusMetaLeafNode>(std::move(item), 0, hash);
46
47 return intr_ptr::make_shared<SHAMapTxPlusMetaLeafNode>(std::move(item), 0);
48}
49
51SHAMapTreeNode::makeAccountState(Slice data, SHAMapHash const& hash, bool hashValid)
52{
53 Serializer s(data.data(), data.size());
54
55 uint256 tag;
56
57 if (s.size() < tag.bytes)
58 Throw<std::runtime_error>("short AS node");
59
60 // FIXME: improve this interface so that the above check isn't needed
61 if (!s.getBitString(tag, s.size() - tag.bytes))
62 Throw<std::out_of_range>("Short AS node (" + std::to_string(s.size()) + ")");
63
64 s.chop(tag.bytes);
65
66 if (tag.isZero())
67 Throw<std::runtime_error>("Invalid AS node");
68
69 auto item = make_shamapitem(tag, s.slice());
70
71 if (hashValid)
72 return intr_ptr::make_shared<SHAMapAccountStateLeafNode>(std::move(item), 0, hash);
73
74 return intr_ptr::make_shared<SHAMapAccountStateLeafNode>(std::move(item), 0);
75}
76
79{
80 if (rawNode.empty())
81 return {};
82
83 auto const type = rawNode[rawNode.size() - 1];
84
85 rawNode.remove_suffix(1);
86
87 bool const hashValid = false;
88 SHAMapHash const hash;
89
90 if (type == wireTypeTransaction)
91 return makeTransaction(rawNode, hash, hashValid);
92
93 if (type == wireTypeAccountState)
94 return makeAccountState(rawNode, hash, hashValid);
95
96 if (type == wireTypeInner)
97 return SHAMapInnerNode::makeFullInner(rawNode, hash, hashValid);
98
99 if (type == wireTypeCompressedInner)
101
102 if (type == wireTypeTransactionWithMeta)
103 return makeTransactionWithMeta(rawNode, hash, hashValid);
104
105 Throw<std::runtime_error>("wire: Unknown type (" + std::to_string(type) + ")");
106}
107
110{
111 if (rawNode.size() < 4)
112 Throw<std::runtime_error>("prefix: short node");
113
114 // FIXME: Use SerialIter::get32?
115 // Extract the prefix
116 auto const type = safe_cast<HashPrefix>(
117 (safe_cast<std::uint32_t>(rawNode[0]) << 24) + (safe_cast<std::uint32_t>(rawNode[1]) << 16) +
118 (safe_cast<std::uint32_t>(rawNode[2]) << 8) + (safe_cast<std::uint32_t>(rawNode[3])));
119
120 rawNode.remove_prefix(4);
121
122 bool const hashValid = true;
123
124 if (type == HashPrefix::transactionID)
125 return makeTransaction(rawNode, hash, hashValid);
126
127 if (type == HashPrefix::leafNode)
128 return makeAccountState(rawNode, hash, hashValid);
129
130 if (type == HashPrefix::innerNode)
131 return SHAMapInnerNode::makeFullInner(rawNode, hash, hashValid);
132
133 if (type == HashPrefix::txNode)
134 return makeTransactionWithMeta(rawNode, hash, hashValid);
135
136 Throw<std::runtime_error>(
137 "prefix: unknown type (" + std::to_string(safe_cast<std::underlying_type_t<HashPrefix>>(type)) + ")");
138}
139
142{
143 return to_string(id);
144}
145
146} // namespace xrpl
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFullInner(Slice data, SHAMapHash const &hash, bool hashValid)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeCompressedInner(Slice data)
Identifies a node inside a SHAMap.
static intr_ptr::SharedPtr< SHAMapTreeNode > makeAccountState(Slice data, SHAMapHash const &hash, bool hashValid)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeTransactionWithMeta(Slice data, SHAMapHash const &hash, bool hashValid)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeFromWire(Slice rawNode)
static intr_ptr::SharedPtr< SHAMapTreeNode > makeTransaction(Slice data, SHAMapHash const &hash, bool hashValid)
virtual std::string getString(SHAMapNodeID const &) const
bool getBitString(base_uint< Bits, Tag > &data, int offset) const
Definition Serializer.h:155
bool chop(int num)
Slice slice() const noexcept
Definition Serializer.h:45
std::size_t size() const noexcept
Definition Serializer.h:51
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:50
void remove_suffix(std::size_t n)
Shrinks the slice by moving its end backward by n characters.
Definition Slice.h:121
void remove_prefix(std::size_t n)
Shrinks the slice by moving its start forward by n characters.
Definition Slice.h:113
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:61
bool isZero() const
Definition base_uint.h:509
static std::size_t constexpr bytes
Definition base_uint.h:85
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition SHAMapItem.h:138
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:205
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
static constexpr unsigned char const wireTypeAccountState
static constexpr unsigned char const wireTypeInner
static constexpr unsigned char const wireTypeCompressedInner
@ leafNode
account state
@ txNode
transaction plus metadata
@ transactionID
transaction plus signature to give transaction ID
@ innerNode
inner node in V1 tree
static constexpr unsigned char const wireTypeTransactionWithMeta
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:20
static constexpr unsigned char const wireTypeTransaction
T to_string(T... args)