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 =
21 std::make_shared<Ledger>(create_genesis, config, std::vector<uint256>{}, env.app().getNodeFamily());
22 history.push_back(prev);
23 for (auto i = 0; i < 1023; ++i)
24 {
25 auto next = std::make_shared<Ledger>(*prev, env.app().timeKeeper().closeTime());
26 next->updateSkipList();
27 history.push_back(next);
28 prev = next;
29 }
30 }
31
32 {
33 auto l = *(std::next(std::begin(history)));
34 BEAST_EXPECT((*std::begin(history))->header().seq < l->header().seq);
35 BEAST_EXPECT(!hashOfSeq(*l, l->header().seq + 1, env.journal).has_value());
36 BEAST_EXPECT(hashOfSeq(*l, l->header().seq, env.journal) == l->header().hash);
37 BEAST_EXPECT(hashOfSeq(*l, l->header().seq - 1, env.journal) == l->header().parentHash);
38 BEAST_EXPECT(!hashOfSeq(*history.back(), l->header().seq, env.journal).has_value());
39 }
40
41 // ledger skip lists store up to the previous 256 hashes
42 for (auto i = history.crbegin(); i != history.crend(); i += 256)
43 {
44 for (auto n = i; n != std::next(i, (*i)->header().seq - 256 > 1 ? 257 : 256); ++n)
45 {
46 BEAST_EXPECT(hashOfSeq(**i, (*n)->header().seq, env.journal) == (*n)->header().hash);
47 }
48
49 // edge case accessing beyond 256
50 BEAST_EXPECT(!hashOfSeq(**i, (*i)->header().seq - 258, env.journal).has_value());
51 }
52
53 // every 256th hash beyond the first 256 is stored
54 for (auto i = history.crbegin(); i != std::next(history.crend(), -512); i += 256)
55 {
56 for (auto n = std::next(i, 512); n != history.crend(); n += 256)
57 {
58 BEAST_EXPECT(hashOfSeq(**i, (*n)->header().seq, env.journal) == (*n)->header().hash);
59 }
60 }
61 }
62
63 void
64 run() override
65 {
67 }
68};
69
70BEAST_DEFINE_TESTSUITE(SkipList, ledger, xrpl);
71
72} // namespace test
73} // namespace xrpl
T back(T... args)
T begin(T... args)
A testsuite class.
Definition suite.h:51
virtual TimeKeeper & timeKeeper()=0
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:55
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:119
Application & app()
Definition Env.h:251
beast::Journal const journal
Definition Env.h:160
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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:878
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:14