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 <xrpld/core/Config.h>
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 <memory>
34#include <mutex>
35#include <optional>
36
37namespace ripple {
38
70{
71 friend class Work;
72
73private:
74 using error_code = boost::system::error_code;
76 using endpoint_type = boost::asio::ip::tcp::endpoint;
77
78 struct Site
79 {
80 struct Status
81 {
82 clock_type::time_point refreshed;
85 };
86
87 struct Resource
88 {
89 explicit Resource(std::string uri_);
92 };
93
94 explicit Site(std::string uri);
95
98
103
108
109 unsigned short redirCount;
111 clock_type::time_point nextRefresh;
115 };
116
119
120 // If both mutex are to be locked at the same time, `sites_mutex_` must be
121 // locked before `state_mutex_` or we may deadlock.
124
127 boost::asio::basic_waitable_timer<clock_type> timer_;
128
129 // A list is currently being fetched from a site
131
132 // One or more lists are due to be fetched
135
136 // The configured list of URIs for fetching lists
138
139 // time to allow for requests to complete
141
142public:
144 Application& app,
145 std::optional<beast::Journal> j = std::nullopt,
148
159 bool
160 load(std::vector<std::string> const& siteURIs);
161
170 void
171 start();
172
179 void
180 join();
181
190 void
191 stop();
192
196 getJson() const;
197
198private:
200 bool
201 load(
202 std::vector<std::string> const& siteURIs,
204
207 void
208 setTimer(
211
213 void
214 onRequestTimeout(std::size_t siteIdx, error_code const& ec);
215
217 void
218 onTimer(std::size_t siteIdx, error_code const& ec);
219
221 void
223 boost::system::error_code const& ec,
224 endpoint_type const& endpoint,
226 std::size_t siteIdx);
227
229 void
231 boost::system::error_code const& ec,
232 std::string const& res,
233 std::size_t siteIdx);
234
237 void
240 std::size_t siteIdx,
242
245 void
247 std::string const& res,
248 std::size_t siteIdx,
250
256 std::size_t siteIdx,
258
261 bool
263};
264
265} // namespace ripple
266
267#endif
Represents a JSON value.
Definition: json_value.h:147
A generic endpoint for log messages.
Definition: Journal.h:59
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:76
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:74
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:35
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:82
std::shared_ptr< Resource > loadedResource
the original uri as loaded from config
Definition: ValidatorSite.h:97
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