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 
72  boost::optional<Blob>
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 boost::none;
80  }
81  return it->second;
82  }
83 
86  };
87 
90  {
91  Serializer s;
92  for (int d = 0; d < 3; ++d)
93  s.add32(ripple::rand_int<std::uint32_t>(r));
94  return std::make_shared<Item>(s.getSHA512Half(), s.peekData());
95  }
96 
97  void
99  {
100  while (n--)
101  {
103  auto const result(
104  t.addItem(SHAMapNodeType::tnACCOUNT_STATE, std::move(*item)));
105  assert(result);
106  (void)result;
107  }
108  }
109 
110  void
111  on_fetch(Map& map, SHAMapHash const& hash, Blob const& blob)
112  {
113  BEAST_EXPECT(sha512Half(makeSlice(blob)) == hash.as_uint256());
114  map.emplace(hash, blob);
115  }
116 
117  void
118  run() override
119  {
120  using namespace beast::severities;
121  test::SuiteJournal journal("FetchPack_test", *this);
122 
123  TestNodeFamily f(journal);
124  std::shared_ptr<Table> t1(std::make_shared<Table>(SHAMapType::FREE, f));
125 
126  pass();
127 
128  // beast::Random r;
129  // add_random_items (tableItems, *t1, r);
130  // std::shared_ptr <Table> t2 (t1->snapShot (true));
131  //
132  // add_random_items (tableItemsExtra, *t1, r);
133  // add_random_items (tableItemsExtra, *t2, r);
134 
135  // turn t1 into t2
136  // Map map;
137  // t2->getFetchPack (t1.get(), true, 1000000, std::bind (
138  // &FetchPack_test::on_fetch, this, std::ref (map),
139  // std::placeholders::_1, std::placeholders::_2));
140  // t1->getFetchPack (nullptr, true, 1000000, std::bind (
141  // &FetchPack_test::on_fetch, this, std::ref (map),
142  // std::placeholders::_1, std::placeholders::_2));
143 
144  // try to rebuild t2 from the fetch pack
145  // std::shared_ptr <Table> t3;
146  // try
147  // {
148  // TestFilter filter (map, beast::Journal());
149  //
150  // t3 = std::make_shared <Table> (SHAMapType::FREE,
151  // t2->getHash (),
152  // fullBelowCache);
153  //
154  // BEAST_EXPECT(t3->fetchRoot (t2->getHash (), &filter),
155  // "unable to get root");
156  //
157  // // everything should be in the pack, no hashes should be
158  // needed std::vector <uint256> hashes =
159  // t3->getNeededHashes(1, &filter);
160  // BEAST_EXPECT(hashes.empty(), "missing hashes");
161  //
162  // BEAST_EXPECT(t3->getHash () == t2->getHash (), "root
163  // hashes do not match"); BEAST_EXPECT(t3->deepCompare
164  // (*t2), "failed compare");
165  // }
166  // catch (std::exception const&)
167  // {
168  // fail ("unhandled exception");
169  // }
170  }
171 };
172 
173 BEAST_DEFINE_TESTSUITE(FetchPack, shamap, ripple);
174 
175 } // namespace tests
176 } // 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:240
ripple::tests::FetchPack_test::tableItems
@ tableItems
Definition: FetchPack_test.cpp:40
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::SHAMap::addItem
bool addItem(SHAMapNodeType type, SHAMapItem &&i)
Definition: SHAMap.cpp:772
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
std::vector< unsigned char >
std::unordered_map::find
T find(T... args)
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:126
ripple::tests::FetchPack_test::tableItemsExtra
@ tableItemsExtra
Definition: FetchPack_test.cpp:40
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:186
ripple::SHAMapHash
Definition: SHAMapTreeNode.h:47
ripple::tests::FetchPack_test::Handler
Definition: FetchPack_test.cpp:46
ripple::tests::FetchPack_test::TestFilter::getNode
boost::optional< Blob > getNode(SHAMapHash const &nodeHash) const override
Definition: FetchPack_test.cpp:73
stdexcept
ripple::tests::FetchPack_test::make_random_item
std::shared_ptr< Item > make_random_item(beast::xor_shift_engine &r)
Definition: FetchPack_test.cpp:89
ripple::SHAMapItem
Definition: SHAMapItem.h:35
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
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:118
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:111
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::Serializer::peekData
Blob const & peekData() const
Definition: Serializer.h:166
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:58
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