xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
shamap
InnerNode.h
1
#pragma once
2
3
#include <xrpl/basics/SHAMapHash.h>
4
#include <xrpl/basics/Slice.h>
5
#include <xrpl/basics/base_uint.h>
6
#include <xrpl/basics/contract.h>
7
#include <xrpl/protocol/Serializer.h>
8
#include <xrpl/shamap/SHAMapInnerNode.h>
9
#include <xrpl/shamap/SHAMapTreeNode.h>
10
11
#include <
array
>
12
#include <
cstdint
>
13
#include <
stdexcept
>
14
#include <
vector
>
15
16
namespace
xrpl::tests
{
17
21
struct
InnerChild
22
{
23
unsigned
int
branch
{};
24
SHAMapHash
hash
;
25
};
26
41
[[nodiscard]]
inline
SHAMapTreeNodePtr
42
makeFullInnerNode
(
std::vector<InnerChild>
const
& children)
43
{
44
// The full form carries a slot for every branch, so a child's position comes from the slot it
45
// is written to.
46
std::array<UInt256, SHAMapInnerNode::kBranchFactor>
hashes{};
47
48
// A duplicate branch is refused here, and one past the last to match the compressed parser.
49
std::uint32_t
seen = 0;
50
for
(
auto
const
& child : children)
51
{
52
if
(child.branch >=
SHAMapInnerNode::kBranchFactor
)
53
Throw<std::logic_error>
(
"makeFullInnerNode: branch is past the last one"
);
54
55
auto
const
bit = 1u << child.branch;
56
if
((seen & bit) != 0)
57
Throw<std::logic_error>
(
"makeFullInnerNode: branch named twice"
);
58
seen |= bit;
59
60
hashes.
at
(child.branch) = child.hash.asUInt256();
61
}
62
63
Serializer
s;
64
for
(
auto
const
& hash : hashes)
65
s.
addBitString
(hash);
66
s.
add8
(
kWireTypeInner
);
67
68
return
SHAMapTreeNode::makeFromWire
(
makeSlice
(s.
peekData
()));
69
}
70
86
[[nodiscard]]
inline
SHAMapTreeNodePtr
87
makeCompressedInnerNode
(
std::vector<InnerChild>
const
& children)
88
{
89
Serializer
s;
90
for
(
auto
const
& child : children)
91
{
92
s.
addBitString
(child.hash.asUInt256());
93
s.
add8
(
static_cast<
unsigned
char
>
(child.branch));
94
}
95
s.
add8
(
kWireTypeCompressedInner
);
96
97
return
SHAMapTreeNode::makeFromWire
(
makeSlice
(s.
peekData
()));
98
}
99
100
}
// namespace xrpl::tests
array
std::array::at
T at(T... args)
xrpl::SHAMapHash
Definition
SHAMapHash.h:16
xrpl::SHAMapInnerNode::kBranchFactor
static constexpr unsigned int kBranchFactor
Each inner node has 16 children (the 'radix tree' part of the map).
Definition
SHAMapInnerNode.h:24
xrpl::SHAMapTreeNode::makeFromWire
static SHAMapTreeNodePtr makeFromWire(Slice rawNode)
Definition
SHAMapTreeNode.cpp:126
xrpl::Serializer
Definition
Serializer.h:23
xrpl::Serializer::peekData
Blob const & peekData() const
Definition
Serializer.h:273
xrpl::Serializer::addBitString
int addBitString(BaseUInt< Bits, Tag > const &v)
Definition
Serializer.h:202
xrpl::Serializer::add8
int add8(unsigned char byteValue)
Definition
libxrpl/protocol/Serializer.cpp:146
cstdint
std::uint32_t
xrpl::tests
Definition
LedgerNodeHelpers_test.cpp:21
xrpl::tests::makeCompressedInnerNode
SHAMapTreeNodePtr makeCompressedInnerNode(std::vector< InnerChild > const &children)
Assemble an inner node in the wire format's compressed form.
Definition
InnerNode.h:87
xrpl::tests::makeFullInnerNode
SHAMapTreeNodePtr makeFullInnerNode(std::vector< InnerChild > const &children)
Assemble an inner node in the wire format's full form.
Definition
InnerNode.h:42
xrpl::SHAMapTreeNodePtr
intr_ptr::SharedPtr< SHAMapTreeNode > SHAMapTreeNodePtr
Definition
SHAMapTreeNode.h:17
xrpl::kWireTypeCompressedInner
static constexpr unsigned char const kWireTypeCompressedInner
Definition
SHAMapTreeNode.h:24
xrpl::makeSlice
Slice makeSlice(std::array< T, N > const &a)
Definition
Slice.h:228
xrpl::kWireTypeInner
static constexpr unsigned char const kWireTypeInner
Definition
SHAMapTreeNode.h:23
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
stdexcept
xrpl::tests::InnerChild
One child of an inner node a test assembles by hand.
Definition
InnerNode.h:22
xrpl::tests::InnerChild::hash
SHAMapHash hash
Definition
InnerNode.h:24
xrpl::tests::InnerChild::branch
unsigned int branch
Definition
InnerNode.h:23
vector
Generated by
1.17.0