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 ripple {
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 {
40 : mMap(map), mJournal(journal)
41 {
42 }
43
44 void
46 bool fromFilter,
47 SHAMapHash const& nodeHash,
48 std::uint32_t ledgerSeq,
49 Blob&& nodeData,
50 SHAMapNodeType type) const override
51 {
52 }
53
55 getNode(SHAMapHash const& nodeHash) const override
56 {
57 Map::iterator it = mMap.find(nodeHash);
58 if (it == mMap.end())
59 {
60 JLOG(mJournal.fatal()) << "Test filter missing node";
61 return std::nullopt;
62 }
63 return it->second;
64 }
65
68 };
69
70 boost::intrusive_ptr<Item>
72 {
73 Serializer s;
74 for (int d = 0; d < 3; ++d)
75 s.add32(ripple::rand_int<std::uint32_t>(r));
76 return make_shamapitem(s.getSHA512Half(), s.slice());
77 }
78
79 void
81 {
82 while (n--)
83 {
84 auto const result(t.addItem(
86 assert(result);
87 (void)result;
88 }
89 }
90
91 void
92 on_fetch(Map& map, SHAMapHash const& hash, Blob const& blob)
93 {
94 BEAST_EXPECT(sha512Half(makeSlice(blob)) == hash.as_uint256());
95 map.emplace(hash, blob);
96 }
97
98 void
99 run() override
100 {
101 using namespace beast::severities;
102 test::SuiteJournal journal("FetchPack_test", *this);
103
104 TestNodeFamily f(journal);
106
107 pass();
108
109 // beast::Random r;
110 // add_random_items (tableItems, *t1, r);
111 // std::shared_ptr <Table> t2 (t1->snapShot (true));
112 //
113 // add_random_items (tableItemsExtra, *t1, r);
114 // add_random_items (tableItemsExtra, *t2, r);
115
116 // turn t1 into t2
117 // Map map;
118 // t2->getFetchPack (t1.get(), true, 1000000, std::bind (
119 // &FetchPack_test::on_fetch, this, std::ref (map),
120 // std::placeholders::_1, std::placeholders::_2));
121 // t1->getFetchPack (nullptr, true, 1000000, std::bind (
122 // &FetchPack_test::on_fetch, this, std::ref (map),
123 // std::placeholders::_1, std::placeholders::_2));
124
125 // try to rebuild t2 from the fetch pack
126 // std::shared_ptr <Table> t3;
127 // try
128 // {
129 // TestFilter filter (map, beast::Journal());
130 //
131 // t3 = std::make_shared <Table> (SHAMapType::FREE,
132 // t2->getHash (),
133 // fullBelowCache);
134 //
135 // BEAST_EXPECT(t3->fetchRoot (t2->getHash (), &filter),
136 // "unable to get root");
137 //
138 // // everything should be in the pack, no hashes should be
139 // needed std::vector <uint256> hashes =
140 // t3->getNeededHashes(1, &filter);
141 // BEAST_EXPECT(hashes.empty(), "missing hashes");
142 //
143 // BEAST_EXPECT(t3->getHash () == t2->getHash (), "root
144 // hashes do not match"); BEAST_EXPECT(t3->deepCompare
145 // (*t2), "failed compare");
146 // }
147 // catch (std::exception const&)
148 // {
149 // fail ("unhandled exception");
150 // }
151 }
152};
153
154BEAST_DEFINE_TESTSUITE(FetchPack, shamap, ripple);
155
156} // namespace tests
157} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:41
Stream fatal() const
Definition Journal.h:333
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:508
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:863
Slice slice() const noexcept
Definition Serializer.h:47
uint256 getSHA512Half() const
void add_random_items(std::size_t n, Table &t, beast::xor_shift_engine &r)
boost::intrusive_ptr< Item > make_random_item(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
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:225
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition SHAMapItem.h:142
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:205
void operator()(std::uint32_t refNum) const
std::optional< Blob > getNode(SHAMapHash const &nodeHash) const override
TestFilter(Map &map, beast::Journal journal)
void gotNode(bool fromFilter, SHAMapHash const &nodeHash, std::uint32_t ledgerSeq, Blob &&nodeData, SHAMapNodeType type) const override