xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
consensus
LedgerTiming.cpp
1
#include <xrpl/ledger/LedgerTiming.h>
2
3
#include <xrpl/basics/chrono.h>
4
5
#include <gtest/gtest.h>
6
7
#include <
chrono
>
8
#include <
cstdint
>
9
#include <
utility
>
10
11
namespace
xrpl::test
{
12
13
TEST
(LedgerTimingTest, get_next_ledger_time_resolution)
14
{
15
// helper to iteratively call into getNextLedgerTimeResolution
16
struct
TestRes
17
{
18
std::uint32_t
decrease = 0;
19
std::uint32_t
equal
= 0;
20
std::uint32_t
increase
= 0;
21
22
static
TestRes
23
run
(
bool
previousAgree,
std::uint32_t
rounds)
24
{
25
TestRes res;
26
auto
closeResolution =
kLedgerDefaultTimeResolution
;
27
auto
nextCloseResolution = closeResolution;
28
std::uint32_t
round = 0;
29
do
30
{
31
nextCloseResolution =
32
getNextLedgerTimeResolution
(closeResolution, previousAgree, ++round);
33
if
(nextCloseResolution < closeResolution)
34
{
35
++res.decrease;
36
}
37
else
if
(nextCloseResolution > closeResolution)
38
{
39
++res.increase;
40
}
41
else
42
{
43
++res.equal;
44
}
45
std::swap
(nextCloseResolution, closeResolution);
46
}
while
(round < rounds);
47
return
res;
48
}
49
};
50
51
// If we never agree on close time, only can increase resolution
52
// until hit the max
53
auto
decreases = TestRes::run(
false
, 10);
54
EXPECT_TRUE(decreases.increase == 3);
55
EXPECT_TRUE(decreases.decrease == 0);
56
EXPECT_TRUE(decreases.equal == 7);
57
58
// If we always agree on close time, only can decrease resolution
59
// until hit the min
60
auto
increases = TestRes::run(
false
, 100);
61
EXPECT_TRUE(increases.increase == 3);
62
EXPECT_TRUE(increases.decrease == 0);
63
EXPECT_TRUE(increases.equal == 97);
64
}
65
66
TEST
(LedgerTimingTest, round_close_time)
67
{
68
using namespace
std::chrono_literals;
69
// A closeTime equal to the epoch is not modified
70
using
Tp =
NetClock::time_point
;
71
Tp
const
def;
72
EXPECT_TRUE(def ==
roundCloseTime
(def, 30s));
73
74
// Otherwise, the closeTime is rounded to the nearest
75
// rounding up on ties
76
EXPECT_TRUE(Tp{0s} ==
roundCloseTime
(Tp{29s}, 60s));
77
EXPECT_TRUE(Tp{30s} ==
roundCloseTime
(Tp{30s}, 1s));
78
EXPECT_TRUE(Tp{60s} ==
roundCloseTime
(Tp{31s}, 60s));
79
EXPECT_TRUE(Tp{60s} ==
roundCloseTime
(Tp{30s}, 60s));
80
EXPECT_TRUE(Tp{60s} ==
roundCloseTime
(Tp{59s}, 60s));
81
EXPECT_TRUE(Tp{60s} ==
roundCloseTime
(Tp{60s}, 60s));
82
EXPECT_TRUE(Tp{60s} ==
roundCloseTime
(Tp{61s}, 60s));
83
}
84
85
TEST
(LedgerTimingTest, eff_close_time)
86
{
87
using namespace
std::chrono_literals;
88
using
Tp =
NetClock::time_point
;
89
Tp close =
effCloseTime
(Tp{10s}, 30s, Tp{0s});
90
EXPECT_TRUE(close == Tp{1s});
91
92
close =
effCloseTime
(Tp{16s}, 30s, Tp{0s});
93
EXPECT_TRUE(close == Tp{30s});
94
95
close =
effCloseTime
(Tp{16s}, 30s, Tp{30s});
96
EXPECT_TRUE(close == Tp{31s});
97
98
close =
effCloseTime
(Tp{16s}, 30s, Tp{60s});
99
EXPECT_TRUE(close == Tp{61s});
100
101
close =
effCloseTime
(Tp{31s}, 30s, Tp{0s});
102
EXPECT_TRUE(close == Tp{30s});
103
}
104
105
}
// namespace xrpl::test
chrono
xrpl::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition
chrono.h:48
cstdint
std::uint32_t
xrpl::test::jtx::equal
bool equal(STAmount const &sa1, STAmount const &sa2)
Definition
TestHelpers.cpp:131
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::TEST
TEST(UnitsTest, types)
Definition
Units.cpp:16
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::run
int run(int argc, char **argv)
Definition
Main.cpp:354
xrpl::increase
static FeeLevel64 increase(FeeLevel64 level, std::uint32_t increasePercent)
Definition
TxQ.cpp:102
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::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::kLedgerDefaultTimeResolution
constexpr auto kLedgerDefaultTimeResolution
Initial resolution of ledger close time.
Definition
LedgerTiming.h:28
std::swap
T swap(T... args)
utility
Generated by
1.17.0