1#include <xrpld/app/misc/ValidatorList.h>
2#include <xrpld/app/misc/ValidatorSite.h>
3#include <xrpld/app/misc/detail/WorkFile.h>
4#include <xrpld/app/misc/detail/WorkPlain.h>
5#include <xrpld/app/misc/detail/WorkSSL.h>
7#include <xrpl/json/json_reader.h>
8#include <xrpl/protocol/digest.h>
9#include <xrpl/protocol/jss.h>
74 ,
j_{j ? *j :
app_.getJournal(
"ValidatorSite")}
86 if (
timer_.expiry() > clock_type::time_point{})
104 return sites.
empty() ||
load(sites, lock_sites);
110 JLOG(
j_.
debug()) <<
"Loading configured validator list sites";
114 return load(siteURIs, lock);
123 if (siteURIs.
empty())
128 for (
auto const& uri : siteURIs)
136 JLOG(
j_.
error()) <<
"Invalid validator site uri: " << uri <<
": " << e.
what();
141 JLOG(
j_.
debug()) <<
"Loaded " << siteURIs.
size() <<
" sites";
151 if (
timer_.expiry() == clock_type::time_point{})
170 if (
auto sp =
work_.lock())
180 catch (boost::system::system_error
const&)
194 return a.nextRefresh < b.nextRefresh;
201 timer_.expires_at(next->nextRefresh);
204 [
this, idx](boost::system::error_code
const& ec) { this->
onTimer(idx, ec); });
215 sites_[siteIdx].activeResource = resource;
217 auto timeoutCancel = [
this]() {
225 catch (boost::system::system_error
const&)
229 auto onFetch = [
this, siteIdx, timeoutCancel](
237 auto onFetchFile = [
this, siteIdx, timeoutCancel](
243 JLOG(
j_.
debug()) <<
"Starting request for " << resource->uri;
245 if (resource->pUrl.scheme ==
"https")
249 resource->pUrl.domain,
255 sites_[siteIdx].lastRequestEndpoint,
256 sites_[siteIdx].lastRequestSuccessful,
259 else if (resource->pUrl.scheme ==
"http")
262 resource->pUrl.domain,
266 sites_[siteIdx].lastRequestEndpoint,
267 sites_[siteIdx].lastRequestSuccessful,
272 BOOST_ASSERT(resource->pUrl.scheme ==
"file");
277 sites_[siteIdx].lastRequestSuccessful =
false;
284 timer_.async_wait([
this, siteIdx](boost::system::error_code
const& ec) {
303 auto const& site =
sites_[siteIdx];
304 if (site.activeResource)
306 JLOG(
j_.
warn()) <<
"Request for " << site.activeResource->uri <<
" took too long";
309 JLOG(
j_.
error()) <<
"Request took too long, but a response has "
310 "already been processed";
314 if (
auto sp =
work_.lock())
325 if (ec != boost::asio::error::operation_aborted)
334 sites_[siteIdx].redirCount = 0;
340 JLOG(
j_.
error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
342 boost::system::error_code{-1, boost::system::generic_category()},
360 JLOG(
j_.
warn()) <<
"Unable to parse JSON response from "
361 <<
sites_[siteIdx].activeResource->uri;
367 auto const [
valid, version, blobs] = [&body]() {
371 body[jss::version].
isInt();
377 version = body[jss::version].
asUInt();
386 JLOG(
j_.
warn()) <<
"Missing fields in JSON response from "
387 <<
sites_[siteIdx].activeResource->uri;
393 version == body[jss::version].asUInt(),
394 "xrpl::ValidatorSite::parseJsonResponse : version match");
395 auto const& uri =
sites_[siteIdx].activeResource->uri;
407 sites_[siteIdx].lastRefreshStatus.emplace(
410 for (
auto const& [disp, count] : applyResult.dispositions)
415 JLOG(
j_.
debug()) <<
"Applied " << count <<
" new validator list(s) from " << uri;
418 JLOG(
j_.
debug()) <<
"Applied " << count <<
" expired validator list(s) from "
422 JLOG(
j_.
debug()) <<
"Ignored " << count
423 <<
" validator list(s) with current sequence from " << uri;
426 JLOG(
j_.
debug()) <<
"Processed " << count <<
" future validator list(s) from "
430 JLOG(
j_.
debug()) <<
"Ignored " << count
431 <<
" validator list(s) with future known sequence from " << uri;
434 JLOG(
j_.
warn()) <<
"Ignored " << count <<
"stale validator list(s) from " << uri;
437 JLOG(
j_.
warn()) <<
"Ignored " << count <<
" untrusted validator list(s) from "
441 JLOG(
j_.
warn()) <<
"Ignored " << count <<
" invalid validator list(s) from " << uri;
444 JLOG(
j_.
warn()) <<
"Ignored " << count
445 <<
" unsupported version validator list(s) from " << uri;
452 if (body.
isMember(jss::refresh_interval) && body[jss::refresh_interval].
isNumeric())
454 using namespace std::chrono_literals;
459 sites_[siteIdx].refreshInterval = refresh;
470 using namespace boost::beast::http;
472 if (!res.contains(field::location) || res[field::location].empty())
474 JLOG(
j_.
warn()) <<
"Request for validator list at " <<
sites_[siteIdx].activeResource->uri
475 <<
" returned a redirect with no Location.";
481 JLOG(
j_.
warn()) <<
"Exceeded max redirects for validator list at "
482 <<
sites_[siteIdx].loadedResource->uri;
486 JLOG(
j_.
debug()) <<
"Got redirect for validator list from "
487 <<
sites_[siteIdx].activeResource->uri <<
" to new location "
488 << res[field::location];
493 ++
sites_[siteIdx].redirCount;
494 if (newLocation->pUrl.scheme !=
"http" && newLocation->pUrl.scheme !=
"https")
499 JLOG(
j_.
error()) <<
"Invalid redirect location: " << res[field::location];
507 boost::system::error_code
const& ec,
515 sites_[siteIdx].lastRequestEndpoint = endpoint;
516 JLOG(
j_.
debug()) <<
"Got completion for " <<
sites_[siteIdx].activeResource->uri <<
" "
518 auto onError = [&](
std::string const& errMsg,
bool retry) {
519 sites_[siteIdx].lastRefreshStatus.emplace(
530 JLOG(
j_.
warn()) <<
"Problem retrieving from " <<
sites_[siteIdx].activeResource->uri
531 <<
" " << endpoint <<
" " << ec.value() <<
":" << ec.message();
532 onError(
"fetch error",
true);
538 using namespace boost::beast::http;
539 switch (res.result())
542 sites_[siteIdx].lastRequestSuccessful =
true;
545 case status::moved_permanently:
546 case status::permanent_redirect:
548 case status::temporary_redirect: {
552 "xrpl::ValidatorSite::onSiteFetch : non-null "
555 if (res.result() == status::moved_permanently ||
556 res.result() == status::permanent_redirect)
558 sites_[siteIdx].startingResource = newLocation;
565 JLOG(
j_.
warn()) <<
"Request for validator list at "
566 <<
sites_[siteIdx].activeResource->uri <<
" " << endpoint
567 <<
" returned bad status: " << res.result_int();
568 onError(
"bad result code",
true);
574 JLOG(
j_.
error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
575 onError(ex.
what(),
false);
578 sites_[siteIdx].activeResource.reset();
590 boost::system::error_code
const& ec,
600 JLOG(
j_.
warn()) <<
"Problem retrieving from " <<
sites_[siteIdx].activeResource->uri
601 <<
" " << ec.value() <<
": " << ec.message();
605 sites_[siteIdx].lastRequestSuccessful =
true;
611 JLOG(
j_.
error()) <<
"Exception in " << __func__ <<
": " << ex.
what();
612 sites_[siteIdx].lastRefreshStatus.emplace(
615 sites_[siteIdx].activeResource.reset();
639 uri << site.loadedResource->uri;
640 if (site.loadedResource != site.startingResource)
641 uri <<
" (redirects to " << site.startingResource->uri +
")";
642 v[jss::uri] = uri.
str();
643 v[jss::next_refresh_time] =
to_string(site.nextRefresh);
644 if (site.lastRefreshStatus)
646 v[jss::last_refresh_time] =
to_string(site.lastRefreshStatus->refreshed);
647 v[jss::last_refresh_status] =
to_string(site.lastRefreshStatus->disposition);
648 if (!site.lastRefreshStatus->message.empty())
649 v[jss::last_refresh_message] = site.lastRefreshStatus->message;
651 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.
virtual Config & config()=0
virtual NetworkOPs & getOPs()=0
virtual ValidatorList & getValidators()=0
virtual HashRouter & getHashRouter()=0
virtual boost::asio::io_context & getIOContext()=0
virtual Overlay & getOverlay()=0
std::vector< std::string > loadLists()
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...
PublisherListStats applyListsAndBroadcast(std::string const &manifest, std::uint32_t version, std::vector< ValidatorBlobInfo > const &blobs, std::string siteUri, uint256 const &hash, Overlay &overlay, HashRouter &hashRouter, NetworkOPs &networkOPs)
Apply multiple published lists of public keys, then broadcast it to all peers that have not seen it o...
void onRequestTimeout(std::size_t siteIdx, error_code const &ec)
request took too long
void setTimer(std::lock_guard< std::mutex > const &, std::lock_guard< std::mutex > const &)
Queue next site to be fetched lock over site_mutex_ and state_mutex_ required.
bool load(std::vector< std::string > const &siteURIs)
Load configured site URIs.
void join()
Wait for current fetches from sites to complete.
std::shared_ptr< Site::Resource > processRedirect(detail::response_type const &res, std::size_t siteIdx, std::lock_guard< std::mutex > const &)
Interpret a redirect response.
bool missingSite(std::lock_guard< std::mutex > const &)
If no sites are provided, or a site fails to load, get a list of local cache files from the Validator...
std::atomic< bool > pending_
std::atomic< bool > fetching_
void start()
Start fetching lists from sites.
std::chrono::seconds const requestTimeout_
boost::asio::basic_waitable_timer< clock_type > timer_
boost::asio::ip::tcp::endpoint endpoint_type
void makeRequest(std::shared_ptr< Site::Resource > resource, std::size_t siteIdx, std::lock_guard< std::mutex > const &)
Initiate request to given resource.
Json::Value getJson() const
Return JSON representation of configured validator sites.
std::condition_variable cv_
void parseJsonResponse(std::string const &res, std::size_t siteIdx, std::lock_guard< std::mutex > const &)
Parse json response from validator list site.
std::atomic< bool > stopping_
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_
@ arrayValue
array value (ordered list)
@ objectValue
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.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::string to_string(base_uint< Bits, Tag > const &a)
@ unsupported_version
List version is not supported.
@ stale
Trusted publisher key, but seq is too old.
@ untrusted
List signed by untrusted publisher key.
@ same_sequence
Same sequence as current list.
@ pending
List will be valid in the future.
@ known_sequence
Future sequence already seen.
@ expired
List is expired, but has the largest non-pending sequence seen so far.
@ invalid
Invalid format or signature.
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
unsigned short constexpr max_redirects
auto constexpr error_retry_interval
auto constexpr default_refresh_interval
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
std::optional< std::uint16_t > port