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