EthashAux.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 EthashAux.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <condition_variable>
  20. #include <libethash/ethash.h>
  21. #include <libdevcore/Log.h>
  22. #include <libdevcore/Worker.h>
  23. #include "EthashProofOfWork.h"
  24. #include "Ethash.h"
  25. namespace dev
  26. {
  27. namespace eth
  28. {
  29. struct DAGChannel: public LogChannel { static const char* name(); static const int verbosity = 1; };
  30. class BlockHeader;
  31. class EthashAux
  32. {
  33. public:
  34. ~EthashAux();
  35. static EthashAux* get();
  36. struct LightAllocation
  37. {
  38. LightAllocation(h256 const& _seedHash);
  39. ~LightAllocation();
  40. bytesConstRef data() const;
  41. EthashProofOfWork::Result compute(h256 const& _headerHash, Nonce const& _nonce) const;
  42. ethash_light_t light;
  43. uint64_t size;
  44. };
  45. struct FullAllocation
  46. {
  47. FullAllocation(ethash_light_t _light, ethash_callback_t _cb);
  48. ~FullAllocation();
  49. EthashProofOfWork::Result compute(h256 const& _headerHash, Nonce const& _nonce) const;
  50. bytesConstRef data() const;
  51. uint64_t size() const { return ethash_full_dag_size(full); }
  52. ethash_full_t full;
  53. };
  54. using LightType = std::shared_ptr<LightAllocation>;
  55. using FullType = std::shared_ptr<FullAllocation>;
  56. static h256 seedHash(unsigned _number);
  57. static uint64_t number(h256 const& _seedHash);
  58. static uint64_t cacheSize(BlockHeader const& _header);
  59. static uint64_t dataSize(uint64_t _blockNumber);
  60. static LightType light(h256 const& _seedHash);
  61. static const uint64_t NotGenerating = (uint64_t)-1;
  62. /// Kicks off generation of DAG for @a _seedHash and @returns false or @returns true if ready.
  63. static unsigned computeFull(h256 const& _seedHash, bool _createIfMissing = true);
  64. /// Information on the generation progress.
  65. static std::pair<uint64_t, unsigned> fullGeneratingProgress() { return std::make_pair(get()->m_generatingFullNumber, get()->m_fullProgress); }
  66. /// Kicks off generation of DAG for @a _blocknumber and blocks until ready; @returns result or empty pointer if not existing and _createIfMissing is false.
  67. static FullType full(h256 const& _seedHash, bool _createIfMissing = false, std::function<int(unsigned)> const& _f = std::function<int(unsigned)>());
  68. static EthashProofOfWork::Result eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce);
  69. private:
  70. EthashAux() {}
  71. /// Kicks off generation of DAG for @a _blocknumber and blocks until ready; @returns result.
  72. void killCache(h256 const& _s);
  73. static EthashAux* s_this;
  74. SharedMutex x_lights;
  75. std::unordered_map<h256, std::shared_ptr<LightAllocation>> m_lights;
  76. Mutex x_fulls;
  77. std::condition_variable m_fullsChanged;
  78. std::unordered_map<h256, std::weak_ptr<FullAllocation>> m_fulls;
  79. FullType m_lastUsedFull;
  80. std::unique_ptr<std::thread> m_fullGenerator;
  81. uint64_t m_generatingFullNumber = NotGenerating;
  82. unsigned m_fullProgress;
  83. Mutex x_epochs;
  84. std::unordered_map<h256, unsigned> m_epochs;
  85. h256s m_seedHashes;
  86. };
  87. }
  88. }