rippled
Loading...
Searching...
No Matches
LedgerTiming.h
1#pragma once
2
3#include <xrpl/basics/chrono.h>
4#include <xrpl/beast/utility/Journal.h>
5
6#include <chrono>
7
8namespace xrpl {
9
22
25
28
31
34
58template <class Rep, class Period, class Seq>
60getNextLedgerTimeResolution(std::chrono::duration<Rep, Period> previousResolution, bool previousAgree, Seq ledgerSeq)
61{
62 XRPL_ASSERT(ledgerSeq != Seq{0}, "ripple:getNextLedgerTimeResolution : valid ledger sequence");
63
64 using namespace std::chrono;
65 // Find the current resolution:
66 auto iter = std::find(
68 XRPL_ASSERT(
69 iter != std::end(ledgerPossibleTimeResolutions), "ripple:getNextLedgerTimeResolution : found time resolution");
70
71 // This should never happen, but just as a precaution
73 return previousResolution;
74
75 // If we did not previously agree, we try to decrease the resolution to
76 // improve the chance that we will agree now.
77 if (!previousAgree && (ledgerSeq % Seq{decreaseLedgerTimeResolutionEvery} == Seq{0}))
78 {
80 return *iter;
81 }
82
83 // If we previously agreed, we try to increase the resolution to determine
84 // if we can continue to agree.
85 if (previousAgree && (ledgerSeq % Seq{increaseLedgerTimeResolutionEvery} == Seq{0}))
86 {
88 return *iter;
89 }
90
91 return previousResolution;
92}
93
101template <class Clock, class Duration, class Rep, class Period>
104{
105 using time_point = decltype(closeTime);
106 if (closeTime == time_point{})
107 return closeTime;
108
109 closeTime += (closeResolution / 2);
110 return closeTime - (closeTime.time_since_epoch() % closeResolution);
111}
112
122template <class Clock, class Duration, class Rep, class Period>
128{
129 using namespace std::chrono_literals;
130 using time_point = decltype(closeTime);
131
132 if (closeTime == time_point{})
133 return closeTime;
134
135 return std::max<time_point>(roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
136}
137
138} // namespace xrpl
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:5
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 increaseLedgerTimeResolutionEvery
How often we increase the close time resolution (in numbers of ledgers)
std::chrono::seconds constexpr ledgerPossibleTimeResolutions[]
Possible ledger close time resolutions.
auto constexpr ledgerDefaultTimeResolution
Initial resolution of ledger close time.
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.
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 ledgerGenesisTimeResolution
Close time resolution in genesis ledger.
auto constexpr decreaseLedgerTimeResolutionEvery
How often we decrease the close time resolution (in numbers of ledgers)
T time_since_epoch(T... args)