xrpld
Loading...
Searching...
No Matches
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
27namespace xrpl::resource {
28
29class ResourceManagerTest : public ::testing::Test
30{
31protected:
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
47 {
48 ++member;
49 }
50
53 {
54 return member;
55 }
56 };
57
58 //--------------------------------------------------------------------------
59
60 static Gossip
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 };
80 gossip.items.push_back(std::move(item));
81 }
82 return gossip;
83 }
84};
85
86TEST_F(ResourceManagerTest, limited_warn_drop)
87{
88 TestLogic logic{j_};
89
90 Charge const fee{kDropThreshold + 1};
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)};
149 {
150 readmitted = true;
151 break;
152 }
153 }
154 }
155 EXPECT_TRUE(readmitted) << "Dropped Consumer left on blacklist too long";
156}
157
158TEST_F(ResourceManagerTest, unlimited_warn_drop)
159{
160 TestLogic logic{j_};
161
162 Charge const fee{kDropThreshold + 1};
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
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
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
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 };
244 g.items.push_back(std::move(item));
245
246 logic.importConsumers("g", g);
247}
248
249} // namespace xrpl::resource
A generic endpoint for log messages.
Definition Journal.h:44
A version-independent IP address and port combination.
Definition IPEndpoint.h:24
static Endpoint fromString(std::string const &s)
static TestSink & instance()
Definition TestSink.h:12
A consumption charge.
Definition Charge.h:13
An endpoint that consumes resources.
Definition Consumer.h:20
bool disconnect(beast::Journal const &j)
Returns true if the consumer should be disconnected.
Definition Consumer.cpp:107
int balance()
Returns the credit balance representing consumption.
Definition Consumer.cpp:119
std::string toString() const
Return a human readable string uniquely identifying this consumer.
Definition Consumer.cpp:61
Disposition charge(Charge const &fee, std::string const &context={})
Apply a load charge to the consumer.
Definition Consumer.cpp:89
Disposition disposition() const
Returns the current disposition of this consumer.
Definition Consumer.cpp:79
Logic(beast::insight::Collector::Ptr const &collector, ClockType &clock, beast::Journal journal)
boost::base_from_member< TestStopwatch > ClockType
Definition Logic.cpp:37
beast::Journal const j_
Definition Logic.cpp:32
T for_each(T... args)
boost::asio::ip::address_v4 AddressV4
Definition IPAddressV4.h:7
static constexpr std::chrono::seconds kSecondsUntilExpiration
@ Warn
Consumer should be disconnected for excess consumption.
Definition Disposition.h:18
static constexpr auto kDropThreshold
TEST_F(ResourceManagerTest, limited_warn_drop)
Definition Logic.cpp:86
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:95
Describes a single consumer.
Definition Gossip.h:20
beast::ip::Endpoint address
Definition Gossip.h:24
Data format for exchanging consumption information across peers.
Definition Gossip.h:13
std::vector< Item > items
Definition Gossip.h:27
T to_string(T... args)