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  checkSanity(std::uint32_t index) = 0;
128 
131  virtual void
132  check() = 0;
133 
135  virtual std::shared_ptr<Peer>
136  findPeerByShortID(Peer::id_t const& id) = 0;
137 
139  virtual std::shared_ptr<Peer>
140  findPeerByPublicKey(PublicKey const& pubKey) = 0;
141 
143  virtual void
144  broadcast(protocol::TMProposeSet& m) = 0;
145 
147  virtual void
148  broadcast(protocol::TMValidation& m) = 0;
149 
151  virtual void
152  relay(protocol::TMProposeSet& m, uint256 const& uid) = 0;
153 
155  virtual void
156  relay(protocol::TMValidation& m, uint256 const& uid) = 0;
157 
165  template <class Function>
166  void
167  foreach(Function f) const
168  {
169  for (auto const& p : getActivePeers())
170  f(p);
171  }
172 
174  virtual void
175  incJqTransOverflow() = 0;
176  virtual std::uint64_t
177  getJqTransOverflow() const = 0;
178 
182  virtual void
183  incPeerDisconnect() = 0;
184  virtual std::uint64_t
185  getPeerDisconnect() const = 0;
186  virtual void
188  virtual std::uint64_t
189  getPeerDisconnectCharges() const = 0;
190 
196  virtual Json::Value
197  crawlShards(bool pubKey, std::uint32_t hops) = 0;
198 
207  virtual boost::optional<std::uint32_t>
208  networkID() const = 0;
209 };
210 
211 } // namespace ripple
212 
213 #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:200
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::relay
virtual void relay(protocol::TMProposeSet &m, uint256 const &uid)=0
Relay a proposal.
ripple::Overlay::checkSanity
virtual void checkSanity(std::uint32_t index)=0
Calls the checkSanity function on each peer.
ripple::Overlay::getActivePeers
virtual PeerSequence getActivePeers() const =0
Returns a sequence representing the current list of 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::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
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
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.
Json::Value
Represents a JSON value.
Definition: json_value.h:145
beast
Definition: base_uint.h:646