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>
61 std::chrono::duration<Rep, Period> previousResolution,
62 bool previousAgree,
63 Seq ledgerSeq)
64{
65 XRPL_ASSERT(ledgerSeq != Seq{0}, "ripple:getNextLedgerTimeResolution : valid ledger sequence");
66
67 using namespace std::chrono;
68 // Find the current resolution:
69 auto iter = std::find(
72 previousResolution);
73 XRPL_ASSERT(
75 "ripple:getNextLedgerTimeResolution : found time resolution");
76
77 // This should never happen, but just as a precaution
79 return previousResolution;
80
81 // If we did not previously agree, we try to decrease the resolution to
82 // improve the chance that we will agree now.
83 if (!previousAgree && (ledgerSeq % Seq{decreaseLedgerTimeResolutionEvery} == Seq{0}))
84 {
86 return *iter;
87 }
88
89 // If we previously agreed, we try to increase the resolution to determine
90 // if we can continue to agree.
91 if (previousAgree && (ledgerSeq % Seq{increaseLedgerTimeResolutionEvery} == Seq{0}))
92 {
94 return *iter;
95 }
96
97 return previousResolution;
98}
99
107template <class Clock, class Duration, class Rep, class Period>
112{
113 using time_point = decltype(closeTime);
114 if (closeTime == time_point{})
115 return closeTime;
116
117 closeTime += (closeResolution / 2);
118 return closeTime - (closeTime.time_since_epoch() % closeResolution);
119}
120
130template <class Clock, class Duration, class Rep, class Period>
136{
137 using namespace std::chrono_literals;
138 using time_point = decltype(closeTime);
139
140 if (closeTime == time_point{})
141 return closeTime;
142
143 return std::max<time_point>(roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
144}
145
146} // 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)