rippled
Loading...
Searching...
No Matches
LedgerTiming_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2016 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/consensus/LedgerTiming.h>
21#include <xrpl/beast/unit_test.h>
22
23namespace ripple {
24namespace test {
25
27{
28 void
30 {
31 // helper to iteratively call into getNextLedgerTimeResolution
32 struct test_res
33 {
34 std::uint32_t decrease = 0;
35 std::uint32_t equal = 0;
36 std::uint32_t increase = 0;
37
38 static test_res
39 run(bool previousAgree, std::uint32_t rounds)
40 {
41 test_res res;
42 auto closeResolution = ledgerDefaultTimeResolution;
43 auto nextCloseResolution = closeResolution;
44 std::uint32_t round = 0;
45 do
46 {
47 nextCloseResolution = getNextLedgerTimeResolution(
48 closeResolution, previousAgree, ++round);
49 if (nextCloseResolution < closeResolution)
50 ++res.decrease;
51 else if (nextCloseResolution > closeResolution)
52 ++res.increase;
53 else
54 ++res.equal;
55 std::swap(nextCloseResolution, closeResolution);
56 } while (round < rounds);
57 return res;
58 }
59 };
60
61 // If we never agree on close time, only can increase resolution
62 // until hit the max
63 auto decreases = test_res::run(false, 10);
64 BEAST_EXPECT(decreases.increase == 3);
65 BEAST_EXPECT(decreases.decrease == 0);
66 BEAST_EXPECT(decreases.equal == 7);
67
68 // If we always agree on close time, only can decrease resolution
69 // until hit the min
70 auto increases = test_res::run(false, 100);
71 BEAST_EXPECT(increases.increase == 3);
72 BEAST_EXPECT(increases.decrease == 0);
73 BEAST_EXPECT(increases.equal == 97);
74 }
75
76 void
78 {
79 using namespace std::chrono_literals;
80 // A closeTime equal to the epoch is not modified
81 using tp = NetClock::time_point;
82 tp def;
83 BEAST_EXPECT(def == roundCloseTime(def, 30s));
84
85 // Otherwise, the closeTime is rounded to the nearest
86 // rounding up on ties
87 BEAST_EXPECT(tp{0s} == roundCloseTime(tp{29s}, 60s));
88 BEAST_EXPECT(tp{30s} == roundCloseTime(tp{30s}, 1s));
89 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{31s}, 60s));
90 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{30s}, 60s));
91 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{59s}, 60s));
92 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{60s}, 60s));
93 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{61s}, 60s));
94 }
95
96 void
98 {
99 using namespace std::chrono_literals;
100 using tp = NetClock::time_point;
101 tp close = effCloseTime(tp{10s}, 30s, tp{0s});
102 BEAST_EXPECT(close == tp{1s});
103
104 close = effCloseTime(tp{16s}, 30s, tp{0s});
105 BEAST_EXPECT(close == tp{30s});
106
107 close = effCloseTime(tp{16s}, 30s, tp{30s});
108 BEAST_EXPECT(close == tp{31s});
109
110 close = effCloseTime(tp{16s}, 30s, tp{60s});
111 BEAST_EXPECT(close == tp{61s});
112
113 close = effCloseTime(tp{31s}, 30s, tp{0s});
114 BEAST_EXPECT(close == tp{30s});
115 }
116
117 void
118 run() override
119 {
123 }
124};
125
126BEAST_DEFINE_TESTSUITE(LedgerTiming, consensus, ripple);
127} // namespace test
128} // namespace ripple
A testsuite class.
Definition: suite.h:55
std::chrono::time_point< NetClock > time_point
Definition: chrono.h:69
void run() override
Runs the suite.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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.
Definition: LedgerTiming.h:156
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.
Definition: LedgerTiming.h:133
auto constexpr ledgerDefaultTimeResolution
Initial resolution of ledger close time.
Definition: LedgerTiming.h:44
int run(int argc, char **argv)
Definition: Main.cpp:341
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.
Definition: LedgerTiming.h:80
T swap(T... args)