rippled
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 #include <ripple/beast/unit_test.h>
20 #include <ripple/consensus/LedgerTiming.h>
21 
22 namespace ripple {
23 namespace test {
24 
25 class LedgerTiming_test : public beast::unit_test::suite
26 {
27  void
29  {
30  // helper to iteratively call into getNextLedgerTimeResolution
31  struct test_res
32  {
33  std::uint32_t decrease = 0;
34  std::uint32_t equal = 0;
36 
37  static test_res
38  run(bool previousAgree, std::uint32_t rounds)
39  {
40  test_res res;
41  auto closeResolution = ledgerDefaultTimeResolution;
42  auto nextCloseResolution = closeResolution;
43  std::uint32_t round = 0;
44  do
45  {
46  nextCloseResolution = getNextLedgerTimeResolution(
47  closeResolution, previousAgree, ++round);
48  if (nextCloseResolution < closeResolution)
49  ++res.decrease;
50  else if (nextCloseResolution > closeResolution)
51  ++res.increase;
52  else
53  ++res.equal;
54  std::swap(nextCloseResolution, closeResolution);
55  } while (round < rounds);
56  return res;
57  }
58  };
59 
60  // If we never agree on close time, only can increase resolution
61  // until hit the max
62  auto decreases = test_res::run(false, 10);
63  BEAST_EXPECT(decreases.increase == 3);
64  BEAST_EXPECT(decreases.decrease == 0);
65  BEAST_EXPECT(decreases.equal == 7);
66 
67  // If we always agree on close time, only can decrease resolution
68  // until hit the min
69  auto increases = test_res::run(false, 100);
70  BEAST_EXPECT(increases.increase == 3);
71  BEAST_EXPECT(increases.decrease == 0);
72  BEAST_EXPECT(increases.equal == 97);
73  }
74 
75  void
77  {
78  using namespace std::chrono_literals;
79  // A closeTime equal to the epoch is not modified
80  using tp = NetClock::time_point;
81  tp def;
82  BEAST_EXPECT(def == roundCloseTime(def, 30s));
83 
84  // Otherwise, the closeTime is rounded to the nearest
85  // rounding up on ties
86  BEAST_EXPECT(tp{0s} == roundCloseTime(tp{29s}, 60s));
87  BEAST_EXPECT(tp{30s} == roundCloseTime(tp{30s}, 1s));
88  BEAST_EXPECT(tp{60s} == roundCloseTime(tp{31s}, 60s));
89  BEAST_EXPECT(tp{60s} == roundCloseTime(tp{30s}, 60s));
90  BEAST_EXPECT(tp{60s} == roundCloseTime(tp{59s}, 60s));
91  BEAST_EXPECT(tp{60s} == roundCloseTime(tp{60s}, 60s));
92  BEAST_EXPECT(tp{60s} == roundCloseTime(tp{61s}, 60s));
93  }
94 
95  void
97  {
98  using namespace std::chrono_literals;
99  using tp = NetClock::time_point;
100  tp close = effCloseTime(tp{10s}, 30s, tp{0s});
101  BEAST_EXPECT(close == tp{1s});
102 
103  close = effCloseTime(tp{16s}, 30s, tp{0s});
104  BEAST_EXPECT(close == tp{30s});
105 
106  close = effCloseTime(tp{16s}, 30s, tp{30s});
107  BEAST_EXPECT(close == tp{31s});
108 
109  close = effCloseTime(tp{16s}, 30s, tp{60s});
110  BEAST_EXPECT(close == tp{61s});
111 
112  close = effCloseTime(tp{31s}, 30s, tp{0s});
113  BEAST_EXPECT(close == tp{30s});
114  }
115 
116  void
117  run() override
118  {
122  }
123 };
124 
125 BEAST_DEFINE_TESTSUITE(LedgerTiming, consensus, ripple);
126 } // namespace test
127 } // namespace ripple
ripple::test::LedgerTiming_test::run
void run() override
Definition: LedgerTiming_test.cpp:117
ripple::test::LedgerTiming_test
Definition: LedgerTiming_test.cpp:25
ripple::roundCloseTime
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:129
ripple::increase
static FeeLevel64 increase(FeeLevel64 level, std::uint32_t increasePercent)
Definition: TxQ.cpp:78
std::uint32_t
std::swap
T swap(T... args)
ripple::test::equal
bool equal(STAmount const &sa1, STAmount const &sa2)
Definition: Path_test.cpp:126
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::LedgerTiming_test::testEffCloseTime
void testEffCloseTime()
Definition: LedgerTiming_test.cpp:96
ripple::test::LedgerTiming_test::testRoundCloseTime
void testRoundCloseTime()
Definition: LedgerTiming_test.cpp:76
ripple::getNextLedgerTimeResolution
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
ripple::ledgerDefaultTimeResolution
constexpr auto ledgerDefaultTimeResolution
Initial resolution of ledger close time.
Definition: LedgerTiming.h:44
ripple::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition: chrono.h:56
ripple::effCloseTime
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:152
ripple::test::LedgerTiming_test::testGetNextLedgerTimeResolution
void testGetNextLedgerTimeResolution()
Definition: LedgerTiming_test.cpp:28
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)