BasicAuthority.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 BasicAuthority.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "BasicAuthority.h"
  19. #include <libdevcore/CommonJS.h>
  20. #include <libdevcore/Log.h>
  21. #include <libethereum/Interface.h>
  22. #include "Exceptions.h"
  23. #include "BlockHeader.h"
  24. using namespace std;
  25. using namespace dev;
  26. using namespace eth;
  27. void BasicAuthority::init()
  28. {
  29. ETH_REGISTER_SEAL_ENGINE(BasicAuthority);
  30. }
  31. StringHashMap BasicAuthority::jsInfo(BlockHeader const& _bi) const
  32. {
  33. return { { "sig", toJS(sig(_bi)) } };
  34. }
  35. bool BasicAuthority::shouldSeal(Interface* _i)
  36. {
  37. // cdebug << "Comparing: " << _i->pendingInfo().timestamp() << " to " << utcTime();
  38. return _i->pendingInfo().timestamp() + 5 <= utcTime() || (_i->pendingInfo().timestamp() <= utcTime() && !_i->pending().empty());
  39. }
  40. void BasicAuthority::generateSeal(BlockHeader const& _bi)
  41. {
  42. BlockHeader bi = _bi;
  43. h256 h = bi.hash(WithoutSeal);
  44. Signature s = sign(m_secret, h);
  45. setSig(bi, s);
  46. SealEngineBase::generateSeal(bi);
  47. }
  48. bool BasicAuthority::onOptionChanging(std::string const& _name, bytes const& _value)
  49. {
  50. RLP rlp(_value);
  51. if (_name == "authorities")
  52. m_authorities = rlp.toUnorderedSet<Address>();
  53. else if (_name == "authority")
  54. m_secret = Secret(rlp.toHash<h256>());
  55. else
  56. return false;
  57. return true;
  58. }
  59. void BasicAuthority::populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const
  60. {
  61. SealEngineFace::populateFromParent(_bi, _parent);
  62. // pseudo-random difficulty to facilitate fork reduction.
  63. _bi.setDifficulty(fromBigEndian<uint32_t>(sha3(sha3(m_secret) ^ _bi.parentHash()).ref().cropped(0, 4)));
  64. }
  65. void BasicAuthority::verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent, bytesConstRef _block) const
  66. {
  67. SealEngineFace::verify(_s, _bi, _parent, _block);
  68. // check it hashes according to proof of work or that it's the genesis block.
  69. Signature s = sig(_bi);
  70. h256 h = _bi.hash(WithoutSeal);
  71. Address a = toAddress(recover(s, h));
  72. if (_s == CheckEverything && _bi.parentHash() && !m_authorities.count(a))
  73. {
  74. InvalidBlockNonce ex;
  75. ex << errinfo_hash256(_bi.hash(WithoutSeal));
  76. BOOST_THROW_EXCEPTION(ex);
  77. }
  78. else if (_s == QuickNonce && _bi.parentHash() && !SignatureStruct(sig(_bi)).isValid())
  79. {
  80. InvalidBlockNonce ex;
  81. ex << errinfo_hash256(_bi.hash(WithoutSeal));
  82. BOOST_THROW_EXCEPTION(ex);
  83. }
  84. }