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).has_value());
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(!hashOfSeq(*history.back(), l->info().seq, env.journal)
65  .has_value());
66  }
67 
68  // ledger skip lists store up to the previous 256 hashes
69  for (auto i = history.crbegin(); i != history.crend(); i += 256)
70  {
71  for (auto n = i;
72  n != std::next(i, (*i)->info().seq - 256 > 1 ? 257 : 256);
73  ++n)
74  {
75  BEAST_EXPECT(
76  hashOfSeq(**i, (*n)->info().seq, env.journal) ==
77  (*n)->info().hash);
78  }
79 
80  // edge case accessing beyond 256
81  BEAST_EXPECT(!hashOfSeq(**i, (*i)->info().seq - 258, env.journal)
82  .has_value());
83  }
84 
85  // every 256th hash beyond the first 256 is stored
86  for (auto i = history.crbegin(); i != std::next(history.crend(), -512);
87  i += 256)
88  {
89  for (auto n = std::next(i, 512); n != history.crend(); n += 256)
90  {
91  BEAST_EXPECT(
92  hashOfSeq(**i, (*n)->info().seq, env.journal) ==
93  (*n)->info().hash);
94  }
95  }
96  }
97 
98  void
99  run() override
100  {
101  testSkipList();
102  }
103 };
104 
105 BEAST_DEFINE_TESTSUITE(SkipList, ledger, ripple);
106 
107 } // namespace test
108 } // namespace ripple
std::vector
STL class.
ripple::test::jtx::Env::journal
const beast::Journal journal
Definition: Env.h:144
ripple::test::SkipList_test::testSkipList
void testSkipList()
Definition: SkipList_test.cpp:32
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:241
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:99
std::vector::push_back
T push_back(T... args)
ripple::Config
Definition: Config.h:89
ripple::hashOfSeq
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition: View.cpp:644
ripple::TimeKeeper::closeTime
virtual time_point closeTime() const =0
Returns the close time, in network time.
std::vector::crend
T crend(T... args)
ripple::test::jtx::seq
Set the sequence number on a JTx.
Definition: seq.h:33
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:62
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
std::vector::crbegin
T crbegin(T... args)
std::next
T next(T... args)
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)