rippled
Loading...
Searching...
No Matches
FetchPack_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/shamap/common.h>
21#include <test/unit_test/SuiteJournal.h>
22
23#include <xrpld/shamap/SHAMap.h>
24#include <xrpld/shamap/SHAMapSyncFilter.h>
25
26#include <xrpl/basics/UnorderedContainers.h>
27#include <xrpl/basics/contract.h>
28#include <xrpl/basics/random.h>
29#include <xrpl/beast/unit_test.h>
30#include <xrpl/beast/xor_shift_engine.h>
31#include <xrpl/protocol/digest.h>
32
33#include <functional>
34#include <stdexcept>
35
36namespace ripple {
37namespace tests {
38
40{
41public:
42 enum { tableItems = 100, tableItemsExtra = 20 };
43
45 using Table = SHAMap;
47
48 struct Handler
49 {
50 void
52 {
53 Throw<std::runtime_error>("missing node");
54 }
55 };
56
58 {
60 : mMap(map), mJournal(journal)
61 {
62 }
63
64 void
66 bool fromFilter,
67 SHAMapHash const& nodeHash,
68 std::uint32_t ledgerSeq,
69 Blob&& nodeData,
70 SHAMapNodeType type) const override
71 {
72 }
73
75 getNode(SHAMapHash const& nodeHash) const override
76 {
77 Map::iterator it = mMap.find(nodeHash);
78 if (it == mMap.end())
79 {
80 JLOG(mJournal.fatal()) << "Test filter missing node";
81 return std::nullopt;
82 }
83 return it->second;
84 }
85
88 };
89
90 boost::intrusive_ptr<Item>
92 {
93 Serializer s;
94 for (int d = 0; d < 3; ++d)
95 s.add32(ripple::rand_int<std::uint32_t>(r));
96 return make_shamapitem(s.getSHA512Half(), s.slice());
97 }
98
99 void
101 {
102 while (n--)
103 {
104 auto const result(t.addItem(
106 assert(result);
107 (void)result;
108 }
109 }
110
111 void
112 on_fetch(Map& map, SHAMapHash const& hash, Blob const& blob)
113 {
114 BEAST_EXPECT(sha512Half(makeSlice(blob)) == hash.as_uint256());
115 map.emplace(hash, blob);
116 }
117
118 void
119 run() override
120 {
121 using namespace beast::severities;
122 test::SuiteJournal journal("FetchPack_test", *this);
123
124 TestNodeFamily f(journal);
126
127 pass();
128
129 // beast::Random r;
130 // add_random_items (tableItems, *t1, r);
131 // std::shared_ptr <Table> t2 (t1->snapShot (true));
132 //
133 // add_random_items (tableItemsExtra, *t1, r);
134 // add_random_items (tableItemsExtra, *t2, r);
135
136 // turn t1 into t2
137 // Map map;
138 // t2->getFetchPack (t1.get(), true, 1000000, std::bind (
139 // &FetchPack_test::on_fetch, this, std::ref (map),
140 // std::placeholders::_1, std::placeholders::_2));
141 // t1->getFetchPack (nullptr, true, 1000000, std::bind (
142 // &FetchPack_test::on_fetch, this, std::ref (map),
143 // std::placeholders::_1, std::placeholders::_2));
144
145 // try to rebuild t2 from the fetch pack
146 // std::shared_ptr <Table> t3;
147 // try
148 // {
149 // TestFilter filter (map, beast::Journal());
150 //
151 // t3 = std::make_shared <Table> (SHAMapType::FREE,
152 // t2->getHash (),
153 // fullBelowCache);
154 //
155 // BEAST_EXPECT(t3->fetchRoot (t2->getHash (), &filter),
156 // "unable to get root");
157 //
158 // // everything should be in the pack, no hashes should be
159 // needed std::vector <uint256> hashes =
160 // t3->getNeededHashes(1, &filter);
161 // BEAST_EXPECT(hashes.empty(), "missing hashes");
162 //
163 // BEAST_EXPECT(t3->getHash () == t2->getHash (), "root
164 // hashes do not match"); BEAST_EXPECT(t3->deepCompare
165 // (*t2), "failed compare");
166 // }
167 // catch (std::exception const&)
168 // {
169 // fail ("unhandled exception");
170 // }
171 }
172};
173
174BEAST_DEFINE_TESTSUITE(FetchPack, shamap, ripple);
175
176} // namespace tests
177} // namespace ripple
A generic endpoint for log messages.
Definition Journal.h:60
Stream fatal() const
Definition Journal.h:352
A testsuite class.
Definition suite.h:55
void pass()
Record a successful test condition.
Definition suite.h:511
uint256 const & as_uint256() const
Definition SHAMapHash.h:44
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition SHAMap.h:99
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
Definition SHAMap.cpp:881
Slice slice() const noexcept
Definition Serializer.h:66
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:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:244
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition SHAMapItem.h:161
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:224
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