Ethereum  PoC-8
The C++ Implementation of Ethereum
WebThree.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <thread>
25 #include <mutex>
26 #include <list>
27 #include <atomic>
28 #include <boost/asio.hpp> // Make sure boost/asio.hpp is included before windows.h.
29 #include <boost/utility.hpp>
30 #include <libdevcore/Common.h>
31 #include <libdevcore/CommonIO.h>
32 #include <libdevcore/Guards.h>
33 #include <libdevcore/Exceptions.h>
34 #include <libp2p/Host.h>
35 #include <libethereum/Client.h>
37 
38 namespace dev
39 {
40 
42 {
43  Active = 0,
45  Deleted
46 };
47 
48 namespace eth { class Interface; }
49 namespace shh { class Interface; }
50 namespace bzz { class Interface; class Client; }
51 
53 {
54 public:
55  virtual ~NetworkFace() = default;
56 
58  virtual p2p::NodeInfo nodeInfo() const = 0;
59 
61  virtual std::vector<p2p::PeerSessionInfo> peers() = 0;
62 
64  virtual size_t peerCount() const = 0;
65 
67  virtual void addPeer(p2p::NodeSpec const& _node, p2p::PeerType _t) = 0;
68 
70  virtual void addNode(p2p::NodeID const& _node, bi::tcp::endpoint const& _hostEndpoint) = 0;
71 
73  virtual void requirePeer(p2p::NodeID const& _node, bi::tcp::endpoint const& _endpoint) = 0;
74 
76  virtual dev::bytes saveNetwork() = 0;
77 
79  virtual void setIdealPeerCount(size_t _n) = 0;
80 
82  virtual u256 networkId() const = 0;
83 
85  virtual void startNetwork() = 0;
86 
88  virtual void stopNetwork() = 0;
89 
91  virtual bool isNetworkStarted() const = 0;
92 
94  virtual std::string enode() const = 0;
95 };
96 
97 
109 {
110 public:
113  WebThreeDirect(std::string const& _clientVersion, boost::filesystem::path const& _dbPath,
114  boost::filesystem::path const& _snapshotPath, eth::ChainParams const& _params,
115  WithExisting _we = WithExisting::Trust, p2p::NetworkConfig const& _n = p2p::NetworkConfig{},
116  bytesConstRef _network = bytesConstRef(), bool _testing = false);
117 
119  ~WebThreeDirect() override;
120 
121  // The mainline interfaces:
122 
124  {
125  if (!m_ethereum)
126  BOOST_THROW_EXCEPTION(InterfaceNotSupported() << errinfo_interface("eth"));
127  return m_ethereum.get();
128  }
129 
130  // Misc stuff:
131 
132  static std::string composeClientVersion(std::string const& _client);
133  std::string const& clientVersion() const { return m_clientVersion; }
134 
135  // Network stuff:
136 
138  std::vector<p2p::PeerSessionInfo> peers() override;
139 
141  size_t peerCount() const override;
142 
144  virtual void addPeer(p2p::NodeSpec const& _node, p2p::PeerType _t) override;
145 
147  virtual void addNode(p2p::NodeID const& _node, bi::tcp::endpoint const& _hostEndpoint) override;
148 
150  void addNode(p2p::NodeID const& _node, std::string const& _hostString) { addNode(_node, p2p::Network::resolveHost(_hostString)); }
151 
153  void addNode(bi::tcp::endpoint const& _endpoint) { addNode(p2p::NodeID(), _endpoint); }
154 
156  void addNode(std::string const& _hostString) { addNode(p2p::NodeID(), _hostString); }
157 
159  void requirePeer(p2p::NodeID const& _node, bi::tcp::endpoint const& _endpoint) override;
160 
162  void requirePeer(p2p::NodeID const& _node, std::string const& _hostString) { requirePeer(_node, p2p::Network::resolveHost(_hostString)); }
163 
165  dev::bytes saveNetwork() override;
166 
168  void setIdealPeerCount(size_t _n) override;
169 
171  void setPeerStretch(size_t _n);
172 
173  p2p::NodeInfo nodeInfo() const override { return m_net.nodeInfo(); }
174 
175  u256 networkId() const override { return m_ethereum.get()->networkId(); }
176 
177  std::string enode() const override { return m_net.enode(); }
178 
180  void startNetwork() override { m_net.start(); }
181 
183  void stopNetwork() override { m_net.stop(); }
184 
186  bool isNetworkStarted() const override { return m_net.isStarted(); }
187 
188 private:
189  std::string m_clientVersion;
190 
191  std::unique_ptr<eth::Client> m_ethereum;
192 
193  p2p::Host m_net;
194 };
195 
196 
197 }
dev::NetworkFace::stopNetwork
virtual void stopNetwork()=0
Stop the network subsystem.
dev::WebThreeDirect::setIdealPeerCount
void setIdealPeerCount(size_t _n) override
Sets the ideal number of peers.
Definition: WebThree.cpp:91
dev::Deleting
@ Deleting
Definition: WebThree.h:44
dev::NetworkFace::requirePeer
virtual void requirePeer(p2p::NodeID const &_node, bi::tcp::endpoint const &_endpoint)=0
Require connection to peer.
dev::WebThreeDirect::~WebThreeDirect
~WebThreeDirect() override
Destructor.
Definition: WebThree.cpp:59
dev::vector_ref< byte const >
dev::WebThreeDirect::composeClientVersion
static std::string composeClientVersion(std::string const &_client)
Definition: WebThree.cpp:74
dev::WebThreeDirect::networkId
u256 networkId() const override
Get network id.
Definition: WebThree.h:175
dev::eth::ChainParams
Definition: ChainParams.h:38
dev::WebThreeDirect::ethereum
eth::Client * ethereum() const
Definition: WebThree.h:123
dev::NetworkFace::networkId
virtual u256 networkId() const =0
Get network id.
dev::WebThreeDirect::setPeerStretch
void setPeerStretch(size_t _n)
Experimental. Sets ceiling for incoming connections to multiple of ideal peer count.
Definition: WebThree.cpp:96
dev::WebThreeDirect::enode
std::string enode() const override
Get enode string.
Definition: WebThree.h:177
dev::NetworkFace::enode
virtual std::string enode() const =0
Get enode string.
dev::WebThreeDirect::peerCount
size_t peerCount() const override
Same as peers().size(), but more efficient.
Definition: WebThree.cpp:86
dev::Deleted
@ Deleted
Definition: WebThree.h:45
dev::NetworkFace
Definition: WebThree.h:53
dev::WithExisting
WithExisting
Definition: Common.h:292
dev::WithExisting::Trust
@ Trust
dev::WebThreeDirect
Main API hub for interfacing with Web 3 components. This doesn't do any local multiplexing,...
Definition: WebThree.h:109
CommonIO.h
dev::WebThreeDirect::addNode
void addNode(bi::tcp::endpoint const &_endpoint)
Add node to connect to.
Definition: WebThree.h:153
Common.h
dev::WebThreeDirect::WebThreeDirect
WebThreeDirect(std::string const &_clientVersion, boost::filesystem::path const &_dbPath, boost::filesystem::path const &_snapshotPath, eth::ChainParams const &_params, WithExisting _we=WithExisting::Trust, p2p::NetworkConfig const &_n=p2p::NetworkConfig{}, bytesConstRef _network=bytesConstRef(), bool _testing=false)
Definition: WebThree.cpp:37
dev::Active
@ Active
Definition: WebThree.h:43
dev::WebThreeDirect::addNode
void addNode(std::string const &_hostString)
Add node to connect to.
Definition: WebThree.h:156
dev::eth::Client
Main API hub for interfacing with Ethereum.
Definition: Client.h:77
Client.h
dev::WebThreeDirect::stopNetwork
void stopNetwork() override
Stop the network subsystem.
Definition: WebThree.h:183
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::eth::NodeID
p2p::NodeID NodeID
Definition: CommonNet.h:105
dev::NetworkFace::nodeInfo
virtual p2p::NodeInfo nodeInfo() const =0
Get information concerning this node.
dev::bytesConstRef
vector_ref< byte const > bytesConstRef
Definition: Common.h:74
Exceptions.h
dev::WebThreeDirect::requirePeer
void requirePeer(p2p::NodeID const &_node, bi::tcp::endpoint const &_endpoint) override
Require connection to peer.
Definition: WebThree.cpp:111
dev::WorkState
WorkState
Definition: WebThree.h:42
dev::NetworkFace::setIdealPeerCount
virtual void setIdealPeerCount(size_t _n)=0
Sets the ideal number of peers.
dev::NetworkFace::saveNetwork
virtual dev::bytes saveNetwork()=0
Save peers.
dev::NetworkFace::addNode
virtual void addNode(p2p::NodeID const &_node, bi::tcp::endpoint const &_hostEndpoint)=0
Add node to connect to.
dev::WebThreeDirect::addNode
virtual void addNode(p2p::NodeID const &_node, bi::tcp::endpoint const &_hostEndpoint) override
Add node to connect to.
Definition: WebThree.cpp:106
ChainParams.h
dev::NetworkFace::peers
virtual std::vector< p2p::PeerSessionInfo > peers()=0
Get information on the current peer set.
dev::NetworkFace::~NetworkFace
virtual ~NetworkFace()=default
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev::WebThreeDirect::peers
std::vector< p2p::PeerSessionInfo > peers() override
Get information on the current peer set.
Definition: WebThree.cpp:81
dev::NetworkFace::addPeer
virtual void addPeer(p2p::NodeSpec const &_node, p2p::PeerType _t)=0
Generalised peer addition.
dev::WebThreeDirect::isNetworkStarted
bool isNetworkStarted() const override
Is network working? there may not be any peers yet.
Definition: WebThree.h:186
dev::WebThreeDirect::addPeer
virtual void addPeer(p2p::NodeSpec const &_node, p2p::PeerType _t) override
Generalised peer addition.
Definition: WebThree.cpp:116
dev::NetworkFace::isNetworkStarted
virtual bool isNetworkStarted() const =0
Is network working? there may not be any peers yet.
dev::WebThreeDirect::requirePeer
void requirePeer(p2p::NodeID const &_node, std::string const &_hostString)
Require connection to peer.
Definition: WebThree.h:162
dev
Definition: Address.cpp:21
dev::WebThreeDirect::startNetwork
void startNetwork() override
Start the network subsystem.
Definition: WebThree.h:180
Guards.h
dev::errinfo_interface
boost::error_info< struct tag_interface, std::string > errinfo_interface
Definition: Exceptions.h:95
dev::WebThreeDirect::saveNetwork
dev::bytes saveNetwork() override
Save peers.
Definition: WebThree.cpp:101
dev::WebThreeDirect::addNode
void addNode(p2p::NodeID const &_node, std::string const &_hostString)
Add node to connect to.
Definition: WebThree.h:150
dev::WebThreeDirect::clientVersion
std::string const & clientVersion() const
Definition: WebThree.h:133
dev::NetworkFace::startNetwork
virtual void startNetwork()=0
Start the network subsystem.
dev::WebThreeDirect::nodeInfo
p2p::NodeInfo nodeInfo() const override
Get information concerning this node.
Definition: WebThree.h:173
dev::NetworkFace::peerCount
virtual size_t peerCount() const =0
Same as peers().size(), but more efficient.