Fabcoin Core  0.16.2
P2P Digital Currency
WebThree.cpp
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 #include "WebThree.h"
23 #include <boost/filesystem.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <libethereum/Defaults.h>
27 #include <libethereum/ClientTest.h>
29 #include "BuildInfo.h"
30 #include <libethashseal/Ethash.h>
31 using namespace std;
32 using namespace dev;
33 using namespace dev::p2p;
34 using namespace dev::eth;
35 using namespace dev::shh;
36 
37 static_assert(BOOST_VERSION == 106300, "Wrong boost headers version");
38 
39 WebThreeDirect::WebThreeDirect(
40  std::string const& _clientVersion,
41  std::string const& _dbPath,
42  eth::ChainParams const& _params,
43  WithExisting _we,
44  std::set<std::string> const& _interfaces,
45  NetworkPreferences const& _n,
46  bytesConstRef _network,
47  bool _testing
48 ):
49  m_clientVersion(_clientVersion),
50  m_net(_clientVersion, _n, _network)
51 {
52  if (_dbPath.size())
53  Defaults::setDBPath(_dbPath);
54  if (_interfaces.count("eth"))
55  {
56  Ethash::init();
57  NoProof::init();
58  if (_params.sealEngineName == "Ethash")
59  m_ethereum.reset(new eth::EthashClient(_params, (int)_params.u256Param("networkID"), &m_net, shared_ptr<GasPricer>(), _dbPath, _we));
60  else if (_params.sealEngineName == "NoProof" && _testing)
61  m_ethereum.reset(new eth::ClientTest(_params, (int)_params.u256Param("networkID"), &m_net, shared_ptr<GasPricer>(), _dbPath, _we));
62  else
63  m_ethereum.reset(new eth::Client(_params, (int)_params.u256Param("networkID"), &m_net, shared_ptr<GasPricer>(), _dbPath, _we));
64  string bp = DEV_QUOTED(ETH_BUILD_PLATFORM);
65  vector<string> bps;
66  boost::split(bps, bp, boost::is_any_of("/"));
67  bps[0] = bps[0].substr(0, 5);
68  bps[1] = bps[1].substr(0, 3);
69  bps.back() = bps.back().substr(0, 3);
70  m_ethereum->setExtraData(rlpList(0, string(dev::Version) + "++" + string(DEV_QUOTED(ETH_COMMIT_HASH)).substr(0, 4) + (ETH_CLEAN_REPO ? "-" : "*") + string(DEV_QUOTED(ETH_BUILD_TYPE)).substr(0, 1) + boost::join(bps, "/")));
71  }
72 
73  if (_interfaces.count("shh"))
74  m_whisper = m_net.registerCapability(make_shared<WhisperHost>());
75 }
76 
78 {
79  // Utterly horrible right now - WebThree owns everything (good), but:
80  // m_net (Host) owns the eth::EthereumHost via a shared_ptr.
81  // The eth::EthereumHost depends on eth::Client (it maintains a reference to the BlockChain field of Client).
82  // eth::Client (owned by us via a unique_ptr) uses eth::EthereumHost (via a weak_ptr).
83  // Really need to work out a clean way of organising ownership and guaranteeing startup/shutdown is perfect.
84 
85  // Have to call stop here to get the Host to kill its io_service otherwise we end up with left-over reads,
86  // still referencing Sessions getting deleted *after* m_ethereum is reset, causing bad things to happen, since
87  // the guarantee is that m_ethereum is only reset *after* all sessions have ended (sessions are allowed to
88  // use bits of data owned by m_ethereum).
89  m_net.stop();
90  m_ethereum.reset();
91 }
92 
93 std::string WebThreeDirect::composeClientVersion(std::string const& _client)
94 {
95  return _client + "/" + \
96  "v" + dev::Version + "/" + \
97  DEV_QUOTED(ETH_BUILD_OS) + "/" + \
98  DEV_QUOTED(ETH_BUILD_COMPILER) + "/" + \
99  DEV_QUOTED(ETH_BUILD_JIT_MODE) + "/" + \
100  DEV_QUOTED(ETH_BUILD_TYPE) + "/" + \
101  string(DEV_QUOTED(ETH_COMMIT_HASH)).substr(0, 8) + \
102  (ETH_CLEAN_REPO ? "" : "*") + "/";
103 }
104 
106 {
107  return m_net.networkPreferences();
108 }
109 
111 {
112  auto had = isNetworkStarted();
113  if (had)
114  stopNetwork();
115  m_net.setNetworkPreferences(_n, _dropPeers);
116  if (had)
117  startNetwork();
118 }
119 
120 std::vector<PeerSessionInfo> WebThreeDirect::peers()
121 {
122  return m_net.peerSessionInfo();
123 }
124 
126 {
127  return m_net.peerCount();
128 }
129 
131 {
132  return m_net.setIdealPeerCount(_n);
133 }
134 
136 {
137  return m_net.setPeerStretch(_n);
138 }
139 
141 {
142  return m_net.saveNetwork();
143 }
144 
145 void WebThreeDirect::addNode(NodeID const& _node, bi::tcp::endpoint const& _host)
146 {
147  m_net.addNode(_node, NodeIPEndpoint(_host.address(), _host.port(), _host.port()));
148 }
149 
150 void WebThreeDirect::requirePeer(NodeID const& _node, bi::tcp::endpoint const& _host)
151 {
152  m_net.requirePeer(_node, NodeIPEndpoint(_host.address(), _host.port(), _host.port()));
153 }
154 
156 {
157  m_net.addPeer(_s, _t);
158 }
159 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void setIdealPeerCount(unsigned _n)
Set ideal number of peers.
Definition: Host.h:183
static std::string composeClientVersion(std::string const &_client)
Definition: WebThree.cpp:93
~WebThreeDirect()
Destructor.
Definition: WebThree.cpp:77
void stopNetwork() override
Stop the network subsystem.
Definition: WebThree.h:207
void addPeer(NodeSpec const &_s, PeerType _t)
Add a potential peer.
Definition: Host.cpp:493
NetworkPreferences const & networkPreferences() const
Definition: Host.h:206
std::hash for asio::adress
Definition: Common.h:323
#define DEV_QUOTED(s)
Definition: Common.h:61
Definition: Eth.h:45
IPv4,UDP/TCP endpoints.
Definition: Common.h:175
void stop()
Stop network.
Definition: Host.cpp:139
size_t peerCount() const
Get number of peers connected.
Definition: Host.cpp:639
std::vector< p2p::PeerSessionInfo > peers() override
Get information on the current peer set.
Definition: WebThree.cpp:120
bytes saveNetwork() const
Serialise the set of known peers.
Definition: Host.cpp:811
std::unique_ptr< eth::Client > m_ethereum
Client for Ethereum ("eth") protocol.
Definition: WebThree.h:217
WithExisting
Definition: Common.h:310
size_t peerCount() const override
Same as peers().size(), but more efficient.
Definition: WebThree.cpp:125
p2p::NetworkPreferences const & networkPreferences() const override
Definition: WebThree.cpp:105
void requirePeer(p2p::NodeID const &_node, bi::tcp::endpoint const &_endpoint) override
Require connection to peer.
Definition: WebThree.cpp:150
std::weak_ptr< shh::WhisperHost > m_whisper
Client for Whisper ("shh") protocol.
Definition: WebThree.h:218
dev::bytes saveNetwork() override
Save peers.
Definition: WebThree.cpp:140
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
PeerSessionInfos peerSessionInfo() const
Get peer information.
Definition: Host.cpp:625
std::vector< byte > bytes
Definition: Common.h:75
void requirePeer(NodeID const &_node, NodeIPEndpoint const &_endpoint)
Create Peer and attempt keeping peer connected.
Definition: Host.cpp:517
Main API hub for interfacing with Ethereum.
Definition: Client.h:75
void setPeerStretch(unsigned _n)
Set multipier for max accepted connections.
Definition: Host.h:186
void setNetworkPreferences(p2p::NetworkPreferences const &_n, bool _dropPeers=false) override
Definition: WebThree.cpp:110
void addNode(NodeID const &_node, NodeIPEndpoint const &_endpoint)
Add node as a peer candidate. Node is added if discovery ping is successful and table has capacity...
Definition: Host.cpp:501
bytes rlpList()
Export a list of items in RLP format, returning a byte array.
Definition: RLP.h:470
bool isNetworkStarted() const override
Is network working? there may not be any peers yet.
Definition: WebThree.h:210
void setPeerStretch(size_t _n)
Experimental. Sets ceiling for incoming connections to multiple of ideal peer count.
Definition: WebThree.cpp:135
u256 u256Param(std::string const &_name) const
Convenience method to get an otherParam as a u256 int.
std::shared_ptr< T > registerCapability(std::shared_ptr< T > const &_t)
Register a peer-capability; all new peer connections will have this capability.
Definition: Host.h:160
PeerType
Definition: Common.h:166
std::string sealEngineName
The chain sealer name: e.g. Ethash, NoProof, BasicAuthority.
virtual void addNode(p2p::NodeID const &_node, bi::tcp::endpoint const &_hostEndpoint) override
Add node to connect to.
Definition: WebThree.cpp:145
char const * Version
Definition: Common.cpp:36
virtual void addPeer(p2p::NodeSpec const &_node, p2p::PeerType _t) override
Generalised peer addition.
Definition: WebThree.cpp:155
void setIdealPeerCount(size_t _n) override
Sets the ideal number of peers.
Definition: WebThree.cpp:130
void setNetworkPreferences(NetworkPreferences const &_p, bool _dropPeers=false)
Definition: Host.h:208
void startNetwork() override
Start the network subsystem.
Definition: WebThree.h:204