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#include <xrpld/shamap/SHAMap.h>
23#include <xrpld/shamap/SHAMapSyncFilter.h>
24#include <xrpl/basics/StringUtilities.h>
25#include <xrpl/basics/UnorderedContainers.h>
26#include <xrpl/basics/contract.h>
27#include <xrpl/basics/random.h>
28#include <xrpl/beast/unit_test.h>
29#include <xrpl/beast/xor_shift_engine.h>
30#include <xrpl/protocol/digest.h>
31#include <functional>
32#include <stdexcept>
33
34namespace ripple {
35namespace tests {
36
38{
39public:
40 enum { tableItems = 100, tableItemsExtra = 20 };
41
43 using Table = SHAMap;
45
46 struct Handler
47 {
48 void
50 {
51 Throw<std::runtime_error>("missing node");
52 }
53 };
54
56 {
58 : mMap(map), mJournal(journal)
59 {
60 }
61
62 void
64 bool fromFilter,
65 SHAMapHash const& nodeHash,
66 std::uint32_t ledgerSeq,
67 Blob&& nodeData,
68 SHAMapNodeType type) const override
69 {
70 }
71
73 getNode(SHAMapHash const& nodeHash) const override
74 {
75 Map::iterator it = mMap.find(nodeHash);
76 if (it == mMap.end())
77 {
78 JLOG(mJournal.fatal()) << "Test filter missing node";
79 return std::nullopt;
80 }
81 return it->second;
82 }
83
86 };
87
88 boost::intrusive_ptr<Item>
90 {
91 Serializer s;
92 for (int d = 0; d < 3; ++d)
93 s.add32(ripple::rand_int<std::uint32_t>(r));
94 return make_shamapitem(s.getSHA512Half(), s.slice());
95 }
96
97 void
99 {
100 while (n--)
101 {
102 auto const result(t.addItem(
104 assert(result);
105 (void)result;
106 }
107 }
108
109 void
110 on_fetch(Map& map, SHAMapHash const& hash, Blob const& blob)
111 {
112 BEAST_EXPECT(sha512Half(makeSlice(blob)) == hash.as_uint256());
113 map.emplace(hash, blob);
114 }
115
116 void
117 run() override
118 {
119 using namespace beast::severities;
120 test::SuiteJournal journal("FetchPack_test", *this);
121
122 TestNodeFamily f(journal);
123 std::shared_ptr<Table> t1(std::make_shared<Table>(SHAMapType::FREE, f));
124
125 pass();
126
127 // beast::Random r;
128 // add_random_items (tableItems, *t1, r);
129 // std::shared_ptr <Table> t2 (t1->snapShot (true));
130 //
131 // add_random_items (tableItemsExtra, *t1, r);
132 // add_random_items (tableItemsExtra, *t2, r);
133
134 // turn t1 into t2
135 // Map map;
136 // t2->getFetchPack (t1.get(), true, 1000000, std::bind (
137 // &FetchPack_test::on_fetch, this, std::ref (map),
138 // std::placeholders::_1, std::placeholders::_2));
139 // t1->getFetchPack (nullptr, true, 1000000, std::bind (
140 // &FetchPack_test::on_fetch, this, std::ref (map),
141 // std::placeholders::_1, std::placeholders::_2));
142
143 // try to rebuild t2 from the fetch pack
144 // std::shared_ptr <Table> t3;
145 // try
146 // {
147 // TestFilter filter (map, beast::Journal());
148 //
149 // t3 = std::make_shared <Table> (SHAMapType::FREE,
150 // t2->getHash (),
151 // fullBelowCache);
152 //
153 // BEAST_EXPECT(t3->fetchRoot (t2->getHash (), &filter),
154 // "unable to get root");
155 //
156 // // everything should be in the pack, no hashes should be
157 // needed std::vector <uint256> hashes =
158 // t3->getNeededHashes(1, &filter);
159 // BEAST_EXPECT(hashes.empty(), "missing hashes");
160 //
161 // BEAST_EXPECT(t3->getHash () == t2->getHash (), "root
162 // hashes do not match"); BEAST_EXPECT(t3->deepCompare
163 // (*t2), "failed compare");
164 // }
165 // catch (std::exception const&)
166 // {
167 // fail ("unhandled exception");
168 // }
169 }
170};
171
172BEAST_DEFINE_TESTSUITE(FetchPack, shamap, ripple);
173
174} // namespace tests
175} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream fatal() const
Definition: Journal.h:351
A testsuite class.
Definition: suite.h:53
void pass()
Record a successful test condition.
Definition: suite.h:509
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:96
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
Definition: SHAMap.cpp:880
Slice slice() const noexcept
Definition: Serializer.h:66
uint256 getSHA512Half() const
Definition: Serializer.cpp:171
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)
A namespace for easy access to logging severity values.
Definition: Journal.h:29
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:243
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition: SHAMapItem.h:160
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:223
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