xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
peerfinder
libxrpl/peerfinder/Config.cpp
1
#include <xrpl/peerfinder/Config.h>
2
3
#include <xrpl/beast/utility/PropertyStream.h>
4
5
#include <
algorithm
>
6
#include <
cstddef
>
7
#include <
cstdint
>
8
#include <
stdexcept
>
9
10
namespace
xrpl::peer_finder
{
11
12
std::size_t
13
Config::calcOutPeers
()
const
14
{
15
return
std::max
(
16
((
maxPeers
*
tuning::kOutPercent
) + 50) / 100,
std::size_t
(
tuning::kMinOutCount
));
17
}
18
19
void
20
Config::applyTuning
()
21
{
22
if
(
ipLimit
== 0)
23
{
24
// Unless a limit is explicitly set, we allow between
25
// 2 and 5 connections from non RFC-1918 "private"
26
// IP addresses.
27
ipLimit
= 2;
28
29
if
(
inPeers
>
tuning::kDefaultMaxPeers
)
30
ipLimit
+=
std::min
(5,
static_cast<
int
>
(
inPeers
/
tuning::kDefaultMaxPeers
));
31
}
32
33
// We don't allow a single IP to consume all incoming slots,
34
// unless we only have one incoming slot available.
35
ipLimit
=
std::max
(1,
std::min
(
ipLimit
,
static_cast<
int
>
(
inPeers
/ 2)));
36
}
37
38
void
39
Config::onWrite
(
beast::PropertyStream::Map
& map)
const
40
{
41
map[
"max_peers"
] =
maxPeers
;
42
map[
"out_peers"
] =
outPeers
;
43
map[
"want_incoming"
] =
wantIncoming
;
44
map[
"auto_connect"
] =
autoConnect
;
45
map[
"port"
] =
listeningPort
;
46
map[
"features"
] =
features
;
47
map[
"ip_limit"
] =
ipLimit
;
48
map[
"verify_endpoints"
] =
verifyEndpoints
;
49
}
50
51
Config
52
Config::makeConfig
(
53
bool
peerPrivate
,
54
bool
standalone,
55
PeerLimitConfig
const
& limits,
56
std::uint16_t
port,
57
bool
validationPublicKey,
58
int
ipLimit
,
59
bool
verifyEndpoints
)
60
{
61
peer_finder::Config
config;
62
63
if
(!limits.
maxPeers
)
64
{
65
if
(limits.
inPeers
&& !limits.
outPeers
)
66
throw
std::runtime_error
(
"Both inbound and outbound peer limits must be configured"
);
67
68
if
(limits.
outPeers
&& !limits.
inPeers
)
69
throw
std::runtime_error
(
"Both inbound and outbound peer limits must be configured"
);
70
71
if
(limits.
inPeers
&& *limits.
inPeers
> 1000)
72
throw
std::runtime_error
(
"Inbound peer limit must be less than or equal to 1000"
);
73
74
if
(limits.
outPeers
&& (*limits.
outPeers
< 10 || *limits.
outPeers
> 1000))
75
throw
std::runtime_error
(
"Outbound peer limit must be in the range 10-1000"
);
76
}
77
78
config.
peerPrivate
=
peerPrivate
;
79
80
// Servers with peer privacy don't want to allow incoming connections
81
config.
wantIncoming
= (!config.
peerPrivate
) && (port != 0);
82
83
if
(limits.
maxPeers
|| (!limits.
inPeers
&& !limits.
outPeers
))
84
{
85
if
(limits.
maxPeers
&& *limits.
maxPeers
!= 0)
86
config.
maxPeers
= *limits.
maxPeers
;
87
88
config.
maxPeers
=
std::max<std::size_t>
(config.
maxPeers
,
tuning::kMinOutCount
);
89
config.
outPeers
= config.
calcOutPeers
();
90
91
// Calculate the number of outbound peers we want. If we dont want
92
// or can't accept incoming, this will simply be equal to maxPeers.
93
if
(!config.
wantIncoming
)
94
config.
outPeers
= config.
maxPeers
;
95
96
// Calculate the largest number of inbound connections we could
97
// take.
98
if
(config.
maxPeers
>= config.
outPeers
)
99
{
100
config.
inPeers
= config.
maxPeers
- config.
outPeers
;
101
}
102
else
103
{
104
config.
inPeers
= 0;
105
}
106
}
107
else
108
{
109
config.
outPeers
= *limits.
outPeers
;
110
111
// Inbound slots only exist if we accept incoming connections, and
112
// `maxPeers` is the total across both directions. The legacy branch
113
// above upholds the same two invariants.
114
config.
inPeers
= config.
wantIncoming
? *limits.
inPeers
: 0;
115
config.
maxPeers
= config.
inPeers
+ config.
outPeers
;
116
}
117
118
// This will cause servers configured as validators to request that
119
// peers they connect to never report their IP address. We set this
120
// after we set the 'wantIncoming' because we want a "soft" version
121
// of peer privacy unless the operator explicitly asks for it.
122
if
(validationPublicKey)
123
config.
peerPrivate
=
true
;
124
125
// if it's a private peer or we are running as standalone
126
// automatic connections would defeat the purpose.
127
config.
autoConnect
= !standalone && !
peerPrivate
;
128
config.
listeningPort
= port;
129
config.
features
=
""
;
130
config.
ipLimit
=
ipLimit
;
131
config.
verifyEndpoints
=
verifyEndpoints
;
132
133
// Enforce business rules
134
config.
applyTuning
();
135
136
return
config;
137
}
138
139
}
// namespace xrpl::peer_finder
algorithm
beast::PropertyStream::Map
Definition
PropertyStream.h:207
cstddef
cstdint
std::uint16_t
std::max
T max(T... args)
std::min
T min(T... args)
xrpl::peer_finder::tuning::kOutPercent
static constexpr auto kOutPercent
The percentage of total peer slots that are outbound.
Definition
include/xrpl/peerfinder/detail/Tuning.h:36
xrpl::peer_finder::tuning::kDefaultMaxPeers
static constexpr auto kDefaultMaxPeers
The default value of Config::maxPeers.
Definition
include/xrpl/peerfinder/detail/Tuning.h:48
xrpl::peer_finder::tuning::kMinOutCount
static constexpr auto kMinOutCount
A hard minimum on the number of outgoing connections.
Definition
include/xrpl/peerfinder/detail/Tuning.h:43
xrpl::peer_finder
Definition
include/xrpl/peerfinder/Config.h:12
std::runtime_error
std::size_t
stdexcept
xrpl::peer_finder::Config
PeerFinder configuration settings.
Definition
include/xrpl/peerfinder/Config.h:25
xrpl::peer_finder::Config::outPeers
std::size_t outPeers
The number of automatic outbound connections to maintain.
Definition
include/xrpl/peerfinder/Config.h:39
xrpl::peer_finder::Config::onWrite
void onWrite(beast::PropertyStream::Map &map) const
Write the configuration into a property stream.
Definition
libxrpl/peerfinder/Config.cpp:39
xrpl::peer_finder::Config::wantIncoming
bool wantIncoming
true if we want to accept incoming connections.
Definition
include/xrpl/peerfinder/Config.h:56
xrpl::peer_finder::Config::peerPrivate
bool peerPrivate
true if we want our IP address kept private.
Definition
include/xrpl/peerfinder/Config.h:51
xrpl::peer_finder::Config::listeningPort
std::uint16_t listeningPort
The listening port number.
Definition
include/xrpl/peerfinder/Config.h:66
xrpl::peer_finder::Config::autoConnect
bool autoConnect
true if we want to establish connections automatically
Definition
include/xrpl/peerfinder/Config.h:61
xrpl::peer_finder::Config::applyTuning
void applyTuning()
Adjusts the values so they follow the business rules.
Definition
libxrpl/peerfinder/Config.cpp:20
xrpl::peer_finder::Config::makeConfig
static Config makeConfig(bool peerPrivate, bool standalone, PeerLimitConfig const &limits, std::uint16_t port, bool validationPublicKey, int ipLimit, bool verifyEndpoints)
Make peer_finder::Config from peer limit and server mode parameters.
Definition
libxrpl/peerfinder/Config.cpp:52
xrpl::peer_finder::Config::inPeers
std::size_t inPeers
The number of automatic inbound connections to maintain.
Definition
include/xrpl/peerfinder/Config.h:46
xrpl::peer_finder::Config::maxPeers
std::size_t maxPeers
The largest number of public peer slots to allow.
Definition
include/xrpl/peerfinder/Config.h:32
xrpl::peer_finder::Config::ipLimit
int ipLimit
Limit how many incoming connections we allow per IP.
Definition
include/xrpl/peerfinder/Config.h:76
xrpl::peer_finder::Config::calcOutPeers
std::size_t calcOutPeers() const
Returns a suitable value for outPeers according to the rules.
Definition
libxrpl/peerfinder/Config.cpp:13
xrpl::peer_finder::Config::verifyEndpoints
bool verifyEndpoints
true if we want to verify endpoints in TMEndpoints messages
Definition
include/xrpl/peerfinder/Config.h:81
xrpl::peer_finder::Config::features
std::string features
The set of features we advertise.
Definition
include/xrpl/peerfinder/Config.h:71
xrpl::peer_finder::PeerLimitConfig
Definition
include/xrpl/peerfinder/Config.h:15
xrpl::peer_finder::PeerLimitConfig::outPeers
std::optional< std::size_t > outPeers
Definition
include/xrpl/peerfinder/Config.h:18
xrpl::peer_finder::PeerLimitConfig::maxPeers
std::optional< std::size_t > maxPeers
Definition
include/xrpl/peerfinder/Config.h:16
xrpl::peer_finder::PeerLimitConfig::inPeers
std::optional< std::size_t > inPeers
Definition
include/xrpl/peerfinder/Config.h:17
Generated by
1.17.0