Ethereum  PoC-8
The C++ Implementation of Ethereum
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 */
17 
18 #include "WebThree.h"
19 
20 #include <libethashseal/Ethash.h>
21 #include <libethashseal/EthashClient.h>
22 #include <libethereum/ClientTest.h>
24 
25 #include <aleth/buildinfo.h>
26 
27 #include <boost/filesystem.hpp>
28 #include <boost/algorithm/string.hpp>
29 
30 using namespace std;
31 using namespace dev;
32 using namespace dev::p2p;
33 using namespace dev::eth;
34 using namespace dev::shh;
35 
36 static_assert(BOOST_VERSION >= 106400, "Wrong boost headers version");
37 
38 WebThreeDirect::WebThreeDirect(std::string const& _clientVersion,
39  boost::filesystem::path const& _dbPath, boost::filesystem::path const& _snapshotPath,
40  eth::ChainParams const& _params, WithExisting _we, NetworkConfig const& _n,
41  bytesConstRef _network, bool _testing)
42  : m_clientVersion(_clientVersion), m_net(_clientVersion, _n, _network)
43 {
44  if (_testing)
45  m_ethereum.reset(new eth::ClientTest(
46  _params, (int)_params.networkID, m_net, shared_ptr<GasPricer>(), _dbPath, _we));
47  else
48  {
49  if (_params.sealEngineName == Ethash::name())
50  m_ethereum.reset(new eth::EthashClient(_params, (int)_params.networkID, m_net,
51  shared_ptr<GasPricer>(), _dbPath, _snapshotPath, _we));
52  else if (_params.sealEngineName == NoProof::name())
53  m_ethereum.reset(new eth::Client(_params, (int)_params.networkID, m_net,
54  shared_ptr<GasPricer>(), _dbPath, _snapshotPath, _we));
55  else
56  BOOST_THROW_EXCEPTION(ChainParamsInvalid() << errinfo_comment(
57  "Unknown seal engine: " + _params.sealEngineName));
58  }
59  m_ethereum->startWorking();
60 
61  const auto* buildinfo = aleth_get_buildinfo();
62  m_ethereum->setExtraData(rlpList(0, string{buildinfo->project_version}.substr(0, 5) + "++" +
63  string{buildinfo->git_commit_hash}.substr(0, 4) +
64  string{buildinfo->build_type}.substr(0, 1) +
65  string{buildinfo->system_name}.substr(0, 5) +
66  string{buildinfo->compiler_id}.substr(0, 3)));
67 }
68 
70 {
71  // Utterly horrible right now - WebThree owns everything (good), but:
72  // m_net (Host) owns the eth::EthereumHost via a shared_ptr.
73  // The eth::EthereumHost depends on eth::Client (it maintains a reference to the BlockChain field of Client).
74  // eth::Client (owned by us via a unique_ptr) uses eth::EthereumHost (via a weak_ptr).
75  // Really need to work out a clean way of organising ownership and guaranteeing startup/shutdown is perfect.
76 
77  // Have to call stop here to get the Host to kill its io_service otherwise we end up with left-over reads,
78  // still referencing Sessions getting deleted *after* m_ethereum is reset, causing bad things to happen, since
79  // the guarantee is that m_ethereum is only reset *after* all sessions have ended (sessions are allowed to
80  // use bits of data owned by m_ethereum).
81  m_net.stop();
82 }
83 
84 std::string WebThreeDirect::composeClientVersion(std::string const& _client)
85 {
86  const auto* buildinfo = aleth_get_buildinfo();
87  return _client + "/" + buildinfo->project_version + "/" + buildinfo->system_name + "/" +
88  buildinfo->compiler_id + buildinfo->compiler_version + "/" + buildinfo->build_type;
89 }
90 
91 std::vector<PeerSessionInfo> WebThreeDirect::peers()
92 {
93  return m_net.peerSessionInfo();
94 }
95 
97 {
98  return m_net.peerCount();
99 }
100 
102 {
103  return m_net.setIdealPeerCount(_n);
104 }
105 
107 {
108  return m_net.setPeerStretch(_n);
109 }
110 
112 {
113  return m_net.saveNetwork();
114 }
115 
116 void WebThreeDirect::addNode(p2p::NodeID const& _node, bi::tcp::endpoint const& _host)
117 {
118  m_net.addNode(_node, NodeIPEndpoint(_host.address(), _host.port(), _host.port()));
119 }
120 
121 void WebThreeDirect::requirePeer(p2p::NodeID const& _node, bi::tcp::endpoint const& _host)
122 {
123  m_net.requirePeer(_node, NodeIPEndpoint(_host.address(), _host.port(), _host.port()));
124 }
125 
126 void WebThreeDirect::addPeer(NodeSpec const& _s, PeerType _t)
127 {
128  m_net.addPeer(_s, _t);
129 }
130 
Definition: Address.cpp:20
static std::string composeClientVersion(std::string const &_client)
Definition: WebThree.cpp:84
Definition: FixedHash.h:390
std::vector< p2p::PeerSessionInfo > peers() override
Get information on the current peer set.
Definition: WebThree.cpp:91
WithExisting
Definition: Common.h:291
const char * name
Definition: VMFactory.cpp:49
size_t peerCount() const override
Same as peers().size(), but more efficient.
Definition: WebThree.cpp:96
void requirePeer(p2p::NodeID const &_node, bi::tcp::endpoint const &_endpoint) override
Require connection to peer.
Definition: WebThree.cpp:121
dev::bytes saveNetwork() override
Save peers.
Definition: WebThree.cpp:111
p2p::NodeID NodeID
Definition: CommonNet.h:105
~WebThreeDirect() override
Destructor.
Definition: WebThree.cpp:69
std::vector< byte > bytes
Definition: Common.h:72
Main API hub for interfacing with Ethereum.
Definition: Client.h:76
bytes rlpList()
Export a list of items in RLP format, returning a byte array.
Definition: RLP.h:456
void setPeerStretch(size_t _n)
Experimental. Sets ceiling for incoming connections to multiple of ideal peer count.
Definition: WebThree.cpp:106
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:116
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69
virtual void addPeer(p2p::NodeSpec const &_node, p2p::PeerType _t) override
Generalised peer addition.
Definition: WebThree.cpp:126
void setIdealPeerCount(size_t _n) override
Sets the ideal number of peers.
Definition: WebThree.cpp:101