EthashClient.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 EthashClient.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2015
  17. */
  18. #include "EthashClient.h"
  19. #include "Ethash.h"
  20. using namespace std;
  21. using namespace dev;
  22. using namespace dev::eth;
  23. using namespace p2p;
  24. EthashClient& dev::eth::asEthashClient(Interface& _c)
  25. {
  26. if (dynamic_cast<Ethash*>(_c.sealEngine()))
  27. return dynamic_cast<EthashClient&>(_c);
  28. throw InvalidSealEngine();
  29. }
  30. EthashClient* dev::eth::asEthashClient(Interface* _c)
  31. {
  32. if (dynamic_cast<Ethash*>(_c->sealEngine()))
  33. return &dynamic_cast<EthashClient&>(*_c);
  34. throw InvalidSealEngine();
  35. }
  36. DEV_SIMPLE_EXCEPTION(ChainParamsNotEthash);
  37. EthashClient::EthashClient(
  38. ChainParams const& _params,
  39. int _networkID,
  40. p2p::Host* _host,
  41. std::shared_ptr<GasPricer> _gpForAdoption,
  42. std::string const& _dbPath,
  43. WithExisting _forceAction,
  44. TransactionQueue::Limits const& _limits
  45. ):
  46. Client(_params, _networkID, _host, _gpForAdoption, _dbPath, _forceAction, _limits)
  47. {
  48. // will throw if we're not an Ethash seal engine.
  49. asEthashClient(*this);
  50. }
  51. Ethash* EthashClient::ethash() const
  52. {
  53. return dynamic_cast<Ethash*>(Client::sealEngine());
  54. }
  55. bool EthashClient::isMining() const
  56. {
  57. return ethash()->farm().isMining();
  58. }
  59. WorkingProgress EthashClient::miningProgress() const
  60. {
  61. if (isMining())
  62. return ethash()->farm().miningProgress();
  63. return WorkingProgress();
  64. }
  65. u256 EthashClient::hashrate() const
  66. {
  67. u256 r = externalHashrate();
  68. if (isMining())
  69. r += miningProgress().rate();
  70. return r;
  71. }
  72. std::tuple<h256, h256, h256> EthashClient::getEthashWork()
  73. {
  74. // lock the work so a later submission isn't invalidated by processing a transaction elsewhere.
  75. // this will be reset as soon as a new block arrives, allowing more transactions to be processed.
  76. bool oldShould = shouldServeWork();
  77. m_lastGetWork = chrono::system_clock::now();
  78. if (!sealEngine()->shouldSeal(this))
  79. return std::tuple<h256, h256, h256>();
  80. // if this request has made us bother to serve work, prep it now.
  81. if (!oldShould && shouldServeWork())
  82. onPostStateChanged();
  83. else
  84. // otherwise, set this to true so that it gets prepped next time.
  85. m_remoteWorking = true;
  86. ethash()->manuallySetWork(m_sealingInfo);
  87. return std::tuple<h256, h256, h256>(m_sealingInfo.hash(WithoutSeal), Ethash::seedHash(m_sealingInfo), Ethash::boundary(m_sealingInfo));
  88. }
  89. bool EthashClient::submitEthashWork(h256 const& _mixHash, h64 const& _nonce)
  90. {
  91. ethash()->manuallySubmitWork(_mixHash, _nonce);
  92. return true;
  93. }
  94. void EthashClient::setShouldPrecomputeDAG(bool _precompute)
  95. {
  96. bytes trueBytes {1};
  97. bytes falseBytes {0};
  98. sealEngine()->setOption("precomputeDAG", _precompute ? trueBytes: falseBytes);
  99. }
  100. void EthashClient::submitExternalHashrate(u256 const& _rate, h256 const& _id)
  101. {
  102. WriteGuard x_externalRates;
  103. m_externalRates[_id] = make_pair(_rate, chrono::steady_clock::now());
  104. }
  105. u256 EthashClient::externalHashrate() const
  106. {
  107. u256 ret = 0;
  108. WriteGuard x_externalRates;
  109. for (auto i = m_externalRates.begin(); i != m_externalRates.end();)
  110. if (chrono::steady_clock::now() - i->second.second > chrono::seconds(5))
  111. i = m_externalRates.erase(i);
  112. else
  113. ret += i++->second.first;
  114. return ret;
  115. }