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
22#include <xrpl/beast/unit_test.h>
23
24namespace ripple {
25namespace test {
26
28{
29 void
31 {
32 // helper to iteratively call into getNextLedgerTimeResolution
33 struct test_res
34 {
35 std::uint32_t decrease = 0;
36 std::uint32_t equal = 0;
37 std::uint32_t increase = 0;
38
39 static test_res
40 run(bool previousAgree, std::uint32_t rounds)
41 {
42 test_res res;
43 auto closeResolution = ledgerDefaultTimeResolution;
44 auto nextCloseResolution = closeResolution;
45 std::uint32_t round = 0;
46 do
47 {
48 nextCloseResolution = getNextLedgerTimeResolution(
49 closeResolution, previousAgree, ++round);
50 if (nextCloseResolution < closeResolution)
51 ++res.decrease;
52 else if (nextCloseResolution > closeResolution)
53 ++res.increase;
54 else
55 ++res.equal;
56 std::swap(nextCloseResolution, closeResolution);
57 } while (round < rounds);
58 return res;
59 }
60 };
61
62 // If we never agree on close time, only can increase resolution
63 // until hit the max
64 auto decreases = test_res::run(false, 10);
65 BEAST_EXPECT(decreases.increase == 3);
66 BEAST_EXPECT(decreases.decrease == 0);
67 BEAST_EXPECT(decreases.equal == 7);
68
69 // If we always agree on close time, only can decrease resolution
70 // until hit the min
71 auto increases = test_res::run(false, 100);
72 BEAST_EXPECT(increases.increase == 3);
73 BEAST_EXPECT(increases.decrease == 0);
74 BEAST_EXPECT(increases.equal == 97);
75 }
76
77 void
79 {
80 using namespace std::chrono_literals;
81 // A closeTime equal to the epoch is not modified
82 using tp = NetClock::time_point;
83 tp def;
84 BEAST_EXPECT(def == roundCloseTime(def, 30s));
85
86 // Otherwise, the closeTime is rounded to the nearest
87 // rounding up on ties
88 BEAST_EXPECT(tp{0s} == roundCloseTime(tp{29s}, 60s));
89 BEAST_EXPECT(tp{30s} == roundCloseTime(tp{30s}, 1s));
90 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{31s}, 60s));
91 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{30s}, 60s));
92 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{59s}, 60s));
93 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{60s}, 60s));
94 BEAST_EXPECT(tp{60s} == roundCloseTime(tp{61s}, 60s));
95 }
96
97 void
99 {
100 using namespace std::chrono_literals;
101 using tp = NetClock::time_point;
102 tp close = effCloseTime(tp{10s}, 30s, tp{0s});
103 BEAST_EXPECT(close == tp{1s});
104
105 close = effCloseTime(tp{16s}, 30s, tp{0s});
106 BEAST_EXPECT(close == tp{30s});
107
108 close = effCloseTime(tp{16s}, 30s, tp{30s});
109 BEAST_EXPECT(close == tp{31s});
110
111 close = effCloseTime(tp{16s}, 30s, tp{60s});
112 BEAST_EXPECT(close == tp{61s});
113
114 close = effCloseTime(tp{31s}, 30s, tp{0s});
115 BEAST_EXPECT(close == tp{30s});
116 }
117
118 void
119 run() override
120 {
124 }
125};
126
127BEAST_DEFINE_TESTSUITE(LedgerTiming, consensus, ripple);
128} // namespace test
129} // 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:25
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:349
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)