Fabcoin Core  0.16.2
P2P Digital Currency
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 <libwhisper/WhisperHost.h>
36 #include <libethereum/Client.h>
38 
39 namespace dev
40 {
41 
43 {
44  Active = 0,
47 };
48 
49 namespace eth { class Interface; }
50 namespace shh { class Interface; }
51 namespace bzz { class Interface; class Client; }
52 
53 class Support;
54 
56 {
57 public:
59  virtual p2p::NodeInfo nodeInfo() const = 0;
60 
62  virtual std::vector<p2p::PeerSessionInfo> peers() = 0;
63 
65  virtual size_t peerCount() const = 0;
66 
68  virtual void addPeer(p2p::NodeSpec const& _node, p2p::PeerType _t) = 0;
69 
71  virtual void addNode(p2p::NodeID const& _node, bi::tcp::endpoint const& _hostEndpoint) = 0;
72 
74  virtual void requirePeer(p2p::NodeID const& _node, bi::tcp::endpoint const& _endpoint) = 0;
75 
77  virtual dev::bytes saveNetwork() = 0;
78 
80  virtual void setIdealPeerCount(size_t _n) = 0;
81 
82  virtual bool haveNetwork() const = 0;
83 
84  virtual p2p::NetworkPreferences const& networkPreferences() const = 0;
85  virtual void setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers) = 0;
86 
87  virtual p2p::NodeID id() const = 0;
88 
90  virtual u256 networkId() const = 0;
91 
93  virtual p2p::Peers nodes() const = 0;
94 
96  virtual void startNetwork() = 0;
97 
99  virtual void stopNetwork() = 0;
100 
102  virtual bool isNetworkStarted() const = 0;
103 
105  virtual std::string enode() const = 0;
106 };
107 
108 
120 {
121 public:
125  std::string const& _clientVersion,
126  std::string const& _dbPath,
127  eth::ChainParams const& _params,
129  std::set<std::string> const& _interfaces = {"eth", "shh", "bzz"},
131  bytesConstRef _network = bytesConstRef(),
132  bool _testing = false
133  );
134 
136  ~WebThreeDirect();
137 
138  // The mainline interfaces:
139 
140  eth::Client* ethereum() const { if (!m_ethereum) BOOST_THROW_EXCEPTION(InterfaceNotSupported("eth")); return m_ethereum.get(); }
141  std::shared_ptr<shh::WhisperHost> whisper() const { auto w = m_whisper.lock(); if (!w) BOOST_THROW_EXCEPTION(InterfaceNotSupported("shh")); return w; }
142 
143  // Misc stuff:
144 
145  static std::string composeClientVersion(std::string const& _client);
146  std::string const& clientVersion() const { return m_clientVersion; }
147 
148  // Network stuff:
149 
151  std::vector<p2p::PeerSessionInfo> peers() override;
152 
154  size_t peerCount() const override;
155 
157  virtual void addPeer(p2p::NodeSpec const& _node, p2p::PeerType _t) override;
158 
160  virtual void addNode(p2p::NodeID const& _node, bi::tcp::endpoint const& _hostEndpoint) override;
161 
163  void addNode(p2p::NodeID const& _node, std::string const& _hostString) { addNode(_node, p2p::Network::resolveHost(_hostString)); }
164 
166  void addNode(bi::tcp::endpoint const& _endpoint) { addNode(p2p::NodeID(), _endpoint); }
167 
169  void addNode(std::string const& _hostString) { addNode(p2p::NodeID(), _hostString); }
170 
172  void requirePeer(p2p::NodeID const& _node, bi::tcp::endpoint const& _endpoint) override;
173 
175  void requirePeer(p2p::NodeID const& _node, std::string const& _hostString) { requirePeer(_node, p2p::Network::resolveHost(_hostString)); }
176 
178  dev::bytes saveNetwork() override;
179 
181  void setIdealPeerCount(size_t _n) override;
182 
184  void setPeerStretch(size_t _n);
185 
186  bool haveNetwork() const override { return m_net.haveNetwork(); }
187 
188  p2p::NetworkPreferences const& networkPreferences() const override;
189 
190  void setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers = false) override;
191 
192  p2p::NodeInfo nodeInfo() const override { return m_net.nodeInfo(); }
193 
194  p2p::NodeID id() const override { return m_net.id(); }
195 
196  u256 networkId() const override { return m_ethereum.get()->networkId(); }
197 
198  std::string enode() const override { return m_net.enode(); }
199 
201  p2p::Peers nodes() const override { return m_net.getPeers(); }
202 
204  void startNetwork() override { m_net.start(); }
205 
207  void stopNetwork() override { m_net.stop(); }
208 
210  bool isNetworkStarted() const override { return m_net.isStarted(); }
211 
212 private:
213  std::string m_clientVersion;
214 
216 
217  std::unique_ptr<eth::Client> m_ethereum;
218  std::weak_ptr<shh::WhisperHost> m_whisper;
219 };
220 
221 // TODO, probably move into libdevrpc:
222 
223 class RPCSlave {};
224 class RPCMaster {};
225 
226 // TODO, probably move into eth:
227 
229 {
230 public:
232 
233  // TODO: implement all of the virtuals with the RLPClient link.
234 };
235 
237 {
238 public:
240 
241  // TODO: implement the master-end of whatever the RLPClient link will send over.
242 };
243 
244 // TODO, probably move into shh:
245 
247 {
248 public:
250 
251  // TODO: implement all of the virtuals with the RLPClient link.
252 };
253 
255 {
256 public:
258 
259  // TODO: implement the master-end of whatever the RLPClient link will send over.
260 };
261 
268 class WebThree
269 {
270 public:
272  WebThree();
273 
275  ~WebThree();
276 
277  // The mainline interfaces.
278 
279  eth::Interface* ethereum() const { if (!m_ethereum) BOOST_THROW_EXCEPTION(InterfaceNotSupported("eth")); return m_ethereum; }
280  shh::Interface* whisper() const { if (!m_whisper) BOOST_THROW_EXCEPTION(InterfaceNotSupported("shh")); return m_whisper; }
281  bzz::Interface* swarm() const { BOOST_THROW_EXCEPTION(InterfaceNotSupported("bzz")); }
282 
283  // Peer network stuff - forward through RPCSlave, probably with P2PNetworkSlave/Master classes like Whisper & Ethereum.
284 
286  std::vector<p2p::PeerSessionInfo> peers();
287 
289  size_t peerCount() const;
290 
292  void connect(std::string const& _seedHost, unsigned short _port = 30303);
293 
295  bool haveNetwork();
296 
298  dev::bytes savePeers();
299 
301  void restorePeers(bytesConstRef _saved);
302 
303 private:
304  EthereumSlave* m_ethereum = nullptr;
305  WhisperSlave* m_whisper = nullptr;
306 
307  // TODO:
309 };
310 
311 }
EthereumSlave(RPCSlave *)
Definition: WebThree.h:231
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string const & clientVersion() const
Definition: WebThree.h:146
void stopNetwork() override
Stop the network subsystem.
Definition: WebThree.h:207
p2p::NodeID id() const override
Definition: WebThree.h:194
eth::Client * ethereum() const
Definition: WebThree.h:140
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition: Host.h:129
void requirePeer(p2p::NodeID const &_node, std::string const &_hostString)
Require connection to peer.
Definition: WebThree.h:175
Main API hub for interfacing with Web 3 components.
Definition: WebThree.h:119
WorkState
Definition: WebThree.h:42
u256 networkId() const override
Get network id.
Definition: WebThree.h:196
std::string m_clientVersion
Our end-application client&#39;s name/version.
Definition: WebThree.h:213
Main API hub for interfacing with Web 3 components.
Definition: WebThree.h:268
std::unique_ptr< eth::Client > m_ethereum
Client for Ethereum ("eth") protocol.
Definition: WebThree.h:217
WithExisting
Definition: Common.h:310
RPCSlave m_rpcSlave
Definition: WebThree.h:308
EthereumMaster(RPCMaster *)
Definition: WebThree.h:239
std::vector< Peer > Peers
Definition: Peer.h:99
bzz::Interface * swarm() const
Definition: WebThree.h:281
std::shared_ptr< shh::WhisperHost > whisper() const
Definition: WebThree.h:141
std::weak_ptr< shh::WhisperHost > m_whisper
Client for Whisper ("shh") protocol.
Definition: WebThree.h:218
bool haveNetwork() const override
Definition: WebThree.h:186
p2p::Host m_net
Should run in background and send us events when blocks found and allow us to send blocks as required...
Definition: WebThree.h:215
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
WhisperSlave(RPCSlave *)
Definition: WebThree.h:249
const char * networkId
p2p::NodeInfo nodeInfo() const override
Get information concerning this node.
Definition: WebThree.h:192
Main API hub for interfacing with Ethereum.
Definition: Client.h:75
p2p::Peers nodes() const override
Gets the nodes.
Definition: WebThree.h:201
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
void addNode(std::string const &_hostString)
Add node to connect to.
Definition: WebThree.h:169
bool isNetworkStarted() const override
Is network working? there may not be any peers yet.
Definition: WebThree.h:210
static bi::tcp::endpoint resolveHost(std::string const &_host)
Resolve "host:port" string as TCP endpoint. Returns unspecified endpoint on failure.
Definition: Network.cpp:216
eth::Interface * ethereum() const
Definition: WebThree.h:279
PeerType
Definition: Common.h:166
std::string enode() const override
Get enode string.
Definition: WebThree.h:198
void addNode(p2p::NodeID const &_node, std::string const &_hostString)
Add node to connect to.
Definition: WebThree.h:163
WhisperMaster(RPCMaster *)
Definition: WebThree.h:257
shh::Interface * whisper() const
Definition: WebThree.h:280
void addNode(bi::tcp::endpoint const &_endpoint)
Add node to connect to.
Definition: WebThree.h:166
void startNetwork() override
Start the network subsystem.
Definition: WebThree.h:204