rippled
SkipList_test.cpp
1 //-----------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2015 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/app/ledger/Ledger.h>
21 #include <ripple/basics/Log.h>
22 #include <ripple/beast/unit_test.h>
23 #include <ripple/ledger/View.h>
24 #include <test/jtx.h>
25 
26 namespace ripple {
27 namespace test {
28 
29 class SkipList_test : public beast::unit_test::suite
30 {
31  void
33  {
34  jtx::Env env(*this);
36  {
37  Config config;
38  auto prev = std::make_shared<Ledger>(
40  config,
42  env.app().getNodeFamily());
43  history.push_back(prev);
44  for (auto i = 0; i < 1023; ++i)
45  {
46  auto next = std::make_shared<Ledger>(
47  *prev, env.app().timeKeeper().closeTime());
48  next->updateSkipList();
49  history.push_back(next);
50  prev = next;
51  }
52  }
53 
54  {
55  auto l = *(std::next(std::begin(history)));
56  BEAST_EXPECT((*std::begin(history))->info().seq < l->info().seq);
57  BEAST_EXPECT(
58  hashOfSeq(*l, l->info().seq + 1, env.journal) == boost::none);
59  BEAST_EXPECT(
60  hashOfSeq(*l, l->info().seq, env.journal) == l->info().hash);
61  BEAST_EXPECT(
62  hashOfSeq(*l, l->info().seq - 1, env.journal) ==
63  l->info().parentHash);
64  BEAST_EXPECT(
65  hashOfSeq(*history.back(), l->info().seq, env.journal) ==
66  boost::none);
67  }
68 
69  // ledger skip lists store up to the previous 256 hashes
70  for (auto i = history.crbegin(); i != history.crend(); i += 256)
71  {
72  for (auto n = i;
73  n != std::next(i, (*i)->info().seq - 256 > 1 ? 257 : 256);
74  ++n)
75  {
76  BEAST_EXPECT(
77  hashOfSeq(**i, (*n)->info().seq, env.journal) ==
78  (*n)->info().hash);
79  }
80 
81  // edge case accessing beyond 256
82  BEAST_EXPECT(
83  hashOfSeq(**i, (*i)->info().seq - 258, env.journal) ==
84  boost::none);
85  }
86 
87  // every 256th hash beyond the first 256 is stored
88  for (auto i = history.crbegin(); i != std::next(history.crend(), -512);
89  i += 256)
90  {
91  for (auto n = std::next(i, 512); n != history.crend(); n += 256)
92  {
93  BEAST_EXPECT(
94  hashOfSeq(**i, (*n)->info().seq, env.journal) ==
95  (*n)->info().hash);
96  }
97  }
98  }
99 
100  void
101  run() override
102  {
103  testSkipList();
104  }
105 };
106 
107 BEAST_DEFINE_TESTSUITE(SkipList, ledger, ripple);
108 
109 } // namespace test
110 } // namespace ripple
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
std::vector
STL class.
ripple::test::jtx::Env::journal
const beast::Journal journal
Definition: Env.h:143
ripple::test::SkipList_test::testSkipList
void testSkipList()
Definition: SkipList_test.cpp:32
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:240
std::vector::back
T back(T... args)
ripple::Application::timeKeeper
virtual TimeKeeper & timeKeeper()=0
ripple::test::SkipList_test::run
void run() override
Definition: SkipList_test.cpp:101
std::vector::push_back
T push_back(T... args)
ripple::Config
Definition: Config.h:67
ripple::TimeKeeper::closeTime
virtual time_point closeTime() const =0
Returns the close time, in network time.
std::vector::crend
T crend(T... args)
ripple::hashOfSeq
boost::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition: View.cpp:573
ripple::test::jtx::seq
Set the sequence number on a JTx.
Definition: seq.h:32
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::SkipList_test
Definition: SkipList_test.cpp:29
std::begin
T begin(T... args)
ripple::create_genesis
const create_genesis_t create_genesis
Definition: Ledger.cpp:57
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:115
std::vector::crbegin
T crbegin(T... args)
std::next
T next(T... args)