Ethereum  PoC-8
The C++ Implementation of Ethereum
BasicAuthority.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "BasicAuthority.h"
23 #include <libdevcore/CommonJS.h>
24 #include <libdevcore/Log.h>
25 #include <libethereum/Interface.h>
26 #include "Exceptions.h"
27 #include "BlockHeader.h"
28 using namespace std;
29 using namespace dev;
30 using namespace eth;
31 
32 
33 void BasicAuthority::init()
34 {
36 }
37 
38 StringHashMap BasicAuthority::jsInfo(BlockHeader const& _bi) const
39 {
40  return { { "sig", toJS(sig(_bi)) } };
41 }
42 
43 bool BasicAuthority::shouldSeal(Interface* _i)
44 {
45  return _i->pendingInfo().timestamp() + 5 <= utcTime() || (_i->pendingInfo().timestamp() <= utcTime() && !_i->pending().empty());
46 }
47 
48 void BasicAuthority::generateSeal(BlockHeader const& _bi)
49 {
50  BlockHeader bi = _bi;
51  h256 h = bi.hash(WithoutSeal);
52  Signature s = sign(m_secret, h);
53  setSig(bi, s);
54  RLPStream ret;
55  bi.streamRLP(ret);
56  if (m_onSealGenerated)
57  m_onSealGenerated(ret.out());
58 }
59 
60 bool BasicAuthority::onOptionChanging(std::string const& _name, bytes const& _value)
61 {
62  RLP rlp(_value);
63  if (_name == "authorities")
64  m_authorities = rlp.toUnorderedSet<Address>();
65  else if (_name == "authority")
66  m_secret = Secret(rlp.toHash<h256>());
67  else
68  return false;
69  return true;
70 }
71 
72 void BasicAuthority::populateFromParent(BlockHeader& _bi, BlockHeader const& _parent) const
73 {
74  SealEngineFace::populateFromParent(_bi, _parent);
75  // pseudo-random difficulty to facilitate fork reduction.
76  _bi.setDifficulty(fromBigEndian<uint32_t>(sha3(sha3(m_secret) ^ _bi.parentHash()).ref().cropped(0, 4)));
77 }
78 
79 void BasicAuthority::verify(Strictness _s, BlockHeader const& _bi, BlockHeader const& _parent, bytesConstRef _block) const
80 {
81  SealEngineFace::verify(_s, _bi, _parent, _block);
82  // check it hashes according to proof of work or that it's the genesis block.
83  Signature s = sig(_bi);
84  h256 h = _bi.hash(WithoutSeal);
85  Address a = toAddress(recover(s, h));
86  if (_s == CheckEverything && _bi.parentHash() && !m_authorities.count(a))
87  {
88  InvalidBlockNonce ex;
89  ex << errinfo_hash256(_bi.hash(WithoutSeal));
90  BOOST_THROW_EXCEPTION(ex);
91  }
92  else if (_s == QuickNonce && _bi.parentHash() && !SignatureStruct(sig(_bi)).isValid())
93  {
94  InvalidBlockNonce ex;
95  ex << errinfo_hash256(_bi.hash(WithoutSeal));
96  BOOST_THROW_EXCEPTION(ex);
97  }
98 }
dev::eth::BasicAuthority
Definition: BasicAuthority.h:35
dev::eth::BlockHeader::hash
h256 hash(IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:119
dev::vector_ref< byte const >
dev::eth::BlockHeader
Encapsulation of a block header. Class to contain all of a block header's data. It is able to parse a...
Definition: BlockHeader.h:97
dev::sha3
bool sha3(bytesConstRef _input, bytesRef o_output) noexcept
Definition: SHA3.cpp:28
BasicAuthority.h
dev::errinfo_hash256
boost::error_info< struct tag_hash, h256 > errinfo_hash256
Definition: Exceptions.h:89
dev::RLPStream::out
bytes const & out() const
Read the byte stream.
Definition: RLP.h:419
Exceptions.h
dev::eth::QuickNonce
@ QuickNonce
Definition: BlockHeader.h:49
dev::eth::Interface::pendingInfo
virtual BlockHeader pendingInfo() const
Definition: Interface.h:167
BlockHeader.h
dev::rlp
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:453
dev::utcTime
int64_t utcTime()
Get the current time in seconds since the epoch in UTC.
Definition: Common.cpp:53
CommonJS.h
dev::FixedHash< 32 >
dev::verify
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition: Common.cpp:273
dev::eth::Interface::pending
virtual Transactions pending() const =0
Interface.h
dev::eth::BlockHeader::parentHash
h256 const & parentHash() const
Definition: BlockHeader.h:157
dev::SignatureStruct
Definition: Common.h:51
dev::recover
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:221
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::eth::CheckEverything
@ CheckEverything
Definition: BlockHeader.h:48
dev::eth::BlockHeader::streamRLP
void streamRLP(RLPStream &_s, IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:139
dev::eth::Interface
Main API hub for interfacing with Ethereum.
Definition: Interface.h:68
dev::toAddress
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:105
dev::RLPStream
Class for writing to an RLP bytestream.
Definition: RLP.h:370
dev::sign
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:251
std
Definition: FixedHash.h:393
dev
Definition: Address.cpp:21
ETH_REGISTER_SEAL_ENGINE
#define ETH_REGISTER_SEAL_ENGINE(Name)
Definition: SealEngine.h:139
dev::Secret
SecureFixedHash< 32 > Secret
Definition: Common.h:36
dev::StringHashMap
std::unordered_map< std::string, std::string > StringHashMap
Definition: Common.h:139
dev::toJS
std::string toJS(byte _b)
Definition: CommonJS.h:28
dev::eth::Strictness
Strictness
Definition: BlockHeader.h:47
dev::RLP
Definition: RLP.h:48
dev::eth::BlockHeader::setDifficulty
void setDifficulty(u256 const &_v)
Definition: BlockHeader.h:153
Log.h
dev::eth::WithoutSeal
@ WithoutSeal
Definition: BlockHeader.h:41
dev::eth::BlockHeader::timestamp
int64_t timestamp() const
Definition: BlockHeader.h:160