Ethash.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Ethash.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. *
  18. * A proof of work algorithm.
  19. */
  20. #pragma once
  21. #include <libethcore/SealEngine.h>
  22. #include <libethereum/GenericFarm.h>
  23. #include "EthashProofOfWork.h"
  24. namespace dev
  25. {
  26. namespace eth
  27. {
  28. class Ethash: public SealEngineFace
  29. {
  30. public:
  31. Ethash();
  32. std::string name() const override { return "Ethash"; }
  33. unsigned revision() const override { return 1; }
  34. unsigned sealFields() const override { return 2; }
  35. bytes sealRLP() const override { return rlp(h256()) + rlp(Nonce()); }
  36. StringHashMap jsInfo(BlockHeader const& _bi) const override;
  37. void verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent, bytesConstRef _block) const override;
  38. void verifyTransaction(ImportRequirements::value _ir, TransactionBase const& _t, BlockHeader const& _bi) const override;
  39. void populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const override;
  40. strings sealers() const override;
  41. std::string sealer() const override { return m_sealer; }
  42. void setSealer(std::string const& _sealer) override { m_sealer = _sealer; }
  43. void cancelGeneration() override { m_farm.stop(); }
  44. void generateSeal(BlockHeader const& _bi) override;
  45. void onSealGenerated(std::function<void(bytes const&)> const& _f) override;
  46. bool shouldSeal(Interface* _i) override;
  47. eth::GenericFarm<EthashProofOfWork>& farm() { return m_farm; }
  48. enum { MixHashField = 0, NonceField = 1 };
  49. static h256 seedHash(BlockHeader const& _bi);
  50. static Nonce nonce(BlockHeader const& _bi) { return _bi.seal<Nonce>(NonceField); }
  51. static h256 mixHash(BlockHeader const& _bi) { return _bi.seal<h256>(MixHashField); }
  52. static h256 boundary(BlockHeader const& _bi) { auto d = _bi.difficulty(); return d ? (h256)u256((bigint(1) << 256) / d) : h256(); }
  53. static BlockHeader& setNonce(BlockHeader& _bi, Nonce _v) { _bi.setSeal(NonceField, _v); return _bi; }
  54. static BlockHeader& setMixHash(BlockHeader& _bi, h256 const& _v) { _bi.setSeal(MixHashField, _v); return _bi; }
  55. u256 calculateDifficulty(BlockHeader const& _bi, BlockHeader const& _parent) const;
  56. u256 childGasLimit(BlockHeader const& _bi, u256 const& _gasFloorTarget = Invalid256) const;
  57. virtual EVMSchedule const& evmSchedule(EnvInfo const&) const override;
  58. void manuallySetWork(BlockHeader const& _work) { m_sealing = _work; }
  59. void manuallySubmitWork(h256 const& _mixHash, Nonce _nonce);
  60. static void ensurePrecomputed(unsigned _number);
  61. static void init();
  62. private:
  63. bool verifySeal(BlockHeader const& _bi) const;
  64. bool quickVerifySeal(BlockHeader const& _bi) const;
  65. eth::GenericFarm<EthashProofOfWork> m_farm;
  66. std::string m_sealer = "cpu";
  67. BlockHeader m_sealing;
  68. std::function<void(bytes const&)> m_onSealGenerated;
  69. };
  70. }
  71. }