rippled
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/UnorderedContainers.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/basics/random.h>
7#include <xrpl/beast/unit_test.h>
8#include <xrpl/beast/xor_shift_engine.h>
9#include <xrpl/protocol/digest.h>
10#include <xrpl/shamap/SHAMap.h>
11#include <xrpl/shamap/SHAMapSyncFilter.h>
12
13#include <functional>
14#include <stdexcept>
15
16namespace xrpl {
17namespace tests {
18
20{
21public:
22 enum { tableItems = 100, tableItemsExtra = 20 };
23
25 using Table = SHAMap;
27
28 struct Handler
29 {
30 void
32 {
33 Throw<std::runtime_error>("missing node");
34 }
35 };
36
38 {
39 TestFilter(Map& map, beast::Journal journal) : mMap(map), mJournal(journal)
40 {
41 }
42
43 void
45 bool fromFilter,
46 SHAMapHash const& nodeHash,
47 std::uint32_t ledgerSeq,
48 Blob&& nodeData,
49 SHAMapNodeType type) const override
50 {
51 }
52
54 getNode(SHAMapHash const& nodeHash) const override
55 {
56 Map::iterator it = mMap.find(nodeHash);
57 if (it == mMap.end())
58 {
59 JLOG(mJournal.fatal()) << "Test filter missing node";
60 return std::nullopt;
61 }
62 return it->second;
63 }
64
67 };
68
69 boost::intrusive_ptr<Item>
71 {
72 Serializer s;
73 for (int d = 0; d < 3; ++d)
74 s.add32(xrpl::rand_int<std::uint32_t>(r));
75 return make_shamapitem(s.getSHA512Half(), s.slice());
76 }
77
78 void
80 {
81 while (n--)
82 {
84 assert(result);
85 (void)result;
86 }
87 }
88
89 void
90 on_fetch(Map& map, SHAMapHash const& hash, Blob const& blob)
91 {
92 BEAST_EXPECT(sha512Half(makeSlice(blob)) == hash.as_uint256());
93 map.emplace(hash, blob);
94 }
95
96 void
97 run() override
98 {
99 using namespace beast::severities;
100 test::SuiteJournal journal("FetchPack_test", *this);
101
102 TestNodeFamily f(journal);
104
105 pass();
106
107 // beast::Random r;
108 // add_random_items (tableItems, *t1, r);
109 // std::shared_ptr <Table> t2 (t1->snapShot (true));
110 //
111 // add_random_items (tableItemsExtra, *t1, r);
112 // add_random_items (tableItemsExtra, *t2, r);
113
114 // turn t1 into t2
115 // Map map;
116 // t2->getFetchPack (t1.get(), true, 1000000, std::bind (
117 // &FetchPack_test::on_fetch, this, std::ref (map),
118 // std::placeholders::_1, std::placeholders::_2));
119 // t1->getFetchPack (nullptr, true, 1000000, std::bind (
120 // &FetchPack_test::on_fetch, this, std::ref (map),
121 // std::placeholders::_1, std::placeholders::_2));
122
123 // try to rebuild t2 from the fetch pack
124 // std::shared_ptr <Table> t3;
125 // try
126 // {
127 // TestFilter filter (map, beast::Journal());
128 //
129 // t3 = std::make_shared <Table> (SHAMapType::FREE,
130 // t2->getHash (),
131 // fullBelowCache);
132 //
133 // BEAST_EXPECT(t3->fetchRoot (t2->getHash (), &filter),
134 // "unable to get root");
135 //
136 // // everything should be in the pack, no hashes should be
137 // needed std::vector <uint256> hashes =
138 // t3->getNeededHashes(1, &filter);
139 // BEAST_EXPECT(hashes.empty(), "missing hashes");
140 //
141 // BEAST_EXPECT(t3->getHash () == t2->getHash (), "root
142 // hashes do not match"); BEAST_EXPECT(t3->deepCompare
143 // (*t2), "failed compare");
144 // }
145 // catch (std::exception const&)
146 // {
147 // fail ("unhandled exception");
148 // }
149 }
150};
151
152BEAST_DEFINE_TESTSUITE(FetchPack, shamap, xrpl);
153
154} // namespace tests
155} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:325
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:495
uint256 const & as_uint256() const
Definition SHAMapHash.h:25
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:78
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
Definition SHAMap.cpp:775
uint256 getSHA512Half() const
Slice slice() const noexcept
Definition Serializer.h:45
boost::intrusive_ptr< Item > make_random_item(beast::xor_shift_engine &r)
void add_random_items(std::size_t n, Table &t, beast::xor_shift_engine &r)
void on_fetch(Map &map, SHAMapHash const &hash, Blob const &blob)
void run() override
Runs the suite.
T emplace(T... args)
T end(T... args)
T find(T... args)
T is_same_v
A namespace for easy access to logging severity values.
Definition Journal.h:11
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::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:214
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)