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 xrpl {
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(
39 (*std::begin(history))->header().seq < l->header().seq);
40 BEAST_EXPECT(
41 !hashOfSeq(*l, l->header().seq + 1, env.journal).has_value());
42 BEAST_EXPECT(
43 hashOfSeq(*l, l->header().seq, env.journal) ==
44 l->header().hash);
45 BEAST_EXPECT(
46 hashOfSeq(*l, l->header().seq - 1, env.journal) ==
47 l->header().parentHash);
48 BEAST_EXPECT(
49 !hashOfSeq(*history.back(), l->header().seq, env.journal)
50 .has_value());
51 }
52
53 // ledger skip lists store up to the previous 256 hashes
54 for (auto i = history.crbegin(); i != history.crend(); i += 256)
55 {
56 for (auto n = i;
57 n != std::next(i, (*i)->header().seq - 256 > 1 ? 257 : 256);
58 ++n)
59 {
60 BEAST_EXPECT(
61 hashOfSeq(**i, (*n)->header().seq, env.journal) ==
62 (*n)->header().hash);
63 }
64
65 // edge case accessing beyond 256
66 BEAST_EXPECT(!hashOfSeq(**i, (*i)->header().seq - 258, env.journal)
67 .has_value());
68 }
69
70 // every 256th hash beyond the first 256 is stored
71 for (auto i = history.crbegin(); i != std::next(history.crend(), -512);
72 i += 256)
73 {
74 for (auto n = std::next(i, 512); n != history.crend(); n += 256)
75 {
76 BEAST_EXPECT(
77 hashOfSeq(**i, (*n)->header().seq, env.journal) ==
78 (*n)->header().hash);
79 }
80 }
81 }
82
83 void
84 run() override
85 {
87 }
88};
89
90BEAST_DEFINE_TESTSUITE(SkipList, ledger, xrpl);
91
92} // namespace test
93} // namespace xrpl
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:244
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
create_genesis_t const create_genesis
Definition Ledger.cpp:32
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition View.cpp:1063
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