xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
resource
Logic.cpp
1
#include <xrpl/resource/detail/Logic.h>
2
3
#include <xrpl/basics/Log.h>
4
#include <xrpl/basics/chrono.h>
5
#include <xrpl/basics/random.h>
6
#include <xrpl/beast/insight/NullCollector.h>
7
#include <xrpl/beast/net/IPAddressV4.h>
8
#include <xrpl/beast/utility/Journal.h>
9
#include <xrpl/resource/Charge.h>
10
#include <xrpl/resource/Consumer.h>
11
#include <xrpl/resource/Disposition.h>
12
#include <xrpl/resource/Gossip.h>
13
#include <xrpl/resource/detail/Tuning.h>
14
15
#include <boost/utility/base_from_member.hpp>
16
17
#include <gtest/gtest.h>
18
#include <helpers/TestSink.h>
19
20
#include <
algorithm
>
21
#include <
chrono
>
22
#include <
cstdint
>
23
#include <
ranges
>
24
#include <
string
>
25
#include <
utility
>
26
27
namespace
xrpl::resource
{
28
29
class
ResourceManagerTest
:
public
::testing::Test
30
{
31
protected
:
32
beast::Journal
const
j_
{
TestSink::instance
()};
33
34
class
TestLogic
:
private
boost::base_from_member<TestStopwatch>,
public
Logic
35
{
36
private
:
37
using
ClockType
= boost::base_from_member<TestStopwatch>;
38
39
public
:
40
explicit
TestLogic
(
beast::Journal
journal)
41
:
Logic
(
beast
::insight::NullCollector::make(), member, journal)
42
{
43
}
44
45
void
46
advance
()
47
{
48
++member;
49
}
50
51
TestStopwatch
&
52
clock
()
53
{
54
return
member;
55
}
56
};
57
58
//--------------------------------------------------------------------------
59
60
static
Gossip
61
makeGossip
()
62
{
63
Gossip
gossip;
64
std::uint8_t
const
v(10 +
randInt
(9));
65
std::uint8_t
const
n(10 +
randInt
(9));
66
gossip.
items
.reserve(n);
67
for
(
std::uint8_t
i = 0; i < n; ++i)
68
{
69
Gossip::Item
item;
70
item.
balance
= 100 +
randInt
(499);
71
beast::ip::AddressV4::bytes_type
const
d = {
72
{
73
192,
74
0,
75
2,
76
static_cast<
std::uint8_t
>
(v + i),
77
},
78
};
79
item.
address
=
beast::ip::Endpoint
{
beast::ip::AddressV4
{d}};
80
gossip.
items
.push_back(std::move(item));
81
}
82
return
gossip;
83
}
84
};
85
86
TEST_F
(
ResourceManagerTest
, limited_warn_drop)
87
{
88
TestLogic logic{j_};
89
90
Charge
const
fee{
kDropThreshold
+ 1};
91
beast::ip::Endpoint
const
addr{
beast::ip::Endpoint::fromString
(
"192.0.2.2"
)};
92
93
{
94
Consumer
c{logic.newInboundEndpoint(addr)};
95
96
// Create load until we get a warning
97
auto
n = 10000;
98
auto
warned =
false
;
99
100
while
(--n >= 0)
101
{
102
if
(c.
charge
(fee) ==
Disposition::Warn
)
103
{
104
warned =
true
;
105
break
;
106
}
107
logic.advance();
108
}
109
110
ASSERT_TRUE(warned) <<
"Loop count exceeded without warning"
;
111
112
// Create load until we get dropped
113
bool
dropped =
false
;
114
while
(--n >= 0)
115
{
116
if
(c.
charge
(fee) ==
Disposition::Drop
)
117
{
118
dropped =
true
;
119
// Disconnect abusive Consumer
120
EXPECT_TRUE(c.
disconnect
(j_));
121
break
;
122
}
123
logic.advance();
124
}
125
126
ASSERT_TRUE(dropped) <<
"Loop count exceeded without dropping"
;
127
}
128
129
// Make sure the consumer is on the blacklist for a while.
130
{
131
Consumer
const
c{logic.newInboundEndpoint(addr)};
132
logic.periodicActivity();
133
EXPECT_EQ(c.
disposition
(),
Disposition::Drop
) <<
"Dropped consumer not put on blacklist"
;
134
}
135
136
// Makes sure the Consumer is eventually removed from blacklist
137
bool
readmitted =
false
;
138
{
139
using namespace
std::chrono_literals;
140
// Give Consumer time to become readmitted. Should never
141
// exceed expiration time.
142
auto
n =
kSecondsUntilExpiration
+ 1s;
143
while
(--n > 0s)
144
{
145
logic.advance();
146
logic.periodicActivity();
147
Consumer
const
c{logic.newInboundEndpoint(addr)};
148
if
(c.
disposition
() !=
Disposition::Drop
)
149
{
150
readmitted =
true
;
151
break
;
152
}
153
}
154
}
155
EXPECT_TRUE(readmitted) <<
"Dropped Consumer left on blacklist too long"
;
156
}
157
158
TEST_F
(
ResourceManagerTest
, unlimited_warn_drop)
159
{
160
TestLogic logic{j_};
161
162
Charge
const
fee{
kDropThreshold
+ 1};
163
beast::ip::Endpoint
const
addr{
beast::ip::Endpoint::fromString
(
"192.0.2.2"
)};
164
Consumer
c{logic.newUnlimitedEndpoint(addr)};
165
166
// Create load until we get a warning
167
int
n = 10000;
168
bool
warned =
false
;
169
170
while
(--n >= 0)
171
{
172
if
(c.
charge
(fee) ==
Disposition::Warn
)
173
{
174
warned =
true
;
175
break
;
176
}
177
logic.advance();
178
}
179
180
EXPECT_FALSE(warned) <<
"Should loop forever with no warning"
;
181
}
182
183
TEST_F
(
ResourceManagerTest
, charges)
184
{
185
static
constexpr
auto
kDecayTicks = 128uz;
186
187
TestLogic logic{j_};
188
189
{
190
beast::ip::Endpoint
const
address{
beast::ip::Endpoint::fromString
(
"192.0.2.1"
)};
191
Consumer
c{logic.newInboundEndpoint(address)};
192
Charge
const
fee{1000};
193
JLOG(j_.info()) <<
"Charging "
<< c.
toString
() <<
" "
<< fee <<
" per second"
;
194
c.
charge
(fee);
195
for
(
auto
tick = 0uz; tick < kDecayTicks; ++tick)
196
{
197
JLOG(j_.info()) <<
"Time= "
<< logic.clock().now().time_since_epoch().count()
198
<<
", Balance = "
<< c.
balance
();
199
logic.advance();
200
}
201
}
202
203
{
204
beast::ip::Endpoint
const
address{
beast::ip::Endpoint::fromString
(
"192.0.2.2"
)};
205
Consumer
c{logic.newInboundEndpoint(address)};
206
Charge
const
fee{1000};
207
JLOG(j_.info()) <<
"Charging "
<< c.
toString
() <<
" "
<< fee <<
" per second"
;
208
for
(
auto
tick = 0uz; tick < kDecayTicks; ++tick)
209
{
210
c.
charge
(fee);
211
JLOG(j_.info()) <<
"Time= "
<< logic.clock().now().time_since_epoch().count()
212
<<
", Balance = "
<< c.
balance
();
213
logic.advance();
214
}
215
}
216
}
217
218
TEST_F
(
ResourceManagerTest
, imports)
219
{
220
TestLogic logic{j_};
221
222
static
constexpr
auto
kGossipSources = 5uz;
223
std::ranges::for_each
(std::views::iota(0uz, kGossipSources), [&](
auto
const
i) {
224
logic.importConsumers(
std::to_string
(i), makeGossip());
225
});
226
}
227
228
TEST_F
(
ResourceManagerTest
,
import
)
229
{
230
TestLogic logic{j_};
231
232
Gossip
g;
233
Gossip::Item
item;
234
item.
balance
= 100;
235
beast::ip::AddressV4::bytes_type
const
d = {
236
{
237
192,
238
0,
239
2,
240
1,
241
},
242
};
243
item.
address
=
beast::ip::Endpoint
{
beast::ip::AddressV4
{d}};
244
g.
items
.push_back(std::move(item));
245
246
logic.importConsumers(
"g"
, g);
247
}
248
249
}
// namespace xrpl::resource
algorithm
chrono
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
beast::ip::Endpoint
A version-independent IP address and port combination.
Definition
IPEndpoint.h:24
beast::ip::Endpoint::fromString
static Endpoint fromString(std::string const &s)
Definition
IPEndpoint.cpp:42
xrpl::TestSink::instance
static TestSink & instance()
Definition
TestSink.h:12
xrpl::resource::Charge
A consumption charge.
Definition
Charge.h:13
xrpl::resource::Consumer
An endpoint that consumes resources.
Definition
Consumer.h:20
xrpl::resource::Consumer::disconnect
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition
Consumer.cpp:107
xrpl::resource::Consumer::balance
int balance()
Returns the credit balance representing consumption.
Definition
Consumer.cpp:119
xrpl::resource::Consumer::toString
std::string toString() const
Return a human readable string uniquely identifying this consumer.
Definition
Consumer.cpp:61
xrpl::resource::Consumer::charge
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Definition
Consumer.cpp:89
xrpl::resource::Consumer::disposition
Disposition disposition() const
Returns the current disposition of this consumer.
Definition
Consumer.cpp:79
xrpl::resource::Logic::Logic
Logic(beast::insight::Collector::Ptr const &collector, ClockType &clock, beast::Journal journal)
Definition
resource/detail/Logic.h:79
xrpl::resource::ResourceManagerTest::TestLogic::clock
TestStopwatch & clock()
Definition
Logic.cpp:52
xrpl::resource::ResourceManagerTest::TestLogic::ClockType
boost::base_from_member< TestStopwatch > ClockType
Definition
Logic.cpp:37
xrpl::resource::ResourceManagerTest::TestLogic::advance
void advance()
Definition
Logic.cpp:46
xrpl::resource::ResourceManagerTest::TestLogic::TestLogic
TestLogic(beast::Journal journal)
Definition
Logic.cpp:40
xrpl::resource::ResourceManagerTest
Definition
Logic.cpp:30
xrpl::resource::ResourceManagerTest::j_
beast::Journal const j_
Definition
Logic.cpp:32
xrpl::resource::ResourceManagerTest::makeGossip
static Gossip makeGossip()
Definition
Logic.cpp:61
cstdint
std::ranges::for_each
T for_each(T... args)
std::uint8_t
beast::ip::AddressV4
boost::asio::ip::address_v4 AddressV4
Definition
IPAddressV4.h:7
beast
Definition
base_uint.h:698
xrpl::resource
Definition
ServiceRegistry.h:21
xrpl::resource::kSecondsUntilExpiration
static constexpr std::chrono::seconds kSecondsUntilExpiration
Definition
include/xrpl/resource/detail/Tuning.h:24
xrpl::resource::Disposition::Drop
@ Drop
Definition
Disposition.h:24
xrpl::resource::Disposition::Warn
@ Warn
Consumer should be disconnected for excess consumption.
Definition
Disposition.h:18
xrpl::resource::kDropThreshold
static constexpr auto kDropThreshold
Definition
include/xrpl/resource/detail/Tuning.h:15
xrpl::resource::TEST_F
TEST_F(ResourceManagerTest, limited_warn_drop)
Definition
Logic.cpp:86
xrpl::TestStopwatch
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition
chrono.h:95
xrpl::randInt
Integral randInt()
Definition
include/xrpl/basics/random.h:142
ranges
string
xrpl::resource::Gossip::Item
Describes a single consumer.
Definition
Gossip.h:20
xrpl::resource::Gossip::Item::address
beast::ip::Endpoint address
Definition
Gossip.h:24
xrpl::resource::Gossip::Item::balance
int balance
Definition
Gossip.h:23
xrpl::resource::Gossip
Data format for exchanging consumption information across peers.
Definition
Gossip.h:13
xrpl::resource::Gossip::items
std::vector< Item > items
Definition
Gossip.h:27
std::to_string
T to_string(T... args)
utility
Generated by
1.17.0