xrpld
Loading...
Searching...
No Matches
LedgerTiming.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4
5#include <algorithm>
6#include <chrono>
7#include <iterator>
8
9namespace xrpl {
10
24
29
34
39
44
69template <class Rep, class Period, class Seq>
72 std::chrono::duration<Rep, Period> previousResolution,
73 bool previousAgree,
74 Seq ledgerSeq)
75{
76 XRPL_ASSERT(ledgerSeq != Seq{0}, "xrpl::getNextLedgerTimeResolution : valid ledger sequence");
77
78 using namespace std::chrono;
79 // Find the current resolution:
80 auto iter = std::find(
83 previousResolution);
84 XRPL_ASSERT(
86 "xrpl::getNextLedgerTimeResolution : found time resolution");
87
88 // This should never happen, but just as a precaution
90 return previousResolution;
91
92 // If we did not previously agree, we try to decrease the resolution to
93 // improve the chance that we will agree now.
94 if (!previousAgree && (ledgerSeq % Seq{kDecreaseLedgerTimeResolutionEvery} == Seq{0}))
95 {
97 return *iter;
98 }
99
100 // If we previously agreed, we try to increase the resolution to determine
101 // if we can continue to agree.
102 if (previousAgree && (ledgerSeq % Seq{kIncreaseLedgerTimeResolutionEvery} == Seq{0}))
103 {
105 return *iter;
106 }
107
108 return previousResolution;
109}
110
119template <class Clock, class Duration, class Rep, class Period>
124{
125 using time_point = decltype(closeTime);
126 if (closeTime == time_point{})
127 return closeTime;
128
129 closeTime += (closeResolution / 2);
130 return closeTime - (closeTime.time_since_epoch() % closeResolution);
131}
132
143template <class Clock, class Duration, class Rep, class Period>
149{
150 using namespace std::chrono_literals;
151 using time_point = decltype(closeTime);
152
153 if (closeTime == time_point{})
154 return closeTime;
155
156 return std::max<time_point>(roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
157}
158
159} // namespace xrpl
T begin(T... args)
T end(T... args)
T find(T... args)
T max(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr auto kDecreaseLedgerTimeResolutionEvery
How often we decrease the close time resolution (in numbers of ledgers).
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.
constexpr auto kIncreaseLedgerTimeResolutionEvery
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.
constexpr std::chrono::seconds kLedgerPossibleTimeResolutions[]
Possible ledger close time resolutions.
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.
constexpr auto kLedgerGenesisTimeResolution
Close time resolution in genesis ledger.
constexpr auto kLedgerDefaultTimeResolution
Initial resolution of ledger close time.
T time_since_epoch(T... args)