rippled
Loading...
Searching...
No Matches
LedgerTiming_test.cpp
1#include <xrpld/consensus/LedgerTiming.h>
2
3#include <xrpl/beast/unit_test.h>
4
5namespace ripple {
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(
30 closeResolution, previousAgree, ++round);
31 if (nextCloseResolution < closeResolution)
32 ++res.decrease;
33 else if (nextCloseResolution > closeResolution)
34 ++res.increase;
35 else
36 ++res.equal;
37 std::swap(nextCloseResolution, closeResolution);
38 } while (round < rounds);
39 return res;
40 }
41 };
42
43 // If we never agree on close time, only can increase resolution
44 // until hit the max
45 auto decreases = test_res::run(false, 10);
46 BEAST_EXPECT(decreases.increase == 3);
47 BEAST_EXPECT(decreases.decrease == 0);
48 BEAST_EXPECT(decreases.equal == 7);
49
50 // If we always agree on close time, only can decrease resolution
51 // until hit the min
52 auto increases = test_res::run(false, 100);
53 BEAST_EXPECT(increases.increase == 3);
54 BEAST_EXPECT(increases.decrease == 0);
55 BEAST_EXPECT(increases.equal == 97);
56 }
57
58 void
60 {
61 using namespace std::chrono_literals;
62 // A closeTime equal to the epoch is not modified
63 using tp = NetClock::time_point;
64 tp def;
65 BEAST_EXPECT(def == roundCloseTime(def, 30s));
66
67 // Otherwise, the closeTime is rounded to the nearest
68 // rounding up on ties
69 BEAST_EXPECT(tp{0s} == roundCloseTime(tp{29s}, 60s));
70 BEAST_EXPECT(tp{30s} == roundCloseTime(tp{30s}, 1s));
71 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{31s}, 60s));
72 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{30s}, 60s));
73 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{59s}, 60s));
74 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{60s}, 60s));
75 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{61s}, 60s));
76 }
77
78 void
80 {
81 using namespace std::chrono_literals;
82 using tp = NetClock::time_point;
83 tp close = effCloseTime(tp{10s}, 30s, tp{0s});
84 BEAST_EXPECT(close == tp{1s});
85
86 close = effCloseTime(tp{16s}, 30s, tp{0s});
87 BEAST_EXPECT(close == tp{30s});
88
89 close = effCloseTime(tp{16s}, 30s, tp{30s});
90 BEAST_EXPECT(close == tp{31s});
91
92 close = effCloseTime(tp{16s}, 30s, tp{60s});
93 BEAST_EXPECT(close == tp{61s});
94
95 close = effCloseTime(tp{31s}, 30s, tp{0s});
96 BEAST_EXPECT(close == tp{30s});
97 }
98
99 void
100 run() override
101 {
105 }
106};
107
108BEAST_DEFINE_TESTSUITE(LedgerTiming, consensus, ripple);
109} // namespace test
110} // namespace ripple
A testsuite class.
Definition suite.h:52
std::chrono::time_point< NetClock > time_point
Definition chrono.h:50
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.
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.
auto constexpr ledgerDefaultTimeResolution
Initial resolution of ledger close time.
int run(int argc, char **argv)
Definition Main.cpp:330
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.
T swap(T... args)