xrpld
Loading...
Searching...
No Matches
OverlayImpl.cpp
1#include <xrpld/overlay/detail/OverlayImpl.h>
2
3#include <xrpld/app/misc/ValidatorList.h>
4#include <xrpld/app/misc/ValidatorSite.h>
5#include <xrpld/overlay/Cluster.h>
6#include <xrpld/overlay/detail/ConnectAttempt.h>
7#include <xrpld/overlay/detail/Handshake.h>
8#include <xrpld/overlay/detail/PeerImp.h>
9#include <xrpld/overlay/detail/ProtocolVersion.h>
10#include <xrpld/overlay/detail/TrafficCount.h>
11#include <xrpld/overlay/detail/Tuning.h>
12#include <xrpld/peerfinder/PeerfinderManager.h>
13#include <xrpld/rpc/ServerHandler.h>
14#include <xrpld/rpc/handlers/admin/status/GetCounts.h>
15#include <xrpld/rpc/json_body.h>
16
17#include <xrpl/basics/Log.h>
18#include <xrpl/basics/Resolver.h>
19#include <xrpl/basics/Slice.h>
20#include <xrpl/basics/base64.h>
21#include <xrpl/basics/base_uint.h>
22#include <xrpl/basics/chrono.h>
23#include <xrpl/basics/contract.h>
24#include <xrpl/basics/make_SSLContext.h>
25#include <xrpl/basics/random.h>
26#include <xrpl/basics/strHex.h>
27#include <xrpl/beast/core/LexicalCast.h>
28#include <xrpl/beast/insight/Collector.h>
29#include <xrpl/beast/net/IPAddress.h>
30#include <xrpl/beast/net/IPAddressConversion.h>
31#include <xrpl/beast/net/IPEndpoint.h>
32#include <xrpl/beast/rfc2616.h>
33#include <xrpl/beast/utility/PropertyStream.h>
34#include <xrpl/beast/utility/WrappedSink.h>
35#include <xrpl/beast/utility/instrumentation.h>
36#include <xrpl/config/BasicConfig.h>
37#include <xrpl/config/Constants.h>
38#include <xrpl/core/HashRouter.h>
39#include <xrpl/json/json_value.h>
40#include <xrpl/peerfinder/Config.h>
41#include <xrpl/peerfinder/Slot.h>
42#include <xrpl/peerfinder/make_Manager.h>
43#include <xrpl/protocol/BuildInfo.h>
44#include <xrpl/protocol/STTx.h>
45#include <xrpl/protocol/Serializer.h>
46#include <xrpl/protocol/SystemParameters.h>
47#include <xrpl/protocol/jss.h>
48#include <xrpl/resource/Fees.h>
49#include <xrpl/resource/ResourceManager.h>
50#include <xrpl/server/Handoff.h>
51#include <xrpl/server/Manifest.h>
52#include <xrpl/server/NetworkOPs.h>
53#include <xrpl/server/SimpleWriter.h>
54#include <xrpl/server/Wallet.h>
55#include <xrpl/server/Writer.h>
56
57#include <boost/algorithm/string/predicate.hpp>
58#include <boost/asio/bind_executor.hpp>
59#include <boost/asio/dispatch.hpp>
60#include <boost/asio/error.hpp>
61#include <boost/asio/executor_work_guard.hpp>
62#include <boost/asio/io_context.hpp>
63#include <boost/asio/ip/address.hpp>
64#include <boost/asio/post.hpp>
65#include <boost/asio/strand.hpp>
66#include <boost/beast/http/empty_body.hpp>
67#include <boost/beast/http/field.hpp>
68#include <boost/beast/http/status.hpp>
69#include <boost/lexical_cast.hpp>
70#include <boost/lexical_cast/bad_lexical_cast.hpp>
71#include <boost/lexical_cast/try_lexical_convert.hpp>
72
73#include <xrpl.pb.h>
74
75#include <algorithm>
76#include <array>
77#include <chrono>
78#include <cstddef>
79#include <cstdint>
80#include <exception>
81#include <format>
82#include <functional>
83#include <iomanip>
84#include <memory>
85#include <mutex>
86#include <optional>
87#include <set>
88#include <sstream>
89#include <stdexcept>
90#include <string>
91#include <string_view>
92#include <tuple>
93#include <unordered_map>
94#include <utility>
95#include <vector>
96
97namespace xrpl {
98
99namespace crawl_options {
100static constexpr auto kDisabled = 0;
101static constexpr auto kOverlay = (1 << 0);
102static constexpr auto kServerInfo = (1 << 1);
103static constexpr auto kServerCounts = (1 << 2);
104static constexpr auto kUnl = (1 << 3);
105} // namespace crawl_options
106
107//------------------------------------------------------------------------------
108
110{
111}
112
114{
115 overlay_.remove(*this);
116}
117
118//------------------------------------------------------------------------------
119
123
124void
126{
127 // This method is only ever called from the same strand that calls
128 // Timer::on_timer, ensuring they never execute concurrently.
129 stopping = true;
130 timer.cancel();
131}
132
133void
135{
136 timer.expires_after(std::chrono::seconds(1));
137 timer.async_wait(
138 boost::asio::bind_executor(
139 overlay_.strand_,
140 [self = shared_from_this()](ErrorCode const& ec) { self->onTimer(ec); }));
141}
142
143void
145{
146 if (ec || stopping)
147 {
148 if (ec && ec != boost::asio::error::operation_aborted)
149 {
150 JLOG(overlay_.journal_.error()) << "on_timer: " << ec.message();
151 }
152 return;
153 }
154
155 overlay_.peerFinder_->oncePerSecond();
156 overlay_.sendEndpoints();
157 overlay_.autoConnect();
158 if (overlay_.app_.config().txReduceRelayEnable)
159 overlay_.sendTxQueue();
160
161 if ((++overlay_.timerCount_ % tuning::kCheckIdlePeers) == 0)
162 overlay_.deleteIdlePeers();
163
164 asyncWait();
165}
166
167//------------------------------------------------------------------------------
168
170 Application& app,
171 Setup setup,
172 ServerHandler& serverHandler,
174 Resolver& resolver,
175 boost::asio::io_context& ioContext,
176 BasicConfig const& config,
177 beast::insight::Collector::Ptr const& collector)
178 : app_(app)
179 , ioContext_(ioContext)
180 , work_(std::in_place, boost::asio::make_work_guard(ioContext_))
181 , strand_(boost::asio::make_strand(ioContext_))
182 , setup_(std::move(setup))
183 , journal_(app_.getJournal("Overlay"))
184 , serverHandler_(serverHandler)
186 , store_(app_.getJournal("PeerFinder"))
187 , peerFinder_(
188 peer_finder::makeManager(
189 ioContext,
190 stopwatch(),
191 app_.getJournal("PeerFinder"),
192 store_,
193 collector))
194 , resolver_(resolver)
195 , nextId_(1)
196 , slots_(app, *this, app.config())
197 , stats_(
198 [this] { collectMetrics(); },
199 collector,
200 [counts = traffic_.getCounts(), collector] {
202
203 for (auto const& pair : counts)
204 ret.emplace(pair.first, TrafficGauges(pair.second.name, collector));
205
206 return ret;
207 }())
208{
209 store_.open(config);
210 beast::PropertyStream::Source::add(peerFinder_.get());
211}
212
215 std::unique_ptr<StreamType>&& streamPtr,
216 HttpRequestType&& request,
217 EndpointType remoteEndpoint)
218{
219 auto const id = nextId_++;
220 auto const peerJournal = app_.getJournal("Peer");
221 beast::WrappedSink sink(peerJournal.sink(), makePrefix(id));
222 beast::Journal const journal(sink);
223
224 Handoff handoff;
225 if (processRequest(request, handoff))
226 return handoff;
227 if (!isPeerUpgrade(request))
228 return handoff;
229
230 handoff.moved = true;
231
232 JLOG(journal.debug()) << "Peer connection upgrade from " << remoteEndpoint;
233
234 ErrorCode ec;
235 auto const localEndpoint(streamPtr->next_layer().socket().local_endpoint(ec));
236 if (ec)
237 {
238 JLOG(journal.debug()) << remoteEndpoint << " failed: " << ec.message();
239 return handoff;
240 }
241
242 auto consumer =
243 resourceManager_.newInboundEndpoint(beast::IPAddressConversion::fromAsio(remoteEndpoint));
244 if (consumer.disconnect(journal))
245 return handoff;
246
247 auto const [slot, result] = peerFinder_->newInboundSlot(
250
251 if (slot == nullptr)
252 {
253 // connection refused either IP limit exceeded or self-connect
254 handoff.moved = false;
255 JLOG(journal.debug()) << "Peer " << remoteEndpoint << " refused, " << to_string(result);
256 return handoff;
257 }
258
259 // Validate HTTP request
260
261 {
262 auto const types = beast::rfc2616::splitCommas(request["Connect-As"]);
263 if (std::ranges::find_if(types, [](std::string const& s) {
264 return boost::iequals(s, "peer");
265 }) == types.end())
266 {
267 handoff.moved = false;
268 handoff.response = makeRedirectResponse(slot, request, remoteEndpoint.address());
269 handoff.keepAlive = beast::rfc2616::isKeepAlive(request);
270 return handoff;
271 }
272 }
273
274 auto const negotiatedVersion = negotiateProtocolVersion(request["Upgrade"]);
275 if (!negotiatedVersion)
276 {
277 peerFinder_->onClosed(slot);
278 handoff.moved = false;
279 handoff.response = makeErrorResponse(
280 slot, request, remoteEndpoint.address(), "Unable to agree on a protocol version");
281 handoff.keepAlive = false;
282 return handoff;
283 }
284
285 auto const sharedValue = makeSharedValue(*streamPtr, journal);
286 if (!sharedValue)
287 {
288 peerFinder_->onClosed(slot);
289 handoff.moved = false;
290 handoff.response =
291 makeErrorResponse(slot, request, remoteEndpoint.address(), "Incorrect security cookie");
292 handoff.keepAlive = false;
293 return handoff;
294 }
295
296 try
297 {
298 auto const publicKey = verifyHandshake(
299 request,
300 *sharedValue,
301 setup_.networkID,
302 setup_.publicIp,
303 remoteEndpoint.address(),
304 app_);
305
306 consumer.setPublicKey(publicKey);
307
308 {
309 // The node gets a reserved slot if it is in our cluster
310 // or if it has a reservation.
311 bool const reserved = app_.getCluster().isMember(publicKey) ||
312 app_.getPeerReservations().contains(publicKey);
313 auto const result = peerFinder_->activate(slot, publicKey, reserved);
314 if (result != peer_finder::Result::Success)
315 {
316 peerFinder_->onClosed(slot);
317 JLOG(journal.debug())
318 << "Peer " << remoteEndpoint << " redirected, " << to_string(result);
319 handoff.moved = false;
320 handoff.response = makeRedirectResponse(slot, request, remoteEndpoint.address());
321 handoff.keepAlive = false;
322 return handoff;
323 }
324 }
325
326 auto const peer = std::make_shared<PeerImp>(
327 app_,
328 id,
329 slot,
330 std::move(request),
331 publicKey,
332 *negotiatedVersion,
333 consumer,
334 std::move(streamPtr),
335 *this);
336 {
337 // As we are not on the strand, run() must be called
338 // while holding the lock, otherwise new I/O can be
339 // queued after a call to stop().
340 std::scoped_lock const lock(mutex_);
341 {
342 auto const result = peers_.emplace(peer->slot(), peer);
343 XRPL_ASSERT(result.second, "xrpl::OverlayImpl::onHandoff : peer is inserted");
344 (void)result.second;
345 }
346 list_.emplace(peer.get(), peer);
347
348 peer->run();
349 }
350 handoff.moved = true;
351 return handoff;
352 }
353 catch (std::exception const& e)
354 {
355 JLOG(journal.debug()) << "Peer " << remoteEndpoint << " fails handshake (" << e.what()
356 << ")";
357
358 peerFinder_->onClosed(slot);
359 handoff.moved = false;
360 handoff.response = makeErrorResponse(slot, request, remoteEndpoint.address(), e.what());
361 handoff.keepAlive = false;
362 return handoff;
363 }
364}
365
366//------------------------------------------------------------------------------
367
368bool
370{
371 if (!isUpgrade(request))
372 return false;
373 auto const versions = parseProtocolVersions(request["Upgrade"]);
374 return !versions.empty();
375}
376
379{
381 ss << "[" << std::setfill('0') << std::setw(3) << id << "] ";
382 return ss.str();
383}
384
388 HttpRequestType const& request,
389 AddressType remoteAddress)
390{
391 boost::beast::http::response<JsonBody> msg;
392 msg.version(request.version());
393 msg.result(boost::beast::http::status::service_unavailable);
394 msg.insert("Server", build_info::getFullVersionString());
395 {
397 ostr << remoteAddress;
398 msg.insert("Remote-Address", ostr.str());
399 }
400 msg.insert("Content-Type", "application/json");
401 msg.insert(boost::beast::http::field::connection, "close");
402 msg.body() = json::ValueType::Object;
403 {
404 json::Value& ips = (msg.body()["peer-ips"] = json::ValueType::Array);
405 for (auto const& _ : peerFinder_->redirect(slot))
406 ips.append(_.address.toString());
407 }
408 msg.prepare_payload();
410}
411
415 HttpRequestType const& request,
416 AddressType remoteAddress,
417 std::string const& text)
418{
419 boost::beast::http::response<boost::beast::http::empty_body> msg;
420 msg.version(request.version());
421 msg.result(boost::beast::http::status::bad_request);
422 msg.reason("Bad Request (" + text + ")");
423 msg.insert("Server", build_info::getFullVersionString());
424 msg.insert("Remote-Address", remoteAddress.to_string());
425 msg.insert(boost::beast::http::field::connection, "close");
426 msg.prepare_payload();
428}
429
430//------------------------------------------------------------------------------
431
432void
434{
435 XRPL_ASSERT(work_, "xrpl::OverlayImpl::connect : work is set");
436
437 auto usage = resourceManager().newOutboundEndpoint(remoteEndpoint);
438 if (usage.disconnect(journal_))
439 {
440 JLOG(journal_.info()) << "Over resource limit: " << remoteEndpoint;
441 return;
442 }
443
444 auto const [slot, result] = peerFinder().newOutboundSlot(remoteEndpoint);
445 if (slot == nullptr)
446 {
447 JLOG(journal_.debug()) << "Connect: No slot for " << remoteEndpoint << ": "
448 << to_string(result);
449 return;
450 }
451
453 app_,
456 usage,
457 setup_.context,
458 nextId_++,
459 slot,
460 app_.getJournal("Peer"),
461 *this);
462
463 std::scoped_lock const lock(mutex_);
464 list_.emplace(p.get(), p);
465 p->run();
466}
467
468//------------------------------------------------------------------------------
469
470// Adds a peer that is already handshaked and active
471void
473{
474 beast::WrappedSink sink{journal_.sink(), peer->prefix()};
475 beast::Journal const journal{sink};
476
477 std::scoped_lock const lock(mutex_);
478
479 {
480 auto const result = peers_.emplace(peer->slot(), peer);
481 XRPL_ASSERT(result.second, "xrpl::OverlayImpl::addActive : peer is inserted");
482 (void)result.second;
483 }
484
485 {
486 auto const result = ids_.emplace(
488 XRPL_ASSERT(result.second, "xrpl::OverlayImpl::addActive : peer ID is inserted");
489 (void)result.second;
490 }
491
492 list_.emplace(peer.get(), peer);
493
494 JLOG(journal.debug()) << "activated";
495
496 // As we are not on the strand, run() must be called
497 // while holding the lock, otherwise new I/O can be
498 // queued after a call to stop().
499 peer->run();
500}
501
502void
504{
505 std::scoped_lock const lock(mutex_);
506 auto const iter = peers_.find(slot);
507 XRPL_ASSERT(iter != peers_.end(), "xrpl::OverlayImpl::remove : valid input");
508 peers_.erase(iter);
509}
510
511// Well-known high-capacity servers to bootstrap from when [ips] and [ips_fixed] are empty.
512static constexpr auto kDefaultBootstrapIps = std::to_array<std::string_view>({
513 "r.ripple.com 51235", // Ripple Labs Inc. - https://ripple.com
514 "sahyadri.isrdc.in 51235", // ISRDC - https://isrdc.in
515 "hubs.xrpkuwait.com 51235", // @Xrpkuwait - https://xrpkuwait.com
516 "hub.xrpl-commons.org 51235", // XRPL Commons - https://xrpl-commons.org
517});
518
519// Resolved names without an explicit port get the default peer port.
522{
524 endpoints.reserve(addresses.size());
525 for (auto const& addr : addresses)
526 endpoints.push_back(addr.port() == 0 ? addr.atPort(kDefaultPeerPort) : addr);
527 return endpoints;
528}
529
530void
532{
534 app_.config(),
535 serverHandler_.setup().overlay.port(),
536 app_.getValidationPublicKey().has_value(),
537 setup_.ipLimit,
538 setup_.verifyEndpoints);
539
540 peerFinder_->setConfig(config);
541 peerFinder_->start();
542
543 // Standalone servers (including unit tests) never connect to peers, so skip the DNS lookups.
544 if (!app_.config().standalone())
545 {
546 // Populate our boot cache: if there are no entries in [ips] then we use
547 // the entries in [ips_fixed], falling back to the well-known defaults.
548 auto bootstrapIps = app_.config().ips.empty() ? app_.config().ipsFixed : app_.config().ips;
549 if (bootstrapIps.empty())
550 bootstrapIps.assign(kDefaultBootstrapIps.begin(), kDefaultBootstrapIps.end());
551
552 resolver_.resolve(
553 bootstrapIps,
554 [this](std::string const& name, std::vector<beast::ip::Endpoint> const& addresses) {
556 ips.reserve(addresses.size());
557 for (auto const& endpoint : withDefaultPeerPort(addresses))
558 ips.push_back(to_string(endpoint));
559
560 std::string const base("config: ");
561 if (!ips.empty())
562 peerFinder_->addFallbackStrings(base + name, ips);
563 });
564
565 // Add the ips_fixed from the xrpld.cfg file
566 if (!app_.config().ipsFixed.empty())
567 {
568 resolver_.resolve(
569 app_.config().ipsFixed,
570 [this](std::string const& name, std::vector<beast::ip::Endpoint> const& addresses) {
571 auto const ips = withDefaultPeerPort(addresses);
572 if (!ips.empty())
573 peerFinder_->addFixedPeer(name, ips);
574 });
575 }
576 }
577
578 auto const timer = std::make_shared<Timer>(*this);
579 std::scoped_lock const lock(mutex_);
580 list_.emplace(timer.get(), timer);
581 timer_ = timer;
582 timer->asyncWait();
583}
584
585void
587{
588 boost::asio::dispatch(strand_, [this] { stopChildren(); });
589 {
590 std::unique_lock<decltype(mutex_)> lock(mutex_);
591 cond_.wait(lock, [this] { return list_.empty(); });
592 }
593 peerFinder_->stop();
594}
595
596//------------------------------------------------------------------------------
597//
598// PropertyStream
599//
600//------------------------------------------------------------------------------
601
602void
604{
605 beast::PropertyStream::Set set("traffic", stream);
606 auto const stats = traffic_.getCounts();
607 for (auto const& pair : stats)
608 {
610 item["category"] = pair.second.name;
611 item["bytes_in"] = std::to_string(pair.second.bytesIn.load());
612 item["messages_in"] = std::to_string(pair.second.messagesIn.load());
613 item["bytes_out"] = std::to_string(pair.second.bytesOut.load());
614 item["messages_out"] = std::to_string(pair.second.messagesOut.load());
615 }
616}
617
618//------------------------------------------------------------------------------
625void
627{
628 beast::WrappedSink sink{journal_.sink(), peer->prefix()};
629 beast::Journal const journal{sink};
630
631 // Now track this peer
632 {
633 std::scoped_lock const lock(mutex_);
634 auto const result(ids_.emplace(
636 XRPL_ASSERT(result.second, "xrpl::OverlayImpl::activate : peer ID is inserted");
637 (void)result.second;
638 }
639
640 JLOG(journal.debug()) << "activated";
641
642 // We just accepted this peer so we have non-zero active peers
643 XRPL_ASSERT(size(), "xrpl::OverlayImpl::activate : nonzero peers");
644}
645
646void
648{
649 std::scoped_lock const lock(mutex_);
650 ids_.erase(id);
651}
652
653void
656 std::shared_ptr<PeerImp> const& from)
657{
658 auto const& journal = from->pJournal();
659
660 // Process every trusted manifest, but stop processing untrusted ones once
661 // the configured untrusted count has been handled, so the work stays
662 // bounded. Trusted manifests are always processed: dropping one would delay
663 // a validator key rotation reaching this node.
664 auto const maxUntrusted = untrustedManifestCount(app_.config().maxUntrustedCount);
665 auto const total = static_cast<std::size_t>(m->list_size());
666 std::size_t untrusted = 0;
667 bool skippedUntrusted = false;
668
669 protocol::TMManifests relay;
670
671 for (std::size_t i = 0; i < total; ++i)
672 {
673 auto& s = m->list().Get(i).stobject();
674
675 if (auto mo = deserializeManifest(s))
676 {
677 auto const serialized = mo->serialized;
678 // Resolve trust before applyManifest takes the manifest-cache
679 // lock: listed() takes the validator-list lock, so ordering it
680 // first avoids holding the two locks in opposite orders.
681 bool const isTrusted = app_.getValidators().listed(mo->masterKey);
682
683 // Bound untrusted work: process at most maxUntrusted untrusted
684 // manifests, but never skip a trusted one. Trusted manifests are
685 // not counted against the cap.
686 if (!isTrusted)
687 {
688 if (untrusted >= maxUntrusted)
689 {
690 skippedUntrusted = true;
691 continue;
692 }
693 ++untrusted;
694 }
695
696 auto const result = app_.getValidatorManifests().applyManifest(
697 std::move(*mo),
700
701 if (result == ManifestDisposition::Accepted)
702 {
703 // N.B.: this is important; the applyManifest call above moves
704 // the loaded Manifest out of the optional so we need to
705 // reload it here.
706 mo = deserializeManifest(serialized);
707 XRPL_ASSERT(
708 mo,
709 "xrpl::OverlayImpl::onManifests : manifest "
710 "deserialization succeeded");
711 // NOLINTBEGIN(bugprone-unchecked-optional-access) assert above
712 app_.getOPs().pubManifest(*mo);
713 // NOLINTEND(bugprone-unchecked-optional-access)
714
715 relay.add_list()->set_stobject(s);
716
717 // Persist to the wallet DB only for trusted keys, so untrusted
718 // gossip never survives a restart.
719 if (isTrusted)
720 {
721 auto db = app_.getWalletDB().checkoutDb();
722 addValidatorManifest(*db, serialized);
723 }
724 }
725 }
726 else
727 {
728 JLOG(journal.debug()) << "Malformed manifest #" << i + 1 << ": " << strHex(s);
729 continue;
730 }
731 }
732
733 if (skippedUntrusted)
734 {
735 // The sender exceeded the untrusted per-message cap. Charge it (once,
736 // here) so a flood of untrusted manifests is penalized.
737 from->charge(resource::kFeeMalformedRequest, "too many untrusted manifests");
738
739 JLOG(journal.warn()) << "Manifests: message had " << total
740 << " entries; processed all trusted plus the first " << maxUntrusted
741 << " untrusted";
742 }
743
744 if (!relay.list().empty())
745 {
746 forEach([m2 = std::make_shared<Message>(relay, protocol::mtMANIFESTS)](
747 std::shared_ptr<PeerImp> const& p) { p->send(m2); });
748 }
749}
750
751void
753{
754 traffic_.addCount(cat, true, size);
755}
756
757void
759{
760 traffic_.addCount(cat, false, size);
761}
762
769{
770 std::scoped_lock const lock(mutex_);
771 return ids_.size();
772}
773
774int
776{
777 return peerFinder_->config().maxPeers;
778}
779
782{
783 using namespace std::chrono;
784 json::Value jv;
785 auto& av = jv[jss::active] = json::Value(json::ValueType::Array);
786
787 forEach([&](std::shared_ptr<PeerImp> const& sp) {
788 auto& pv = av.append(json::Value(json::ValueType::Object));
789 pv[jss::public_key] = base64Encode(sp->getNodePublic().data(), sp->getNodePublic().size());
790 pv[jss::type] = sp->slot()->inbound() ? jss::in : jss::out;
791 pv[jss::uptime] = static_cast<std::uint32_t>(duration_cast<seconds>(sp->uptime()).count());
792 if (sp->crawl())
793 {
794 pv[jss::ip] = sp->getRemoteAddress().address().to_string();
795 if (sp->slot()->inbound())
796 {
797 if (auto port = sp->slot()->listeningPort())
798 pv[jss::port] = *port;
799 }
800 else
801 {
802 pv[jss::port] = sp->getRemoteAddress().port();
803 }
804 }
805
806 {
807 auto const version{sp->getVersion()};
808 if (!version.empty())
809 {
810 // Could move here if json::value supported moving from strings
811 pv[jss::version] = std::string{version};
812 }
813 }
814
815 std::uint32_t minSeq = 0, maxSeq = 0;
816 sp->ledgerRange(minSeq, maxSeq);
817 if (minSeq != 0 || maxSeq != 0)
818 pv[jss::complete_ledgers] = std::to_string(minSeq) + "-" + std::to_string(maxSeq);
819 });
820
821 return jv;
822}
823
826{
827 bool const humanReadable = false;
828 bool const admin = false;
829 bool const counters = false;
830
831 json::Value serverInfo = app_.getOPs().getServerInfo(humanReadable, admin, counters);
832
833 // Filter out some information
834 serverInfo.removeMember(jss::hostid);
835 serverInfo.removeMember(jss::load_factor_fee_escalation);
836 serverInfo.removeMember(jss::load_factor_fee_queue);
837 serverInfo.removeMember(jss::validation_quorum);
838
839 if (serverInfo.isMember(jss::validated_ledger))
840 {
841 json::Value& validatedLedger = serverInfo[jss::validated_ledger];
842
843 validatedLedger.removeMember(jss::base_fee);
844 validatedLedger.removeMember(jss::reserve_base_xrp);
845 validatedLedger.removeMember(jss::reserve_inc_xrp);
846 }
847
848 return serverInfo;
849}
850
856
859{
860 json::Value validators = app_.getValidators().getJson();
861
862 if (validators.isMember(jss::publisher_lists))
863 {
864 json::Value& publisherLists = validators[jss::publisher_lists];
865
866 for (auto& publisher : publisherLists)
867 {
868 publisher.removeMember(jss::list);
869 }
870 }
871
872 validators.removeMember(jss::signing_keys);
873 validators.removeMember(jss::trusted_validator_keys);
874 validators.removeMember(jss::validation_quorum);
875
876 json::Value validatorSites = app_.getValidatorSites().getJson();
877
878 if (validatorSites.isMember(jss::validator_sites))
879 {
880 validators[jss::validator_sites] = std::move(validatorSites[jss::validator_sites]);
881 }
882
883 return validators;
884}
885
886// Returns information on verified peers.
889{
891 for (auto const& peer : getActivePeers())
892 {
893 json.append(peer->json());
894 }
895 return json;
896}
897
898bool
900{
901 if (req.target() != "/crawl" || setup_.crawlOptions == crawl_options::kDisabled)
902 return false;
903
904 boost::beast::http::response<JsonBody> msg;
905 msg.version(req.version());
906 msg.result(boost::beast::http::status::ok);
907 msg.insert("Server", build_info::getFullVersionString());
908 msg.insert("Content-Type", "application/json");
909 msg.insert("Connection", "close");
910 msg.body()["version"] = json::Value(2u);
911
912 if ((setup_.crawlOptions & crawl_options::kOverlay) != 0u)
913 {
914 msg.body()["overlay"] = getOverlayInfo();
915 }
916 if ((setup_.crawlOptions & crawl_options::kServerInfo) != 0u)
917 {
918 msg.body()["server"] = getServerInfo();
919 }
920 if ((setup_.crawlOptions & crawl_options::kServerCounts) != 0u)
921 {
922 msg.body()["counts"] = getServerCounts();
923 }
924 if ((setup_.crawlOptions & crawl_options::kUnl) != 0u)
925 {
926 msg.body()["unl"] = getUnlInfo();
927 }
928
929 msg.prepare_payload();
931 return true;
932}
933
934bool
936{
937 // If the target is in the form "/vl/<validator_list_public_key>",
938 // return the most recent validator list for that key.
939 constexpr std::string_view kPrefix("/vl/");
940
941 if (!req.target().starts_with(kPrefix) || !setup_.vlEnabled)
942 return false;
943
944 std::uint32_t version = 1;
945
946 boost::beast::http::response<JsonBody> msg;
947 msg.version(req.version());
948 msg.insert("Server", build_info::getFullVersionString());
949 msg.insert("Content-Type", "application/json");
950 msg.insert("Connection", "close");
951
952 auto const fail = [&msg, &handoff](auto status) {
953 msg.result(status);
954 msg.insert("Content-Length", "0");
955
956 msg.body() = json::ValueType::Null;
957
958 msg.prepare_payload();
960 return true;
961 };
962
963 std::string_view key = req.target().substr(kPrefix.size());
964
965 if (auto const slash = key.find('/'); slash != std::string_view::npos)
966 {
967 auto const verString = key.substr(0, slash);
968 if (!boost::conversion::try_lexical_convert(verString, version))
969 return fail(boost::beast::http::status::bad_request);
970 key = key.substr(slash + 1);
971 }
972
973 if (key.empty())
974 return fail(boost::beast::http::status::bad_request);
975
976 // find the list
977 auto vl = app_.getValidators().getAvailable(key, version);
978
979 if (!vl)
980 {
981 // 404 not found
982 return fail(boost::beast::http::status::not_found);
983 }
984 if (!*vl)
985 {
986 return fail(boost::beast::http::status::bad_request);
987 }
988
989 msg.result(boost::beast::http::status::ok);
990
991 msg.body() = *vl;
992
993 msg.prepare_payload();
995 return true;
996}
997
998bool
1000{
1001 if (req.target() != "/health")
1002 return false;
1003 boost::beast::http::response<JsonBody> msg;
1004 msg.version(req.version());
1005 msg.insert("Server", build_info::getFullVersionString());
1006 msg.insert("Content-Type", "application/json");
1007 msg.insert("Connection", "close");
1008
1009 auto info = getServerInfo();
1010
1011 int lastValidatedLedgerAge = -1;
1012 if (info.isMember(jss::validated_ledger))
1013 lastValidatedLedgerAge = info[jss::validated_ledger][jss::age].asInt();
1014 bool amendmentBlocked = false;
1015 if (info.isMember(jss::amendment_blocked))
1016 amendmentBlocked = true;
1017 int const numberPeers = info[jss::peers].asInt();
1018 std::string const serverState = info[jss::server_state].asString();
1019 auto const loadFactor =
1020 info[jss::load_factor_server].asDouble() / info[jss::load_base].asDouble();
1021
1022 enum class HealthState { Healthy, Warning, Critical };
1023 auto health = HealthState::Healthy;
1024 auto const setHealth = [&health](HealthState state) { health = std::max(health, state); };
1025
1026 msg.body()[jss::info] = json::ValueType::Object;
1027 if (lastValidatedLedgerAge >= 7 || lastValidatedLedgerAge < 0)
1028 {
1029 msg.body()[jss::info][jss::validated_ledger] = lastValidatedLedgerAge;
1030 if (lastValidatedLedgerAge < 20)
1031 {
1032 setHealth(HealthState::Warning);
1033 }
1034 else
1035 {
1036 setHealth(HealthState::Critical);
1037 }
1038 }
1039
1040 if (amendmentBlocked)
1041 {
1042 msg.body()[jss::info][jss::amendment_blocked] = true;
1043 setHealth(HealthState::Critical);
1044 }
1045
1046 if (numberPeers <= 7)
1047 {
1048 msg.body()[jss::info][jss::peers] = numberPeers;
1049 if (numberPeers != 0)
1050 {
1051 setHealth(HealthState::Warning);
1052 }
1053 else
1054 {
1055 setHealth(HealthState::Critical);
1056 }
1057 }
1058
1059 if (!(serverState == "full" || serverState == "validating" || serverState == "proposing"))
1060 {
1061 msg.body()[jss::info][jss::server_state] = serverState;
1062 if (serverState == "syncing" || serverState == "tracking" || serverState == "connected")
1063 {
1064 setHealth(HealthState::Warning);
1065 }
1066 else
1067 {
1068 setHealth(HealthState::Critical);
1069 }
1070 }
1071
1072 if (loadFactor > 100)
1073 {
1074 msg.body()[jss::info][jss::load_factor] = loadFactor;
1075 if (loadFactor < 1000)
1076 {
1077 setHealth(HealthState::Warning);
1078 }
1079 else
1080 {
1081 setHealth(HealthState::Critical);
1082 }
1083 }
1084
1085 switch (health)
1086 {
1087 case HealthState::Healthy:
1088 msg.result(boost::beast::http::status::ok);
1089 break;
1090 case HealthState::Warning:
1091 msg.result(boost::beast::http::status::service_unavailable);
1092 break;
1093 case HealthState::Critical:
1094 msg.result(boost::beast::http::status::internal_server_error);
1095 break;
1096 }
1097
1098 msg.prepare_payload();
1100 return true;
1101}
1102
1103bool
1105{
1106 // Take advantage of || short-circuiting
1107 return processCrawl(req, handoff) || processValidatorList(req, handoff) ||
1108 processHealth(req, handoff);
1109}
1110
1113{
1115 ret.reserve(size());
1116
1117 forEach([&ret](std::shared_ptr<PeerImp> const& sp) { ret.emplace_back(sp); });
1118
1119 return ret;
1120}
1121
1124 std::set<Peer::ID> const& toSkip,
1125 std::size_t& active,
1126 std::size_t& disabled,
1127 std::size_t& enabledInSkip) const
1128{
1131
1132 active = ids_.size();
1133 disabled = enabledInSkip = 0;
1134 ret.reserve(ids_.size());
1135
1136 // NOTE The purpose of p is to delay the destruction of PeerImp
1138 for (auto& [id, w] : ids_)
1139 {
1140 if (p = w.lock(); p != nullptr)
1141 {
1142 bool const reduceRelayEnabled = p->txReduceRelayEnabled();
1143 // tx reduced relay feature disabled
1144 if (!reduceRelayEnabled)
1145 ++disabled;
1146
1147 if (!toSkip.contains(id))
1148 {
1149 ret.emplace_back(std::move(p));
1150 }
1151 else if (reduceRelayEnabled)
1152 {
1153 ++enabledInSkip;
1154 }
1155 }
1156 }
1157
1158 return ret;
1159}
1160
1161void
1163{
1164 forEach([index](std::shared_ptr<PeerImp> const& sp) { sp->checkTracking(index); });
1165}
1166
1169{
1171 auto const iter = ids_.find(id);
1172 if (iter != ids_.end())
1173 return iter->second.lock();
1174 return {};
1175}
1176
1177// A public key hash map was not used due to the peer connect/disconnect
1178// update overhead outweighing the performance of a small set linear search.
1181{
1183 // NOTE The purpose of peer is to delay the destruction of PeerImp
1185 for (auto const& e : ids_)
1186 {
1187 if (peer = e.second.lock(); peer != nullptr)
1188 {
1189 if (peer->getNodePublic() == pubKey)
1190 return peer;
1191 }
1192 }
1193 return {};
1194}
1195
1196void
1197OverlayImpl::broadcast(protocol::TMProposeSet const& m)
1198{
1199 auto const sm = std::make_shared<Message>(m, protocol::mtPROPOSE_LEDGER);
1200 forEach([&](std::shared_ptr<PeerImp> const& p) { p->send(sm); });
1201}
1202
1204OverlayImpl::relay(protocol::TMProposeSet const& m, UInt256 const& uid, PublicKey const& validator)
1205{
1206 if (auto const toSkip = app_.getHashRouter().shouldRelay(uid))
1207 {
1208 auto const sm = std::make_shared<Message>(m, protocol::mtPROPOSE_LEDGER, validator);
1209 forEach([&](std::shared_ptr<PeerImp> const& p) {
1210 if (!toSkip->contains(p->id()))
1211 p->send(sm);
1212 });
1213 return *toSkip;
1214 }
1215 return {};
1216}
1217
1218void
1219OverlayImpl::broadcast(protocol::TMValidation const& m)
1220{
1221 auto const sm = std::make_shared<Message>(m, protocol::mtVALIDATION);
1222 forEach([sm](std::shared_ptr<PeerImp> const& p) { p->send(sm); });
1223}
1224
1226OverlayImpl::relay(protocol::TMValidation const& m, UInt256 const& uid, PublicKey const& validator)
1227{
1228 if (auto const toSkip = app_.getHashRouter().shouldRelay(uid))
1229 {
1230 auto const sm = std::make_shared<Message>(m, protocol::mtVALIDATION, validator);
1231 forEach([&](std::shared_ptr<PeerImp> const& p) {
1232 if (!toSkip->contains(p->id()))
1233 p->send(sm);
1234 });
1235 return *toSkip;
1236 }
1237 return {};
1238}
1239
1242{
1244
1245 if (auto seq = app_.getValidatorManifests().sequence(); seq != manifestListSeq_)
1246 {
1247 // Phase 1: snapshot the cache under its own lock. Do not call
1248 // Validators::listed() here — that takes the validator-list lock, and
1249 // forEachManifest holds the manifest-cache lock, so consulting trust
1250 // inside the callback would invert the lock order used elsewhere
1251 // (see onManifests) and risk deadlock. Capture the manifest hash now,
1252 // while we have the Manifest object, for the suppression key.
1253 struct CachedManifest
1254 {
1255 PublicKey masterKey;
1256 std::string serialized;
1257 UInt256 hash;
1258 };
1260 app_.getValidatorManifests().forEachManifest(
1261 [&cached](std::size_t s) { cached.reserve(s); },
1262 [&cached](Manifest const& manifest) {
1263 cached.push_back({
1264 .masterKey = manifest.masterKey,
1265 .serialized = manifest.serialized,
1266 .hash = manifest.hash(),
1267 });
1268 });
1269
1270 // Phase 2: no cache lock held, so trust checks are safe. Include every
1271 // trusted manifest, then fill any remaining headroom up to the
1272 // configured untrusted count with gossip. Trusted manifests are never
1273 // dropped; the trusted count only sizes the accepted message.
1276 for (auto const& e : cached)
1277 {
1278 if (app_.getValidators().listed(e.masterKey))
1279 {
1280 selected.push_back(&e);
1281 }
1282 else
1283 {
1284 untrusted.push_back(&e);
1285 }
1286 }
1287
1288 // Cap untrusted only; trusted manifests are all included above.
1289 auto const take =
1290 std::min(untrustedManifestCount(app_.config().maxUntrustedCount), untrusted.size());
1291 selected.insert(selected.end(), untrusted.begin(), untrusted.begin() + take);
1292
1293 // Shuffle the order. Cryptographic randomness is not needed here.
1294 std::shuffle(selected.begin(), selected.end(), defaultPrng());
1295
1296 protocol::TMManifests tm;
1297 auto& hr = app_.getHashRouter();
1298 tm.mutable_list()->Reserve(static_cast<int>(selected.size()));
1299 for (auto const* e : selected)
1300 {
1301 tm.add_list()->set_stobject(e->serialized.data(), e->serialized.size());
1302 hr.addSuppression(e->hash);
1303 }
1304
1305 manifestMessage_.reset();
1306
1307 if (tm.list_size() != 0)
1308 manifestMessage_ = std::make_shared<Message>(tm, protocol::mtMANIFESTS);
1309
1310 manifestListSeq_ = seq;
1311 }
1312
1313 return manifestMessage_;
1314}
1315
1316void
1318 UInt256 const& hash,
1320 std::set<Peer::ID> const& toSkip)
1321{
1322 if (tx)
1323 {
1324 auto const& txn = tx->get();
1325 SerialIter sit(makeSlice(txn.rawtransaction()));
1326 try
1327 {
1328 if (isPseudoTx(STTx{sit}))
1329 tx.reset();
1330 }
1331 catch (std::exception const&)
1332 {
1333 // Could not construct STTx, not relaying
1334 JLOG(journal_.debug()) << "Could not construct STTx: " << hash;
1335 return;
1336 }
1337 }
1338
1339 relayChecked(hash, tx, toSkip);
1340}
1341
1342void
1344 STTx const& tx,
1345 protocol::TMTransaction const& m,
1346 std::set<Peer::ID> const& toSkip)
1347{
1348 if (isPseudoTx(tx))
1349 {
1350 relayChecked(tx.getTransactionID(), std::nullopt, toSkip);
1351 }
1352 else
1353 {
1354 relayChecked(tx.getTransactionID(), m, toSkip);
1355 }
1356}
1357
1358void
1360 UInt256 const& hash,
1362 std::set<Peer::ID> const& toSkip)
1363{
1364 bool const relay = tx.has_value();
1365
1366 Overlay::PeerSequence peers = {};
1367 std::size_t total = 0;
1368 std::size_t disabled = 0;
1369 std::size_t enabledInSkip = 0;
1370
1371 if (!relay)
1372 {
1373 if (!app_.config().txReduceRelayEnable)
1374 return;
1375
1376 peers = getActivePeers(toSkip, total, disabled, enabledInSkip);
1377 JLOG(journal_.trace()) << "not relaying tx, total peers " << peers.size();
1378 for (auto const& p : peers)
1379 {
1380 if (p->txReduceRelayEnabled())
1381 p->addTxQueue(hash);
1382 }
1383 return;
1384 }
1385
1386 auto const& txn = tx->get();
1387 auto const sm = std::make_shared<Message>(txn, protocol::mtTRANSACTION);
1388 peers = getActivePeers(toSkip, total, disabled, enabledInSkip);
1389 auto const minRelay = app_.config().txReduceRelayMinPeers + disabled;
1390
1391 if (!app_.config().txReduceRelayEnable || total <= minRelay)
1392 {
1393 for (auto const& p : peers)
1394 p->send(sm);
1395 if (app_.config().txReduceRelayEnable || app_.config().txReduceRelayMetrics)
1396 txMetrics_.addMetrics(total, toSkip.size(), 0);
1397 return;
1398 }
1399
1400 // We have more peers than the minimum (disabled + minimum enabled),
1401 // relay to all disabled and some randomly selected enabled that
1402 // do not have the transaction.
1403 auto const enabledTarget = app_.config().txReduceRelayMinPeers +
1404 ((total - minRelay) * app_.config().txRelayPercentage / 100);
1405
1406 txMetrics_.addMetrics(enabledTarget, toSkip.size(), disabled);
1407
1408 if (enabledTarget > enabledInSkip)
1409 std::shuffle(peers.begin(), peers.end(), defaultPrng());
1410
1411 JLOG(journal_.trace()) << "relaying tx, total peers " << peers.size() << " selected "
1412 << enabledTarget << " skip " << toSkip.size() << " disabled "
1413 << disabled;
1414
1415 // count skipped peers with the enabled feature towards the quota
1416 std::uint16_t enabledAndRelayed = enabledInSkip;
1417 for (auto const& p : peers)
1418 {
1419 // always relay to a peer with the disabled feature
1420 if (!p->txReduceRelayEnabled())
1421 {
1422 p->send(sm);
1423 }
1424 else if (enabledAndRelayed < enabledTarget)
1425 {
1426 enabledAndRelayed++;
1427 p->send(sm);
1428 }
1429 else
1430 {
1431 p->addTxQueue(hash);
1432 }
1433 }
1434}
1435
1436//------------------------------------------------------------------------------
1437
1438void
1440{
1442 list_.erase(&child);
1443 if (list_.empty())
1444 cond_.notify_all();
1445}
1446
1447void
1449{
1450 // Calling list_[].second->stop() may cause list_ to be modified
1451 // (OverlayImpl::remove() may be called on this same thread). So
1452 // iterating directly over list_ to call child->stop() could lead to
1453 // undefined behavior.
1454 //
1455 // Therefore we copy all of the weak/shared ptrs out of list_ before we
1456 // start calling stop() on them. That guarantees OverlayImpl::remove()
1457 // won't be called until vector<> children leaves scope.
1459 {
1461 if (!work_)
1462 return;
1463 work_ = std::nullopt;
1464
1465 children.reserve(list_.size());
1466 for (auto const& element : list_)
1467 {
1468 children.emplace_back(element.second.lock());
1469 }
1470 } // lock released
1471
1472 for (auto const& child : children)
1473 {
1474 if (child != nullptr)
1475 child->stop();
1476 }
1477}
1478
1479void
1481{
1482 auto const result = peerFinder_->autoconnect();
1483 for (auto const& addr : result)
1484 connect(addr);
1485}
1486
1487void
1489{
1490 auto const result = peerFinder_->buildEndpointsForPeers();
1491 for (auto const& e : result)
1492 {
1494 {
1496 auto const iter = peers_.find(e.first);
1497 if (iter != peers_.end())
1498 peer = iter->second.lock();
1499 }
1500 if (peer)
1501 peer->sendEndpoints(e.second.begin(), e.second.end());
1502 }
1503}
1504
1505void
1507{
1508 forEach([](auto const& p) {
1509 if (p->txReduceRelayEnabled())
1510 p->sendTxQueue();
1511 });
1512}
1513
1515makeSquelchMessage(PublicKey const& validator, bool squelch, uint32_t squelchDuration)
1516{
1517 protocol::TMSquelch m;
1518 m.set_squelch(squelch);
1519 m.set_validatorpubkey(validator.data(), validator.size());
1520 if (squelch)
1521 m.set_squelchduration(squelchDuration);
1522 return std::make_shared<Message>(m, protocol::mtSQUELCH);
1523}
1524
1525void
1526OverlayImpl::unsquelch(PublicKey const& validator, Peer::ID id) const
1527{
1528 if (auto const peer = findPeerByShortID(id); peer)
1529 {
1530 // optimize - multiple message with different
1531 // validator might be sent to the same peer
1532 peer->send(makeSquelchMessage(validator, false, 0));
1533 }
1534}
1535
1536void
1537OverlayImpl::squelch(PublicKey const& validator, Peer::ID id, uint32_t squelchDuration) const
1538{
1539 if (auto const peer = findPeerByShortID(id); peer)
1540 {
1541 peer->send(makeSquelchMessage(validator, true, squelchDuration));
1542 }
1543}
1544
1545void
1547 UInt256 const& key,
1548 PublicKey const& validator,
1549 std::set<Peer::ID>&& peers,
1550 protocol::MessageType type)
1551{
1552 if (!slots_.baseSquelchReady())
1553 return;
1554
1555 if (!strand_.running_in_this_thread())
1556 {
1557 post(
1558 strand_,
1559 // Must capture copies of reference parameters (i.e. key, validator)
1560 [this, key = key, validator = validator, peers = std::move(peers), type] mutable {
1561 updateSlotAndSquelch(key, validator, std::move(peers), type);
1562 });
1563
1564 return;
1565 }
1566
1567 for (auto const id : peers)
1568 {
1569 slots_.updateSlotAndSquelch(key, validator, id, type, [&] {
1571 });
1572 }
1573}
1574
1575void
1577 UInt256 const& key,
1578 PublicKey const& validator,
1579 Peer::ID peer,
1580 protocol::MessageType type)
1581{
1582 if (!slots_.baseSquelchReady())
1583 return;
1584
1585 if (!strand_.running_in_this_thread())
1586 {
1587 {
1588 post(
1589 strand_,
1590 // Must capture copies of reference parameters (i.e. key, validator)
1591 [this, key = key, validator = validator, peer, type] {
1592 updateSlotAndSquelch(key, validator, peer, type);
1593 });
1594 }
1595 return;
1596 }
1597
1598 slots_.updateSlotAndSquelch(key, validator, peer, type, [&] {
1600 });
1601}
1602
1603void
1605{
1606 if (!strand_.running_in_this_thread())
1607 {
1608 post(strand_, [this, id] { deletePeer(id); });
1609 return;
1610 }
1611
1612 slots_.deletePeer(id, true);
1613}
1614
1615void
1617{
1618 if (!strand_.running_in_this_thread())
1619 {
1620 post(strand_, [this] { deleteIdlePeers(); });
1621 return;
1622 }
1623
1624 slots_.deleteIdlePeers();
1625}
1626
1627//------------------------------------------------------------------------------
1628
1631{
1632 Overlay::Setup setup;
1633
1634 {
1635 auto const& section = config.section(Sections::kOverlay);
1636 setup.context = makeSslContext("");
1637
1638 set(setup.ipLimit, "ip_limit", section);
1639 if (setup.ipLimit < 0)
1640 Throw<std::runtime_error>("Configured IP limit is invalid");
1641
1642 std::string ip;
1643 set(ip, "public_ip", section);
1644 if (!ip.empty())
1645 {
1646 boost::system::error_code ec;
1647 setup.publicIp = boost::asio::ip::make_address(ip, ec);
1648 if (ec || !beast::ip::isPublic(setup.publicIp))
1649 Throw<std::runtime_error>("Configured public IP is invalid");
1650 }
1651
1652 set(setup.verifyEndpoints, true, "verify_endpoints", section);
1653 if (!setup.verifyEndpoints)
1654 {
1655 JLOG(j.warn()) << "Endpoint verification is disabled. This is a "
1656 "security risk and should only be used for "
1657 "testing.";
1658 }
1659 }
1660
1661 {
1662 auto const& section = config.section(Sections::kCrawl);
1663 auto const& values = section.values();
1664
1665 if (values.size() > 1)
1666 {
1667 Throw<std::runtime_error>("Configured [crawl] section is invalid, too many values");
1668 }
1669
1670 bool crawlEnabled = true;
1671
1672 // Only allow "0|1" as a value
1673 if (values.size() == 1)
1674 {
1675 try
1676 {
1677 crawlEnabled = boost::lexical_cast<bool>(values.front());
1678 }
1679 catch (boost::bad_lexical_cast const&)
1680 {
1682 "Configured [crawl] section has invalid value: {}", values.front()));
1683 }
1684 }
1685
1686 if (crawlEnabled)
1687 {
1688 if (get<bool>(section, Keys::kOverlay, true))
1689 {
1691 }
1692 if (get<bool>(section, Keys::kServer, true))
1693 {
1695 }
1696 if (get<bool>(section, Keys::kCounts, false))
1697 {
1699 }
1700 if (get<bool>(section, Keys::kUnl, true))
1701 {
1703 }
1704 }
1705 }
1706 {
1707 auto const& section = config.section(Sections::kVl);
1708
1709 set(setup.vlEnabled, "enabled", section);
1710 }
1711
1712 try
1713 {
1714 auto id = config.legacy(Sections::kNetworkId);
1715
1716 if (!id.empty())
1717 {
1718 if (id == "main")
1719 id = "0";
1720
1721 if (id == "testnet")
1722 id = "1";
1723
1724 if (id == "devnet")
1725 id = "2";
1726
1728 }
1729 }
1730 catch (...)
1731 {
1733 "Configured [network_id] section is invalid: must be a number "
1734 "or one of the strings 'main', 'testnet' or 'devnet'.");
1735 }
1736
1737 return setup;
1738}
1739
1742 Application& app,
1743 Overlay::Setup const& setup,
1744 ServerHandler& serverHandler,
1745 resource::Manager& resourceManager,
1746 Resolver& resolver,
1747 boost::asio::io_context& ioContext,
1748 BasicConfig const& config,
1749 beast::insight::Collector::Ptr const& collector)
1750{
1752 app, setup, serverHandler, resourceManager, resolver, ioContext, config, collector);
1753}
1754
1755} // namespace xrpl
T begin(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
Stream debug() const
Definition Journal.h:344
Stream warn() const
Definition Journal.h:356
std::string const & name() const
Returns the name of this source.
void add(Source &source)
Add a child source.
Wraps a Journal::Sink to prefix its output with a string.
Definition WrappedSink.h:19
std::shared_ptr< Collector > Ptr
Definition Collector.h:29
A version-independent IP address and port combination.
Definition IPEndpoint.h:24
Represents a JSON value.
Definition json_value.h:121
Value removeMember(char const *key)
Remove and return the named member.
Value & append(Value const &value)
Append value to array at the end.
bool isMember(char const *key) const
Return true if the object has a member named key.
Holds unparsed configuration information.
void legacy(std::string const &section, std::string value)
Set a value that is not a key/value pair.
Section & section(std::string const &name)
Returns the section with the given name.
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
Definition OverlayImpl.h:90
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.
Resolver & resolver_
void addActive(std::shared_ptr< PeerImp > const &peer)
std::atomic< Peer::ID > nextId_
void broadcast(protocol::TMProposeSet const &m) override
Broadcast a proposal.
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::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()
void remove(std::shared_ptr< peer_finder::Slot > const &slot)
void onPeerDeactivate(Peer::ID id)
void stop() override
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::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.
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::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_
Application & app_
json::Value getServerCounts()
Returns information about the local server's performance counters.
std::recursive_mutex mutex_
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.
std::mutex manifestLock_
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
HashMap< Peer::ID, std::weak_ptr< PeerImp > > ids_
boost::asio::ip::address AddressType
Definition OverlayImpl.h:88
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.
TrafficCount traffic_
boost::asio::ip::tcp::endpoint EndpointType
Definition OverlayImpl.h:89
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)
void relayChecked(UInt256 const &hash, std::optional< std::reference_wrapper< protocol::TMTransaction const > > tx, std::set< Peer::ID > const &toSkip)
Relay tx if present, else only queue its hash for peers that have tx reduce-relay enabled.
void start() override
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.
std::vector< std::shared_ptr< Peer > > PeerSequence
Definition Overlay.h:68
std::uint32_t ID
Uniquely identifies a peer.
A public key.
Definition PublicKey.h:53
UInt256 getTransactionID() const
Definition STTx.h:262
std::vector< std::string > const & values() const
Returns all the values in the section.
Definition BasicConfig.h:68
virtual std::pair< std::shared_ptr< Slot >, Result > newOutboundSlot(beast::ip::Endpoint const &remoteEndpoint)=0
Create a new outbound slot with the specified remote endpoint.
Tracks load and resource consumption.
virtual Consumer newOutboundEndpoint(beast::ip::Endpoint const &address)=0
Create a new endpoint keyed by outbound IP address and port.
T contains(T... args)
T duration_cast(T... args)
T emplace_back(T... args)
T emplace(T... args)
T empty(T... args)
T end(T... args)
T find_if(T... args)
T format(T... args)
T get(T... args)
T insert(T... args)
T lock(T... args)
T make_shared(T... args)
T make_tuple(T... args)
T make_unique(T... args)
T max(T... args)
T min(T... args)
bool isPublic(Address const &addr)
Returns true if the address is a public routable address.
Definition IPAddress.h:71
bool isKeepAlive(boost::beast::http::message< IsRequest, Body, Fields > const &m)
Definition rfc2616.h:367
Result splitCommas(FwdIt first, FwdIt last)
Definition rfc2616.h:183
constexpr Out lexicalCastThrow(In in)
Convert from one type to another, throw on error.
JSON (JavaScript Object Notation).
Definition json_errors.h:5
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
@ Null
'null' value
Definition json_value.h:26
STL namespace.
std::string const & getFullVersionString()
Full server version string.
Definition BuildInfo.cpp:77
static constexpr auto kDisabled
static constexpr auto kOverlay
static constexpr auto kUnl
static constexpr auto kServerCounts
static constexpr auto kServerInfo
Config makeConfig(xrpl::Config const &cfg, std::uint16_t port, bool validationPublicKey, int ipLimit, bool verifyEndpoints)
Charge const kFeeMalformedRequest
Schedule of fees charged for imposing load on the server.
static constexpr auto kCheckIdlePeers
How often we check for idle peers (seconds).
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
beast::XorShiftEngine & defaultPrng()
Return the default random engine.
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:101
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::optional< ProtocolVersion > negotiateProtocolVersion(std::vector< ProtocolVersion > const &versions)
Given a list of supported protocol versions, choose the one we prefer.
static std::vector< beast::ip::Endpoint > withDefaultPeerPort(std::vector< beast::ip::Endpoint > const &addresses)
static constexpr auto kDefaultBootstrapIps
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
void addValidatorManifest(soci::session &session, std::string const &serialized)
addValidatorManifest Saves the manifest of a validator to the database.
Definition Wallet.cpp:137
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
@ Uncapped
Bypasses the cap (listed/trusted or config manifests).
Definition Manifest.h:365
@ Capped
Subject to the untrusted cap (unlisted peer gossip).
Definition Manifest.h:364
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
BaseUInt< 256 > UInt256
Definition base_uint.h:581
boost::beast::http::request< boost::beast::http::dynamic_body > HttpRequestType
Definition Handoff.h:12
std::shared_ptr< boost::asio::ssl::context > makeSslContext(std::string const &cipherList)
Create a self-signed SSL context that allows anonymous Diffie Hellman.
std::string base64Encode(std::uint8_t const *data, std::size_t len)
std::optional< UInt256 > makeSharedValue(StreamType &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
std::unique_ptr< Overlay > makeOverlay(Application &app, Overlay::Setup const &setup, ServerHandler &serverHandler, resource::Manager &resourceManager, Resolver &resolver, boost::asio::io_context &ioContext, BasicConfig const &config, beast::insight::Collector::Ptr const &collector)
Creates the implementation of Overlay.
constexpr Number squelch(Number const &x, Number const &limit) noexcept
Definition Number.h:911
std::shared_ptr< Message > makeSquelchMessage(PublicKey const &validator, bool squelch, uint32_t squelchDuration)
constexpr std::size_t untrustedManifestCount(std::optional< std::size_t > const &configured)
Number of untrusted manifests to store in cache and allowed in one Manifest message.
Definition Manifest.h:246
Overlay::Setup setupOverlay(BasicConfig const &config, beast::Journal j)
json::Value getCountsJson(Application &app, int minObjectCount)
Definition GetCounts.cpp:46
std::vector< ProtocolVersion > parseProtocolVersions(std::string_view value)
Parse a set of protocol versions.
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
Definition STTx.cpp:889
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
PublicKey verifyHandshake(boost::beast::http::fields const &headers, xrpl::UInt256 const &sharedValue, std::optional< std::uint32_t > networkID, beast::ip::Address publicIp, beast::ip::Address remote, Application &app)
Validate header fields necessary for upgrading the link to the peer protocol.
@ Accepted
Manifest is valid.
Definition Manifest.h:321
T piecewise_construct
T push_back(T... args)
T shuffle(T... args)
T reserve(T... args)
T setfill(T... args)
T setw(T... args)
T size(T... args)
T str(T... args)
static boost::asio::ip::tcp::endpoint toAsioEndpoint(ip::Endpoint const &address)
static ip::Endpoint fromAsio(boost::asio::ip::address const &address)
Used to indicate the result of a server connection handoff.
Definition Handoff.h:20
bool keepAlive
Definition Handoff.h:26
std::shared_ptr< Writer > response
Definition Handoff.h:29
static constexpr auto kUnl
Definition Constants.h:179
static constexpr auto kCounts
Definition Constants.h:104
static constexpr auto kOverlay
Definition Constants.h:143
static constexpr auto kServer
Definition Constants.h:160
void onTimer(ErrorCode ec)
boost::asio::basic_waitable_timer< ClockType > timer
Definition OverlayImpl.h:94
Timer(OverlayImpl &overlay)
std::uint32_t crawlOptions
Definition Overlay.h:62
std::optional< std::uint32_t > networkID
Definition Overlay.h:63
beast::ip::Address publicIp
Definition Overlay.h:60
std::shared_ptr< boost::asio::ssl::context > context
Definition Overlay.h:59
static constexpr auto kOverlay
Definition Constants.h:35
static constexpr auto kVl
Definition Constants.h:77
static constexpr auto kCrawl
Definition Constants.h:12
static constexpr auto kNetworkId
Definition Constants.h:30
PeerFinder configuration settings.
T substr(T... args)
T to_string(T... args)
T what(T... args)