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