xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
ledger
LedgerTiming.h
1
#pragma once
2
3
#include <xrpl/beast/utility/instrumentation.h>
4
5
#include <
algorithm
>
6
#include <
chrono
>
7
#include <
iterator
>
8
9
namespace
xrpl
{
10
17
constexpr
std::chrono::seconds
kLedgerPossibleTimeResolutions
[] = {
18
std::chrono::seconds
{10},
19
std::chrono::seconds
{20},
20
std::chrono::seconds
{30},
21
std::chrono::seconds
{60},
22
std::chrono::seconds
{90},
23
std::chrono::seconds
{120}};
24
28
constexpr
auto
kLedgerDefaultTimeResolution
=
kLedgerPossibleTimeResolutions
[2];
29
33
constexpr
auto
kLedgerGenesisTimeResolution
=
kLedgerPossibleTimeResolutions
[0];
34
38
constexpr
auto
kIncreaseLedgerTimeResolutionEvery
= 8;
39
43
constexpr
auto
kDecreaseLedgerTimeResolutionEvery
= 1;
44
69
template
<
class
Rep,
class
Period,
class
Seq>
70
std::chrono::duration<Rep, Period>
71
getNextLedgerTimeResolution
(
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
(
81
std::begin
(
kLedgerPossibleTimeResolutions
),
82
std::end
(
kLedgerPossibleTimeResolutions
),
83
previousResolution);
84
XRPL_ASSERT(
85
iter !=
std::end
(
kLedgerPossibleTimeResolutions
),
86
"xrpl::getNextLedgerTimeResolution : found time resolution"
);
87
88
// This should never happen, but just as a precaution
89
if
(iter ==
std::end
(
kLedgerPossibleTimeResolutions
))
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
{
96
if
(++iter !=
std::end
(
kLedgerPossibleTimeResolutions
))
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
{
104
if
(iter-- !=
std::begin
(
kLedgerPossibleTimeResolutions
))
105
return
*iter;
106
}
107
108
return
previousResolution;
109
}
110
119
template
<
class
Clock,
class
Duration,
class
Rep,
class
Period>
120
std::chrono::time_point<Clock, Duration>
121
roundCloseTime
(
122
std::chrono::time_point<Clock, Duration>
closeTime,
123
std::chrono::duration<Rep, Period>
closeResolution)
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
143
template
<
class
Clock,
class
Duration,
class
Rep,
class
Period>
144
std::chrono::time_point<Clock, Duration>
145
effCloseTime
(
146
std::chrono::time_point<Clock, Duration>
closeTime,
147
std::chrono::duration<Rep, Period>
resolution,
148
std::chrono::time_point<Clock, Duration>
priorCloseTime)
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
algorithm
std::begin
T begin(T... args)
chrono
std::chrono::seconds
std::end
T end(T... args)
std::find
T find(T... args)
iterator
std::max
T max(T... args)
std::chrono
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::kDecreaseLedgerTimeResolutionEvery
constexpr auto kDecreaseLedgerTimeResolutionEvery
How often we decrease the close time resolution (in numbers of ledgers).
Definition
LedgerTiming.h:43
xrpl::effCloseTime
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.
Definition
LedgerTiming.h:145
xrpl::kIncreaseLedgerTimeResolutionEvery
constexpr auto kIncreaseLedgerTimeResolutionEvery
How often we increase the close time resolution (in numbers of ledgers).
Definition
LedgerTiming.h:38
xrpl::getNextLedgerTimeResolution
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.
Definition
LedgerTiming.h:71
xrpl::kLedgerPossibleTimeResolutions
constexpr std::chrono::seconds kLedgerPossibleTimeResolutions[]
Possible ledger close time resolutions.
Definition
LedgerTiming.h:17
xrpl::roundCloseTime
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.
Definition
LedgerTiming.h:121
xrpl::kLedgerGenesisTimeResolution
constexpr auto kLedgerGenesisTimeResolution
Close time resolution in genesis ledger.
Definition
LedgerTiming.h:33
xrpl::kLedgerDefaultTimeResolution
constexpr auto kLedgerDefaultTimeResolution
Initial resolution of ledger close time.
Definition
LedgerTiming.h:28
std::chrono::time_point
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
Generated by
1.17.0