rippled
Loading...
Searching...
No Matches
ValidatorSite.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2016 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_MISC_VALIDATORSITE_H_INCLUDED
21#define RIPPLE_APP_MISC_VALIDATORSITE_H_INCLUDED
22
23#include <xrpld/app/main/Application.h>
24#include <xrpld/app/misc/ValidatorList.h>
25#include <xrpld/app/misc/detail/Work.h>
26
27#include <xrpl/basics/Log.h>
28#include <xrpl/basics/StringUtilities.h>
29#include <xrpl/json/json_value.h>
30
31#include <boost/asio.hpp>
32
33#include <mutex>
34#include <optional>
35
36namespace ripple {
37
69{
70 friend class Work;
71
72private:
73 using error_code = boost::system::error_code;
75 using endpoint_type = boost::asio::ip::tcp::endpoint;
76
77 struct Site
78 {
79 struct Status
80 {
81 clock_type::time_point refreshed;
84 };
85
86 struct Resource
87 {
88 explicit Resource(std::string uri_);
91 };
92
93 explicit Site(std::string uri);
94
97
102
107
108 unsigned short redirCount;
110 clock_type::time_point nextRefresh;
114 };
115
118
119 // If both mutex are to be locked at the same time, `sites_mutex_` must be
120 // locked before `state_mutex_` or we may deadlock.
123
126 boost::asio::basic_waitable_timer<clock_type> timer_;
127
128 // A list is currently being fetched from a site
130
131 // One or more lists are due to be fetched
134
135 // The configured list of URIs for fetching lists
137
138 // time to allow for requests to complete
140
141public:
143 Application& app,
144 std::optional<beast::Journal> j = std::nullopt,
147
158 bool
159 load(std::vector<std::string> const& siteURIs);
160
169 void
170 start();
171
178 void
179 join();
180
189 void
190 stop();
191
195 getJson() const;
196
197private:
199 bool
200 load(
201 std::vector<std::string> const& siteURIs,
203
206 void
207 setTimer(
210
212 void
213 onRequestTimeout(std::size_t siteIdx, error_code const& ec);
214
216 void
217 onTimer(std::size_t siteIdx, error_code const& ec);
218
220 void
222 boost::system::error_code const& ec,
223 endpoint_type const& endpoint,
225 std::size_t siteIdx);
226
228 void
230 boost::system::error_code const& ec,
231 std::string const& res,
232 std::size_t siteIdx);
233
236 void
239 std::size_t siteIdx,
241
244 void
246 std::string const& res,
247 std::size_t siteIdx,
249
255 std::size_t siteIdx,
257
260 bool
262};
263
264} // namespace ripple
265
266#endif
Represents a JSON value.
Definition: json_value.h:150
A generic endpoint for log messages.
Definition: Journal.h:60
void start()
Start fetching lists from sites.
std::condition_variable cv_
beast::Journal const j_
std::vector< Site > sites_
boost::asio::ip::tcp::endpoint endpoint_type
Definition: ValidatorSite.h:75
void stop()
Stop fetching lists from sites.
Json::Value getJson() const
Return JSON representation of configured validator sites.
bool load(std::vector< std::string > const &siteURIs)
Load configured site URIs.
void onTextFetch(boost::system::error_code const &ec, std::string const &res, std::size_t siteIdx)
Store latest list fetched from anywhere.
std::weak_ptr< detail::Work > work_
std::chrono::seconds const requestTimeout_
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.
std::atomic< bool > stopping_
void join()
Wait for current fetches from sites to complete.
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::shared_ptr< Site::Resource > processRedirect(detail::response_type &res, std::size_t siteIdx, std::lock_guard< std::mutex > const &)
Interpret a redirect response.
void parseJsonResponse(std::string const &res, std::size_t siteIdx, std::lock_guard< std::mutex > const &)
Parse json response from validator list site.
void makeRequest(std::shared_ptr< Site::Resource > resource, std::size_t siteIdx, std::lock_guard< std::mutex > const &)
Initiate request to given resource.
void onRequestTimeout(std::size_t siteIdx, error_code const &ec)
request took too long
std::atomic< bool > pending_
boost::system::error_code error_code
Definition: ValidatorSite.h:73
boost::asio::basic_waitable_timer< clock_type > timer_
void onTimer(std::size_t siteIdx, error_code const &ec)
Fetch site whose time has come.
void onSiteFetch(boost::system::error_code const &ec, endpoint_type const &endpoint, detail::response_type &&res, std::size_t siteIdx)
Store latest list fetched from site.
std::atomic< bool > fetching_
boost::beast::http::response< boost::beast::http::string_body > response_type
Definition: Work.h:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
clock_type::time_point refreshed
Definition: ValidatorSite.h:81
std::shared_ptr< Resource > loadedResource
the original uri as loaded from config
Definition: ValidatorSite.h:96
std::shared_ptr< Resource > startingResource
the resource to request at <timer> intervals.
endpoint_type lastRequestEndpoint
std::chrono::minutes refreshInterval
std::shared_ptr< Resource > activeResource
the active resource being requested.
std::optional< Status > lastRefreshStatus
clock_type::time_point nextRefresh