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
25
30
35
40
45
70template <class Rep, class Period, class Seq>
73 std::chrono::duration<Rep, Period> previousResolution,
74 bool previousAgree,
75 Seq ledgerSeq)
76{
77 XRPL_ASSERT(ledgerSeq != Seq{0}, "xrpl::getNextLedgerTimeResolution : valid ledger sequence");
78
79 using namespace std::chrono;
80 // Find the current resolution:
81 auto iter = std::find(
84 previousResolution);
85 XRPL_ASSERT(
87 "xrpl::getNextLedgerTimeResolution : found time resolution");
88
89 // This should never happen, but just as a precaution
91 return previousResolution;
92
93 // If we did not previously agree, we try to decrease the resolution to
94 // improve the chance that we will agree now.
95 if (!previousAgree && (ledgerSeq % Seq{kDecreaseLedgerTimeResolutionEvery} == Seq{0}))
96 {
98 return *iter;
99 }
100
101 // If we previously agreed, we try to increase the resolution to determine
102 // if we can continue to agree.
103 if (previousAgree && (ledgerSeq % Seq{kIncreaseLedgerTimeResolutionEvery} == Seq{0}))
104 {
106 return *iter;
107 }
108
109 return previousResolution;
110}
111
120template <class Clock, class Duration, class Rep, class Period>
125{
126 using time_point = decltype(closeTime);
127 if (closeTime == time_point{})
128 return closeTime;
129
130 closeTime += (closeResolution / 2);
131 return closeTime - (closeTime.time_since_epoch() % closeResolution);
132}
133
144template <class Clock, class Duration, class Rep, class Period>
150{
151 using namespace std::chrono_literals;
152 using time_point = decltype(closeTime);
153
154 if (closeTime == time_point{})
155 return closeTime;
156
157 return std::max<time_point>(roundCloseTime(closeTime, resolution), (priorCloseTime + 1s));
158}
159
160} // 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)