xrpld
Loading...
Searching...
No Matches
FetchPack_test.cpp
1#include <test/shamap/common.h>
2#include <test/unit_test/SuiteJournal.h>
3
4#include <xrpl/basics/Blob.h>
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/SHAMapHash.h>
7#include <xrpl/basics/Slice.h>
8#include <xrpl/basics/UnorderedContainers.h>
9#include <xrpl/basics/contract.h>
10#include <xrpl/basics/random.h>
11#include <xrpl/beast/unit_test/suite.h>
12#include <xrpl/beast/utility/Journal.h>
13#include <xrpl/beast/xor_shift_engine.h>
14#include <xrpl/protocol/Serializer.h>
15#include <xrpl/protocol/digest.h>
16#include <xrpl/shamap/SHAMap.h>
17#include <xrpl/shamap/SHAMapItem.h>
18#include <xrpl/shamap/SHAMapMissingNode.h>
19#include <xrpl/shamap/SHAMapSyncFilter.h>
20#include <xrpl/shamap/SHAMapTreeNode.h>
21
22#include <boost/smart_ptr/intrusive_ptr.hpp>
23
24#include <cassert>
25#include <cstddef>
26#include <cstdint>
27#include <memory>
28#include <optional>
29#include <stdexcept>
30
31namespace xrpl::tests {
32
34{
35public:
36 static constexpr auto kTableItems = 100;
37 static constexpr auto kTableItemsExtra = 20;
38
40 using Table = SHAMap;
42
43 struct Handler
44 {
45 void
47 {
48 Throw<std::runtime_error>("missing node");
49 }
50 };
51
53 {
57
58 void
60 bool fromFilter,
61 SHAMapHash const& nodeHash,
62 std::uint32_t ledgerSeq,
63 Blob&& nodeData, // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
64 SHAMapNodeType type) const override
65 {
66 }
67
68 [[nodiscard]] std::optional<Blob>
69 getNode(SHAMapHash const& nodeHash) const override
70 {
71 Map::iterator const it = map.find(nodeHash);
72 if (it == map.end())
73 {
74 JLOG(journal.fatal()) << "Test filter missing node";
75 return std::nullopt;
76 }
77 return it->second;
78 }
79
82 };
83
84 static boost::intrusive_ptr<Item>
86 {
87 Serializer s;
88 for (int d = 0; d < 3; ++d)
90 return makeShamapitem(s.getSHA512Half(), s.slice());
91 }
92
93 static void
95 {
96 while ((n--) != 0u)
97 {
99 assert(result);
100 (void)result;
101 }
102 }
103
104 void
105 onFetch(Map& map, SHAMapHash const& hash, Blob const& blob)
106 {
107 BEAST_EXPECT(sha512Half(makeSlice(blob)) == hash.asUInt256());
108 map.emplace(hash, blob);
109 }
110
111 void
112 run() override
113 {
114 using beast::Severity;
115 test::SuiteJournal journal("FetchPack_test", *this);
116
117 TestNodeFamily f(journal);
119
120 pass();
121
122 // beast::Random r;
123 // add_random_items_ (tableItems, *t1, r);
124 // std::shared_ptr <Table> t2 (t1->snapShot (true));
125 //
126 // add_random_items_ (tableItemsExtra, *t1, r);
127 // add_random_items_ (tableItemsExtra, *t2, r);
128
129 // turn t1 into t2
130 // Map map;
131 // t2->getFetchPack (t1.get(), true, 1000000, std::bind (
132 // &FetchPack_test::on_fetch, this, std::ref (map),
133 // std::placeholders::_1, std::placeholders::_2));
134 // t1->getFetchPack (nullptr, true, 1000000, std::bind (
135 // &FetchPack_test::on_fetch, this, std::ref (map),
136 // std::placeholders::_1, std::placeholders::_2));
137
138 // try to rebuild t2 from the fetch pack
139 // std::shared_ptr <Table> t3;
140 // try
141 // {
142 // TestFilter filter (map, beast::Journal());
143 //
144 // t3 = std::make_shared <Table> (SHAMapType::FREE,
145 // t2->getHash (),
146 // fullBelowCache);
147 //
148 // BEAST_EXPECT(t3->fetchRoot (t2->getHash (), &filter),
149 // "unable to get root");
150 //
151 // // everything should be in the pack, no hashes should be
152 // needed std::vector <uint256> hashes =
153 // t3->getNeededHashes(1, &filter);
154 // BEAST_EXPECT(hashes.empty(), "missing hashes");
155 //
156 // BEAST_EXPECT(t3->getHash () == t2->getHash (), "root
157 // hashes do not match"); BEAST_EXPECT(t3->deepCompare
158 // (*t2), "failed compare");
159 // }
160 // catch (std::exception const&)
161 // {
162 // fail ("unhandled exception");
163 // }
164 }
165};
166
167BEAST_DEFINE_TESTSUITE(FetchPack, shamap, xrpl);
168
169} // namespace xrpl::tests
A generic endpoint for log messages.
Definition Journal.h:38
A testsuite class.
Definition suite.h:50
void pass()
Record a successful test condition.
Definition suite.h:500
uint256 const & asUInt256() const
Definition SHAMapHash.h:24
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:77
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
Definition SHAMap.cpp:830
uint256 getSHA512Half() const
Slice slice() const noexcept
Definition Serializer.h:44
void onFetch(Map &map, SHAMapHash const &hash, Blob const &blob)
hash_map< SHAMapHash, Blob > Map
static boost::intrusive_ptr< Item > makeRandomItemMember(beast::xor_shift_engine &r)
static void addRandomItems(std::size_t n, Table &t, beast::xor_shift_engine &r)
static constexpr auto kTableItems
static constexpr auto kTableItemsExtra
void run() override
Runs the suite.
T emplace(T... args)
T make_shared(T... args)
detail::XorShiftEngine<> xor_shift_engine
XOR-shift Generator.
Severity
Severity level / threshold of a Journal message.
Definition Journal.h:11
BEAST_DEFINE_TESTSUITE(IntrusiveShared, basics, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:204
std::enable_if_t< std::is_integral_v< Integral > &&detail::is_engine< Engine >::value, Integral > randInt(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
boost::intrusive_ptr< SHAMapItem > makeShamapitem(uint256 const &tag, Slice data)
Definition SHAMapItem.h:139
std::unordered_map< Key, Value, Hash, Pred, Allocator > hash_map
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
std::enable_if_t< std::is_same_v< T, char >||std::is_same_v< T, unsigned char >, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:215
void operator()(std::uint32_t refNum) const
void gotNode(bool fromFilter, SHAMapHash const &nodeHash, std::uint32_t ledgerSeq, Blob &&nodeData, SHAMapNodeType type) const override
std::optional< Blob > getNode(SHAMapHash const &nodeHash) const override
TestFilter(Map &map, beast::Journal journal)