rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/chrono.h>
24#include <xrpl/beast/utility/Journal.h>
25
26#include <chrono>
27
28namespace ripple {
29
42
45
48
51
54
78template <class Rep, class Period, class Seq>
81 std::chrono::duration<Rep, Period> previousResolution,
82 bool previousAgree,
83 Seq ledgerSeq)
84{
85 XRPL_ASSERT(
86 ledgerSeq != Seq{0},
87 "ripple:getNextLedgerTimeResolution : valid ledger sequence");
88
89 using namespace std::chrono;
90 // Find the current resolution:
91 auto iter = std::find(
94 previousResolution);
95 XRPL_ASSERT(
97 "ripple:getNextLedgerTimeResolution : found time resolution");
98
99 // This should never happen, but just as a precaution
101 return previousResolution;
102
103 // If we did not previously agree, we try to decrease the resolution to
104 // improve the chance that we will agree now.
105 if (!previousAgree &&
106 (ledgerSeq % Seq{decreaseLedgerTimeResolutionEvery} == Seq{0}))
107 {
109 return *iter;
110 }
111
112 // If we previously agreed, we try to increase the resolution to determine
113 // if we can continue to agree.
114 if (previousAgree &&
115 (ledgerSeq % Seq{increaseLedgerTimeResolutionEvery} == Seq{0}))
116 {
118 return *iter;
119 }
120
121 return previousResolution;
122}
123
131template <class Clock, class Duration, class Rep, class Period>
136{
137 using time_point = decltype(closeTime);
138 if (closeTime == time_point{})
139 return closeTime;
140
141 closeTime += (closeResolution / 2);
142 return closeTime - (closeTime.time_since_epoch() % closeResolution);
143}
144
154template <class Clock, class Duration, class Rep, class Period>
160{
161 using namespace std::chrono_literals;
162 using time_point = decltype(closeTime);
163
164 if (closeTime == time_point{})
165 return closeTime;
166
168 roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
169}
170
171} // namespace ripple
172#endif
T begin(T... args)
T end(T... args)
T find(T... args)
T is_same_v
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.
auto constexpr decreaseLedgerTimeResolutionEvery
How often we decrease the close time resolution (in numbers of ledgers)
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.
auto constexpr ledgerGenesisTimeResolution
Close time resolution in genesis ledger.
std::chrono::seconds constexpr ledgerPossibleTimeResolutions[]
Possible ledger close time resolutions.
auto constexpr increaseLedgerTimeResolutionEvery
How often we increase the close time resolution (in numbers of ledgers)
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 time_since_epoch(T... args)