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