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/beast/utility/PropertyStream.h>
24 #include <ripple/core/Stoppable.h>
25 #include <ripple/json/json_value.h>
26 #include <ripple/overlay/Peer.h>
27 #include <ripple/overlay/PeerSet.h>
28 #include <ripple/server/Handoff.h>
29 #include <boost/asio/buffer.hpp>
30 #include <boost/asio/ip/tcp.hpp>
31 #include <boost/asio/ssl/context.hpp>
32 #include <boost/asio/ssl/stream.hpp>
33 #include <boost/beast/core/tcp_stream.hpp>
34 #include <boost/beast/http/message.hpp>
35 #include <boost/beast/ssl/ssl_stream.hpp>
36 #include <boost/optional.hpp>
37 #include <functional>
38 #include <memory>
39 #include <type_traits>
40 
41 namespace boost {
42 namespace asio {
43 namespace ssl {
44 class context;
45 }
46 } // namespace asio
47 } // namespace boost
48 
49 namespace ripple {
50 
53 {
54 protected:
55  using socket_type = boost::beast::tcp_stream;
56  using stream_type = boost::beast::ssl_stream<socket_type>;
57 
58  // VFALCO NOTE The requirement of this constructor is an
59  // unfortunate problem with the API for
60  // Stoppable and PropertyStream
61  //
62  Overlay(Stoppable& parent)
63  : Stoppable("Overlay", parent), beast::PropertyStream::Source("peers")
64  {
65  }
66 
67 public:
68  enum class Promote { automatic, never, always };
69 
70  struct Setup
71  {
72  explicit Setup() = default;
73 
76  int ipLimit = 0;
78  boost::optional<std::uint32_t> networkID;
79  bool vlEnabled = true;
80  };
81 
83 
84  virtual ~Overlay() = default;
85 
87  virtual Handoff
88  onHandoff(
90  http_request_type&& request,
91  boost::asio::ip::tcp::endpoint remote_address) = 0;
92 
97  virtual void
98  connect(beast::IP::Endpoint const& address) = 0;
99 
101  virtual int
102  limit() = 0;
103 
108  virtual std::size_t
109  size() const = 0;
110 
114  virtual Json::Value
115  json() = 0;
116 
120  virtual PeerSequence
121  getActivePeers() const = 0;
122 
126  virtual void
127  checkTracking(std::uint32_t index) = 0;
128 
130  virtual std::shared_ptr<Peer>
131  findPeerByShortID(Peer::id_t const& id) const = 0;
132 
134  virtual std::shared_ptr<Peer>
135  findPeerByPublicKey(PublicKey const& pubKey) = 0;
136 
138  virtual void
139  broadcast(protocol::TMProposeSet& m) = 0;
140 
142  virtual void
143  broadcast(protocol::TMValidation& m) = 0;
144 
151  virtual std::set<Peer::id_t>
152  relay(
153  protocol::TMProposeSet& m,
154  uint256 const& uid,
155  PublicKey const& validator) = 0;
156 
163  virtual std::set<Peer::id_t>
164  relay(
165  protocol::TMValidation& m,
166  uint256 const& uid,
167  PublicKey const& validator) = 0;
168 
176  template <class Function>
177  void
178  foreach(Function f) const
179  {
180  for (auto const& p : getActivePeers())
181  f(p);
182  }
183 
185  virtual void
186  incJqTransOverflow() = 0;
187  virtual std::uint64_t
188  getJqTransOverflow() const = 0;
189 
193  virtual void
194  incPeerDisconnect() = 0;
195  virtual std::uint64_t
196  getPeerDisconnect() const = 0;
197  virtual void
199  virtual std::uint64_t
200  getPeerDisconnectCharges() const = 0;
201 
207  virtual Json::Value
208  crawlShards(bool pubKey, std::uint32_t hops) = 0;
209 
218  virtual boost::optional<std::uint32_t>
219  networkID() const = 0;
220 };
221 
222 } // namespace ripple
223 
224 #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 >
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:342
ripple::Overlay::Setup::crawlOptions
std::uint32_t crawlOptions
Definition: Overlay.h:77
std::vector
STL class.
ripple::Overlay::Promote
Promote
Definition: Overlay.h:68
ripple::Overlay::size
virtual std::size_t size() const =0
Returns the number of active peers.
boost
Definition: IPAddress.h:117
ripple::Overlay::socket_type
boost::beast::tcp_stream socket_type
Definition: Overlay.h:55
ripple::Overlay::stream_type
boost::beast::ssl_stream< socket_type > stream_type
Definition: Overlay.h:56
ripple::Overlay::incPeerDisconnectCharges
virtual void incPeerDisconnectCharges()=0
ripple::Overlay::Promote::never
@ never
ripple::base_uint< 256 >
ripple::Overlay::broadcast
virtual void broadcast(protocol::TMProposeSet &m)=0
Broadcast a proposal.
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::Overlay::Setup::public_ip
beast::IP::Address public_ip
Definition: Overlay.h:75
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
ripple::Overlay::incJqTransOverflow
virtual void incJqTransOverflow()=0
Increment and retrieve counter for transaction job queue overflows.
ripple::Overlay::getActivePeers
virtual PeerSequence getActivePeers() const =0
Returns a sequence representing the current list of peers.
ripple::Overlay::relay
virtual std::set< Peer::id_t > relay(protocol::TMProposeSet &m, uint256 const &uid, PublicKey const &validator)=0
Relay a proposal.
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::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:76
ripple::Overlay::Overlay
Overlay(Stoppable &parent)
Definition: Overlay.h:62
ripple::Overlay::Setup::vlEnabled
bool vlEnabled
Definition: Overlay.h:79
beast::PropertyStream::Source::Source
Source(std::string const &name)
Definition: beast_PropertyStream.cpp:176
ripple::Overlay
Manages the set of connected peers.
Definition: Overlay.h:52
ripple::Overlay::json
virtual Json::Value json()=0
Return diagnostics on the status of all peers.
ripple::Overlay::Setup::networkID
boost::optional< std::uint32_t > networkID
Definition: Overlay.h:78
ripple::Handoff
Used to indicate the result of a server connection handoff.
Definition: Handoff.h:37
ripple::Overlay::Setup
Definition: Overlay.h:70
ripple::Overlay::findPeerByShortID
virtual std::shared_ptr< Peer > findPeerByShortID(Peer::id_t const &id) const =0
Returns the peer with the matching short id, or null.
ripple::Overlay::checkTracking
virtual void checkTracking(std::uint32_t index)=0
Calls the checkTracking function on each peer.
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:74
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
std::set
STL class.
Json::Value
Represents a JSON value.
Definition: json_value.h:145
beast
Definition: base_uint.h:585