rippled
Bootcache.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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_PEERFINDER_BOOTCACHE_H_INCLUDED
21 #define RIPPLE_PEERFINDER_BOOTCACHE_H_INCLUDED
22 
23 #include <ripple/basics/comparators.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/beast/utility/PropertyStream.h>
26 #include <ripple/peerfinder/PeerfinderManager.h>
27 #include <ripple/peerfinder/impl/Store.h>
28 #include <boost/bimap.hpp>
29 #include <boost/bimap/multiset_of.hpp>
30 #include <boost/bimap/unordered_set_of.hpp>
31 #include <boost/iterator/transform_iterator.hpp>
32 
33 namespace ripple {
34 namespace PeerFinder {
35 
51 class Bootcache
52 {
53 private:
54  class Entry
55  {
56  public:
58  {
59  }
60 
61  int&
63  {
64  return m_valence;
65  }
66 
67  int
68  valence() const
69  {
70  return m_valence;
71  }
72 
73  friend bool
74  operator<(Entry const& lhs, Entry const& rhs)
75  {
76  if (lhs.valence() > rhs.valence())
77  return true;
78  return false;
79  }
80 
81  private:
82  int m_valence;
83  };
84 
85  using left_t = boost::bimaps::unordered_set_of<
87  boost::hash<beast::IP::Endpoint>,
89  using right_t = boost::bimaps::multiset_of<Entry, ripple::less<Entry>>;
90  using map_type = boost::bimap<left_t, right_t>;
91  using value_type = map_type::value_type;
92 
93  struct Transform
94 #ifdef _LIBCPP_VERSION
96  map_type::right_map::const_iterator::value_type const&,
97  beast::IP::Endpoint const&>
98 #endif
99  {
100 #ifndef _LIBCPP_VERSION
101  using first_argument_type =
102  map_type::right_map::const_iterator::value_type const&;
104 #endif
105 
106  explicit Transform() = default;
107 
108  beast::IP::Endpoint const&
110  map_type::right_map::const_iterator::value_type const& v) const
111  {
112  return v.get_left();
113  }
114  };
115 
116 private:
118 
122 
123  // Time after which we can update the database again
125 
126  // Set to true when a database update is needed
128 
129 public:
130  static constexpr int staticValence = 32;
131 
132  using iterator = boost::
133  transform_iterator<Transform, map_type::right_map::const_iterator>;
134 
136 
137  Bootcache(Store& store, clock_type& clock, beast::Journal journal);
138 
139  ~Bootcache();
140 
142  bool
143  empty() const;
144 
146  map_type::size_type
147  size() const;
148 
152  begin() const;
154  cbegin() const;
156  end() const;
158  cend() const;
159  void
160  clear();
164  void
165  load();
166 
168  bool
169  insert(beast::IP::Endpoint const& endpoint);
170 
172  bool
173  insertStatic(beast::IP::Endpoint const& endpoint);
174 
176  void
177  on_success(beast::IP::Endpoint const& endpoint);
178 
180  void
181  on_failure(beast::IP::Endpoint const& endpoint);
182 
184  void
186 
188  void
190 
191 private:
192  void
193  prune();
194  void
195  update();
196  void
197  checkUpdate();
198  void
199  flagForUpdate();
200 };
201 
202 } // namespace PeerFinder
203 } // namespace ripple
204 
205 #endif
ripple::PeerFinder::Bootcache::cend
const_iterator cend() const
Definition: Bootcache.cpp:73
ripple::PeerFinder::Bootcache::insertStatic
bool insertStatic(beast::IP::Endpoint const &endpoint)
Add a staticallyconfigured address to the cache.
Definition: Bootcache.cpp:125
ripple::PeerFinder::Bootcache::Entry::valence
int valence() const
Definition: Bootcache.h:68
beast::PropertyStream::Map
Definition: PropertyStream.h:224
ripple::PeerFinder::Bootcache::checkUpdate
void checkUpdate()
Definition: Bootcache.cpp:272
ripple::PeerFinder::Bootcache::Transform::first_argument_type
map_type::right_map::const_iterator::value_type const & first_argument_type
Definition: Bootcache.h:102
ripple::PeerFinder::Bootcache::m_store
Store & m_store
Definition: Bootcache.h:119
ripple::PeerFinder::Bootcache::map_type
boost::bimap< left_t, right_t > map_type
Definition: Bootcache.h:90
ripple::PeerFinder::Bootcache::on_failure
void on_failure(beast::IP::Endpoint const &endpoint)
Called when an outbound connection attempt fails to handshake.
Definition: Bootcache.cpp:173
ripple::PeerFinder::Bootcache::clear
void clear()
Definition: Bootcache.cpp:79
ripple::PeerFinder::Bootcache::m_journal
beast::Journal m_journal
Definition: Bootcache.h:121
ripple::PeerFinder::Bootcache::right_t
boost::bimaps::multiset_of< Entry, ripple::less< Entry > > right_t
Definition: Bootcache.h:89
ripple::PeerFinder::Bootcache::periodicActivity
void periodicActivity()
Stores the cache in the persistent database on a timer.
Definition: Bootcache.cpp:199
ripple::PeerFinder::Store
Abstract persistence for PeerFinder data.
Definition: Store.h:27
ripple::PeerFinder::Bootcache::flagForUpdate
void flagForUpdate()
Definition: Bootcache.cpp:280
ripple::PeerFinder::Bootcache::prune
void prune()
Definition: Bootcache.cpp:220
ripple::PeerFinder::Bootcache::load
void load()
Load the persisted data from the Store into the container.
Definition: Bootcache.cpp:88
ripple::PeerFinder::Bootcache::iterator
boost::transform_iterator< Transform, map_type::right_map::const_iterator > iterator
Definition: Bootcache.h:133
ripple::PeerFinder::Bootcache::size
map_type::size_type size() const
Returns the number of entries in the cache.
Definition: Bootcache.cpp:49
ripple::PeerFinder::Bootcache::const_iterator
iterator const_iterator
Definition: Bootcache.h:135
ripple::PeerFinder::Bootcache::~Bootcache
~Bootcache()
Definition: Bootcache.cpp:37
ripple::PeerFinder::Bootcache
Stores IP addresses useful for gaining initial connections.
Definition: Bootcache.h:51
ripple::PeerFinder::Bootcache::insert
bool insert(beast::IP::Endpoint const &endpoint)
Add a newly-learned address to the cache.
Definition: Bootcache.cpp:111
ripple::equal_to
Definition: comparators.h:53
ripple::PeerFinder::Bootcache::end
const_iterator end() const
Definition: Bootcache.cpp:67
ripple::PeerFinder::Bootcache::Bootcache
Bootcache(Store &store, clock_type &clock, beast::Journal journal)
Definition: Bootcache.cpp:28
ripple::PeerFinder::Bootcache::m_clock
clock_type & m_clock
Definition: Bootcache.h:120
ripple::PeerFinder::Bootcache::m_map
map_type m_map
Definition: Bootcache.h:117
ripple::PeerFinder::Bootcache::staticValence
static constexpr int staticValence
Definition: Bootcache.h:130
ripple::PeerFinder::Bootcache::Entry::operator<
friend bool operator<(Entry const &lhs, Entry const &rhs)
Definition: Bootcache.h:74
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::PeerFinder::Bootcache::Transform::Transform
Transform()=default
ripple::PeerFinder::Bootcache::Transform::operator()
beast::IP::Endpoint const & operator()(map_type::right_map::const_iterator::value_type const &v) const
Definition: Bootcache.h:109
beast::abstract_clock< std::chrono::steady_clock >
ripple::PeerFinder::Bootcache::onWrite
void onWrite(beast::PropertyStream::Map &map)
Write the cache state to the property stream.
Definition: Bootcache.cpp:207
std::unary_function
ripple::PeerFinder::Bootcache::on_success
void on_success(beast::IP::Endpoint const &endpoint)
Called when an outbound connection handshake completes.
Definition: Bootcache.cpp:147
ripple::PeerFinder::Bootcache::update
void update()
Definition: Bootcache.cpp:251
ripple::PeerFinder::Bootcache::Transform
Definition: Bootcache.h:93
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::PeerFinder::Bootcache::value_type
map_type::value_type value_type
Definition: Bootcache.h:91
ripple::PeerFinder::Bootcache::m_needsUpdate
bool m_needsUpdate
Definition: Bootcache.h:127
ripple::PeerFinder::Bootcache::left_t
boost::bimaps::unordered_set_of< beast::IP::Endpoint, boost::hash< beast::IP::Endpoint >, ripple::equal_to< beast::IP::Endpoint > > left_t
Definition: Bootcache.h:88
ripple::PeerFinder::Bootcache::cbegin
const_iterator cbegin() const
Definition: Bootcache.cpp:61
ripple::PeerFinder::Bootcache::empty
bool empty() const
Returns true if the cache is empty.
Definition: Bootcache.cpp:43
ripple::PeerFinder::Bootcache::begin
const_iterator begin() const
IP::Endpoint iterators that traverse in decreasing valence.
Definition: Bootcache.cpp:55
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::PeerFinder::Bootcache::m_whenUpdate
clock_type::time_point m_whenUpdate
Definition: Bootcache.h:124
ripple::PeerFinder::Bootcache::Transform::result_type
beast::IP::Endpoint const & result_type
Definition: Bootcache.h:103
ripple::PeerFinder::Bootcache::Entry::m_valence
int m_valence
Definition: Bootcache.h:82
beast::abstract_clock::time_point
typename Clock::time_point time_point
Definition: abstract_clock.h:63
ripple::PeerFinder::Bootcache::Entry::valence
int & valence()
Definition: Bootcache.h:62
ripple::PeerFinder::Bootcache::Entry::Entry
Entry(int valence)
Definition: Bootcache.h:57
ripple::PeerFinder::Bootcache::Entry
Definition: Bootcache.h:54