1#ifndef XRPL_OVERLAY_OVERLAYIMPL_H_INCLUDED
2#define XRPL_OVERLAY_OVERLAYIMPL_H_INCLUDED
4#include <xrpld/app/main/Application.h>
5#include <xrpld/overlay/Message.h>
6#include <xrpld/overlay/Overlay.h>
7#include <xrpld/overlay/Slot.h>
8#include <xrpld/overlay/detail/Handshake.h>
9#include <xrpld/overlay/detail/TrafficCount.h>
10#include <xrpld/overlay/detail/TxMetrics.h>
11#include <xrpld/peerfinder/PeerfinderManager.h>
12#include <xrpld/rpc/ServerHandler.h>
14#include <xrpl/basics/Resolver.h>
15#include <xrpl/basics/UnorderedContainers.h>
16#include <xrpl/basics/chrono.h>
17#include <xrpl/beast/utility/instrumentation.h>
18#include <xrpl/core/Job.h>
19#include <xrpl/resource/ResourceManager.h>
20#include <xrpl/server/Handoff.h>
22#include <boost/algorithm/string/predicate.hpp>
23#include <boost/asio/basic_waitable_timer.hpp>
24#include <boost/asio/ip/tcp.hpp>
25#include <boost/asio/ssl/context.hpp>
26#include <boost/asio/strand.hpp>
27#include <boost/container/flat_map.hpp>
69 boost::asio::basic_waitable_timer<clock_type>
timer_;
87 boost::asio::io_context::executor_type>>
89 boost::asio::strand<boost::asio::io_context::executor_type>
strand_;
93 boost::container::flat_map<Child*, std::weak_ptr<Child>>
list_;
130 boost::asio::io_context& io_context,
175 size()
const override;
208 broadcast(protocol::TMProposeSet& m)
override;
211 broadcast(protocol::TMValidation& m)
override;
215 protocol::TMProposeSet& m,
221 protocol::TMValidation& m,
260 template <
class UnaryFunc>
278 if (
auto p = w.lock())
292 template <
class Body>
298 return response.result() ==
299 boost::beast::http::status::switching_protocols;
302 template <
class Fields>
304 is_upgrade(boost::beast::http::header<true, Fields>
const& req)
306 if (req.version() < 11)
308 if (req.method() != boost::beast::http::verb::get)
310 if (!boost::beast::http::token_list{req[
"Connection"]}.exists(
316 template <
class Fields>
318 is_upgrade(boost::beast::http::header<false, Fields>
const& req)
320 if (req.version() < 11)
322 if (!boost::beast::http::token_list{req[
"Connection"]}.exists(
393 protocol::MessageType type);
402 protocol::MessageType type);
419 template <
typename... Args>
423 if (!
strand_.running_in_this_thread())
426 std::bind(&OverlayImpl::addTxMetrics<Args...>,
this, args...));
569 template <
class Handler>
571 Handler
const& handler,
576 collector->make_gauge(
"Overlay",
"Peer_Disconnects"))
578 ,
hook(collector->make_hook(handler))
598 "xrpl::OverlayImpl::collect_metrics : counts size do match");
600 for (
auto const& [key, value] : counts)
606 auto& gauge = it->second;
609 gauge.name == value.name,
610 "xrpl::OverlayImpl::collect_metrics : gauge and counter "
613 gauge.bytesIn = value.bytesIn;
614 gauge.bytesOut = value.bytesOut;
615 gauge.messagesIn = value.messagesIn;
616 gauge.messagesOut = value.messagesOut;
A version-independent IP address and port combination.
A generic endpoint for log messages.
A metric for measuring an integral value.
A reference to a handler for performing polled collection.
Holds unparsed configuration information.
void deletePeer(Peer::id_t id)
Called when the peer is deleted.
void remove(std::shared_ptr< PeerFinder::Slot > const &slot)
std::weak_ptr< Timer > timer_
boost::asio::io_context & io_context_
bool processRequest(http_request_type const &req, Handoff &handoff)
Handles non-peer protocol requests.
boost::asio::ip::address address_type
static bool isPeerUpgrade(http_request_type const &request)
Resource::Manager & m_resourceManager
boost::system::error_code error_code
bool processCrawl(http_request_type const &req, Handoff &handoff)
Handles crawl requests.
OverlayImpl(OverlayImpl const &)=delete
void addTxMetrics(Args... args)
Add tx reduce-relay metrics.
bool processHealth(http_request_type const &req, Handoff &handoff)
Handles health requests.
Json::Value getServerCounts()
Returns information about the local server's performance counters.
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...
Handoff onHandoff(std::unique_ptr< stream_type > &&bundle, http_request_type &&request, endpoint_type remote_endpoint) override
Conditionally accept an incoming HTTP request.
std::atomic< uint64_t > peerDisconnectsCharges_
boost::asio::ip::tcp::socket socket_type
void reportOutboundTraffic(TrafficCount::category cat, int bytes)
static bool is_upgrade(boost::beast::http::header< false, Fields > const &req)
void for_each(UnaryFunc &&f) const
void connect(beast::IP::Endpoint const &remote_endpoint) override
Establish a peer connection to the specified endpoint.
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...
ServerHandler & serverHandler_
void onManifests(std::shared_ptr< protocol::TMManifests > const &m, std::shared_ptr< PeerImp > const &from)
std::uint64_t getPeerDisconnectCharges() const override
void broadcast(protocol::TMProposeSet &m) override
Broadcast a proposal.
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
std::shared_ptr< Writer > makeErrorResponse(std::shared_ptr< PeerFinder::Slot > const &slot, http_request_type const &request, address_type remote_address, std::string msg)
reduce_relay::Slots< UptimeClock > slots_
hash_map< Peer::id_t, std::weak_ptr< PeerImp > > ids_
void deleteIdlePeers()
Check if peers stopped relaying messages and if slots stopped receiving messages from the validator.
void squelch(PublicKey const &validator, Peer::id_t const id, std::uint32_t squelchDuration) const override
Squelch handler.
void sendTxQueue()
Send once a second transactions' hashes aggregated by peers.
std::shared_ptr< Message > manifestMessage_
std::unique_ptr< PeerFinder::Manager > m_peerFinder
std::uint64_t getPeerDisconnect() const override
std::optional< std::uint32_t > manifestListSeq_
Json::Value txMetrics() const override
Returns tx reduce-relay metrics.
void onWrite(beast::PropertyStream::Map &stream) override
Subclass override.
void add_active(std::shared_ptr< PeerImp > const &peer)
PeerFinder::Manager & peerFinder()
std::recursive_mutex mutex_
Resource::Manager & resourceManager()
void incPeerDisconnect() override
Increment and retrieve counters for total peer disconnects, and disconnects we initiate for excessive...
beast::Journal const journal_
boost::asio::ip::tcp::endpoint endpoint_type
void onPeerDeactivate(Peer::id_t id)
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Json::Value json() override
Return diagnostics on the status of all peers.
static std::string makePrefix(std::uint32_t id)
Setup const & setup() const
std::atomic< uint64_t > peerDisconnects_
std::set< Peer::id_t > relay(protocol::TMProposeSet &m, uint256 const &uid, PublicKey const &validator) override
Relay a proposal.
static bool is_upgrade(boost::beast::http::header< true, Fields > const &req)
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_
hash_map< std::shared_ptr< PeerFinder::Slot >, std::weak_ptr< PeerImp > > m_peers
std::shared_ptr< Message > getManifestsMessage()
Json::Value getUnlInfo()
Returns information about the local server's UNL.
std::shared_ptr< Peer > findPeerByPublicKey(PublicKey const &pubKey) override
Returns the peer with the matching public key, or null.
std::atomic< uint64_t > jqTransOverflow_
std::shared_ptr< Writer > makeRedirectResponse(std::shared_ptr< PeerFinder::Slot > const &slot, http_request_type const &request, address_type remote_address)
std::optional< std::uint32_t > networkID() const override
Returns the ID of the network this server is configured for, if any.
std::atomic< Peer::id_t > next_id_
void reportInboundTraffic(TrafficCount::category cat, int bytes)
bool processValidatorList(http_request_type const &req, Handoff &handoff)
Handles validator list requests.
void checkTracking(std::uint32_t) override
Calls the checkTracking function on each peer.
Json::Value getServerInfo()
Returns information about the local server.
void updateSlotAndSquelch(uint256 const &key, PublicKey const &validator, std::set< Peer::id_t > &&peers, protocol::MessageType type)
Updates message count for validator/peer.
std::uint64_t getJqTransOverflow() const override
static bool isPeerUpgrade(boost::beast::http::response< Body > const &response)
std::shared_ptr< Peer > findPeerByShortID(Peer::id_t const &id) const override
Returns the peer with the matching short id, or null.
PeerSequence getActivePeers() const override
Returns a sequence representing the current list of peers.
Json::Value getOverlayInfo()
Returns information about peers on the overlay network.
void unsquelch(PublicKey const &validator, Peer::id_t id) const override
Unsquelch handler.
void incJqTransOverflow() override
Increment and retrieve counter for transaction job queue overflows.
Manages the set of connected peers.
std::vector< std::shared_ptr< Peer > > PeerSequence
Maintains a set of IP addresses used for getting into the network.
Tracks load and resource consumption.
TrafficCount is used to count ingress and egress wire bytes and number of messages.
auto const & getCounts() const
An up-to-date copy of all the counters.
Slots is a container for validator's Slot and handles Slot update when a message is received from a v...
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Used to indicate the result of a server connection handoff.
beast::insight::Gauge peerDisconnects
std::unordered_map< TrafficCount::category, TrafficGauges > trafficGauges
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector, std::unordered_map< TrafficCount::category, TrafficGauges > &&trafficGauges_)
beast::insight::Hook hook
void on_timer(error_code ec)
boost::asio::basic_waitable_timer< clock_type > timer_
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
std::optional< std::uint32_t > networkID
Run transaction reduce-relay feature related metrics.
void addMetrics(protocol::MessageType type, std::uint32_t val)
Add protocol message metrics.
Json::Value json() const
Get json representation of the metrics.