xrpld
Loading...
Searching...
No Matches
PeerfinderConfig.cpp
1#include <xrpld/core/Config.h>
2#include <xrpld/peerfinder/PeerfinderManager.h>
3#include <xrpld/peerfinder/detail/Tuning.h>
4
5#include <xrpl/beast/utility/PropertyStream.h>
6
7#include <algorithm>
8#include <cstddef>
9#include <cstdint>
10
11namespace xrpl::PeerFinder {
12
17
24
25void
27{
28 if (ipLimit == 0)
29 {
30 // Unless a limit is explicitly set, we allow between
31 // 2 and 5 connections from non RFC-1918 "private"
32 // IP addresses.
33 ipLimit = 2;
34
36 ipLimit += std::min(5, static_cast<int>(inPeers / Tuning::kDefaultMaxPeers));
37 }
38
39 // We don't allow a single IP to consume all incoming slots,
40 // unless we only have one incoming slot available.
41 ipLimit = std::max(1, std::min(ipLimit, static_cast<int>(inPeers / 2)));
42}
43
44void
46{
47 map["max_peers"] = maxPeers;
48 map["out_peers"] = outPeers;
49 map["want_incoming"] = wantIncoming;
50 map["auto_connect"] = autoConnect;
51 map["port"] = listeningPort;
52 map["features"] = features;
53 map["ip_limit"] = ipLimit;
54 map["verify_endpoints"] = verifyEndpoints;
55}
56
59 xrpl::Config const& cfg,
60 std::uint16_t port,
61 bool validationPublicKey,
62 int ipLimit,
63 bool verifyEndpoints)
64{
65 PeerFinder::Config config;
66
67 config.peerPrivate = cfg.peerPrivate;
68
69 // Servers with peer privacy don't want to allow incoming connections
70 config.wantIncoming = (!config.peerPrivate) && (port != 0);
71
72 if ((cfg.peersOutMax == 0u) && (cfg.peersInMax == 0u))
73 {
74 if (cfg.peersMax != 0)
75 config.maxPeers = cfg.peersMax;
76
78 config.outPeers = config.calcOutPeers();
79
80 // Calculate the number of outbound peers we want. If we dont want
81 // or can't accept incoming, this will simply be equal to maxPeers.
82 if (!config.wantIncoming)
83 config.outPeers = config.maxPeers;
84
85 // Calculate the largest number of inbound connections we could
86 // take.
87 if (config.maxPeers >= config.outPeers)
88 {
89 config.inPeers = config.maxPeers - config.outPeers;
90 }
91 else
92 {
93 config.inPeers = 0;
94 }
95 }
96 else
97 {
98 config.outPeers = cfg.peersOutMax;
99 config.inPeers = cfg.peersInMax;
100 config.maxPeers = 0;
101 }
102
103 // This will cause servers configured as validators to request that
104 // peers they connect to never report their IP address. We set this
105 // after we set the 'wantIncoming' because we want a "soft" version
106 // of peer privacy unless the operator explicitly asks for it.
107 if (validationPublicKey)
108 config.peerPrivate = true;
109
110 // if it's a private peer or we are running as standalone
111 // automatic connections would defeat the purpose.
112 config.autoConnect = !cfg.standalone() && !cfg.peerPrivate;
113 config.listeningPort = port;
114 config.features = "";
115 config.ipLimit = ipLimit;
117
118 // Enforce business rules
119 config.applyTuning();
120
121 return config;
122}
123
124} // namespace xrpl::PeerFinder
std::size_t peersMax
Definition Config.h:165
bool standalone() const
Definition Config.h:316
std::size_t peersOutMax
Definition Config.h:166
bool peerPrivate
Definition Config.h:159
std::size_t peersInMax
Definition Config.h:167
T max(T... args)
T min(T... args)
static constexpr auto kDefaultMaxPeers
The default value of Config::maxPeers.
static constexpr auto kMinOutCount
A hard minimum on the number of outgoing connections.
static constexpr auto kOutPercent
The percentage of total peer slots that are outbound.
PeerFinder configuration settings.
std::size_t maxPeers
The largest number of public peer slots to allow.
int ipLimit
Limit how many incoming connections we allow per IP.
std::size_t outPeers
The number of automatic outbound connections to maintain.
bool verifyEndpoints
true if we want to verify endpoints in TMEndpoints messages
Config()
Create a configuration with default values.
void applyTuning()
Adjusts the values so they follow the business rules.
void onWrite(beast::PropertyStream::Map &map) const
Write the configuration into a property stream.
bool wantIncoming
true if we want to accept incoming connections.
bool autoConnect
true if we want to establish connections automatically
std::string features
The set of features we advertise.
std::size_t inPeers
The number of automatic inbound connections to maintain.
std::uint16_t listeningPort
The listening port number.
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
static Config makeConfig(xrpl::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit, bool verifyEndpoints)
Make PeerFinder::Config from configuration parameters.
bool peerPrivate
true if we want our IP address kept private.