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
};
25
29
constexpr
auto
kLedgerDefaultTimeResolution
=
kLedgerPossibleTimeResolutions
[2];
30
34
constexpr
auto
kLedgerGenesisTimeResolution
=
kLedgerPossibleTimeResolutions
[0];
35
39
constexpr
auto
kIncreaseLedgerTimeResolutionEvery
= 8;
40
44
constexpr
auto
kDecreaseLedgerTimeResolutionEvery
= 1;
45
70
template
<
class
Rep,
class
Period,
class
Seq>
71
std::chrono::duration<Rep, Period>
72
getNextLedgerTimeResolution
(
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
(
82
std::begin
(
kLedgerPossibleTimeResolutions
),
83
std::end
(
kLedgerPossibleTimeResolutions
),
84
previousResolution);
85
XRPL_ASSERT(
86
iter !=
std::end
(
kLedgerPossibleTimeResolutions
),
87
"xrpl::getNextLedgerTimeResolution : found time resolution"
);
88
89
// This should never happen, but just as a precaution
90
if
(iter ==
std::end
(
kLedgerPossibleTimeResolutions
))
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
{
97
if
(++iter !=
std::end
(
kLedgerPossibleTimeResolutions
))
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
{
105
if
(iter-- !=
std::begin
(
kLedgerPossibleTimeResolutions
))
106
return
*iter;
107
}
108
109
return
previousResolution;
110
}
111
120
template
<
class
Clock,
class
Duration,
class
Rep,
class
Period>
121
std::chrono::time_point<Clock, Duration>
122
roundCloseTime
(
123
std::chrono::time_point<Clock, Duration>
closeTime,
124
std::chrono::duration<Rep, Period>
closeResolution)
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
144
template
<
class
Clock,
class
Duration,
class
Rep,
class
Period>
145
std::chrono::time_point<Clock, Duration>
146
effCloseTime
(
147
std::chrono::time_point<Clock, Duration>
closeTime,
148
std::chrono::duration<Rep, Period>
resolution,
149
std::chrono::time_point<Clock, Duration>
priorCloseTime)
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
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:44
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:146
xrpl::kIncreaseLedgerTimeResolutionEvery
constexpr auto kIncreaseLedgerTimeResolutionEvery
How often we increase the close time resolution (in numbers of ledgers).
Definition
LedgerTiming.h:39
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:72
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:122
xrpl::kLedgerGenesisTimeResolution
constexpr auto kLedgerGenesisTimeResolution
Close time resolution in genesis ledger.
Definition
LedgerTiming.h:34
xrpl::kLedgerDefaultTimeResolution
constexpr auto kLedgerDefaultTimeResolution
Initial resolution of ledger close time.
Definition
LedgerTiming.h:29
std::chrono::time_point
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
Generated by
1.17.0