WebThree.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file WebThree.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "WebThree.h"
  19. #include <chrono>
  20. #include <thread>
  21. #include <boost/filesystem.hpp>
  22. #include <boost/algorithm/string.hpp>
  23. #include <libdevcore/Log.h>
  24. #include <libethereum/Defaults.h>
  25. #include <libethereum/EthereumHost.h>
  26. #include <libwhisper/WhisperHost.h>
  27. #include <libethereum/ClientTest.h>
  28. #include <libethashseal/EthashClient.h>
  29. #include "cpp-ethereum/BuildInfo.h"
  30. #include <libethashseal/Ethash.h>
  31. #include "Swarm.h"
  32. #include "Support.h"
  33. using namespace std;
  34. using namespace dev;
  35. using namespace dev::p2p;
  36. using namespace dev::eth;
  37. using namespace dev::shh;
  38. WebThreeDirect::WebThreeDirect(
  39. std::string const& _clientVersion,
  40. std::string const& _dbPath,
  41. eth::ChainParams const& _params,
  42. WithExisting _we,
  43. std::set<std::string> const& _interfaces,
  44. NetworkPreferences const& _n,
  45. bytesConstRef _network,
  46. bool _testing
  47. ):
  48. m_clientVersion(_clientVersion),
  49. m_net(_clientVersion, _n, _network)
  50. {
  51. if (_dbPath.size())
  52. Defaults::setDBPath(_dbPath);
  53. if (_interfaces.count("eth"))
  54. {
  55. Ethash::init();
  56. NoProof::init();
  57. if (_params.sealEngineName == "Ethash")
  58. m_ethereum.reset(new eth::EthashClient(_params, (int)_params.u256Param("networkID"), &m_net, shared_ptr<GasPricer>(), _dbPath, _we));
  59. else if (_params.sealEngineName == "NoProof" && _testing)
  60. m_ethereum.reset(new eth::ClientTest(_params, (int)_params.u256Param("networkID"), &m_net, shared_ptr<GasPricer>(), _dbPath, _we));
  61. else
  62. m_ethereum.reset(new eth::Client(_params, (int)_params.u256Param("networkID"), &m_net, shared_ptr<GasPricer>(), _dbPath, _we));
  63. string bp = DEV_QUOTED(ETH_BUILD_PLATFORM);
  64. vector<string> bps;
  65. boost::split(bps, bp, boost::is_any_of("/"));
  66. bps[0] = bps[0].substr(0, 5);
  67. bps[1] = bps[1].substr(0, 3);
  68. bps.back() = bps.back().substr(0, 3);
  69. 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, "/")));
  70. }
  71. if (_interfaces.count("shh"))
  72. m_whisper = m_net.registerCapability(make_shared<WhisperHost>());
  73. if (_interfaces.count("bzz"))
  74. {
  75. m_swarm.reset(new bzz::Client(this));
  76. }
  77. m_support = make_shared<Support>(this);
  78. }
  79. WebThreeDirect::~WebThreeDirect()
  80. {
  81. // Utterly horrible right now - WebThree owns everything (good), but:
  82. // m_net (Host) owns the eth::EthereumHost via a shared_ptr.
  83. // The eth::EthereumHost depends on eth::Client (it maintains a reference to the BlockChain field of Client).
  84. // eth::Client (owned by us via a unique_ptr) uses eth::EthereumHost (via a weak_ptr).
  85. // Really need to work out a clean way of organising ownership and guaranteeing startup/shutdown is perfect.
  86. // Have to call stop here to get the Host to kill its io_service otherwise we end up with left-over reads,
  87. // still referencing Sessions getting deleted *after* m_ethereum is reset, causing bad things to happen, since
  88. // the guarantee is that m_ethereum is only reset *after* all sessions have ended (sessions are allowed to
  89. // use bits of data owned by m_ethereum).
  90. m_net.stop();
  91. m_ethereum.reset();
  92. }
  93. bzz::Interface* WebThreeDirect::swarm() const
  94. {
  95. if (!m_swarm)
  96. BOOST_THROW_EXCEPTION(InterfaceNotSupported("bzz"));
  97. return m_swarm.get();
  98. }
  99. std::string WebThreeDirect::composeClientVersion(std::string const& _client)
  100. {
  101. return _client + "/" + \
  102. "v" + dev::Version + "/" + \
  103. DEV_QUOTED(ETH_BUILD_OS) + "/" + \
  104. DEV_QUOTED(ETH_BUILD_COMPILER) + "/" + \
  105. DEV_QUOTED(ETH_BUILD_JIT_MODE) + "/" + \
  106. DEV_QUOTED(ETH_BUILD_TYPE) + "/" + \
  107. string(DEV_QUOTED(ETH_COMMIT_HASH)).substr(0, 8) + \
  108. (ETH_CLEAN_REPO ? "" : "*") + "/";
  109. }
  110. p2p::NetworkPreferences const& WebThreeDirect::networkPreferences() const
  111. {
  112. return m_net.networkPreferences();
  113. }
  114. void WebThreeDirect::setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers)
  115. {
  116. auto had = isNetworkStarted();
  117. if (had)
  118. stopNetwork();
  119. m_net.setNetworkPreferences(_n, _dropPeers);
  120. if (had)
  121. startNetwork();
  122. }
  123. std::vector<PeerSessionInfo> WebThreeDirect::peers()
  124. {
  125. return m_net.peerSessionInfo();
  126. }
  127. size_t WebThreeDirect::peerCount() const
  128. {
  129. return m_net.peerCount();
  130. }
  131. void WebThreeDirect::setIdealPeerCount(size_t _n)
  132. {
  133. return m_net.setIdealPeerCount(_n);
  134. }
  135. void WebThreeDirect::setPeerStretch(size_t _n)
  136. {
  137. return m_net.setPeerStretch(_n);
  138. }
  139. bytes WebThreeDirect::saveNetwork()
  140. {
  141. return m_net.saveNetwork();
  142. }
  143. void WebThreeDirect::addNode(NodeID const& _node, bi::tcp::endpoint const& _host)
  144. {
  145. m_net.addNode(_node, NodeIPEndpoint(_host.address(), _host.port(), _host.port()));
  146. }
  147. void WebThreeDirect::requirePeer(NodeID const& _node, bi::tcp::endpoint const& _host)
  148. {
  149. m_net.requirePeer(_node, NodeIPEndpoint(_host.address(), _host.port(), _host.port()));
  150. }
  151. void WebThreeDirect::addPeer(NodeSpec const& _s, PeerType _t)
  152. {
  153. m_net.addPeer(_s, _t);
  154. }