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 <ripple/shamap/SHAMapSyncFilter.h>
29 #include <functional>
30 #include <stdexcept>
31 #include <test/shamap/common.h>
32 #include <test/unit_test/SuiteJournal.h>
33 
34 namespace ripple {
35 namespace tests {
36 
37 class FetchPack_test : public beast::unit_test::suite
38 {
39 public:
40  enum { tableItems = 100, tableItemsExtra = 20 };
41 
43  using Table = SHAMap;
44  using Item = SHAMapItem;
45 
46  struct Handler
47  {
48  void
49  operator()(std::uint32_t refNum) const
50  {
51  Throw<std::runtime_error>("missing node");
52  }
53  };
54 
56  {
57  TestFilter(Map& map, beast::Journal journal)
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 
172 BEAST_DEFINE_TESTSUITE(FetchPack, shamap, ripple);
173 
174 } // namespace tests
175 } // namespace ripple
beast::Journal::fatal
Stream fatal() const
Definition: Journal.h:339
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:241
std::shared_ptr
STL class.
ripple::tests::FetchPack_test::Handler::operator()
void operator()(std::uint32_t refNum) const
Definition: FetchPack_test.cpp:49
functional
ripple::SHAMapNodeType::tnACCOUNT_STATE
@ tnACCOUNT_STATE
ripple::tests::FetchPack_test::TestFilter::gotNode
void gotNode(bool fromFilter, SHAMapHash const &nodeHash, std::uint32_t ledgerSeq, Blob &&nodeData, SHAMapNodeType type) const override
Definition: FetchPack_test.cpp:63
ripple::tests::FetchPack_test::TestFilter
Definition: FetchPack_test.cpp:55
ripple::tests::FetchPack_test::TestFilter::mJournal
beast::Journal mJournal
Definition: FetchPack_test.cpp:85
ripple::make_shamapitem
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition: SHAMapItem.h:160
std::vector< unsigned char >
std::unordered_map::find
T find(T... args)
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:46
std::unordered_map::emplace
T emplace(T... args)
ripple::tests::TestNodeFamily
Definition: common.h:32
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:194
ripple::tests::FetchPack_test::tableItemsExtra
@ tableItemsExtra
Definition: FetchPack_test.cpp:40
ripple::SHAMap::addItem
bool addItem(SHAMapNodeType type, boost::intrusive_ptr< SHAMapItem const > item)
Definition: SHAMap.cpp:844
ripple::SHAMapHash
Definition: SHAMapHash.h:32
ripple::tests::FetchPack_test::Handler
Definition: FetchPack_test.cpp:46
stdexcept
ripple::tests::FetchPack_test::make_random_item
boost::intrusive_ptr< Item > make_random_item(beast::xor_shift_engine &r)
Definition: FetchPack_test.cpp:89
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:98
ripple::SHAMap
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition: SHAMap.h:95
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:63
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::tests::FetchPack_test::run
void run() override
Definition: FetchPack_test.cpp:117
ripple::test::SuiteJournal
Definition: SuiteJournal.h:88
ripple::Serializer
Definition: Serializer.h:39
ripple::tests::FetchPack_test
Definition: FetchPack_test.cpp:37
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:110
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:216
ripple::SHAMapType::FREE
@ FREE
std::optional
std::size_t
beast::detail::xor_shift_engine
Definition: xor_shift_engine.h:32
ripple::tests::FetchPack_test::TestFilter::getNode
std::optional< Blob > getNode(SHAMapHash const &nodeHash) const override
Definition: FetchPack_test.cpp:73
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: SHAMapHash.h:43
ripple::SHAMapSyncFilter
Definition: SHAMapSyncFilter.h:30
ripple::tests::FetchPack_test::TestFilter::TestFilter
TestFilter(Map &map, beast::Journal journal)
Definition: FetchPack_test.cpp:57
std::unordered_map
STL class.
ripple::tests::FetchPack_test::TestFilter::mMap
Map & mMap
Definition: FetchPack_test.cpp:84
ripple::tests::FetchPack_test::tableItems
@ tableItems
Definition: FetchPack_test.cpp:40