rippled
Loading...
Searching...
No Matches
LedgerTiming_test.cpp
1#include <xrpl/beast/unit_test.h>
2#include <xrpl/ledger/LedgerTiming.h>
3
4namespace xrpl {
5namespace test {
6
8{
9 void
11 {
12 // helper to iteratively call into getNextLedgerTimeResolution
13 struct test_res
14 {
15 std::uint32_t decrease = 0;
16 std::uint32_t equal = 0;
17 std::uint32_t increase = 0;
18
19 static test_res
20 run(bool previousAgree, std::uint32_t rounds)
21 {
22 test_res res;
23 auto closeResolution = ledgerDefaultTimeResolution;
24 auto nextCloseResolution = closeResolution;
25 std::uint32_t round = 0;
26 do
27 {
28 nextCloseResolution =
29 getNextLedgerTimeResolution(closeResolution, previousAgree, ++round);
30 if (nextCloseResolution < closeResolution)
31 {
32 ++res.decrease;
33 }
34 else if (nextCloseResolution > closeResolution)
35 {
36 ++res.increase;
37 }
38 else
39 {
40 ++res.equal;
41 }
42 std::swap(nextCloseResolution, closeResolution);
43 } while (round < rounds);
44 return res;
45 }
46 };
47
48 // If we never agree on close time, only can increase resolution
49 // until hit the max
50 auto decreases = test_res::run(false, 10);
51 BEAST_EXPECT(decreases.increase == 3);
52 BEAST_EXPECT(decreases.decrease == 0);
53 BEAST_EXPECT(decreases.equal == 7);
54
55 // If we always agree on close time, only can decrease resolution
56 // until hit the min
57 auto increases = test_res::run(false, 100);
58 BEAST_EXPECT(increases.increase == 3);
59 BEAST_EXPECT(increases.decrease == 0);
60 BEAST_EXPECT(increases.equal == 97);
61 }
62
63 void
65 {
66 using namespace std::chrono_literals;
67 // A closeTime equal to the epoch is not modified
68 using tp = NetClock::time_point;
69 tp const def;
70 BEAST_EXPECT(def == roundCloseTime(def, 30s));
71
72 // Otherwise, the closeTime is rounded to the nearest
73 // rounding up on ties
74 BEAST_EXPECT(tp{0s} == roundCloseTime(tp{29s}, 60s));
75 BEAST_EXPECT(tp{30s} == roundCloseTime(tp{30s}, 1s));
76 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{31s}, 60s));
77 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{30s}, 60s));
78 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{59s}, 60s));
79 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{60s}, 60s));
80 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{61s}, 60s));
81 }
82
83 void
85 {
86 using namespace std::chrono_literals;
87 using tp = NetClock::time_point;
88 tp close = effCloseTime(tp{10s}, 30s, tp{0s});
89 BEAST_EXPECT(close == tp{1s});
90
91 close = effCloseTime(tp{16s}, 30s, tp{0s});
92 BEAST_EXPECT(close == tp{30s});
93
94 close = effCloseTime(tp{16s}, 30s, tp{30s});
95 BEAST_EXPECT(close == tp{31s});
96
97 close = effCloseTime(tp{16s}, 30s, tp{60s});
98 BEAST_EXPECT(close == tp{61s});
99
100 close = effCloseTime(tp{31s}, 30s, tp{0s});
101 BEAST_EXPECT(close == tp{30s});
102 }
103
104 void
105 run() override
106 {
110 }
111};
112
113BEAST_DEFINE_TESTSUITE(LedgerTiming, consensus, xrpl);
114} // namespace test
115} // namespace xrpl
A testsuite class.
Definition suite.h:51
std::chrono::time_point< NetClock > time_point
Definition chrono.h:46
void run() override
Runs the suite.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::chrono::time_point< Clock, Duration > effCloseTime(std::chrono::time_point< Clock, Duration > closeTime, std::chrono::duration< Rep, Period > resolution, std::chrono::time_point< Clock, Duration > priorCloseTime)
Calculate the effective ledger close time.
int run(int argc, char **argv)
Definition Main.cpp:332
auto constexpr ledgerDefaultTimeResolution
Initial resolution of ledger close time.
std::chrono::duration< Rep, Period > getNextLedgerTimeResolution(std::chrono::duration< Rep, Period > previousResolution, bool previousAgree, Seq ledgerSeq)
Calculates the close time resolution for the specified ledger.
std::chrono::time_point< Clock, Duration > roundCloseTime(std::chrono::time_point< Clock, Duration > closeTime, std::chrono::duration< Rep, Period > closeResolution)
Calculates the close time for a ledger, given a close time resolution.
T swap(T... args)