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  {
95  using first_argument_type =
96  map_type::right_map::const_iterator::value_type const&;
98 
99  explicit Transform() = default;
100 
101  beast::IP::Endpoint const&
103  map_type::right_map::const_iterator::value_type const& v) const
104  {
105  return v.get_left();
106  }
107  };
108 
109 private:
111 
115 
116  // Time after which we can update the database again
118 
119  // Set to true when a database update is needed
121 
122 public:
123  static constexpr int staticValence = 32;
124 
125  using iterator = boost::
126  transform_iterator<Transform, map_type::right_map::const_iterator>;
127 
129 
130  Bootcache(Store& store, clock_type& clock, beast::Journal journal);
131 
132  ~Bootcache();
133 
135  bool
136  empty() const;
137 
139  map_type::size_type
140  size() const;
141 
145  begin() const;
147  cbegin() const;
149  end() const;
151  cend() const;
152  void
153  clear();
157  void
158  load();
159 
161  bool
162  insert(beast::IP::Endpoint const& endpoint);
163 
165  bool
166  insertStatic(beast::IP::Endpoint const& endpoint);
167 
169  void
170  on_success(beast::IP::Endpoint const& endpoint);
171 
173  void
174  on_failure(beast::IP::Endpoint const& endpoint);
175 
177  void
179 
181  void
183 
184 private:
185  void
186  prune();
187  void
188  update();
189  void
190  checkUpdate();
191  void
192  flagForUpdate();
193 };
194 
195 } // namespace PeerFinder
196 } // namespace ripple
197 
198 #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:96
ripple::PeerFinder::Bootcache::m_store
Store & m_store
Definition: Bootcache.h:112
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:114
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:126
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:128
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:113
ripple::PeerFinder::Bootcache::m_map
map_type m_map
Definition: Bootcache.h:110
ripple::PeerFinder::Bootcache::staticValence
static constexpr int staticValence
Definition: Bootcache.h:123
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:102
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
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:120
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:117
ripple::PeerFinder::Bootcache::Transform::result_type
beast::IP::Endpoint const & result_type
Definition: Bootcache.h:97
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