EthashClient.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <tuple>
  20. #include <libethereum/Client.h>
  21. namespace dev
  22. {
  23. namespace eth
  24. {
  25. class Ethash;
  26. DEV_SIMPLE_EXCEPTION(InvalidSealEngine);
  27. class EthashClient: public Client
  28. {
  29. public:
  30. /// Trivial forwarding constructor.
  31. EthashClient(
  32. ChainParams const& _params,
  33. int _networkID,
  34. p2p::Host* _host,
  35. std::shared_ptr<GasPricer> _gpForAdoption,
  36. std::string const& _dbPath = std::string(),
  37. WithExisting _forceAction = WithExisting::Trust,
  38. TransactionQueue::Limits const& _l = TransactionQueue::Limits{1024, 1024}
  39. );
  40. Ethash* ethash() const;
  41. /// Enable/disable precomputing of the DAG for next epoch
  42. void setShouldPrecomputeDAG(bool _precompute);
  43. /// Are we mining now?
  44. bool isMining() const;
  45. /// The hashrate...
  46. u256 hashrate() const;
  47. /// Check the progress of the mining.
  48. WorkingProgress miningProgress() const;
  49. /// @returns true only if it's worth bothering to prep the mining block.
  50. bool shouldServeWork() const { return m_bq.items().first == 0 && (isMining() || remoteActive()); }
  51. /// Update to the latest transactions and get hash of the current block to be mined minus the
  52. /// nonce (the 'work hash') and the difficulty to be met.
  53. /// @returns Tuple of hash without seal, seed hash, target boundary.
  54. std::tuple<h256, h256, h256> getEthashWork();
  55. /** @brief Submit the proof for the proof-of-work.
  56. * @param _s A valid solution.
  57. * @return true if the solution was indeed valid and accepted.
  58. */
  59. bool submitEthashWork(h256 const& _mixHash, h64 const& _nonce);
  60. void submitExternalHashrate(u256 const& _rate, h256 const& _id);
  61. protected:
  62. u256 externalHashrate() const;
  63. // external hashrate
  64. mutable std::unordered_map<h256, std::pair<u256, std::chrono::steady_clock::time_point>> m_externalRates;
  65. mutable SharedMutex x_externalRates;
  66. };
  67. EthashClient& asEthashClient(Interface& _c);
  68. EthashClient* asEthashClient(Interface* _c);
  69. }
  70. }