1#include <test/unit_test/SuiteJournal.h>
3#include <xrpld/core/Config.h>
4#include <xrpld/peerfinder/PeerfinderManager.h>
5#include <xrpld/peerfinder/detail/Logic.h>
7#include <xrpl/basics/chrono.h>
8#include <xrpl/beast/unit_test/suite.h>
9#include <xrpl/protocol/PublicKey.h>
10#include <xrpl/protocol/SecretKey.h>
50 template <
class Handler>
54 boost::system::error_code ec;
62 auto const seconds = 10000;
81 BEAST_EXPECT(list.size() == 1);
98 auto const seconds = 10000;
120 BEAST_EXPECT(list.size() == 1);
125 if (!BEAST_EXPECT(logic.
activate(slot, pk,
false) == PeerFinder::Result::success))
134 BEAST_EXPECT(n <= (seconds + 59) / 60);
156 BEAST_EXPECT(slot1 !=
nullptr);
157 BEAST_EXPECT(r == Result::success);
163 BEAST_EXPECT(r2 == Result::duplicatePeer);
165 if (!BEAST_EXPECT(slot2 ==
nullptr))
192 BEAST_EXPECT(slot1 !=
nullptr);
193 BEAST_EXPECT(r == Result::success);
197 BEAST_EXPECT(r2 == Result::duplicatePeer);
199 if (!BEAST_EXPECT(slot2 ==
nullptr))
222 BEAST_EXPECT(slot !=
nullptr);
223 BEAST_EXPECT(r == Result::success);
226 BEAST_EXPECT(slot1 !=
nullptr);
227 BEAST_EXPECT(r1 == Result::success);
230 BEAST_EXPECT(r2 == Result::ipLimitExceeded);
232 if (!BEAST_EXPECT(slot2 ==
nullptr))
241 testcase(
"test activate duplicate peer");
259 BEAST_EXPECT(slot !=
nullptr);
260 BEAST_EXPECT(rSlot == Result::success);
263 BEAST_EXPECT(slot2 !=
nullptr);
264 BEAST_EXPECT(r2Slot == Result::success);
269 BEAST_EXPECT(logic.
activate(slot, pk1,
false) == Result::success);
272 BEAST_EXPECT(logic.
activate(slot2, pk1,
false) == Result::duplicatePeer);
277 BEAST_EXPECT(logic.
activate(slot2, pk1,
false) == Result::success);
284 testcase(
"test activate inbound disabled");
301 BEAST_EXPECT(slot !=
nullptr);
302 BEAST_EXPECT(rSlot == Result::success);
304 BEAST_EXPECT(logic.
activate(slot, pk1,
false) == Result::inboundDisabled);
315 BEAST_EXPECT(logic.
activate(slot, pk1,
false) == Result::success);
319 BEAST_EXPECT(slot2 !=
nullptr);
320 BEAST_EXPECT(r2Slot == Result::success);
325 BEAST_EXPECT(logic.
activate(slot2, pk2,
false) == Result::full);
334 testcase(
"test addFixedPeer no port");
342 fail(
"invalid endpoint successfully added");
353 testcase(
"test onConnected self connection");
361 BEAST_EXPECT(slot !=
nullptr);
362 BEAST_EXPECT(r == Result::success);
390 max = maxPeers.value();
391 toLoad +=
"[peers_max]\n" +
std::to_string(max) +
"\n" +
"[peers_in_max]\n" +
395 else if (maxIn && maxOut)
397 toLoad +=
"[peers_in_max]\n" +
std::to_string(*maxIn) +
"\n" +
"[peers_out_max]\n" +
411 counts.
out_max() == expectOut && counts.
in_max() == expectIn && config.
ipLimit == expectIpLimit);
419 BEAST_EXPECT(logic.
config() == config);
433 run(
"legacy no config", {}, {}, {}, 4000, 10, 11, 2);
434 run(
"legacy max_peers 0", 0, 100, 10, 4000, 10, 11, 2);
435 run(
"legacy max_peers 5", 5, 100, 10, 4000, 10, 0, 1);
436 run(
"legacy max_peers 20", 20, 100, 10, 4000, 10, 10, 2);
437 run(
"legacy max_peers 100", 100, 100, 10, 4000, 15, 85, 6);
438 run(
"legacy max_peers 20, private", 20, 100, 10, 0, 20, 0, 1);
441 run(
"new in 100/out 10", {}, 100, 10, 4000, 10, 100, 6);
442 run(
"new in 0/out 10", {}, 0, 10, 4000, 10, 0, 1);
443 run(
"new in 100/out 10, private", {}, 100, 10, 0, 10, 0, 6);
508BEAST_DEFINE_TESTSUITE(PeerFinder, peerfinder,
xrpl);
A version-independent IP address and port combination.
static Endpoint from_string(std::string const &s)
void pass()
Record a successful test condition.
testcase_t testcase
Memberspace for declaring test cases.
void fail(String const &reason, char const *file, int line)
Record a failure.
std::size_t PEERS_OUT_MAX
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
Manages the count of available connections for the various slots.
int out_max() const
Returns the total number of outbound slots.
int in_max() const
Returns the total number of inbound slots.
void onConfig(Config const &config)
Called when the config is set or changed.
The Logic for maintaining the list of Slot addresses.
void on_closed(SlotImp::ptr const &slot)
void addFixedPeer(std::string const &name, beast::IP::Endpoint const &ep)
void config(Config const &c)
std::pair< SlotImp::ptr, Result > new_inbound_slot(beast::IP::Endpoint const &local_endpoint, beast::IP::Endpoint const &remote_endpoint)
Result activate(SlotImp::ptr const &slot, PublicKey const &key, bool reserved)
bool onConnected(SlotImp::ptr const &slot, beast::IP::Endpoint const &local_endpoint)
std::vector< beast::IP::Endpoint > autoconnect()
Create new outbound connection attempts as needed.
std::multiset< beast::IP::Address > connectedAddresses_
std::pair< SlotImp::ptr, Result > new_outbound_slot(beast::IP::Endpoint const &remote_endpoint)
void test_invalid_config()
void test_peerLimitExceeded()
test::SuiteJournal journal_
void test_duplicateInOut()
void test_activate_inbound_disabled()
void test_onConnected_self_connection()
void test_addFixedPeer_no_port()
void test_duplicateOutIn()
void test_activate_duplicate_peer()
void run() override
Runs the suite.
Abstract persistence for PeerFinder data.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PeerFinder configuration settings.
int ipLimit
Limit how many incoming connections we allow per IP.
bool autoConnect
true if we want to establish connections automatically
std::size_t inPeers
The number of automatic inbound connections to maintain.
static Config makeConfig(xrpl::Config const &config, std::uint16_t port, bool validationPublicKey, int ipLimit)
Make PeerFinder::Config from configuration parameters.
std::uint16_t listeningPort
The listening port number.
void async_connect(beast::IP::Endpoint const &ep, Handler &&handler)
void save(std::vector< Entry > const &) override
std::size_t load(load_callback const &cb) override