SealEngine.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 SealEngine.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. *
  18. * Determines the PoW algorithm.
  19. */
  20. #pragma once
  21. #include <functional>
  22. #include <unordered_map>
  23. #include <libdevcore/Guards.h>
  24. #include <libdevcore/RLP.h>
  25. #include "BlockHeader.h"
  26. #include "Common.h"
  27. namespace dev
  28. {
  29. namespace eth
  30. {
  31. class BlockHeader;
  32. struct ChainOperationParams;
  33. class Interface;
  34. class PrecompiledFace;
  35. class TransactionBase;
  36. class EnvInfo;
  37. class SealEngineFace
  38. {
  39. public:
  40. virtual ~SealEngineFace() {}
  41. virtual std::string name() const = 0;
  42. virtual unsigned revision() const { return 0; }
  43. virtual unsigned sealFields() const { return 0; }
  44. virtual bytes sealRLP() const { return bytes(); }
  45. virtual StringHashMap jsInfo(BlockHeader const&) const { return StringHashMap(); }
  46. /// Don't forget to call Super::verify when subclassing & overriding.
  47. virtual void verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent = BlockHeader(), bytesConstRef _block = bytesConstRef()) const;
  48. /// Additional verification for transactions in blocks.
  49. virtual void verifyTransaction(ImportRequirements::value _ir, TransactionBase const& _t, BlockHeader const& _bi) const;
  50. /// Don't forget to call Super::populateFromParent when subclassing & overriding.
  51. virtual void populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const;
  52. bytes option(std::string const& _name) const { Guard l(x_options); return m_options.count(_name) ? m_options.at(_name) : bytes(); }
  53. bool setOption(std::string const& _name, bytes const& _value) { Guard l(x_options); try { if (onOptionChanging(_name, _value)) { m_options[_name] = _value; return true; } } catch (...) {} return false; }
  54. virtual strings sealers() const { return { "default" }; }
  55. virtual std::string sealer() const { return "default"; }
  56. virtual void setSealer(std::string const&) {}
  57. virtual bool shouldSeal(Interface*) { return true; }
  58. virtual void generateSeal(BlockHeader const& _bi) = 0;
  59. virtual void onSealGenerated(std::function<void(bytes const& s)> const& _f) = 0;
  60. virtual void cancelGeneration() {}
  61. ChainOperationParams const& chainParams() const { return m_params; }
  62. void setChainParams(ChainOperationParams const& _params) { m_params = _params; }
  63. SealEngineFace* withChainParams(ChainOperationParams const& _params) { setChainParams(_params); return this; }
  64. virtual EVMSchedule const& evmSchedule(EnvInfo const&) const { return DefaultSchedule; }
  65. virtual bool isPrecompiled(Address const& _a) const { return m_params.precompiled.count(_a); }
  66. virtual bigint costOfPrecompiled(Address const& _a, bytesConstRef _in) const { return m_params.precompiled.at(_a).cost(_in); }
  67. virtual void executePrecompiled(Address const& _a, bytesConstRef _in, bytesRef _out) const { return m_params.precompiled.at(_a).execute(_in, _out); }
  68. protected:
  69. virtual bool onOptionChanging(std::string const&, bytes const&) { return true; }
  70. void injectOption(std::string const& _name, bytes const& _value) { Guard l(x_options); m_options[_name] = _value; }
  71. private:
  72. mutable Mutex x_options;
  73. std::unordered_map<std::string, bytes> m_options;
  74. ChainOperationParams m_params;
  75. };
  76. class SealEngineBase: public SealEngineFace
  77. {
  78. public:
  79. void generateSeal(BlockHeader const& _bi) override
  80. {
  81. RLPStream ret;
  82. _bi.streamRLP(ret);
  83. if (m_onSealGenerated)
  84. m_onSealGenerated(ret.out());
  85. }
  86. void onSealGenerated(std::function<void(bytes const&)> const& _f) override { m_onSealGenerated = _f; }
  87. private:
  88. std::function<void(bytes const& s)> m_onSealGenerated;
  89. };
  90. using SealEngineFactory = std::function<SealEngineFace*()>;
  91. class SealEngineRegistrar
  92. {
  93. public:
  94. /// Creates the seal engine and uses it to "polish" the params (i.e. fill in implicit values) as necessary. Use this rather than the other two
  95. /// unless you *know* that the params contain all information regarding the seal on the Genesis block.
  96. static SealEngineFace* create(ChainOperationParams const& _params);
  97. static SealEngineFace* create(std::string const& _name) { if (!get()->m_sealEngines.count(_name)) return nullptr; return get()->m_sealEngines[_name](); }
  98. template <class SealEngine> static SealEngineFactory registerSealEngine(std::string const& _name) { return (get()->m_sealEngines[_name] = [](){return new SealEngine;}); }
  99. static void unregisterSealEngine(std::string const& _name) { get()->m_sealEngines.erase(_name); }
  100. private:
  101. static SealEngineRegistrar* get() { if (!s_this) s_this = new SealEngineRegistrar; return s_this; }
  102. std::unordered_map<std::string, SealEngineFactory> m_sealEngines;
  103. static SealEngineRegistrar* s_this;
  104. };
  105. #define ETH_REGISTER_SEAL_ENGINE(Name) static SealEngineFactory __eth_registerSealEngineFactory ## Name = SealEngineRegistrar::registerSealEngine<Name>(#Name)
  106. class NoProof: public eth::SealEngineBase
  107. {
  108. public:
  109. std::string name() const override { return "NoProof"; }
  110. static void init();
  111. };
  112. }
  113. }