1#include <xrpld/app/misc/ValidatorSite.h>
3#include <xrpld/app/main/Application.h>
4#include <xrpld/app/misc/ValidatorList.h>
5#include <xrpld/app/misc/detail/Work.h>
6#include <xrpld/app/misc/detail/WorkFile.h>
7#include <xrpld/app/misc/detail/WorkPlain.h>
8#include <xrpld/app/misc/detail/WorkSSL.h>
10#include <xrpl/basics/Log.h>
11#include <xrpl/basics/SlabAllocator.h>
12#include <xrpl/basics/StringUtilities.h>
13#include <xrpl/basics/chrono.h>
14#include <xrpl/beast/utility/Journal.h>
15#include <xrpl/beast/utility/instrumentation.h>
16#include <xrpl/json/json_reader.h>
17#include <xrpl/json/json_value.h>
18#include <xrpl/protocol/digest.h>
19#include <xrpl/protocol/jss.h>
21#include <boost/asio/error.hpp>
22#include <boost/beast/http/field.hpp>
23#include <boost/beast/http/impl/serializer.hpp>
24#include <boost/beast/http/status.hpp>
25#include <boost/system/detail/error_code.hpp>
26#include <boost/system/detail/generic_category.hpp>
27#include <boost/system/system_error.hpp>
56 if (
pUrl.scheme ==
"file")
58 if (!pUrl.domain.empty())
59 throw std::runtime_error(
"file URI cannot contain a hostname");
63 if (pUrl.path[0] ==
'/')
64 pUrl.path = pUrl.path.substr(1);
67 if (pUrl.path.empty())
68 throw std::runtime_error(
"file URI must contain a path");
70 else if (
pUrl.scheme ==
"http")
72 if (pUrl.domain.empty())
73 throw std::runtime_error(
"http URI must contain a hostname");
78 else if (
pUrl.scheme ==
"https")
80 if (pUrl.domain.empty())
81 throw std::runtime_error(
"https URI must contain a hostname");
88 throw std::runtime_error(
"Unsupported scheme: '" + pUrl.scheme +
"'");
106 ,
j_{j ? *j :
app_.getJournal(
"ValidatorSite")}
118 if (
timer_.expiry() > clock_type::time_point{})
135 auto const sites =
app_.getValidators().loadLists();
136 return sites.empty() ||
load(sites, lockSites);
142 JLOG(
j_.debug()) <<
"Loading configured validator list sites";
146 return load(siteURIs, lock);
155 if (siteURIs.
empty())
160 for (
auto const& uri : siteURIs)
168 JLOG(
j_.error()) <<
"Invalid validator site uri: " << uri <<
": " << e.
what();
173 JLOG(
j_.debug()) <<
"Loaded " << siteURIs.
size() <<
" sites";
183 if (
timer_.expiry() == clock_type::time_point{})
202 if (
auto sp =
work_.lock())
212 catch (boost::system::system_error
const&)
232 timer_.expires_at(next->nextRefresh);
235 [
this, idx](boost::system::error_code
const& ec) { this->
onTimer(idx, ec); });
246 sites_[siteIdx].activeResource = resource;
248 auto timeoutCancel = [
this]() {
256 catch (boost::system::system_error
const&)
260 auto onFetch = [
this, siteIdx, timeoutCancel](
268 auto onFetchFile = [
this, siteIdx, timeoutCancel](
274 JLOG(
j_.debug()) <<
"Starting request for " << resource->uri;
276 if (resource->pUrl.scheme ==
"https")
280 resource->pUrl.domain,
287 sites_[siteIdx].lastRequestEndpoint,
288 sites_[siteIdx].lastRequestSuccessful,
291 else if (resource->pUrl.scheme ==
"http")
294 resource->pUrl.domain,
299 sites_[siteIdx].lastRequestEndpoint,
300 sites_[siteIdx].lastRequestSuccessful,
305 BOOST_ASSERT(resource->pUrl.scheme ==
"file");
307 resource->pUrl.path,
app_.getIOContext(), onFetchFile);
310 sites_[siteIdx].lastRequestSuccessful =
false;
317 timer_.async_wait([
this, siteIdx](boost::system::error_code
const& ec) {
336 auto const& site =
sites_[siteIdx];
337 if (site.activeResource)
339 JLOG(
j_.warn()) <<
"Request for " << site.activeResource->uri <<
" took too long";
343 JLOG(
j_.error()) <<
"Request took too long, but a response has "
344 "already been processed";
349 if (
auto sp =
work_.lock())
360 if (ec != boost::asio::error::operation_aborted)
369 sites_[siteIdx].redirCount = 0;
375 JLOG(
j_.error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
377 boost::system::error_code{-1, boost::system::generic_category()},
395 JLOG(
j_.warn()) <<
"Unable to parse JSON response from "
396 <<
sites_[siteIdx].activeResource->uri;
402 auto const [
valid, version, blobs] = [&body]() {
406 body[jss::version].
isInt();
412 version = body[jss::version].
asUInt();
421 JLOG(
j_.warn()) <<
"Missing fields in JSON response from "
422 <<
sites_[siteIdx].activeResource->uri;
426 auto const manifest = body[jss::manifest].
asString();
428 version == body[jss::version].asUInt(),
429 "xrpl::ValidatorSite::parseJsonResponse : version match");
430 auto const& uri =
sites_[siteIdx].activeResource->uri;
431 auto const hash =
sha512Half(manifest, blobs, version);
432 auto const applyResult =
app_.getValidators().applyListsAndBroadcast(
439 app_.getHashRouter(),
442 sites_[siteIdx].lastRefreshStatus.emplace(
445 .disposition = applyResult.bestDisposition(),
448 for (
auto const& [disp, count] : applyResult.dispositions)
453 JLOG(
j_.debug()) <<
"Applied " << count <<
" new validator list(s) from " << uri;
456 JLOG(
j_.debug()) <<
"Applied " << count <<
" expired validator list(s) from "
460 JLOG(
j_.debug()) <<
"Ignored " << count
461 <<
" validator list(s) with current sequence from " << uri;
464 JLOG(
j_.debug()) <<
"Processed " << count <<
" future validator list(s) from "
468 JLOG(
j_.debug()) <<
"Ignored " << count
469 <<
" validator list(s) with future known sequence from " << uri;
472 JLOG(
j_.warn()) <<
"Ignored " << count <<
"stale validator list(s) from " << uri;
475 JLOG(
j_.warn()) <<
"Ignored " << count <<
" untrusted validator list(s) from "
479 JLOG(
j_.warn()) <<
"Ignored " << count <<
" invalid validator list(s) from " << uri;
482 JLOG(
j_.warn()) <<
"Ignored " << count
483 <<
" unsupported version validator list(s) from " << uri;
490 if (body.
isMember(jss::refresh_interval) && body[jss::refresh_interval].
isNumeric())
492 using namespace std::chrono_literals;
497 sites_[siteIdx].refreshInterval = refresh;
508 using namespace boost::beast::http;
510 if (!res.contains(field::location) || res[field::location].empty())
512 JLOG(
j_.warn()) <<
"Request for validator list at " <<
sites_[siteIdx].activeResource->uri
513 <<
" returned a redirect with no Location.";
519 JLOG(
j_.warn()) <<
"Exceeded max redirects for validator list at "
520 <<
sites_[siteIdx].loadedResource->uri;
524 JLOG(
j_.debug()) <<
"Got redirect for validator list from "
525 <<
sites_[siteIdx].activeResource->uri <<
" to new location "
526 << res[field::location];
531 ++
sites_[siteIdx].redirCount;
532 if (newLocation->pUrl.scheme !=
"http" && newLocation->pUrl.scheme !=
"https")
537 JLOG(
j_.error()) <<
"Invalid redirect location: " << res[field::location];
545 boost::system::error_code
const& ec,
553 sites_[siteIdx].lastRequestEndpoint = endpoint;
554 JLOG(
j_.debug()) <<
"Got completion for " <<
sites_[siteIdx].activeResource->uri <<
" "
556 auto onError = [&](
std::string const& errMsg,
bool retry) {
557 sites_[siteIdx].lastRefreshStatus.emplace(
571 JLOG(
j_.warn()) <<
"Problem retrieving from " <<
sites_[siteIdx].activeResource->uri
572 <<
" " << endpoint <<
" " << ec.value() <<
":" << ec.message();
573 onError(
"fetch error",
true);
579 using namespace boost::beast::http;
580 switch (res.result())
583 sites_[siteIdx].lastRequestSuccessful =
true;
586 case status::moved_permanently:
587 case status::permanent_redirect:
589 case status::temporary_redirect: {
593 "xrpl::ValidatorSite::onSiteFetch : non-null "
596 if (res.result() == status::moved_permanently ||
597 res.result() == status::permanent_redirect)
599 sites_[siteIdx].startingResource = newLocation;
606 JLOG(
j_.warn()) <<
"Request for validator list at "
607 <<
sites_[siteIdx].activeResource->uri <<
" " << endpoint
608 <<
" returned bad status: " << res.result_int();
609 onError(
"bad result code",
true);
615 JLOG(
j_.error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
616 onError(ex.
what(),
false);
619 sites_[siteIdx].activeResource.reset();
631 boost::system::error_code
const& ec,
641 JLOG(
j_.warn()) <<
"Problem retrieving from " <<
sites_[siteIdx].activeResource->uri
642 <<
" " << ec.value() <<
": " << ec.message();
646 sites_[siteIdx].lastRequestSuccessful =
true;
652 JLOG(
j_.error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
653 sites_[siteIdx].lastRefreshStatus.emplace(
657 .message = ex.
what()});
659 sites_[siteIdx].activeResource.reset();
683 uri << site.loadedResource->uri;
684 if (site.loadedResource != site.startingResource)
685 uri <<
" (redirects to " << site.startingResource->uri +
")";
686 v[jss::uri] = uri.
str();
687 v[jss::next_refresh_time] =
to_string(site.nextRefresh);
688 if (site.lastRefreshStatus)
690 v[jss::last_refresh_time] =
to_string(site.lastRefreshStatus->refreshed);
691 v[jss::last_refresh_status] =
to_string(site.lastRefreshStatus->disposition);
692 if (!site.lastRefreshStatus->message.empty())
693 v[jss::last_refresh_message] = site.lastRefreshStatus->message;
695 v[jss::refresh_interval_min] =
static_cast<Int
>(site.refreshInterval.count());
Unserialize a JSON document into a Value.
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
Value & append(Value const &value)
Append value to array at the end.
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
static std::vector< ValidatorBlobInfo > parseBlobs(std::uint32_t version, json::Value const &body)
Pull the blob/signature/manifest information out of the appropriate Json body fields depending on the...
bool missingSite(std::scoped_lock< std::mutex > const &)
If no sites are provided, or a site fails to load, get a list of local cache files from the Validator...
void onRequestTimeout(std::size_t siteIdx, error_code const &ec)
request took too long
std::chrono::system_clock clock_type
json::Value getJson() const
Return JSON representation of configured validator sites.
bool load(std::vector< std::string > const &siteURIs)
Load configured site URIs.
void join()
Wait for current fetches from sites to complete.
std::atomic< bool > pending_
std::shared_ptr< Site::Resource > processRedirect(detail::response_type const &res, std::size_t siteIdx, std::scoped_lock< std::mutex > const &)
Interpret a redirect response.
std::atomic< bool > fetching_
void setTimer(std::scoped_lock< std::mutex > const &, std::scoped_lock< std::mutex > const &)
Queue next site to be fetched lock over site_mutex_ and state_mutex_ required.
void start()
Start fetching lists from sites.
std::chrono::seconds const requestTimeout_
boost::asio::basic_waitable_timer< clock_type > timer_
void makeRequest(std::shared_ptr< Site::Resource > resource, std::size_t siteIdx, std::scoped_lock< std::mutex > const &)
Initiate request to given resource.
boost::asio::ip::tcp::endpoint endpoint_type
std::condition_variable cv_
std::atomic< bool > stopping_
void parseJsonResponse(std::string const &res, std::size_t siteIdx, std::scoped_lock< std::mutex > const &)
Parse json response from validator list site.
ValidatorSite(Application &app, std::optional< beast::Journal > j=std::nullopt, std::chrono::seconds timeout=std::chrono::seconds{20})
boost::system::error_code error_code
std::weak_ptr< detail::Work > work_
void onSiteFetch(boost::system::error_code const &ec, endpoint_type const &endpoint, detail::response_type const &res, std::size_t siteIdx)
Store latest list fetched from site.
void onTextFetch(boost::system::error_code const &ec, std::string const &res, std::size_t siteIdx)
Store latest list fetched from anywhere.
void onTimer(std::size_t siteIdx, error_code const &ec)
Fetch site whose time has come.
void stop()
Stop fetching lists from sites.
std::vector< Site > sites_
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
boost::beast::http::response< boost::beast::http::string_body > response_type
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr auto kErrorRetryInterval
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
@ UnsupportedVersion
List version is not supported.
@ Expired
List is expired, but has the largest non-pending sequence seen so far.
@ SameSequence
Same sequence as current list.
@ KnownSequence
Future sequence already seen.
@ Pending
List will be valid in the future.
@ Invalid
Invalid format or signature.
@ Untrusted
List signed by untrusted publisher key.
@ Stale
Trusted publisher key, but seq is too old.
std::string to_string(BaseUInt< Bits, Tag > const &a)
unsigned constexpr short kMaxRedirects
constexpr auto kDefaultRefreshInterval
bool parseUrl(ParsedUrl &pUrl, std::string const &strUrl)
Resource(std::string uri)
std::shared_ptr< Resource > startingResource
the resource to request at <timer> intervals.
clock_type::time_point nextRefresh
std::chrono::minutes refreshInterval
std::shared_ptr< Resource > loadedResource
the original uri as loaded from config