xrpld
Loading...
Searching...
No Matches
OverlayImpl.h
1#pragma once
2
3#include <xrpld/app/main/Application.h>
4#include <xrpld/core/Config.h>
5#include <xrpld/overlay/Message.h>
6#include <xrpld/overlay/Overlay.h>
7#include <xrpld/overlay/Peer.h>
8#include <xrpld/overlay/Slot.h>
9#include <xrpld/overlay/detail/Handshake.h>
10#include <xrpld/overlay/detail/TrafficCount.h>
11#include <xrpld/overlay/detail/TxMetrics.h>
12#include <xrpld/peerfinder/detail/StoreSqdb.h>
13#include <xrpld/rpc/ServerHandler.h>
14
15#include <xrpl/basics/Resolver.h>
16#include <xrpl/basics/UnorderedContainers.h>
17#include <xrpl/basics/UptimeClock.h>
18#include <xrpl/basics/base_uint.h>
19#include <xrpl/beast/insight/Collector.h>
20#include <xrpl/beast/insight/Gauge.h>
21#include <xrpl/beast/insight/Hook.h>
22#include <xrpl/beast/net/IPEndpoint.h>
23#include <xrpl/beast/utility/Journal.h>
24#include <xrpl/beast/utility/PropertyStream.h>
25#include <xrpl/beast/utility/instrumentation.h>
26#include <xrpl/json/json_value.h>
27#include <xrpl/peerfinder/PeerfinderManager.h>
28#include <xrpl/peerfinder/Slot.h>
29#include <xrpl/resource/ResourceManager.h>
30#include <xrpl/server/Handoff.h>
31#include <xrpl/server/Writer.h>
32
33#include <boost/algorithm/string/predicate.hpp>
34#include <boost/asio/basic_waitable_timer.hpp>
35#include <boost/asio/ip/tcp.hpp>
36#include <boost/asio/ssl/context.hpp>
37#include <boost/asio/strand.hpp>
38#include <boost/container/flat_map.hpp>
39
40#include <xrpl.pb.h>
41
42#include <atomic>
43#include <chrono>
44#include <condition_variable>
45#include <cstddef>
46#include <cstdint>
47#include <functional>
48#include <memory>
49#include <mutex>
50#include <optional>
51#include <set>
52#include <string>
53#include <unordered_map>
54#include <utility>
55#include <vector>
56
57namespace xrpl {
58
59// The largest counts an operator can configure must still imply a message size
60// within the overall protocol message limit. The same check for the defaults
61// lives in Message.h.
62static_assert(
65
66class PeerImp;
67class BasicConfig;
68
70{
71public:
72 class Child
73 {
74 protected:
76
77 explicit Child(OverlayImpl& overlay);
78
79 public:
80 virtual ~Child();
81 virtual void
82 stop() = 0;
83 };
84
85private:
87 using SocketType = boost::asio::ip::tcp::socket;
88 using AddressType = boost::asio::ip::address;
89 using EndpointType = boost::asio::ip::tcp::endpoint;
90 using ErrorCode = boost::system::error_code;
91
93 {
94 boost::asio::basic_waitable_timer<ClockType> timer;
95 bool stopping{false};
96
97 explicit Timer(OverlayImpl& overlay);
98
99 void
100 stop() override;
101
102 void
103 asyncWait();
104
105 void
106 onTimer(ErrorCode ec);
107 };
108
110 boost::asio::io_context& ioContext_;
112 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
113 mutable std::recursive_mutex mutex_; // VFALCO use std::mutex
116 boost::container::flat_map<Child*, std::weak_ptr<Child>> list_;
132
134
135 // Transaction reduce-relay metrics
137
138 // A message with the list of manifests we send to peers
140 // Used to track whether we need to update the cached list of manifests
142 // Protects the message and the sequence list of manifests
144
145 //--------------------------------------------------------------------------
146
147public:
149 Application& app,
150 Setup setup,
151 ServerHandler& serverHandler,
153 Resolver& resolver,
154 boost::asio::io_context& ioContext,
155 BasicConfig const& config,
156 beast::insight::Collector::Ptr const& collector);
157
158 OverlayImpl(OverlayImpl const&) = delete;
160 operator=(OverlayImpl const&) = delete;
161
162 void
163 start() override;
164
165 void
166 stop() override;
167
170 {
171 return *peerFinder_;
172 }
173
176 {
177 return resourceManager_;
178 }
179
180 Setup const&
181 setup() const
182 {
183 return setup_;
184 }
185
186 Handoff
187 onHandoff(
189 HttpRequestType&& request,
190 EndpointType remoteEndpoint) override;
191
192 void
193 connect(beast::ip::Endpoint const& remoteEndpoint) override;
194
195 int
196 limit() override;
197
199 size() const override;
200
202 json() override;
203
205 getActivePeers() const override;
206
219 std::set<Peer::ID> const& toSkip,
220 std::size_t& active,
221 std::size_t& disabled,
222 std::size_t& enabledInSkip) const;
223
224 void
226
228 findPeerByShortID(Peer::ID const& id) const override;
229
231 findPeerByPublicKey(PublicKey const& pubKey) override;
232
233 void
234 broadcast(protocol::TMProposeSet const& m) override;
235
236 void
237 broadcast(protocol::TMValidation const& m) override;
238
240 relay(protocol::TMProposeSet const& m, UInt256 const& uid, PublicKey const& validator) override;
241
243 relay(protocol::TMValidation const& m, UInt256 const& uid, PublicKey const& validator) override;
244
245 void
246 relay(
247 UInt256 const&,
249 std::set<Peer::ID> const& skip) override;
250
251 void
252 relay(STTx const& tx, protocol::TMTransaction const& m, std::set<Peer::ID> const& toSkip)
253 override;
254
257
258 //--------------------------------------------------------------------------
259 //
260 // OverlayImpl
261 //
262
263 void
265
266 void
268
275 void
277
278 // Called when an active peer is destroyed.
279 void
281
282 // UnaryFunc will be called as
283 // void(std::shared_ptr<PeerImp>&&)
284 //
285 template <class UnaryFunc>
286 void
287 forEach(UnaryFunc&& f) const
288 {
290 {
291 std::scoped_lock const lock(mutex_);
292
293 // Iterate over a copy of the peer list because peer
294 // destruction can invalidate iterators.
295 wp.reserve(ids_.size());
296
297 for (auto& x : ids_)
298 wp.push_back(x.second);
299 }
300
301 for (auto const& w : wp)
302 {
303 // NOLINTNEXTLINE(misc-const-correctness) moved into f
304 if (auto p = w.lock())
305 f(std::move(p));
306 }
307 }
308
309 // Called when TMManifests is received from a peer
310 void
313 std::shared_ptr<PeerImp> const& from);
314
315 static bool
316 isPeerUpgrade(HttpRequestType const& request);
317
318 template <class Body>
319 static bool
320 isPeerUpgrade(boost::beast::http::response<Body> const& response)
321 {
322 if (!isUpgrade(response))
323 return false;
324 return response.result() == boost::beast::http::status::switching_protocols;
325 }
326
327 template <class Fields>
328 static bool
329 isUpgrade(boost::beast::http::header<true, Fields> const& req)
330 {
331 if (req.version() < 11)
332 return false;
333 if (req.method() != boost::beast::http::verb::get)
334 return false;
335 if (!boost::beast::http::token_list{req["Connection"]}.exists("upgrade"))
336 return false;
337 return true;
338 }
339
340 template <class Fields>
341 static bool
342 isUpgrade(boost::beast::http::header<false, Fields> const& req)
343 {
344 if (req.version() < 11)
345 return false;
346 if (!boost::beast::http::token_list{req["Connection"]}.exists("upgrade"))
347 return false;
348 return true;
349 }
350
351 static std::string
353
354 void
356
357 void
359
360 void
362 {
364 }
365
367 getJqTransOverflow() const override
368 {
369 return jqTransOverflow_;
370 }
371
372 void
374 {
376 }
377
379 getPeerDisconnect() const override
380 {
381 return peerDisconnects_;
382 }
383
384 void
389
392 {
394 }
395
397 networkID() const override
398 {
399 return setup_.networkID;
400 }
401
412 void
414 UInt256 const& key,
415 PublicKey const& validator,
416 std::set<Peer::ID>&& peers,
417 protocol::MessageType type);
418
422 void
424 UInt256 const& key,
425 PublicKey const& validator,
426 Peer::ID peer,
427 protocol::MessageType type);
428
435 void
437
439 txMetrics() const override
440 {
441 return txMetrics_.json();
442 }
443
447 template <typename... Args>
448 void
449 addTxMetrics(Args... args)
450 {
451 if (!strand_.running_in_this_thread())
452 return post(strand_, [this, args...] { addTxMetrics(args...); });
453
454 txMetrics_.addMetrics(args...);
455 }
456
457private:
462 void
464 UInt256 const& hash,
466 std::set<Peer::ID> const& toSkip);
467
468 void
469 squelch(PublicKey const& validator, Peer::ID const id, std::uint32_t squelchDuration)
470 const override;
471
472 void
473 unsquelch(PublicKey const& validator, Peer::ID id) const override;
474
478 HttpRequestType const& request,
479 AddressType remoteAddress);
480
484 HttpRequestType const& request,
485 AddressType remoteAddress,
486 std::string const& msg);
487
494 bool
495 processCrawl(HttpRequestType const& req, Handoff& handoff);
496
505 bool
506 processValidatorList(HttpRequestType const& req, Handoff& handoff);
507
514 bool
515 processHealth(HttpRequestType const& req, Handoff& handoff);
516
522 bool
523 processRequest(HttpRequestType const& req, Handoff& handoff);
524
531 getOverlayInfo() const;
532
540
548
555 getUnlInfo();
556
557 //--------------------------------------------------------------------------
558
559 //
560 // PropertyStream
561 //
562
563 void
564 onWrite(beast::PropertyStream::Map& stream) override;
565
566 //--------------------------------------------------------------------------
567
568 void
569 remove(Child& child);
570
571 void
572 stopChildren();
573
574 void
575 autoConnect();
576
577 void
579
583 void
584 sendTxQueue() const;
585
590 void
592
593private:
595 {
597 : name(name)
598 , bytesIn(collector->makeGauge(name, "Bytes_In"))
599 , bytesOut(collector->makeGauge(name, "Bytes_Out"))
600 , messagesIn(collector->makeGauge(name, "Messages_In"))
601 , messagesOut(collector->makeGauge(name, "Messages_Out"))
602 {
603 }
609 };
610
611 struct Stats
612 {
613 template <class Handler>
615 Handler const& handler,
616 beast::insight::Collector::Ptr const& collector,
618 : peerDisconnects(collector->makeGauge("Overlay", "Peer_Disconnects"))
620 , hook(collector->makeHook(handler))
621 {
622 }
623
627 };
628
631
632private:
633 void
635 {
636 auto const counts = traffic_.getCounts();
637 std::scoped_lock const lock(statsMutex_);
638 XRPL_ASSERT(
639 counts.size() == stats_.trafficGauges.size(),
640 "xrpl::OverlayImpl::collect_metrics : counts size do match");
641
642 for (auto const& [key, value] : counts)
643 {
644 auto const it = stats_.trafficGauges.find(key);
645 if (it == stats_.trafficGauges.end())
646 continue;
647
648 auto const& gauge = it->second;
649
650 XRPL_ASSERT(
651 gauge.name == value.name,
652 "xrpl::OverlayImpl::collect_metrics : gauge and counter "
653 "match");
654
655 gauge.bytesIn = value.bytesIn;
656 gauge.bytesOut = value.bytesOut;
657 gauge.messagesIn = value.messagesIn;
658 gauge.messagesOut = value.messagesOut;
659 }
660
661 stats_.peerDisconnects = getPeerDisconnect();
662 }
663};
664
665} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
std::shared_ptr< Collector > Ptr
Definition Collector.h:29
A metric for measuring an integral value.
Definition Gauge.h:21
A reference to a handler for performing polled collection.
Definition Hook.h:14
A version-independent IP address and port combination.
Definition IPEndpoint.h:24
Represents a JSON value.
Definition json_value.h:121
Holds unparsed configuration information.
static constexpr std::size_t kMaxManifestCount
Child(OverlayImpl &overlay)
virtual void stop()=0
static std::shared_ptr< Writer > makeErrorResponse(std::shared_ptr< peer_finder::Slot > const &slot, HttpRequestType const &request, AddressType remoteAddress, std::string const &msg)
boost::system::error_code ErrorCode
Definition OverlayImpl.h:90
std::weak_ptr< Timer > timer_
boost::asio::io_context & ioContext_
resource::Manager & resourceManager()
void squelch(PublicKey const &validator, Peer::ID const id, std::uint32_t squelchDuration) const override
Squelch handler.
std::set< Peer::ID > relay(protocol::TMProposeSet const &m, UInt256 const &uid, PublicKey const &validator) override
Relay a proposal.
json::Value getOverlayInfo() const
Returns information about peers on the overlay network.
bool processRequest(HttpRequestType const &req, Handoff &handoff)
Handles non-peer protocol requests.
bool processValidatorList(HttpRequestType const &req, Handoff &handoff)
Handles validator list requests.
Resolver & resolver_
void addActive(std::shared_ptr< PeerImp > const &peer)
std::mutex statsMutex_
std::atomic< Peer::ID > nextId_
void broadcast(protocol::TMProposeSet const &m) override
Broadcast a proposal.
OverlayImpl(OverlayImpl const &)=delete
void addTxMetrics(Args... args)
Add tx reduce-relay metrics.
void incPeerDisconnectCharges() override
void activate(std::shared_ptr< PeerImp > const &peer)
Called when a peer has connected successfully This is called after the peer handshake has been comple...
std::atomic< uint64_t > peerDisconnectsCharges_
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
void connect(beast::ip::Endpoint const &remoteEndpoint) override
Establish a peer connection to the specified endpoint.
peer_finder::Manager & peerFinder()
static bool isUpgrade(boost::beast::http::header< false, Fields > const &req)
void remove(std::shared_ptr< peer_finder::Slot > const &slot)
json::Value txMetrics() const override
Returns tx reduce-relay metrics.
void onPeerDeactivate(Peer::ID id)
void stop() override
OverlayImpl & operator=(OverlayImpl const &)=delete
std::size_t size() const override
The number of active peers on the network Active peers are only those peers that have completed the h...
static bool isUpgrade(boost::beast::http::header< true, Fields > const &req)
ServerHandler & serverHandler_
void onManifests(std::shared_ptr< protocol::TMManifests > const &m, std::shared_ptr< PeerImp > const &from)
std::uint64_t getPeerDisconnectCharges() const override
std::shared_ptr< Writer > makeRedirectResponse(std::shared_ptr< peer_finder::Slot > const &slot, HttpRequestType const &request, AddressType remoteAddress)
peer_finder::StoreSqdb store_
reduce_relay::Slots< UptimeClock > slots_
void deleteIdlePeers()
Check if peers stopped relaying messages and if slots stopped receiving messages from the validator.
std::chrono::steady_clock ClockType
Definition OverlayImpl.h:86
void unsquelch(PublicKey const &validator, Peer::ID id) const override
Unsquelch handler.
bool processHealth(HttpRequestType const &req, Handoff &handoff)
Handles health requests.
void reportInboundTraffic(TrafficCount::Category cat, int bytes)
std::shared_ptr< Message > manifestMessage_
void sendTxQueue() const
Send once a second transactions' hashes aggregated by peers.
std::uint64_t getPeerDisconnect() const override
std::optional< std::uint32_t > manifestListSeq_
std::unique_ptr< peer_finder::Manager > peerFinder_
void onWrite(beast::PropertyStream::Map &stream) override
Subclass override.
resource::Manager & resourceManager_
HashMap< std::shared_ptr< peer_finder::Slot >, std::weak_ptr< PeerImp > > peers_
Application & app_
json::Value getServerCounts()
Returns information about the local server's performance counters.
std::recursive_mutex mutex_
void incPeerDisconnect() override
Increment and retrieve counters for total peer disconnects, and disconnects we initiate for excessive...
bool processCrawl(HttpRequestType const &req, Handoff &handoff)
Handles crawl requests.
beast::Journal const journal_
json::Value json() override
Return diagnostics on the status of all peers.
void forEach(UnaryFunc &&f) const
std::shared_ptr< Peer > findPeerByShortID(Peer::ID const &id) const override
Returns the peer with the matching short id, or null.
std::mutex manifestLock_
Handoff onHandoff(std::unique_ptr< StreamType > &&bundle, HttpRequestType &&request, EndpointType remoteEndpoint) override
Conditionally accept an incoming HTTP request.
boost::asio::strand< boost::asio::io_context::executor_type > strand_
static std::string makePrefix(std::uint32_t id)
Setup const & setup() const
std::atomic< uint64_t > peerDisconnects_
HashMap< Peer::ID, std::weak_ptr< PeerImp > > ids_
boost::asio::ip::address AddressType
Definition OverlayImpl.h:88
static bool isPeerUpgrade(HttpRequestType const &request)
metrics::TxMetrics txMetrics_
boost::container::flat_map< Child *, std::weak_ptr< Child > > list_
int limit() override
Returns the maximum number of peers we are configured to allow.
std::condition_variable_any cond_
OverlayImpl(Application &app, Setup setup, ServerHandler &serverHandler, resource::Manager &resourceManager, Resolver &resolver, boost::asio::io_context &ioContext, BasicConfig const &config, beast::insight::Collector::Ptr const &collector)
json::Value getUnlInfo()
Returns information about the local server's UNL.
std::shared_ptr< Message > getManifestsMessage()
std::shared_ptr< Peer > findPeerByPublicKey(PublicKey const &pubKey) override
Returns the peer with the matching public key, or null.
TrafficCount traffic_
std::atomic< uint64_t > jqTransOverflow_
boost::asio::ip::tcp::endpoint EndpointType
Definition OverlayImpl.h:89
std::optional< std::uint32_t > networkID() const override
Returns the ID of the network this server is configured for, if any.
void deletePeer(Peer::ID id)
Called when the peer is deleted.
void checkTracking(std::uint32_t) override
Calls the checkTracking function on each peer.
json::Value getServerInfo()
Returns information about the local server.
void reportOutboundTraffic(TrafficCount::Category cat, int bytes)
std::uint64_t getJqTransOverflow() const override
static bool isPeerUpgrade(boost::beast::http::response< Body > const &response)
boost::asio::ip::tcp::socket SocketType
Definition OverlayImpl.h:87
void relayChecked(UInt256 const &hash, std::optional< std::reference_wrapper< protocol::TMTransaction const > > tx, std::set< Peer::ID > const &toSkip)
Relay tx if present, else only queue its hash for peers that have tx reduce-relay enabled.
void start() override
PeerSequence getActivePeers() const override
Returns a sequence representing the current list of peers.
void updateSlotAndSquelch(UInt256 const &key, PublicKey const &validator, std::set< Peer::ID > &&peers, protocol::MessageType type)
Updates message count for validator/peer.
void incJqTransOverflow() override
Increment and retrieve counter for transaction job queue overflows.
std::vector< std::shared_ptr< Peer > > PeerSequence
Definition Overlay.h:68
std::uint32_t ID
Uniquely identifies a peer.
A public key.
Definition PublicKey.h:53
TrafficCount is used to count ingress and egress wire bytes and number of messages.
Maintains a set of IP addresses used for getting into the network.
Database persistence for PeerFinder using SQLite.
Definition StoreSqdb.h:23
Slots is a container for validator's Slot and handles Slot update when a message is received from a v...
Tracks load and resource consumption.
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr std::size_t kMaximumMessageSize
Definition Message.h:22
constexpr std::size_t maximumManifestsMessageSize(std::size_t const trustedCount, std::size_t const untrustedCount)
Upper bound on the wire size of a TMManifests message.
Definition Message.h:48
BaseUInt< 256 > UInt256
Definition base_uint.h:581
boost::beast::http::request< boost::beast::http::dynamic_body > HttpRequestType
Definition Handoff.h:12
std::unordered_map< Key, Value, Hash, Pred, Allocator > HashMap
T push_back(T... args)
T reserve(T... args)
Used to indicate the result of a server connection handoff.
Definition Handoff.h:20
beast::insight::Gauge peerDisconnects
std::unordered_map< TrafficCount::Category, TrafficGauges > trafficGauges
beast::insight::Hook hook
Stats(Handler const &handler, beast::insight::Collector::Ptr const &collector, std::unordered_map< TrafficCount::Category, TrafficGauges > &&trafficGauges)
void onTimer(ErrorCode ec)
boost::asio::basic_waitable_timer< ClockType > timer
Definition OverlayImpl.h:94
Timer(OverlayImpl &overlay)
beast::insight::Gauge messagesOut
beast::insight::Gauge bytesOut
TrafficGauges(std::string const &name, beast::insight::Collector::Ptr const &collector)
beast::insight::Gauge messagesIn
beast::insight::Gauge bytesIn
Run transaction reduce-relay feature related metrics.
Definition TxMetrics.h:78