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