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>
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>
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>
94 boost::asio::basic_waitable_timer<ClockType>
timer;
112 boost::asio::strand<boost::asio::io_context::executor_type>
strand_;
116 boost::container::flat_map<Child*, std::weak_ptr<Child>>
list_;
154 boost::asio::io_context& ioContext,
199 size()
const override;
234 broadcast(protocol::TMProposeSet
const& m)
override;
237 broadcast(protocol::TMValidation
const& m)
override;
281 template <
class UnaryFunc>
299 if (
auto p = w.lock())
313 template <
class Body>
319 return response.result() == boost::beast::http::status::switching_protocols;
322 template <
class Fields>
324 isUpgrade(boost::beast::http::header<true, Fields>
const& req)
326 if (req.version() < 11)
328 if (req.method() != boost::beast::http::verb::get)
330 if (!boost::beast::http::token_list{req[
"Connection"]}.exists(
"upgrade"))
335 template <
class Fields>
337 isUpgrade(boost::beast::http::header<false, Fields>
const& req)
339 if (req.version() < 11)
341 if (!boost::beast::http::token_list{req[
"Connection"]}.exists(
"upgrade"))
412 protocol::MessageType type);
422 protocol::MessageType type);
442 template <
typename... Args>
446 if (!
strand_.running_in_this_thread())
447 return post(
strand_, [
this, args...] { addTxMetrics(args...); });
598 template <
class Handler>
600 Handler
const& handler,
605 ,
hook(collector->makeHook(handler))
624 counts.size() ==
stats_.trafficGauges.size(),
625 "xrpl::OverlayImpl::collect_metrics : counts size do match");
627 for (
auto const& [key, value] : counts)
629 auto it =
stats_.trafficGauges.find(key);
630 if (it ==
stats_.trafficGauges.end())
633 auto& gauge = it->second;
636 gauge.name == value.name,
637 "xrpl::OverlayImpl::collect_metrics : gauge and counter "
640 gauge.bytesIn = value.bytesIn;
641 gauge.bytesOut = value.bytesOut;
642 gauge.messagesIn = value.messagesIn;
643 gauge.messagesOut = value.messagesOut;
A generic endpoint for log messages.
std::shared_ptr< Collector > Ptr
A metric for measuring an integral value.
A reference to a handler for performing polled collection.
A version-independent IP address and port combination.
Holds unparsed configuration information.
static constexpr std::size_t kMaxManifestCount
Child(OverlayImpl &overlay)
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
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.
void addActive(std::shared_ptr< PeerImp > const &peer)
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)
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
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_
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.
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
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.
std::atomic< uint64_t > jqTransOverflow_
boost::asio::ip::tcp::endpoint EndpointType
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
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
std::uint32_t ID
Uniquely identifies a peer.
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.
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr std::size_t kMaximumMessageSize
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.
boost::beast::http::request< boost::beast::http::dynamic_body > HttpRequestType
std::unordered_map< Key, Value, Hash, Pred, Allocator > HashMap
Used to indicate the result of a server connection handoff.
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
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.