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