xrpld
Loading...
Searching...
No Matches
Bootcache.h
1#pragma once
2
3#include <xrpld/peerfinder/PeerfinderManager.h>
4#include <xrpld/peerfinder/detail/Store.h>
5
6#include <xrpl/basics/comparators.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/beast/utility/PropertyStream.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
15namespace xrpl::PeerFinder {
16
33{
34private:
35 class Entry
36 {
37 public:
39 {
40 }
41
42 int&
44 {
45 return valence_;
46 }
47
48 [[nodiscard]] int
49 valence() const
50 {
51 return valence_;
52 }
53
54 friend bool
55 operator<(Entry const& lhs, Entry const& rhs)
56 {
57 return lhs.valence() > rhs.valence();
58 }
59
60 private:
62 };
63
64 using left_t = boost::bimaps::unordered_set_of<
66 boost::hash<beast::IP::Endpoint>,
68 using right_t = boost::bimaps::multiset_of<Entry, xrpl::less<Entry>>;
69 using map_type = boost::bimap<left_t, right_t>;
70 using value_type = map_type::value_type;
71
72 struct Transform
73 {
74 using first_argument_type = map_type::right_map::const_iterator::value_type const&;
76
77 explicit Transform() = default;
78
80 operator()(map_type::right_map::const_iterator::value_type const& v) const
81 {
82 return v.get_left();
83 }
84 };
85
86private:
88
92
93 // Time after which we can update the database again
95
96 // Set to true when a database update is needed
97 bool needsUpdate_{false};
98
99public:
100 static constexpr int kStaticValence = 32;
101
102 using iterator = boost::transform_iterator<Transform, map_type::right_map::const_iterator>;
103
105
106 Bootcache(Store& store, clock_type& clock, beast::Journal journal);
107
108 ~Bootcache();
109
111 [[nodiscard]] bool
112 empty() const;
113
115 [[nodiscard]] map_type::size_type
116 size() const;
117
120 [[nodiscard]] const_iterator
121 begin() const;
122 [[nodiscard]] const_iterator
123 cbegin() const;
124 [[nodiscard]] const_iterator
125 end() const;
126 [[nodiscard]] const_iterator
127 cend() const;
128 void
129 clear();
131
133 void
134 load();
135
137 bool
138 insert(beast::IP::Endpoint const& endpoint);
139
141 bool
142 insertStatic(beast::IP::Endpoint const& endpoint);
143
145 void
146 onSuccess(beast::IP::Endpoint const& endpoint);
147
149 void
150 onFailure(beast::IP::Endpoint const& endpoint);
151
153 void
155
157 void
159
160private:
161 void
162 prune();
163 void
164 update();
165 void
166 checkUpdate();
167 void
169};
170
171} // namespace xrpl::PeerFinder
std::chrono::steady_clock::time_point time_point
A version-independent IP address and port combination.
Definition IPEndpoint.h:17
A generic endpoint for log messages.
Definition Journal.h:38
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition Bootcache.h:55
bool insert(beast::IP::Endpoint const &endpoint)
Add a newly-learned address to the cache.
Definition Bootcache.cpp:98
boost::transform_iterator< Transform, map_type::right_map::const_iterator > iterator
Definition Bootcache.h:102
static constexpr int kStaticValence
Definition Bootcache.h:100
void periodicActivity()
Stores the cache in the persistent database on a timer.
boost::bimaps::unordered_set_of< beast::IP::Endpoint, boost::hash< beast::IP::Endpoint >, xrpl::equal_to< beast::IP::Endpoint > > left_t
Definition Bootcache.h:64
boost::bimap< left_t, right_t > map_type
Definition Bootcache.h:69
const_iterator cbegin() const
Definition Bootcache.cpp:51
const_iterator cend() const
Definition Bootcache.cpp:63
void onFailure(beast::IP::Endpoint const &endpoint)
Called when an outbound connection attempt fails to handshake.
boost::bimaps::multiset_of< Entry, xrpl::less< Entry > > right_t
Definition Bootcache.h:68
beast::Journal journal_
Definition Bootcache.h:91
void onWrite(beast::PropertyStream::Map &map)
Write the cache state to the property stream.
void onSuccess(beast::IP::Endpoint const &endpoint)
Called when an outbound connection handshake completes.
map_type::value_type value_type
Definition Bootcache.h:70
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
Definition Bootcache.cpp:45
void load()
Load the persisted data from the Store into the container.
Definition Bootcache.cpp:78
bool insertStatic(beast::IP::Endpoint const &endpoint)
Add a staticallyconfigured address to the cache.
clock_type::time_point whenUpdate_
Definition Bootcache.h:94
const_iterator end() const
Definition Bootcache.cpp:57
bool empty() const
Returns true if the cache is empty.
Definition Bootcache.cpp:33
map_type::size_type size() const
Returns the number of entries in the cache.
Definition Bootcache.cpp:39
Bootcache(Store &store, clock_type &clock, beast::Journal journal)
Definition Bootcache.cpp:21
Abstract persistence for PeerFinder data.
Definition Store.h:7
beast::AbstractClock< std::chrono::steady_clock > clock_type
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition Bootcache.h:74
beast::IP::Endpoint const & operator()(map_type::right_map::const_iterator::value_type const &v) const
Definition Bootcache.h:80
beast::IP::Endpoint const & result_type
Definition Bootcache.h:75