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/Env.h>
21
22#include <xrpld/app/ledger/Ledger.h>
23
24#include <xrpl/beast/unit_test.h>
25#include <xrpl/ledger/View.h>
26
27namespace ripple {
28namespace test {
29
31{
32 void
34 {
35 jtx::Env env(*this);
37 {
38 Config config;
39 auto prev = std::make_shared<Ledger>(
41 config,
43 env.app().getNodeFamily());
44 history.push_back(prev);
45 for (auto i = 0; i < 1023; ++i)
46 {
47 auto next = std::make_shared<Ledger>(
48 *prev, env.app().timeKeeper().closeTime());
49 next->updateSkipList();
50 history.push_back(next);
51 prev = next;
52 }
53 }
54
55 {
56 auto l = *(std::next(std::begin(history)));
57 BEAST_EXPECT((*std::begin(history))->info().seq < l->info().seq);
58 BEAST_EXPECT(
59 !hashOfSeq(*l, l->info().seq + 1, env.journal).has_value());
60 BEAST_EXPECT(
61 hashOfSeq(*l, l->info().seq, env.journal) == l->info().hash);
62 BEAST_EXPECT(
63 hashOfSeq(*l, l->info().seq - 1, env.journal) ==
64 l->info().parentHash);
65 BEAST_EXPECT(!hashOfSeq(*history.back(), l->info().seq, env.journal)
66 .has_value());
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(!hashOfSeq(**i, (*i)->info().seq - 258, env.journal)
83 .has_value());
84 }
85
86 // every 256th hash beyond the first 256 is stored
87 for (auto i = history.crbegin(); i != std::next(history.crend(), -512);
88 i += 256)
89 {
90 for (auto n = std::next(i, 512); n != history.crend(); n += 256)
91 {
92 BEAST_EXPECT(
93 hashOfSeq(**i, (*n)->info().seq, env.journal) ==
94 (*n)->info().hash);
95 }
96 }
97 }
98
99 void
100 run() override
101 {
102 testSkipList();
103 }
104};
105
106BEAST_DEFINE_TESTSUITE(SkipList, ledger, ripple);
107
108} // namespace test
109} // namespace ripple
T back(T... args)
T begin(T... args)
A testsuite class.
Definition suite.h:55
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:121
Application & app()
Definition Env.h:261
beast::Journal const journal
Definition Env.h:162
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition View.cpp:958
create_genesis_t const create_genesis
Definition Ledger.cpp:51
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