rippled
Overlay.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_OVERLAY_OVERLAY_H_INCLUDED
21 #define RIPPLE_OVERLAY_OVERLAY_H_INCLUDED
22 
23 #include <ripple/json/json_value.h>
24 #include <ripple/overlay/Peer.h>
25 #include <ripple/overlay/PeerSet.h>
26 #include <ripple/server/Handoff.h>
27 #include <boost/asio/ip/tcp.hpp>
28 #include <boost/asio/ssl/context.hpp>
29 #include <boost/asio/ssl/stream.hpp>
30 #include <boost/beast/http/message.hpp>
31 #include <boost/beast/core/tcp_stream.hpp>
32 #include <boost/beast/ssl/ssl_stream.hpp>
33 #include <ripple/core/Stoppable.h>
34 #include <ripple/beast/utility/PropertyStream.h>
35 #include <memory>
36 #include <type_traits>
37 #include <boost/asio/buffer.hpp>
38 #include <boost/asio/ip/tcp.hpp>
39 #include <boost/optional.hpp>
40 #include <functional>
41 
42 namespace boost { namespace asio { namespace ssl { class context; } } }
43 
44 namespace ripple {
45 
47 class Overlay
48  : public Stoppable
50 {
51 protected:
52  using socket_type = boost::beast::tcp_stream;
53  using stream_type = boost::beast::ssl_stream <socket_type>;
54 
55  // VFALCO NOTE The requirement of this constructor is an
56  // unfortunate problem with the API for
57  // Stoppable and PropertyStream
58  //
59  Overlay (Stoppable& parent)
60  : Stoppable ("Overlay", parent)
61  , beast::PropertyStream::Source ("peers")
62  {
63  }
64 
65 public:
66  enum class Promote
67  {
68  automatic,
69  never,
70  always
71  };
72 
73  struct Setup
74  {
75  explicit Setup() = default;
76 
78  bool expire = false;
80  int ipLimit = 0;
82  boost::optional<std::uint32_t> networkID;
83  bool vlEnabled = true;
84  };
85 
87 
88  virtual ~Overlay() = default;
89 
91  virtual
92  Handoff
94  http_request_type&& request,
95  boost::asio::ip::tcp::endpoint remote_address) = 0;
96 
101  virtual
102  void
103  connect (beast::IP::Endpoint const& address) = 0;
104 
106  virtual
107  int
108  limit () = 0;
109 
114  virtual
116  size () = 0;
117 
121  virtual
123  json () = 0;
124 
128  virtual
130  getActivePeers () = 0;
131 
135  virtual
136  void
137  checkSanity (std::uint32_t index) = 0;
138 
141  virtual
142  void
143  check () = 0;
144 
146  virtual
148  findPeerByShortID (Peer::id_t const& id) = 0;
149 
151  virtual
153  findPeerByPublicKey (PublicKey const& pubKey) = 0;
154 
156  virtual
157  void
158  send (protocol::TMProposeSet& m) = 0;
159 
161  virtual
162  void
163  send (protocol::TMValidation& m) = 0;
164 
166  virtual
167  void
168  relay (protocol::TMProposeSet& m,
169  uint256 const& uid) = 0;
170 
172  virtual
173  void
174  relay (protocol::TMValidation& m,
175  uint256 const& uid) = 0;
176 
191  template <typename UnaryFunc>
193  typename UnaryFunc::return_type>::value,
194  typename UnaryFunc::return_type>
195  foreach (UnaryFunc f)
196  {
197  for (auto const& p : getActivePeers())
198  f (p);
199  return f();
200  }
201 
211  template <class Function>
214  typename Function::return_type
215  >
216  foreach(Function f)
217  {
218  for (auto const& p : getActivePeers())
219  f (p);
220  }
221 
239  virtual
242  bool(std::shared_ptr<Peer> const&)> score) = 0;
243 
245  virtual void incJqTransOverflow() = 0;
246  virtual std::uint64_t getJqTransOverflow() const = 0;
247 
251  virtual void incPeerDisconnect() = 0;
252  virtual std::uint64_t getPeerDisconnect() const = 0;
253  virtual void incPeerDisconnectCharges() = 0;
254  virtual std::uint64_t getPeerDisconnectCharges() const = 0;
255 
261  virtual
263  crawlShards(bool pubKey, std::uint32_t hops) = 0;
264 
273  virtual
274  boost::optional<std::uint32_t>
275  networkID() const = 0;
276 };
277 
279 {
280  uint256 const& hash_;
282  bool operator()(std::shared_ptr<Peer> const&) const;
283 
285  : hash_ (hash), seq_ (seq)
286  {}
287 };
288 
290 {
291  uint256 const& hash_;
292  bool operator()(std::shared_ptr<Peer> const&) const;
293 
294  ScoreHasTxSet (uint256 const& hash) : hash_ (hash)
295  {}
296 };
297 
298 }
299 
300 #endif
ripple::Overlay::~Overlay
virtual ~Overlay()=default
ripple::Overlay::onHandoff
virtual Handoff onHandoff(std::unique_ptr< stream_type > &&bundle, http_request_type &&request, boost::asio::ip::tcp::endpoint remote_address)=0
Conditionally accept an incoming HTTP request.
std::shared_ptr< boost::asio::ssl::context >
ripple::Overlay::stream_type
boost::beast::ssl_stream< socket_type > stream_type
Definition: Overlay.h:53
functional
ripple::Overlay::limit
virtual int limit()=0
Returns the maximum number of peers we are configured to allow.
ripple::http_request_type
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition: Handoff.h:31
ripple::Overlay::getJqTransOverflow
virtual std::uint64_t getJqTransOverflow() const =0
beast::PropertyStream::Source
Subclasses can be called to write to a stream and have children.
Definition: PropertyStream.h:274
ripple::Overlay::Setup::crawlOptions
std::uint32_t crawlOptions
Definition: Overlay.h:81
std::is_void
std::vector
STL class.
ripple::Overlay::getActivePeers
virtual PeerSequence getActivePeers()=0
Returns a sequence representing the current list of peers.
ripple::Overlay::Promote
Promote
Definition: Overlay.h:66
ripple::ScoreHasTxSet
Definition: Overlay.h:289
boost
Definition: IPAddress.h:127
std::function
ripple::Overlay::socket_type
boost::beast::tcp_stream socket_type
Definition: Overlay.h:52
ripple::Overlay::incPeerDisconnectCharges
virtual void incPeerDisconnectCharges()=0
ripple::ScoreHasLedger::hash_
uint256 const & hash_
Definition: Overlay.h:280
ripple::Overlay::Promote::never
@ never
ripple::base_uint< 256 >
ripple::ScoreHasTxSet::hash_
uint256 const & hash_
Definition: Overlay.h:291
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:200
ripple::Overlay::Setup::public_ip
beast::IP::Address public_ip
Definition: Overlay.h:79
ripple::Overlay::Promote::always
@ always
beast::IP::Address
boost::asio::ip::address Address
Definition: IPAddress.h:41
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
std::enable_if_t
ripple::ScoreHasTxSet::ScoreHasTxSet
ScoreHasTxSet(uint256 const &hash)
Definition: Overlay.h:294
ripple::Overlay::incJqTransOverflow
virtual void incJqTransOverflow()=0
Increment and retrieve counter for transaction job queue overflows.
ripple::Overlay::relay
virtual void relay(protocol::TMProposeSet &m, uint256 const &uid)=0
Relay a proposal.
ripple::set
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Definition: BasicConfig.h:271
ripple::Overlay::checkSanity
virtual void checkSanity(std::uint32_t index)=0
Calls the checkSanity function on each peer.
ripple::ScoreHasLedger::seq_
std::uint32_t seq_
Definition: Overlay.h:281
ripple::Overlay::send
virtual void send(protocol::TMProposeSet &m)=0
Broadcast a proposal.
ripple::Overlay::selectPeers
virtual std::size_t selectPeers(PeerSet &set, std::size_t limit, std::function< bool(std::shared_ptr< Peer > const &)> score)=0
Select from active peers.
std::uint32_t
ripple::Overlay::findPeerByPublicKey
virtual std::shared_ptr< Peer > findPeerByPublicKey(PublicKey const &pubKey)=0
Returns the peer with the matching public key, or null.
ripple::Overlay::incPeerDisconnect
virtual void incPeerDisconnect()=0
Increment and retrieve counters for total peer disconnects, and disconnects we initiate for excessive...
ripple::Overlay::crawlShards
virtual Json::Value crawlShards(bool pubKey, std::uint32_t hops)=0
Returns information reported to the crawl shard RPC command.
memory
ripple::Overlay::size
virtual std::size_t size()=0
Returns the number of active peers.
ripple::Overlay::connect
virtual void connect(beast::IP::Endpoint const &address)=0
Establish a peer connection to the specified endpoint.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Overlay::Setup::ipLimit
int ipLimit
Definition: Overlay.h:80
ripple::Overlay::Overlay
Overlay(Stoppable &parent)
Definition: Overlay.h:59
ripple::Overlay::Setup::vlEnabled
bool vlEnabled
Definition: Overlay.h:83
beast::PropertyStream::Source::Source
Source(std::string const &name)
Definition: beast_PropertyStream.cpp:174
ripple::Overlay
Manages the set of connected peers.
Definition: Overlay.h:47
ripple::Overlay::json
virtual Json::Value json()=0
Return diagnostics on the status of all peers.
ripple::PeerSet
Supports data retrieval by managing a set of peers.
Definition: PeerSet.h:48
ripple::ScoreHasLedger
Definition: Overlay.h:278
ripple::Overlay::Setup::networkID
boost::optional< std::uint32_t > networkID
Definition: Overlay.h:82
ripple::ScoreHasLedger::operator()
bool operator()(std::shared_ptr< Peer > const &) const
Definition: OverlayImpl.cpp:1305
ripple::Handoff
Used to indicate the result of a server connection handoff.
Definition: Handoff.h:37
ripple::Overlay::Setup
Definition: Overlay.h:73
ripple::ScoreHasTxSet::operator()
bool operator()(std::shared_ptr< Peer > const &) const
Definition: OverlayImpl.cpp:1311
std::size_t
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
ripple::Overlay::Setup::context
std::shared_ptr< boost::asio::ssl::context > context
Definition: Overlay.h:77
ripple::Overlay::Setup::expire
bool expire
Definition: Overlay.h:78
ripple::Overlay::Promote::automatic
@ automatic
ripple::Overlay::getPeerDisconnect
virtual std::uint64_t getPeerDisconnect() const =0
std::unique_ptr< stream_type >
ripple::Overlay::networkID
virtual boost::optional< std::uint32_t > networkID() const =0
Returns the ID of the network this server is configured for, if any.
ripple::Overlay::Setup::Setup
Setup()=default
ripple::Overlay::getPeerDisconnectCharges
virtual std::uint64_t getPeerDisconnectCharges() const =0
type_traits
ripple::Overlay::findPeerByShortID
virtual std::shared_ptr< Peer > findPeerByShortID(Peer::id_t const &id)=0
Returns the peer with the matching short id, or null.
ripple::Overlay::check
virtual void check()=0
Calls the check function on each peer.
ripple::ScoreHasLedger::ScoreHasLedger
ScoreHasLedger(uint256 const &hash, std::uint32_t seq)
Definition: Overlay.h:284
Json::Value
Represents a JSON value.
Definition: json_value.h:141
beast
Definition: base_uint.h:582