rippled
LedgerTiming.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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 #ifndef RIPPLE_APP_LEDGER_LEDGERTIMING_H_INCLUDED
21 #define RIPPLE_APP_LEDGER_LEDGERTIMING_H_INCLUDED
22 
23 #include <ripple/basics/chrono.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <chrono>
26 #include <cstdint>
27 
28 namespace ripple {
29 
42 
45 
48 
51 
75 template <class Rep, class Period, class Seq>
78  std::chrono::duration<Rep, Period> previousResolution,
79  bool previousAgree,
80  Seq ledgerSeq)
81 {
82  assert(ledgerSeq != Seq{0});
83 
84  using namespace std::chrono;
85  // Find the current resolution:
86  auto iter = std::find(
89  previousResolution);
90  assert(iter != std::end(ledgerPossibleTimeResolutions));
91 
92  // This should never happen, but just as a precaution
94  return previousResolution;
95 
96  // If we did not previously agree, we try to decrease the resolution to
97  // improve the chance that we will agree now.
98  if (!previousAgree &&
99  (ledgerSeq % Seq{decreaseLedgerTimeResolutionEvery} == Seq{0}))
100  {
102  return *iter;
103  }
104 
105  // If we previously agreed, we try to increase the resolution to determine
106  // if we can continue to agree.
107  if (previousAgree &&
108  (ledgerSeq % Seq{increaseLedgerTimeResolutionEvery} == Seq{0}))
109  {
111  return *iter;
112  }
113 
114  return previousResolution;
115 }
116 
124 template <class Clock, class Duration, class Rep, class Period>
128  std::chrono::duration<Rep, Period> closeResolution)
129 {
130  using time_point = decltype(closeTime);
131  if (closeTime == time_point{})
132  return closeTime;
133 
134  closeTime += (closeResolution / 2);
135  return closeTime - (closeTime.time_since_epoch() % closeResolution);
136 }
137 
147 template <class Clock, class Duration, class Rep, class Period>
153 {
154  using namespace std::chrono_literals;
155  using time_point = decltype(closeTime);
156 
157  if (closeTime == time_point{})
158  return closeTime;
159 
160  return std::max<time_point>(
161  roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
162 }
163 
164 } // namespace ripple
165 #endif
std::find
T find(T... args)
std::chrono::seconds
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:126
ripple::decreaseLedgerTimeResolutionEvery
constexpr auto decreaseLedgerTimeResolutionEvery
How often we decrease the close time resolution (in numbers of ledgers)
Definition: LedgerTiming.h:50
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
chrono
std::chrono::time_point
cstdint
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::begin
T begin(T... args)
ripple::increaseLedgerTimeResolutionEvery
constexpr auto increaseLedgerTimeResolutionEvery
How often we increase the close time resolution (in numbers of ledgers)
Definition: LedgerTiming.h:47
ripple::ledgerPossibleTimeResolutions
constexpr std::chrono::seconds ledgerPossibleTimeResolutions[]
Possible ledger close time resolutions.
Definition: LedgerTiming.h:35
std::end
T end(T... args)
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:77
ripple::ledgerDefaultTimeResolution
constexpr auto ledgerDefaultTimeResolution
Initial resolution of ledger close time.
Definition: LedgerTiming.h:44
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:149
std::chrono