rippled
Loading...
Searching...
No Matches
SkipList_test.cpp
1#include <test/jtx/Env.h>
2
3#include <xrpl/beast/unit_test.h>
4#include <xrpl/ledger/Ledger.h>
5#include <xrpl/ledger/View.h>
6
7namespace xrpl {
8namespace test {
9
11{
12 void
14 {
15 jtx::Env env(*this);
17 {
18 Config const config;
19 auto prev = std::make_shared<Ledger>(
21 Rules{config.features},
22 config.FEES.toFees(),
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>(*prev, env.app().getTimeKeeper().closeTime());
29 next->updateSkipList();
30 history.push_back(next);
31 prev = next;
32 }
33 }
34
35 {
36 auto l = *(std::next(std::begin(history)));
37 BEAST_EXPECT((*std::begin(history))->header().seq < l->header().seq);
38 BEAST_EXPECT(!hashOfSeq(*l, l->header().seq + 1, env.journal).has_value());
39 BEAST_EXPECT(hashOfSeq(*l, l->header().seq, env.journal) == l->header().hash);
40 BEAST_EXPECT(hashOfSeq(*l, l->header().seq - 1, env.journal) == l->header().parentHash);
41 BEAST_EXPECT(!hashOfSeq(*history.back(), l->header().seq, env.journal).has_value());
42 }
43
44 // ledger skip lists store up to the previous 256 hashes
45 for (auto i = history.crbegin(); i != history.crend(); i += 256)
46 {
47 for (auto n = i; n != std::next(i, (*i)->header().seq - 256 > 1 ? 257 : 256); ++n)
48 {
49 BEAST_EXPECT(
50 hashOfSeq(**i, (*n)->header().seq, env.journal) == (*n)->header().hash);
51 }
52
53 // edge case accessing beyond 256
54 BEAST_EXPECT(!hashOfSeq(**i, (*i)->header().seq - 258, env.journal).has_value());
55 }
56
57 // every 256th hash beyond the first 256 is stored
58 for (auto i = history.crbegin(); i != std::next(history.crend(), -512); i += 256)
59 {
60 for (auto n = std::next(i, 512); n != history.crend(); n += 256)
61 {
62 BEAST_EXPECT(
63 hashOfSeq(**i, (*n)->header().seq, env.journal) == (*n)->header().hash);
64 }
65 }
66 }
67
68 void
69 run() override
70 {
72 }
73};
74
75BEAST_DEFINE_TESTSUITE(SkipList, ledger, xrpl);
76
77} // namespace test
78} // namespace xrpl
T back(T... args)
T begin(T... args)
A testsuite class.
Definition suite.h:51
std::unordered_set< uint256, beast::uhash<> > features
Definition Config.h:261
FeeSetup FEES
Definition Config.h:189
Rules controlling protocol behavior.
Definition Rules.h:18
virtual TimeKeeper & getTimeKeeper()=0
time_point closeTime() const
Returns the predicted close time, in network time.
Definition TimeKeeper.h:56
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:122
Application & app()
Definition Env.h:259
beast::Journal const journal
Definition Env.h:163
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
std::optional< uint256 > hashOfSeq(ReadView const &ledger, LedgerIndex seq, beast::Journal journal)
Return the hash of a ledger by sequence.
Definition View.cpp:232
T next(T... args)
T push_back(T... args)
T crbegin(T... args)
T crend(T... args)
Fees toFees() const
Convert to a Fees object for use with Ledger construction.
Definition Config.h:64
Set the sequence number on a JTx.
Definition seq.h:14