xrpld
Loading...
Searching...
No Matches
Bootcache.h
1#pragma once
2
3#include <xrpl/beast/net/IPEndpoint.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/beast/utility/PropertyStream.h>
6#include <xrpl/peerfinder/Types.h>
7#include <xrpl/peerfinder/detail/Store.h>
8#include <xrpl/peerfinder/detail/Tuning.h>
9
10#include <boost/bimap.hpp>
11#include <boost/bimap/multiset_of.hpp>
12#include <boost/bimap/unordered_set_of.hpp>
13#include <boost/iterator/transform_iterator.hpp>
14
15#include <functional>
16
17namespace xrpl::peer_finder {
18
36{
37private:
38 class Entry
39 {
40 public:
42 {
43 }
44
45 int&
47 {
48 return valence_;
49 }
50
51 [[nodiscard]] int
52 valence() const
53 {
54 return valence_;
55 }
56
57 friend bool
58 operator<(Entry const& lhs, Entry const& rhs)
59 {
60 return lhs.valence() > rhs.valence();
61 }
62
63 private:
65 };
66
67 using left_t = boost::bimaps::
68 unordered_set_of<beast::ip::Endpoint, boost::hash<beast::ip::Endpoint>, std::equal_to<>>;
69 using right_t = boost::bimaps::multiset_of<Entry, std::less<>>;
70 using map_type = boost::bimap<left_t, right_t>;
71 using value_type = map_type::value_type;
72
73 struct Transform
74 {
75 using first_argument_type = map_type::right_map::const_iterator::value_type const&;
77
78 explicit Transform() = default;
79
81 operator()(map_type::right_map::const_iterator::value_type const& v) const
82 {
83 return v.get_left();
84 }
85 };
86
87private:
89
93
94 // Time after which we can update the database again
96
97 // Set to true when a database update is needed
98 bool needsUpdate_{false};
99
100public:
101 static constexpr int kStaticValence = 32;
102
103 using iterator = boost::transform_iterator<Transform, map_type::right_map::const_iterator>;
104
106
107 Bootcache(Store& store, clock_type& clock, beast::Journal journal);
108
109 ~Bootcache();
110
114 [[nodiscard]] bool
115 empty() const;
116
120 [[nodiscard]] map_type::size_type
121 size() const;
122
127 [[nodiscard]] const_iterator
128 begin() const;
129 [[nodiscard]] const_iterator
130 cbegin() const;
131 [[nodiscard]] const_iterator
132 end() const;
133 [[nodiscard]] const_iterator
134 cend() const;
135 void
136 clear();
138
142 void
143 load();
144
148 bool
149 insert(beast::ip::Endpoint const& endpoint);
150
154 bool
155 insertStatic(beast::ip::Endpoint const& endpoint);
156
160 void
161 onSuccess(beast::ip::Endpoint const& endpoint);
162
166 void
167 onFailure(beast::ip::Endpoint const& endpoint);
168
172 void
174
178 void
180
181private:
182 void
183 prune();
184 void
185 update();
186 void
187 checkUpdate();
188 void
190};
191
192} // namespace xrpl::peer_finder
std::chrono::steady_clock::time_point time_point
A generic endpoint for log messages.
Definition Journal.h:44
A version-independent IP address and port combination.
Definition IPEndpoint.h:24
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition Bootcache.h:58
static constexpr int kStaticValence
Definition Bootcache.h:101
bool insertStatic(beast::ip::Endpoint const &endpoint)
Add a staticallyconfigured address to the cache.
void onWrite(beast::PropertyStream::Map &map)
Write the cache state to the property stream.
map_type::value_type value_type
Definition Bootcache.h:71
void load()
Load the persisted data from the Store into the container.
Definition Bootcache.cpp:78
const_iterator begin() const
ip::Endpoint iterators that traverse in decreasing valence.
Definition Bootcache.cpp:45
bool insert(beast::ip::Endpoint const &endpoint)
Add a newly-learned address to the cache.
Definition Bootcache.cpp:99
bool empty() const
Returns true if the cache is empty.
Definition Bootcache.cpp:33
boost::transform_iterator< Transform, map_type::right_map::const_iterator > iterator
Definition Bootcache.h:103
boost::bimaps::multiset_of< Entry, std::less<> > right_t
Definition Bootcache.h:69
boost::bimap< left_t, right_t > map_type
Definition Bootcache.h:70
clock_type::time_point whenUpdate_
Definition Bootcache.h:95
const_iterator end() const
Definition Bootcache.cpp:57
map_type::size_type size() const
Returns the number of entries in the cache.
Definition Bootcache.cpp:39
void onFailure(beast::ip::Endpoint const &endpoint)
Called when an outbound connection attempt fails to handshake.
const_iterator cend() const
Definition Bootcache.cpp:63
void onSuccess(beast::ip::Endpoint const &endpoint)
Called when an outbound connection handshake completes.
void periodicActivity()
Stores the cache in the persistent database on a timer.
const_iterator cbegin() const
Definition Bootcache.cpp:51
Bootcache(Store &store, clock_type &clock, beast::Journal journal)
Definition Bootcache.cpp:21
boost::bimaps:: unordered_set_of< beast::ip::Endpoint, boost::hash< beast::ip::Endpoint >, std::equal_to<> > left_t
Definition Bootcache.h:67
Abstract persistence for PeerFinder data.
Definition Store.h:15
beast::AbstractClock< std::chrono::steady_clock > clock_type
beast::ip::Endpoint const & result_type
Definition Bootcache.h:76
beast::ip::Endpoint const & operator()(map_type::right_map::const_iterator::value_type const &v) const
Definition Bootcache.h:81
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition Bootcache.h:75