xrpld
Loading...
Searching...
No Matches
Livecache.cpp
1#include <xrpl/peerfinder/detail/Livecache.h>
2
3#include <xrpl/basics/chrono.h>
4#include <xrpl/basics/random.h>
5#include <xrpl/beast/net/IPAddressV4.h>
6#include <xrpl/beast/net/IPAddressV6.h>
7#include <xrpl/beast/net/IPEndpoint.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/beast/utility/PropertyStream.h>
10#include <xrpl/json/JsonPropertyStream.h>
11#include <xrpl/json/json_forwards.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/peerfinder/Types.h>
14#include <xrpl/peerfinder/detail/Tuning.h>
15
16#include <boost/algorithm/string/classification.hpp>
17#include <boost/algorithm/string/split.hpp>
18#include <boost/algorithm/string/trim.hpp>
19#include <boost/lexical_cast.hpp>
20
21#include <gtest/gtest.h>
22#include <helpers/TestSink.h>
23
24#include <algorithm>
25#include <array>
26#include <cstdint>
27#include <iterator>
28#include <string>
29#include <utility>
30#include <vector>
31
32namespace xrpl::peer_finder {
33namespace {
34
35class LivecacheTest : public ::testing::Test
36{
37protected:
38 static beast::Journal
39 journal()
40 {
41 return beast::Journal{TestSink::instance()};
42 }
43
44 static beast::ip::Endpoint
45 endpoint(std::uint16_t index, bool v4 = true)
46 {
47 auto const port = static_cast<std::uint16_t>(10000 + index);
48
49 if (v4)
50 {
51 auto const bytes = beast::ip::AddressV4::bytes_type{
52 {
53 54,
54 static_cast<std::uint8_t>((index / 256) % 256),
55 static_cast<std::uint8_t>(index % 256),
56 1,
57 },
58 };
59 return beast::ip::Endpoint{beast::ip::Address{beast::ip::AddressV4{bytes}}, port};
60 }
61
62 auto const bytes = beast::ip::AddressV6::bytes_type{
63 {
64 0x20,
65 0x01,
66 0x0d,
67 0xb8,
68 0,
69 0,
70 0,
71 0,
72 0,
73 0,
74 0,
75 0,
76 0,
77 static_cast<std::uint8_t>((index / 256) % 256),
78 static_cast<std::uint8_t>(index % 256),
79 1,
80 },
81 };
82 return beast::ip::Endpoint{beast::ip::Address{beast::ip::AddressV6{bytes}}, port};
83 }
84
85 void
86 addEndpoint(beast::ip::Endpoint const& ep, std::uint32_t hops = 0)
87 {
88 cache_.insert(Endpoint{ep, hops});
89 }
90
91 TestStopwatch clock_;
92 Livecache<> cache_{clock_, journal()};
93};
94
95} // namespace
96
97TEST_F(LivecacheTest, basic_insert)
98{
99 EXPECT_TRUE(cache_.empty());
100
101 for (auto i = 0; i < 10; ++i)
102 addEndpoint(endpoint(i, true));
103
104 EXPECT_FALSE(cache_.empty());
105 EXPECT_EQ(cache_.size(), 10u);
106
107 for (auto i = 10; i < 20; ++i)
108 addEndpoint(endpoint(i, false));
109
110 EXPECT_FALSE(cache_.empty());
111 EXPECT_EQ(cache_.size(), 20u);
112}
113
114TEST_F(LivecacheTest, insert_update_keeps_lowest_hop_count)
115{
116 auto const ep1 = Endpoint{endpoint(1), 2};
117 cache_.insert(ep1);
118 ASSERT_EQ(cache_.size(), 1u);
119 EXPECT_EQ((cache_.hops.begin() + 2)->begin()->hops, 2u);
120
121 auto const ep2 = Endpoint{ep1.address, 4};
122 cache_.insert(ep2);
123 EXPECT_EQ(cache_.size(), 1u);
124 EXPECT_EQ((cache_.hops.begin() + 2)->begin()->hops, 2u);
125
126 auto const ep3 = Endpoint{ep1.address, 2};
127 cache_.insert(ep3);
128 EXPECT_EQ(cache_.size(), 1u);
129 EXPECT_EQ((cache_.hops.begin() + 2)->begin()->hops, 2u);
130
131 auto const ep4 = Endpoint{ep1.address, 1};
132 cache_.insert(ep4);
133 EXPECT_EQ(cache_.size(), 1u);
134 EXPECT_EQ((cache_.hops.begin() + 1)->begin()->hops, 1u);
135}
136
137TEST_F(LivecacheTest, hop_iterators_support_const_reverse_and_move_back)
138{
139 auto const ep1 = Endpoint{endpoint(1), 1};
140 auto const ep2 = Endpoint{endpoint(2), 1};
141 cache_.insert(ep1);
142 cache_.insert(ep2);
143
144 auto hop = *(cache_.hops.begin() + 1);
145 ASSERT_NE(hop.begin(), hop.end());
146 ASSERT_NE(hop.cbegin(), hop.cend());
147 ASSERT_NE(hop.rbegin(), hop.rend());
148 ASSERT_NE(hop.crbegin(), hop.crend());
149
150 auto const firstAddress = hop.begin()->address;
151 hop.moveBack(hop.begin());
152 EXPECT_EQ(hop.rbegin()->address, firstAddress);
153
154 auto const& constHops = cache_.hops;
155 EXPECT_NE(constHops.begin(), constHops.end());
156 EXPECT_NE(constHops.cbegin(), constHops.cend());
157 EXPECT_NE(constHops.rbegin(), constHops.rend());
158 EXPECT_NE(constHops.crbegin(), constHops.crend());
159
160 auto const constHop = *(constHops.cbegin() + 1);
161 EXPECT_EQ(std::distance(constHop.begin(), constHop.end()), 2);
162 EXPECT_EQ(std::distance(constHop.cbegin(), constHop.cend()), 2);
163 EXPECT_EQ(std::distance(constHop.rbegin(), constHop.rend()), 2);
164 EXPECT_EQ(std::distance(constHop.crbegin(), constHop.crend()), 2);
165}
166
167TEST_F(LivecacheTest, on_write_reports_entries_and_expiration)
168{
169 cache_.insert(Endpoint{endpoint(1), 1});
170 cache_.insert(Endpoint{endpoint(2), tuning::kMaxHops + 1});
171
172 JsonPropertyStream stream;
173 {
174 beast::PropertyStream::Map map(stream);
175 cache_.onWrite(map);
176 }
177
178 auto const& top = stream.top();
179 EXPECT_EQ(top["size"].asUInt(), 2u);
180 EXPECT_FALSE(top["hist"].asString().empty());
181 ASSERT_TRUE(top.isMember("entries"));
182 ASSERT_EQ(top["entries"].size(), 2u);
183 auto const& entry = top["entries"][json::UInt{0}];
184 EXPECT_TRUE(entry.isMember("hops"));
185 EXPECT_TRUE(entry.isMember("address"));
186 EXPECT_TRUE(entry.isMember("expires"));
187}
188
189TEST_F(LivecacheTest, expire_removes_entries_after_ttl)
190{
191 using namespace std::chrono_literals;
192
193 cache_.insert(Endpoint{endpoint(1), 1});
194 ASSERT_EQ(cache_.size(), 1u);
195
196 cache_.expire();
197 EXPECT_EQ(cache_.size(), 1u);
198
199 clock_.advance(tuning::kLiveCacheSecondsToLive - 1s);
200 cache_.expire();
201 EXPECT_EQ(cache_.size(), 1u);
202
203 clock_.advance(1s);
204 cache_.expire();
205 EXPECT_TRUE(cache_.empty());
206}
207
208TEST_F(LivecacheTest, expire_removes_multiple_entries_after_ttl)
209{
210 using namespace std::chrono_literals;
211
212 cache_.insert(Endpoint{endpoint(1), 1});
213 cache_.insert(Endpoint{endpoint(2), 2});
214
215 clock_.advance(tuning::kLiveCacheSecondsToLive);
216 cache_.expire();
217 EXPECT_TRUE(cache_.empty());
218}
219
220TEST_F(LivecacheTest, histogram_counts_all_entries)
221{
222 constexpr auto kNumEndpoints = 40;
223
224 for (auto i = 0; i < kNumEndpoints; ++i)
225 {
226 addEndpoint(endpoint(static_cast<std::uint16_t>(i)), xrpl::randInt<std::uint32_t>());
227 }
228
229 auto const histogram = cache_.hops.histogram();
230 ASSERT_FALSE(histogram.empty());
231
233 boost::split(values, histogram, boost::algorithm::is_any_of(","));
234
235 auto sum = 0;
236 for (auto const& value : values)
237 {
238 auto const count = boost::lexical_cast<int>(boost::trim_copy(value));
239 sum += count;
240 EXPECT_GE(count, 0);
241 }
242 EXPECT_EQ(sum, kNumEndpoints);
243}
244
245TEST_F(LivecacheTest, shuffle_preserves_bucket_contents)
246{
247 for (auto i = 0; i < 100; ++i)
248 {
249 addEndpoint(endpoint(static_cast<std::uint16_t>(i)), xrpl::randInt(tuning::kMaxHops + 1));
250 }
251
252 using AtHop = std::vector<Endpoint>;
254
255 auto const compareEndpoint = [](Endpoint const& lhs, Endpoint const& rhs) {
256 return rhs.hops < lhs.hops || (rhs.hops == lhs.hops && rhs.address < lhs.address);
257 };
258 auto const sameEndpoint = [](Endpoint const& lhs, Endpoint const& rhs) {
259 return lhs.hops == rhs.hops && lhs.address == rhs.address;
260 };
261 auto const sameEndpoints =
262 [&sameEndpoint](std::vector<Endpoint> const& lhs, std::vector<Endpoint> const& rhs) {
263 return lhs.size() == rhs.size() &&
264 std::equal(lhs.begin(), lhs.end(), rhs.begin(), sameEndpoint);
265 };
266
267 AllHops before;
268 AllHops beforeSorted;
269 for (auto i = std::make_pair(0, cache_.hops.begin()); i.second != cache_.hops.end();
270 ++i.first, ++i.second)
271 {
272 std::ranges::copy(*i.second, std::back_inserter(before[i.first]));
273 std::ranges::copy(*i.second, std::back_inserter(beforeSorted[i.first]));
274 std::ranges::sort(beforeSorted[i.first], compareEndpoint);
275 }
276
277 cache_.hops.shuffle();
278
279 AllHops after;
280 AllHops afterSorted;
281 for (auto i = std::make_pair(0, cache_.hops.begin()); i.second != cache_.hops.end();
282 ++i.first, ++i.second)
283 {
284 std::ranges::copy(*i.second, std::back_inserter(after[i.first]));
285 std::ranges::copy(*i.second, std::back_inserter(afterSorted[i.first]));
286 std::ranges::sort(afterSorted[i.first], compareEndpoint);
287 }
288
289 auto allBucketsKeptOriginalOrder = true;
290 for (auto i = 0u; i < before.size(); ++i)
291 {
292 EXPECT_EQ(before[i].size(), after[i].size());
293 allBucketsKeptOriginalOrder =
294 allBucketsKeptOriginalOrder && sameEndpoints(before[i], after[i]);
295 EXPECT_TRUE(sameEndpoints(beforeSorted[i], afterSorted[i]));
296 }
297 EXPECT_FALSE(allBucketsKeptOriginalOrder);
298}
299
300} // namespace xrpl::peer_finder
T back_inserter(T... args)
Address const & address() const
Returns the address portion of this endpoint.
Definition IPEndpoint.h:74
bool isMember(char const *key) const
Return true if the object has a member named key.
A PropertyStream::Sink which produces a json::Value of type ValueType::Object.
static TestSink & instance()
Definition TestSink.h:12
T copy(T... args)
T distance(T... args)
T equal(T... args)
T make_pair(T... args)
boost::asio::ip::address Address
Definition IPAddress.h:20
boost::asio::ip::address_v6 AddressV6
Definition IPAddressV6.h:7
boost::asio::ip::address_v4 AddressV4
Definition IPAddressV4.h:7
unsigned int UInt
constexpr std::chrono::seconds kLiveCacheSecondsToLive(30)
TEST_F(LivecacheTest, basic_insert)
Definition Livecache.cpp:97
static auto sum(TCollection const &col)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
Integral randInt(Engine &engine, Integral min, Integral max)
Return a uniformly distributed random integer.
beast::ManualClock< std::chrono::steady_clock > TestStopwatch
A manual Stopwatch for unit tests.
Definition chrono.h:95
T sort(T... args)
Describes a connectable peer address along with some metadata.
beast::ip::Endpoint address