rippled
Loading...
Searching...
No Matches
LedgerTiming.h
1#ifndef XRPL_APP_LEDGER_LEDGERTIMING_H_INCLUDED
2#define XRPL_APP_LEDGER_LEDGERTIMING_H_INCLUDED
3
4#include <xrpl/basics/chrono.h>
5#include <xrpl/beast/utility/Journal.h>
6
7#include <chrono>
8
9namespace ripple {
10
23
26
29
32
35
59template <class Rep, class Period, class Seq>
62 std::chrono::duration<Rep, Period> previousResolution,
63 bool previousAgree,
64 Seq ledgerSeq)
65{
66 XRPL_ASSERT(
67 ledgerSeq != Seq{0},
68 "ripple:getNextLedgerTimeResolution : valid ledger sequence");
69
70 using namespace std::chrono;
71 // Find the current resolution:
72 auto iter = std::find(
75 previousResolution);
76 XRPL_ASSERT(
78 "ripple:getNextLedgerTimeResolution : found time resolution");
79
80 // This should never happen, but just as a precaution
82 return previousResolution;
83
84 // If we did not previously agree, we try to decrease the resolution to
85 // improve the chance that we will agree now.
86 if (!previousAgree &&
87 (ledgerSeq % Seq{decreaseLedgerTimeResolutionEvery} == Seq{0}))
88 {
90 return *iter;
91 }
92
93 // If we previously agreed, we try to increase the resolution to determine
94 // if we can continue to agree.
95 if (previousAgree &&
96 (ledgerSeq % Seq{increaseLedgerTimeResolutionEvery} == Seq{0}))
97 {
99 return *iter;
100 }
101
102 return previousResolution;
103}
104
112template <class Clock, class Duration, class Rep, class Period>
117{
118 using time_point = decltype(closeTime);
119 if (closeTime == time_point{})
120 return closeTime;
121
122 closeTime += (closeResolution / 2);
123 return closeTime - (closeTime.time_since_epoch() % closeResolution);
124}
125
135template <class Clock, class Duration, class Rep, class Period>
141{
142 using namespace std::chrono_literals;
143 using time_point = decltype(closeTime);
144
145 if (closeTime == time_point{})
146 return closeTime;
147
149 roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
150}
151
152} // namespace ripple
153#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:6
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)