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#include <xrpl/basics/Log.h>
27#include <xrpl/basics/StringUtilities.h>
28#include <xrpl/json/json_value.h>
29
30#include <boost/asio.hpp>
31
32#include <mutex>
33#include <optional>
34
35namespace ripple {
36
68{
69 friend class Work;
70
71private:
72 using error_code = boost::system::error_code;
74 using endpoint_type = boost::asio::ip::tcp::endpoint;
75
76 struct Site
77 {
78 struct Status
79 {
80 clock_type::time_point refreshed;
83 };
84
85 struct Resource
86 {
87 explicit Resource(std::string uri_);
90 };
91
92 explicit Site(std::string uri);
93
96
101
106
107 unsigned short redirCount;
109 clock_type::time_point nextRefresh;
113 };
114
117
118 // If both mutex are to be locked at the same time, `sites_mutex_` must be
119 // locked before `state_mutex_` or we may deadlock.
122
125 boost::asio::basic_waitable_timer<clock_type> timer_;
126
127 // A list is currently being fetched from a site
129
130 // One or more lists are due to be fetched
133
134 // The configured list of URIs for fetching lists
136
137 // time to allow for requests to complete
139
140public:
142 Application& app,
143 std::optional<beast::Journal> j = std::nullopt,
146
157 bool
158 load(std::vector<std::string> const& siteURIs);
159
168 void
169 start();
170
177 void
178 join();
179
188 void
189 stop();
190
194 getJson() const;
195
196private:
198 bool
199 load(
200 std::vector<std::string> const& siteURIs,
202
205 void
206 setTimer(
209
211 void
212 onRequestTimeout(std::size_t siteIdx, error_code const& ec);
213
215 void
216 onTimer(std::size_t siteIdx, error_code const& ec);
217
219 void
221 boost::system::error_code const& ec,
222 endpoint_type const& endpoint,
224 std::size_t siteIdx);
225
227 void
229 boost::system::error_code const& ec,
230 std::string const& res,
231 std::size_t siteIdx);
232
235 void
238 std::size_t siteIdx,
240
243 void
245 std::string const& res,
246 std::size_t siteIdx,
248
254 std::size_t siteIdx,
256
259 bool
261};
262
263} // namespace ripple
264
265#endif
Represents a JSON value.
Definition: json_value.h:148
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:74
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_
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:72
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.
const std::chrono::seconds requestTimeout_
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:80
std::shared_ptr< Resource > loadedResource
the original uri as loaded from config
Definition: ValidatorSite.h:95
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