rippled
Loading...
Searching...
No Matches
SkipList_test.cpp
1#include <test/jtx/Env.h>
2
3#include <xrpld/app/ledger/Ledger.h>
4
5#include <xrpl/beast/unit_test.h>
6#include <xrpl/ledger/View.h>
7
8namespace ripple {
9namespace test {
10
12{
13 void
15 {
16 jtx::Env env(*this);
18 {
19 Config config;
20 auto prev = std::make_shared<Ledger>(
22 config,
24 env.app().getNodeFamily());
25 history.push_back(prev);
26 for (auto i = 0; i < 1023; ++i)
27 {
28 auto next = std::make_shared<Ledger>(
29 *prev, env.app().timeKeeper().closeTime());
30 next->updateSkipList();
31 history.push_back(next);
32 prev = next;
33 }
34 }
35
36 {
37 auto l = *(std::next(std::begin(history)));
38 BEAST_EXPECT((*std::begin(history))->info().seq < l->info().seq);
39 BEAST_EXPECT(
40 !hashOfSeq(*l, l->info().seq + 1, env.journal).has_value());
41 BEAST_EXPECT(
42 hashOfSeq(*l, l->info().seq, env.journal) == l->info().hash);
43 BEAST_EXPECT(
44 hashOfSeq(*l, l->info().seq - 1, env.journal) ==
45 l->info().parentHash);
46 BEAST_EXPECT(!hashOfSeq(*history.back(), l->info().seq, env.journal)
47 .has_value());
48 }
49
50 // ledger skip lists store up to the previous 256 hashes
51 for (auto i = history.crbegin(); i != history.crend(); i += 256)
52 {
53 for (auto n = i;
54 n != std::next(i, (*i)->info().seq - 256 > 1 ? 257 : 256);
55 ++n)
56 {
57 BEAST_EXPECT(
58 hashOfSeq(**i, (*n)->info().seq, env.journal) ==
59 (*n)->info().hash);
60 }
61
62 // edge case accessing beyond 256
63 BEAST_EXPECT(!hashOfSeq(**i, (*i)->info().seq - 258, env.journal)
64 .has_value());
65 }
66
67 // every 256th hash beyond the first 256 is stored
68 for (auto i = history.crbegin(); i != std::next(history.crend(), -512);
69 i += 256)
70 {
71 for (auto n = std::next(i, 512); n != history.crend(); n += 256)
72 {
73 BEAST_EXPECT(
74 hashOfSeq(**i, (*n)->info().seq, env.journal) ==
75 (*n)->info().hash);
76 }
77 }
78 }
79
80 void
81 run() override
82 {
84 }
85};
86
87BEAST_DEFINE_TESTSUITE(SkipList, ledger, ripple);
88
89} // namespace test
90} // namespace ripple
T back(T... args)
T begin(T... args)
A testsuite class.
Definition suite.h:52
virtual TimeKeeper & timeKeeper()=0
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:57
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:102
Application & app()
Definition Env.h:242
beast::Journal const journal
Definition Env.h:143
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition View.cpp:942
create_genesis_t const create_genesis
Definition Ledger.cpp:32
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:15