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 
54 
78 template <class Rep, class Period, class Seq>
81  std::chrono::duration<Rep, Period> previousResolution,
82  bool previousAgree,
83  Seq ledgerSeq)
84 {
85  assert(ledgerSeq != Seq{0});
86 
87  using namespace std::chrono;
88  // Find the current resolution:
89  auto iter = std::find(
92  previousResolution);
93  assert(iter != std::end(ledgerPossibleTimeResolutions));
94 
95  // This should never happen, but just as a precaution
97  return previousResolution;
98 
99  // If we did not previously agree, we try to decrease the resolution to
100  // improve the chance that we will agree now.
101  if (!previousAgree &&
102  (ledgerSeq % Seq{decreaseLedgerTimeResolutionEvery} == Seq{0}))
103  {
105  return *iter;
106  }
107 
108  // If we previously agreed, we try to increase the resolution to determine
109  // if we can continue to agree.
110  if (previousAgree &&
111  (ledgerSeq % Seq{increaseLedgerTimeResolutionEvery} == Seq{0}))
112  {
114  return *iter;
115  }
116 
117  return previousResolution;
118 }
119 
127 template <class Clock, class Duration, class Rep, class Period>
131  std::chrono::duration<Rep, Period> closeResolution)
132 {
133  using time_point = decltype(closeTime);
134  if (closeTime == time_point{})
135  return closeTime;
136 
137  closeTime += (closeResolution / 2);
138  return closeTime - (closeTime.time_since_epoch() % closeResolution);
139 }
140 
150 template <class Clock, class Duration, class Rep, class Period>
156 {
157  using namespace std::chrono_literals;
158  using time_point = decltype(closeTime);
159 
160  if (closeTime == time_point{})
161  return closeTime;
162 
163  return std::max<time_point>(
164  roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
165 }
166 
167 } // namespace ripple
168 #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:129
ripple::decreaseLedgerTimeResolutionEvery
constexpr auto decreaseLedgerTimeResolutionEvery
How often we decrease the close time resolution (in numbers of ledgers)
Definition: LedgerTiming.h:53
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:50
ripple::ledgerPossibleTimeResolutions
constexpr std::chrono::seconds ledgerPossibleTimeResolutions[]
Possible ledger close time resolutions.
Definition: LedgerTiming.h:35
ripple::ledgerGenesisTimeResolution
constexpr auto ledgerGenesisTimeResolution
Close time resolution in genesis ledger.
Definition: LedgerTiming.h:47
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:80
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:152
std::chrono