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