rippled
Loading...
Searching...
No Matches
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 <test/jtx.h>
21#include <xrpld/app/ledger/Ledger.h>
22#include <xrpld/ledger/View.h>
23#include <xrpl/basics/Log.h>
24#include <xrpl/beast/unit_test.h>
25
26namespace ripple {
27namespace test {
28
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
105BEAST_DEFINE_TESTSUITE(SkipList, ledger, ripple);
106
107} // namespace test
108} // namespace ripple
T back(T... args)
T begin(T... args)
A testsuite class.
Definition: suite.h:53
virtual TimeKeeper & timeKeeper()=0
time_point closeTime() const
Returns the predicted close time, in network time.
Definition: TimeKeeper.h:76
void run() override
Runs the suite.
A transaction testing environment.
Definition: Env.h:117
Application & app()
Definition: Env.h:255
beast::Journal const journal
Definition: Env.h:158
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition: View.cpp:798
create_genesis_t const create_genesis
Definition: Ledger.cpp:60
T next(T... args)
T push_back(T... args)
T crbegin(T... args)
T crend(T... args)
Set the sequence number on a JTx.
Definition: seq.h:34