rippled
Loading...
Searching...
No Matches
LedgerTiming_test.cpp
1#include <xrpld/consensus/LedgerTiming.h>
2
3#include <xrpl/beast/unit_test.h>
4
5namespace xrpl {
6namespace test {
7
9{
10 void
12 {
13 // helper to iteratively call into getNextLedgerTimeResolution
14 struct test_res
15 {
16 std::uint32_t decrease = 0;
17 std::uint32_t equal = 0;
18 std::uint32_t increase = 0;
19
20 static test_res
21 run(bool previousAgree, std::uint32_t rounds)
22 {
23 test_res res;
24 auto closeResolution = ledgerDefaultTimeResolution;
25 auto nextCloseResolution = closeResolution;
26 std::uint32_t round = 0;
27 do
28 {
29 nextCloseResolution = getNextLedgerTimeResolution(closeResolution, previousAgree, ++round);
30 if (nextCloseResolution < closeResolution)
31 ++res.decrease;
32 else if (nextCloseResolution > closeResolution)
33 ++res.increase;
34 else
35 ++res.equal;
36 std::swap(nextCloseResolution, closeResolution);
37 } while (round < rounds);
38 return res;
39 }
40 };
41
42 // If we never agree on close time, only can increase resolution
43 // until hit the max
44 auto decreases = test_res::run(false, 10);
45 BEAST_EXPECT(decreases.increase == 3);
46 BEAST_EXPECT(decreases.decrease == 0);
47 BEAST_EXPECT(decreases.equal == 7);
48
49 // If we always agree on close time, only can decrease resolution
50 // until hit the min
51 auto increases = test_res::run(false, 100);
52 BEAST_EXPECT(increases.increase == 3);
53 BEAST_EXPECT(increases.decrease == 0);
54 BEAST_EXPECT(increases.equal == 97);
55 }
56
57 void
59 {
60 using namespace std::chrono_literals;
61 // A closeTime equal to the epoch is not modified
62 using tp = NetClock::time_point;
63 tp def;
64 BEAST_EXPECT(def == roundCloseTime(def, 30s));
65
66 // Otherwise, the closeTime is rounded to the nearest
67 // rounding up on ties
68 BEAST_EXPECT(tp{0s} == roundCloseTime(tp{29s}, 60s));
69 BEAST_EXPECT(tp{30s} == roundCloseTime(tp{30s}, 1s));
70 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{31s}, 60s));
71 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{30s}, 60s));
72 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{59s}, 60s));
73 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{60s}, 60s));
74 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{61s}, 60s));
75 }
76
77 void
79 {
80 using namespace std::chrono_literals;
81 using tp = NetClock::time_point;
82 tp close = effCloseTime(tp{10s}, 30s, tp{0s});
83 BEAST_EXPECT(close == tp{1s});
84
85 close = effCloseTime(tp{16s}, 30s, tp{0s});
86 BEAST_EXPECT(close == tp{30s});
87
88 close = effCloseTime(tp{16s}, 30s, tp{30s});
89 BEAST_EXPECT(close == tp{31s});
90
91 close = effCloseTime(tp{16s}, 30s, tp{60s});
92 BEAST_EXPECT(close == tp{61s});
93
94 close = effCloseTime(tp{31s}, 30s, tp{0s});
95 BEAST_EXPECT(close == tp{30s});
96 }
97
98 void
99 run() override
100 {
104 }
105};
106
107BEAST_DEFINE_TESTSUITE(LedgerTiming, consensus, xrpl);
108} // namespace test
109} // namespace xrpl
A testsuite class.
Definition suite.h:52
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:6
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:322
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)